473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
2 6018
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PL | last post by:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for example, add 1 to each element) and pass it back to Python. With these building blocks I will be able to figure out all the...
0
by: Colin J. Williams | last post by:
Travis Oliphant wrote: > Gerard Vermeulen wrote: > >> On Wed, 01 Feb 2006 11:15:09 -0500 >> "Colin J. Williams" <cjw@sympatico.ca> wrote: >> >> >> >>
6
by: mistabean | last post by:
Hello, first of all, I am a programming newbie, especially in python... Onwards to the problem, I have been having difficulty embedding a python module into my C/C++ program. (just a test...
0
by: Marc Oldenhof | last post by:
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); int...
2
by: Travis Oliphant | last post by:
I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one...
13
by: Gabriel Genellina | last post by:
En Mon, 06 Oct 2008 11:19:58 -0300, Joe Strout <joe@strout.netescribió: Apart from the wise words that others have said, I'd add that I see no point in identifier prefixes when they merely...
2
by: jpecci | last post by:
I would like to ask you a quick question: I am facing the opposite problem described here: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html In short, I want to create a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.