-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use different alg for two_byte_sum that fixes off-by-one error #315
Conversation
This is just a draft and I haven't looked at the full git history, but it is complaining about ruff fixes that were fixed in #312 so probably needs a rebase. |
258ce59
to
ebfc72a
Compare
ebfc72a
to
038546c
Compare
# Make a 2xN array, then transpose to Nx2, then flatten to 2N, then copy to | ||
# get values continous in memory. | ||
bytes8_2xN = np.ma.vstack([bytes0, bytes1], dtype=np.uint8) | ||
bytes8 = bytes8_2xN.transpose().flatten().copy() | ||
|
||
# Now view the 2N bytes as N 16-bit signed integers. | ||
ints16 = np.ma.array(bytes8.data.view(">i2"), mask=bytes8.mask[::2]) |
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.
Thanks for the fix! Sorry I was not paying enough attention to the way to transform bytes to ints in general. I do wonder, now that I'm paying more attention, if instead of ending up with transpose().flatten().copy() + view if an updated shift and cast would end up being more readable
ints16 = ((bytes0.astype(np.uint16) << 8) | bytes1).astype(np.int16)
What are your thoughts?
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.
(though I suppose I'd need to figure out if there are actually masked values in there anywhere and what happens to them)
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.
What you wrote might be more concise and (with more investigation and testing) might work. But this PR is already so far down in the weeds, let's just go with the already-tested version.
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.
Looks good to me. My only concern is that it looks to me like the new code returns a masked array even if that wasn't supplied. I think for our current use cases we don't care.
Description
This fixes a bug in
two_byte_sum
where it is off by one for negative integers.Interface impacts
None
Testing
Unit tests
Independent check of unit tests by Jean
Functional tests
This looks right.
As a reviewer I, Jean, also just looked at this and this makes sense to me to appropriately cover the range of signed 16 bit ints with the new code.
The previous code/output just didn't succeed in covering the range.