Boolean numpy arrays with Cython
I have a numpy boolean array:
myarr = np.array([[False, True], [True, False]])
If I try to initialise a Cython MemoryView with it, like this:
cdef bint[:,:] mymem = myarr
I get this error:
ValueError: Does not understand character buffer dtype format string ('?')
If I do this instead, it works fine:
cdef np.int_t[:,:] mymem = np.int_(myarr)
How can I store a boolean numpy array using Cython MemoryViews?
I ran into the same problem some time ago. Unfortunately I did not find a direct solution to this. But there is another approach: Since an array of boolean vales has the same data type size as
uint8, you could use a memory view with this type as well. Values in the
uint8 memory view can also be compared to boolean values, so the behavior is mostly equal to an actual
bint memory view:
cimport cython cimport numpy as np import numpy as np ctypedef np.uint8_t uint8 cdef int i cdef np.ndarray array = np.array([True,False,True,True,False], dtype=bool) cdef uint8[:] view = np.frombuffer(array, dtype=np.uint8) for i in range(view.shape): if view[i] == True: print(i)
0 2 3
Memoryview of boolean ndarrays · Issue #2204 · cython/cython , Example: cimport cython cimport numpy as np import numpy as np cdef np.ndarray array = np.array([True, True, False, True], dtype=np.bool) Boolean arrays in NumPy are simple NumPy arrays with array elements as either ‘True’ or ‘False’. Other than creating Boolean arrays by writing the elements one by one and converting them into a NumPy array, we can also convert an array into a ‘Boolean’ array in some easy ways, that we will look at here in this post.
This information seems to be not easy to find, my reference is pretty old (2011), but not much seems to have changed since then.
Numpy's bool-array uses a 8bit-value for False/True (this is not obvious per se - C++'s
std::vector<bool> uses for example 1 bit per value) with
True. You can use
cast=True for an
unit8-array in order to use it as a
bool-array, for example:
%%cython import numpy as np cimport numpy as np def to_bool_array(lst): cdef np.ndarray[np.uint8_t, ndim = 1, cast=True] res res=np.array(lst, dtype=bool) return res
>>> to_bool_array([True,False,True,False]) array([ True, False, True, False], dtype=bool)
cast=True gives some slack to Cython's type-checking, so the numpy-arrays with the same element-size (for example
bool) can be reinterpreted. This however would not work if element-sizes were different: for example
Using Cython with NumPy, Using Cython with NumPy. Cython has support for fast access to NumPy arrays. To optimize code using such arrays one must cimport the NumPy pxd file (which ships with Cython), and declare any arrays as having the ndarray type. The data type and number of dimensions should be fixed at compile-time and passed. We’ll say that array_1 and array_2 are 2D NumPy arrays of integer type and a, b and c are three Python integers. This function uses NumPy and is already really fast, so it might be a bit overkill to do it again with Cython. This is for demonstration purposes.
I found it simplest to do:
cdef uint8_t[:] arr_memview8 = data.astype(np.uint8) bool* ptr = <bool*>&arr_memview8
how to get a numpy bool array?, a numpy bool array in Cython (typed as such) IIRC, Numpy boolean arrays are stored in a packed format, one bit per element (requiring Python has a builtin array module supporting dynamic 1-dimensional arrays of primitive types. It is possible to access the underlying C array of a Python array from within Cython. At the same time they are ordinary Python objects which can be stored in lists and serialized between processes when using multiprocessing.
Fast indexing of a boolean numpy array, 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 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. Let’s see how this works with a simple example.
[Numpy-discussion] Creating a bool array with Cython, [Numpy-discussion] Creating a bool array with Cython. Neal Becker ndbecker2 at gmail.com. Sun Feb 26 09:52:16 EST 2012. Previous message (by thread): Booleans represent one of two values: True or False. In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False. When you compare two values, the expression is evaluated and Python returns the Boolean answer: When you run a condition in an if
[PDF] NumPy User Guide - Numpy and Scipy, fast operations on arrays, including mathematical, logical, shape manipulation on PyPi include the C files generated from Cython code, so for Cython uses the normal C syntax for C types, including pointers. It provides all the standard C types, namely char, short, int, long , long long as well as their unsigned versions, e.g. unsigned int . The special bint type is used for C boolean values ( int with 0/non-0 values for False/True) and Py_ssize_t for (signed) sizes of Python containers.