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..ece83d30 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='