By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

export an array of floats to python

P: n/a
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!
Feb 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Peter Wuertz wrote:
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

Feb 22 '07 #2

P: n/a
Peter Wuertz wrote:
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
Feb 22 '07 #3

P: n/a
Travis Oliphant wrote:
Peter Wuertz wrote:
>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?
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!
Feb 22 '07 #4

P: n/a
On Feb 22, 5:40 pm, Peter Wuertz <pwue...@students.uni-mainz.de>
wrote:
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.




Feb 22 '07 #5

P: n/a
On Feb 22, 7:36 pm, "sturlamolden" <sturlamol...@yahoo.nowrote:
npy_int dim = {480, 640}; /* whatever the size of your data, in C-
order */
oops...

npy_int dim[] = {480, 640};
Feb 22 '07 #6

P: n/a
Thank you! Thats the complete solution *g*
Feb 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.