Reconstruction¶
Counts to posteriors, posteriors to likelihood parameters, and the search.
bonsai_from_counts is the first three in one call.
core
¶
Reconstruction: Sanity, the S5 conversion, Bonsai and backbone mode.
bonsai_from_counts
¶
bonsai_from_counts(
counts: Any,
*,
cell_totals: ndarray | 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 = True,
) -> BonsaiResult
Raw counts to tree: sanity, from_sanity, then bonsai.
The whole chain runs in Rust, so the dense posteriors never cross into Python.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
counts
|
Any
|
As |
required |
cell_totals
|
ndarray | 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
|
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 |
True
|
Returns:
| Type | Description |
|---|---|
BonsaiResult
|
As |
BonsaiResult
|
|
sanity
¶
sanity(
counts: Any,
*,
cell_totals: ndarray | None = None,
variance_rule: VarianceRule = "marginalise",
fixed_variance: float | None = None,
dtype: type[float32] | type[float64] = float32,
gpu: bool = False,
) -> SanityResult
Posterior log expression and error bars from raw UMI counts.
Runs Sanity (Breda et al., Nat. Biotechnol. 2021) via sanity-sc-rs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
counts
|
Any
|
|
required |
cell_totals
|
ndarray | None
|
Total UMIs of each cell over all genes. Defaults to the
row sums of |
None
|
variance_rule
|
VarianceRule
|
How the per-gene variance enters the estimates.
|
'marginalise'
|
fixed_variance
|
float | None
|
The variance used for every gene when
|
None
|
dtype
|
type[float32] | type[float64]
|
Storage type of the output. Reductions are |
float32
|
gpu
|
bool
|
Run Sanity on the GPU through wgpu. The device path is
|
False
|
Returns:
| Type | Description |
|---|---|
SanityResult
|
The posteriors, cells x genes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On malformed or non-integer counts. |
BonsaiError
|
If |
from_sanity
¶
from_sanity(
posterior_means: ndarray,
posterior_sds: ndarray,
variances: ndarray,
*,
max_amplification: float | None = None,
) -> Likelihood
Recover likelihood means and SDs from Sanity posteriors (SI eq. S5).
Sanity shrinks each cell towards the gene mean under a N(0, v) prior.
Bonsai wants the measurement before that shrinkage, so this undoes it:
mu = x * v / (v - eps^2) and sig^2 = eps^2 * v / (v - eps^2).
Pass the zero-centred log fold changes, not the log transcription quotients. With the gene mean added, the per-cell amplification scales it differently in every cell and invents structure. On simulated counts that took Robinson-Foulds from 0 to 100 of a possible 122.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
posterior_means
|
ndarray
|
Log fold changes, |
required |
posterior_sds
|
ndarray
|
Their error bars, same shape. |
required |
variances
|
ndarray
|
Sanity's per-gene variance |
required |
max_amplification
|
float | None
|
Largest |
None
|
Returns:
| Type | Description |
|---|---|
Likelihood
|
Means, SDs and variances for the kept genes, plus which genes were kept |
Likelihood
|
and dropped. |
Raises:
| Type | Description |
|---|---|
BonsaiError
|
If every gene is ill-conditioned. |
bonsai
¶
bonsai(
means: ndarray,
sds: ndarray,
*,
variances: ndarray | None = None,
start: Start = "linkage",
search: Search = "approximate",
min_signal_to_noise: float | None = None,
reroot: bool = True,
) -> BonsaiResult
Reconstruct a tree from per-cell means and error bars.
Storage follows the input: float32 in, float32 all the way down.
Same input gives the same tree whatever the thread count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
means
|
ndarray
|
|
required |
sds
|
ndarray
|
Standard deviations on |
required |
variances
|
ndarray | None
|
Per-feature variance. |
None
|
start
|
Start
|
|
'linkage'
|
search
|
Search
|
|
'approximate'
|
min_signal_to_noise
|
float | None
|
Features below this signal-to-noise are dropped
before the search. |
None
|
reroot
|
bool
|
Reroot for display once the search is done. Changes the picture, not the likelihood. |
True
|
Returns:
| Type | Description |
|---|---|
BonsaiResult
|
The tree, its loglikelihood and the posterior over every node. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On shape mismatches, non-positive SDs or non-finite means. |
BonsaiError
|
If no feature survives selection. |
backbone
¶
backbone(
means: ndarray,
sds: ndarray,
*,
variances: ndarray | None = None,
backbone_cells: int | None = None,
seed: int = 0,
start: Start = "linkage",
search: Search = "approximate",
min_signal_to_noise: float | None = None,
reroot: bool = True,
) -> BonsaiResult
Backbone mode for datasets too large to search directly.
Reconstructs on a random subset, places every other cell onto it one at a time, then refines the whole tree. This is the paper's route to large datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
means
|
ndarray
|
As |
required |
sds
|
ndarray
|
As |
required |
variances
|
ndarray | None
|
As |
None
|
backbone_cells
|
int | None
|
Cells in the initial backbone. |
None
|
seed
|
int
|
Seed for choosing the backbone subset. |
0
|
start
|
Start
|
As |
'linkage'
|
search
|
Search
|
As |
'approximate'
|
min_signal_to_noise
|
float | None
|
As |
None
|
reroot
|
bool
|
As |
True
|
Returns:
| Type | Description |
|---|---|
BonsaiResult
|
As |
BonsaiError
¶
Bases: Exception
The data was well formed but the method could not use it.