Skip to content

Commit

Permalink
move v2 chunk io into chunk.py
Browse files Browse the repository at this point in the history
  • Loading branch information
d-v-b committed Dec 5, 2023
1 parent cb6c9a8 commit 4ca61fb
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 119 deletions.
36 changes: 31 additions & 5 deletions zarr/v3/abc/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@
from __future__ import annotations

from abc import abstractmethod, ABC
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Type

import numpy as np

from zarr.v3.common import BytesLike
from zarr.v3.common import BytesLike, SliceSelection
from zarr.v3.store import StorePath


if TYPE_CHECKING:
from zarr.v3.array.base import CoreArrayMetadata
from zarr.v3.array.base import CoreArrayMetadata, CodecMetadata


class Codec(ABC):
supports_partial_decode: bool
supports_partial_encode: bool
is_fixed_size: bool
array_metadata: CoreArrayMetadata

Expand All @@ -35,6 +34,12 @@ def compute_encoded_size(self, input_byte_length: int) -> int:
def resolve_metadata(self) -> CoreArrayMetadata:
return self.array_metadata

@classmethod
def from_metadata(
cls, codec_metadata: "CodecMetadata", array_metadata: CoreArrayMetadata
) -> "Type[Codec]":
pass


class ArrayArrayCodec(Codec):
@abstractmethod
Expand Down Expand Up @@ -68,6 +73,27 @@ async def encode(
pass


class ArrayBytesCodecPartialDecodeMixin:
@abstractmethod
async def decode_partial(
self,
store_path: StorePath,
selection: SliceSelection,
) -> Optional[np.ndarray]:
pass


class ArrayBytesCodecPartialEncodeMixin:
@abstractmethod
async def encode_partial(
self,
store_path: StorePath,
chunk_array: np.ndarray,
selection: SliceSelection,
) -> None:
pass


class BytesBytesCodec(Codec):
@abstractmethod
async def decode(
Expand Down
72 changes: 16 additions & 56 deletions zarr/v3/array/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,61 +93,6 @@ def to_numpy_shortname(self) -> str:
}


@frozen
class RegularChunkGridConfigurationMetadata:
chunk_shape: ChunkCoords


@frozen
class RegularChunkGridMetadata:
configuration: RegularChunkGridConfigurationMetadata
name: Literal["regular"] = "regular"


@frozen
class DefaultChunkKeyEncodingConfigurationMetadata:
separator: Literal[".", "/"] = "/"


@frozen
class DefaultChunkKeyEncodingMetadata:
configuration: DefaultChunkKeyEncodingConfigurationMetadata = (
DefaultChunkKeyEncodingConfigurationMetadata()
)
name: Literal["default"] = "default"

def decode_chunk_key(self, chunk_key: str) -> ChunkCoords:
if chunk_key == "c":
return ()
return tuple(map(int, chunk_key[1:].split(self.configuration.separator)))

def encode_chunk_key(self, chunk_coords: ChunkCoords) -> str:
return self.configuration.separator.join(map(str, ("c",) + chunk_coords))


@frozen
class V2ChunkKeyEncodingConfigurationMetadata:
separator: Literal[".", "/"] = "."


@frozen
class V2ChunkKeyEncodingMetadata:
configuration: V2ChunkKeyEncodingConfigurationMetadata = (
V2ChunkKeyEncodingConfigurationMetadata()
)
name: Literal["v2"] = "v2"

def decode_chunk_key(self, chunk_key: str) -> ChunkCoords:
return tuple(map(int, chunk_key.split(self.configuration.separator)))

def encode_chunk_key(self, chunk_coords: ChunkCoords) -> str:
chunk_identifier = self.configuration.separator.join(map(str, chunk_coords))
return "0" if chunk_identifier == "" else chunk_identifier


ChunkKeyEncodingMetadata = Union[DefaultChunkKeyEncodingMetadata, V2ChunkKeyEncodingMetadata]


@frozen
class CoreArrayMetadata:
shape: ChunkCoords
Expand Down Expand Up @@ -185,4 +130,19 @@ def ndim(self) -> int:
@property
def dtype(self) -> np.dtype:
return np.dtype(self.metadata.dtype) """
return np.dtype(self.metadata.dtype)
@property
def size(self) -> int
return np.prod(self.metadata.shape)
@property
def T(self) -> 'ZArray':
...
def __getitem__(*args):
return _chunk_getitem_sync(*args):
def __setitem__(*args):
return _chunk_setitem_sync(*args)
"""
Loading

0 comments on commit 4ca61fb

Please sign in to comment.