Skip to content

Doublets and ambient RNA

Doublet callers, empty-droplet calls and CellSweep ambient RNA removal.

Three callers find doublets from the counts: scrublet, doublet_detection_boost and scdblfinder. Run one, turn the result into an obs frame with get_data and add it. cellsweep is a separate job: it removes ambient and bulk contamination from a raw experiment that still holds its empty droplets, and uses infer_empty_droplets to find them.

What lands where. The callers leave the experiment alone and return a frozen record (ScrubletRes, BoostRes, ScDblFinderRes). get_data turns a record into a frame with a 0-based cell_idx for sc.add_obs. call_doublets_manual re-calls a Scrublet result at a threshold of your choosing and returns a new record. get_feature_mat and get_scores read the classifier output of an scDblFinder record. infer_empty_droplets returns a boolean array. cellsweep writes a new experiment into the directory you give it and returns it, with cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged in obs and cellsweep_ambient in var. The input stays as it was.

Object types. All functions take a SingleCells. The callers run on the kept cells unless you hand them cells_to_use.

res = bv.doublets.scrublet(sc, group_by="sample_id")
sc.add_obs(bv.doublets.get_data(res))
sc.obs()[["doublet", "doublet_score"]].head()

With group_by every method runs once per group, and per-group scalars such as threshold become {group: value} dicts. Doublets form inside a droplet emulsion, so group by the sample or the lane, never across them. A group below 50 cells is an error, below 500 a warning.

Gotchas. get_data names its columns after the caller: Scrublet and Boost both write doublet and doublet_score, so adding the second replaces the first. Take one result at a time, or rename the columns before add_obs. call_doublets_manual only takes Scrublet results; on a grouped one it re-calls a single group (for_sample, else the first). cellsweep runs after annotation and wants the empty droplets in the object, so ingest with MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0); with the default freeze_ambient_profile it refuses an object whose smallest library size is above 100. Barcodes that are neither empty nor annotated and kept are left out of the fit and the output.

Worked through in Doublet detection with bixverse and Ambient RNA removal with CellSweep.

Scrublet

Doublet callers, empty-droplet calls and CellSweep ambient RNA removal.

Three callers find doublets from the counts: scrublet, doublet_detection_boost and scdblfinder. Run one, turn the result into an obs frame with get_data and add it. cellsweep is a separate job: it removes ambient and bulk contamination from a raw experiment that still holds its empty droplets, and uses infer_empty_droplets to find them.

What lands where. The callers leave the experiment alone and return a frozen record (ScrubletRes, BoostRes, ScDblFinderRes). get_data turns a record into a frame with a 0-based cell_idx for sc.add_obs. call_doublets_manual re-calls a Scrublet result at a threshold of your choosing and returns a new record. get_feature_mat and get_scores read the classifier output of an scDblFinder record. infer_empty_droplets returns a boolean array. cellsweep writes a new experiment into the directory you give it and returns it, with cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged in obs and cellsweep_ambient in var. The input stays as it was.

Object types. All functions take a SingleCells. The callers run on the kept cells unless you hand them cells_to_use.

res = bv.doublets.scrublet(sc, group_by="sample_id")
sc.add_obs(bv.doublets.get_data(res))
sc.obs()[["doublet", "doublet_score"]].head()

With group_by every method runs once per group, and per-group scalars such as threshold become {group: value} dicts. Doublets form inside a droplet emulsion, so group by the sample or the lane, never across them. A group below 50 cells is an error, below 500 a warning.

Gotchas. get_data names its columns after the caller: Scrublet and Boost both write doublet and doublet_score, so adding the second replaces the first. Take one result at a time, or rename the columns before add_obs. call_doublets_manual only takes Scrublet results; on a grouped one it re-calls a single group (for_sample, else the first). cellsweep runs after annotation and wants the empty droplets in the object, so ingest with MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0); with the default freeze_ambient_profile it refuses an object whose smallest library size is above 100. Barcodes that are neither empty nor annotated and kept are left out of the fit and the output.

ScrubletRes dataclass

ScrubletRes(
    predicted_doublets: ndarray,
    doublet_scores_obs: ndarray,
    doublet_errors_obs: ndarray,
    z_scores: ndarray,
    threshold: GroupScalar,
    detected_doublet_rate: GroupScalar,
    detectable_doublet_fraction: GroupScalar,
    overall_doublet_rate: GroupScalar,
    doublet_scores_sim: ndarray | dict[str, ndarray],
    cell_indices: ndarray,
    pca: ndarray | dict[str, ndarray] | None = None,
    pair_1: ndarray | dict[str, ndarray] | None = None,
    pair_2: ndarray | dict[str, ndarray] | None = None,
    cell_groups: ndarray | None = None,
    group_by: str | None = None,
)

