Skip to content

Commit 9d046ea

Browse files
dcherianrabernatjhamman
authored
Fix is_total_slice for size-1 dimensions (#1800)
Closes #1730 Co-authored-by: Ryan Abernathey <[email protected]> Co-authored-by: Joe Hamman <[email protected]>
1 parent 0a29fb3 commit 9d046ea

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

docs/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Unreleased
2020

2121
Enhancements
2222
~~~~~~~~~~~~
23+
* Performance improvement for reading and writing chunks if any of the dimensions is size 1. :issue:`1730`
24+
By :user:`Deepak Cherian <dcherian>`.
2325

2426

2527
Docs

zarr/tests/test_util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ def test_is_total_slice():
8989
assert not is_total_slice((slice(0, 50), slice(0, 50)), (100, 100))
9090
assert not is_total_slice((slice(0, 100, 2), slice(0, 100)), (100, 100))
9191

92+
# size-1 dimension edge-case
93+
# https://github.com/zarr-developers/zarr-python/issues/1730
94+
assert is_total_slice((slice(0, 1),), (1,))
95+
# this is an equivalent selection (without a slice)
96+
assert is_total_slice((0,), (1,))
97+
# same for multidimensional selection
98+
assert is_total_slice((slice(0, 1), slice(0, 10)), (1, 10))
99+
assert is_total_slice((0, slice(0, 10)), (1, 10))
100+
92101
with pytest.raises(TypeError):
93102
is_total_slice("foo", (100,))
94103

zarr/util.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,17 @@ def is_total_slice(item, shape: Tuple[int]) -> bool:
234234
if isinstance(item, tuple):
235235
return all(
236236
(
237-
isinstance(it, slice)
238-
and ((it == slice(None)) or ((it.stop - it.start == sh) and (it.step in [1, None])))
237+
(
238+
isinstance(it, slice)
239+
and (
240+
(it == slice(None))
241+
or ((it.stop - it.start == sh) and (it.step in [1, None]))
242+
)
243+
)
244+
# The only scalar edge case, indexing with int 0 along a size-1 dimension
245+
# is identical to a total slice
246+
# https://github.com/zarr-developers/zarr-python/issues/1730
247+
or (isinstance(it, int) and it == 0 and sh == 1)
239248
)
240249
for it, sh in zip(item, shape)
241250
)

0 commit comments

Comments
 (0)