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 |
cell_names |
Index
|
|
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
|
nodes |
DataFrame
|
One row per node, leaves first: |
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
|
|
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. |
'counts'
|
genes
|
Sequence[str] | ndarray | Index | Series | None
|
Candidate genes: names, a boolean mask over var, or a boolean
Series matched to |
None
|
cell_totals
|
ndarray | Series | None
|
Total UMIs per cell. A Series is matched to
|
None
|
variance_rule
|
VarianceRule
|
As |
'marginalise'
|
fixed_variance
|
float | None
|
As |
None
|
dtype
|
type[float32] | type[float64]
|
As |
float32
|
gpu
|
bool
|
As |
False
|
start
|
Start
|
As |
'linkage'
|
search
|
Search
|
As |
'approximate'
|
min_signal_to_noise
|
float | None
|
As |
None
|
max_amplification
|
float | None
|
As |
None
|
reroot
|
bool
|
As |
False
|
verbose
|
int
|
As |
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 |
Examples:
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 |
required |
layer
|
str | None
|
As |
'counts'
|
genes
|
Sequence[str] | ndarray | Index | Series | None
|
As |
None
|
cell_totals
|
ndarray | Series | None
|
As |
None
|
variance_rule
|
VarianceRule
|
As |
'marginalise'
|
fixed_variance
|
float | None
|
As |
None
|
dtype
|
type[float32] | type[float64]
|
As |
float32
|
gpu
|
bool
|
As |
False
|
min_signal_to_noise
|
float | None
|
As |
None
|
max_amplification
|
float | None
|
As |
None
|
verbose
|
int
|
As |
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 |