Scrublet result.

Grouped results hold the per-group scalars and simulated scores as {group: value} dicts.

Attributes:

Name Type Description
predicted_doublets ndarray

Per cell, whether it is called a doublet.

doublet_scores_obs ndarray

Per-cell doublet score.

doublet_errors_obs ndarray

Per-cell standard error of the score.

z_scores ndarray

(score - threshold) / error per cell.

threshold GroupScalar

The score threshold.

detected_doublet_rate GroupScalar

Share of cells called doublets.

detectable_doublet_fraction GroupScalar

Share of simulated doublets above the threshold.

overall_doublet_rate GroupScalar

Estimated overall doublet rate.

doublet_scores_sim ndarray | dict[str, ndarray]

Scores of the simulated doublets.

cell_indices ndarray

0-based store indices the per-cell vectors align to.

pca ndarray | dict[str, ndarray] | None

PCA of observed then simulated cells, if asked for.

pair_1 ndarray | dict[str, ndarray] | None

First parent (0-based, within the run's cells) per simulated doublet, if asked for.

pair_2 ndarray | dict[str, ndarray] | None

Second parent per simulated doublet, if asked for.

cell_groups ndarray | None

Group label per cell when grouped, else None.

group_by str | None

The grouping column when grouped, else None.

scrublet

scrublet(
    sc: SingleCells,
    *,
    scrublet_params: ScrubletParams | None = None,
    seed: int = 42,
    streaming: bool | None = None,
    cells_to_use: Selection | None = None,
    group_by: str | None = None,
    return_combined_pca: bool = False,
    return_pairs: bool = False,
    verbose: Verbose = True,
) -> ScrubletRes

Scrublet doublet detection.

Simulates doublets from random pairs of observed cells, embeds both in one PCA on the HVGs and scores each observed cell by the share of simulated doublets among its neighbours. The threshold comes from Otsu's method on the observed scores unless set by hand.

Parameters:

Name Type Description Default
sc SingleCells

The experiment.

required
scrublet_params ScrubletParams | None

The parameters. None uses the defaults.

None
seed int

Seed.

42
streaming bool | None

Stream the HVG pass. None streams above N_CELLS_STREAMING_THRESHOLD cells.

None
cells_to_use Selection | None

0-based cell indices or ids to run on. None uses the kept cells.

None
group_by str | None

obs column to run per group on, e.g. the sample.

None
return_combined_pca bool

Also return the PCA of observed and simulated cells.

False
return_pairs bool

Also return the parents of the simulated doublets.

False
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it. With group_by, 1 shows a progress bar over the groups and 2 also runs each group at level 1.

True

Returns:

Type Description
ScrubletRes

The ScrubletRes.

Raises:

Type Description
ValueError

If group_by is missing or incomplete, or a group has fewer than GROUP_MIN_CELLS cells.

References

Wolock, et al., Cell Syst, 2019.

Examples:

>>> res = bv.doublets.scrublet(sc)
>>> res = bv.doublets.scrublet(sc, group_by="sample_id", return_pairs=True)

call_doublets_manual

call_doublets_manual(
    scrublet_res: ScrubletRes,
    threshold: float,
    *,
    for_sample: str | None = None,
    verbose: Verbose = True,
) -> ScrubletRes

Re-call Scrublet doublets at a hand-picked threshold.

Recomputes the calls, the z-scores and the three rates, for the whole result or, when grouped, for one group.

Parameters:

Name Type Description Default
scrublet_res ScrubletRes

The result.

required
threshold float

The new threshold, in [0, 1].

required
for_sample str | None

The group to update when grouped; None takes the first. Ignored when not grouped.

None
verbose Verbose

Log the new rates.

True

Returns:

Type Description
ScrubletRes

A new ScrubletRes.

Raises:

Type Description
ValueError

If threshold is outside [0, 1] or for_sample is not a group.

Examples:

>>> res = bv.doublets.scrublet(sc)
>>> res = bv.doublets.call_doublets_manual(res, 0.35)

Boosted classification

Doublet callers, empty-droplet calls and CellSweep ambient RNA removal.

Three callers find doublets from the counts: scrublet, doublet_detection_boost and scdblfinder. Run one, turn the result into an obs frame with get_data and add it. cellsweep is a separate job: it removes ambient and bulk contamination from a raw experiment that still holds its empty droplets, and uses infer_empty_droplets to find them.

What lands where. The callers leave the experiment alone and return a frozen record (ScrubletRes, BoostRes, ScDblFinderRes). get_data turns a record into a frame with a 0-based cell_idx for sc.add_obs. call_doublets_manual re-calls a Scrublet result at a threshold of your choosing and returns a new record. get_feature_mat and get_scores read the classifier output of an scDblFinder record. infer_empty_droplets returns a boolean array. cellsweep writes a new experiment into the directory you give it and returns it, with cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged in obs and cellsweep_ambient in var. The input stays as it was.

Object types. All functions take a SingleCells. The callers run on the kept cells unless you hand them cells_to_use.

res = bv.doublets.scrublet(sc, group_by="sample_id")
sc.add_obs(bv.doublets.get_data(res))
sc.obs()[["doublet", "doublet_score"]].head()

With group_by every method runs once per group, and per-group scalars such as threshold become {group: value} dicts. Doublets form inside a droplet emulsion, so group by the sample or the lane, never across them. A group below 50 cells is an error, below 500 a warning.

Gotchas. get_data names its columns after the caller: Scrublet and Boost both write doublet and doublet_score, so adding the second replaces the first. Take one result at a time, or rename the columns before add_obs. call_doublets_manual only takes Scrublet results; on a grouped one it re-calls a single group (for_sample, else the first). cellsweep runs after annotation and wants the empty droplets in the object, so ingest with MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0); with the default freeze_ambient_profile it refuses an object whose smallest library size is above 100. Barcodes that are neither empty nor annotated and kept are left out of the fit and the output.

