Skip to content

Commit

Permalink
support zero-sized chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
brokkoli71 committed Oct 23, 2024
1 parent 8a33df7 commit b5b92af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/zarr/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def __iter__(self) -> Iterator[ChunkProjection]: ...


def ceildiv(a: float, b: float) -> int:
if a == 0:
return 0
return math.ceil(a / b)


Expand Down Expand Up @@ -374,7 +376,7 @@ def __init__(self, dim_sel: slice, dim_len: int, dim_chunk_len: int) -> None:

def __iter__(self) -> Iterator[ChunkDimProjection]:
# figure out the range of chunks we need to visit
dim_chunk_ix_from = self.start // self.dim_chunk_len
dim_chunk_ix_from = 0 if self.start == 0 else self.start // self.dim_chunk_len
dim_chunk_ix_to = ceildiv(self.stop, self.dim_chunk_len)

# iterate over chunks in range
Expand Down
9 changes: 9 additions & 0 deletions tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from numpy.testing import assert_array_equal

import zarr
from zarr import Array
from zarr.core.buffer import BufferPrototype, default_buffer_prototype
from zarr.core.indexing import (
BasicSelection,
Expand Down Expand Up @@ -1927,3 +1928,11 @@ def test_indexing_with_zarr_array(store: StorePath) -> None:

assert_array_equal(a[ii], za[zii])
assert_array_equal(a[ii], za.oindex[zii])


@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
@pytest.mark.parametrize("shape", [(0, 2, 3), (0), (3, 0)])
def test_zero_sized_chunks(store: StorePath, shape: list[int]) -> None:
z = Array.create(store=store, shape=shape, chunk_shape=shape, zarr_format=3, dtype="f8")
z[...] = 42
assert_array_equal(z[...], np.zeros(shape, dtype="f8"))

0 comments on commit b5b92af

Please sign in to comment.