473,780 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_pote ntials (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 6039
On 9 May 2007 08:08:46 -0700, TG <gi****@gmail.c omwrote:
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_pote ntials (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_pote ntials (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_pote ntials (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
3396
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 rest of what I need to do for my project. I am very familiar with Python, but less so with C++ and Boost or SWIG. Does anyone have an example with all steps that I can follow? More specifically I am looking for the C++ code, ".i" file for...
0
1060
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
465
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 program before moving on into the real thing). I have been making test runs using the codes from http://docs.python.org/ext/pure-embedding.html as a basic, but modifiying it now as a function inside my C/C++ code.
0
1427
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 cols = extract<int>(shape);
2
2789
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 used by NumPy. The pattern shows how to create a NumPy array that points to previously allocated memory and then shows how to construct an object that allows the correct deallocator to be called when the NumPy array is freed. This may be...
13
1582
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 indicate "type" (in the Python sense or even a broader sense) - called "system Hungarian notation" by some. In contrast, the prefix used in "apps Hungarian notation" declares the "kind" or "intent" of the variable, and it may be useful in some cases....
2
3073
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 Dummy(np.ndarray) which returns a plain array whenever is sliced or viewed. I cannot figure out how. I would really appreciate you help, here is a chunk of code, what is commented was an attempt which didn’t work. Thanks a lot, Jacopo
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.