I’m attempting to optimise a simple cython routine I’ve written that accepts a 1D numpy array as input and returns a 1D numpy array as output.
I have a naive version working, and am now attempting to do some standard optimisations to the numpy parts of the cython code.
However, as soon as I add cimport numpy to my cython source file, I am unable to run the extension module (though it compiles okay), throwing a ValueError: numpy.dtype does not appear to be the correct type object.
My numpy version is 1.6.0 and python version 2.6.5 (default, Ubuntu 10.04 apt-get installations)
Minimal example causing the problem:
test_ext.pyx
import numpy as np
cimport numpy as np #Commenting this line un-breaks the extension
def test_ext(refls, str mode, int nguard, int nblock):
cdef int i
cdef int _len = refls.shape[0]
_out = np.zeros([_len])
for i in range(_len):
_out[i] = refls[i] + 1
return _out
setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("test_ext", ["test_ext.pyx"])]
setup(
name = 'test',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
run_ext.py
import numpy
from test_ext import test_ext
a = np.array([x for x in range(260)])
_res = test_ext.test_ext(a, 'avg', 2, 3)
build with:
python setup.py build_ext --inplace
My build output:
running build_ext
cythoning my_ext.pyx to my_ext.c
building 'my_ext' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c my_ext.c -o build/temp.linux-x86_64-2.6/my_ext.o
/usr/include/python2.6/numpy/__multiarray_api.h:968: warning: ‘_import_array’ defined but not used
my_ext.c:732: warning: ‘__pyx_k_3’ defined but not used
my_ext.c:733: warning: ‘__pyx_k_4’ defined but not used
my_ext.c:753: warning: ‘__pyx_k_24’ defined but not used
my_ext.c:759: warning: ‘__pyx_k_26’ defined but not used
my_ext.c:760: warning: ‘__pyx_k_27’ defined but not used
my_ext.c:1118: warning: ‘__pyx_pf_5numpy_7ndarray___getbuffer__’ defined but not used
my_ext.c:1876: warning: ‘__pyx_pf_5numpy_7ndarray___releasebuffer__’ defined but not used
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.6/my_ext.o -o /home/path/to/my_ext.so
and run then run_ext.py.
With the cimport line uncommented, I get the error:
Traceback (most recent call last):
File "run_ext.py", line 2, in <module>
from test_ext import test_ext
File "numpy.pxd", line 43, in test_ext (test_ext.c:2788)
ValueError: numpy.dtype does not appear to be the correct type object
What causes this and how can I fix it to continue optimising my extension? I have attempted to continue with the optimisations as per the link above, but however far along I get, nothing un-breaks this issue for me.
I’ve seen some other people with this problem that had version problems with python/numpy, but I don’t think that’s the case for me (though I’m open to suggestion there). Any suggestions appreciated.
Looks like my build file just needed to include the numpy include files. Changing setup.py to what is below fixed everything:
setup.py