Kris Thielemans
2015-10-18 09:15:39 UTC
Hi
I'm trying to get numpy support into our STIR library using SWIG and intend
to use the SWIG files supplied with numpy (or is there a better option?).
I'm running into the issue mentioned on
http://docs.scipy.org/doc/numpy/reference/swig.interface-file.html#numpy-arr
ay-scalars-and-swig
i.e. when having a numpy scalar, the current SWIG converters don't recognise
it. The numpy folks supply a pyfragments.swg to add fragments
SWIG_AsVal_frag(long) and SWIG_AsVal_frag(unsigned long):
https://github.com/numpy/numpy/blob/master/tools/swig/pyfragments.swg
There is no fragment for double though. It's easy enough to add one, but I
have a few questions:
- to me, it seems terribly unsafe (i.e. non-maintainable) to copy the
fragment code from pyprimtypes.swg and then add some numpy specific to it.
It would be far better to be able to call the "original" function, and then
do numpy checks if the "original" SWIG_AsVal failed. Is that possible?
- There is no SWIG_AsVal(float) in pyprimtypes.swg, as it relies on
conversion from double to float. This works, but I guess for numpy can load
to numpy::float32->double->float conversions (with lots of overhead along
the way). I don't mind too much, but is there an easy way around that?
- there is an open issue about a memory leak in the current numpy
pyfragments.swg, see https://github.com/numpy/numpy/issues/5181. I think
that concern raised is correct and intend to add the Py_DECREF as suggested.
I'm ending up with code like
if (PyArray_IsScalar(obj,Double))
{
PyArray_Descr * descr = PyArray_DescrNewFromType(NPY_DOUBLE);
PyArray_CastScalarToCtype(obj, (void*)val, descr);
Py_DECREF(descr);
return SWIG_AddCast(SWIG_OK);
}
See attached pyfragments.swg.
Any comments?
Apologies if you think this should go to the numpy list, but I'm hoping for
good response here!
Kris
I'm trying to get numpy support into our STIR library using SWIG and intend
to use the SWIG files supplied with numpy (or is there a better option?).
I'm running into the issue mentioned on
http://docs.scipy.org/doc/numpy/reference/swig.interface-file.html#numpy-arr
ay-scalars-and-swig
i.e. when having a numpy scalar, the current SWIG converters don't recognise
it. The numpy folks supply a pyfragments.swg to add fragments
SWIG_AsVal_frag(long) and SWIG_AsVal_frag(unsigned long):
https://github.com/numpy/numpy/blob/master/tools/swig/pyfragments.swg
There is no fragment for double though. It's easy enough to add one, but I
have a few questions:
- to me, it seems terribly unsafe (i.e. non-maintainable) to copy the
fragment code from pyprimtypes.swg and then add some numpy specific to it.
It would be far better to be able to call the "original" function, and then
do numpy checks if the "original" SWIG_AsVal failed. Is that possible?
- There is no SWIG_AsVal(float) in pyprimtypes.swg, as it relies on
conversion from double to float. This works, but I guess for numpy can load
to numpy::float32->double->float conversions (with lots of overhead along
the way). I don't mind too much, but is there an easy way around that?
- there is an open issue about a memory leak in the current numpy
pyfragments.swg, see https://github.com/numpy/numpy/issues/5181. I think
that concern raised is correct and intend to add the Py_DECREF as suggested.
I'm ending up with code like
if (PyArray_IsScalar(obj,Double))
{
PyArray_Descr * descr = PyArray_DescrNewFromType(NPY_DOUBLE);
PyArray_CastScalarToCtype(obj, (void*)val, descr);
Py_DECREF(descr);
return SWIG_AddCast(SWIG_OK);
}
See attached pyfragments.swg.
Any comments?
Apologies if you think this should go to the numpy list, but I'm hoping for
good response here!
Kris