Skip to content

Commit 11fd8db

Browse files
dcheriandstansby
andauthored
Fix orthogonal indexing with scalar. (#1947)
Co-authored-by: David Stansby <[email protected]>
1 parent f231bb2 commit 11fd8db

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

docs/release.rst

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ Release notes
1818
See `GH1777 <https://github.com/zarr-developers/zarr-python/issues/1777>`_ for more details on the upcoming
1919
3.0 release.
2020

21+
.. _release_2.18.3:
22+
23+
2.18.3
24+
------
25+
26+
Maintenance
27+
~~~~~~~~~~~
28+
* Fix a regression when using orthogonal indexing with a scalar.
29+
By :user:`Deepak Cherian <dcherian>` :issue:`1931`
30+
31+
2132
.. _release_2.18.2:
2233

2334
Enhancements

zarr/core.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,12 @@ def _process_chunk(
20532053
if isinstance(cdata, UncompressedPartialReadBufferV3):
20542054
cdata = cdata.read_full()
20552055
chunk = ensure_ndarray_like(cdata).view(self._dtype)
2056-
chunk = chunk.reshape(self._chunks, order=self._order)
2056+
# dest.shape is not self._chunks when a dimensions is squeezed out
2057+
# For example, assume self._chunks = (5, 5, 1)
2058+
# and the selection is [:, :, 0]
2059+
# Then out_selection is (slice(5), slice(5))
2060+
# See https://github.com/zarr-developers/zarr-python/issues/1931
2061+
chunk = chunk.reshape(dest.shape, order=self._order)
20572062
np.copyto(dest, chunk)
20582063
return
20592064

zarr/tests/test_core.py

+17
Original file line numberDiff line numberDiff line change
@@ -3206,3 +3206,20 @@ def test_object_array_indexing():
32063206
elem = [1, 3]
32073207
arr[1] = elem
32083208
assert arr[1] == elem
3209+
3210+
3211+
@pytest.mark.parametrize("shape", ((1, 1, 1), (5, 5, 1), (1, 5, 5)))
3212+
def test_scalar_orthogonal_indexing(shape):
3213+
# regression test for https://github.com/zarr-developers/zarr-python/issues/1931
3214+
store = zarr.MemoryStore({})
3215+
data = np.random.randint(0, 255, shape)
3216+
arr = zarr.zeros(
3217+
shape=shape, chunks=shape[:-1] + (1,), compressor=None, store=store, dtype="u1"
3218+
)
3219+
arr[:, :, :] = data
3220+
store.close()
3221+
3222+
zf = zarr.open(store, "r")
3223+
assert_array_equal(zf[0, :, :], data[0, :, :])
3224+
assert_array_equal(zf[:, 0, :], data[:, 0, :])
3225+
assert_array_equal(zf[:, :, 0], data[:, :, 0])

0 commit comments

Comments
 (0)