Skip to content

scdef.tl.set_batch_technical_factors

set_batch_technical_factors(model: scDEF, factors: Sequence[str]) -> None

Mark layer-0 factors as batch-technical (per-batch splits of one type).

Unlike set_technical_factors, this flag does not propagate up the tree and no factor is deleted. Batch-technical factors are layer-0 per-batch views of a program that the parent layer already represents once, so that parent is the roll-up target and propagating the flag into it would throw away the very signal the roll-up depends on.

This works on any fitted model with a batch column in adata.obs, not only on one from decompose_batch_effects — a plain fit made without a batch_key leaves batch structure in the factors directly, and can be flagged and corrected the same way. See batch_structure_report for how the reading differs between the two.

This records which factors are batch-technical and nothing else. The roll-up target comes from adata.uns['batch_technical_top_layer'], written by decompose_batch_effects as the layer whose z it froze — the only place that knows it. Consumers (factor_batch_correction, assign_confident) read it by default and take an override argument, so there is no second copy here to fall out of step with the decomposition.

Sets factor_obs['batch_technical'].

Parameters:

Name Type Description Default
model scDEF

scDEF model instance.

required
factors Sequence[str]

factor names to flag. Names are resolved against the current model.factor_names (see _resolve_factor_obs_names), so names taken from a filtered model are safe.

required

Raises:

Type Description
ValueError

if any name in factors cannot be resolved.

Example

import scdef

Stage 1: batch-corrected reference fit.

ref = scdef.scDEF(adata, counts_layer="counts", batch_key="Experiment") ref.fit()

Stage 2: re-learn L0/L1 without the batch key to expose batch programs.

model = scdef.scDEF.decompose_batch_effects(ref, top_layer=1)

Describe the batch geometry, then decide from the design which

branch splits are technical (here both batches are one donor).

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

Cells owned by a split are assigned at their L1 parent instead.

scdef.tl.assign_confident(model, exclude_batch_technical=True)