Rich E | 28 Nov 23:04

converting c float array to numpy array or python list

Hi all,

So I have a nice C library wrapped in python and I'd like to start
doing some plots of things in python... the problem is the data is all
stuck in C arrays.  I'm sure this is a common problem, but I have not
found an elegant solution.  Right now I have a help function that will
return one member of the C array, such as:

%inline %{

        float getFloat(float *array, int index)
        {
                return array[index];
        }
        %}

But how would I go about returning a python list of the entire array,
or even better, an NumPy array?

My thoughts are that I will have to convert between C arrays and
python types (list or NumPy) quite often, probably at a refresh rate
suitable for animation. Any advice?

regards,
Rich

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
(Continue reading)

Christopher Barker | 1 Dec 20:43
Favicon

Re: converting c float array to numpy array or python list

Rich E wrote:
> So I have a nice C library wrapped in python and I'd like to start
> doing some plots of things in python... the problem is the data is all
> stuck in C arrays.

> But how would I go about returning a python list of the entire array,
> or even better, an NumPy array?

there is a numpy.i file distributed with numpy ( or with the docs, 
anyway). There was also a recent post on the numpy list with some extra 
hints and examples for using it.

This is another nice article about it:

http://matt.eifelle.com/2008/11/04/exposing-an-array-interface-with-swig-for-a-cc-structure/

> My thoughts are that I will have to convert between C arrays and
> python types (list or NumPy) quite often, probably at a refresh rate
> suitable for animation.

the above allow a numpy array and your C-code to share the same pointer, 
so it is very fast.

-Chris

--

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
(Continue reading)

Rich E | 1 Dec 20:49

Re: converting c float array to numpy array or python list

Someone earlier today in my office pointed me in the same direction,
so it looks like the way to go!  I'll give it a try.. . thanks
(especially for the article link).

cheers,
Rich

On Mon, Dec 1, 2008 at 8:43 PM, Christopher Barker
<Chris.Barker <at> noaa.gov> wrote:
> Rich E wrote:
>>
>> So I have a nice C library wrapped in python and I'd like to start
>> doing some plots of things in python... the problem is the data is all
>> stuck in C arrays.
>
>> But how would I go about returning a python list of the entire array,
>> or even better, an NumPy array?
>
> there is a numpy.i file distributed with numpy ( or with the docs, anyway).
> There was also a recent post on the numpy list with some extra hints and
> examples for using it.
>
> This is another nice article about it:
>
> http://matt.eifelle.com/2008/11/04/exposing-an-array-interface-with-swig-for-a-cc-structure/
>
>
>> My thoughts are that I will have to convert between C arrays and
>> python types (list or NumPy) quite often, probably at a refresh rate
>> suitable for animation.
(Continue reading)

Rich E | 8 Dec 15:09

Re: converting c float array to numpy array or python list

On Mon, Dec 1, 2008 at 8:43 PM, Christopher Barker
<Chris.Barker <at> noaa.gov> wrote:
> Rich E wrote:
>>
>> So I have a nice C library wrapped in python and I'd like to start
>> doing some plots of things in python... the problem is the data is all
>> stuck in C arrays.
>
>> But how would I go about returning a python list of the entire array,
>> or even better, an NumPy array?
>
> there is a numpy.i file distributed with numpy ( or with the docs, anyway).
> There was also a recent post on the numpy list with some extra hints and
> examples for using it.
>
> This is another nice article about it:
>
> http://matt.eifelle.com/2008/11/04/exposing-an-array-interface-with-swig-for-a-cc-structure/

After looking at this example, I have to admit that I am a bit
befuddled.  I also couldn't get the code to work, but this is probably
because I am not understanding the example.  So, I'll ask a couple
questions here and if there is a better place to do this, please let
me know.

In the example, Matt is converting data in a c struct.. all I want to
do is, provided a pointer to a c array and the size of the array,
convert it to a numpy array.  Hopefully then I will not have to write
a wrapper for every type of struct I have (and even worse, every
function that handles pointers to arrays).
(Continue reading)

Rich E | 21 Dec 19:12

Re: converting c float array to numpy array or python list

Well I've made progress.  In the numpy svn there is a good deal of
documentation about how to use the typemaps in numpy.i (btw, none of
which is related to the posted link I was previously/am confused
about).

It is quite simple to add an %apply directive in the interface file like:

%apply (int DIM1, float* INPLACE_ARRAY1) {(int sizeWaveform, float* pWaveform)};

which will then search and replace any function that has the two
variables in this format, effectively allowing the function to operate
on a numpy array.

But what if I have multiple array pointers in the same function?  For
example, here is a function in my library that converts spectra in
rectangular coordinates to polar form:

/*! \brief compute magnitude spectrum of a DFT
 *
 * \param sizeMag	       size of output Magnitude (half of input real FFT)
 * \param pFReal	       pointer to input FFT real array (real/imag floats)
 * \param pFMAg	       pointer to float array of magnitude spectrum
 */
void sms_spectrumMag( int sizeMag, float *pInRect, float *pOutMag)
{
        int i, it2;
        float fReal, fImag;

	for (i=0; i<sizeMag; i++)
	{
(Continue reading)

Rich E | 21 Dec 20:07

Re: converting c float array to numpy array or python list

I'm going to expand on what I think I need to do, which is modify the
typemap to fit my specific format.  So this is what I'm thinking:

I start with this code for the typemap:

/* Typemap suite for (DIM_TYPE DIM1, DATA_TYPE* INPLACE_ARRAY1)
 */
%typecheck(SWIG_TYPECHECK_DOUBLE_ARRAY,
           fragment="NumPy_Macros")
  (DIM_TYPE DIM1, DATA_TYPE* INPLACE_ARRAY1)
{
  $1 = is_array($input) && PyArray_EquivTypenums(array_type($input),
                                                 DATA_TYPECODE);
}
%typemap(in,
         fragment="NumPy_Fragments")
  (DIM_TYPE DIM1, DATA_TYPE* INPLACE_ARRAY1)
  (PyArrayObject* array=NULL, int i=0)
{
  array = obj_to_array_no_conversion($input, DATA_TYPECODE);
  if (!array || !require_dimensions(array,1) || !require_contiguous(array)
      || !require_native(array)) SWIG_fail;
  $1 = 1;
  for (i=0; i < array_numdims(array); ++i) $1 *= array_size(array,i);
  $2 = (DATA_TYPE*) array_data(array);
}

and try to alter it to allow for a conversion of type:
(DIM_TYPE DIM1, DATA_TYPE* ARRAY1, DATA_TYPE* ARRAY2)
where ARRAY1 is size DIM1 * 2 and  ARRAY2 is size DIM1.  Then I can
(Continue reading)


Gmane