Appearance
AnnData and H5CP API
Use this reference for file/slot arguments, storage policy and materialization. The AnnData guide provides a complete annotated matrix round trip with checked values.
python
from pycellpack import read_h5, write_h5ad, write_h5cp, open_matrix
from pycellpack.io import h5ad_to_cpk
from pycellpack.anndata import AnnDataThese calls load the optional anndata integration. The package pins its AnnData version and requires Python >=3.12 for that extra. Importing the core package does not eagerly import AnnData, h5py, SciPy, pandas or Zarr.
Select the object you need
- Read matrices together with annotationsread_h5 → AnnDataChoose eager decoding or preserve embedded streams as Stores.
- Read a single embedded matrixopen_matrix → StoreKeep its file region lazy without loading the full annotated dataset.
- Publish an annotated datasetwrite_h5cp / write_h5adSelect embedded or decoded matrix storage explicitly.
Reading and backed behavior
read_h5(filename, backed=None, *, lazy=False, **kwargs) selects behavior from file contents, not the filename suffix:
| Contents / options | Result and allocation |
|---|---|
| Embedded streams, defaults | Adapted AnnData; streams decoded to ordinary arrays |
Embedded streams, lazy=True or backed="r" | Adapted AnnData; embedded matrices remain Stores; other elements read into memory |
No embedded streams, backed=None | AnnData's normal eager read, adapted to this subclass |
| No embedded streams, backed read | AnnData's backed object returned unchanged |
For files containing streams, backed accepts only None, False or "r"; additional AnnData read kwargs are rejected. In particular, backed="r+" is not supported. For ordinary H5AD, backed and kwargs are forwarded to AnnData; lazy=True does not add CellPack encoding or make its arrays Stores.
Embedded Stores keep independent file descriptors after the metadata handle closes. They provide immutable matrix access, rather than writable HDF5-backed arrays. Preserve the source file while derived Stores and datasets use it.
Writing and publication
The module functions take filepath first, adata second:
| Function | Result | Storage behavior |
|---|---|---|
write_h5cp(filepath, adata, ...) | Resolved absolute Path | Policy selected by encode below |
write_h5ad(filepath, adata, ...) | None | Plain H5AD; existing Stores decoded in row batches |
adata.write_h5cp(filename=None, ...) | None | Instance convenience using the same H5CP policy |
adata.write_h5ad(filename=None, ...) | None | Instance convenience for plain H5AD |
A missing instance filename uses the AnnData backing filename; an unbacked object must receive an explicit filename.
write_h5cp policy | Existing Store slots | Ordinary supported numeric matrices |
|---|---|---|
encode=None (default) | Embed compressed streams | Keep normal AnnData encoding |
encode=True | Embed compressed streams | Encode matrix slots as CellPack streams |
encode=False | Decode to ordinary datasets | Keep normal AnnData encoding |
Supported numeric NumPy/SciPy matrices have at least two dimensions and a positive product of trailing dimensions, using supported integer or float32/64 dtypes. Dense higher-rank matrices retain their logical shape, with flattened columns in the embedded stream. Annotation values use AnnData encodings.
Encoding parameters
| Parameter | Default | Contract |
|---|---|---|
chunk_rows | 4096 | New-encoding rows per chunk, integer in [1, 2**32) |
page_size | 4096 | New-encoding page bytes; power of two in [512, 65536] |
workers | None | Encoding/decoding threads; storage worker resolution applies |
convert_strings_to_categoricals | True | Convert copied annotation frames on the stream-writing path |
dataset_kwargs, **kwargs | Empty | Options for ordinary AnnData datasets, not embedded stream compression |
as_dense | () | AnnData dense-export selection; nonempty values rejected on the H5CP stream-writing path |
Whole identity Stores whose source paths are still current are copied byte for byte. Slices, column maps and dtype-converting views are decoded and re-encoded. chunk_rows and page_size affect those new encodings, not copied streams. Embedded payloads are contiguous, unfiltered uint8 HDF5 datasets. Their largest page size determines HDF5 region alignment; no HDF5 filter wraps those payloads.
When writing streams or decoding existing Stores, the implementation stages in a sibling temporary directory and replaces the destination only on success. It can therefore write an annotated object over the file its Stores read from. When no Stores exist and encode is None or False, write_h5cp delegates directly to AnnData; write_h5ad also delegates directly when no Stores exist. These branches follow upstream publication behavior. The staging paths do not promise power-loss durability.
Matrix slots and export
Both functions accept AnnData slot paths: X, layers/<name>, obsm/<name>, obsp/<name>, varm/<name>, varp/<name>, raw/X and raw/varm/<name>. Leading/trailing slashes are stripped; individual keys must not contain path separators, NUL, . or ... These paths identify matrix groups, not their internal data datasets.
| Function | Input | Result / important constraint |
|---|---|---|
open_matrix(filename, key="X") | Supported embedded stream group | Lazy Store with logical shape; no annotations loaded |
h5ad_to_cpk(filename, path, key="X", ...) | Embedded stream, 2-D dense dataset or CSR group | File-backed Store over exported .cpk; annotations stay in the source |
h5ad_to_cpk uses batch_rows=65536 for ordinary matrix inputs; it must be positive. format=None, workers=None, chunk_rows=4096, page_size=4096, options=None and atomic=False configure their row-batched encoding through io.write_cpk. Ordinary CSC and unsupported element encodings are not converted by this row-export helper.
For an already embedded stream, export copies encoded bytes directly. That branch does not apply requested format/encoding options or atomic=True; use a separate destination path. open_matrix also requires an embedded stream: it does not lazily wrap an ordinary H5AD dense/CSR matrix. Its File owns a bounded region after the HDF5 metadata handle has closed.
AnnData materialization and datasets
| Interface | Store behavior |
|---|---|
Constructor / X, layers, obsm/varm, obsp/varp, raw | Accept Stores alongside ordinary AnnData arrays; aligned axes are validated |
| Slicing | Compose Store selections lazily |
copy() | Copy annotations while sharing immutable Stores |
is_lazy | True if any matrix slot, including raw, contains a Store |
to_memory(copy=False) | Return a new AnnData with every Store and backed array materialized; Store decoding allocates regardless of copy |
transpose() / T | Materialize remaining Stores before transposing |
to_df(layer=None) | Materialize the selected matrix as a dense data frame |
chunked_X(chunk_size=None) | Yield decoded chunks with start/end row positions |
chunk_X(select=1000, replace=True) | Materialize only selected/sampled X rows as dense output |
ScDataset.from_anndata(adata, ids=..., layer=None) selects X, or the named layer. It requires an existing 2-D file-backed Store in its stored dtype and one explicit standard ID per visible column. It does not encode ordinary arrays or match gene names. See dataset contracts.
Errors and boundaries
Missing optional packages raise ModuleNotFoundError with installation guidance. Invalid shapes, layout declarations, slot paths and unsupported embedded stream layouts raise ValueError. Missing HDF5 slots raise KeyError; unsupported read modes, kwargs, dense-writing requests or row-export encodings raise NotImplementedError. Filesystem/HDF5 and native I/O errors propagate.
On the stream-writing path, metadata mappings are checked for unsafe keys and recursive mappings before publication. These file-structure checks do not make trusted CellPack record bytes a validation boundary. Keep source streams immutable during reads and exports; after replacing a source path, reopen the new stream before constructing a new training source.
Declarations
Complete declarations and members
read_h5
python
read_h5(filename: PathLike[str] | str, backed=None, *, lazy: bool=False, **kwargs)Read an H5CP or H5AD file as a pycellpack.anndata.AnnData.
Files without CellPack streams (plain H5AD) are read by anndata.io.read_h5ad with backed and kwargs and converted (backed objects are returned unchanged). Otherwise matrices stored as CellPack streams are decoded, or with lazy=True (or backed="r") returned as Stores over the embedded streams; other elements are read into memory. Stores keep their own file descriptors, so the file is closed on return.
write_h5ad
python
write_h5ad(filepath: PathLike[str] | str, adata: ad.AnnData, *, as_dense: Sequence[str]=(), convert_strings_to_categoricals: bool=True, dataset_kwargs: Mapping[str, Any]=MappingProxyType({}), **kwargs) -> NoneWrite a plain H5AD exactly as anndata.io.write_h5ad does.
Arguments and behavior are AnnData's. Stores are written as AnnData writes the matrices they decode to (SciPy's default index dtype), one row batch at a time, and the file is staged next to the target so a Store may be written over the file it reads from.
write_h5cp
python
write_h5cp(filepath: PathLike[str] | str, adata: ad.AnnData, *, as_dense: Sequence[str]=(), convert_strings_to_categoricals: bool=True, dataset_kwargs: Mapping[str, Any]=MappingProxyType({}), encode: bool | None=None, chunk_rows: int=4096, page_size: int=4096, workers: int | None=None, **kwargs) -> PathWrite H5CP, embedding CellPack streams; argument order follows AnnData.
encode selects how matrices in X, layers, obs[mp], var[mp] and raw are stored:
None: Stores are embedded as CellPack streams and other values use AnnData's encodings. Objects without Stores are written byanndata.io.write_h5adunchanged.True: numeric NumPy/SciPy matrices are additionally encoded.False: Stores are decoded as inwrite_h5ad, producing a plain H5AD.
Whole Stores are copied byte for byte; sliced Stores are re-encoded with chunk_rows and page_size, as are encoded in-memory matrices, using workers threads (default: one per CPU, at most 32). Dense/CSR form, dtype and higher-rank shapes are retained. kwargs and dataset_kwargs (e.g. compression) apply to AnnData's datasets only. The file is staged next to the target and replaces it only on success, so a Store may be written over the file it reads from.
h5ad_to_cpk
python
h5ad_to_cpk(filename: PathLike[str] | str, path: PathLike[str] | str, key: str='X', *, batch_rows: int=65536, format: str | None=None, workers: int | None=None, chunk_rows: int=4096, page_size: int=4096, options: EncodeOptions | None=None, atomic: bool=False) -> StoreExport one matrix slot of an H5AD or H5CP file to a .cpk file.
key names an AnnData matrix slot ("X", "layers/counts", "raw/X", ...). A dense dataset or a CSR group is read and encoded batch_rows rows at a time, so the matrix need not fit in memory; the remaining arguments are ~pycellpack.io.write_cpk's. An embedded CellPack stream is copied byte for byte. Returns a Store over the new file; the file keeps no annotations, so cell and gene names stay in the source.
open_matrix
python
open_matrix(filename: PathLike[str] | str, key: str='X') -> StoreOpen one embedded matrix lazily, for reads or as a loader source.
Higher-rank arrays keep their logical shape (their stream stores flattened columns). The Store keeps its own file descriptor after the HDF5 metadata handle closes.
AnnData
AnnData whose matrix slots can hold lazy CellPack Stores.
X, layers, obsm, varm, obsp and varp accept ~pycellpack.cellpack.Store (or an opened ~pycellpack.cellpack.File) next to the usual AnnData arrays; raw takes the Store form. Slicing the object composes the stores' selections without reading; to_memory decodes them. H5CP files written from Stores embed the streams, and lazy reads return Stores over the embedded streams.
Fields and defaults
python
layers: Any = _am.AlignedMappingProperty(Layers)
obsm: Any = _am.AlignedMappingProperty(AxisArrays, 0)
varm: Any = _am.AlignedMappingProperty(AxisArrays, 1)
obsp: Any = _am.AlignedMappingProperty(PairwiseArrays, 0)
varp: Any = _am.AlignedMappingProperty(PairwiseArrays, 1)
T = property(transpose)AnnData.X
X — property.
Data matrix of shape n_obs × n_vars; may be a lazy Store.
AnnData.raw
raw: Raw | None — property.
Raw data, or None; a base-class Raw is retagged on access.
AnnData.to_memory
python
AnnData.to_memory(*, copy: bool=False) -> AnnDataReturn an AnnData with every Store (and backed array) decoded.
AnnData.is_lazy
is_lazy: bool — property.
True when any matrix slot (including raw) still holds a Store.
AnnData.transpose
python
AnnData.transpose() -> AnnDataTranspose, decoding Stores first since streams are cell-major.
AnnData.to_df
python
AnnData.to_df(layer: str | None=None) -> pd.DataFrameData frame of X (or a layer); a Store is decoded first.
AnnData.chunked_X
python
AnnData.chunked_X(chunk_size: int | None=None)Yield (chunk, start, end) for X, decoding Store chunks.
AnnData.chunk_X
python
AnnData.chunk_X(select=1000, *, replace: bool=True)Dense sample of X; a Store contributes only the sampled cells.
AnnData.write_h5cp
python
AnnData.write_h5cp(filename: PathLike[str] | str | None=None, **kwargs: Any) -> NoneWrite H5CP, embedding Stores as CellPack streams.
Arguments follow pycellpack.io.write_h5cp; filename defaults to the backing file.
AnnData.write_h5ad
python
AnnData.write_h5ad(filename: PathLike[str] | str | None=None, **kwargs: Any) -> NoneWrite a plain H5AD as anndata.AnnData.write_h5ad does, decoding Stores (arguments follow pycellpack.io.write_h5ad).