Batch correction with bixverse¶
Python port of the R vignette Batch correction with bixverse. Original authors: Grant Neilson, Gregor Lueg and Liesbeth François.
Intro¶
Batch effects are systematic technical differences between experimental runs that can obscure genuine biological signal in single cell data. When datasets from multiple experiments, technologies, or laboratories are combined, cells tend to group by their source rather than by cell type. Batch correction methods aim to remove these technical artefacts whilst preserving biological variation.
bixverse provides four batch correction approaches, each with different
trade-offs:
- fastMNN (Haghverdi, et al., 2018) identifies mutual nearest neighbours across batches: pairs of cells from different batches that are each other's closest match. Correction vectors are computed from these pairs and applied to align the embeddings. It is particularly effective when cell type composition differs across batches.
- Harmony (Korsunsky, et al., 2019) operates on PCA embeddings using iterative soft clustering with diversity penalties. It assigns cells to clusters, estimates batch effects per cluster via ridge regression, and corrects the embedding. It tends to be fast and works well across a range of scenarios. A version 2 has been released too, see Patikas, et al., 2026, with improvements that increase scalability to larger data sets and modifications to the objective functions to not overcorrect across batches when different cell types are there.
- BBKNN (Polanski, et al., 2020) takes a fundamentally different approach. Rather than correcting an embedding, it constructs a batch-balanced k-nearest neighbour graph by building separate neighbour indices per batch. The corrected graph can then be used directly for clustering and visualisation.
- Seurat anchors (Stuart, et al., 2019) find pairs of cells that are mutual nearest neighbours in a space shared by two batches, score them by how much of their neighbourhood they share, and use them to pull one batch onto the other. Two flavours are available. CCA builds the shared space from canonical correlations and corrects hardest, which suits batches that don't share their cell types. rPCA projects each batch into the other's PCA basis instead: cheaper, gentler, and the better default when the batches broadly match. Both need batch-aware HVGs.
A correction has two jobs: mix the batches and keep the biology intact. Going
too hard on the first ruins the second, so bixverse scores both, following
the scIB benchmark (Luecken, et al., 2022).
Batch mixing:
- kBET: tests whether neighbourhood batch proportions match the global ones.
- Batch ASW: silhouette width on batch labels in the embedding.
- iLISI: effective number of batches per neighbourhood. Works on any kNN graph.
- PCR: how much of the embedding variance batch explains, before versus after correction.
Bio conservation, which needs cell type labels:
- cLISI: effective number of cell types per neighbourhood. You want one.
- Cell type ASW: silhouette width on cell type labels.
- Graph connectivity: whether each cell type stays one connected piece of the kNN graph.
bv.integrate.calculate_integration_metrics() runs the lot and returns one
row, every column rescaled so that higher is better.
import tempfile
from pathlib import Path
from typing import Literal
import matplotlib.pyplot as plt
import numpy as np
import polars as pl
import bixverse as bv
import bixverse.embed
import bixverse.params
dir_data = bv.io.download_pbmc_batches()
tempdir_batch_cor = tempfile.TemporaryDirectory()
h5ad_paths = {
"pbmc3k": dir_data / "pbmc3k.h5ad",
"pbmc4k": dir_data / "pbmc4k.h5ad",
}
h5_tasks = bv.io.prescan_h5ad_files(h5ad_paths)
sc = bv.io.load_multi_h5ad(
Path(tempdir_batch_cor.name) / "batch_cor",
h5_tasks,
verbose=True,
)
sc
Scan 1/2: Counting gene expression across 2 files... Scanning genes in pbmc3k... Scanning genes in pbmc4k... Genes passing global QC: 13_925 / 31_232 Scan 2/2: Computing cell QC with final gene set... Scanning cells in pbmc3k... Scanning cells in pbmc4k... pbmc3k: 2_700 / 2_700 cells passing QC pbmc4k: 4_340 / 4_340 cells passing QC Total cells: 7_040 Writing cells to binary... Writing pbmc3k (2_700 cells)... Writing pbmc4k (4_340 cells)... Converting the cell-based data into the gene-based format. Multi-h5ad loading complete: 144.95ms
Counted 7_889_186 non-zeros over 13_925 genes in 3.12ms; converting in 1 phase(s). Phase 1/1: genes 0-13925, scatter 14.56ms, compress + write 21.43ms Conversion into gene-friendly format done: 40.39ms
SingleCells( dir='/var/folders/l0/d50zd8812jg94mhtl8t2swfc0000gn/T/tmp72036sbl/batch_cor', cells=7040 (kept 7040), genes=13925, hvg=False, pca=False, embeddings=none, knn=False, graph=False, magic=False, residuals=False, stale=none )
Quality control¶
Before batch correction, we filter out low-quality cells using mitochondrial gene proportions and library complexity metrics. In a multi file import the additional var columns are dropped, so the gene symbols come from one of the h5ad files.
h5_metadata = bv.io.read_h5ad_metadata(h5ad_paths["pbmc3k"])
h5_var = h5_metadata["var"]
assert isinstance(h5_var, pl.DataFrame)
var = sc.var().join(
h5_var.select(
pl.col("ensembl_id").alias("gene_id"),
pl.col("symbol_tenx").alias("gene_symbol"),
),
on="gene_id",
)
gs_of_interest = {
"MT": var.filter(pl.col("gene_symbol").str.contains("^MT-"))["gene_id"].to_list(),
"Ribo": var.filter(pl.col("gene_symbol").str.contains("^RP[SL]"))[
"gene_id"
].to_list(),
}
bv.qc.gene_set_proportions(sc, gs_of_interest, streaming=False, verbose=True)
qc_df = sc.obs(cols=["cell_id", "lib_size", "nnz", "MT"])
metrics = {
"log10_lib_size": np.log10(qc_df["lib_size"].to_numpy()),
"log10_nnz": np.log10(qc_df["nnz"].to_numpy()),
"MT": qc_df["MT"].to_numpy(),
}
directions: dict[str, Literal["twosided", "below", "above"]] = {
"log10_lib_size": "twosided",
"log10_nnz": "twosided",
"MT": "above",
}
qc = bv.qc.run_cell_qc(
metrics,
sc.cells_to_keep,
directions=directions,
threshold=3.0,
)
# set the cells to keep
sc.add_obs(pl.DataFrame({"cell_idx": qc.cell_idx, "outlier": qc.combined}))
sc.set_cells_to_keep(qc.cell_idx[~qc.combined])
sc
Load in data: 7.71ms Finished the gene set proportion calculations: 2.04ms
SingleCells( dir='/var/folders/l0/d50zd8812jg94mhtl8t2swfc0000gn/T/tmp72036sbl/batch_cor', cells=7040 (kept 6626), genes=13925, hvg=False, pca=False, embeddings=none, knn=False, graph=False, magic=False, residuals=False, stale=none )
Pre-processing¶
Standard pre-processing: HVG selection, PCA, and neighbour computation.
bv.pp.find_hvg(sc, hvg_no=2000, verbose=True)
bv.pp.calculate_pca(sc, no_pcs=30)
bv.graph.find_neighbours(sc)
HVG (VST): 13_925 genes, 1 batches, 1 disk block(s) HVG (VST): Calculated gene statistics in 9.25ms HVG (VST): Fitted Loess in 560.92µs HVG (VST): Standardised variance in 789.58µs (690 gene(s) re-read for the clip) HVG (VST): Total run time -> 10.64ms Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 12.56ms Sparse PCA: finished the data preparations in 2.05ms
Sparse PCA: finished calculations in 251.09ms Sparse PCA: total run time -> 265.81ms Generating kNN data with kmknn method.
Building kMkNN index ... ... built kMkNN index in 20.66ms Generated KmKnn index: 20.99ms Querying kMkNN index (self) ... ... queried kMkNN index (self) in 15.22ms Queried KmKnn index: 15.26ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 11.06ms
Cell type labels¶
The bio conservation metrics need cell types. We cluster the uncorrected data and annotate the clusters with scType (Ianevski, et al., 2022) on a handful of canonical PBMC markers, same as in the PBMC vignette. The labels are deliberately coarse: T cells stay lumped, since splitting CD4 from CD8 on uncorrected data is asking for trouble.
One caveat: the labels come from uncorrected data, so they carry whatever the batch effect did to the clusters. Good enough to spot a method that smears cell types together, not a ground truth.
bv.graph.find_clusters(sc, res=1.0, name="leiden_uncorrected")
cell_markers = {
"CD3D": "T cells",
"CD3E": "T cells",
"IL7R": "T cells",
"CD8A": "T cells",
"MS4A1": "B cells",
"CD79A": "B cells",
"CD14": "Monocytes",
"LYZ": "Monocytes",
"FCGR3A": "Monocytes",
"GNLY": "NK",
"NKG7": "NK",
"FCER1A": "DC",
"CD1C": "DC",
"PPBP": "Platelets",
}
cell_markers_df = pl.DataFrame(
{"gene_symbol": list(cell_markers), "cell_type": list(cell_markers.values())}
).join(var.select("gene_symbol", "gene_id"), on="gene_symbol")
marker_list = bv.annotate.prepare_cell_markers(sc, cell_markers_df)
sctype_scores = bv.annotate.calc_sc_type_scores(sc, marker_list)
obs = sc.obs(cols=["cell_idx", "leiden_uncorrected"], filtered=True)
cell_type_anno = bv.annotate.score_clusters(
sctype_scores, obs["leiden_uncorrected"].to_numpy()
)
obs = obs.join(
cell_type_anno.select(
pl.col("cluster_id").alias("leiden_uncorrected"), "cell_type"
),
on="leiden_uncorrected",
how="left",
)
sc.add_obs(obs.select("cell_idx", "cell_type"))
obs["cell_type"].value_counts(sort=True)
ScType: 6 cell types, 14 marker genes, 6626 cells ScType: streamed scoring in 501.83µs ScType: total run time -> 753.96µs
| cell_type | count |
|---|---|
| str | u32 |
| "T cells" | 3192 |
| "Monocytes" | 1421 |
| "B cells" | 933 |
| "NK" | 889 |
| "DC" | 163 |
| "Platelets" | 28 |
bv.embed.tsne(sc, params=bv.embed.tsne_defaults().set_params(approx="qd")) # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
tSNE plot (tSNEs usually look prettier... It's anyway not a good way to assess batch effect correction, so at least let's make it pleasant on the eye).
ax = bv.pl.embedding_plot(sc, "tsne", "exp_id", label_by="exp_id", discrete=True)
ax.set_title("Before batch correction")
plt.show()
Each metric has its own function and its own result record. Here's kBET on its own:
kbet = bv.integrate.calculate_kbet(sc, "exp_id")
assert kbet is not None
print(
f"kBET rejection rate: {kbet.kbet_score:.3f} over {kbet.n_batches} batches "
f"(median chi-square {kbet.median_chi_square:.2f})"
)
Running kBET on 6_626 samples kBET rejection rate: 0.987 over 2 batches (median chi-square 8.17)
In practice, you'd want all of them at once.
calculate_integration_metrics() gives one row per call, and we'll stack one
row per method into metrics_df:
metrics_df = bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type"
).with_columns(method=pl.lit("Uncorrected"))
metrics_df
Running kBET on 6_626 samples Running LISI calculations on 6_626 samples. Running silhouette calculations on 5_000 samples.
Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| "pca" | 0.012979 | 0.906355 | 0.0 | null | 1.0 | 0.650836 | 0.950153 | "Uncorrected" |
The high kBET rejection rate (so a low kbet_accept) and a low ilisi
confirm a substantial batch effect, and the tSNE shows clear separation by
batch. pcr_comparison is null here: it compares a corrected embedding
against this PCA, so the PCA has nothing to compare against.
fastMNN¶
fastMNN works on batch-aware HVGs and produces a corrected embedding. It identifies mutual nearest neighbours across batches to compute correction vectors. For it to work best, we need the batch-aware HVGs. We calculate these, provide them to fastMNN (which regenerates the PCA embedding based on the batch-aware HVGs) and then run the actual algorithm.
batch_aware_hvg = bv.pp.find_hvg_batch_aware(sc, "exp_id")
bv.integrate.fast_mnn(sc, "exp_id", batch_aware_hvg.hvg_gene_idx)
bv.graph.find_neighbours(sc, embd_to_use="mnn")
HVG (VST): 13_925 genes, 2 batches, 1 disk block(s) HVG (VST): Calculated gene statistics in 7.00ms HVG (VST): Fitted Loess in 991.62µs HVG (VST): Standardised variance in 1.24ms (1_344 gene(s) re-read for the clip) HVG (VST): Total run time -> 9.26ms
Sparse PCA: Loaded in data in 21.24ms Sparse PCA: finished the data preparations in 5.32ms
Sparse PCA: finished calculations in 272.76ms Sparse PCA: total run time -> 299.44ms Applying cosine normalisation to the dimension reduction. Found 14_507 MNN pairs Merged 2 of 2 batches in 0.06s Generating kNN data with kmknn method.
Building kMkNN index ... ... built kMkNN index in 26.83ms Generated KmKnn index: 27.17ms Querying kMkNN index (self) ... ... queried kMkNN index (self) in 12.03ms Queried KmKnn index: 12.08ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 12.77ms
metrics_df = pl.concat(
[
metrics_df,
bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type", embd_to_use="mnn"
).with_columns(method=pl.lit("fastMNN")),
]
)
metrics_df.filter(pl.col("method") == "fastMNN")
Running kBET on 6_626 samples
Running LISI calculations on 6_626 samples. Running silhouette calculations on 5_000 samples.
Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| "mnn" | 0.685331 | 0.973646 | 0.470588 | 0.959994 | 1.0 | 0.649196 | 0.948964 | "fastMNN" |
We can see clear improvements on the batch side: kBET acceptance, batch ASW and iLISI all go up.
bv.embed.tsne(
sc,
slot_name="tsne_mnn",
use_knn=True,
params=bv.embed.tsne_defaults().set_params(approx="qd"), # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
)
ax = bv.pl.embedding_plot(sc, "tsne_mnn", "exp_id", label_by="exp_id", discrete=True)
ax.set_title("fastMNN batch correction")
plt.show()
Also, visually the batches get mixed now.
Harmony¶
Harmony operates on the PCA embedding directly. Leave k as None and the
number of clusters is set to one per 30 cells, capped at 100. Both versions
are supported and share one numerical core in Rust. They differ in the
diversity penalty, the ridge regression and how the ridge penalty gets set.
The intercept is never penalised in either. bv.integrate.harmony() runs v2;
the original is bv.integrate.harmony_v1(). Let's start with the original:
Version 1¶
bv.integrate.harmony_v1(sc, "exp_id")
bv.graph.find_neighbours(sc, embd_to_use="harmony_v1")
Auto-determined number of Harmony clusters: 100
Harmony: 6_626 cells, 30 dims, 1 variable(s), 100 clusters Variable 0: 2 levels Running initial k-means... Initial data preparation done in 26.74ms Initial objective: 597.8345 === Harmony iteration 1 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 524.9860 Finished iteration in 11.87ms / Total runtime 38.62ms === Harmony iteration 2 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 372.4543 Finished iteration in 9.33ms / Total runtime 47.95ms === Harmony iteration 3 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 363.6414 Finished iteration in 6.93ms / Total runtime 54.89ms === Harmony iteration 4 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 362.3722 Finished iteration in 7.14ms / Total runtime 62.04ms Harmony converged at iteration 4 Finished Harmony 62.04ms Generating kNN data with kmknn method.
Building kMkNN index ... ... built kMkNN index in 22.41ms Generated KmKnn index: 22.76ms Querying kMkNN index (self) ... ... queried kMkNN index (self) in 14.55ms Queried KmKnn index: 14.60ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 14.44ms
Let's calculate the batch correction-related scores.
metrics_df = pl.concat(
[
metrics_df,
bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type", embd_to_use="harmony_v1"
).with_columns(method=pl.lit("Harmony")),
]
)
metrics_df.filter(pl.col("method") == "Harmony")
Running kBET on 6_626 samples
Running LISI calculations on 6_626 samples. Running silhouette calculations on 5_000 samples.
Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| "harmony_v1" | 0.88364 | 0.97534 | 0.642336 | 0.982693 | 1.0 | 0.667803 | 0.949914 | "Harmony" |
Also here, we observe improvements across the board.
bv.embed.tsne(
sc,
slot_name="tsne_harm",
use_knn=True,
params=bv.embed.tsne_defaults().set_params(approx="qd"), # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
)
ax = bv.pl.embedding_plot(sc, "tsne_harm", "exp_id", label_by="exp_id", discrete=True)
ax.set_title("Harmony batch correction")
plt.show()
Version 2¶
Patikas, et al., 2026
changed four things compared to v1. The last three have knobs in
bv.params.HarmonyV2Params:
- Stabilised diversity penalty, which makes the objective scale-invariant.
- Batch pruning in the ridge regression. A batch that puts less than
batch_proportion_cutoffof its cells into a cluster is dropped from that cluster's regression. A cluster left with a single batch isn't corrected, so a cell type unique to one batch doesn't get pulled towards the others. - Dynamic lambda. With
use_dynamic_lambda=True(the default) the ridge penalty is set per cluster asalphatimes the expected counts and the fixedlambda_is ignored. - Theta scaling by batch size via
tau. Off by default (tau=0).
The paper's arrowhead matrix inversion for a single batch covariate is used by both versions here, so v1 gets that speed-up too. v2 also runs only 4 k-means iterations per Harmony round versus 20 in v1. Unless you need to reproduce an old v1 result, go with v2, which is why it is the default.
bv.integrate.harmony(sc, "exp_id")
bv.graph.find_neighbours(sc, embd_to_use="harmony")
# for the cluster comparison in the BBKNN section
bv.graph.find_clusters(sc, res=1.0, name="leiden_harmony")
Auto-determined number of Harmony clusters: 100
Harmony v2: 6_626 cells, 30 dims, 1 variable(s), 100 clusters Variable 0: 2 levels Initial data preparation done in 466.17µs Running initial k-means... Initial objective: 318.7685 === Harmony v2 iteration 1 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 299.8384 Finished iteration in 8.14ms / Total runtime 36.35ms === Harmony v2 iteration 2 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 192.9918 Finished iteration in 8.17ms / Total runtime 44.54ms === Harmony v2 iteration 3 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 120.4948 Finished iteration in 8.22ms / Total runtime 52.78ms === Harmony v2 iteration 4 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 99.1038 Finished iteration in 8.20ms / Total runtime 60.98ms === Harmony v2 iteration 5 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 94.8485 Finished iteration in 7.85ms / Total runtime 68.84ms === Harmony v2 iteration 6 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 92.6682 Finished iteration in 8.13ms / Total runtime 76.97ms === Harmony v2 iteration 7 === Running k-means clustering... Applying ridge regression correction... Harmony objective: 91.7636 Finished iteration in 7.73ms / Total runtime 84.71ms Harmony v2 converged at iteration 7 Finished Harmony 84.71ms Generating kNN data with kmknn method.
Building kMkNN index ... ... built kMkNN index in 20.12ms Generated KmKnn index: 20.47ms Querying kMkNN index (self) ... ... queried kMkNN index (self) in 12.75ms Queried KmKnn index: 12.79ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 13.93ms
Let's calculate the batch correction-related scores.
metrics_df = pl.concat(
[
metrics_df,
bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type", embd_to_use="harmony"
).with_columns(method=pl.lit("Harmony v2")),
]
)
metrics_df.filter(pl.col("method") == "Harmony v2")
Running kBET on 6_626 samples Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| "harmony" | 0.798521 | 0.977848 | 0.642336 | 0.950098 | 1.0 | 0.668443 | 0.949914 | "Harmony v2" |
bv.embed.tsne(
sc,
slot_name="tsne_harm_v2",
use_knn=True,
params=bv.embed.tsne_defaults().set_params(approx="qd"), # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
)
ax = bv.pl.embedding_plot(
sc, "tsne_harm_v2", "exp_id", label_by="exp_id", discrete=True
)
ax.set_title("Harmony (version 2) batch correction")
plt.show()
Got a GPU? bixverse_gpu.integrate.harmony() runs the same v2 algorithm
and stores the result as "harmony_gpu". It takes a single batch column
only.
Seurat CCA¶
Seurat's anchor methods take a different route. For each pair of batches they find cells that are mutual nearest neighbours in a shared space, treat those pairs as anchors, score them by how much of their neighbourhood they share, and then apply a kernel-weighted correction that pulls the query batch onto the reference. Batches get merged in the order of their pairwise anchor counts.
For CCA the shared space is a canonical correlation embedding computed per batch pair. Anchors also get filtered in gene space before scoring, which is CCA-only in Seurat.
A few things differ from Seurat's reference implementation:
- The per-gene
ScaleDatastep is skipped. We work directly from per-cell standardised log-normalised HVG expression. M = X1^T @ X2is never materialised. The canonical correlations come from a matrix-free randomised SVD, so memory stays atO((n1 + n2) * num_cc)rather thann1 x n2. On big batch pairs that is the difference between running and not running.- The correction is applied to the embedding (dims x cells), not to full log-expression.
- The effective CC rank is
max(num_cc, dims), notnum_cc.
Anchor structure comes out close to identical. Like fastMNN, CCA wants batch-aware HVGs, so we reuse the ones computed above.
bv.integrate.seurat_cca(sc, "exp_id", batch_aware_hvg.hvg_gene_idx)
bv.graph.find_neighbours(sc, embd_to_use="cca")
Computing base PCA Sparse PCA: Loaded in data in 17.43ms Sparse PCA: finished the data preparations in 4.36ms
Sparse PCA: finished calculations in 283.37ms Sparse PCA: total run time -> 305.28ms CCA: finding anchors for batch pair (0, 1)
found 3_510 anchors in 266.50ms CCA integration complete in 605.50ms Generating kNN data with kmknn method.
Building kMkNN index ... ... built kMkNN index in 26.59ms Generated KmKnn index: 26.92ms Querying kMkNN index (self) ... ... queried kMkNN index (self) in 12.50ms Queried KmKnn index: 12.54ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 14.34ms
metrics_df = pl.concat(
[
metrics_df,
bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type", embd_to_use="cca"
).with_columns(method=pl.lit("Seurat CCA")),
]
)
metrics_df.filter(pl.col("method") == "Seurat CCA")
Running kBET on 6_626 samples Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| "cca" | 0.757772 | 0.954764 | 0.470588 | 0.977771 | 1.0 | 0.666077 | 0.949778 | "Seurat CCA" |
bv.embed.tsne(
sc,
slot_name="tsne_cca",
use_knn=True,
params=bv.embed.tsne_defaults().set_params(approx="qd"), # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
)
ax = bv.pl.embedding_plot(sc, "tsne_cca", "exp_id", label_by="exp_id", discrete=True)
ax.set_title("Seurat CCA batch correction")
plt.show()
Seurat rPCA¶
rPCA runs the same anchor pipeline on a cheaper shared space. Instead of computing canonical correlations, each batch keeps its own PCA basis and the other batch's expression is projected into it. Cross-batch neighbour queries then happen in these projected bases.
The trade-off is the usual one: rPCA is faster and corrects less aggressively, which makes it the safer choice when your batches share most of their cell types. CCA is the one to reach for when they don't. As in Seurat, no gene-space anchor filter runs here, that step is CCA-only.
bv.integrate.seurat_rpca(sc, "exp_id", batch_aware_hvg.hvg_gene_idx)
bv.graph.find_neighbours(sc, embd_to_use="rpca")
Computing base union PCA Sparse PCA: Loaded in data in 16.26ms Sparse PCA: finished the data preparations in 3.69ms
Sparse PCA: finished calculations in 283.73ms Sparse PCA: total run time -> 303.84ms Loading per-batch standardised HVG expression Computing per-batch PCAs (2 batches) rPCA: finding anchors for batch pair (0, 1) found 1_179 anchors in 95.36ms
rPCA integration complete in 519.17ms Generating kNN data with kmknn method.
Building kMkNN index ... ... built kMkNN index in 21.89ms Generated KmKnn index: 22.24ms Querying kMkNN index (self) ... ... queried kMkNN index (self) in 12.61ms Queried KmKnn index: 12.65ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 13.05ms
metrics_df = pl.concat(
[
metrics_df,
bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type", embd_to_use="rpca"
).with_columns(method=pl.lit("Seurat rPCA")),
]
)
metrics_df.filter(pl.col("method") == "Seurat rPCA")
Running kBET on 6_626 samples Running LISI calculations on 6_626 samples. Running silhouette calculations on 5_000 samples.
Running LISI calculations on 6_626 samples.
Running silhouette calculations on 5_000 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| "rpca" | 0.816179 | 0.966625 | 0.642336 | 0.941192 | 1.0 | 0.663437 | 0.949539 | "Seurat rPCA" |
bv.embed.tsne(
sc,
slot_name="tsne_rpca",
use_knn=True,
params=bv.embed.tsne_defaults().set_params(approx="qd"), # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
)
ax = bv.pl.embedding_plot(sc, "tsne_rpca", "exp_id", label_by="exp_id", discrete=True)
ax.set_title("Seurat rPCA batch correction")
plt.show()
One caveat worth knowing about both anchor methods: on data that already mixes well they have nothing to gain, and the correction can nudge the batch metrics slightly in the wrong direction while still sharpening the cell type structure. Check your uncorrected metrics before reaching for them.
BBKNN¶
BBKNN doesn't touch the embedding. It builds a batch-balanced neighbour graph
instead: neighbours_within_batch nearest neighbours from every batch, turned
into UMAP-style connectivities. Here that's 10 per batch, so 20 neighbours per
cell feed the connectivities. no_neighbours_to_keep only cuts the stored
kNN, and the cut stays balanced: each batch keeps its nearest
ceil(15 / 2) = 8, so a cell ends up with 8 from one batch and 7 from the
other.
That makes BBKNN awkward to score. kBET and iLISI read the stored kNN, which
is balanced by construction, so they come out near perfect whatever the data
looks like. That's the method grading its own homework, not a sign of mixing.
ASW and PCR need an embedding, and BBKNN doesn't have one, so we pass
embd_to_use=None and those come back null. cLISI and graph connectivity are
still fair, since they only look at cell types.
bv.integrate.bbknn(
sc,
"exp_id",
no_neighbours_to_keep=15,
bbknn_params=bv.params.BbknnParams(neighbours_within_batch=10),
)
Running BBKNN algorithm.
BBKNN: generating the batch balanced kNN values. Processing batch 1 / 2: 0 Processing batch 2 / 2: 1 BBKNN: Calculating UMAP-based connectivities and removing weak connections. BBKNN: Finalising data.
/Users/gregorlueg/repos/shared/bixverse-rs/.claude/worktrees/serialized-sniffing-catmull/python/bixverse/_core/integrate.py:548: UserWarning: Prior kNN matrix found. Will be overwritten. _bbknn(
metrics_df = pl.concat(
[
metrics_df,
bv.integrate.calculate_integration_metrics(
sc, "exp_id", cell_type_column="cell_type", embd_to_use=None
).with_columns(method=pl.lit("BBKNN")),
]
)
metrics_df.filter(pl.col("method") == "BBKNN")
Running kBET on 6_626 samples Running LISI calculations on 6_626 samples.
Running LISI calculations on 6_626 samples.
| embedding | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity | method |
|---|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 | str |
| null | 1.0 | null | 0.99115 | null | 1.0 | null | 0.950153 | "BBKNN" |
So what does the graph actually look like? Counting edges is not enough: the connectivities are weighted, and everything downstream (clustering, t-SNE, UMAP) follows the weights. Below: the cross-batch share of the stored kNN, of the graph's edges and of its total weight, plus the median distance from each cell to its nearest neighbour in its own batch and in the other one.
batch = sc.obs(cols=["exp_id"], filtered=True)["exp_id"].to_numpy()
knn_idx = sc.cache.knn.indices
knn_dist = sc.cache.knn.distances
other_batch = batch[knn_idx] != batch[:, None]
conn = sc.cache.connectivities
edge_rows = np.repeat(np.arange(conn.shape[0]), np.diff(conn.indptr))
cross_edge = batch[edge_rows] != batch[conn.indices]
pl.DataFrame(
{
"cross_batch_share_knn": [other_batch.mean()],
"cross_batch_share_edges": [cross_edge.mean()],
"cross_batch_share_weight": [conn.data[cross_edge].sum() / conn.data.sum()],
"median_nearest_same_batch": [
np.median(np.where(other_batch, np.inf, knn_dist).min(axis=1))
],
"median_nearest_other_batch": [
np.median(np.where(other_batch, knn_dist, np.inf).min(axis=1))
],
}
)
| cross_batch_share_knn | cross_batch_share_edges | cross_batch_share_weight | median_nearest_same_batch | median_nearest_other_batch |
|---|---|---|---|---|
| f64 | f64 | f32 | f32 | f32 |
| 0.466838 | 0.507216 | 0.037278 | 4.548621 | 8.465794 |
Half the edges cross batches, but they carry only a few percent of the weight.
The nearest cell in the other batch sits about twice as far away as the
nearest one in the cell's own batch, and the UMAP connectivities decay with
distance, so the cross-batch edges end up with next to no weight. This isn't
an implementation detail: the reference bbknn package behaves the same on
this data. pbmc3k and pbmc4k were run on different 10x chemistries (GemCode
v1 against v2), and that gap is real.
The fair test of a graph method is clustering, so let's run Leiden on the BBKNN graph and compare its clusters against the uncorrected ones and Harmony v2. A cluster where one batch holds more than 90% of the cells counts as unmixed.
bv.graph.find_clusters(sc, res=1.0, name="leiden_bbknn")
# share of a cluster's cells from one batch above which it counts as unmixed
SINGLE_BATCH_CUTOFF = 0.9
def batch_composition(cluster_col: str) -> pl.DataFrame:
obs = sc.obs(cols=[cluster_col, "exp_id"], filtered=True)
per_cluster = (
obs.group_by(cluster_col, "exp_id")
.len()
.with_columns(share=pl.col("len") / pl.col("len").sum().over(cluster_col))
.group_by(cluster_col)
.agg(n_cells=pl.col("len").sum(), top_batch_share=pl.col("share").max())
)
unmixed = per_cluster.filter(pl.col("top_batch_share") > SINGLE_BATCH_CUTOFF)
return pl.DataFrame(
{
"clustering": [cluster_col],
"n_clusters": [per_cluster.height],
"n_unmixed_clusters": [unmixed.height],
"share_cells_in_unmixed": [unmixed["n_cells"].sum() / obs.height],
}
)
pl.concat(
[
batch_composition(col)
for col in ["leiden_uncorrected", "leiden_harmony", "leiden_bbknn"]
]
)
| clustering | n_clusters | n_unmixed_clusters | share_cells_in_unmixed |
|---|---|---|---|
| str | i64 | i64 | f64 |
| "leiden_uncorrected" | 22 | 17 | 0.971174 |
| "leiden_harmony" | 15 | 1 | 0.004528 |
| "leiden_bbknn" | 18 | 13 | 0.877 |
bv.embed.tsne(sc, use_knn=True, params=bv.embed.tsne_defaults().set_params(approx="qd")) # ty: ignore[invalid-argument-type] # manifolds-rs types set_params as BaseEmbedding
ax = bv.pl.embedding_plot(sc, "tsne", "exp_id", label_by="exp_id", discrete=True)
ax.set_title("BBKNN batch correction")
plt.show()
The clusters and the t-SNE tell the same story. Leiden on the BBKNN graph still puts most cells into single-batch clusters, barely better than doing nothing, while Harmony mixes nearly all of them. The t-SNE follows the same weights and stays split by batch. On a batch effect this strong, BBKNN on its own doesn't correct much. It's built for milder effects, where the cross-batch neighbours sit close enough to keep real weight.
Choosing a method¶
Here's everything side by side:
batch_metrics = ["kbet_accept", "batch_asw", "ilisi", "pcr_comparison"]
bio_metrics = ["clisi", "cell_type_asw", "graph_connectivity"]
metrics_df.select("method", *batch_metrics, *bio_metrics)
| method | kbet_accept | batch_asw | ilisi | pcr_comparison | clisi | cell_type_asw | graph_connectivity |
|---|---|---|---|---|---|---|---|
| str | f64 | f64 | f64 | f64 | f64 | f64 | f64 |
| "Uncorrected" | 0.012979 | 0.906355 | 0.0 | null | 1.0 | 0.650836 | 0.950153 |
| "fastMNN" | 0.685331 | 0.973646 | 0.470588 | 0.959994 | 1.0 | 0.649196 | 0.948964 |
| "Harmony" | 0.88364 | 0.97534 | 0.642336 | 0.982693 | 1.0 | 0.667803 | 0.949914 |
| "Harmony v2" | 0.798521 | 0.977848 | 0.642336 | 0.950098 | 1.0 | 0.668443 | 0.949914 |
| "Seurat CCA" | 0.757772 | 0.954764 | 0.470588 | 0.977771 | 1.0 | 0.666077 | 0.949778 |
| "Seurat rPCA" | 0.816179 | 0.966625 | 0.642336 | 0.941192 | 1.0 | 0.663437 | 0.949539 |
| "BBKNN" | 1.0 | null | 0.99115 | null | 1.0 | null | 0.950153 |
fig, axes = plt.subplots(
1,
2,
figsize=(9, 5),
sharey=True,
layout="constrained",
gridspec_kw={"width_ratios": [len(batch_metrics), len(bio_metrics)]},
)
methods = metrics_df["method"].to_list()
cmap = plt.get_cmap("Blues").with_extremes(bad="0.9")
# BBKNN's kBET and iLISI read its own balanced kNN, so they are greyed out
plot_df = metrics_df.with_columns(
pl.when(pl.col("method") == "BBKNN").then(None).otherwise(pl.col(col)).alias(col)
for col in ["kbet_accept", "ilisi"]
)
for ax, cols, title in zip(
axes,
[batch_metrics, bio_metrics],
["Batch mixing", "Bio conservation"],
strict=True,
):
scores = plot_df.select(cols).to_numpy().astype(np.float64)
im = ax.imshow(np.ma.masked_invalid(scores), cmap=cmap, vmin=0, vmax=1)
for i, j in np.ndindex(scores.shape):
if not np.isnan(scores[i, j]):
ax.text(
j,
i,
f"{scores[i, j]:.2f}",
ha="center",
va="center",
fontsize=8,
color="white" if scores[i, j] > 0.6 else "black",
)
ax.set_xticks(range(len(cols)), cols, rotation=45, ha="right")
ax.set_yticks(range(len(methods)), methods)
ax.set_title(title)
fig.colorbar(im, ax=axes, label="Sc metrics", shrink=0.6)
plt.show()
Read the two panels together. A method that tops batch mixing but drops on bio conservation has merged cell types along with the batches, which is worse than doing nothing.
Two of these tell you little on this data set. cLISI sits at 1 for every method: six coarse, well separated cell types keep their neighbourhoods pure no matter what. Batch ASW barely moves either, since two PBMC batches of the same tissue never separate much on silhouette to begin with. Both earn their keep on finer labels and nastier batch effects. On two PBMC runs, kBET, iLISI and PCR do the talking. BBKNN's row is greyed out on the batch side: its kBET and iLISI in the table above are the artefact from its section, not a win.
There is no universally best batch correction method. Some practical guidance:
- Harmony is a good default. It is fast, operates on the PCA embedding directly, and handles multiple batch variables simultaneously. It tends to work well when batch effects are moderate and cell type composition is broadly similar across batches. Version 2 prunes batches per cluster and sets the ridge penalty dynamically. On this data set it is not an improvement: v1 is ahead on kBET acceptance and PCR, the rest is tied, and v2 is only modestly faster (46 ms against 51 ms).
- fastMNN is worth considering when cell type composition differs substantially across batches. The mutual nearest neighbour approach is less sensitive to this because it only aligns cells that have a plausible biological match in another batch.
- Seurat CCA and rPCA are the anchor-based options. Like fastMNN they only align cells with a plausible match in another batch, so they hold up when cell type composition differs. They are the heaviest of the five though, and on data that already mixes well they can make the batch metrics slightly worse. rPCA is the cheaper and gentler of the two and a sensible default; go to CCA when the batches genuinely don't share their cell types.
- BBKNN leaves the embedding alone and hands a batch-balanced graph straight to clustering. It's the lightest touch of the lot, and that cuts both ways: when the batches sit far apart, as here, the cross-batch edges carry next to no weight and the clusters stay split by batch. Judge it on clustering, not on kBET or iLISI.
In practice, running multiple methods and looking at quantitative metrics is the most reliable approach. No single metric captures everything: kBET and PCR only see batch, cLISI and cell type ASW only see biology, and the kNN metrics can't score a method that builds the kNN to look balanced. UMAPs and tSNE assessments should be taken with a big pinch of salt, see Chari, et al. Admittedly, they do look pretty however.
Clean up¶
tempdir_batch_cor.cleanup()