Skip to content

scdef.tl.factor_diagnostics

factor_diagnostics(model: scDEF, recompute: bool = False, batch_key: Optional[str] = None, sensible_top_n_eff_parents_max: float = 1.5, sensible_top_min_best_parent_prob: Optional[float] = None, sensible_top_min_clear_children: int = 2, sensible_top_ignore_root: bool = True, sensible_top_use_filtered: bool = True, confidence_threshold: float = 0.9, tau_quantile: float = 0.99, min_effect: Optional[float] = None, mc_samples: int = 100, random_seed: int = 0, batch_split_min_batch_frac: float = 0.7, gene_scale_reference: Optional[Any] = None) -> None

Compute/store factor diagnostics in model.adata.uns['factor_obs'].

Populates per-factor hierarchy scores plus ARD, BRD, n_cells (hard argmax posterior z assignments among kept factors — the same rule as annotate_adata / make_graph(..., assignments=True)), and optional batch metrics when batch_key is set.

Also runs set_confident_signatures so plotting helpers (make_graph, pl.factor_diagnostics(color='signature_confidence'), etc.) can use cached signatures without a separate call.

Parameters:

Name Type Description Default
model scDEF

scDEF model instance

required
recompute bool

if True, force recomputation of the cached fixed upper-layer factor subset used for clarity scores, even if the fit revision did not change.

False
batch_key Optional[str]

key in model.adata.obs used to compute per-factor batch metrics. Defaults to the model's own batch_key when it has one — from being constructed with it, or from decompose_batch_effects, which carries it — so a batch-aware model gets its batch metrics without being told twice. A model with no batch key, or one whose key has fewer than two observed values, gets no batch columns. batch_purity uses hard winner cells (argmax variational z). batch_purity_soft uses the batch distribution of per-cell memberships from X_<layer>_probs (or row-normalized X_<layer> / posterior z if probs are missing). Both are 1 - entropy / log(n_batches). Also writes dom_batch / frac_dom_batch (the factor's dominant batch and its share of the factor's cells) and, for layer 0, the batch_split_* columns from _compute_l0_batch_split. All are plottable via scdef.pl.factor_diagnostics.

None
sensible_top_n_eff_parents_max float

threshold used to classify sensible-top factors on the hierarchy walk.

1.5
sensible_top_min_best_parent_prob Optional[float]

optional best-parent probability threshold used alongside n_eff_parents_max when deciding whether a child merge is clear.

None
sensible_top_min_clear_children int

parent escape-hatch count; a parent is accepted if it owns at least this many clear best-parent children, even when weighted ambiguity is high.

2
sensible_top_ignore_root bool

whether to ignore a width-1 final root layer when classifying sensible-top factors.

True
sensible_top_use_filtered bool

whether to use model.factor_lists / model.factor_names for hierarchy transitions.

True
confidence_threshold float 0.9
tau_quantile float 0.99
min_effect Optional[float] None
mc_samples int 100
random_seed int 0
batch_split_min_batch_frac float

center of the batch-skew ramp that weights candidate partners in _compute_l0_batch_split (default 0.7: a candidate at 0.6 contributes nothing, one at 0.8 contributes fully). Only used when batch_key is set.

0.7
gene_scale_reference Optional[Any]

optional gene-side batch diagnostic, off by default. Pass the REFERENCE fit (the model made with batch_key and per-batch gene_scale) — or anything else get_factor_batch_gene_scale_affinity accepts — to add two layer-0 columns: gene_scale_affinity, the Spearman correlation between the factor's gene loadings and the per-batch gene_scale log-ratio of the batch it matches best, and gene_scale_affinity_batch, that batch. High values say the factor is built from the genes the reference fit's per-batch term absorbed; they do not say the factor is technical (on a condition-like batch key that programme is the biology of the experiment). See that function's docstring.

None