Re: Boost python : get the shape of a numpy ndarray in C++ code.
On 9 May 2007 08:08:46 -0700, TG <[EMAIL PROTECTED]> wrote: > Hi there. > > I'm strugling here with some boost python code (damn I hate C++) : > > All I want to do is to initialize the content of an array with a numpy > ndarray parameter. I have this, which actually works. But I want to > add some kind of data check such as : > > * is array two dimensional ? This question has nothing to do with Boost.Python > * are the dimensions corresponding to map's width / height ? Same as above > * is array field with floats or ints ? Read "extract" documentation http://boost.org/libs/python/doc/v2/extract.html > void > Layer::set_potentials (numeric::array& array) > { > for (int h=0; hheight; h++){ > for (int w=0; wwidth; w++){ > units[w+h*map->width]->potential = > extract(array[make_tuple(w,h)]); > } > } > } > > > Some help is very welcome here ... thanks. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Boost python : get the shape of a numpy ndarray in C++ code.
What I'm trying to say here : a numpy array is supposed to have it's shape stored as a tuple. What I want to do is to access this information from my C++ code, in order to do some validity check. So, by looking around in the doc of boost/python/numeric.hpp I was able to do this : void Layer::set_potentials (numeric::array& array) { for (int h=0; hheight; h++){ for (int w=0; wwidth; w++){ units[w+h*map->width]->potential = extract(array[make_tuple(w,h)]); } } } which is fairly simple and actually works. Now, if I look further, I see there is a method called getshape() in array class, which gives back an object - I guess this object is a tuple, because the documentation is quite poor. So my idea is to get this object and use extract in order to get the actual dimensions as integers. but when I add this : void Layer::set_potentials (numeric::array& array) { object shape = array.getshape(); [...] } It compiles, and then on execution I get this error : AttributeError: 'numpy.ndarray' object has no attribute 'getshape' Does it still have nothing to do with Boost.Python ? -- http://mail.python.org/mailman/listinfo/python-list