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  }
37  NumpyView( boost::python::object obj):allocFromCpp_(false){
38  boost::python::numeric::array array = boost::python::extract<boost::python::numeric::array > (obj);
39  void * voidDataPtr=PyArray_DATA(array.ptr());
40  CastPtrType dataPtr = static_cast<CastPtrType>(voidDataPtr);
41  size_t dimension =static_cast<size_t>(PyArray_NDIM(array.ptr()));
42 
43  npy_intp * shapePtr = PyArray_DIMS(array.ptr());
44  npy_intp * stridePtr = PyArray_STRIDES(array.ptr());
45  opengm::FastSequence<size_t> mystrides(dimension);
46  for(size_t i=0;i<dimension;++i){
47  mystrides[i]=(stridePtr[i])/sizeof(V);
48  }
49  view_.assign(shapePtr,shapePtr+dimension,mystrides.begin(),dataPtr,marray::FirstMajorOrder);
50  }
51 
52 
53  NumpyView( boost::python::numeric::array array):allocFromCpp_(false){
54  void * voidDataPtr=PyArray_DATA(array.ptr());
55  CastPtrType dataPtr = static_cast<CastPtrType>(voidDataPtr);
56  size_t dimension =static_cast<size_t>(PyArray_NDIM(array.ptr()));
57  npy_intp * shapePtr = PyArray_DIMS(array.ptr());
58  npy_intp * stridePtr = PyArray_STRIDES(array.ptr());
59  opengm::FastSequence<size_t> mystrides(dimension);
60  for(size_t i=0;i<dimension;++i){
61  mystrides[i]=(stridePtr[i])/sizeof(V);
62  }
63  view_.assign(shapePtr,shapePtr+dimension,mystrides.begin(),dataPtr,marray::FirstMajorOrder);
64  }
65 
66 
67  size_t size()const {return view_.size();}
68  size_t dimension()const{return view_.dimension();}
69  size_t shape(const size_t i)const{return view_.shape(i);}
70  size_t const * shapeBegin()const{return view_.shapeBegin();}
71  size_t const * shapeEnd()const{return view_.shapeEnd();}
72  void error(const std::string &reason=std::string(" "))const{throw opengm::RuntimeError(reason);}
73  //ShapeIteratorType shapeBegin()const{return view_.shapeBegin();}
74  //ShapeIteratorType shapeEnd()const{return view_.shapeBegin();}
75 
76  template<class X0>
77  const ValueType & operator()(X0 x0)const{
78  return view_(x0);
79  }
80  const ValueType & operator()(const size_t x0,const size_t x1)const{
81  return view_(x0,x1);
82  }
83  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2)const{
84  return view_(x0,x1,x2);
85  }
86  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3)const{
87  return view_(x0,x1,x2,x3);
88  }
89  const ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4)const{
90  return view_(x0,x1,x2,x3,x4);
91  }
92  /*
93  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{
94  return view_(x0,x1,x2,x3,x4,x5);
95  }
96  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{
97  return view_(x0,x1,x2,x3,x4,x5,x6);
98  }
99  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{
100  return view_(x0,x1,x2,x3,x4,x5,x6,x7);
101  }
102  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{
103  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8);
104  }
105  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{
106  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
107  }
108  */
109  template<class ITERATOR>
110  const ValueType & operator[](ITERATOR iterator)const{
111  return view_(iterator);
112  }
113 
114  template<class X0>
115  ValueType & operator()(X0 x0){
116  return view_(x0);
117  }
118  ValueType & operator()(const size_t x0,const size_t x1){
119  return view_(x0,x1);
120  }
121  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2){
122  return view_(x0,x1,x2);
123  }
124  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3){
125  return view_(x0,x1,x2,x3);
126  }
127  ValueType & operator()(const size_t x0,const size_t x1,const size_t x2,const size_t x3,const size_t x4){
128  return view_(x0,x1,x2,x3,x4);
129  }
130  /*
131  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){
132  return view_(x0,x1,x2,x3,x4,x5);
133  }
134  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){
135  return view_(x0,x1,x2,x3,x4,x5,x6);
136  }
137  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){
138  return view_(x0,x1,x2,x3,x4,x5,x6,x7);
139  }
140  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){
141  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8);
142  }
143  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){
144  return view_(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9);
145  }
146  */
147  template<class ITERATOR>
148  ValueType & operator[](ITERATOR iterator){
149  return view_(iterator);
150  }
151 
152  ConstIteratorType begin1d()const{
153  return view_.begin();
154  }
155  ConstIteratorType end1d()const{
156  return view_.end();
157  }
158  IteratorType begin1d(){
159  return view_.begin();
160  }
161  IteratorType end1d(){
162  return view_.end();
163  }
164 
165  ConstIteratorType begin()const{
166  return view_.begin();
167  }
168  ConstIteratorType end()const{
169  return view_.end();
170  }
171  IteratorType begin(){
172  return view_.begin();
173  }
174  IteratorType end(){
175  return view_.end();
176  }
177 
178  marray::View< V, false > getSliceView(size_t dimension, size_t sliceIndex) {
179  // create base coordinate
180  std::vector<size_t> baseIterator(view_.dimension(), 0);
181  baseIterator[dimension] = sliceIndex;
182 
183  // create expected shape
184  std::vector<size_t> shapeIterator(view_.shapeBegin(), view_.shapeEnd());
185  shapeIterator[dimension] = 1;
186 
187  // return sub view of slice with reduced dimensions
188  marray::View< V, false > new_view = view_.view(baseIterator.begin(), shapeIterator.begin());
189  new_view.squeeze();
190  return new_view;
191  }
192 
194  return view_;
195  }
196 
197  //boost::python::object arrayObject()const{
198  // return arrayObj_;
199  //};
200 private:
201  //boost::python::numeric::array arrayObj_;
202  bool allocFromCpp_;
204  void * arrayData_;
205 };
206 
207 }
208 }
209 
210 
211 #endif /* NUMPYVIEW_INCL_HXX */
212 
NumpyView(boost::python::numeric::array array)
Definition: numpyview.hxx:53
ValueType & operator()(const size_t x0, const size_t x1, const size_t x2, const size_t x3)
Definition: numpyview.hxx:124
The OpenGM namespace.
Definition: config.hxx:43
ValueType & operator()(const size_t x0, const size_t x1)
Definition: numpyview.hxx:118
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:115
size_t const * shapeBegin() const
Definition: numpyview.hxx:70
size_t const * shapeEnd() const
Definition: numpyview.hxx:71
void view(BaseIterator, ShapeIterator, View< T, isConst, A > &) const
Get a sub-view with the same coordinate order.
Definition: marray.hxx:1987
const ValueType & operator()(const size_t x0, const size_t x1) const
Definition: numpyview.hxx:80
marray::View< V, false > getSliceView(size_t dimension, size_t sliceIndex)
Definition: numpyview.hxx:178
IteratorType begin1d()
Definition: numpyview.hxx:158
Vector that stores values on the stack if size is smaller than MAX_STACK.
marray::View< V,false > view() const
Definition: numpyview.hxx:193
const ValueType & operator()(X0 x0) const
Definition: numpyview.hxx:77
const ValueType & operator[](ITERATOR iterator) const
Definition: numpyview.hxx:110
const ValueType & operator()(const size_t x0, const size_t x1, const size_t x2, const size_t x3) const
Definition: numpyview.hxx:86
const ValueType & operator()(const size_t x0, const size_t x1, const size_t x2) const
Definition: numpyview.hxx:83
void error(const std::string &reason=std::string(" ")) const
Definition: numpyview.hxx:72
NumpyView(boost::python::object obj)
Definition: numpyview.hxx:37
ValueType & operator()(const size_t x0, const size_t x1, const size_t x2)
Definition: numpyview.hxx:121
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:127
size_t const * ShapeIteratorType
Definition: numpyview.hxx:32
size_t dimension() const
Definition: numpyview.hxx:68
void squeeze()
Remove singleton dimensions by setting their coordinates to zero.
Definition: marray.hxx:2414
ConstIteratorType begin1d() const
Definition: numpyview.hxx:152
marray::View< V,false >::iterator IteratorType
Definition: numpyview.hxx:30
ConstIteratorType end() const
Definition: numpyview.hxx:168
size_t shape(const size_t i) const
Definition: numpyview.hxx:69
ConstIteratorType end1d() const
Definition: numpyview.hxx:155
ValueType & operator[](ITERATOR iterator)
Definition: numpyview.hxx:148
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:89
ConstIteratorType begin() const
Definition: numpyview.hxx:165
OpenGM runtime error.
Definition: opengm.hxx:100