[Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array typemap to share?

Xavier Gnata gnata at obs.univ-lyon1.fr
Sat Sep 8 15:20:37 EDT 2007


Christopher Barker wrote:
> Xavier Gnata wrote:
>   
>> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the 
>> conversion but my code is written by hands.
>>     
>
> I'd like to see that. How are you getting the pointer to pass in to 
> PyArray_SimpleNewFromData? It looks like you can do something like:
>
> (VA is a valarray<double>)
> npy_intp *dims
> dims[0] = VA.size()
>
> NPA = PyArray_SimpleNewFromData(1, dims, typenum, &VA[0]);
>
> Is that what you're doing? Is there any guarantee that &VA[0] won't 
> change? In any case, I assume that you have to make sure that VA doesn't 
> get deleted while the array is still around.
>
>   
>> I would like to simplify it using SWIG  but I also would like to see a good typemap valarray <=>
>> numpy.array :)
>>     
>
> In principle, if you know how to write the code by hand, you know how to 
> write the typemap.
>
>
>   

Here it is :) OK it is only a smal code but I often use something like 
that to debug my C++ code (the goal is just to have a quick look to 
larrge array to unnderstand what is going wrong).

#include <python2.4/Python.h>
#include <numpy/arrayobject.h>
#include <valarray>

using namespace std;

int
main (int argc, char *argv[])
{
  PyObject *pylab_module, *pylab_module_dict, *func_imshow, *func_show, 
*args,
    *result_imshow, *result_show, *array;

  int NbDims = 2;
  int *Dims;
  long NbData;

  Dims = new int[NbDims];
  Dims[0] = 10;
  Dims[1] = 10;

  NbData = Dims[0] * Dims[1];


  valarray < double >Data (NbData);
  for (long i = 0; i < NbData; i++)
    {
      Data[i] = (double) i / (NbData - 1);
    }

  Py_Initialize ();

// Needed before any call to PyArray_foo function.
  import_array1 (-1);

  // New reference to a numpy array
  array = PyArray_SimpleNewFromData (NbDims, Dims, PyArray_DOUBLE, 
&Data[0]);

  pylab_module = PyImport_Import (PyString_FromString ("pylab"));
  if (pylab_module)
    {
      pylab_module_dict = PyModule_GetDict (pylab_module);
      if (pylab_module_dict)
    {
      func_imshow = PyDict_GetItemString (pylab_module_dict, "imshow");
      if (func_imshow)
        {
          func_show = PyDict_GetItemString (pylab_module_dict, "show");
          if (func_show)
        {

          args = PyTuple_New (1);
          PyTuple_SetItem (args, 0, array);
          result_imshow = PyObject_CallObject (func_imshow, args);
          Py_XDECREF (result_imshow);    // We dont use the result...
          Py_XDECREF (args);
          result_show = PyObject_CallObject (func_show, NULL);
          Py_XDECREF (result_show);    // We dont use the result...
          Py_XDECREF (array);
        }
        }
    }
      Py_XDECREF (pylab_module);
    }

  Py_Finalize ();

  return 0;
}


"In principle, if you know how to write the code by hand, you know how to 
write the typemap."


Yes but I had to spend a bit more time on that. Hand written code I have posted fit my (debug) needs so I decided not to use SWIG.


"Is that what you're doing? Is there any guarantee that &VA[0] won't 
change? In any case, I assume that you have to make sure that VA doesn't 
get deleted while the array is still around.
"

Yes I have but it is not a problem in my simple use cases.

Xavier


-- 
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles André
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################ 




More information about the NumPy-Discussion mailing list