From 91e0c9a4546dc9c2e0307a7099648073f4bbce03 Mon Sep 17 00:00:00 2001 From: Dohyeon Lee Date: Sun, 24 Nov 2024 21:51:31 +0900 Subject: [PATCH 1/3] Support unsafe casting in Delta filter --- numcodecs/delta.py | 2 +- numcodecs/tests/test_delta.py | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/numcodecs/delta.py b/numcodecs/delta.py index f6307312..a76ce483 100644 --- a/numcodecs/delta.py +++ b/numcodecs/delta.py @@ -67,7 +67,7 @@ def encode(self, buf): if arr.dtype == bool: np.not_equal(arr[1:], arr[:-1], out=enc[1:]) else: - np.subtract(arr[1:], arr[:-1], out=enc[1:]) + np.subtract(arr[1:], arr[:-1], out=enc[1:], casting='unsafe') return enc diff --git a/numcodecs/tests/test_delta.py b/numcodecs/tests/test_delta.py index 9664efee..e5976711 100644 --- a/numcodecs/tests/test_delta.py +++ b/numcodecs/tests/test_delta.py @@ -13,19 +13,20 @@ # mix of dtypes: integer, float # mix of shapes: 1D, 2D, 3D # mix of orders: C, F +# mix of encoding types: All available types for each arrays arrays = [ - np.random.randint(0, 1, size=110, dtype='?').reshape(10, 11), - np.arange(1000, dtype=' Date: Sun, 24 Nov 2024 22:03:56 +0900 Subject: [PATCH 2/3] ruff formatting --- numcodecs/tests/test_delta.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/numcodecs/tests/test_delta.py b/numcodecs/tests/test_delta.py index e5976711..68c2dc82 100644 --- a/numcodecs/tests/test_delta.py +++ b/numcodecs/tests/test_delta.py @@ -15,13 +15,17 @@ # mix of orders: C, F # mix of encoding types: All available types for each arrays arrays = [ - (np.random.randint(0, 1, size=110, dtype='?').reshape(10, 11), ('?',' Date: Sat, 30 Nov 2024 21:24:22 +0900 Subject: [PATCH 3/3] add floating point overflow unit test for Delta filter --- numcodecs/tests/test_delta.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/numcodecs/tests/test_delta.py b/numcodecs/tests/test_delta.py index 68c2dc82..21c49988 100644 --- a/numcodecs/tests/test_delta.py +++ b/numcodecs/tests/test_delta.py @@ -64,3 +64,35 @@ def test_errors(): Delta(dtype=object) with pytest.raises(ValueError): Delta(dtype='i8', astype=object) + + +# overflow tests +# Note: Before implementing similar test for integer -> integer types, check numpy/numpy#8987. +oveflow_proned_float_float_pairs = [ + ('f4', 'f2'), + ('f8', 'f4'), +] + + +def test_oveflow_proned_float_float_encode(): + for dtype, astype in oveflow_proned_float_float_pairs: + codec = Delta(dtype=dtype, astype=astype) + arr = np.array([0, np.finfo(astype).max.astype(dtype) * 2], dtype=dtype) + with pytest.warns(RuntimeWarning, match=r"overflow encountered"): + codec.encode(arr) + + +overflow_proned_integer_float_paris = [ + ('i4', 'f2'), + ('i8', 'f2'), + ('u4', 'f2'), + ('u8', 'f2'), +] + + +def test_oveflow_proned_integer_float_encode(): + for dtype, astype in overflow_proned_integer_float_paris: + codec = Delta(dtype=dtype, astype=astype) + arr = np.array([0, int(np.rint(np.finfo(astype).max)) * 2], dtype=dtype) + with pytest.warns(RuntimeWarning, match=r"overflow encountered"): + codec.encode(arr)