[C++-sig] getting shape of numpy array

2009-11-10 Thread Nathan Huesken
Hi,

I am trying to interaface a numpy array using boost::python::numeric::array.

void function(numeric::array& a)
{
tuple shape=extract(a.getshape());
double x;
int i,j;
for(i=0;i(a[make_tuple(i,j)]);
}

Now I had to add
numeric::array::set_module_and_type("numpy", "ndarray");

to the module, hope that was correct.

When I call this function in python, I get:
AttributeError: 'numpy.ndarray' object has no attribute 'getshape'

What is the correct way to do it?

Thanks!
Nathan 
-- 
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] getting shape of numpy array

2009-11-10 Thread Michele De Stefano
Hello Nathan.

Find my answers below.

2009/11/10 Nathan Huesken :
> Hi,
>
> I am trying to interaface a numpy array using boost::python::numeric::array.
>
> void function(numeric::array& a)
> {
>        tuple shape=extract(a.getshape());
>        double x;
>        int i,j;
>        for(i=0;i                for(j=0;j                        x=extract(a[make_tuple(i,j)]);
> }
>
> Now I had to add
> numeric::array::set_module_and_type("numpy", "ndarray");
>
> to the module, hope that was correct.
>
> When I call this function in python, I get:
> AttributeError: 'numpy.ndarray' object has no attribute 'getshape'

The error comes out because, actually, numpy.ndarray has no getshape method.

>
> What is the correct way to do it?
>

Within your function, declare

const tuple &shape = extract(in.attr("shape"));


I suggest you to have a look also at my open source library (mds-utils):

http://code.google.com/p/mds-utils/

I've written some from/to Python converters that are able to convert a
Python sequence directly into a Boost uBLAS vector or matrix.
If you download the software, you can build the doxygen documentation
(follow the instructions) and you will find an example for each C++
class or function.

In other words, using my library, you could write your function as:

void function(const boost::numeric::ublas::matrix& m) {
...
}

and export it to Python and pass, from Python, a Numpy 2D array.

Have a look at mds-utils and at its usage examples.

Bye,
Michele

> Thanks!
> Nathan
> --
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://code.google.com/p/mds-utils
http://xoomer.virgilio.it/michele_de_stefano
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] [Boost.Python] tutorial python extension fails on import with ImportError: No module named hello_ext

2009-11-10 Thread Charles Lentz
Hey all, 

 

When using boost_1_38_0\libs\python\example\tutorial, I try to import the
extension that is created (hello_ext) but get the exception, ImportError: No
module named hello_ext

 

Here's some details that might be needed:

 

OS: Windows Vista

Boost version: 1.38.0

Bjam version: 3.1.17

Python version: 2.6.2

 

My user-config.jam looks like this:

 

#  MSVC configuration

using msvc : 9.0 ;

 

#  Python configuration

using python 

  : 2.6
#version

  : C:/Python26
#location

  : C:/projects/external/python/Python-2.6.2/Include
#includes

  : C:/projects/external/python/Python-2.6.2/Lib
#libraries

  ;

 

 

I am working with the tutorial example located here:
boost_1_38_0\libs\python\example\tutorial.  When I run Bjam after I cd to
this directory, this tutorial example says that it passes all the tests and
I can check the output in the resulting files within the hello.test
directory.  That's all well and good that the test can get this extension
module to work, but I want to be able to import it myself in a python
session or python file.  That way I can see it work and then move on to
creating my own extensions.

 

I modify Jamroot so that the last few lines where it creates tests are
commented out.  When I run bjam again, hello_ext.pyd remains (previously it
was being created, used for the test, and then deleted).  I try to run
hello.py (which is the script that the test uses) from IDLE (which comes
with the installation of python) and it throws an exception for my import
statement

 

Traceback (most recent call last):

  File
"C:\Users\Charles\Desktop\boost_1_38_0\boost_1_38_0\libs\python\example\tuto
rial\hello.py", line 6, in 

import hello_ext

ImportError: No module named hello_ext

 

Hrmm.  Okay.  So I move the hello.py file into the same directory as the
hello_ext.pyd file.  Run it again using IDLE, still get the exception

 

Traceback (most recent call last):

  File
"C:\Users\Charles\Desktop\boost_1_38_0\boost_1_38_0\libs\python\example\tuto
rial\bin\msvc-9.0\debug\threading-multi\hello.py", line 6, in 

import hello_ext

ImportError: No module named hello_ext

 

 

So I guess I need some help here.  I want to be able to create an extension
module and then import it in a script.  This is what I was led to believe I
could do, but I do not see how.  I have tried to provide all information
that I think is necessary.  Any help is greatly appreciated.

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig