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

(chore): add docstring example for read_elem_as_dask #1722

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions src/anndata/_io/specs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,60 @@ def read_elem_as_dask(
chunks, optional
length `n`, the same `n` as the size of the underlying array.
Note that the minor axis dimension must match the shape for sparse.
Defaults to `(1000, adata.shape[1])`.

Returns
-------
DaskArray

Examples
--------

Setting up our example:

>>> from scanpy.datasets import pbmc3k
>>> import tempfile
>>> import anndata as ad
>>> import zarr

>>> tmp_path = tempfile.gettempdir()
>>> zarr_path = tmp_path + "/adata.zarr"

>>> adata = pbmc3k()
>>> adata.layers["dense"] = adata.X.toarray()
>>> adata.write_zarr(zarr_path)

Reading a sparse matrix from a zarr store lazily, with custom chunk size and default:

>>> g = zarr.open(zarr_path)
>>> adata.X = ad.experimental.read_elem_as_dask(g["X"])
>>> adata.X
dask.array<make_dask_chunk, shape=(2700, 32738), dtype=float32, chunksize=(1000, 32738), chunktype=scipy.csr_matrix>
>>> adata.X = ad.experimental.read_elem_as_dask(
... g["X"], chunks=(500, adata.shape[1])
... )
>>> adata.X
dask.array<make_dask_chunk, shape=(2700, 32738), dtype=float32, chunksize=(500, 32738), chunktype=scipy.csr_matrix>

Reading a dense matrix from a zarr store lazily:

>>> adata.layers["dense"] = ad.experimental.read_elem_as_dask(g["layers/dense"])
>>> adata.layers["dense"]
dask.array<from-zarr, shape=(2700, 32738), dtype=float32, chunksize=(169, 2047), chunktype=numpy.ndarray>
ilan-gold marked this conversation as resolved.
Show resolved Hide resolved

Making a new anndata object from on-disk, with custom chunks:

>>> adata = ad.AnnData(
... obs=ad.io.read_elem(g["obs"]),
... var=ad.io.read_elem(g["var"]),
... uns=ad.io.read_elem(g["uns"]),
... obsm=ad.io.read_elem(g["obsm"]),
... varm=ad.io.read_elem(g["varm"]),
... )
>>> adata.X = ad.experimental.read_elem_as_dask(
... g["X"], chunks=(500, adata.shape[1])
... )
>>> adata.layers["dense"] = ad.experimental.read_elem_as_dask(g["layers/dense"])
"""
return DaskReader(_LAZY_REGISTRY).read_elem(elem, chunks=chunks)

Expand Down
Loading