export an array of floats to python

Discussion in 'Python' started by Peter Wuertz, Feb 22, 2007.

  1. Peter Wuertz

    Peter Wuertz Guest

    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!
     
    Peter Wuertz, Feb 22, 2007
    #1
    1. Advertising

  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.
    >


    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
     
    Travis Oliphant, Feb 22, 2007
    #2
    1. Advertising

  3. Peter Wuertz

    Larry Bates Guest

    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
     
    Larry Bates, Feb 22, 2007
    #3
  4. Peter Wuertz

    Peter Wuertz Guest

    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!
     
    Peter Wuertz, Feb 22, 2007
    #4
  5. Peter Wuertz

    sturlamolden Guest

    On Feb 22, 5:40 pm, Peter Wuertz <-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.
     
    sturlamolden, Feb 22, 2007
    #5
  6. Peter Wuertz

    sturlamolden Guest

    On Feb 22, 7:36 pm, "sturlamolden" <> wrote:

    > npy_int dim = {480, 640}; /* whatever the size of your data, in C-
    > order */


    oops...

    npy_int dim[] = {480, 640};
     
    sturlamolden, Feb 22, 2007
    #6
  7. Peter Wuertz

    Peter Wuertz Guest

    Thank you! Thats the complete solution *g*
     
    Peter Wuertz, Feb 23, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,347
    Tim Rentsch
    Sep 23, 2005
  2. Daniel Fetchinson
    Replies:
    13
    Views:
    1,112
    Daniel Fetchinson
    Dec 28, 2008
  3. Norah Jones
    Replies:
    7
    Views:
    146
    llanitedave
    Mar 13, 2013
  4. Boris FELD
    Replies:
    0
    Views:
    117
    Boris FELD
    Mar 12, 2013
  5. Gary Herron
    Replies:
    0
    Views:
    117
    Gary Herron
    Mar 12, 2013
Loading...

Share This Page