Skip to content

Commit

Permalink
fix: sharding codec with fancy indexing (#2817)
Browse files Browse the repository at this point in the history
* fix: sharding codec with fancy indexing

* changelog

* add a better test

* proper fix

* fix: ArrayOfIntOrBool typing

* Revert "fix: ArrayOfIntOrBool typing"

This reverts commit 1a30563.

* ignore typing error in test

---------

Co-authored-by: Deepak Cherian <[email protected]>
  • Loading branch information
LDeakin and dcherian authored Feb 13, 2025
1 parent feeb08f commit 870265a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/2817.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix fancy indexing (e.g. arr[5, [0, 1]]) with the sharding codec
6 changes: 5 additions & 1 deletion src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,11 @@ async def _decode_partial_single(
],
out,
)
return out

if hasattr(indexer, "sel_shape"):
return out.reshape(indexer.sel_shape)
else:
return out

async def _encode_single(
self,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,3 +1429,18 @@ def test_multiprocessing(store: Store, method: Literal["fork", "spawn", "forkser

results = pool.starmap(_index_array, [(arr, slice(len(data)))])
assert all(np.array_equal(r, data) for r in results)


async def test_sharding_coordinate_selection() -> None:
store = MemoryStore()
g = zarr.open_group(store, mode="w")
arr = g.create_array(
name="a",
shape=(2, 3, 4),
chunks=(1, 2, 2),
overwrite=True,
dtype=np.float32,
shards=(2, 4, 4),
)
arr[:] = np.arange(2 * 3 * 4).reshape((2, 3, 4))
assert (arr[1, [0, 1]] == np.array([[12, 13, 14, 15], [16, 17, 18, 19]])).all() # type: ignore[index]

0 comments on commit 870265a

Please sign in to comment.