2 #ifndef OPENGM_PYTHON_FUNCTION_INCL_HXX
3 #define OPENGM_PYTHON_FUNCTION_INCL_HXX
13 #include <boost/python.hpp>
14 #include <boost/python/stl_iterator.hpp>
24 template<
class T,
class I ,
class L>
41 gilEnsure_(other.gilEnsure_),
42 functionObj_(other.functionObj_),
43 labelVector_(other.labelVector_),
44 coordinateBuffer_(other.coordinateBuffer_),
51 PythonFunction(boost::python::object functionObj,boost::python::object shapeObj,
const bool gilEnsure=
true)
52 :gilEnsure_(gilEnsure),
53 functionObj_(functionObj),
58 stl_input_iterator<int> shapeBegin(shapeObj), shapeEnd;
59 shape_.assign(shapeBegin,shapeEnd);
60 labelVector_.resize(shape_.size());
62 for(
size_t d=0;d<shape_.size();++d){
71 gilEnsure_=other.gilEnsure_;
72 coordinateBuffer_=other.coordinateBuffer_;
74 functionObj_=other.functionObj_;
76 labelVector_=other.labelVector_;
89 LabelType
shape(
const size_t i)
const{
99 template<
class ITERATOR>
101 std::copy(labeling,labeling+shape_.size(),labelVector_.begin());
102 ValueType returnValue;
104 PyGILState_STATE gstate;
105 gstate = PyGILState_Ensure ();
106 returnValue = boost::python::extract<ValueType> ( functionObj_( labelVector_) );
107 PyGILState_Release (gstate);
110 returnValue = boost::python::extract<ValueType> ( functionObj_( labelVector_) );
117 boost::python::object functionObj_;
118 mutable std::vector<LabelType> labelVector_;
120 mutable LabelType * coordinateBuffer_;
121 std::vector<LabelType> shape_;
131 template<
class T,
class I,
class L>
137 throw RuntimeError(
"Cannot save/load gm with a pure python function: Pure python function cannot be serialized / deserialized");
141 throw RuntimeError(
"Cannot save/load gm with a pure python function: Pure Python Function cannot be serialized / deserialized");
144 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
146 throw RuntimeError(
"Cannot save/load gm with a pure python function: Pure Python Function cannot be serialized / deserialized");
148 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
150 throw RuntimeError(
" Cannot save/load gm with a pure python function: Pure Python Function cannot be serialized / deserialized");
157 struct FunctionRegistration;
160 template<
class T,
class I,
class L>
161 struct FunctionRegistration<python::PythonFunction<T, I, L> > {
Fallback implementation of member functions of OpenGM functions.
static void serialize(const python::PythonFunction< T, I, L > &, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR)
static size_t valueSequenceSize(const python::PythonFunction< T, I, L > &)
python::PythonFunction< T, I, L >::ValueType ValueType
PythonFunction & operator=(const PythonFunction &other)
static size_t indexSequenceSize(const python::PythonFunction< T, I, L > &)
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
LabelType shape(const size_t i) const
PythonFunction(boost::python::object functionObj, boost::python::object shapeObj, const bool gilEnsure=true)
static void deserialize(INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, python::PythonFunction< T, I, L > &)
ValueType operator()(ITERATOR labeling) const
PythonFunction(const PythonFunction &other)