MAINT: Prevent OverflowError in multiplication#230
Merged
cbespin merged 2 commits intoSiLab-Bonn:masterfrom Oct 31, 2024
Merged
MAINT: Prevent OverflowError in multiplication#230cbespin merged 2 commits intoSiLab-Bonn:masterfrom
cbespin merged 2 commits intoSiLab-Bonn:masterfrom
Conversation
In newer numpy versions the multiplication of ndarrays containing uint8 results in uint8. Here this leads to an OverflowError. Therefore changing to an uint64 before the multiplication prevents this.
cbespin
approved these changes
Oct 10, 2024
Contributor
cbespin
left a comment
There was a problem hiding this comment.
Nice catch! Only a short comment, maybe personal style and happy to discuss if you think differently
basil/utils/utils.py
Outdated
| ba.reverse() # this flip the byte order and the bit order of each byte | ||
| bs = np.frombuffer(ba.tobytes(), dtype=np.uint8) # byte padding happens here, bitarray.tobytes() | ||
| bs = (bs * 0x0202020202 & 0x010884422010) % 1023 | ||
| bs = (bs.astype(np.uint64) * 0x0202020202 & 0x010884422010) % 1023 |
Contributor
There was a problem hiding this comment.
I would argue it is a bit easier to follow if you do bs * np.uint64(0x0202020202) to make the casting more explicit and keep bs an array of uint8 aka bytes. Performance seems to be about the same with a very simple test.
MarcoVogt
added a commit
that referenced
this pull request
Oct 27, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In newer numpy versions the multiplication of ndarrays containing uint8 results in uint8. Here this leads to an OverflowError. Therefore changing to an uint64 before the multiplication prevents this.