Skip to main content

Batch-effect harmonization for machine learning frameworks.

Project description

combatlearn

Python versions Test PyPI Downloads 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.

Three methods:

  • method="johnson" - classic ComBat (Johnson et al., 2007)
  • method="fortin" - covariate-aware ComBat (Fortin et al., 2018)
  • method="chen" - CovBat (Chen et al., 2022)

Installation

pip install combatlearn

Quick start

import pandas as pd
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
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=True)
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.

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" and "chen" methods.
continuous_covariates array-like, pd.Series or pd.DataFrame None Optional continuous covariates (e.g., age). Only used in "fortin" and "chen" methods.

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)
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 $]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.

Contributing

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

Author

Ettore Rocchi @ University of Bologna

Google Scholar $\cdot$ Scopus

Acknowledgements

This project builds on the excellent work of the ComBat family of harmonisation methods. We gratefully acknowledge:

Citation

If combatlearn is useful in your research, please cite the original papers:

  • Johnson WE, Li C, Rabinovic A. Adjusting batch effects in microarray expression data using empirical Bayes methods. Biostatistics. 2007 Jan;8(1):118-27. doi: 10.1093/biostatistics/kxj037

  • Fortin JP, Cullen N, Sheline YI, Taylor WD, Aselcioglu I, Cook PA, Adams P, Cooper C, Fava M, McGrath PJ, McInnis M, Phillips ML, Trivedi MH, Weissman MM, Shinohara RT. Harmonization of cortical thickness measurements across scanners and sites. Neuroimage. 2018 Feb 15;167:104-120. doi: 10.1016/j.neuroimage.2017.11.024

  • Chen AA, Beer JC, Tustison NJ, Cook PA, Shinohara RT, Shou H; Alzheimer's Disease Neuroimaging Initiative. Mitigating site effects in covariance for machine learning in neuroimaging data. Hum Brain Mapp. 2022 Mar;43(4):1179-1195. doi: 10.1002/hbm.25688

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-0.1.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

combatlearn-0.1.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: combatlearn-0.1.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for combatlearn-0.1.1.tar.gz
Algorithm Hash digest
SHA256 77f45a00860f4b329772ba35757eb1eb1d0cbc66504ff9fa907ac8ada7e05260
MD5 0977ce3a2e557952d2a412e6b9534750
BLAKE2b-256 4d88e92c2cfdd7020fa48c53334427963369066be1c8fd7d5816baa105420623

See more details on using hashes here.

File details

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

File metadata

  • Download URL: combatlearn-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for combatlearn-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2615f1800e84d273ca649f51d6eaa7866cd3294c2fb0a9279b513f25df0d9e5
MD5 f99c121c19670ef87ce4e281e8468bb3
BLAKE2b-256 18985f2c21f25e411f21d9a6a5eb48e335e6b8d0d9436e4c4e9b0c01823d95b8

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