Skip to content

Commit 52a7383

Browse files
authored
Merge branch 'main' into update-pcodec
2 parents 94db6a8 + 9bdbaf0 commit 52a7383

File tree

6 files changed

+97
-49
lines changed

6 files changed

+97
-49
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repos:
1515
- id: debug-statements
1616

1717
- repo: https://github.com/astral-sh/ruff-pre-commit
18-
rev: v0.8.0
18+
rev: v0.8.1
1919
hooks:
2020
- id: ruff
2121
args: ["--fix", "--show-fixes"]

docs/release.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,28 @@ Breaking changes
2222
are now keyword only, to support the updated API.
2323
By :user:`Sam Levang <slevang>`, :issue:`623`
2424

25-
Enhancements
25+
26+
Fixes
27+
~~~~~
28+
* Fixes issue with ``Delta`` Zarr 3 codec not working with ``astype``.
29+
By :user:`Norman Rzepka <normanrz>`, :issue:`664`
30+
31+
32+
Improvements
2633
~~~~~~~~~~~~
2734
* Add support for ``pcodec`` 0.3. This exposes the new ``delta_spec``
2835
and ``paging_spec`` arguments, but maintains full backwards
2936
compatibility for data written with older package versions.
3037
By :user:`Sam Levang <slevang>`, :issue:`623`
38+
* If an import error is raised when trying to define a codec that is *not*
39+
an optional dependency, it is no longer silently caught. Instead it will
40+
be propagated to the user, as this indicates an issue with the installed
41+
package.
42+
43+
Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec)
44+
are still silently caught.
45+
By :user:`David Stansby <dstansby>`, :issue:`550`.
46+
3147

3248
0.14.1
3349
------

docs/zarr3.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _Zarr 3 codecs:
2+
13
Zarr 3 codecs
24
=============
35
.. automodule:: numcodecs.zarr3

numcodecs/__init__.py

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,32 @@
3636

3737
register_codec(BZ2)
3838

39-
with suppress(ImportError):
40-
from numcodecs.lzma import LZMA
39+
from numcodecs.lzma import LZMA
4140

42-
register_codec(LZMA)
41+
register_codec(LZMA)
4342

44-
with suppress(ImportError):
45-
from numcodecs import blosc
46-
from numcodecs.blosc import Blosc
47-
48-
register_codec(Blosc)
49-
# initialize blosc
50-
try:
51-
ncores = multiprocessing.cpu_count()
52-
except OSError: # pragma: no cover
53-
ncores = 1
54-
blosc.init()
55-
blosc.set_nthreads(min(8, ncores))
56-
atexit.register(blosc.destroy)
43+
from numcodecs import blosc
44+
from numcodecs.blosc import Blosc
5745

58-
with suppress(ImportError):
59-
from numcodecs import zstd as zstd
60-
from numcodecs.zstd import Zstd
46+
register_codec(Blosc)
47+
# initialize blosc
48+
try:
49+
ncores = multiprocessing.cpu_count()
50+
except OSError: # pragma: no cover
51+
ncores = 1
52+
blosc.init()
53+
blosc.set_nthreads(min(8, ncores))
54+
atexit.register(blosc.destroy)
6155

62-
register_codec(Zstd)
56+
from numcodecs import zstd as zstd
57+
from numcodecs.zstd import Zstd
6358

64-
with suppress(ImportError):
65-
from numcodecs import lz4 as lz4
66-
from numcodecs.lz4 import LZ4
59+
register_codec(Zstd)
6760

68-
register_codec(LZ4)
61+
from numcodecs import lz4 as lz4
62+
from numcodecs.lz4 import LZ4
6963

70-
with suppress(ImportError):
71-
from numcodecs.zfpy import ZFPY
72-
73-
register_codec(ZFPY)
64+
register_codec(LZ4)
7465

7566
from numcodecs.astype import AsType
7667

@@ -112,38 +103,44 @@
112103

113104
register_codec(BitRound)
114105

115-
with suppress(ImportError):
116-
from numcodecs.msgpacks import MsgPack
117-
118-
register_codec(MsgPack)
119-
120106
from numcodecs.checksum32 import CRC32, Adler32, JenkinsLookup3
121107

122108
register_codec(CRC32)
123109
register_codec(Adler32)
124110
register_codec(JenkinsLookup3)
125111

126-
with suppress(ImportError):
127-
from numcodecs.checksum32 import CRC32C
128-
129-
register_codec(CRC32C)
130-
131112
from numcodecs.json import JSON
132113

133114
register_codec(JSON)
134115

135-
with suppress(ImportError):
136-
from numcodecs import vlen as vlen
137-
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8
116+
from numcodecs import vlen as vlen
117+
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8
138118

139-
register_codec(VLenUTF8)
140-
register_codec(VLenBytes)
141-
register_codec(VLenArray)
119+
register_codec(VLenUTF8)
120+
register_codec(VLenBytes)
121+
register_codec(VLenArray)
142122

143123
from numcodecs.fletcher32 import Fletcher32
144124

145125
register_codec(Fletcher32)
146126

147-
from numcodecs.pcodec import PCodec
127+
# Optional depenedencies
128+
with suppress(ImportError):
129+
from numcodecs.zfpy import ZFPY
130+
131+
register_codec(ZFPY)
132+
133+
with suppress(ImportError):
134+
from numcodecs.msgpacks import MsgPack
135+
136+
register_codec(MsgPack)
137+
138+
with suppress(ImportError):
139+
from numcodecs.checksum32 import CRC32C
140+
141+
register_codec(CRC32C)
142+
143+
with suppress(ImportError):
144+
from numcodecs.pcodec import PCodec
148145

149-
register_codec(PCodec)
146+
register_codec(PCodec)

numcodecs/tests/test_zarr3.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,24 @@ def test_generic_bytes_codec(store: StorePath, codec_class: type[numcodecs.zarr3
244244

245245
a[:, :] = data.copy()
246246
np.testing.assert_array_equal(data, a[:, :])
247+
248+
249+
def test_delta_astype(store: StorePath):
250+
data = np.linspace(0, 10, 256, dtype="i8").reshape((16, 16))
251+
252+
with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR):
253+
a = Array.create(
254+
store / "generic",
255+
shape=data.shape,
256+
chunk_shape=(16, 16),
257+
dtype=data.dtype,
258+
fill_value=0,
259+
codecs=[
260+
numcodecs.zarr3.Delta(dtype="i8", astype="i2"), # type: ignore[arg-type]
261+
BytesCodec(),
262+
],
263+
)
264+
265+
a[:, :] = data.copy()
266+
a = Array.open(store / "generic")
267+
np.testing.assert_array_equal(data, a[:, :])

numcodecs/zarr3.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,19 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Shuffle:
266266

267267

268268
# array-to-array codecs ("filters")
269-
Delta = _add_docstring(_make_array_array_codec("delta", "Delta"), "numcodecs.delta.Delta")
269+
@_add_docstring_wrapper("numcodecs.delta.Delta")
270+
class Delta(_NumcodecsArrayArrayCodec):
271+
codec_name = f"{CODEC_PREFIX}delta"
272+
273+
def __init__(self, **codec_config: dict[str, JSON]) -> None:
274+
super().__init__(**codec_config)
275+
276+
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
277+
if astype := self.codec_config.get("astype"):
278+
return replace(chunk_spec, dtype=np.dtype(astype)) # type: ignore[arg-type]
279+
return chunk_spec
280+
281+
270282
BitRound = _add_docstring(
271283
_make_array_array_codec("bitround", "BitRound"), "numcodecs.bitround.BitRound"
272284
)

0 commit comments

Comments
 (0)