Skip to content

Commit

Permalink
(chore): add docstring example for read_elem_as_dask
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-gold committed Oct 21, 2024
1 parent 3260222 commit 17ff0f5
Showing 1 changed file with 50 additions and 0 deletions.
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>
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

0 comments on commit 17ff0f5

Please sign in to comment.