|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Mapping |
| 4 | +from dataclasses import replace |
| 5 | +from typing import TYPE_CHECKING, Any, Literal, Self, TypedDict, TypeGuard, overload |
| 6 | + |
| 7 | +from typing_extensions import ReadOnly |
| 8 | + |
| 9 | +from zarr.codecs.numcodecs._codecs import ( |
| 10 | + _NumcodecsArrayArrayCodec, |
| 11 | + _warn_unstable_specification, |
| 12 | +) |
| 13 | +from zarr.core.common import ( |
| 14 | + CodecJSON, |
| 15 | + CodecJSON_V2, |
| 16 | + CodecJSON_V3, |
| 17 | + NamedRequiredConfig, |
| 18 | + ZarrFormat, |
| 19 | + _check_codecjson_v2, |
| 20 | + _check_codecjson_v3, |
| 21 | +) |
| 22 | +from zarr.core.dtype.common import check_dtype_name_v2, check_dtype_spec_v3 |
| 23 | +from zarr.dtype import parse_dtype |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from zarr.core.array_spec import ArraySpec |
| 27 | + from zarr.core.dtype.common import DTypeName_V2, DTypeSpec_V3 |
| 28 | + |
| 29 | + |
| 30 | +class DeltaConfig_V2(TypedDict): |
| 31 | + dtype: DTypeName_V2 |
| 32 | + astype: DTypeName_V2 |
| 33 | + |
| 34 | + |
| 35 | +class DeltaConfig_V3(TypedDict): |
| 36 | + dtype: DTypeSpec_V3 |
| 37 | + astype: DTypeSpec_V3 |
| 38 | + |
| 39 | + |
| 40 | +class DeltaJSON_V2(DeltaConfig_V2): |
| 41 | + """JSON representation of Delta codec for Zarr V2.""" |
| 42 | + |
| 43 | + id: ReadOnly[Literal["delta"]] |
| 44 | + |
| 45 | + |
| 46 | +class DeltaJSON_V3(NamedRequiredConfig[Literal["delta"], DeltaConfig_V3]): |
| 47 | + """JSON representation of Delta codec for Zarr V3.""" |
| 48 | + |
| 49 | + |
| 50 | +def check_json_v2(data: object) -> TypeGuard[DeltaJSON_V2]: |
| 51 | + """ |
| 52 | + A type guard for the Zarr V2 form of the Delta codec JSON |
| 53 | + """ |
| 54 | + return ( |
| 55 | + _check_codecjson_v2(data) |
| 56 | + and data["id"] == "delta" |
| 57 | + and "astype" in data |
| 58 | + and "dtype" in data |
| 59 | + and check_dtype_name_v2(data["dtype"]) # type: ignore[typeddict-item] |
| 60 | + and check_dtype_name_v2(data["astype"]) # type: ignore[typeddict-item] |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def check_json_v3(data: object) -> TypeGuard[DeltaJSON_V3]: |
| 65 | + """ |
| 66 | + A type guard for the Zarr V3 form of the Delta codec JSON |
| 67 | + """ |
| 68 | + return ( |
| 69 | + _check_codecjson_v3(data) |
| 70 | + and isinstance(data, Mapping) |
| 71 | + and data["name"] == "delta" |
| 72 | + and "configuration" in data |
| 73 | + and "astype" in data["configuration"] |
| 74 | + and "dtype" in data["configuration"] |
| 75 | + and check_dtype_spec_v3(data["configuration"]["dtype"]) |
| 76 | + and check_dtype_spec_v3(data["configuration"]["astype"]) |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +class Delta(_NumcodecsArrayArrayCodec): |
| 81 | + """ |
| 82 | + A wrapper around the numcodecs.Delta codec that provides Zarr V3 compatibility. |
| 83 | +
|
| 84 | + This class does not have a stable API. |
| 85 | + """ |
| 86 | + |
| 87 | + codec_name = "numcodecs.delta" |
| 88 | + _codec_id = "delta" |
| 89 | + codec_config: DeltaJSON_V2 |
| 90 | + |
| 91 | + def __init__(self, **codec_config: Any) -> None: |
| 92 | + if "codec_config" in codec_config: |
| 93 | + raise ValueError("The argument 'codec_config' is not supported.") |
| 94 | + super().__init__(**codec_config) |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def _from_json_v2(cls, data: CodecJSON_V2) -> Self: |
| 98 | + return cls(**data) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def _from_json_v3(cls, data: CodecJSON_V3) -> Self: |
| 102 | + if check_json_v3(data): |
| 103 | + config = data["configuration"] |
| 104 | + astype = parse_dtype(config["astype"], zarr_format=3).to_json(zarr_format=2)["name"] |
| 105 | + dtype = parse_dtype(config["dtype"], zarr_format=3).to_json(zarr_format=2)["name"] |
| 106 | + |
| 107 | + return cls(astype=astype, dtype=dtype) |
| 108 | + raise TypeError(f"Invalid JSON: {data}") |
| 109 | + |
| 110 | + @classmethod |
| 111 | + def from_json(cls, data: CodecJSON) -> Self: |
| 112 | + if _check_codecjson_v2(data): |
| 113 | + return cls._from_json_v2(data) |
| 114 | + return cls._from_json_v3(data) |
| 115 | + |
| 116 | + @overload |
| 117 | + def to_json(self, zarr_format: Literal[2]) -> DeltaJSON_V2: ... |
| 118 | + @overload |
| 119 | + def to_json(self, zarr_format: Literal[3]) -> DeltaJSON_V3: ... |
| 120 | + def to_json(self, zarr_format: ZarrFormat) -> DeltaJSON_V2 | DeltaJSON_V3: |
| 121 | + _warn_unstable_specification(self) |
| 122 | + if zarr_format == 2: |
| 123 | + return self.codec_config |
| 124 | + conf = self.codec_config |
| 125 | + astype_v3 = parse_dtype(conf["astype"], zarr_format=2).to_json(zarr_format=3) |
| 126 | + dtype_v3 = parse_dtype(conf["dtype"], zarr_format=2).to_json(zarr_format=3) |
| 127 | + return { |
| 128 | + "name": "delta", |
| 129 | + "configuration": {"astype": astype_v3, "dtype": dtype_v3}, |
| 130 | + } |
| 131 | + |
| 132 | + def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec: |
| 133 | + if astype := self.codec_config.get("astype"): |
| 134 | + dtype = parse_dtype(astype, zarr_format=3) |
| 135 | + return replace(chunk_spec, dtype=dtype) |
| 136 | + return chunk_spec |
0 commit comments