By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

May 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 9 May 2007 08:08:46 -0700, TG <gi****@gmail.comwrote:
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.

--
http://mail.python.org/mailman/listinfo/python-list

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
May 9 '07 #2

P: n/a
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 ?

May 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.