Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy.packbits doesn't accept a bool array #5377

Closed
Dunes opened this issue Dec 18, 2014 · 5 comments
Closed

numpy.packbits doesn't accept a bool array #5377

Dunes opened this issue Dec 18, 2014 · 5 comments

Comments

@Dunes
Copy link

Dunes commented Dec 18, 2014

Example of problem

data = numpy.array([True])
byte_values = numpy.packbits(data)

This results with an exception -- TypeError: Expected an input array of integer data type. This seems to go against the documentation of the function, which says it expects a 'binary-valued array'.

To make this work, you need to first convert the data type of the array to int, which seems redundant. eg.

byte_values = numpy.packbits(numpy.array(data, dtype=int))

A better use case of where I encountered the problem.

data = numpy.random.sample(1024)
truth_values = data >= threshold
bits = numpy.packbits(truth_values)
@LuisBL
Copy link

LuisBL commented Dec 29, 2015

This is a real problem, because packbits is the only workaround the fact that np.array of boolean use 8bit by boolean. This is a limit to how compact a memory structure can be.

@LuisBL
Copy link

LuisBL commented Dec 29, 2015

In [8]: x = np.array([0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1], dtype=np.bool)

In [9]: px = np.packbits(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-326e19e2360b> in <module>()
----> 1 px = np.packbits(x)

TypeError: Expected an input array of integer data type
In [10]

@LuisBL
Copy link

LuisBL commented Dec 29, 2015

@alimuldal
Copy link
Contributor

@LuisBL There is no need to cast the input array to the native int dtype - you can avoid generating a copy simply by viewing it as np.uint8, which also uses a single byte per element, e.g.:

px = np.packbits(x.view(np.uint8))

@jaimefrio
Copy link
Member

That feature was added in #5319, it does work in current master, so closing this.

briggySmalls added a commit to briggySmalls/pyflipdot that referenced this issue Mar 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants