Skip to content

scdef.pl.factor_diagnostics

factor_diagnostics(model: scDEF, brd_min: float = 1.0, ard_min: float = 0.001, clarity_min: float = 0.5, batch_purity_max: Optional[float] = None, batch_purity_soft_max: Optional[float] = None, n_eff_parents_max: float = 1.5, brd_exceptional: Optional[float] = None, figsize: tuple = (6, 4), ax: Optional[Axes] = None, annotate_factors: bool = False, annotation_fontsize: int = 8, annotation_alpha: float = 0.8, all_factors: bool = False, local_l0_scores: bool = False, x: FactorDiagQuantity = 'BRD', y: Optional[FactorDiagQuantity] = None, color: Optional[FactorDiagQuantity] = None, size: Optional[FactorDiagQuantity] = 'ARD', batch_panel: Optional[bool] = None, show: bool = True, _draw_colorbar: bool = True, _draw_size_legend: bool = True) -> Optional[Union[Axes, ndarray]]

Diagnostic scatter plot of layer-0 factors with flexible axis/color/size mapping.

By default plots BRD vs effective parents with marker size scaled by ARD. When batch_purity_max or batch_purity_soft_max is set and color is not overridden, points are colored by the corresponding batch purity (sizes still default to ARD).

Any per-factor column of factor_obs can be mapped to an axis, color or size. That includes the batch-split diagnostics written by scdef.tools.factor_diagnostics(..., batch_key=...), so a within-cell-type per-batch split can be inspected directly, e.g.::

scdef.pl.factor_diagnostics(
    model, x="avg_n_eff_parents", y="batch_split_corr", color="frac_dom_batch"
)

When those diagnostics exist and the axes are left at their defaults, a second panel showing them is drawn automatically (see batch_panel). Both batch_split_corr and frac_dom_batch are dense — every kept factor has a value — so nothing is silently dropped from either panel. A high batch_split_corr at a balanced frac_dom_batch (top-left of that panel) is the batch-corrected cell-type factor, not a split half.

This plot renders diagnostics; it issues no verdict, and neither does any tool it draws from. To describe the shape of the batch structure — which factors are batch-skewed, which have an opposite-batch sibling under the same parent, and how separable the batches are inside each branch — use batch_structure_report::

rep = scdef.tl.batch_structure_report(model, batch_key="Experiment")
splits = rep.index[rep["shape"] == "branch_split"]

shape is geometry, never cause: only the experimental design can say whether a per-batch split is a technical duplication or a genuine condition-specific program.

Parameters:

Name Type Description Default
model scDEF

scDEF model instance

required
brd_min float

minimum BRD filter threshold

1.0
ard_min float

minimum ARD filter threshold (fraction of total ARD)

0.001
clarity_min float

used for the horizontal cutoff when filtering on local n_eff_parents (not lineage avg_n_eff_parents): cutoff is effective_parents_from_clarity(clarity_min, K_parents).

0.5
batch_purity_max Optional[float]

optional upper bound on hard-assignment batch purity. Factors pass when batch_purity <= batch_purity_max. If provided and color is not set, the scatter is colored by batch_purity with the colorbar threshold at this value.

None
batch_purity_soft_max Optional[float]

optional upper bound on soft batch purity (from X_<layer>_probs). Factors pass when batch_purity_soft <= batch_purity_soft_max. If provided and color is not set (and batch_purity_max is None), color defaults to batch_purity_soft. Requires scdef.tools.factor_diagnostics(..., batch_key=...).

None
n_eff_parents_max float

used when filtering on lineage avg_n_eff_parents (local_l0_scores=False and column present): dashed line at this value and pass rule y <= n_eff_parents_max (default 1.5). When brd_exceptional is set, factors with BRD >= brd_exceptional also pass.

1.5
brd_exceptional Optional[float]

if set, dashed vertical line on BRD and pass rule allowing high-BRD factors to be kept even when effective parents exceed n_eff_parents_max. Default None (disabled).

None
figsize tuple

Figure size (if ax is None)

(6, 4)
ax Optional[Axes]

matplotlib Axes to plot on

None
annotate_factors bool

whether to annotate each point with its factor label

False
annotation_fontsize int

fontsize for factor text annotations

8
annotation_alpha float

alpha value for factor text annotations

0.8
all_factors bool

if True, plot diagnostics for all layer-0 factors from the complete snapshot model.adata.uns['factor_obs_full'] (including factors that were filtered out). Default (False) plots the current view model.adata.uns['factor_obs'], which after model.filter_factors() contains only kept factors.

False
local_l0_scores bool

when y is not set, use layer-0 n_eff_parents on the y-axis instead of lineage avg_n_eff_parents.

False
x FactorDiagQuantity

quantity for the x-axis (ARD, BRD, n_eff_parents, avg_n_eff_parents, batch_purity, batch_purity_soft, batch_split_corr, frac_dom_batch, signature_confidence, n_cells). batch_split_corr and frac_dom_batch require scdef.tools.factor_diagnostics(..., batch_key=...).

'BRD'
y Optional[FactorDiagQuantity]

quantity for the y-axis; default follows local_l0_scores / avg_n_eff_parents availability.

None
color Optional[FactorDiagQuantity]

quantity for marker color. Default None: use batch_purity when batch_purity_max is set, else batch_purity_soft when batch_purity_soft_max is set, else uncolored markers. In the two-panel batch layout the default is batch_purity for both panels, so the same factor reads the same way in each; pass color explicitly to override the left panel.

None
size Optional[FactorDiagQuantity]

quantity for marker size. Default ARD. Pass None for fixed marker size.

'ARD'
batch_panel Optional[bool]

draw a second panel with the per-batch split diagnostics (x='frac_dom_batch', y='batch_split_corr', size='n_cells', color='batch_purity') beside the usual one. None (default) enables it automatically when the batch diagnostics exist and the axes were left at their defaults; passing x/y or an ax keeps the single panel. True forces both panels (requires ax=None and the batch diagnostics); False forces one. In the two-panel layout the left panel keeps whatever x/y/color/size you pass.

None
show bool

whether to show the plot

True

Returns:

Type Description
Optional[Union[Axes, ndarray]]

Axes object if show is False, None otherwise. With two panels, an array

Optional[Union[Axes, ndarray]]

of the two Axes.