BoostRes dataclass

BoostRes(
    doublet: ndarray,
    doublet_score: ndarray,
    voting_avg: ndarray,
    cell_indices: ndarray,
    cell_groups: ndarray | None = None,
    group_by: str | None = None,
)

Boosted doublet classification result.

Attributes:

Name Type Description
doublet ndarray

Per cell, whether it is called a doublet.

doublet_score ndarray

Per-cell doublet score, averaged over the iterations.

voting_avg ndarray

Per-cell share of iterations that voted doublet.

cell_indices ndarray

0-based store indices the per-cell vectors align to.

cell_groups ndarray | None

Group label per cell when grouped, else None.

group_by str | None

The grouping column when grouped, else None.

doublet_detection_boost

doublet_detection_boost(
    sc: SingleCells,
    *,
    boost_params: BoostParams | None = None,
    cells_to_use: Selection | None = None,
    group_by: str | None = None,
    seed: int = 42,
    streaming: bool | None = None,
    verbose: Verbose = True,
) -> BoostRes

Boosted doublet classification.

Each iteration simulates doublets, clusters observed and simulated cells together with Louvain and tests every cluster for doublet enrichment; a cell is a doublet if enough iterations vote for it.

Parameters:

Name Type Description Default
sc SingleCells

The experiment.

required
boost_params BoostParams | None

The parameters. None uses the defaults. With fast_cluster and no n_centroids, int(sqrt(n_cells) * 4) centroids.

None
cells_to_use Selection | None

0-based cell indices or ids to run on. None uses the kept cells.

None
group_by str | None

obs column to run per group on, e.g. the sample.

None
seed int

Seed.

42
streaming bool | None

Stream the HVG pass. None streams above N_CELLS_STREAMING_THRESHOLD cells.

None
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it. With group_by, 1 shows a progress bar over the groups and 2 also runs each group at level 1.

True

Returns:

Type Description
BoostRes

The BoostRes.

Raises:

Type Description
ValueError

If group_by is missing or incomplete, or a group has fewer than GROUP_MIN_CELLS cells.

Examples:

>>> boost = bv.doublets.doublet_detection_boost(sc, group_by="sample_id")
>>> sc.add_obs(bv.doublets.get_data(boost))

scDblFinder

Doublet callers, empty-droplet calls and CellSweep ambient RNA removal.

Three callers find doublets from the counts: scrublet, doublet_detection_boost and scdblfinder. Run one, turn the result into an obs frame with get_data and add it. cellsweep is a separate job: it removes ambient and bulk contamination from a raw experiment that still holds its empty droplets, and uses infer_empty_droplets to find them.

