|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +import numcodecs.zarr3 |
| 7 | + |
| 8 | +zarr = pytest.importorskip("zarr") |
| 9 | + |
| 10 | +pytestmark = [ |
| 11 | + pytest.mark.skipif(zarr.__version__ < "3.0.0", reason="zarr 3.0.0 or later is required"), |
| 12 | + pytest.mark.filterwarnings("ignore:Codec 'numcodecs.*' not configured in config.*:UserWarning"), |
| 13 | + pytest.mark.filterwarnings( |
| 14 | + "ignore:Numcodecs codecs are not in the Zarr version 3 specification and may not be supported by other zarr implementations." |
| 15 | + ), |
| 16 | +] |
| 17 | + |
| 18 | +get_codec_class = zarr.registry.get_codec_class |
| 19 | +Array = zarr.Array |
| 20 | +JSON = zarr.core.common.JSON |
| 21 | +BytesCodec = zarr.codecs.BytesCodec |
| 22 | +Store = zarr.abc.store.Store |
| 23 | +MemoryStore = zarr.storage.MemoryStore |
| 24 | +StorePath = zarr.storage.StorePath |
| 25 | + |
| 26 | + |
| 27 | +EXPECTED_WARNING_STR = "Numcodecs codecs are not in the Zarr version 3.*" |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def store() -> Store: |
| 32 | + return StorePath(MemoryStore(mode="w")) |
| 33 | + |
| 34 | + |
| 35 | +ALL_CODECS = [getattr(numcodecs.zarr3, cls_name) for cls_name in numcodecs.zarr3.__all__] |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("codec_class", ALL_CODECS) |
| 39 | +def test_entry_points(codec_class: type[numcodecs.zarr3._NumcodecsCodec]): |
| 40 | + codec_name = codec_class.codec_name |
| 41 | + assert get_codec_class(codec_name) == codec_class |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("codec_class", ALL_CODECS) |
| 45 | +def test_docstring(codec_class: type[numcodecs.zarr3._NumcodecsCodec]): |
| 46 | + assert "See :class:`numcodecs." in codec_class.__doc__ |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + "codec_class", |
| 51 | + [ |
| 52 | + numcodecs.zarr3.Blosc, |
| 53 | + numcodecs.zarr3.LZ4, |
| 54 | + numcodecs.zarr3.Zstd, |
| 55 | + numcodecs.zarr3.Zlib, |
| 56 | + numcodecs.zarr3.GZip, |
| 57 | + numcodecs.zarr3.BZ2, |
| 58 | + numcodecs.zarr3.LZMA, |
| 59 | + numcodecs.zarr3.Shuffle, |
| 60 | + ], |
| 61 | +) |
| 62 | +def test_generic_codec_class(store: Store, codec_class: type[numcodecs.zarr3._NumcodecsCodec]): |
| 63 | + data = np.arange(0, 256, dtype="uint16").reshape((16, 16)) |
| 64 | + |
| 65 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 66 | + a = Array.create( |
| 67 | + store / "generic", |
| 68 | + shape=data.shape, |
| 69 | + chunk_shape=(16, 16), |
| 70 | + dtype=data.dtype, |
| 71 | + fill_value=0, |
| 72 | + codecs=[BytesCodec(), codec_class()], |
| 73 | + ) |
| 74 | + |
| 75 | + a[:, :] = data.copy() |
| 76 | + np.testing.assert_array_equal(data, a[:, :]) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + ("codec_class", "codec_config"), |
| 81 | + [ |
| 82 | + (numcodecs.zarr3.Delta, {"dtype": "float32"}), |
| 83 | + (numcodecs.zarr3.FixedScaleOffset, {"offset": 0, "scale": 25.5}), |
| 84 | + (numcodecs.zarr3.FixedScaleOffset, {"offset": 0, "scale": 51, "astype": "uint16"}), |
| 85 | + (numcodecs.zarr3.AsType, {"encode_dtype": "float32", "decode_dtype": "float64"}), |
| 86 | + ], |
| 87 | + ids=[ |
| 88 | + "delta", |
| 89 | + "fixedscaleoffset", |
| 90 | + "fixedscaleoffset2", |
| 91 | + "astype", |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_generic_filter( |
| 95 | + store: Store, codec_class: type[numcodecs.zarr3._NumcodecsCodec], codec_config: dict[str, JSON] |
| 96 | +): |
| 97 | + data = np.linspace(0, 10, 256, dtype="float32").reshape((16, 16)) |
| 98 | + |
| 99 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 100 | + a = Array.create( |
| 101 | + store / "generic", |
| 102 | + shape=data.shape, |
| 103 | + chunk_shape=(16, 16), |
| 104 | + dtype=data.dtype, |
| 105 | + fill_value=0, |
| 106 | + codecs=[ |
| 107 | + codec_class(**codec_config), |
| 108 | + BytesCodec(), |
| 109 | + ], |
| 110 | + ) |
| 111 | + |
| 112 | + a[:, :] = data.copy() |
| 113 | + a = Array.open(store / "generic") |
| 114 | + np.testing.assert_array_equal(data, a[:, :]) |
| 115 | + |
| 116 | + |
| 117 | +def test_generic_filter_bitround(store: Store): |
| 118 | + data = np.linspace(0, 1, 256, dtype="float32").reshape((16, 16)) |
| 119 | + |
| 120 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 121 | + a = Array.create( |
| 122 | + store / "generic_bitround", |
| 123 | + shape=data.shape, |
| 124 | + chunk_shape=(16, 16), |
| 125 | + dtype=data.dtype, |
| 126 | + fill_value=0, |
| 127 | + codecs=[numcodecs.zarr3.BitRound(keepbits=3), BytesCodec()], |
| 128 | + ) |
| 129 | + |
| 130 | + a[:, :] = data.copy() |
| 131 | + a = Array.open(store / "generic_bitround") |
| 132 | + assert np.allclose(data, a[:, :], atol=0.1) |
| 133 | + |
| 134 | + |
| 135 | +def test_generic_filter_quantize(store: Store): |
| 136 | + data = np.linspace(0, 10, 256, dtype="float32").reshape((16, 16)) |
| 137 | + |
| 138 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 139 | + a = Array.create( |
| 140 | + store / "generic_quantize", |
| 141 | + shape=data.shape, |
| 142 | + chunk_shape=(16, 16), |
| 143 | + dtype=data.dtype, |
| 144 | + fill_value=0, |
| 145 | + codecs=[numcodecs.zarr3.Quantize(digits=3), BytesCodec()], |
| 146 | + ) |
| 147 | + |
| 148 | + a[:, :] = data.copy() |
| 149 | + a = Array.open(store / "generic_quantize") |
| 150 | + assert np.allclose(data, a[:, :], atol=0.001) |
| 151 | + |
| 152 | + |
| 153 | +def test_generic_filter_packbits(store: Store): |
| 154 | + data = np.zeros((16, 16), dtype="bool") |
| 155 | + data[0:4, :] = True |
| 156 | + |
| 157 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 158 | + a = Array.create( |
| 159 | + store / "generic_packbits", |
| 160 | + shape=data.shape, |
| 161 | + chunk_shape=(16, 16), |
| 162 | + dtype=data.dtype, |
| 163 | + fill_value=0, |
| 164 | + codecs=[numcodecs.zarr3.PackBits(), BytesCodec()], |
| 165 | + ) |
| 166 | + |
| 167 | + a[:, :] = data.copy() |
| 168 | + a = Array.open(store / "generic_packbits") |
| 169 | + np.testing.assert_array_equal(data, a[:, :]) |
| 170 | + |
| 171 | + with pytest.raises(ValueError, match=".*requires bool dtype.*"): |
| 172 | + Array.create( |
| 173 | + store / "generic_packbits_err", |
| 174 | + shape=data.shape, |
| 175 | + chunk_shape=(16, 16), |
| 176 | + dtype="uint32", |
| 177 | + fill_value=0, |
| 178 | + codecs=[numcodecs.zarr3.PackBits(), BytesCodec()], |
| 179 | + ) |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.parametrize( |
| 183 | + "codec_class", |
| 184 | + [ |
| 185 | + numcodecs.zarr3.CRC32, |
| 186 | + numcodecs.zarr3.CRC32C, |
| 187 | + numcodecs.zarr3.Adler32, |
| 188 | + numcodecs.zarr3.Fletcher32, |
| 189 | + numcodecs.zarr3.JenkinsLookup3, |
| 190 | + ], |
| 191 | +) |
| 192 | +def test_generic_checksum(store: Store, codec_class: type[numcodecs.zarr3._NumcodecsCodec]): |
| 193 | + data = np.linspace(0, 10, 256, dtype="float32").reshape((16, 16)) |
| 194 | + |
| 195 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 196 | + a = Array.create( |
| 197 | + store / "generic_checksum", |
| 198 | + shape=data.shape, |
| 199 | + chunk_shape=(16, 16), |
| 200 | + dtype=data.dtype, |
| 201 | + fill_value=0, |
| 202 | + codecs=[BytesCodec(), codec_class()], |
| 203 | + ) |
| 204 | + |
| 205 | + a[:, :] = data.copy() |
| 206 | + a = Array.open(store / "generic_checksum") |
| 207 | + np.testing.assert_array_equal(data, a[:, :]) |
| 208 | + |
| 209 | + |
| 210 | +@pytest.mark.parametrize("codec_class", [numcodecs.zarr3.PCodec, numcodecs.zarr3.ZFPY]) |
| 211 | +def test_generic_bytes_codec(store: Store, codec_class: type[numcodecs.zarr3._NumcodecsCodec]): |
| 212 | + try: |
| 213 | + codec_class()._codec # noqa: B018 |
| 214 | + except ValueError as e: |
| 215 | + if "codec not available" in str(e): |
| 216 | + pytest.xfail(f"{codec_class.codec_name} is not available: {e}") |
| 217 | + else: |
| 218 | + raise # pragma: no cover |
| 219 | + except ImportError as e: |
| 220 | + pytest.xfail(f"{codec_class.codec_name} is not available: {e}") |
| 221 | + |
| 222 | + data = np.arange(0, 256, dtype="float32").reshape((16, 16)) |
| 223 | + |
| 224 | + with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): |
| 225 | + a = Array.create( |
| 226 | + store / "generic", |
| 227 | + shape=data.shape, |
| 228 | + chunk_shape=(16, 16), |
| 229 | + dtype=data.dtype, |
| 230 | + fill_value=0, |
| 231 | + codecs=[ |
| 232 | + codec_class(), |
| 233 | + ], |
| 234 | + ) |
| 235 | + |
| 236 | + a[:, :] = data.copy() |
| 237 | + np.testing.assert_array_equal(data, a[:, :]) |
0 commit comments