From 2fdbb6422abe6eca715d87ba6daaf09992f0e93a Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Thu, 8 Aug 2024 19:53:53 -0700 Subject: [PATCH 1/2] adds round_to_int boolean argument to FixedScaleOffset to allow applying this filter without automatically converting to integers. (#549) --- numcodecs/fixedscaleoffset.py | 12 ++++++++---- numcodecs/tests/test_fixedscaleoffset.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/numcodecs/fixedscaleoffset.py b/numcodecs/fixedscaleoffset.py index b4d9469a..d632ebce 100644 --- a/numcodecs/fixedscaleoffset.py +++ b/numcodecs/fixedscaleoffset.py @@ -8,7 +8,7 @@ class FixedScaleOffset(Codec): """Simplified version of the scale-offset filter available in HDF5. Applies the transformation `(x - offset) * scale` to all chunks. Results - are rounded to the nearest integer but are not packed according to the + are optionally rounded to the nearest integer but are not packed according to the minimum number of bits. Parameters @@ -21,6 +21,8 @@ class FixedScaleOffset(Codec): Data type to use for decoded data. astype : dtype, optional Data type to use for encoded data. + round_to_int : boolean + Flag to control rounding encoded data to integers after applying scale and offset Notes ----- @@ -70,10 +72,11 @@ class FixedScaleOffset(Codec): codec_id = 'fixedscaleoffset' - def __init__(self, offset, scale, dtype, astype=None): + def __init__(self, offset, scale, dtype, astype=None, round_to_int=True): self.offset = offset self.scale = scale self.dtype = np.dtype(dtype) + self.round_to_int = round_to_int if astype is None: self.astype = self.dtype else: @@ -91,8 +94,9 @@ def encode(self, buf): # compute scale offset enc = (arr - self.offset) * self.scale - # round to nearest integer - enc = np.around(enc) + if self.round_to_int: + # round to nearest integer + enc = np.around(enc) # convert dtype enc = enc.astype(self.astype, copy=False) diff --git a/numcodecs/tests/test_fixedscaleoffset.py b/numcodecs/tests/test_fixedscaleoffset.py index 8f985c4a..e8d2f379 100644 --- a/numcodecs/tests/test_fixedscaleoffset.py +++ b/numcodecs/tests/test_fixedscaleoffset.py @@ -2,7 +2,7 @@ import numpy as np -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal, assert_allclose import pytest @@ -30,6 +30,7 @@ FixedScaleOffset(offset=1000, scale=10**6, dtype=' Date: Thu, 8 Aug 2024 20:12:50 -0700 Subject: [PATCH 2/2] fix whitespace between test functions --- numcodecs/tests/test_fixedscaleoffset.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numcodecs/tests/test_fixedscaleoffset.py b/numcodecs/tests/test_fixedscaleoffset.py index e8d2f379..ece83d30 100644 --- a/numcodecs/tests/test_fixedscaleoffset.py +++ b/numcodecs/tests/test_fixedscaleoffset.py @@ -50,6 +50,7 @@ def test_encode(): assert_array_equal(expect, actual) assert np.dtype(astype) == actual.dtype + def test_encode_no_round(): dtype = '