Boost python : get the shape of a numpy ndarray in C++ code.

T

TG

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 ?
* are the dimensions corresponding to map's width / height ?
* is array field with floats or ints ?

void
Layer::set_potentials (numeric::array& array)
{
for (int h=0; h<map->height; h++){
for (int w=0; w<map->width; w++){
units[w+h*map->width]->potential =
extract<float>(array[make_tuple(w,h)]);
}
}
}


Some help is very welcome here ... thanks.
 
R

Roman Yakovenko

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; h<map->height; h++){
for (int w=0; w<map->width; w++){
units[w+h*map->width]->potential =
extract<float>(array[make_tuple(w,h)]);
}
}
}


Some help is very welcome here ... thanks.
 
T

TG

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; h<map->height; h++){
for (int w=0; w<map->width; w++){
units[w+h*map->width]->potential =
extract<float>(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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top