Skip to content

Commit e178492

Browse files
committed
normalize in codec_pipeline
1 parent 9a47b15 commit e178492

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@
3535
U = TypeVar("U")
3636

3737

38+
def normalize_slices(
39+
idxr: tuple[int | slice, ...], shape: tuple[int, ...]
40+
) -> tuple[int | slice, ...]:
41+
# replace slice objects with stop==None with size
42+
out = []
43+
for i, size in zip(idxr, shape, strict=False):
44+
if not isinstance(i, slice):
45+
out.append(i)
46+
continue
47+
if i.step not in [1, None] or i.start not in [0, None]:
48+
out.append(i)
49+
continue
50+
out.append(slice(i.start, i.stop if i.stop is not None else size, i.step))
51+
return tuple(out)
52+
53+
3854
def _unzip2(iterable: Iterable[tuple[T, U]]) -> tuple[list[T], list[U]]:
3955
out0: list[T] = []
4056
out1: list[U] = []
@@ -279,7 +295,10 @@ async def read_batch(
279295
chunk_array_batch, batch_info, strict=False
280296
):
281297
if chunk_array is not None:
282-
tmp = chunk_array[chunk_selection]
298+
normalized_selection = normalize_slices(
299+
chunk_selection, out[out_selection].shape
300+
)
301+
tmp = chunk_array[normalized_selection]
283302
if drop_axes != ():
284303
tmp = tmp.squeeze(axis=drop_axes)
285304
out[out_selection] = tmp
@@ -322,7 +341,9 @@ def _merge_chunk_array(
322341
for idx in range(chunk_spec.ndim)
323342
)
324343
chunk_value = chunk_value[item]
325-
chunk_array[chunk_selection] = chunk_value
344+
345+
normalized_selection = normalize_slices(chunk_selection, chunk_value.shape)
346+
chunk_array[normalized_selection] = chunk_value
326347
return chunk_array
327348

328349
async def write_batch(

0 commit comments

Comments
 (0)