scdef.tl.factor_batch_correction ¶
factor_batch_correction(model: scDEF, reduce: Literal['sum', 'max'] = 'sum', key_added: str = 'X_L0_batch_corrected', labels_key_added: str = 'batch_corrected', top_layer: Optional[int] = None) -> None
Apply the batch-technical correction to the scores and to the labels.
Factors flagged by set_batch_technical_factors are per-batch views of
a program that the batch-corrected top_layer already represents once.
This removes them from the layer-0 representation and writes the same
correction as cell-level labels, so an embedding, a heatmap and a UMAP
colouring all describe one corrected view.
Flagged factors are grouped by their top_layer parent and handled by
group size:
- Two or more flagged siblings under one parent -- the per-batch halves
of a single program -- collapse into one merged column, labelled by
joining the members with
+in layer order (e.g."L0_7+L0_14"). - A lone flagged factor under a parent, with no flagged counterpart -- a batch-skewed program with no opposite-batch half -- has nothing to merge with, so its column is dropped. The cells it claimed are then described by the factors they still score on, and their labels roll up to the parent.
Anything not flagged is untouched and keeps its own column, including a batch-restricted factor that is genuine biology (an ISG program, say) and any non-flagged sibling of a flagged one. An embedding built on the result therefore mixes batches where the split was judged technical and keeps them apart where it was not.
Two adata.obs columns are always written, from the same grouping:
f"L0_{labels_key_added}" labels merged siblings by their joined name and
a lone flagged factor by its parent, while labels_key_added labels every
flagged cell by its parent. All other cells keep their layer-0 label in both.
This addresses one shape of batch structure only: the one that appears as an
extra column per batch. It does nothing about a factor that both batches
use and merely score differently in magnitude -- on pbmcs2b, correcting the
branch_split columns moves the median within-branch batch AUC only
0.970 -> 0.930, because what is left is not confined to a clean pair of
sibling columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
scDEF
|
scDEF model instance with cell scores annotated ( |
required |
reduce
|
Literal['sum', 'max']
|
how to merge a group of two or more flagged siblings. |
'sum'
|
key_added
|
str
|
|
'X_L0_batch_corrected'
|
labels_key_added
|
str
|
base name for the two |
'batch_corrected'
|
top_layer
|
Optional[int]
|
the batch-corrected parent layer flagged factors group and
roll up to. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
None
|
|
None
|
|
None
|
anything was flagged — its column labels to |
None
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
|
ValueError
|
|
Example
model = scdef.scDEF.decompose_batch_effects(ref, top_layer=1) rep = scdef.tl.batch_structure_report(model) flagged = rep.index[rep["shape"].isin(["branch_split", "branch_skewed"])] scdef.tl.set_batch_technical_factors(model, flagged) scdef.tl.factor_batch_correction(model) scdef.pl.umap(model, color=["L0_batch_corrected", "batch_corrected"])