Jake Vanderplas
unread,Feb 18, 2012, 11:14:09 AM2/18/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cython...@googlegroups.com
Hi all,
I was wondering if I could get some input on this: I
have an application where I need to quickly index a numpy boolean
array in a cython script. If it were an integer or float array, I know how I'd do this, using templated ndarray types as in the online tutorials. It doesn't appear that the same solution is available for boolean types.
Below is a simple example of summation using the solution I came up with.
It works in the few cases I've tried, but I'd appreciate feedback: are there any details I'm missing that could make this sort of solution unstable? Is there a more elegant way to quickly loop through a boolean array in cython? My mask array is large enough that converting it to an int type is not an option.
Thanks
Jake
import numpy as np
cimport numpy as np
# Here's how I would quickly cycle through an integer array
def sum_int(np.ndarray[int, ndim=2, mode='c'] mask):
cdef unsigned int i, j, N1, N2, tot
tot = 0
N1 = mask.shape[0]
N2 = mask.shape[1]
for i from 0 <= i < N1:
for j from 0 <= j < N2:
if mask[i, j] != 0:
tot += 1
return tot
# cython does not support any bool dtype, so here's how I quickly cycle through a boolean array
def sum_bool(np.ndarray mask):
assert mask.dtype == bool
assert mask.flags['C_CONTIGUOUS']
cdef np.npy_bool* mptr = <np.npy_bool*> mask.data
cdef unsigned int N = mask.size
cdef unsigned int i, tot=0
for i from 0 <= i < N:
if mptr[i] != 0:
tot += 1
return tot