/**
 * @param mapping A Python dictionary instance
 * @return A new C++ PropertyManager instance
 */
boost::shared_ptr<Kernel::PropertyManager>
createPropertyManager(const boost::python::dict &mapping) {
  auto pmgr = boost::make_shared<PropertyManager>();
#if PY_MAJOR_VERSION >= 3
  object view(mapping.attr("items")());
  object itemIter(handle<>(PyObject_GetIter(view.ptr())));
#else
  object itemIter(mapping.attr("iteritems")());
#endif
  auto length = len(mapping);
  for (ssize_t i = 0; i < length; ++i) {
    const object keyValue(handle<>(PyIter_Next(itemIter.ptr())));
    const std::string cppkey = extract<std::string>(keyValue[0])();
    pmgr->declareProperty(PropertyWithValueFactory::create(cppkey, keyValue[1],
                                                           Direction::Input));
  }
  return pmgr;
}
Exemple #2
0
void add_mrfenergy_class(py::dict cls_dict, py::object key, const std::string& suffix)
{
    typedef typename T::LocalSize LocalSize;
    typedef typename T::NodeData NodeData;
    typedef typename T::EdgeData EdgeData;
    
    // NodeId is a pointer to a private struct Node. These lines
    // mask NodeId as an integer value (numeric_pointer).
    typedef numeric_pointer (MRFEnergy<T>::*add_node_type)(LocalSize, NodeData);
    typedef void (MRFEnergy<T>::*add_edge_type)(numeric_pointer, numeric_pointer, EdgeData);
    
    // Export the MRFEnergy class for the given T.
    py::object c = py::class_<MRFEnergy<T> >(("MRFEnergy"+suffix).c_str(), "MRF energy representation.",
            py::init<typename T::GlobalSize>())
        .def("add_node", (add_node_type)(&MRFEnergy<T>::AddNode),
            (py::arg("local_size"), py::arg("node_data")))
        .def("add_edge", (add_edge_type)(&MRFEnergy<T>::AddEdge),
            (py::arg("nodeid1"), py::arg("nodeid2"), py::arg("edge_data")))
        .def("minimize_trw", &minimize_trw<T>,
            "Minimize the energy of the MRF using TRW.",
            (py::arg("self"), py::arg("eps")=-1, py::arg("itermax")=50,
                py::arg("printiter")=5, py::arg("printminiter")=0, py::arg("details")=false))
        .def("minimize_bp", &minimize_bp<T>,
            "Minimize the energy of the MRF using BP.",
            (py::arg("self"), py::arg("eps")=-1, py::arg("itermax")=50,
                py::arg("printiter")=5, py::arg("printminiter")=0, py::arg("details")=false))
        .def("zero_messages", &MRFEnergy<T>::ZeroMessages)
        .def("set_automatic_ordering", &MRFEnergy<T>::SetAutomaticOrdering)
        .def("add_grid_nodes", &add_grid_nodes<T>,
            (py::arg("unary_terms"), py::arg("axis")=0))
        .def("add_grid_edges", &add_grid_edges<T>,
            (py::arg("nodeids"), py::arg("edge_data")))
        .def("add_grid_edges_direction", &add_grid_edges_direction<T>,
            (py::arg("nodeis"), py::arg("edge_data"), py::arg("direction")))
        .def("add_grid_edges_direction_local", &add_grid_edges_direction_local<T>,
            (py::arg("nodeis"), py::arg("edge_data"), py::arg("direction"), py::arg("axis")=0))
        .def("get_solution", &get_solution<T>,
            (py::arg("nodeids")));
    
    cls_dict.attr("__setitem__")(key, c);
}