Skip to content

Eager adapter

LazyArray indexing returns views. Consumers that require indexing to produce data, such as dask.array.from_array, wrap a view in EagerArrayAdapter: the adapter reads each indexed block through the view's reader and partitioning, while the view itself keeps lazy indexing.

zarr_indexing.eager

Explicit eager indexing for consumers such as Dask's from_array.

EagerArrayAdapter

Expose an eager array interface over a lazy view.

Constructing the adapter does not read source values. Indexing materializes the selected view in fresh memory, using its existing reader and partitioning. Tokenization delegates to the view's source-token contract and may read data.

Examples:

>>> import numpy as np
>>> from zarr_indexing import LazyArray
>>> view = LazyArray(np.arange(8))[1::2]
>>> EagerArrayAdapter(view)[1:3]
array([3, 5])
Source code in src/zarr_indexing/eager.py
class EagerArrayAdapter:
    """Expose an eager array interface over a lazy view.

    Constructing the adapter does not read source values. Indexing materializes
    the selected view in fresh memory, using its existing reader and partitioning.
    Tokenization delegates to the view's source-token contract and may read data.

    Examples
    --------
    >>> import numpy as np
    >>> from zarr_indexing import LazyArray
    >>> view = LazyArray(np.arange(8))[1::2]
    >>> EagerArrayAdapter(view)[1:3]
    array([3, 5])
    """

    __slots__ = ("_view",)

    def __init__(self, view: LazyArray) -> None:
        self._view = view

    @property
    def shape(self) -> tuple[int, ...]:
        """Shape of the selected view."""
        return self._view.shape

    @property
    def ndim(self) -> int:
        """Number of dimensions of the selected view."""
        return self._view.ndim

    @property
    def dtype(self) -> Any:
        """Data type reported by the source."""
        return self._view.dtype

    def __getitem__(self, selection: Any) -> Any:
        """Read a basic selection into fresh memory."""
        return self._view[selection].result()

    def __array__(self, dtype: Any = None, copy: bool | None = None) -> Any:
        """Materialize using the view's NumPy conversion contract."""
        return self._view.__array__(dtype=dtype, copy=copy)

    def __dask_tokenize__(self) -> tuple[Any, ...]:
        """Distinguish eager tasks while preserving the source identity contract."""
        return (type(self).__qualname__, self._view.__dask_tokenize__())

__slots__ class-attribute instance-attribute

__slots__ = ('_view',)

dtype property

dtype: Any

Data type reported by the source.

ndim property

ndim: int

Number of dimensions of the selected view.

shape property

shape: tuple[int, ...]

Shape of the selected view.

__array__

__array__(
    dtype: Any = None, copy: bool | None = None
) -> Any

Materialize using the view's NumPy conversion contract.

Source code in src/zarr_indexing/eager.py
def __array__(self, dtype: Any = None, copy: bool | None = None) -> Any:
    """Materialize using the view's NumPy conversion contract."""
    return self._view.__array__(dtype=dtype, copy=copy)

__dask_tokenize__

__dask_tokenize__() -> tuple[Any, ...]

Distinguish eager tasks while preserving the source identity contract.

Source code in src/zarr_indexing/eager.py
def __dask_tokenize__(self) -> tuple[Any, ...]:
    """Distinguish eager tasks while preserving the source identity contract."""
    return (type(self).__qualname__, self._view.__dask_tokenize__())

__getitem__

__getitem__(selection: Any) -> Any

Read a basic selection into fresh memory.

Source code in src/zarr_indexing/eager.py
def __getitem__(self, selection: Any) -> Any:
    """Read a basic selection into fresh memory."""
    return self._view[selection].result()

__init__

__init__(view: LazyArray) -> None
Source code in src/zarr_indexing/eager.py
def __init__(self, view: LazyArray) -> None:
    self._view = view