Cell trees¶
Bonsai trees over cells or meta cells.
Bonsai builds a tree with every cell a leaf and inferred ancestors inside, from
the raw counts after Sanity, and lays it out in 2D. Use it where a hierarchy
over all cells says more than a process with a start (that is bv.traj). Call
bonsai first, change the drawing with relayout_bonsai, and put the leaf
coordinates in sc.cache.embeddings with set_bonsai_embedding to plot them
like a UMAP.
What lands where. bonsai and relayout_bonsai return a BonsaiTree and
leave the experiment alone. set_bonsai_embedding is the only writer: it
stores the (n_leaves, 2) leaf positions in sc.cache.embeddings[name]
(default "bonsai") and stamps it. bonsai takes a SingleCells or a
MetaCells, not a SingleCellsSubset.
tree = bv.trees.bonsai(sc)
tree = bv.trees.relayout_bonsai(tree, layout="dendrogram")
bv.trees.set_bonsai_embedding(sc, tree)
On meta cells every meta cell is a leaf, with n_cells counting the cells
behind it. That is the way to large data. The generators want the kNN from
bv.graph.find_neighbours on sc first:
Gotchas. No HVGs, PCA or kNN are needed; the signal-to-noise filter picks
the genes, or pass hvg= to restrict the candidates. tree.nodes is 0-based with a
null parent for the root, leaves first in the object's cell order. relayout_bonsai
renumbers the inferred ancestors, the leaves keep their numbers.
set_bonsai_embedding refuses a tree built over other cells than the object
now keeps, so rerun bonsai after changing the cell filter.
Worked through in Cell trees with Bonsai.
Building a tree¶
Bonsai trees over cells or meta cells.
Bonsai builds a tree with every cell a leaf and inferred ancestors inside, from
the raw counts after Sanity, and lays it out in 2D. Use it where a hierarchy
over all cells says more than a process with a start (that is bv.traj). Call
bonsai first, change the drawing with relayout_bonsai, and put the leaf
coordinates in sc.cache.embeddings with set_bonsai_embedding to plot them
like a UMAP.
What lands where. bonsai and relayout_bonsai return a BonsaiTree and
leave the experiment alone. set_bonsai_embedding is the only writer: it
stores the (n_leaves, 2) leaf positions in sc.cache.embeddings[name]
(default "bonsai") and stamps it. bonsai takes a SingleCells or a
MetaCells, not a SingleCellsSubset.
tree = bv.trees.bonsai(sc)
tree = bv.trees.relayout_bonsai(tree, layout="dendrogram")
bv.trees.set_bonsai_embedding(sc, tree)
On meta cells every meta cell is a leaf, with n_cells counting the cells
behind it. That is the way to large data. The generators want the kNN from
bv.graph.find_neighbours on sc first:
Gotchas. No HVGs, PCA or kNN are needed; the signal-to-noise filter picks
the genes, or pass hvg= to restrict the candidates. tree.nodes is 0-based with a
null parent for the root, leaves first in the object's cell order. relayout_bonsai
renumbers the inferred ancestors, the leaves keep their numbers.
set_bonsai_embedding refuses a tree built over other cells than the object
now keeps, so rerun bonsai after changing the cell filter.
BonsaiTree
dataclass
¶
BonsaiTree(
nodes: DataFrame,
loglik: float,
steps: DataFrame,
timings: DataFrame,
genes_used: tuple[str, ...],
genes_dropped: tuple[str, ...],
cell_idx: ndarray,
layout: str,
hyperbolic: bool,
params: BonsaiParams,
)
A Bonsai tree with its layout.
Attributes:
| Name | Type | Description |
|---|---|---|
nodes |
DataFrame
|
|
loglik |
float
|
Loglikelihood of the final tree. |
steps |
DataFrame
|
|
timings |
DataFrame
|
Wall time in |
genes_used |
tuple[str, ...]
|
Ids of the genes the tree was built on. |
genes_dropped |
tuple[str, ...]
|
The candidate genes left out: no counts, ill
conditioned Sanity posteriors, or a signal-to-noise below
|
cell_idx |
ndarray
|
The 0-based cells (store indices) or meta cells the tree was built over, in leaf order. |
layout |
str
|
The current layout. |
hyperbolic |
bool
|
Whether the layout is on the hyperbolic disk. |
params |
BonsaiParams
|
The parameters of the run. |
bonsai
¶
bonsai(
sc: SingleCells | MetaCells,
*,
hvg: ndarray | Sequence[int] | None = None,
bonsai_params: BonsaiParams | None = None,
verbose: Verbose = True,
) -> BonsaiTree
A Bonsai tree over the cells.
Every cell is a leaf and the internal nodes are inferred ancestral
states, with branch lengths that carry the amount of change between
them. The raw counts go through Sanity first for posterior log fold
changes with error bars, and only genes with enough signal over their own
noise (min_signal_to_noise) are kept for the tree; no HVG selection
needed.
On MetaCells every meta cell is a leaf. Their summed raw counts are
Poisson like a cell's, so Sanity treats them alike, and a bigger meta
cell gets tighter error bars. That is the way to large data; what goes
is any structure inside a meta cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sc
|
SingleCells | MetaCells
|
The experiment or meta cells. |
required |
hvg
|
ndarray | Sequence[int] | None
|
0-based candidate genes, e.g. |
None
|
bonsai_params
|
BonsaiParams | None
|
The parameters. |
None
|
verbose
|
Verbose
|
|
True
|
Returns:
| Type | Description |
|---|---|
BonsaiTree
|
The |
Raises:
| Type | Description |
|---|---|
ValueError
|
On a gene index out of range, or if no gene survives the signal-to-noise filter. |
References
de Groot, et al., Nat. Biotechnol., 2026; Breda, et al., Nat. Biotechnol., 2021.
Examples:
Layout and embedding¶
Bonsai trees over cells or meta cells.
Bonsai builds a tree with every cell a leaf and inferred ancestors inside, from
the raw counts after Sanity, and lays it out in 2D. Use it where a hierarchy
over all cells says more than a process with a start (that is bv.traj). Call
bonsai first, change the drawing with relayout_bonsai, and put the leaf
coordinates in sc.cache.embeddings with set_bonsai_embedding to plot them
like a UMAP.
What lands where. bonsai and relayout_bonsai return a BonsaiTree and
leave the experiment alone. set_bonsai_embedding is the only writer: it
stores the (n_leaves, 2) leaf positions in sc.cache.embeddings[name]
(default "bonsai") and stamps it. bonsai takes a SingleCells or a
MetaCells, not a SingleCellsSubset.
tree = bv.trees.bonsai(sc)
tree = bv.trees.relayout_bonsai(tree, layout="dendrogram")
bv.trees.set_bonsai_embedding(sc, tree)
On meta cells every meta cell is a leaf, with n_cells counting the cells
behind it. That is the way to large data. The generators want the kNN from
bv.graph.find_neighbours on sc first:
Gotchas. No HVGs, PCA or kNN are needed; the signal-to-noise filter picks
the genes, or pass hvg= to restrict the candidates. tree.nodes is 0-based with a
null parent for the root, leaves first in the object's cell order. relayout_bonsai
renumbers the inferred ancestors, the leaves keep their numbers.
set_bonsai_embedding refuses a tree built over other cells than the object
now keeps, so rerun bonsai after changing the cell filter.
relayout_bonsai
¶
relayout_bonsai(
x: BonsaiTree,
*,
layout: BonsaiLayout = "equal_angle",
hyperbolic: bool = False,
) -> BonsaiTree
The tree laid out again without searching.
The tree is renumbered on the Rust side, so the inferred ancestors can come back with other node numbers; the leaves, and so the cells, keep theirs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
BonsaiTree
|
The tree. |
required |
layout
|
BonsaiLayout
|
|
'equal_angle'
|
hyperbolic
|
bool
|
Project the layout onto the hyperbolic disk. |
False
|
Returns:
| Type | Description |
|---|---|
BonsaiTree
|
A new |
Examples:
set_bonsai_embedding
¶
set_bonsai_embedding(
sc: SingleCells | MetaCells,
tree: BonsaiTree,
*,
name: str = "bonsai",
) -> None
Store the leaf coordinates as an embedding.
Writes the (n_leaves, 2) positions to sc.cache.embeddings[name] and
stamps it, so the embedding helpers can use it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sc
|
SingleCells | MetaCells
|
The experiment or meta cells the tree was built over. |
required |
tree
|
BonsaiTree
|
The |
required |
name
|
str
|
The embedding name. |
'bonsai'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tree was not built over the object's current cells. |
Examples: