numcodecs 0.16.4__cp313-cp313-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- numcodecs/__init__.py +146 -0
- numcodecs/_shuffle.cpython-313-darwin.so +0 -0
- numcodecs/abc.py +126 -0
- numcodecs/astype.py +72 -0
- numcodecs/base64.py +26 -0
- numcodecs/bitround.py +80 -0
- numcodecs/blosc.cpython-313-darwin.so +0 -0
- numcodecs/bz2.py +45 -0
- numcodecs/categorize.py +98 -0
- numcodecs/checksum32.py +189 -0
- numcodecs/compat.py +206 -0
- numcodecs/compat_ext.cpython-313-darwin.so +0 -0
- numcodecs/delta.py +94 -0
- numcodecs/errors.py +26 -0
- numcodecs/fixedscaleoffset.py +130 -0
- numcodecs/fletcher32.cpython-313-darwin.so +0 -0
- numcodecs/gzip.py +50 -0
- numcodecs/jenkins.cpython-313-darwin.so +0 -0
- numcodecs/json.py +107 -0
- numcodecs/lz4.cpython-313-darwin.so +0 -0
- numcodecs/lzma.py +71 -0
- numcodecs/msgpacks.py +86 -0
- numcodecs/ndarray_like.py +65 -0
- numcodecs/packbits.py +82 -0
- numcodecs/pcodec.py +119 -0
- numcodecs/pickles.py +55 -0
- numcodecs/quantize.py +98 -0
- numcodecs/registry.py +74 -0
- numcodecs/shuffle.py +61 -0
- numcodecs/tests/__init__.py +3 -0
- numcodecs/tests/common.py +275 -0
- numcodecs/tests/package_with_entrypoint/__init__.py +11 -0
- numcodecs/tests/package_with_entrypoint-0.1.dist-info/entry_points.txt +2 -0
- numcodecs/tests/test_astype.py +74 -0
- numcodecs/tests/test_base64.py +81 -0
- numcodecs/tests/test_bitround.py +81 -0
- numcodecs/tests/test_blosc.py +290 -0
- numcodecs/tests/test_bz2.py +66 -0
- numcodecs/tests/test_categorize.py +87 -0
- numcodecs/tests/test_checksum32.py +199 -0
- numcodecs/tests/test_compat.py +111 -0
- numcodecs/tests/test_delta.py +61 -0
- numcodecs/tests/test_entrypoints.py +24 -0
- numcodecs/tests/test_entrypoints_backport.py +36 -0
- numcodecs/tests/test_fixedscaleoffset.py +77 -0
- numcodecs/tests/test_fletcher32.py +56 -0
- numcodecs/tests/test_gzip.py +110 -0
- numcodecs/tests/test_jenkins.py +150 -0
- numcodecs/tests/test_json.py +85 -0
- numcodecs/tests/test_lz4.py +83 -0
- numcodecs/tests/test_lzma.py +94 -0
- numcodecs/tests/test_msgpacks.py +126 -0
- numcodecs/tests/test_ndarray_like.py +48 -0
- numcodecs/tests/test_packbits.py +39 -0
- numcodecs/tests/test_pcodec.py +90 -0
- numcodecs/tests/test_pickles.py +61 -0
- numcodecs/tests/test_pyzstd.py +76 -0
- numcodecs/tests/test_quantize.py +76 -0
- numcodecs/tests/test_registry.py +43 -0
- numcodecs/tests/test_shuffle.py +166 -0
- numcodecs/tests/test_vlen_array.py +97 -0
- numcodecs/tests/test_vlen_bytes.py +93 -0
- numcodecs/tests/test_vlen_utf8.py +91 -0
- numcodecs/tests/test_zarr3.py +48 -0
- numcodecs/tests/test_zarr3_import.py +13 -0
- numcodecs/tests/test_zfpy.py +104 -0
- numcodecs/tests/test_zlib.py +94 -0
- numcodecs/tests/test_zstd.py +189 -0
- numcodecs/version.py +34 -0
- numcodecs/vlen.cpython-313-darwin.so +0 -0
- numcodecs/zarr3.py +67 -0
- numcodecs/zfpy.py +112 -0
- numcodecs/zlib.py +42 -0
- numcodecs/zstd.cpython-313-darwin.so +0 -0
- numcodecs-0.16.4.dist-info/METADATA +67 -0
- numcodecs-0.16.4.dist-info/RECORD +87 -0
- numcodecs-0.16.4.dist-info/WHEEL +6 -0
- numcodecs-0.16.4.dist-info/licenses/LICENSE.txt +21 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSE.txt +31 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/BITSHUFFLE.txt +21 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/FASTLZ.txt +20 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/LZ4.txt +25 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/SNAPPY.txt +28 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/STDINT.txt +29 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/ZLIB-NG.txt +17 -0
- numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/ZLIB.txt +22 -0
- numcodecs-0.16.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
from types import ModuleType
|
|
2
|
+
from typing import cast
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
# noinspection PyProtectedMember
|
|
9
|
+
from numcodecs.zfpy import ZFPY, _zfpy
|
|
10
|
+
except ImportError: # pragma: no cover
|
|
11
|
+
pytest.skip("ZFPY not available", allow_module_level=True)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from numcodecs.tests.common import (
|
|
15
|
+
check_backwards_compatibility,
|
|
16
|
+
check_config,
|
|
17
|
+
check_encode_decode_array,
|
|
18
|
+
check_err_decode_object_buffer,
|
|
19
|
+
check_err_encode_object_buffer,
|
|
20
|
+
check_repr,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
_zfpy = cast(ModuleType, _zfpy)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
codecs = [
|
|
27
|
+
ZFPY(mode=_zfpy.mode_fixed_rate, rate=-1),
|
|
28
|
+
ZFPY(),
|
|
29
|
+
ZFPY(mode=_zfpy.mode_fixed_accuracy, tolerance=-1),
|
|
30
|
+
ZFPY(mode=_zfpy.mode_fixed_precision, precision=-1),
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# mix of dtypes: integer, float, bool, string
|
|
35
|
+
# mix of shapes: 1D, 2D, 3D
|
|
36
|
+
# mix of orders: C, F
|
|
37
|
+
arrays = [
|
|
38
|
+
np.linspace(1000, 1001, 1000, dtype="f4"),
|
|
39
|
+
np.linspace(1000, 1001, 1000, dtype="f8"),
|
|
40
|
+
np.random.normal(loc=1000, scale=1, size=(100, 10)),
|
|
41
|
+
np.random.normal(loc=1000, scale=1, size=(10, 10, 10)),
|
|
42
|
+
np.random.normal(loc=1000, scale=1, size=(2, 5, 10, 10)),
|
|
43
|
+
np.random.randint(-(2**31), -(2**31) + 20, size=1000, dtype="i4").reshape(100, 10),
|
|
44
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype="i8").reshape(10, 10, 10),
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_encode_decode():
|
|
49
|
+
for arr in arrays:
|
|
50
|
+
if arr.dtype in (np.int32, np.int64):
|
|
51
|
+
codec = [codecs[-1]]
|
|
52
|
+
else:
|
|
53
|
+
codec = codecs
|
|
54
|
+
for code in codec:
|
|
55
|
+
check_encode_decode_array(arr, code)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_config():
|
|
59
|
+
for codec in codecs:
|
|
60
|
+
check_config(codec)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_repr():
|
|
64
|
+
check_repr("ZFPY(mode=4, tolerance=0.001, rate=-1, precision=-1)")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_backwards_compatibility():
|
|
68
|
+
for code in codecs:
|
|
69
|
+
if code.mode == _zfpy.mode_fixed_rate:
|
|
70
|
+
codec = [code]
|
|
71
|
+
check_backwards_compatibility(ZFPY.codec_id, arrays, codec)
|
|
72
|
+
else:
|
|
73
|
+
check_backwards_compatibility(ZFPY.codec_id, arrays[: len(arrays) - 2], codecs)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_err_decode_object_buffer():
|
|
77
|
+
check_err_decode_object_buffer(ZFPY())
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_err_encode_object_buffer():
|
|
81
|
+
check_err_encode_object_buffer(ZFPY())
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_err_encode_list():
|
|
85
|
+
data = ['foo', 'bar', 'baz']
|
|
86
|
+
for codec in codecs:
|
|
87
|
+
with pytest.raises(TypeError):
|
|
88
|
+
codec.encode(data)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_err_encode_non_contiguous():
|
|
92
|
+
# non-contiguous memory
|
|
93
|
+
arr = np.arange(1000, dtype='i4')[::2]
|
|
94
|
+
for codec in codecs:
|
|
95
|
+
with pytest.raises(ValueError):
|
|
96
|
+
codec.encode(arr)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_err_encode_fortran_array():
|
|
100
|
+
# fortran array
|
|
101
|
+
arr = np.asfortranarray(np.random.normal(loc=1000, scale=1, size=(5, 10, 20)))
|
|
102
|
+
for codec in codecs:
|
|
103
|
+
with pytest.raises(ValueError):
|
|
104
|
+
codec.encode(arr)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from numcodecs.tests.common import (
|
|
7
|
+
check_backwards_compatibility,
|
|
8
|
+
check_config,
|
|
9
|
+
check_encode_decode,
|
|
10
|
+
check_err_decode_object_buffer,
|
|
11
|
+
check_err_encode_object_buffer,
|
|
12
|
+
check_repr,
|
|
13
|
+
)
|
|
14
|
+
from numcodecs.zlib import Zlib
|
|
15
|
+
|
|
16
|
+
codecs = [
|
|
17
|
+
Zlib(),
|
|
18
|
+
Zlib(level=-1),
|
|
19
|
+
Zlib(level=0),
|
|
20
|
+
Zlib(level=1),
|
|
21
|
+
Zlib(level=5),
|
|
22
|
+
Zlib(level=9),
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# mix of dtypes: integer, float, bool, string
|
|
27
|
+
# mix of shapes: 1D, 2D, 3D
|
|
28
|
+
# mix of orders: C, F
|
|
29
|
+
arrays = [
|
|
30
|
+
np.arange(1000, dtype='i4'),
|
|
31
|
+
np.linspace(1000, 1001, 1000, dtype='f8'),
|
|
32
|
+
np.random.normal(loc=1000, scale=1, size=(100, 10)),
|
|
33
|
+
np.random.randint(0, 2, size=1000, dtype=bool).reshape(100, 10, order='F'),
|
|
34
|
+
np.random.choice([b'a', b'bb', b'ccc'], size=1000).reshape(10, 10, 10),
|
|
35
|
+
np.random.randint(0, 2**60, size=1000, dtype='u8').view('M8[ns]'),
|
|
36
|
+
np.random.randint(0, 2**60, size=1000, dtype='u8').view('m8[ns]'),
|
|
37
|
+
np.random.randint(0, 2**25, size=1000, dtype='u8').view('M8[m]'),
|
|
38
|
+
np.random.randint(0, 2**25, size=1000, dtype='u8').view('m8[m]'),
|
|
39
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[ns]'),
|
|
40
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[ns]'),
|
|
41
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[m]'),
|
|
42
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[m]'),
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_encode_decode():
|
|
47
|
+
for arr, codec in itertools.product(arrays, codecs):
|
|
48
|
+
check_encode_decode(arr, codec)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_config():
|
|
52
|
+
codec = Zlib(level=3)
|
|
53
|
+
check_config(codec)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_repr():
|
|
57
|
+
check_repr("Zlib(level=3)")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_eq():
|
|
61
|
+
assert Zlib() == Zlib()
|
|
62
|
+
assert not Zlib() != Zlib()
|
|
63
|
+
assert Zlib(1) == Zlib(1)
|
|
64
|
+
assert Zlib(1) != Zlib(9)
|
|
65
|
+
assert Zlib() != 'foo'
|
|
66
|
+
assert 'foo' != Zlib()
|
|
67
|
+
assert not Zlib() == 'foo'
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_backwards_compatibility():
|
|
71
|
+
check_backwards_compatibility(Zlib.codec_id, arrays, codecs)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_err_decode_object_buffer():
|
|
75
|
+
check_err_decode_object_buffer(Zlib())
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_err_encode_object_buffer():
|
|
79
|
+
check_err_encode_object_buffer(Zlib())
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_err_encode_list():
|
|
83
|
+
data = ['foo', 'bar', 'baz']
|
|
84
|
+
for codec in codecs:
|
|
85
|
+
with pytest.raises(TypeError):
|
|
86
|
+
codec.encode(data)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def test_err_encode_non_contiguous():
|
|
90
|
+
# non-contiguous memory
|
|
91
|
+
arr = np.arange(1000, dtype='i4')[::2]
|
|
92
|
+
for codec in codecs:
|
|
93
|
+
with pytest.raises(ValueError):
|
|
94
|
+
codec.encode(arr)
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
import subprocess
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from numcodecs.tests.common import (
|
|
8
|
+
check_backwards_compatibility,
|
|
9
|
+
check_config,
|
|
10
|
+
check_encode_decode,
|
|
11
|
+
check_err_decode_object_buffer,
|
|
12
|
+
check_err_encode_object_buffer,
|
|
13
|
+
check_repr,
|
|
14
|
+
)
|
|
15
|
+
from numcodecs.zstd import Zstd
|
|
16
|
+
|
|
17
|
+
codecs = [
|
|
18
|
+
Zstd(),
|
|
19
|
+
Zstd(level=-1),
|
|
20
|
+
Zstd(level=0),
|
|
21
|
+
Zstd(level=1),
|
|
22
|
+
Zstd(level=10),
|
|
23
|
+
Zstd(level=22),
|
|
24
|
+
Zstd(level=100),
|
|
25
|
+
Zstd(checksum=True),
|
|
26
|
+
Zstd(level=0, checksum=True),
|
|
27
|
+
Zstd(level=22, checksum=True),
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# mix of dtypes: integer, float, bool, string
|
|
32
|
+
# mix of shapes: 1D, 2D, 3D
|
|
33
|
+
# mix of orders: C, F
|
|
34
|
+
arrays = [
|
|
35
|
+
np.arange(1000, dtype="i4"),
|
|
36
|
+
np.linspace(1000, 1001, 1000, dtype="f8"),
|
|
37
|
+
np.random.normal(loc=1000, scale=1, size=(100, 10)),
|
|
38
|
+
np.random.randint(0, 2, size=1000, dtype=bool).reshape(100, 10, order='F'),
|
|
39
|
+
np.random.choice([b'a', b'bb', b'ccc'], size=1000).reshape(10, 10, 10),
|
|
40
|
+
np.random.randint(0, 2**60, size=1000, dtype='u8').view('M8[ns]'),
|
|
41
|
+
np.random.randint(0, 2**60, size=1000, dtype='u8').view('m8[ns]'),
|
|
42
|
+
np.random.randint(0, 2**25, size=1000, dtype='u8').view('M8[m]'),
|
|
43
|
+
np.random.randint(0, 2**25, size=1000, dtype='u8').view('m8[m]'),
|
|
44
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[ns]'),
|
|
45
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[ns]'),
|
|
46
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[m]'),
|
|
47
|
+
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[m]'),
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_encode_decode():
|
|
52
|
+
for arr, codec in itertools.product(arrays, codecs):
|
|
53
|
+
check_encode_decode(arr, codec)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_config():
|
|
57
|
+
for codec in codecs:
|
|
58
|
+
check_config(codec)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_repr():
|
|
62
|
+
check_repr("Zstd(level=3)")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_backwards_compatibility():
|
|
66
|
+
check_backwards_compatibility(Zstd.codec_id, arrays, codecs)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_err_decode_object_buffer():
|
|
70
|
+
check_err_decode_object_buffer(Zstd())
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_err_encode_object_buffer():
|
|
74
|
+
check_err_encode_object_buffer(Zstd())
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_checksum():
|
|
78
|
+
data = np.arange(0, 64, dtype="uint8")
|
|
79
|
+
assert len(Zstd(level=0, checksum=False).encode(data)) + 4 == len(
|
|
80
|
+
Zstd(level=0, checksum=True).encode(data)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_native_functions():
|
|
85
|
+
# Note, these assertions might need to be changed for new versions of zstd
|
|
86
|
+
assert Zstd.default_level() == 3
|
|
87
|
+
assert Zstd.min_level() == -131072
|
|
88
|
+
assert Zstd.max_level() == 22
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_streaming_decompression():
|
|
92
|
+
# Test input frames with unknown frame content size
|
|
93
|
+
codec = Zstd()
|
|
94
|
+
|
|
95
|
+
# If the zstd command line interface is available, check the bytes
|
|
96
|
+
cli = zstd_cli_available()
|
|
97
|
+
if cli:
|
|
98
|
+
view_zstd_streaming_bytes()
|
|
99
|
+
|
|
100
|
+
# Encode bytes directly that were the result of streaming compression
|
|
101
|
+
bytes_val = b'(\xb5/\xfd\x00Xa\x00\x00Hello World!'
|
|
102
|
+
dec = codec.decode(bytes_val)
|
|
103
|
+
dec_expected = b'Hello World!'
|
|
104
|
+
assert dec == dec_expected
|
|
105
|
+
if cli:
|
|
106
|
+
assert bytes_val == generate_zstd_streaming_bytes(dec_expected)
|
|
107
|
+
assert dec_expected == generate_zstd_streaming_bytes(bytes_val, decompress=True)
|
|
108
|
+
|
|
109
|
+
# Two consecutive frames given as input
|
|
110
|
+
bytes2 = bytes(bytearray(bytes_val * 2))
|
|
111
|
+
dec2 = codec.decode(bytes2)
|
|
112
|
+
dec2_expected = b'Hello World!Hello World!'
|
|
113
|
+
assert dec2 == dec2_expected
|
|
114
|
+
if cli:
|
|
115
|
+
assert dec2_expected == generate_zstd_streaming_bytes(bytes2, decompress=True)
|
|
116
|
+
|
|
117
|
+
# Single long frame that decompresses to a large output
|
|
118
|
+
bytes3 = b'(\xb5/\xfd\x00X$\x02\x00\xa4\x03ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz\x01\x00:\xfc\xdfs\x05\x05L\x00\x00\x08s\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08k\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08c\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08[\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08S\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08K\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08C\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08u\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08m\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08e\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08]\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08U\x01\x00\xfc\xff9\x10\x02L\x00\x00\x08M\x01\x00\xfc\xff9\x10\x02M\x00\x00\x08E\x01\x00\xfc\x7f\x1d\x08\x01'
|
|
119
|
+
dec3 = codec.decode(bytes3)
|
|
120
|
+
dec3_expected = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz' * 1024 * 32
|
|
121
|
+
assert dec3 == dec3_expected
|
|
122
|
+
if cli:
|
|
123
|
+
assert bytes3 == generate_zstd_streaming_bytes(dec3_expected)
|
|
124
|
+
assert dec3_expected == generate_zstd_streaming_bytes(bytes3, decompress=True)
|
|
125
|
+
|
|
126
|
+
# Garbage input results in an error
|
|
127
|
+
bytes4 = bytes(bytearray([0, 0, 0, 0, 0, 0, 0, 0]))
|
|
128
|
+
with pytest.raises(RuntimeError, match='Zstd decompression error: invalid input data'):
|
|
129
|
+
codec.decode(bytes4)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def generate_zstd_streaming_bytes(input: bytes, *, decompress: bool = False) -> bytes:
|
|
133
|
+
"""
|
|
134
|
+
Use the zstd command line interface to compress or decompress bytes in streaming mode.
|
|
135
|
+
"""
|
|
136
|
+
if decompress:
|
|
137
|
+
args = ["-d"]
|
|
138
|
+
else:
|
|
139
|
+
args = []
|
|
140
|
+
|
|
141
|
+
p = subprocess.run(["zstd", "--no-check", *args], input=input, capture_output=True)
|
|
142
|
+
return p.stdout
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def view_zstd_streaming_bytes():
|
|
146
|
+
bytes_val = generate_zstd_streaming_bytes(b"Hello world!")
|
|
147
|
+
print(f" bytes_val = {bytes_val}")
|
|
148
|
+
|
|
149
|
+
bytes3 = generate_zstd_streaming_bytes(
|
|
150
|
+
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz" * 1024 * 32
|
|
151
|
+
)
|
|
152
|
+
print(f" bytes3 = {bytes3}")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def zstd_cli_available() -> bool:
|
|
156
|
+
return not subprocess.run(
|
|
157
|
+
["zstd", "-V"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
|
158
|
+
).returncode
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def test_multi_frame():
|
|
162
|
+
codec = Zstd()
|
|
163
|
+
|
|
164
|
+
hello_world = codec.encode(b"Hello world!")
|
|
165
|
+
assert codec.decode(hello_world) == b"Hello world!"
|
|
166
|
+
assert codec.decode(hello_world * 2) == b"Hello world!Hello world!"
|
|
167
|
+
|
|
168
|
+
hola = codec.encode(b"Hola ")
|
|
169
|
+
mundo = codec.encode(b"Mundo!")
|
|
170
|
+
assert codec.decode(hola) == b"Hola "
|
|
171
|
+
assert codec.decode(mundo) == b"Mundo!"
|
|
172
|
+
assert codec.decode(hola + mundo) == b"Hola Mundo!"
|
|
173
|
+
|
|
174
|
+
bytes_val = b'(\xb5/\xfd\x00Xa\x00\x00Hello World!'
|
|
175
|
+
dec = codec.decode(bytes_val)
|
|
176
|
+
dec_expected = b'Hello World!'
|
|
177
|
+
assert dec == dec_expected
|
|
178
|
+
cli = zstd_cli_available()
|
|
179
|
+
if cli:
|
|
180
|
+
assert bytes_val == generate_zstd_streaming_bytes(dec_expected)
|
|
181
|
+
assert dec_expected == generate_zstd_streaming_bytes(bytes_val, decompress=True)
|
|
182
|
+
|
|
183
|
+
# Concatenate frames of known sizes and unknown sizes
|
|
184
|
+
# unknown size frame at the end
|
|
185
|
+
assert codec.decode(hola + mundo + bytes_val) == b"Hola Mundo!Hello World!"
|
|
186
|
+
# unknown size frame at the beginning
|
|
187
|
+
assert codec.decode(bytes_val + hola + mundo) == b"Hello World!Hola Mundo!"
|
|
188
|
+
# unknown size frame in the middle
|
|
189
|
+
assert codec.decode(hola + bytes_val + mundo) == b"Hola Hello World!Mundo!"
|
numcodecs/version.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# file generated by setuptools-scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
TYPE_CHECKING = False
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
from typing import Union
|
|
17
|
+
|
|
18
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
20
|
+
else:
|
|
21
|
+
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
23
|
+
|
|
24
|
+
version: str
|
|
25
|
+
__version__: str
|
|
26
|
+
__version_tuple__: VERSION_TUPLE
|
|
27
|
+
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
30
|
+
|
|
31
|
+
__version__ = version = '0.16.4'
|
|
32
|
+
__version_tuple__ = version_tuple = (0, 16, 4)
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = 'g91dfe4296'
|
|
Binary file
|
numcodecs/zarr3.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module is DEPRECATED. It will may be removed entirely in a future release of Numcodecs.
|
|
3
|
+
The codecs exported here are available in Zarr Python >= 3.1.3
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import importlib
|
|
9
|
+
import warnings
|
|
10
|
+
from importlib.metadata import version
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from packaging.version import Version
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def __getattr__(name: str) -> Any:
|
|
17
|
+
"""
|
|
18
|
+
Emit a warning when someone imports from this module
|
|
19
|
+
"""
|
|
20
|
+
if name in __all__:
|
|
21
|
+
msg = (
|
|
22
|
+
"The numcodecs.zarr3 module is deprecated and will be removed in a future release of numcodecs. "
|
|
23
|
+
f"Import {name} via zarr.codecs.numcodecs.{name} instead. This requires Zarr Python >= 3.1.3. "
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
|
|
27
|
+
module = importlib.import_module("zarr.codecs.numcodecs")
|
|
28
|
+
obj = getattr(module, name)
|
|
29
|
+
globals()[name] = obj # cache so subsequent lookups skip __getattr__
|
|
30
|
+
return obj
|
|
31
|
+
raise AttributeError(f"module {__name__} has no attribute {name}")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
try:
|
|
35
|
+
import zarr # noqa: F401
|
|
36
|
+
|
|
37
|
+
zarr_version = version('zarr')
|
|
38
|
+
if Version(zarr_version) < Version("3.1.3"): # pragma: no cover
|
|
39
|
+
msg = f"Zarr 3.1.3 or later is required to use the numcodecs zarr integration. Got {zarr_version}."
|
|
40
|
+
raise ImportError(msg)
|
|
41
|
+
except ImportError as e: # pragma: no cover
|
|
42
|
+
msg = "zarr could not be imported. Zarr 3.1.3 or later is required to use the numcodecs zarr integration."
|
|
43
|
+
raise ImportError(msg) from e
|
|
44
|
+
|
|
45
|
+
__all__ = [
|
|
46
|
+
"BZ2", # noqa: F822
|
|
47
|
+
"CRC32", # noqa: F822
|
|
48
|
+
"CRC32C", # noqa: F822
|
|
49
|
+
"LZ4", # noqa: F822
|
|
50
|
+
"LZMA", # noqa: F822
|
|
51
|
+
"ZFPY", # noqa: F822
|
|
52
|
+
"Adler32", # noqa: F822
|
|
53
|
+
"AsType", # noqa: F822
|
|
54
|
+
"BitRound", # noqa: F822
|
|
55
|
+
"Blosc", # noqa: F822
|
|
56
|
+
"Delta", # noqa: F822
|
|
57
|
+
"FixedScaleOffset", # noqa: F822
|
|
58
|
+
"Fletcher32", # noqa: F822
|
|
59
|
+
"GZip", # noqa: F822
|
|
60
|
+
"JenkinsLookup3", # noqa: F822
|
|
61
|
+
"PCodec", # noqa: F822
|
|
62
|
+
"PackBits", # noqa: F822
|
|
63
|
+
"Quantize", # noqa: F822
|
|
64
|
+
"Shuffle", # noqa: F822
|
|
65
|
+
"Zlib", # noqa: F822
|
|
66
|
+
"Zstd", # noqa: F822
|
|
67
|
+
]
|
numcodecs/zfpy.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
from contextlib import suppress
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
from types import ModuleType
|
|
5
|
+
|
|
6
|
+
_zfpy: ModuleType | None = None
|
|
7
|
+
|
|
8
|
+
_zfpy_version: tuple = ()
|
|
9
|
+
with suppress(PackageNotFoundError):
|
|
10
|
+
_zfpy_version = tuple(map(int, version("zfpy").split(".")))
|
|
11
|
+
|
|
12
|
+
if _zfpy_version:
|
|
13
|
+
# Check NumPy version
|
|
14
|
+
_numpy_version: tuple = tuple(map(int, version("numpy").split('.')))
|
|
15
|
+
if _numpy_version >= (2, 0, 0) and _zfpy_version < (1, 0, 1): # pragma: no cover
|
|
16
|
+
_zfpy_version = ()
|
|
17
|
+
warnings.warn(
|
|
18
|
+
"NumPy version >= 2.0.0 detected. The zfpy library is incompatible with this version of NumPy. "
|
|
19
|
+
"Please downgrade to NumPy < 2.0.0 or wait for an update from zfpy.",
|
|
20
|
+
UserWarning,
|
|
21
|
+
stacklevel=2,
|
|
22
|
+
)
|
|
23
|
+
else:
|
|
24
|
+
with suppress(ImportError):
|
|
25
|
+
import zfpy as _zfpy # type: ignore[no-redef]
|
|
26
|
+
|
|
27
|
+
if _zfpy:
|
|
28
|
+
import numpy as np
|
|
29
|
+
|
|
30
|
+
from .abc import Codec
|
|
31
|
+
from .compat import ensure_bytes, ensure_contiguous_ndarray, ndarray_copy
|
|
32
|
+
|
|
33
|
+
# noinspection PyShadowingBuiltins
|
|
34
|
+
class ZFPY(Codec):
|
|
35
|
+
"""Codec providing compression using zfpy via the Python standard
|
|
36
|
+
library.
|
|
37
|
+
|
|
38
|
+
Parameters
|
|
39
|
+
----------
|
|
40
|
+
mode : integer
|
|
41
|
+
One of the zfpy mode choice, e.g., ``zfpy.mode_fixed_accuracy``.
|
|
42
|
+
tolerance : double, optional
|
|
43
|
+
A double-precision number, specifying the compression accuracy needed.
|
|
44
|
+
rate : double, optional
|
|
45
|
+
A double-precision number, specifying the compression rate needed.
|
|
46
|
+
precision : int, optional
|
|
47
|
+
A integer number, specifying the compression precision needed.
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
codec_id = "zfpy"
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
mode=_zfpy.mode_fixed_accuracy,
|
|
56
|
+
tolerance=-1,
|
|
57
|
+
rate=-1,
|
|
58
|
+
precision=-1,
|
|
59
|
+
compression_kwargs=None,
|
|
60
|
+
):
|
|
61
|
+
self.mode = mode
|
|
62
|
+
if mode == _zfpy.mode_fixed_accuracy:
|
|
63
|
+
self.compression_kwargs = {"tolerance": tolerance}
|
|
64
|
+
elif mode == _zfpy.mode_fixed_rate:
|
|
65
|
+
self.compression_kwargs = {"rate": rate}
|
|
66
|
+
elif mode == _zfpy.mode_fixed_precision:
|
|
67
|
+
self.compression_kwargs = {"precision": precision}
|
|
68
|
+
|
|
69
|
+
self.tolerance = tolerance
|
|
70
|
+
self.rate = rate
|
|
71
|
+
self.precision = precision
|
|
72
|
+
|
|
73
|
+
def encode(self, buf):
|
|
74
|
+
# not flatten c-order array and raise exception for f-order array
|
|
75
|
+
if not isinstance(buf, np.ndarray):
|
|
76
|
+
raise TypeError(
|
|
77
|
+
"The zfp codec does not support none numpy arrays."
|
|
78
|
+
f" Your buffers were {type(buf)}."
|
|
79
|
+
)
|
|
80
|
+
if buf.flags.c_contiguous:
|
|
81
|
+
flatten = False
|
|
82
|
+
else:
|
|
83
|
+
raise ValueError(
|
|
84
|
+
"The zfp codec does not support F order arrays. "
|
|
85
|
+
f"Your arrays flags were {buf.flags}."
|
|
86
|
+
)
|
|
87
|
+
buf = ensure_contiguous_ndarray(buf, flatten=flatten)
|
|
88
|
+
|
|
89
|
+
# do compression
|
|
90
|
+
return _zfpy.compress_numpy(buf, write_header=True, **self.compression_kwargs)
|
|
91
|
+
|
|
92
|
+
def decode(self, buf, out=None):
|
|
93
|
+
# normalise inputs
|
|
94
|
+
buf = ensure_bytes(buf)
|
|
95
|
+
if out is not None:
|
|
96
|
+
out = ensure_contiguous_ndarray(out)
|
|
97
|
+
|
|
98
|
+
# do decompression
|
|
99
|
+
dec = _zfpy.decompress_numpy(buf)
|
|
100
|
+
|
|
101
|
+
# handle destination
|
|
102
|
+
if out is not None:
|
|
103
|
+
return ndarray_copy(dec, out)
|
|
104
|
+
else:
|
|
105
|
+
return dec
|
|
106
|
+
|
|
107
|
+
def __repr__(self):
|
|
108
|
+
return (
|
|
109
|
+
f"{type(self).__name__}(mode={self.mode!r}, "
|
|
110
|
+
f"tolerance={self.tolerance}, rate={self.rate}, "
|
|
111
|
+
f"precision={self.precision})"
|
|
112
|
+
)
|
numcodecs/zlib.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import zlib as _zlib
|
|
2
|
+
|
|
3
|
+
from .abc import Codec
|
|
4
|
+
from .compat import ensure_contiguous_ndarray, ndarray_copy
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Zlib(Codec):
|
|
8
|
+
"""Codec providing compression using zlib via the Python standard library.
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
level : int
|
|
13
|
+
Compression level.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
codec_id = 'zlib'
|
|
18
|
+
|
|
19
|
+
def __init__(self, level=1):
|
|
20
|
+
self.level = level
|
|
21
|
+
|
|
22
|
+
def encode(self, buf):
|
|
23
|
+
# normalise inputs
|
|
24
|
+
buf = ensure_contiguous_ndarray(buf)
|
|
25
|
+
|
|
26
|
+
# do compression
|
|
27
|
+
return _zlib.compress(buf, self.level)
|
|
28
|
+
|
|
29
|
+
# noinspection PyMethodMayBeStatic
|
|
30
|
+
def decode(self, buf, out=None):
|
|
31
|
+
# normalise inputs
|
|
32
|
+
buf = ensure_contiguous_ndarray(buf)
|
|
33
|
+
if out is not None:
|
|
34
|
+
out = ensure_contiguous_ndarray(out)
|
|
35
|
+
|
|
36
|
+
# do decompression
|
|
37
|
+
dec = _zlib.decompress(buf)
|
|
38
|
+
|
|
39
|
+
# handle destination - Python standard library zlib module does not
|
|
40
|
+
# support direct decompression into buffer, so we have to copy into
|
|
41
|
+
# out if given
|
|
42
|
+
return ndarray_copy(dec, out)
|
|
Binary file
|