What lands where. The callers leave the experiment alone and return a frozen record (ScrubletRes, BoostRes, ScDblFinderRes). get_data turns a record into a frame with a 0-based cell_idx for sc.add_obs. call_doublets_manual re-calls a Scrublet result at a threshold of your choosing and returns a new record. get_feature_mat and get_scores read the classifier output of an scDblFinder record. infer_empty_droplets returns a boolean array. cellsweep writes a new experiment into the directory you give it and returns it, with cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged in obs and cellsweep_ambient in var. The input stays as it was.

Object types. All functions take a SingleCells. The callers run on the kept cells unless you hand them cells_to_use.

res = bv.doublets.scrublet(sc, group_by="sample_id")
sc.add_obs(bv.doublets.get_data(res))
sc.obs()[["doublet", "doublet_score"]].head()

With group_by every method runs once per group, and per-group scalars such as threshold become {group: value} dicts. Doublets form inside a droplet emulsion, so group by the sample or the lane, never across them. A group below 50 cells is an error, below 500 a warning.

Gotchas. get_data names its columns after the caller: Scrublet and Boost both write doublet and doublet_score, so adding the second replaces the first. Take one result at a time, or rename the columns before add_obs. call_doublets_manual only takes Scrublet results; on a grouped one it re-calls a single group (for_sample, else the first). cellsweep runs after annotation and wants the empty droplets in the object, so ingest with MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0); with the default freeze_ambient_profile it refuses an object whose smallest library size is above 100. Barcodes that are neither empty nor annotated and kept are left out of the fit and the output.

ScDblFinderRes dataclass

ScDblFinderRes(
    predicted_doublets: ndarray,
    doublet_score: ndarray,
    cxds_scores: ndarray,
    weighted: ndarray,
    threshold: GroupScalar,
    cluster_labels: ndarray,
    detected_doublet_rate: GroupScalar,
    cell_indices: ndarray,
    features: DataFrame | None = None,
    cell_groups: ndarray | None = None,
    group_by: str | None = None,
)

scDblFinder result.

Attributes:

Name Type Description
predicted_doublets ndarray

Per cell, whether it is called a doublet.

doublet_score ndarray

Per-cell classifier probability.

cxds_scores ndarray

Per-cell cxds score.

weighted ndarray

Per-cell weighted score.

threshold GroupScalar

The score threshold.

cluster_labels ndarray

Per-cell cluster of the last iteration, 0-based; grouped, <group>_<label> strings.

detected_doublet_rate GroupScalar

Share of cells called doublets.

features DataFrame | None

The classifier features of the observed cells, cell_id first, if asked for.

cell_indices ndarray

0-based store indices the per-cell vectors align to.

cell_groups ndarray | None

Group label per cell when grouped, else None.

group_by str | None

The grouping column when grouped, else None.

scdblfinder

scdblfinder(
    sc: SingleCells,
    *,
    scdblfinder_params: ScdblfinderParams | None = None,
    return_features: bool = False,
    cells_to_use: Selection | None = None,
    group_by: str | None = None,
    streaming: bool | None = None,
    seed: int = 42,
    verbose: Verbose = True,
) -> ScDblFinderRes

Cluster-aware doublet detection.

Simulates doublets with a bias towards heterotypic pairs, engineers kNN and cxds features and trains a gradient-boosted classifier, iterating to drop likely doublets from the training labels.

Parameters:

Name Type Description Default
sc SingleCells

The experiment.

required
scdblfinder_params ScdblfinderParams | None

The parameters. None uses the defaults. With fast_cluster and no n_centroids, int(sqrt(n_cells) * 4) centroids.

None
return_features bool

Also return the classifier features.

False
cells_to_use Selection | None

0-based cell indices or ids to run on. None uses the kept cells.

None
group_by str | None

obs column to run per group on, e.g. the sample.

None
streaming bool | None

Stream the gene selection pass. None streams above N_CELLS_STREAMING_THRESHOLD cells.

None
seed int

Seed.

42
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it. With group_by, 1 shows a progress bar over the groups and 2 also runs each group at level 1.

True

Returns:

Type Description
ScDblFinderRes

The ScDblFinderRes.

Raises:

Type Description
ValueError

If group_by is missing or incomplete, or a group has fewer than GROUP_MIN_CELLS cells.

References

Germain, et al., F1000Research, 2022.

Examples:

>>> dbl = bv.doublets.scdblfinder(sc, return_features=True)
>>> sc.add_obs(bv.doublets.get_data(dbl))

get_feature_mat

