Skip to content

Commit c283106

Browse files
authored
Update optimized Delta codec for bool (#595)
* 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
1 parent 9518e0b commit c283106

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

numcodecs/delta.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ def encode(self, buf):
6464

6565
# compute differences
6666
# using np.subtract for in-place operations
67-
np.subtract(arr[1:], arr[0:-1], out=enc[1:])
67+
if arr.dtype == bool:
68+
np.not_equal(arr[1:], arr[:-1], out=enc[1:])
69+
else:
70+
np.subtract(arr[1:], arr[:-1], out=enc[1:])
6871

6972
return enc
7073

numcodecs/tests/test_delta.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# mix of shapes: 1D, 2D, 3D
1515
# mix of orders: C, F
1616
arrays = [
17+
np.random.randint(0, 1, size=110, dtype='?').reshape(10, 11),
1718
np.arange(1000, dtype='<i4'),
1819
np.linspace(1000, 1001, 1000, dtype='<f4').reshape(100, 10),
1920
np.random.normal(loc=1000, scale=1, size=(10, 10, 10)).astype('<f8'),

0 commit comments

Comments
 (0)