forked from zarr-developers/numcodecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fixedscaleoffset.py
More file actions
77 lines (60 loc) · 2.47 KB
/
test_fixedscaleoffset.py
File metadata and controls
77 lines (60 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import itertools
import numpy as np
import pytest
from numcodecs.fixedscaleoffset import FixedScaleOffset
from numpy.testing import assert_array_equal
from tests.common import (
check_backwards_compatibility,
check_config,
check_encode_decode,
check_repr,
)
arrays = [
np.linspace(1000, 1001, 1000, dtype='<f8'),
np.random.normal(loc=1000, scale=1, size=1000).astype('<f8'),
np.linspace(1000, 1001, 1000, dtype='<f8').reshape(100, 10),
np.linspace(1000, 1001, 1000, dtype='<f8').reshape(100, 10, order='F'),
np.linspace(1000, 1001, 1000, dtype='<f8').reshape(10, 10, 10),
]
codecs = [
FixedScaleOffset(offset=1000, scale=10, dtype='<f8', astype='<i1'),
FixedScaleOffset(offset=1000, scale=10**2, dtype='<f8', astype='<i2'),
FixedScaleOffset(offset=1000, scale=10**6, dtype='<f8', astype='<i4'),
FixedScaleOffset(offset=1000, scale=10**12, dtype='<f8', astype='<i8'),
FixedScaleOffset(offset=1000, scale=10**12, dtype='<f8'),
]
def test_encode_decode():
for arr, codec in itertools.product(arrays, codecs):
precision = int(np.log10(codec.scale))
check_encode_decode(arr, codec, precision=precision)
@pytest.mark.parametrize(
("offset", "scale", "expected"),
[
(1000, 10, [0, 6, 11, 17, 22, 28, 33, 39, 44, 50]),
(1002.5, 10, [-25, -19, -14, -8, -3, 3, 8, 14, 19, 25]),
(1000, 0.5, [0, 0, 1, 1, 1, 1, 2, 2, 2, 2]),
],
)
def test_encode(offset: float, scale: float, expected: list[int]):
dtype = '<f8'
astype = np.int16
codec = FixedScaleOffset(scale=scale, offset=offset, dtype=dtype, astype=astype)
arr = np.linspace(1000, 1005, 10, dtype=dtype)
expect = np.array(expected, dtype=astype)
actual = codec.encode(arr)
assert_array_equal(expect, actual)
assert np.dtype(astype) == actual.dtype
def test_config():
codec = FixedScaleOffset(dtype='<f8', astype='<i4', scale=10, offset=100)
check_config(codec)
def test_repr():
stmt = "FixedScaleOffset(scale=10, offset=100, dtype='<f8', astype='<i4')"
check_repr(stmt)
def test_backwards_compatibility():
precision = [int(np.log10(codec.scale)) for codec in codecs]
check_backwards_compatibility(FixedScaleOffset.codec_id, arrays, codecs, precision=precision)
def test_errors():
with pytest.raises(ValueError):
FixedScaleOffset(dtype=object, astype='i4', scale=10, offset=100)
with pytest.raises(ValueError):
FixedScaleOffset(dtype='f8', astype=object, scale=10, offset=100)