Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using numpy in cython: defining ndarray datatype/ndims

Tags:

python

cython

I'm trying to write some cython code to do computations with numpy arrays. Cython seems to not like the [] used in all the examples I've seen to define the datatype and number of dimensions.

For example, I have a file test.pyx:

cimport numpy as np
import numpy as np

ctypedef np.ndarray[np.float64_t, ndim=2] mymatrix

cpdef mymatrix hat (mymatrix x):
    a = np.zeros((3,3));
    a[0,1] =  x[2,0];
    a[0,2] = -x[1,0];
    a[1,2] =  x[0,0];
    a[1,0] = -x[2,0];
    a[2,0] =  x[1,0];
    a[2,1] = -x[0,0];
    return a;

I compile this using a setup.py (see end of post), which I run with "python setup.py build_ext --inplace"

I get the following output:

running build_ext
cythoning test.pyx to test.c

Error converting Pyrex file to C:
------------------------------------------------------------
...
cimport numpy as np
import numpy as np

ctypedef np.ndarray[np.float64_t, ndim=2] mymatrix
                                         ^
------------------------------------------------------------

test.pyx:4:42: Syntax error in ctypedef statement

<snip, irrelevant>

whereas if I remove the "[np.float64_t, ndim=2]" part, it works fine.

Does anyone have any ideas?

As to my system setup: OS: Windows XP

full, complete pythonxy installation, version 2.6.5.1 (latest at this point)

pythonxy supposedly comes with cython, but I ended up installing cython version 0.12.1 for Python 2.6 from this site: http://www.lfd.uci.edu/~gohlke/pythonlibs/#cython

I suspect that I somehow am missing a path or something: I solved some problems by explicitly adding the numpy header file directory to the include path used by mingw (see the setup.py file below)

here is that setup.py file I mentioned:

from distutils.core import setup
from distutils.extension import Extension
from distutils.sysconfig import get_python_inc
from Cython.Distutils import build_ext
import os.path

inc_base = get_python_inc( plat_specific=1 );
incdir = os.path.join( get_python_inc( plat_specific=1 ), );

#libraries=['math'],
ext_modules = [Extension("test", 
 ["test.pyx"], 
 include_dirs = [
  os.path.join(inc_base,'..\\Lib\\site-packages\\numpy\\core\\include\\numpy'),
  ]
 )
 ]

setup(
  name = 'test',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)
like image 904
stochastic Avatar asked Jul 31 '10 18:07

stochastic


People also ask

Can I use NumPy in Cython?

You can use NumPy from Cython exactly the same as in regular Python, but by doing so you are losing potentially high speedups because Cython has support for fast access to NumPy arrays.

What data type is NumPy Ndarray?

ndarray. An array object represents a multidimensional, homogeneous array of fixed-size items. An associated data-type object describes the format of each element in the array (its byte-order, how many bytes it occupies in memory, whether it is an integer, a floating point number, or something else, etc.)

Does Cython improve NumPy?

By explicitly declaring the "ndarray" data type, your array processing can be 1250x faster. This tutorial will show you how to speed up the processing of NumPy arrays using Cython. By explicitly specifying the data types of variables in Python, Cython can give drastic speed increases at runtime.

Can NumPy Ndarray hold any type of object?

NumPy arrays are typed arrays of fixed size. Python lists are heterogeneous and thus elements of a list may contain any object type, while NumPy arrays are homogenous and can contain object of only one type.


1 Answers

Put the type information in the declaration of the function, as in:

def hat (ndarray[np.float64_t, ndim=2] x):
    a = np.zeros((3,3));
    a[0,1] =  x[2,0];
    etc.
like image 97
Fabian Pedregosa Avatar answered Nov 13 '22 03:11

Fabian Pedregosa