Skip to content

Commit

Permalink
Merge branch 'v3' into store_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-v-b committed May 22, 2024
2 parents df14bc0 + 4da9505 commit bc31613
Show file tree
Hide file tree
Showing 19 changed files with 30 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ repos:
hooks:
- id: mypy
files: src
args: []
additional_dependencies:
- types-redis
- types-setuptools
- pytest
- numpy
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ module = [
"zarr.v2.*",
"zarr.array_v2",
"zarr.array",
"zarr.buffer"
]
disallow_untyped_calls = false

Expand Down
4 changes: 2 additions & 2 deletions src/zarr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ async def setitem(
# We accept any ndarray like object from the user and convert it
# to a NDBuffer (or subclass). From this point onwards, we only pass
# Buffer and NDBuffer between components.
value = factory(value)
value_buffer = factory(value)

# merging with existing data and encoding chunks
await self.metadata.codec_pipeline.write(
Expand All @@ -432,7 +432,7 @@ async def setitem(
)
for chunk_coords, chunk_selection, out_selection in indexer
],
value,
value_buffer,
)

async def resize(
Expand Down
27 changes: 14 additions & 13 deletions src/zarr/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)

import numpy as np
import numpy.typing as npt

if TYPE_CHECKING:
from typing_extensions import Self
Expand All @@ -20,8 +21,8 @@

# TODO: create a protocol for the attributes we need, for now we alias Numpy's ndarray
# both for the array-like and ndarray-like
ArrayLike: TypeAlias = np.ndarray
NDArrayLike: TypeAlias = np.ndarray
ArrayLike: TypeAlias = npt.NDArray[Any]
NDArrayLike: TypeAlias = npt.NDArray[Any]


