Skip to content

Commit

Permalink
Don't overwrite codec config values when set. (#700)
Browse files Browse the repository at this point in the history
* Don't overwrite codec config values when set.

A codec doesn't know where it is in the codec pipeline, so it
cannot know whether `array_spec.dtype` should match `codec.dtype`
or not.

* little more

* FIx test

* fix test

---------

Co-authored-by: Davis Bennett <[email protected]>
  • Loading branch information
dcherian and d-v-b authored Feb 8, 2025
1 parent 8a0bd3b commit 3cf8ab1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion numcodecs/tests/test_zarr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_generic_compressor(
(numcodecs.zarr3.Delta, {"dtype": "float32"}),
(numcodecs.zarr3.FixedScaleOffset, {"offset": 0, "scale": 25.5}),
(numcodecs.zarr3.FixedScaleOffset, {"offset": 0, "scale": 51, "astype": "uint16"}),
(numcodecs.zarr3.AsType, {"encode_dtype": "float32", "decode_dtype": "float64"}),
(numcodecs.zarr3.AsType, {"encode_dtype": "float32", "decode_dtype": "float32"}),
],
ids=[
"delta",
Expand Down
9 changes: 4 additions & 5 deletions numcodecs/zarr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def __init__(self, **codec_config: JSON) -> None:
super().__init__(**codec_config)

def evolve_from_array_spec(self, array_spec: ArraySpec) -> Shuffle:
if array_spec.dtype.itemsize != self.codec_config.get("elementsize"):
if self.codec_config.get("elementsize", None) is None:
return Shuffle(**{**self.codec_config, "elementsize": array_spec.dtype.itemsize})
return self # pragma: no cover

Expand Down Expand Up @@ -308,7 +308,7 @@ def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
return chunk_spec

def evolve_from_array_spec(self, array_spec: ArraySpec) -> FixedScaleOffset:
if str(array_spec.dtype) != self.codec_config.get("dtype"):
if self.codec_config.get("dtype", None) is None:
return FixedScaleOffset(**{**self.codec_config, "dtype": str(array_spec.dtype)})
return self

Expand All @@ -321,7 +321,7 @@ def __init__(self, **codec_config: JSON) -> None:
super().__init__(**codec_config)

def evolve_from_array_spec(self, array_spec: ArraySpec) -> Quantize:
if str(array_spec.dtype) != self.codec_config.get("dtype"):
if self.codec_config.get("dtype", None) is None:
return Quantize(**{**self.codec_config, "dtype": str(array_spec.dtype)})
return self

Expand Down Expand Up @@ -356,8 +356,7 @@ def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
return replace(chunk_spec, dtype=np.dtype(self.codec_config["encode_dtype"])) # type: ignore[arg-type]

def evolve_from_array_spec(self, array_spec: ArraySpec) -> AsType:
decode_dtype = self.codec_config.get("decode_dtype")
if str(array_spec.dtype) != decode_dtype:
if self.codec_config.get("decode_dtype", None) is None:
return AsType(**{**self.codec_config, "decode_dtype": str(array_spec.dtype)})
return self

Expand Down

0 comments on commit 3cf8ab1

Please sign in to comment.