Skip to content

AnnData

Sanity and Bonsai straight off an AnnData layer, as bs.anndata.bonsai and bs.anndata.sanity. Needs the anndata extra: uv add 'bonsai-rs[anndata]'. Nothing is written back to adata; both return a frozen record with the cell and gene names attached.

Sanity and Bonsai on AnnData, cells x genes as scanpy keeps them.

The counts are read straight out of the layer's CSR buffers: Rust transposes the candidate genes once into the gene-major matrix Sanity wants, and Sanity only stores the genes that pass Bonsai's ingest filters. adata is never written to; the result is a frozen record. Scanpy itself is never imported.

>>> res = bs.anndata.bonsai(adata)
>>> res.nodes.head()
>>> res.to_newick()

Imported on first access of bonsai_rs.anndata, so anndata stays an optional dependency.

AnnDataBonsai dataclass

AnnDataBonsai(
    result: BonsaiResult,
    cell_names: Index,
    genes_used: Index,
    genes_dropped: Index,
    nodes: DataFrame,
)

A Bonsai tree over the cells of an AnnData.

Attributes:

Name Type Description
result BonsaiResult

The reconstruction. Its features and dropped index the genes of the layer; dropped is every candidate gene left out, for whatever reason.

cell_names Index

obs_names, in leaf order: leaf i is cell i.

genes_used Index

Names of the genes the tree was built on, in var order.

genes_dropped Index

The candidate genes left out: no counts, ill conditioned Sanity posteriors, or a signal-to-noise below min_signal_to_noise.

nodes DataFrame

One row per node, leaves first: node, parent (<NA> for the root), branch, is_leaf, cell_id (<NA> for the inferred ancestors) and n_cells (1 for a leaf, <NA> for an ancestor).

to_newick

to_newick() -> str

The tree as Newick, leaves labelled by cell name.

AnnDataSanity dataclass

AnnDataSanity(
    result: SanityResult, cell_names: Index, genes: Index
)

Sanity posteriors over the genes Bonsai would keep.

Attributes:

Name Type Description
result SanityResult

The posteriors, cells x kept genes.

cell_names Index

obs_names, the rows of every matrix in result.

genes Index

Names of the kept genes, the columns, in var order.

bonsai

bonsai(
    adata: AnnData,
    *,
    layer: str | None = "counts",
    genes: Sequence[str]
    | ndarray
    | Index
    | Series
    | None = None,
    cell_totals: ndarray | Series | None = None,
    variance_rule: VarianceRule = "marginalise",
    fixed_variance: float | None = None,
    dtype: type[float32] | type[float64] = float32,
    gpu: bool = False,
    start: Start = "linkage",
    search: Search = "approximate",
    min_signal_to_noise: float | None = None,
    max_amplification: float | None = None,
    reroot: bool = False,
    verbose: int = 0,
) -> AnnDataBonsai

Raw counts in an AnnData to a Bonsai tree over its cells.

bonsai_from_counts on a layer: Sanity, the S5 conversion and the tree search, all in Rust. Sanity stores only the genes that would survive Bonsai's ingest (the S5 amplification cap and the signal-to-noise floor), so no HVG selection is needed and the dense posteriors never exceed kept genes x cells. Same tree and loglikelihood as bonsai_from_counts on the same counts. adata is not modified.

Parameters:

Name Type Description Default
adata AnnData

Annotated data matrix with raw UMI counts.

required
layer str | None

Where the counts are. None reads X, "raw" reads adata.raw.X unless a layer of that name exists.

'counts'
genes Sequence[str] | ndarray | Index | Series | None

Candidate genes: names, a boolean mask over var, or a boolean Series matched to var_names by its index, such as adata.var["highly_variable"]. None considers every gene; only those passing the filters are used.

None
cell_totals ndarray | Series | None

Total UMIs per cell. A Series is matched to obs_names by its index. None sums each cell over every gene of the layer, also when genes is a subset. Pass this if the layer itself is a gene subset.

None
variance_rule VarianceRule

As sanity.

'marginalise'
fixed_variance float | None

As sanity.

None
dtype type[float32] | type[float64]

As sanity.

float32
gpu bool

As sanity. Only Sanity runs on the GPU. The keep test then sees float32 posteriors, so a gene sitting on a threshold can go the other way from the CPU run.

False
start Start

As bonsai.

'linkage'
search Search

As bonsai.

'approximate'
min_signal_to_noise float | None

As bonsai.

None
max_amplification float | None

As from_sanity.

None
reroot bool

As bonsai.

False
verbose int

As bonsai_from_counts.

0

Returns:

Type Description
AnnDataBonsai

The tree with cell and gene names attached, and a node table.

Raises:

Type Description
ValueError

On a missing layer, non-integral counts, or an unknown gene.

BonsaiError

If no gene survives, or gpu=True and no GPU can be reached.

Examples:

>>> res = bs.anndata.bonsai(adata, layer="counts")
>>> res.nodes.query("is_leaf").head()

sanity

sanity(
    adata: AnnData,
    *,
    layer: str | None = "counts",
    genes: Sequence[str]
    | ndarray
    | Index
    | Series
    | None = None,
    cell_totals: ndarray | Series | None = None,
    variance_rule: VarianceRule = "marginalise",
    fixed_variance: float | None = None,
    dtype: type[float32] | type[float64] = float32,
    gpu: bool = False,
    min_signal_to_noise: float | None = None,
    max_amplification: float | None = None,
    verbose: int = 0,
) -> AnnDataSanity

Sanity posteriors from an AnnData, over the genes Bonsai would keep.

The first half of bonsai: genes failing the S5 amplification cap or the signal-to-noise floor are never stored. For every gene, run bonsai_rs.sanity on the matrix instead. adata is not modified.

Parameters:

Name Type Description Default
adata AnnData

As bonsai.

required
layer str | None

As bonsai.

'counts'
genes Sequence[str] | ndarray | Index | Series | None

As bonsai.

None
cell_totals ndarray | Series | None

As bonsai.

None
variance_rule VarianceRule

As bonsai_rs.sanity.

'marginalise'
fixed_variance float | None

As bonsai_rs.sanity.

None
dtype type[float32] | type[float64]

As bonsai_rs.sanity.

float32
gpu bool

As bonsai.

False
min_signal_to_noise float | None

As bonsai_rs.bonsai.

None
max_amplification float | None

As from_sanity.

None
verbose int

As bonsai_rs.sanity.

0

Returns:

Type Description
AnnDataSanity

The posteriors, cells x kept genes, with their names.

Raises:

Type Description
ValueError

On a missing layer, non-integral counts, or an unknown gene.

BonsaiError

If no gene survives, or gpu=True and no GPU can be reached.