Skip to content

Batch correction (GPU)

The CPU twin is bixverse.integrate.

integrate

GPU batch correction: Harmony v2, BBKNN and fastMNN.

Harmony and fastMNN write their corrected embedding as "harmony_gpu" and "mnn_gpu", stamped as derived from the embedding they corrected; BBKNN writes the kNN and the connectivities as bixverse.integrate.bbknn does.

>>> harmony(sc, "batch")
>>> bixverse_gpu.graph.find_neighbours(sc, embd_to_use="harmony_gpu")

Unlike bixverse.integrate.fast_mnn, the GPU fastMNN corrects the PCA in the object rather than computing one on batch-aware HVGs.

harmony

harmony(
    sc: ScOrScSubset,
    batch_column: str,
    *,
    embd_to_use: str = "pca",
    modality: CacheModality = "rna",
    harmony_params: HarmonyV2GpuParams | None = None,
    seed: int = 42,
    verbose: Verbose = True,
    backend: Backend = "wgpu",
) -> None

Harmony v2 on the PCA or another embedding on the GPU.

One batch covariate only. The corrected embedding goes to the modality's embeddings["harmony_gpu"], stamped as derived from embd_to_use, on either backend.

Parameters:

Name Type Description Default
sc ScOrScSubset

The experiment, with the embedding.

required
batch_column str

obs column with the batch.

required
embd_to_use str

The embedding to correct, "pca" or a name in the modality's embeddings ("lsi" for ATAC).

'pca'
modality CacheModality

The cache to read and write: "rna", or "adt" / "atac" on a SingleCellsMultiModal.

'rna'
harmony_params HarmonyV2GpuParams | None

The parameters. None uses the defaults; a None k becomes min(round(n_cells / 30), 100).

None
seed int

Seed.

42
verbose Verbose

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

True
backend Backend

"wgpu" (any GPU), or "mlx" (Apple Silicon, needs a source build with the mlx feature; see mlx_available()).

'wgpu'

Raises:

Type Description
ValueError

If the embedding is stale, or the batch column is missing or incomplete.

BixverseError

Without a GPU adapter, or with backend="mlx" on a build without the MLX twins.

References

Patikas, et al., bioRxiv, 2026.

bbknn

bbknn(
    sc: ScOrScSubset,
    batch_column: str,
    *,
    no_neighbours_to_keep: int = 5,
    no_embd_to_use: int | None = None,
    bbknn_params: BbknnGpuParams | None = None,
    seed: int = 42,
    verbose: Verbose = True,
) -> None

Batch-balanced kNN with the per-batch searches on the GPU.

Writes what bixverse.integrate.bbknn writes: the kNN (the no_neighbours_to_keep nearest per row of the BBKNN distance matrix, balanced across batches) to sc.cache.knn and the max-symmetrised connectivities to sc.cache.connectivities, on the PCA.

Parameters:

Name Type Description Default
sc ScOrScSubset

The experiment, with a PCA.

required
batch_column str

obs column with the batch.

required
no_neighbours_to_keep int

Neighbours per cell in the stored kNN; capped at n_batches * neighbours_within_batch.

5
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
bbknn_params BbknnGpuParams | None

The parameters. None uses the defaults.

None
seed int

Seed.

42
verbose Verbose

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

True

Raises:

Type Description
ValueError

If the PCA is stale, or the batch column is missing or incomplete.

BixverseError

Without a GPU adapter.

References

Polański, et al., Bioinformatics, 2020.

fast_mnn

fast_mnn(
    sc: ScOrScSubset,
    batch_column: str,
    *,
    no_embd_to_use: int | None = None,
    fastmnn_params: FastmnnGpuParams | None = None,
    seed: int = 42,
    verbose: Verbose = True,
) -> None

fastMNN on the PCA in the object, neighbour searches on the GPU.

The corrected embedding goes to sc.cache.embeddings["mnn_gpu"], stamped as derived from "pca". Run the PCA on batch-aware HVGs first if that is what you want.

Parameters:

Name Type Description Default
sc ScOrScSubset

The experiment, with a PCA.

required
batch_column str

obs column with the batch.

required
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
fastmnn_params FastmnnGpuParams | None

The parameters. None uses the defaults.

None
seed int

Seed.

42
verbose Verbose

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

True

Raises:

Type Description
ValueError

If the PCA is stale, or the batch column is missing, incomplete or does not cover the PCA.

BixverseError

Without a GPU adapter.

References

Haghverdi, et al., Nat Biotechnol, 2018.