Skip to content

Array wrap

DaskArrayWrapperConfig #

Bases: TypedDict

A model of the attributes of DaskArrayWrapper.

ZarrArrayWrapper dataclass #

ZarrArrayWrapper()

Bases: BaseArrayWrapper

An array wrapper that passes zarr.Array instances through unchanged.

DaskArrayWrapper dataclass #

DaskArrayWrapper(chunks='auto', meta=None, inline_array=True, naming='array_url')

Bases: BaseArrayWrapper

An array wrapper that wraps a zarr.Array in a dask array using dask.array.from_array. The attributes of this class are a subset of the keyword arguments to dask.array.from_array; specifically, those keyword arguments that make sense when the input to from_array is a zarr.Array.

Attributes:

Name Type Description
chunks str | int | tuple[int, ...] tuple[tuple[int, ...], ...] = "auto"

The chunks for the Dask array. See dask.array.from_array for details.

meta Any = `None`

The array type of each chunk of the Dask array. See dask.array.from_array for details.

inline_array bool = True

Whether slices of this array should be inlined into the Dask task graph. See dask.array.from_array for details.

naming 'auto' | 'array_url'

The naming scheme for the Dask array. If "auto", the default, then Dask will name the array with a non-deterministic hash. If "array_url", then the array will be named according to its URL.

wrap #

wrap(data)

Wrap the input in a dask array.

Source code in src/xarray_ome_ngff/array_wrap.py
def wrap(self, data: zarr.Array) -> DaskArray:  # type: ignore
    """
    Wrap the input in a dask array.
    """
    import dask.array as da  # noqa

    if self.naming == "auto":
        name = None
    elif self.naming == "array_url":
        name = f"{get_store_url(data.store)}/{data.path}"

    return da.from_array(
        data,
        chunks=self.chunks,
        inline_array=self.inline_array,
        meta=self.meta,
        name=name,
    )

resolve_wrapper #

resolve_wrapper(spec)

Convert an ArrayWrapperSpec into the corresponding BaseArrayWrapper subclass.

Source code in src/xarray_ome_ngff/array_wrap.py
def resolve_wrapper(spec: ArrayWrapperSpec) -> ZarrArrayWrapper | DaskArrayWrapper:
    """
    Convert an `ArrayWrapperSpec` into the corresponding `BaseArrayWrapper` subclass.
    """
    if spec["name"] == "dask_array":
        spec = cast(DaskArrayWrapperSpec, spec)  # type: ignore
        return DaskArrayWrapper(**spec["config"])
    elif spec["name"] == "zarr_array":
        spec = cast(ZarrArrayWrapperSpec, spec)  # type: ignore
        return ZarrArrayWrapper(**spec["config"])
    else:
        raise ValueError(f"Spec {spec} is not recognized.")

parse_wrapper #

parse_wrapper(data)

Parse the input into a BaseArrayWrapper subclass.

If the input is already BaseArrayWrapper, it is returned as-is. Otherwise, the input is presumed to be ArrayWrapperSpec and is passed to resolve_wrapper.

Source code in src/xarray_ome_ngff/array_wrap.py
def parse_wrapper(
    data: ArrayWrapperSpec | DaskArrayWrapper | ZarrArrayWrapper,
) -> DaskArrayWrapper | ZarrArrayWrapper:
    """
    Parse the input into a `BaseArrayWrapper` subclass.

    If the input is already `BaseArrayWrapper`, it is returned as-is.
    Otherwise, the input is presumed to be `ArrayWrapperSpec` and is passed to `resolve_wrapper`.
    """
    if isinstance(data, (ZarrArrayWrapper, DaskArrayWrapper)):
        return data
    return resolve_wrapper(data)