Skip to content

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 (X_L0 in adata.obsm) and factors flagged by set_batch_technical_factors.

required
reduce Literal['sum', 'max']

how to merge a group of two or more flagged siblings. "sum" (default) adds them, which is exact when the halves are disjoint -- the usual case, since each cell is dominated by the half from its own batch. "max" takes the per-cell peak instead. The two differ only for cells carrying real mass on both halves, where "sum" reports the combined program and "max" the stronger half alone. Irrelevant to a lone flagged factor, which is dropped either way.

'sum'
key_added str

adata.obsm key for the corrected matrix. The column labels go to adata.uns[key_added + '_factors'], the per-column member names to ..._members, the flagged factors to ..._batch_technical and the dropped ones to ..._dropped, so a cached matrix can be checked against the current flags.

'X_L0_batch_corrected'
labels_key_added str

base name for the two adata.obs columns.

'batch_corrected'
top_layer Optional[int]

the batch-corrected parent layer flagged factors group and roll up to. Defaults to adata.uns['batch_technical_top_layer'], recorded by decompose_batch_effects as the layer whose z it froze, else 1. Override only to inspect a different grouping.

None

Returns:

Type Description
None

None. Everything is written to model.adata: the corrected

None

(n_cells, n_corrected_factors) score matrix to

None

obsm[key_added] — with fewer columns than X_L0 whenever

None

anything was flagged — its column labels to

None

uns[key_added + '_factors'], and the two label columns to obs.

Raises:

Type Description
KeyError

X_L0 is missing, the model has no parent layer to group by, or the connection weights are unavailable.

ValueError

reduce is not "sum" or "max", top_layer is out of range, the stored scores are stale, or every column was dropped.

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"])