Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sharding codec with fancy indexing #2817

Merged
merged 8 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -529,7 +529,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
2 changes: 1 addition & 1 deletion src/zarr/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from zarr.core.common import ChunkCoords

IntSequence = list[int] | npt.NDArray[np.intp]
ArrayOfIntOrBool = npt.NDArray[np.intp] | npt.NDArray[np.bool_]
ArrayOfIntOrBool = IntSequence | npt.NDArray[np.bool_]
BasicSelector = int | slice | EllipsisType
Selector = BasicSelector | ArrayOfIntOrBool
BasicSelection = BasicSelector | tuple[BasicSelector, ...] # also used for BlockIndex
Expand Down
15 changes: 15 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,3 +1420,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()