bitwise view on numpy array
Hi, I'm gonna have large (e.g. 2400x2400) arrays of 16 and 32 bit bitfields. I've been searching in vain for an efficient and convenient way to represent these array's individual bit's (or, even better, configureable bitfields of 1-4 bits each). Of course I know I can 'split' the array in its separate bitfields using bitwise operators and shifts, but this will greatly increase the memory usage because it'll create one byte array for each bitfield. So I was looking for a way to create a bitwise view on the original array's data. I've been looking at recarray's, but the smallest element these can use are bytes, correct?. I've been looking at ctypes arrays of Structure subclasses, which can define bitfields. However, these will give me an object array of elements with the Structure class subclass, and only allow me to access the bits per array element instead of for the entire array (or a subset), e.g. data[:].bit17-19 or someting like that. After searching the net in vain for some hours, the list is my last resort :-) Anyone having ideas of how to get both memory-efficient and convenient access to single bits of a numpy array? On a slightly related note, during my search I found some comments saying that numpy.bool arrays use an entire byte for each element. Could someone confirm (or, better, negate) that? Thanks, Vincent.
Hi Vincent Take a look at http://pypi.python.org/pypi/bitarray/ I'm not sure if you can initialise bitarrays from NumPy arrays. If not, you'll have to implement a conversion scheme, but that can be done without making a copy. Regards Stéfan 2009/5/6 Vincent Schut <schut@sarvision.nl>:
Hi,
I'm gonna have large (e.g. 2400x2400) arrays of 16 and 32 bit bitfields. I've been searching in vain for an efficient and convenient way to represent these array's individual bit's (or, even better, configureable bitfields of 1-4 bits each).
participants (2)
-
Stéfan van der Walt
-
Vincent Schut