combatlearn


Namecombatlearn JSON
Version 0.2.1 PyPI version JSON
download
home_pageNone
SummaryBatch-effect harmonization for machine learning frameworks.
upload_time2025-07-21 10:29:46
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords machine-learning harmonization combat preprocessing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # **combatlearn**

[![Python versions](https://img.shields.io/badge/python-%3E%3D3.10-blue?logo=python)](https://www.python.org/)
[![Test](https://github.com/EttoreRocchi/combatlearn/actions/workflows/test.yaml/badge.svg)](https://github.com/EttoreRocchi/combatlearn/actions/workflows/test.yaml)
[![PyPI Downloads](https://static.pepy.tech/badge/combatlearn)](https://pepy.tech/projects/combatlearn)
[![PyPI Version](https://img.shields.io/pypi/v/combatlearn?cacheSeconds=300)](https://pypi.org/project/combatlearn/)
[![License](https://img.shields.io/github/license/EttoreRocchi/combatlearn)](https://github.com/EttoreRocchi/combatlearn/blob/main/LICENSE)

<div align="center">
<p><img src="https://raw.githubusercontent.com/EttoreRocchi/combatlearn/main/docs/logo.png" alt="combatlearn logo" width="350" /></p>
</div>

**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"` - neuroComBat (Fortin _et al._, 2018)
- `method="chen"` - CovBat (Chen _et al._, 2022)

## Installation

```bash
pip install combatlearn
```

## Quick start

```python
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](https://github.com/EttoreRocchi/combatlearn/blob/main/docs/demo/combatlearn_demo.ipynb)

## `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: <ul><li>`"johnson"` - Classical ComBat (_Johnson et al. 2007_)</li><li>`"fortin"` - ComBat with covariates (_Fortin et al. 2018_)</li><li>`"chen"` - CovBat, PCA-based correction (_Chen et al. 2022_)</li></ul> |
| `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. |


### 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 [notebook demo](https://github.com/EttoreRocchi/combatlearn/blob/main/docs/demo/combatlearn_demo.ipynb).

## Contributing

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

## Author

[**Ettore Rocchi**](https://github.com/ettorerocchi) @ University of Bologna

[Google Scholar](https://scholar.google.com/citations?user=MKHoGnQAAAAJ) | [Scopus](https://www.scopus.com/authid/detail.uri?authorId=57220152522)

## Acknowledgements

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

- [**ComBat**](https://rdrr.io/bioc/sva/man/ComBat.html)
- [**neuroCombat**](https://github.com/Jfortin1/neuroCombat)
- [**CovBat**](https://github.com/andy1764/CovBat_Harmonization)

## 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](https://doi.org/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](https://doi.org/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](https://doi.org/10.1002/hbm.25688)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "combatlearn",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "machine-learning, harmonization, combat, preprocessing",
    "author": null,
    "author_email": "Ettore Rocchi <ettoreroc@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/94/3a/7e7b4a2df7d938fcf4b280220e4d1acc3e6d2a81f0ab12614378e35415fa/combatlearn-0.2.1.tar.gz",
    "platform": null,
    "description": "# **combatlearn**\n\n[![Python versions](https://img.shields.io/badge/python-%3E%3D3.10-blue?logo=python)](https://www.python.org/)\n[![Test](https://github.com/EttoreRocchi/combatlearn/actions/workflows/test.yaml/badge.svg)](https://github.com/EttoreRocchi/combatlearn/actions/workflows/test.yaml)\n[![PyPI Downloads](https://static.pepy.tech/badge/combatlearn)](https://pepy.tech/projects/combatlearn)\n[![PyPI Version](https://img.shields.io/pypi/v/combatlearn?cacheSeconds=300)](https://pypi.org/project/combatlearn/)\n[![License](https://img.shields.io/github/license/EttoreRocchi/combatlearn)](https://github.com/EttoreRocchi/combatlearn/blob/main/LICENSE)\n\n<div align=\"center\">\n<p><img src=\"https://raw.githubusercontent.com/EttoreRocchi/combatlearn/main/docs/logo.png\" alt=\"combatlearn logo\" width=\"350\" /></p>\n</div>\n\n**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**.\n\n**Three methods**:\n- `method=\"johnson\"` - classic ComBat (Johnson _et al._, 2007)\n- `method=\"fortin\"` - neuroComBat (Fortin _et al._, 2018)\n- `method=\"chen\"` - CovBat (Chen _et al._, 2022)\n\n## Installation\n\n```bash\npip install combatlearn\n```\n\n## Quick start\n\n```python\nimport pandas as pd\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom combatlearn import ComBat\n\ndf = pd.read_csv(\"data.csv\", index_col=0)\nX, y = df.drop(columns=\"y\"), df[\"y\"]\n\nbatch = pd.read_csv(\"batch.csv\", index_col=0, squeeze=True)\ndiag = pd.read_csv(\"diagnosis.csv\", index_col=0) # categorical\nage = pd.read_csv(\"age.csv\", index_col=0) # continuous\n\npipe = Pipeline([\n    (\"combat\", ComBat(\n        batch=batch,\n        discrete_covariates=diag,\n        continuous_covariates=age,\n        method=\"fortin\", # or \"johnson\" or \"chen\"\n        parametric=True\n    )),\n    (\"scaler\", StandardScaler()),\n    (\"clf\", LogisticRegression())\n])\n\nparam_grid = {\n    \"combat__mean_only\": [True, False],\n    \"clf__C\": [0.01, 0.1, 1, 10],\n}\n\ngrid = GridSearchCV(\n    estimator=pipe,\n    param_grid=param_grid,\n    cv=5,\n    scoring=\"roc_auc\",\n)\n\ngrid.fit(X, y)\n\nprint(\"Best parameters:\", grid.best_params_)\nprint(f\"Best CV AUROC: {grid.best_score_:.3f}\")\n```\n\nFor a full example of how to use **combatlearn** see the [notebook demo](https://github.com/EttoreRocchi/combatlearn/blob/main/docs/demo/combatlearn_demo.ipynb)\n\n## `ComBat` parameters\n\nThe following section provides a detailed explanation of all parameters available in the scikit-learn-compatible `ComBat` class.\n\n### Main Parameters\n\n| Parameter | Type | Default | Description |\n| --- | ---  | --- | --- |\n| `batch` | array-like or pd.Series | **required** | Vector indicating batch assignment for each sample. This is used to estimate and remove batch effects. |\n| `discrete_covariates` | array-like, pd.Series, or pd.DataFrame | `None` | Optional categorical covariates (e.g., sex, site). Only used in `\"fortin\"` and `\"chen\"` methods. |\n| `continuous_covariates` | array-like, pd.Series or pd.DataFrame | `None` | Optional continuous covariates (e.g., age). Only used in `\"fortin\"` and `\"chen\"` methods. |\n\n### Algorithm Options\n\n| Parameter | Type | Default | Description |\n| --- | --- | --- | --- |\n| `method` | str | `\"johnson\"` | ComBat method to use: <ul><li>`\"johnson\"` - Classical ComBat (_Johnson et al. 2007_)</li><li>`\"fortin\"` - ComBat with covariates (_Fortin et al. 2018_)</li><li>`\"chen\"` - CovBat, PCA-based correction (_Chen et al. 2022_)</li></ul> |\n| `parametric` | bool | `True` | Whether to use the **parametric empirical Bayes** formulation. If `False`, a non-parametric iterative scheme is used. |\n| `mean_only` | bool | `False` | If `True`, only the **mean** is corrected, while variances are left unchanged. Useful for preserving variance structure in the data. |\n| `reference_batch` | str or `None` | `None` | If specified, acts as a reference batch - other batches will be corrected to match this one. |\n| `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. |\n| `eps` | float | `1e-8` | Small jitter value added to variances to prevent divide-by-zero errors during standardization. |\n\n\n### Batch Effect Correction Visualization \n\nThe `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.\n\nFor further details see the [notebook demo](https://github.com/EttoreRocchi/combatlearn/blob/main/docs/demo/combatlearn_demo.ipynb).\n\n## Contributing\n\nPull requests, bug reports, and feature ideas are welcome: feel free to open a PR!\n\n## Author\n\n[**Ettore Rocchi**](https://github.com/ettorerocchi) @ University of Bologna\n\n[Google Scholar](https://scholar.google.com/citations?user=MKHoGnQAAAAJ) | [Scopus](https://www.scopus.com/authid/detail.uri?authorId=57220152522)\n\n## Acknowledgements\n\nThis project builds on the excellent work of the ComBat family of harmonisation methods.\nWe gratefully acknowledge:\n\n- [**ComBat**](https://rdrr.io/bioc/sva/man/ComBat.html)\n- [**neuroCombat**](https://github.com/Jfortin1/neuroCombat)\n- [**CovBat**](https://github.com/andy1764/CovBat_Harmonization)\n\n## Citation\n\nIf **combatlearn** is useful in your research, please cite the original\npapers:\n\n- 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](https://doi.org/10.1093/biostatistics/kxj037)\n\n- 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](https://doi.org/10.1016/j.neuroimage.2017.11.024)\n\n- 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](https://doi.org/10.1002/hbm.25688)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Batch-effect harmonization for machine learning frameworks.",
    "version": "0.2.1",
    "project_urls": null,
    "split_keywords": [
        "machine-learning",
        " harmonization",
        " combat",
        " preprocessing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f45d793d526182be438c0b358597d0bbe6d751416b33f6f30e101b0382cae4c9",
                "md5": "8dd00ed7d16c56ef3912fed9cbc0da4b",
                "sha256": "32fa4b96c9db8d2c3504b7d3bac7157d35b00eb61e0f431c69e19e9765088bbe"
            },
            "downloads": -1,
            "filename": "combatlearn-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8dd00ed7d16c56ef3912fed9cbc0da4b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 12933,
            "upload_time": "2025-07-21T10:29:45",
            "upload_time_iso_8601": "2025-07-21T10:29:45.037808Z",
            "url": "https://files.pythonhosted.org/packages/f4/5d/793d526182be438c0b358597d0bbe6d751416b33f6f30e101b0382cae4c9/combatlearn-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "943a7e7b4a2df7d938fcf4b280220e4d1acc3e6d2a81f0ab12614378e35415fa",
                "md5": "bba6a67d95af78580782ef100513141c",
                "sha256": "ca0aa8abd7912cde2136a555714db11a1997b6e20158aba2f10837bef6562f2f"
            },
            "downloads": -1,
            "filename": "combatlearn-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "bba6a67d95af78580782ef100513141c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 17002,
            "upload_time": "2025-07-21T10:29:46",
            "upload_time_iso_8601": "2025-07-21T10:29:46.050431Z",
            "url": "https://files.pythonhosted.org/packages/94/3a/7e7b4a2df7d938fcf4b280220e4d1acc3e6d2a81f0ab12614378e35415fa/combatlearn-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-21 10:29:46",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "combatlearn"
}
        
Elapsed time: 1.50555s