def check_item_key_is_1d_contiguous(key: Any) -> None:
Expand All @@ -40,7 +41,7 @@ def __call__(
self,
*,
shape: Iterable[int],
dtype: np.DTypeLike,
dtype: npt.DTypeLike,
order: Literal["C", "F"],
fill_value: Any | None,
) -> NDBuffer:
Expand Down Expand Up @@ -163,7 +164,7 @@ def as_array_like(self) -> NDArrayLike:
"""
return self._data

def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
def as_nd_buffer(self, *, dtype: npt.DTypeLike) -> NDBuffer:
"""Create a new NDBuffer from this one.
This will never copy data.
Expand All @@ -179,7 +180,7 @@ def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
"""
return NDBuffer.from_ndarray_like(self._data.view(dtype=dtype))

def as_numpy_array(self) -> np.ndarray:
def as_numpy_array(self) -> npt.NDArray[Any]:
"""Return the buffer as a NumPy array (host memory).
Warning
Expand Down Expand Up @@ -271,7 +272,7 @@ def create(
cls,
*,
shape: Iterable[int],
dtype: np.DTypeLike,
dtype: npt.DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self:
Expand All @@ -298,7 +299,7 @@ def create(
A subclass can overwrite this method to create a ndarray-like object
other then the default Numpy array.
"""
ret = cls(np.empty(shape=shape, dtype=dtype, order=order))
ret = cls(np.empty(shape=tuple(shape), dtype=dtype, order=order))
if fill_value is not None:
ret.fill(fill_value)
return ret
Expand All @@ -319,7 +320,7 @@ def from_ndarray_like(cls, ndarray_like: NDArrayLike) -> Self:
return cls(ndarray_like)

@classmethod
def from_numpy_array(cls, array_like: np.ArrayLike) -> Self:
def from_numpy_array(cls, array_like: npt.ArrayLike) -> Self:
"""Create a new buffer of Numpy array-like object
Parameters
Expand Down Expand Up @@ -360,7 +361,7 @@ def as_buffer(self) -> Buffer:
data = np.ascontiguousarray(self._data)
return Buffer(data.reshape(-1).view(dtype="b")) # Flatten the array without copy

def as_numpy_array(self) -> np.ndarray:
def as_numpy_array(self) -> npt.NDArray[Any]:
"""Return the buffer as a NumPy array (host memory).
Warning
Expand Down Expand Up @@ -393,9 +394,9 @@ def byteorder(self) -> Endian:
return Endian(sys.byteorder)

def reshape(self, newshape: Iterable[int]) -> Self:
return self.__class__(self._data.reshape(newshape))
return self.__class__(self._data.reshape(tuple(newshape)))

def astype(self, dtype: np.DTypeLike, order: Literal["K", "A", "C", "F"] = "K") -> Self:
def astype(self, dtype: npt.DTypeLike, order: Literal["K", "A", "C", "F"] = "K") -> Self:
return self.__class__(self._data.astype(dtype=dtype, order=order))

def __getitem__(self, key: Any) -> Self:
Expand All @@ -418,11 +419,11 @@ def fill(self, value: Any) -> None:
def copy(self) -> Self:
return self.__class__(self._data.copy())

def transpose(self, *axes: np.SupportsIndex) -> Self:
def transpose(self, *axes: np.SupportsIndex) -> Self: # type: ignore[name-defined]
return self.__class__(self._data.transpose(*axes))


def as_numpy_array_wrapper(func: Callable[[np.ndarray], bytes], buf: Buffer) -> Buffer:
def as_numpy_array_wrapper(func: Callable[[npt.NDArray[Any]], bytes], buf: Buffer) -> Buffer:
"""Converts the input of `func` to a numpy array and the output back to `Buffer`.
This function is useful when calling a `func` that only support host memory such
Expand Down
4 changes: 3 additions & 1 deletion src/zarr/codecs/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ async def _encode_single(
assert isinstance(chunk_array, NDBuffer)
if chunk_array.dtype.itemsize > 1:
if self.endian is not None and self.endian != chunk_array.byteorder:
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name)
# type-ignore is a numpy bug
# see https://github.com/numpy/numpy/issues/26473
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name) # type: ignore[arg-type]
chunk_array = chunk_array.astype(new_dtype)
return chunk_array.as_buffer()

Expand Down
5 changes: 3 additions & 2 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING, NamedTuple

import numpy as np
import numpy.typing as npt

from zarr.abc.codec import (
ArrayBytesCodec,
Expand Down Expand Up @@ -85,7 +86,7 @@ async def delete(self) -> None:

class _ShardIndex(NamedTuple):
# dtype uint64, shape (chunks_per_shard_0, chunks_per_shard_1, ..., 2)
offsets_and_lengths: np.ndarray
offsets_and_lengths: npt.NDArray[np.uint64]

@property
def chunks_per_shard(self) -> ChunkCoords:
Expand All @@ -100,7 +101,7 @@ def _localize_chunk(self, chunk_coords: ChunkCoords) -> ChunkCoords:
def is_all_empty(self) -> bool:
return bool(np.array_equiv(self.offsets_and_lengths, MAX_UINT_64))

def get_full_chunk_map(self) -> np.ndarray:
def get_full_chunk_map(self) -> npt.NDArray[np.bool_]:
return self.offsets_and_lengths[..., 0] != MAX_UINT_64

def get_chunk_slice(self, chunk_coords: ChunkCoords) -> tuple[int, int] | None:
Expand Down
7 changes: 4 additions & 3 deletions src/zarr/codecs/zstd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import numpy.typing as npt
from zstandard import ZstdCompressor, ZstdDecompressor

from zarr.abc.codec import BytesBytesCodec
Expand Down Expand Up @@ -52,11 +53,11 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
def to_dict(self) -> dict[str, JSON]:
return {"name": "zstd", "configuration": {"level": self.level, "checksum": self.checksum}}

def _compress(self, data: bytes) -> bytes:
def _compress(self, data: npt.NDArray[Any]) -> bytes:
ctx = ZstdCompressor(level=self.level, write_checksum=self.checksum)
return ctx.compress(data)

def _decompress(self, data: bytes) -> bytes:
def _decompress(self, data: npt.NDArray[Any]) -> bytes:
ctx = ZstdDecompressor()
return ctx.decompress(data)

Expand Down
3 changes: 0 additions & 3 deletions src/zarr/fixture/.zgroup

This file was deleted.

23 changes: 0 additions & 23 deletions src/zarr/fixture/flat/.zarray

This file was deleted.

Binary file removed src/zarr/fixture/flat/0.0
Binary file not shown.
22 changes: 0 additions & 22 deletions src/zarr/fixture/flat_legacy/.zarray

This file was deleted.

Binary file removed src/zarr/fixture/flat_legacy/0.0
Binary file not shown.
23 changes: 0 additions & 23 deletions src/zarr/fixture/meta/.zarray

This file was deleted.

Binary file removed src/zarr/fixture/meta/0.0
Binary file not shown.
23 changes: 0 additions & 23 deletions src/zarr/fixture/nested/.zarray

This file was deleted.

Binary file removed src/zarr/fixture/nested/0/0
Binary file not shown.
23 changes: 0 additions & 23 deletions src/zarr/fixture/nested_legacy/.zarray

This file was deleted.

Binary file removed src/zarr/fixture/nested_legacy/0/0
Binary file not shown.
4 changes: 2 additions & 2 deletions src/zarr/store/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def _put(
if start is not None:
with path.open("r+b") as f:
f.seek(start)
f.write(value.as_numpy_array())
f.write(value.as_numpy_array().tobytes())
return None
else:
return path.write_bytes(value.as_numpy_array())
return path.write_bytes(value.as_numpy_array().tobytes())


class LocalStore(Store):
Expand Down

0 comments on commit bc31613

Please sign in to comment.