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

TG girodt at gmail.com
Thu May 10 04:45:50 EDT 2007


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 ?




More information about the Python-list mailing list