Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Cython: Using SciPy with Cython

The Cython tutorial shows a nice example of how to use Numpy with Cython. However, I have code that uses the scipy.stats package and when attempting to compile the code, I errors such as:

dvi.pyx:7:8: 'scipy.stats.pxd' not found

I fear that scipy is not supported with Cython (?). Can someone comment on the use of scipy with Cython or point me in the direction of some resources/tutorials? Thannks!

like image 489
Jason Strimpel Avatar asked Jul 27 '12 00:07

Jason Strimpel


People also ask

Does SciPy use Cython?

All files with a . pyx extension are automatically converted by Cython to . c files when SciPy is built.

Can I use Python libraries in Cython?

Because Cython code compiles to C, it can interact with those libraries directly, and take Python's bottlenecks out of the loop. But NumPy, in particular, works well with Cython. Cython has native support for specific constructions in NumPy and provides fast access to NumPy arrays.

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.

Is Cython better than Python?

The CPython + Cython implementation is the fastest; it is 44 times faster than the CPython implementation. This is an impressive speed improvement, especially considering that the Cython code is very close to the original Python code in its design.


1 Answers

So I found code on the Cython Google Group (https://groups.google.com/forum/?fromgroups#!searchin/cython-users/using$20scipy/cython-users/CF9GqYN1aPU/WKC-N9c6zpgJ)

That shows the following as imports:

import pylab as PL
from scipy import integrate
from scipy import optimize
from scipy.integrate import odeint

import numpy as np
cimport numpy as np
cimport cython

Which gave me confidence I could compile with SciPy. When adding the cimport cython statement, I receive the following compilation errors:

dvi.c:237:31: error: numpy/arrayobject.h: No such file or directory
dvi.c:238:31: error: numpy/ufuncobject.h: No such file or directory

Which seemed like there was a path or directory issue. In fact I was correct and there is a post on this site (My Cython code parses into C, but doesn't compile. First time trying to use external C code)

The solution was to add the following to my setup.py file:

import numpy 
...
Extension(..., include_dirs = [numpy.get_include(), ... ] )

W00t!

like image 121
Jason Strimpel Avatar answered Sep 18 '22 01:09

Jason Strimpel