-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
BUG: Fix packbits to correctly handle empty arrays #8327
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
Conversation
please squash the test and the fix into one commit, it is better to have the testsuite succeed on every commit for easier bisection. |
330c4f9
to
41d10b5
Compare
Hi @juliantaylor, thank you for your quick feedback. I squashed the two commits into one commit. |
@@ -17,6 +17,22 @@ def test_packbits(): | |||
assert_raises(TypeError, np.packbits, np.array(a, dtype=float)) | |||
|
|||
|
|||
def test_packbits_empty(): | |||
for dtype in [np.bool, np.uint8, np.int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just these three types? And why just the last dimension zero? Tests should be exhaustive. Admittedly, most aren't but, strangely enough, when improved often reveal errors.
All integer types are available in np.typecodes['AllInteger']
, the boolean type is separate. Or you could just use the string '?bBhHiIlLqQ'
for typecodes. If boolean is acceptable I think the docstring need improvement also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not a good idea to shadow the numpy dtype
variable. Use something like dt
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it checked anywhere that empty results unpack correctly?
41d10b5
to
fe7448f
Compare
Hi @charris, thank you for the detailed and accurate feedback!
According to your comments, I improved the tests in
I updated the statement in
You are correct --- I added tests for unpack against empty arrays, and indeed, I found a similar bug in unpack. So, I also updated function |
assert_array_equal(b, np.empty((0,))) | ||
|
||
|
||
def test_packbits_empty_with_axis(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function has the wrong (duplicate) name, so only one of them runs
fe7448f
to
47c66c9
Compare
Sorry for stupid mistake. fixed. |
thanks, very nice pull request. |
This small PR addresses several problems of
numpy.packbits
pointed out in Issue #8324.As discussed in Issue #8324,
numpy.packbits
have problems with regard to empty input arrays. Specifically:uint8
dtype,axis
is specified, andnew
).The fundamental reason for these problems is common: function
pack_bits
incompiled_base.c
handles empty arrays separately, in a rough manner. I could not figure out a good reason why we should handle empty arrays separately. Rather, I found that this part is indeed unnecessary, i.e., without this,pack_bits
can handle empty arrays correctly, as confirmed in my new tests. So, I propose to remove that part to fix these problems.