[Numpy-discussion] neighborhood iterator

David Warde-Farley dwf@cs.toronto....
Sun Nov 22 19:21:40 CST 2009


On 22-Nov-09, at 12:50 PM, Nadav Horesh wrote:

>
> I wonder if the neighbourhood iterator can be used as a more  
> efficient replacement for ndimage.generic_filter. Is there a way to  
> use it from cython?

Yes, using the NumPy C API, called like any other C function is from  
Cython. Something like:

######################

import numpy as np
cimport numpy as np

cdef extern from "numpy/arrayobject.h":
     object PyArray_NeighborhoodIterNew(object iter, np.npy_intp bounds,
                                       int mode, object, np.ndarray  
fill_value)
     int PyArrayNeighborhoodIter_Next(object iter)
     int PyArrayNeighborhoodIter_Reset(object iter)

######################

should do the trick.

Note that you'll need to call np.import_array() before using any of  
these functions to initialize the C API, I think.

David


More information about the NumPy-Discussion mailing list