Skip to content

Commit

Permalink
Remove custom zattrs type
Browse files Browse the repository at this point in the history
  • Loading branch information
abarciauskas-bgse committed Feb 12, 2025
1 parent 495d660 commit a262f0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
5 changes: 3 additions & 2 deletions virtualizarr/translators/kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
KerchunkArrRefs,
KerchunkStoreRefs,
)
from virtualizarr.zarr import ZAttrs, determine_chunk_grid_shape
from virtualizarr.zarr import determine_chunk_grid_shape


def virtual_vars_and_metadata_from_kerchunk_refs(
Expand Down Expand Up @@ -265,14 +265,15 @@ def extract_array_refs(
)


from zarr.core.common import JSON
from zarr.core.metadata import ArrayV3Metadata

from virtualizarr.zarr import from_kerchunk_refs


def parse_array_refs(
arr_refs: KerchunkArrRefs,
) -> tuple[dict, ArrayV3Metadata, ZAttrs]:
) -> tuple[dict, ArrayV3Metadata, dict[str, JSON]]:
metadata = from_kerchunk_refs(arr_refs.pop(".zarray"))
zattrs = arr_refs.pop(".zattrs", {})
chunk_dict = arr_refs
Expand Down
37 changes: 6 additions & 31 deletions virtualizarr/zarr.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
from typing import TYPE_CHECKING, Any, Literal, NewType
from typing import Any

import numpy as np
from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec, BytesBytesCodec
from zarr.abc.codec import Codec as ZarrCodec
from zarr.core.codec_pipeline import BatchedCodecPipeline
from zarr.core.common import JSON
from zarr.core.metadata.v2 import ArrayV2Metadata
from zarr.core.metadata.v3 import ArrayV3Metadata

if TYPE_CHECKING:
try:
from zarr.abc.codec import Codec as ZarrCodec
except ImportError:
pass

# TODO replace these with classes imported directly from Zarr? (i.e. Zarr Object Models)
ZAttrs = NewType(
"ZAttrs", dict[str, Any]
) # just the .zattrs (for one array or for the whole store/group)
FillValueT = bool | str | float | int | list | None
ZARR_FORMAT = Literal[2, 3]

ZARR_DEFAULT_FILL_VALUE: dict[str, FillValueT] = {
# numpy dtypes's hierarchy lets us avoid checking for all the widths
# https://numpy.org/doc/stable/reference/arrays.scalars.html
np.dtype("bool").kind: False,
np.dtype("int").kind: 0,
np.dtype("float").kind: 0.0,
np.dtype("complex").kind: [0.0, 0.0],
np.dtype("datetime64").kind: 0,
}
"""
The value and format of the fill_value depend on the `data_type` of the array.
See here for spec:
https://zarr-specs.readthedocs.io/en/latest/v3/core/v3.0.html#fill-value
"""
from zarr.core.metadata.v3 import (
ArrayV3Metadata, # just the .zattrs (for one array or for the whole store/group)
)


def ceildiv(a: int, b: int) -> int:
Expand Down Expand Up @@ -228,9 +202,10 @@ def to_kerchunk_json(v2_metadata: ArrayV2Metadata) -> str:

def from_kerchunk_refs(decoded_arr_refs_zarray) -> "ArrayV3Metadata":
"""
Convert a decoded Zarr array reference to an ArrayV3Metadata object.
Convert a decoded zarr array (.zarray) reference to an ArrayV3Metadata object.
This function processes the given decoded Zarr array reference dictionary,
to construct and return an ArrayV3Metadata object based on the provided information.
Parameters:
----------
decoded_arr_refs_zarray : dict
Expand Down

0 comments on commit a262f0b

Please sign in to comment.