get the shape of a numpy ndarray in C++ code [boost.python]

M

Marc Oldenhof

[sorry is half a post appeared earlier. Bloody Google groups...]

Hello,

I'm trying to use a numpy array in C++ (win2000) using boost.python.

Test code:
void test( numeric::array& nsP)
{
object shape = nsP.getshape();
int rows = extract<int>(shape[0]);
int cols = extract<int>(shape[1]);
}

At first, running it in Python got me this message:

ArgumentError: Python argument types in
d3d.wr_conn(numpy.ndarray)
did not match C++ signature:
wr_conn(class boost::python::numeric::array {lvalue})

I fixed this using this line:

numeric::array::set_module_and_type( "numpy", "ndarray");

[was that right?]


At least it got me one step further; the array is accepted. Now the
message is this:

AttributeError: 'numpy.ndarray' object has no attribute 'getshape'

Now I'm stumped. The only thing I can find is
http://www.thescripts.com/forum/showthread.php?t=644270

which regrettably ends with the same question. What's wrong here?

greets,
Marc
 
Joined
Jan 15, 2014
Messages
1
Reaction score
0
Hello,
I've found a way to fix that.
You should use attr("shape") instead of getshape().
So your code should be:
object shape = nsP.attr("shape");
int rows = extract<int>(shape[0]);
int cols = extract<int>(shape[1]);
 

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

Forum statistics

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

Latest Threads

Top