Skip to content

scdef.add_batch_correction

add_batch_correction(reference_model: 'scDEF', batch_key: str, *, adata: Optional[AnnData] = None, counts_layer: Optional[str] = None, copy_cell_z: bool = True, freeze_w: bool = False, learn_budgets: bool = True, n_epoch: int = 400, lr: float = 0.05, tolerance: float = 0.0001, from_reference_kwargs: Optional[Mapping[str, Any]] = None, **fit_kwargs: Any) -> 'scDEF'

Warm-start a batch-corrected model from a fitted hierarchy.

Designed for the workflow: 1. Fit reference_model without a batch_key to learn the factor hierarchy on the unbatched signal (optionally followed by filter_factors). 2. Call this function to construct a new model that shares the same hierarchy (factor_lists, layer sizes, W, BRD, ARD) and re-fits it under per-batch gene-scale priors so batch effects are absorbed by gene_scale, not by the hierarchy.

Parameters:

Name Type Description Default
reference_model 'scDEF'

a fitted scDEF providing the hierarchy.

required
batch_key str

column in adata.obs to use as the new batch annotation.

required
adata Optional[AnnData]

AnnData for the second pass. Defaults to reference_model.adata.

None
counts_layer Optional[str]

counts layer for the new adata.

None
copy_cell_z bool

whether to copy per-cell z warm starts for shared cells.

True
freeze_w bool

hold every per-layer W fixed during the second fit.

False
learn_budgets bool

allow per-batch gene-scale and per-cell budgets to move.

True
n_epoch int

epochs for the second-pass fit.

400
lr float

learning rate for the second-pass fit.

0.05
tolerance float

early-stopping tolerance for the second-pass fit.

0.0001
from_reference_kwargs Optional[Mapping[str, Any]]

extra kwargs forwarded to from_reference.

None
**fit_kwargs Any

additional kwargs forwarded to model.fit().

{}

Returns:

Type Description
'scDEF'

The new fitted model with batch correction applied.