get_feature_mat(res: ScDblFinderRes) -> DataFrame | None

The scDblFinder classifier features.

Warns and returns None if they were not asked for.

Parameters:

Name Type Description Default
res ScDblFinderRes

The scDblFinder result.

required

Returns:

Type Description
DataFrame | None

The features, or None.

get_scores

get_scores(
    res: ScDblFinderRes, score_type: ScoreType = "weighted"
) -> ndarray

The weighted or cxds scores.

Parameters:

Name Type Description Default
res ScDblFinderRes

The scDblFinder result.

required
score_type ScoreType

"weighted" or "cxds_scores".

'weighted'

Returns:

Type Description
ndarray

One score per cell.

Results into obs

Doublet callers, empty-droplet calls and CellSweep ambient RNA removal.

Three callers find doublets from the counts: scrublet, doublet_detection_boost and scdblfinder. Run one, turn the result into an obs frame with get_data and add it. cellsweep is a separate job: it removes ambient and bulk contamination from a raw experiment that still holds its empty droplets, and uses infer_empty_droplets to find them.

What lands where. The callers leave the experiment alone and return a frozen record (ScrubletRes, BoostRes, ScDblFinderRes). get_data turns a record into a frame with a 0-based cell_idx for sc.add_obs. call_doublets_manual re-calls a Scrublet result at a threshold of your choosing and returns a new record. get_feature_mat and get_scores read the classifier output of an scDblFinder record. infer_empty_droplets returns a boolean array. cellsweep writes a new experiment into the directory you give it and returns it, with cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged in obs and cellsweep_ambient in var. The input stays as it was.

Object types. All functions take a SingleCells. The callers run on the kept cells unless you hand them cells_to_use.

res = bv.doublets.scrublet(sc, group_by="sample_id")
sc.add_obs(bv.doublets.get_data(res))
sc.obs()[["doublet", "doublet_score"]].head()

With group_by every method runs once per group, and per-group scalars such as threshold become {group: value} dicts. Doublets form inside a droplet emulsion, so group by the sample or the lane, never across them. A group below 50 cells is an error, below 500 a warning.

Gotchas. get_data names its columns after the caller: Scrublet and Boost both write doublet and doublet_score, so adding the second replaces the first. Take one result at a time, or rename the columns before add_obs. call_doublets_manual only takes Scrublet results; on a grouped one it re-calls a single group (for_sample, else the first). cellsweep runs after annotation and wants the empty droplets in the object, so ingest with MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0); with the default freeze_ambient_profile it refuses an object whose smallest library size is above 100. Barcodes that are neither empty nor annotated and kept are left out of the fit and the output.

get_data

get_data(
    res: ScrubletRes | BoostRes | ScDblFinderRes,
) -> DataFrame

The per-cell calls as an obs frame for SingleCells.add_obs.

Columns, with cell_idx 0-based: doublet and doublet_score for Scrublet; every per-cell field for Boost (plus cell_groups when grouped); predicted_doublets, doublet_score, cxds_scores, weighted and cluster_labels for scDblFinder.

Parameters:

Name Type Description Default
res ScrubletRes | BoostRes | ScDblFinderRes

The result of a doublet detection run.

required

Returns:

Type Description
DataFrame

One row per cell.

Raises:

Type Description
TypeError

For any other result type.

Examples:

>>> res = bv.doublets.scrublet(sc, group_by="sample_id")
>>> sc.add_obs(bv.doublets.get_data(res))

Empty droplets and CellSweep

Doublet callers, empty-droplet calls and CellSweep ambient RNA removal.

Three callers find doublets from the counts: scrublet, doublet_detection_boost and scdblfinder. Run one, turn the result into an obs frame with get_data and add it. cellsweep is a separate job: it removes ambient and bulk contamination from a raw experiment that still holds its empty droplets, and uses infer_empty_droplets to find them.

What lands where. The callers leave the experiment alone and return a frozen record (ScrubletRes, BoostRes, ScDblFinderRes). get_data turns a record into a frame with a 0-based cell_idx for sc.add_obs. call_doublets_manual re-calls a Scrublet result at a threshold of your choosing and returns a new record. get_feature_mat and get_scores read the classifier output of an scDblFinder record. infer_empty_droplets returns a boolean array. cellsweep writes a new experiment into the directory you give it and returns it, with cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged in obs and cellsweep_ambient in var. The input stays as it was.

Object types. All functions take a SingleCells. The callers run on the kept cells unless you hand them cells_to_use.

