anndata.AnnData.write

AnnData.write(filename=None, compression=None, compression_opts=None, force_dense=None, as_dense=())

Write .h5ad-formatted hdf5 file.

Note

Setting compression to 'gzip' can save disk space but will slow down writing and subsequent reading. Prior to v0.6.16, this was the default for parameter compression.

Generally, if you have sparse data that are stored as a dense matrix, you can dramatically improve performance and reduce disk space by converting to a csr_matrix:

from scipy.sparse import csr_matrix
adata.X = csr_matrix(adata.X)
Parameters
filename : PathLike | NoneOptional[PathLike] (default: None)

Filename of data file. Defaults to backing file.

compression : {‘gzip’, ‘lzf’} | NoneOptional[Literal[‘gzip’, ‘lzf’]] (default: None)

See the h5py Filter pipeline.

compression_opts : int | Any | NoneUnion[int, Any, None] (default: None)

See the h5py Filter pipeline.

as_dense : Sequence[str] (default: ())

Sparse arrays in AnnData object to write as dense. Currently only supports X and raw/X.

force_dense : bool | NoneOptional[bool] (default: None)

Write sparse data as a dense matrix. Defaults to True if object is backed, otherwise to False.