Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing numpy attribute when using Cython

I have a very simply cython module called empty_test.pyx:

cimport numpy as cnp


cpdef return_empty():
    return cnp.empty(0, dtype=np.int32)

When I try to run return_empty I get this error:

empty_test.pyx:5:14: cimported module has no attribute 'empty'

This is my setup.py file:

from distutils.core import setup
from Cython.Build import cythonize

import numpy as np
setup(
    ext_modules=cythonize(['empty_test.pyx'],
    ),
    include_dirs = [np.get_include()],
)

I'm aware that I could try import numpy as np instead of cimport numpy as np, but I'm trying to use C versions of the numpy code.

like image 514
Ginger Avatar asked Oct 24 '25 03:10

Ginger


1 Answers

Inorder to achieve that, you have to access numpy's C-API directly, which is at least partly wrapped by Cython. In your case you need PyArray_SimpleNew, which is already cimported with numpy.

Thus, your function becomes:

%%cython
cimport numpy as cnp

cnp.import_array()  # needed to initialize numpy-API

cpdef return_empty():
    cdef cnp.npy_intp dim = 0
    return cnp.PyArray_SimpleNew(1, &dim, cnp.NPY_INT32)

And now:

>>> return_empty()
array([], dtype=int32)

Obviously, there is still some Python overhead because of the reference-counting but it is much less, as when using np.empty():

>>> %timeit return_empty()   
159 ns ± 2.81 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
>>> %timeit return_empty_py
751 ns ± 8.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Using PyArray_SimpleNew is also faster (about 3 times) than using Cython's array (as considered by you in another question):

%%cython
from cython.view cimport array as cvarray

# shape=(0,) doesn't work
cpdef create_almost_empty_carray():
    return cvarray(shape=(1,), itemsize=sizeof(int), format="i")

and thus:

>>> %timeit create_almost_empty_carray()
435 ns ± 5.85 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Listing of used function return_empty_py:

%%cython
cimport numpy as cnp
import numpy as np

cpdef return_empty_py():
    return np.empty(0, np.int32)
like image 50
ead Avatar answered Oct 26 '25 17:10

ead



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!