Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython numpy array shape, tuple assignments

I use the idiom

 size_x, size_y, size_z = some_array.shape

quite often when dealing with numpy arrays. The same thing doesn't seem to work in Cython when the array in question has a type, e.g.

 def someFunc(np.ndarray[np.float32_t, ndim=2] arr):
      sx, sy = arr.shape

we end up with a compile error like

  Cannot convert 'npy_intp *' to Python object

which is possibly the result of the fact that "shape" gets converted to a C array (for faster access), so it's no longer a tuple.

Is it possible to extract this tuple somehow even in Cython? (Or should I just stick with sx, sy = arr.shape[0], arr.shape[1]?)

like image 578
Latanius Avatar asked Nov 06 '12 02:11

Latanius


1 Answers

I believe that you are correct that the straight forward way to deal with this is something like:

cdef int sx, sy
sx = arr.shape[0]
sy = arr.shape[1]

I am not aware of another way to do this, and this is the convention I use in my own code.

like image 62
JoshAdel Avatar answered Nov 19 '22 09:11

JoshAdel