473,666 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need an example of Python numarray to C++ and back again, Boost / SWIG?

PL
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 SWIG and/or
the analagous setup files that Boost would need to do this.

Thanks in advance for any help you can provide,

Nov 9 '05 #1
3 3385
PL wrote:
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 SWIG and/or
the analagous setup files that Boost would need to do this.


You may want to look into weave.inline or weave.blitz, from scipy. Typemaps for
conversion to blitz++ were recently posted on the scipy list:

http://aspn.activestate.com/ASPN/Mai...ussion/2883831

In particular look at Stefan's post.

For info on weave, here you can find some old slides and example code:

http://amath.colorado.edu/faculty/fperez/python/

Cheers,

f

Nov 10 '05 #2
PL
I looked at Stefan's post - but he remarks that "Unfortunat ely, Blitz
jealously guards its data (restricted pointers), so that it is not so
easy to do the conversion in the other direction. If anyone knows an
answer to this problem, I'd be glad to hear it"

I've previously looked at Phillip Austin's 'num_util' and Paulo J. S.
Silva's 'COIN' example, but even from those two, I can't figure out a
way to do: Python 2D numarray --> C++ (process array) --> Python 2D
numarray.

I forgot about "weave" - I had looked there before and will revisit it
to see if it will work. But I was intending to do this with a compiled
extension. I wish there was a simple example of this in either the
SWIG or Boost docs or a faq/howto posted somewhere . . .

-Paul
Fernando Perez wrote:
PL wrote:
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 SWIG and/or
the analagous setup files that Boost would need to do this.


You may want to look into weave.inline or weave.blitz, from scipy. Typemaps for
conversion to blitz++ were recently posted on the scipy list:

http://aspn.activestate.com/ASPN/Mai...ussion/2883831

In particular look at Stefan's post.

For info on weave, here you can find some old slides and example code:

http://amath.colorado.edu/faculty/fperez/python/

Cheers,

f


Nov 10 '05 #3
PL wrote:
I looked at Stefan's post - but he remarks that "Unfortunat ely, Blitz
jealously guards its data (restricted pointers), so that it is not so
easy to do the conversion in the other direction. If anyone knows an
answer to this problem, I'd be glad to hear it"

I've previously looked at Phillip Austin's 'num_util' and Paulo J. S.
Silva's 'COIN' example, but even from those two, I can't figure out a
way to do: Python 2D numarray --> C++ (process array) --> Python 2D
numarray.


I may be missing something, but what I've done in the past for this is have the
C++ code simply reuse the Numeric data pointer. This way, when I exit the C++
extension (I've used blitz++ for the job), the array as seen from the python
side has been 'magically' modified. Obviously this means that I can't allocate
new arrays in C++ which can be transfered over to python without paying the
price of a copy, but in my cases that hasn't been a problem: I do all
'allocations' in python (via arr=Numeric.emp ty(...)) and let the blitz code
fill in the arrays.

This has the advantage that the blitz array creation is extremely cheap, as only
the shape tuple needs to be copied (not the data region). The following little
snippet is pretty much all that's needed if the above description happens to
work for you. This code is mostly taken from weave's internals:

// -*- C++ -*-
#ifndef PY_TO_BLITZ_H
#define PY_TO_BLITZ_H

#include "Python.h"
#include "Numeric/arrayobject.h"
#include "blitz/array.h"

using namespace blitz;

// Convert a Numpy array to a blitz one, using the original's data (no copy)
template<class T, int N>
static Array<T,N> py_to_blitz(con st PyArrayObject* arr_obj)
{
const int T_size = sizeof(T);
TinyVector<int, N> shape(0);
TinyVector<int, N> strides(0);
int *arr_dimensions = arr_obj->dimensions;
int *arr_strides = arr_obj->strides;

for (int i=0;i<N;++i) {
shape[i] = arr_dimensions[i];
strides[i] = arr_strides[i]/T_size;
}
return Array<T,N>((T*) arr_obj->data,shape,str ides,neverDelet eData);
}
#endif // PY_TO_BLITZ_H
Cheers,

f

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1815
by: Ray Schumacher | last post by:
Hi all, I'm trying to port Hongjie Xie's IDL/ENVI Implementation of the FFT Based Algorithm for Automatic Image Registration ftp://ftp.iamg.org/VOL29/v29-08-10.zip http://www.nmt.edu/%7Ehjxie/xie-paper.pdf http://www.iamg.org/CGEditor/cg2003.htm to Python/numarray/Numeric and running into some subtle problems. I don't have IDL, so I could only read the PDF docs.
37
2794
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
2
2624
by: Carl Gilbert | last post by:
Hi I am trying to serialize an assembly into a byte array. I am using a BinaryFormatter to serialize the assembly into a stream. Once in the stream, would then save this to the database in a byte(). I have come across a few issues with deserializing the stream so I decided to strip out all the un-necessary code and simply serialize from an assembly to a stream and then de-serialize is back again using the same BinaryFormatter.
8
18883
by: Kenny ODell | last post by:
I do not know how to convert from a byte array to a float, and back again. I read data from a serial port into a byte (entire command structure which I parse). I am able to sift the data and isolate the information I want, in this case a structure that contains a bunch of floats and longs. So, lets say I have a byte containing the 4 bytes of info that are actually a float. In old C code, I would simple declare a pointer to the correct...
0
1068
by: KathyB | last post by:
HI, I've posted this type of question before so please don't be annoyed, but I still need example(s) of how to build dynamic controls and post them back to the xmldocument on the server. I realize the html is client side, so I need to assign (somehow) unique id's within the xsl stylesheet, and I think use response.form collection (?) to submit the <input> textboxes back to the xmlDocument. One sticking point: how to identify and code...
2
3580
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string that is displayed in a text box. Once displayed, the text will be copied, transmitted and then pasted (all manually by humans) into a second utility where the string must then be reverse-engineered into the *original* byte array. The byte array will...
4
4015
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back to strings. How do I convert this raw data from one format to another? I don't think system.text.encoding is useful here as the data is not in any particular encoding format, it's just raw data and it needs to stay
14
5506
by: mistral | last post by:
Need compile python code, source is in html and starts with parameters: #!/bin/sh - "exec" "python" "-O" "$0" "$@" I have installed ActivePython for windows.
0
1572
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point - possibly the conversion - Undefined Behavior will begin. And then when whoever collects the reference uses it, Undefined Behavior will continue.
0
8878
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8785
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...
1
8560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7389
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
6200
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
5671
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
4200
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...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1778
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.