OpenGM  2.3.x
Discrete Graphical Model Library
pythonfunction.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_PYTHON_FUNCTION_INCL_HXX
3 #define OPENGM_PYTHON_FUNCTION_INCL_HXX
4 
5 #include <algorithm>
6 #include <vector>
7 #include <cmath>
8 
9 #include "opengm/opengm.hxx"
12 
13 #include <boost/python.hpp>
14 #include <boost/python/stl_iterator.hpp>
15 //#include "../export_typedes.hxx"
16 //#include "../converter.hxx"
17 
18 
19 namespace opengm{
20 namespace python{
21 
22 using namespace boost::python;
23 
24 template<class T, class I , class L>
26 : public opengm::FunctionBase<PythonFunction<T, I, L>, T, I, L>
27 {
28 public:
29  typedef T ValueType;
30  typedef L LabelType;
31  typedef I IndexType;
32 
33  //empty
35 
36  }
37 
38  // copy constructor
40  :
41  gilEnsure_(other.gilEnsure_),
42  functionObj_(other.functionObj_),
43  labelVector_(other.labelVector_),
44  coordinateBuffer_(other.coordinateBuffer_),
45  shape_(other.shape_),
46  size_(other.size_){
47 
48  }
49 
50  // regular constructor
51  PythonFunction(boost::python::object functionObj,boost::python::object shapeObj,const bool gilEnsure=true)
52  :gilEnsure_(gilEnsure),
53  functionObj_(functionObj),
54  labelVector_(),
55  coordinateBuffer_(),
56  shape_(),
57  size_(){
58  stl_input_iterator<int> shapeBegin(shapeObj), shapeEnd;
59  shape_.assign(shapeBegin,shapeEnd);
60  labelVector_.resize(shape_.size());
61  size_=1;
62  for(size_t d=0;d<shape_.size();++d){
63  size_*=shape_[d];
64  }
65  //std::cout<<"construct donen\n";
66  }
67 
68  // assigment operator
70  if(&other!=this){
71  gilEnsure_=other.gilEnsure_;
72  coordinateBuffer_=other.coordinateBuffer_;
73  shape_=other.shape_;
74  functionObj_=other.functionObj_;
75  size_=other.size_;
76  labelVector_=other.labelVector_;
77  }
78  return *this;
79  }
80 
81  // destructor
83  if(shape_.size()!=0){
84  //delete numpyCoordinates_;
85  // delete[] coordinateBuffer_;
86  }
87  }
88 
89  LabelType shape(const size_t i) const{
90  return shape_[i];
91  }
92  size_t size() const{
93  return size_;
94  }
95  size_t dimension() const{
96  return shape_.size();
97  }
98 
99  template<class ITERATOR>
100  ValueType operator()(ITERATOR labeling) const{
101  std::copy(labeling,labeling+shape_.size(),labelVector_.begin());
102  ValueType returnValue;
103  if(gilEnsure_){
104  PyGILState_STATE gstate;
105  gstate = PyGILState_Ensure ();
106  returnValue = boost::python::extract<ValueType> ( functionObj_( labelVector_) );
107  PyGILState_Release (gstate);
108  }
109  else{
110  returnValue = boost::python::extract<ValueType> ( functionObj_( labelVector_) );
111  }
112  return returnValue;
113  }
114 
115 private:
116  bool gilEnsure_;
117  boost::python::object functionObj_;
118  mutable std::vector<LabelType> labelVector_;
119  //mutable boost::python::numeric::array numpyCoordinates_;
120  mutable LabelType * coordinateBuffer_;
121  std::vector<LabelType> shape_;
122  size_t size_;
123 
124 
125 };
126 
127 }
128 }
129 
130 namespace opengm{
131  template<class T,class I,class L>
132  class FunctionSerialization<python::PythonFunction<T, I, L> >{
133  public:
135 
137  throw RuntimeError("Cannot save/load gm with a pure python function: Pure python function cannot be serialized / deserialized");
138  return 0;
139  }
141  throw RuntimeError("Cannot save/load gm with a pure python function: Pure Python Function cannot be serialized / deserialized");
142  return 0;
143  }
144  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR>
145  static void serialize(const python::PythonFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR){
146  throw RuntimeError("Cannot save/load gm with a pure python function: Pure Python Function cannot be serialized / deserialized");
147  }
148  template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR>
149  static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, python::PythonFunction<T, I, L>&){
150  throw RuntimeError(" Cannot save/load gm with a pure python function: Pure Python Function cannot be serialized / deserialized");
151  }
152  };
153 }
154 
155 namespace opengm{
156  template<class F>
157  struct FunctionRegistration;
160  template<class T, class I, class L>
161  struct FunctionRegistration<python::PythonFunction<T, I, L> > {
162  enum ID {
164  };
165  };
166 }
167 
168 
169 
170 #endif
The OpenGM namespace.
Definition: config.hxx:43
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
OpenGM runtime error.
Definition: opengm.hxx:100
PythonFunction(const PythonFunction &other)