473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

export an array of floats to python

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
6 6801
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
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
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
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
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
Thank you! Thats the complete solution *g*
Feb 23 '07 #7

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

Similar topics

1
by: JW | last post by:
Hi, Below is a description of what I would like to do. Would someone tell me if it is possible and if so, how? I have an array (presumably 'large') that is mallocced in a C function and its...
4
by: Faheem Mitha | last post by:
Dear People, I have the current modest goal. Given a user defined class, I want to creata an array of instances of that class. Consider the following class. class cell: def...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
5
by: michealmess | last post by:
Can anyone help me. I wish to export an array from vb.net to excel ranges. This will happen for multiple files. The ranges names will not be start and end in the same cells on all files. How do i...
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
5
by: zefciu | last post by:
Hi! I want to embed a function in my python application, that creates a two-dimensional array of integers and passes it as a list (preferably a list of lists, but that is not necessary, as the...
6
by: nubean19 | last post by:
Im new to the python programming and I wanted to know how to write an array using python
6
by: Kanis | last post by:
Hi, I am a Python beginner and using PyCrust to process data for my research work. I found I not able to import a .py file directly. But I have to type in every lines every time. Or I can save...
3
by: Simon Strobl | last post by:
Hello, a program of mine, which is supposed to be used in a project, uses nltk. I want the other users to be able to use my program without having to fiddle around with their environment....
10
by: ShadowLocke | last post by:
I am trying to pass a string array from c# into c++ and manipulate the data of that array. How can I do this? (I am c++ newb) c++ __declspec(dllexport) int __stdcall Test(LPCTSTR* as_test) {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.