Skip to content

Commit

Permalink
Update optimized Delta codec for bool (#595)
Browse files Browse the repository at this point in the history
* Update optimized `Delta` codec for `bool`

Previously `bool` just worked with `Delta`. However this was not
actually tested. The optimized version switched to `np.subtract` for
in-place computation, which works for other types. Though `bool` needs
special handling. Fortunately this can be done with `np.not_equal`,
which has the same behavior.

Also include a test for `bool` data to make sure this is handled
correctly going forward.

* Simplify type check

This is a bit more succinct and gets to the core point. Namely
`arr.dtype` determines this code path.

Also comparing directly to `bool` works here. It is a bit faster as well
since we need not construct an `np.dtype` object.

* Drop extraneous `0`s
  • Loading branch information
jakirkham authored Oct 12, 2024
1 parent 9518e0b commit c283106
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion numcodecs/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def encode(self, buf):

# compute differences
# using np.subtract for in-place operations
np.subtract(arr[1:], arr[0:-1], out=enc[1:])
if arr.dtype == bool:
np.not_equal(arr[1:], arr[:-1], out=enc[1:])
else:
np.subtract(arr[1:], arr[:-1], out=enc[1:])

return enc

Expand Down
1 change: 1 addition & 0 deletions numcodecs/tests/test_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# mix of shapes: 1D, 2D, 3D
# mix of orders: C, F
arrays = [
np.random.randint(0, 1, size=110, dtype='?').reshape(10, 11),
np.arange(1000, dtype='<i4'),
np.linspace(1000, 1001, 1000, dtype='<f4').reshape(100, 10),
np.random.normal(loc=1000, scale=1, size=(10, 10, 10)).astype('<f8'),
Expand Down

0 comments on commit c283106

Please sign in to comment.