473,698 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_FromM emory - 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 6817
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_FromM emory - 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_SimpleN ewFromData (the NumPy C-API) or PyArray_FromDim sAndData
(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_FromM emory - 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_SimpleN ewFromData (the NumPy C-API) or PyArray_FromDim sAndData
(the Numeric C-API).
Cool thanks, thats exactly what I needed!
Feb 22 '07 #4
On Feb 22, 5:40 pm, Peter Wuertz <pwue...@studen ts.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_Simple NewFromData(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_Simple New(int nd, npy_intp* dims, int typenum);

Then, use

void *PyArray_DATA(P yObject* 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_SimpleN ew(2, &dim, NPY_DOUBLE);
Py_INCREF(myarr ay);
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, "sturlamold en" <sturlamol...@y ahoo.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
4260
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 values are initialized in that same function. I would like to pass just the pointer to the beginning of the array
4
3668
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 setrow(self,row): self.row = row
8
3855
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 = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
5
3730
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 do this?
10
6739
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 the time complexity for this code is O(n) due to its
5
2325
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 python function knows the dimensions of this array). As I read the reference, I see, that I must first initialize a list object and then item-by-item put the values to the list. Is there any faster way to do it? And is it worth to implement? ...
6
13138
by: nubean19 | last post by:
Im new to the python programming and I wanted to know how to write an array using python
6
3585
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 the script in function. Then call out the function in PyCrust. But it's not convenient. And it causes global variable problem. The case is even I import the numpy lib in the function file.py, but it shows error: “NameError: global name 'append' is...
3
9477
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. Therefore, I tried this code: #!/usr/bin/ python
10
8460
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) { as_test = L"This is new data"; return 0; } c#
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9152
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...
1
8885
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
7708
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...
0
4358
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
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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.