Skip to content

Add async oindex method to AsyncArray #3083

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
MaskIndexer,
MaskSelection,
OIndex,
AsyncOIndex,
OrthogonalIndexer,
OrthogonalSelection,
Selection,
Expand Down Expand Up @@ -1358,6 +1359,21 @@
)
return await self._get_selection(indexer, prototype=prototype)

async def get_orthogonal_selection(
self,
selection: OrthogonalSelection,
*,
out: NDBuffer | None = None,
fields: Fields | None = None,
prototype: BufferPrototype | None = None,
) -> NDArrayLike:
if prototype is None:
prototype = default_buffer_prototype()
indexer = OrthogonalIndexer(selection, self.shape, self.metadata.chunk_grid)
return await self._get_selection(

Check warning on line 1373 in src/zarr/core/array.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/array.py#L1370-L1373

Added lines #L1370 - L1373 were not covered by tests
indexer=indexer, out=out, fields=fields, prototype=prototype
)

async def _save_metadata(self, metadata: ArrayMetadata, ensure_parents: bool = False) -> None:
"""
Asynchronously save the array metadata.
Expand Down Expand Up @@ -1488,6 +1504,12 @@
)
return await self._set_selection(indexer, value, prototype=prototype)

@property
def oindex(self) -> AsyncOIndex:
"""Shortcut for orthogonal (outer) indexing, see :func:`get_orthogonal_selection` and
:func:`set_orthogonal_selection` for documentation and examples."""
return AsyncOIndex(self)

Check warning on line 1511 in src/zarr/core/array.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/array.py#L1511

Added line #L1511 was not covered by tests

async def resize(self, new_shape: ShapeLike, delete_outside_chunks: bool = True) -> None:
"""
Asynchronously resize the array to a new shape.
Expand Down
23 changes: 21 additions & 2 deletions src/zarr/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from zarr.core.common import product

if TYPE_CHECKING:
from zarr.core.array import Array
from zarr.core.array import Array, AsyncArray
from zarr.core.buffer import NDArrayLikeOrScalar
from zarr.core.chunk_grids import ChunkGrid
from zarr.core.common import ChunkCoords
Expand Down Expand Up @@ -950,7 +950,7 @@
return self.array.get_orthogonal_selection(
cast(OrthogonalSelection, new_selection), fields=fields
)

def __setitem__(self, selection: OrthogonalSelection, value: npt.ArrayLike) -> None:
fields, new_selection = pop_fields(selection)
new_selection = ensure_tuple(new_selection)
Expand All @@ -960,6 +960,25 @@
)


@dataclass(frozen=True)
class AsyncOIndex:
array: AsyncArray

async def getitem(self, selection: OrthogonalSelection | Array) -> NDArrayLike:
from zarr.core.array import Array

Check warning on line 968 in src/zarr/core/indexing.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/indexing.py#L968

Added line #L968 was not covered by tests

# if input is a Zarr array, we materialize it now.
if isinstance(selection, Array):
selection = _zarr_array_to_int_or_bool_array(selection)

Check warning on line 972 in src/zarr/core/indexing.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/indexing.py#L971-L972

Added lines #L971 - L972 were not covered by tests

fields, new_selection = pop_fields(selection)
new_selection = ensure_tuple(new_selection)
new_selection = replace_lists(new_selection)
return await self.array.get_orthogonal_selection(

Check warning on line 977 in src/zarr/core/indexing.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/indexing.py#L974-L977

Added lines #L974 - L977 were not covered by tests
cast(OrthogonalSelection, new_selection), fields=fields
)


@dataclass(frozen=True)
class BlockIndexer(Indexer):
dim_indexers: list[SliceDimIndexer]
Expand Down
Loading