Skip to main content

Batch-effect harmonization for machine learning frameworks.

Project description

combatlearn

Python versions Test Documentation PyPI Version License

combatlearn logo

combatlearn makes the popular ComBat (and CovBat) batch-effect correction algorithm available for use into machine learning frameworks. It lets you harmonise high-dimensional data inside a scikit-learn Pipeline, so that cross-validation and grid-search automatically take batch structure into account, without data leakage.

Four methods:

  • method="johnson" - classic ComBat (Johnson et al., 2007)
  • method="fortin" - neuroComBat (Fortin et al., 2018)
  • method="chen" - CovBat (Chen et al., 2022)
  • method="longitudinal" - Longitudinal ComBat for repeated measures (Beer et al., 2020)

Installation

pip install combatlearn

Documentation

Full documentation is available at combatlearn.readthedocs.io

The documentation includes:

Quick start

import pandas as pd
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from combatlearn import ComBat

df = pd.read_csv("data.csv", index_col=0)
X, y = df.drop(columns="y"), df["y"]

batch = pd.read_csv("batch.csv", index_col=0).squeeze("columns")
diag = pd.read_csv("diagnosis.csv", index_col=0) # categorical
age = pd.read_csv("age.csv", index_col=0) # continuous

pipe = Pipeline([
    ("combat", ComBat(
        batch=batch,
        discrete_covariates=diag,
        continuous_covariates=age,
        method="fortin", # or "johnson" or "chen"
        parametric=True
    )),
    ("scaler", StandardScaler()),
    ("clf", LogisticRegression())
])

param_grid = {
    "combat__mean_only": [True, False],
    "clf__C": [0.01, 0.1, 1, 10],
}

grid = GridSearchCV(
    estimator=pipe,
    param_grid=param_grid,
    cv=5,
    scoring="roc_auc",
)

grid.fit(X, y)

print("Best parameters:", grid.best_params_)
print(f"Best CV AUROC: {grid.best_score_:.3f}")

For a full example of how to use combatlearn see the notebook demo

ComBat parameters

The following section provides a detailed explanation of all parameters available in the scikit-learn-compatible ComBat class. For complete API documentation, see the API Reference.

Main Parameters

Parameter Type Default Description
batch array-like or pd.Series required Vector indicating batch assignment for each sample. This is used to estimate and remove batch effects.
discrete_covariates array-like, pd.Series, or pd.DataFrame None Optional categorical covariates (e.g., sex, site). Only used in "fortin", "chen", and "longitudinal" methods.
continuous_covariates array-like, pd.Series or pd.DataFrame None Optional continuous covariates (e.g., age). Only used in "fortin", "chen", and "longitudinal" methods.
subject_id array-like or pd.Series None Subject/individual labels for the random intercept. Required for method="longitudinal", ignored otherwise.
time_covariate array-like or pd.Series None Optional continuous time variable for repeated measures. Only used in "longitudinal".

Algorithm Options

Parameter Type Default Description
method str "johnson" ComBat method to use:
  • "johnson" - Classical ComBat (Johnson et al. 2007)
  • "fortin" - ComBat with covariates (Fortin et al. 2018)
  • "chen" - CovBat, PCA-based correction (Chen et al. 2022)
  • "longitudinal" - Longitudinal ComBat with a per-subject random intercept (Beer et al. 2020)
parametric bool True Whether to use the parametric empirical Bayes formulation. If False, a non-parametric iterative scheme is used.
mean_only bool False If True, only the mean is corrected, while variances are left unchanged. Useful for preserving variance structure in the data.
reference_batch str or None None If specified, acts as a reference batch - other batches will be corrected to match this one.
covbat_cov_thresh float, int 0.9 For "chen" method only: Cumulative variance threshold in (0, 1] to retain PCs in PCA space (e.g., 0.9 = retain 90% explained variance). If an integer is provided, it represents the number of principal components to use.
eps float 1e-8 Small jitter value added to variances to prevent divide-by-zero errors during standardization.

Batch Effect Correction Visualization

The plot_transformation method allows to visualize the ComBat transformation effect using dimensionality reduction, showing the before/after comparison of data transformed by ComBat using PCA, t-SNE, or UMAP to reduce dimensions for visualization.

For further details see the API Reference and the notebook demo.

Batch Effect Metrics

The compute_batch_metrics method provides quantitative assessment of batch correction quality. It computes metrics including Silhouette coefficient, Davies-Bouldin index, kBET, LISI, and variance ratio for batch effect quantification, as well as k-NN preservation and distance correlation for structure preservation.

For further details see the API Reference and the notebook demo.

Contributing

Pull requests, bug reports, and feature ideas are welcome: feel free to open a PR!

Author

Ettore Rocchi @ University of Bologna

Google Scholar | Scopus

Citation

If combatlearn is useful in your research, please cite the paper introducing this Python package:

Rocchi, E., Nicitra, E., Calvo, M. et al. Combining mass spectrometry and machine learning models for predicting Klebsiella pneumoniae antimicrobial resistance: a multicenter experience from clinical isolates in Italy. BMC Microbiol (2026). https://doi.org/10.1186/s12866-025-04657-2

@article{Rocchi2026,
  author    = {Rocchi, Ettore and Nicitra, Emanuele and Calvo, Maddalena and Cento, Valeria and Peiretti, Laura and Asif, Zian and Menchinelli, Giulia and Posteraro, Brunella and Sala, Claudia and Colosimo, Claudia and Cricca, Monica and Sambri, Vittorio and Sanguinetti, Maurizio and Castellani, Gastone and Stefani, Stefania},
  title     = {Combining mass spectrometry and machine learning models for predicting Klebsiella pneumoniae antimicrobial resistance: a multicenter experience from clinical isolates in Italy},
  journal   = {BMC Microbiology},
  year      = {2026},
  doi       = {10.1186/s12866-025-04657-2},
  url       = {https://doi.org/10.1186/s12866-025-04657-2}
}

Acknowledgements

This project builds on the excellent work of the ComBat family of harmonisation methods. Please consider citing the original papers:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

combatlearn-2.1.0.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

combatlearn-2.1.0-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

Details for the file combatlearn-2.1.0.tar.gz.

File metadata

  • Download URL: combatlearn-2.1.0.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for combatlearn-2.1.0.tar.gz
Algorithm Hash digest
SHA256 c5064d72a9efef7e1c745fbbde58ed6eaa5cd9083908f04f7022306ca8d24dcb
MD5 ca557be9a81a6379537687fcceee25f1
BLAKE2b-256 1255ce39b277e0822fe4940e8a9f2f1608d32fa7d9ea859e857bc733c7c9eb71

See more details on using hashes here.

File details

Details for the file combatlearn-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: combatlearn-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for combatlearn-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 005afbb988379afb8698538b8d1fef4803d392384b60f42e7b4675db36531283
MD5 fa4578aaba18a1fab52d2003b35967b1
BLAKE2b-256 86de8024eda3779f89057a8744676f783c37fd792afed2f47c301d2d94b6941d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page