res = bv.doublets.scrublet(sc, group_by="sample_id")
sc.add_obs(bv.doublets.get_data(res))
sc.obs()[["doublet", "doublet_score"]].head()

With group_by every method runs once per group, and per-group scalars such as threshold become {group: value} dicts. Doublets form inside a droplet emulsion, so group by the sample or the lane, never across them. A group below 50 cells is an error, below 500 a warning.

Gotchas. get_data names its columns after the caller: Scrublet and Boost both write doublet and doublet_score, so adding the second replaces the first. Take one result at a time, or rename the columns before add_obs. call_doublets_manual only takes Scrublet results; on a grouped one it re-calls a single group (for_sample, else the first). cellsweep runs after annotation and wants the empty droplets in the object, so ingest with MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0); with the default freeze_ambient_profile it refuses an object whose smallest library size is above 100. Barcodes that are neither empty nor annotated and kept are left out of the fit and the output.

infer_empty_droplets

infer_empty_droplets(
    lib_size: ndarray
    | Series
    | Sequence[int]
    | Sequence[float],
    empty_params: EmptyDropletsParams,
) -> ndarray

Call empty droplets from the library sizes.

Library sizes are truncated to integers; missing and negative ones count as zero.

Parameters:

Name Type Description Default
lib_size ndarray | Series | Sequence[int] | Sequence[float]

Library size per barcode.

required
empty_params EmptyDropletsParams

How to call them; "umi_cutoff", "expected_cells" or "knee".

required

Returns:

Type Description
ndarray

A bool array, True where the barcode is empty.

Raises:

Type Description
ValueError

With method = "supplied", which has nothing to infer.

Examples:

>>> lib_size = sc.obs()["lib_size"].to_numpy()
>>> umi = bv.params.EmptyDropletsParams(method="umi_cutoff", umi_cutoff=12000)
>>> is_empty = bv.doublets.infer_empty_droplets(lib_size, umi)

cellsweep

cellsweep(
    directory: Path | str,
    sc: SingleCells,
    celltype_column: str,
    sample_column: str,
    empty_params: EmptyDropletsParams,
    *,
    cellsweep_params: CellsweepParams | None = None,
    qc: MinQualityParams | None = None,
    csc_mem_gb: float | None = None,
    verbose: Verbose = True,
) -> SingleCells

Remove ambient and bulk contamination with CellSweep.

One EM fit per sample splits every count three ways (ambient, bulk, true expression); the denoised counts go to a new experiment. It runs after annotation, and the experiment needs its empty droplets, so ingest with a permissive MinQualityParams(min_unique_genes=0, min_lib_size=0, min_cells=0).

Barcodes split three ways: empty droplets, annotated barcodes that passed QC, and the rest, which is left out of the fit and the output. The raw layer takes the stochastically rounded counts, the normalised layer the float magnitudes. Obs is the input's with nnz and lib_size replaced by the denoised counts' statistics, plus cellsweep_alpha, cellsweep_z, cellsweep_beta, cellsweep_ll and cellsweep_converged; var gains a fresh no_cells_exp and cellsweep_ambient, the mean ambient profile over the samples.

Parameters:

Name Type Description Default
directory Path | str

The target directory, created if missing.

required
sc SingleCells

The raw experiment, empty droplets included.

required
celltype_column str

obs column with the cell-type labels.

required
sample_column str

obs column with the emulsion; one fit per value.

required
empty_params EmptyDropletsParams

How to find the empty droplets.

required
cellsweep_params CellsweepParams | None

The model parameters. None uses the defaults.

None
qc MinQualityParams | None

Only target_size is used, to scale the normalised layer.

None
csc_mem_gb float | None

Memory in GB for the CSR to CSC conversion buffers. None converts in one pass.

None
verbose Verbose

0 (False) silent, 1 (True) progress, 2 extra detail where the method has it.

True

Returns:

Type Description
SingleCells

The denoised experiment.

Raises:

Type Description
ValueError

If the target is the input directory, a column is missing, the empty droplets were filtered at ingest, or no barcode is both annotated and kept.

References

Sullivan, et al., CellSweep, 2025.

Examples:

>>> empty = bv.params.EmptyDropletsParams(method="umi_cutoff", umi_cutoff=1000)
>>> clean = bv.doublets.cellsweep("clean", raw, "cell_grp", "sample_id", empty)
>>> alpha = clean.obs(filtered=False)["cellsweep_alpha"]