export an array of floats to python

P

Peter Wuertz

Hi,

I'm writing a C module for python, that accesses a special usb camera.
This module is supposed to provide python with data (alot of data). Then
SciPy is used to fit the data.

My question is, how to make python read from a C array? By reading the
documentation, one could get the impression that PyBufferObjects do that
job.

http://docs.python.org/api/bufferObjects.html

It says:
"Two examples of objects that support the buffer interface are strings
and arrays."

Where this function looks promising:
"PyBuffer_FromMemory - Return a new read-only buffer object that reads
from a specified location in memory, with a specified size."

All right, lets imagine I created a PyBufferObject from my float array
in the C module, and passed it to python.

I dont know what to do with a BufferObject in python, I would like to
"cast" it to a Array of the type float, but Arrays do not have that kind
of function... they can only be constructed from files or lists.

Apart from creating these BufferObjects, there is no documentation about
what to do with them :(

Thanks for your help!
 
T

Travis Oliphant

Peter said:
Hi,

I'm writing a C module for python, that accesses a special usb camera.
This module is supposed to provide python with data (alot of data). Then
SciPy is used to fit the data.

Which version of scipy are you using?

My question is, how to make python read from a C array? By reading the
documentation, one could get the impression that PyBufferObjects do that
job.

http://docs.python.org/api/bufferObjects.html

It says:
"Two examples of objects that support the buffer interface are strings
and arrays."

Where this function looks promising:
"PyBuffer_FromMemory - Return a new read-only buffer object that reads
from a specified location in memory, with a specified size."

All right, lets imagine I created a PyBufferObject from my float array
in the C module, and passed it to python.

How about creating an array directly from your float array using
PyArray_SimpleNewFromData (the NumPy C-API) or PyArray_FromDimsAndData
(the Numeric C-API).


-Travis
 
L

Larry Bates

Peter said:
Hi,

I'm writing a C module for python, that accesses a special usb camera.
This module is supposed to provide python with data (alot of data). Then
SciPy is used to fit the data.

My question is, how to make python read from a C array? By reading the
documentation, one could get the impression that PyBufferObjects do that
job.

http://docs.python.org/api/bufferObjects.html

It says:
"Two examples of objects that support the buffer interface are strings
and arrays."

Where this function looks promising:
"PyBuffer_FromMemory - Return a new read-only buffer object that reads
from a specified location in memory, with a specified size."

All right, lets imagine I created a PyBufferObject from my float array
in the C module, and passed it to python.

I dont know what to do with a BufferObject in python, I would like to
"cast" it to a Array of the type float, but Arrays do not have that kind
of function... they can only be constructed from files or lists.

Apart from creating these BufferObjects, there is no documentation about
what to do with them :(

Thanks for your help!

I've always used the struct module to unpack the C array into the floats
(or ints or strings or ...) that I wanted.

-Larry Bates
 
P

Peter Wuertz

Travis said:
Which version of scipy are you using?

I'm using ubuntu edgy eft, which ships scipy 0.5.2
How about creating an array directly from your float array using
PyArray_SimpleNewFromData (the NumPy C-API) or PyArray_FromDimsAndData
(the Numeric C-API).

Cool thanks, thats exactly what I needed!
 
S

sturlamolden

I'm writing a C module for python, that accesses a special usb camera.
This module is supposed to provide python with data (alot of data). Then
SciPy is used to fit the data.

My question is, how to make python read from a C array? By reading the
documentation, one could get the impression that PyBufferObjects do that
job.

Never mind the buffer object.

Since you use SciPY I assume you want to wrap your C array with a
NumPy array. This can be accomplished using the API call:

PyObject *PyArray_SimpleNewFromData(int nd, npy_intp* dims, int
typenum, void* data);

nd is the number of dimensions.
dims is an array of length nd containing the size in each dimension.
typenum can e.g. be NPY_FLOAT, NPY_DOUBLE, NPY_BYTE, NPY_UBYTE,
NPY_INT, NPY_UINT, NPY_SHORT, NPY_USHORT, etc.

Don't deallocate the data buffer until the NuPy array is deleted.

Otherwise, you could create a new array from scratch using:

PyObject *PyArray_SimpleNew(int nd, npy_intp* dims, int typenum);

Then, use

void *PyArray_DATA(PyObject* obj);

to get a pointer to the data buffer. Then fill in (e.g. memcpy)
whatever you have from your camera.

If you e.g. have unsigned integers from the camera and want to cast
them into Python floats:

unsigned int *camera_buffer = ... /* whatever */
npy_int dim = {480, 640}; /* whatever the size of your data, in C-
order */
PyObject *myarray;
double *array_buffer;
int i;
myarray = PyArray_SimpleNew(2, &dim, NPY_DOUBLE);
Py_INCREF(myarray);
array_buffer = (double *)PyArray_DATA(myarray);
for (i=0; i<640*480; i++) *array_buffer++ = (double) *camera_buffer++;

Now return myarray and be happy.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top