OpenGM  2.3.x
Discrete Graphical Model Library
numpyview.hxx
Go to the documentation of this file.
1 #ifndef NUMPYVIEW_INCL_HXX
2 #define NUMPYVIEW_INCL_HXX
3 
4 #include <boost/python.hpp>
5 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
6 #include <numpy/noprefix.h>
7 #ifdef Bool
8 #undef Bool
9 #endif
10 #include <stddef.h>
12 
13 //template <typename T>
14 //inline PyArray_TYPES typeEnumFromType(void);
15 
16 namespace opengm{
17 namespace python{
18 
19 using namespace boost::python;
20 
21 //template<class T, size_t DIM>
22 //class NumpyViewAccessor1d;
23 
24 template<class V,size_t DIM=0>
25 class NumpyView{
26 public:
27  typedef V ValueType;
28  typedef V * CastPtrType;
29  typedef int const * ShapePtrType;
32  typedef size_t const * ShapeIteratorType;
33 
34  NumpyView( ):allocFromCpp_(false){
35  }
36  NumpyView( boost::python::object obj):allocFromCpp_(false){
37  boost::python::numeric::array array = boost::python::extract<boost::python::numeric::array > (obj);
38  void * voidDataPtr=PyArray_DATA(array.ptr());
39  CastPtrType dataPtr = static_cast<CastPtrType>(voidDataPtr);
40  size_t dimension =static_cast<size_t>(PyArray_NDIM(array.ptr()));
41 
42  npy_intp * shapePtr = PyArray_DIMS(array.ptr());
43  npy_intp * stridePtr = PyArray_STRIDES(array.ptr());
44  opengm::FastSequence<size_t> mystrides(dimension);
45  for(size_t i=0;i<dimension;++i){
46  mystrides[i]=(stridePtr[i])/sizeof(V);
47  }
48  view_.assign(shapePtr,shapePtr+dimension,mystrides.begin(),dataPtr,marray::FirstMajorOrder);
49  }
50 
51 
52  NumpyView( boost::python::numeric::array array):allocFromCpp_(false){
53  void * voidDataPtr=PyArray_DATA(array.ptr());
54  CastPtrType dataPtr = static_cast<CastPtrType>(voidDataPtr);
55  size_t dimension =static_cast<size_t>(PyArray_NDIM(array.ptr()));
56  npy_intp * shapePtr = PyArray_DIMS(array.ptr());
57  npy_intp * stridePtr = PyArray_STRIDES(array.ptr());
58  opengm::FastSequence<size_t> mystrides(dimension);
59  for(size_t i=0;i<dimension;++i){
60  mystrides[i]=(stridePtr[i])/sizeof(V);
61  }
62  view_.assign(shapePtr,shapePtr+dimension,mystrides.begin(),dataPtr,marray::FirstMajorOrder);
63  }
64 
65 
66  size_t size()const {return view_.size();}
67  size_t dimension()const{return view_.dimension();}
68  size_t shape(const size_t i)const{return view_.shape(i);}
69  size_t const * shapeBegin()const{return view_.shapeBegin();}
70  size_t const * shapeEnd()const{return view_.shapeEnd();}
71  void error(const std::string &reason=std::string(" "))const{throw opengm::RuntimeError(reason);}
72  //ShapeIteratorType shapeBegin()const{return view_.shapeBegin();}
73  //ShapeIteratorType shapeEnd()const{return view_.shapeBegin();}
74 
75  template<class X0>
76  const ValueType & operator()(X0 x0)const{
77  return view_(x0);
78  }
79  const ValueType & operator()(const size_t x0,const size_t x1)const{
80  return view_(x0,x1);
81  }
82  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2)const{
83  return view_(x0,x1,x2);
84  }
85  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3)const{
86  return view_(x0,x1,x2,x3);
87  }
88  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4)const{
89  return view_(x0,x1,x2,x3,x4);
90  }
91  /*
92  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5)const{
93  return view_(x0,x1,x2,x3,x4,x5);
94  }
95  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6)const{
96  return view_(x0,x1,x2,x3,x4,x5,x6);
97  }
98  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6,const size_t x7)const{
99  return view_(x0,x1,x2,x3,x4,x5,x6,x7);
100  }
101  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6,const size_t x7,const size_t x8)const{
102  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8);
103  }
104  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6,const size_t x7,const size_t x8,const size_t x9)const{
105  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
106  }
107  */
108  template<class ITERATOR>
109  const ValueType & operator[](ITERATOR iterator)const{
110  return view_(iterator);
111  }
112 
113  template<class X0>
114  ValueType & operator()(X0 x0){
115  return view_(x0);
116  }
117  ValueType & operator()(const size_t x0,const size_t x1){
118  return view_(x0,x1);
119  }
120  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2){
121  return view_(x0,x1,x2);
122  }
123  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3){
124  return view_(x0,x1,x2,x3);
125  }
126  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4){
127  return view_(x0,x1,x2,x3,x4);
128  }
129  /*
130  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5){
131  return view_(x0,x1,x2,x3,x4,x5);
132  }
133  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6){
134  return view_(x0,x1,x2,x3,x4,x5,x6);
135  }
136  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6,const size_t x7){
137  return view_(x0,x1,x2,x3,x4,x5,x6,x7);
138  }
139  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6,const size_t x7,const size_t x8){
140  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8);
141  }
142  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4,const size_t x5,const size_t x6,const size_t x7,const size_t x8,const size_t x9){
143  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
144  }
145  */
146  template<class ITERATOR>
147  ValueType & operator[](ITERATOR iterator){
148  return view_(iterator);
149  }
150 
151  ConstIteratorType begin1d()const{
152  return view_.begin();
153  }
154  ConstIteratorType end1d()const{
155  return view_.end();
156  }
157  IteratorType begin1d(){
158  return view_.begin();
159  }
160  IteratorType end1d(){
161  return view_.end();
162  }
163 
164  ConstIteratorType begin()const{
165  return view_.begin();
166  }
167  ConstIteratorType end()const{
168  return view_.end();
169  }
170  IteratorType begin(){
171  return view_.begin();
172  }
173  IteratorType end(){
174  return view_.end();
175  }
176 
177  //boost::python::object arrayObject()const{
178  // return arrayObj_;
179  //};
180 private:
181  //boost::python::numeric::array arrayObj_;
182  bool allocFromCpp_;
184  void * arrayData_;
185 };
186 
187 }
188 }
189 
190 
191 #endif /* NUMPYVIEW_INCL_HXX */
192 
NumpyView(boost::python::numeric::array array)
Definition: numpyview.hxx:52
ConstIteratorType end1d() const
Definition: numpyview.hxx:154
ValueType & operator()(const size_t x0, const size_t x1, const size_t x2, const size_t x3)
Definition: numpyview.hxx:123
The OpenGM namespace.
Definition: config.hxx:43
ValueType & operator()(const size_t x0, const size_t x1)
Definition: numpyview.hxx:117
marray::View< V,false >::const_iterator ConstIteratorType
Definition: numpyview.hxx:31
STL-compliant random access iterator for View and Marray.
Definition: marray.hxx:49
ValueType & operator()(X0 x0)
Definition: numpyview.hxx:114
size_t shape(const size_t i) const
Definition: numpyview.hxx:68
Array-Interface to an interval of memory.
Definition: marray.hxx:44
size_t const * shapeBegin() const
Definition: numpyview.hxx:69
IteratorType begin1d()
Definition: numpyview.hxx:157
ConstIteratorType begin1d() const
Definition: numpyview.hxx:151
Vector that stores values on the stack if size is smaller than MAX_STACK.
void error(const std::string &reason=std::string(" ")) const
Definition: numpyview.hxx:71
ConstIteratorType end() const
Definition: numpyview.hxx:167
const ValueType & operator()(const size_t x0, const size_t x1, const size_t x2, const size_t x3, const size_t x4) const
Definition: numpyview.hxx:88
const ValueType & operator()(const size_t x0, const size_t x1, const size_t x2) const
Definition: numpyview.hxx:82
NumpyView(boost::python::object obj)
Definition: numpyview.hxx:36
ValueType & operator()(const size_t x0, const size_t x1, const size_t x2)
Definition: numpyview.hxx:120
ValueType & operator()(const size_t x0, const size_t x1, const size_t x2, const size_t x3, const size_t x4)
Definition: numpyview.hxx:126
size_t size() const
Definition: numpyview.hxx:66
size_t const * shapeEnd() const
Definition: numpyview.hxx:70
const ValueType & operator()(X0 x0) const
Definition: numpyview.hxx:76
size_t const * ShapeIteratorType
Definition: numpyview.hxx:32
const ValueType & operator[](ITERATOR iterator) const
Definition: numpyview.hxx:109
const ValueType & operator()(const size_t x0, const size_t x1) const
Definition: numpyview.hxx:79
ConstIteratorType begin() const
Definition: numpyview.hxx:164
marray::View< V,false >::iterator IteratorType
Definition: numpyview.hxx:30
size_t dimension() const
Definition: numpyview.hxx:67
ValueType & operator[](ITERATOR iterator)
Definition: numpyview.hxx:147
OpenGM runtime error.
Definition: opengm.hxx:100
const ValueType & operator()(const size_t x0, const size_t x1, const size_t x2, const size_t x3) const
Definition: numpyview.hxx:85