Repository navigation
Backport categorical ComBat reference coding to 1.12.x #4496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
flying-sheep
merged 3 commits into
scverse:1.12.x
from
sylvesterkaczmarek:backport/4454-combat-categorical-1.12
Nov 10, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix {func}`~scanpy.pp.combat` with categorical `covariates`, which produced a singular design matrix and either raised or silently returned wrong values {smaller}`L Heumos` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from itertools import product | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| import pytest | ||
| from anndata import AnnData | ||
| from scipy.sparse import csr_matrix # noqa: TID251 | ||
|
|
||
| import scanpy as sc | ||
| from scanpy.preprocessing._combat import _design_matrix | ||
|
|
||
|
|
||
| def _categorical_example(n_covariates): | ||
| combinations = list(product(["batch_a", "batch_b"], ["a", "b", "c"], ["x", "y"])) | ||
| obs = pd.DataFrame(combinations * 4, columns=["batch", "condition", "donor"]) | ||
| obs.index = obs.index.astype(str) | ||
| for key in obs: | ||
| obs[key] = obs[key].astype("category") | ||
| rng = np.random.default_rng(4454) | ||
| obs["age"] = rng.normal(size=len(obs)) | ||
| x = rng.normal(size=(len(obs), 24)) | ||
| x += obs["batch"].cat.codes.to_numpy()[:, None] * np.linspace(0.5, 2, 24) | ||
| x += obs["condition"].cat.codes.to_numpy()[:, None] * np.linspace(0.3, 1, 24) | ||
| categories = ["condition", "donor"][:n_covariates] | ||
| numeric = pd.concat( | ||
| [ | ||
| obs[["batch", "age"]], | ||
| pd.get_dummies(obs[categories], drop_first=True, dtype=float), | ||
| ], | ||
| axis=1, | ||
| ) | ||
| return AnnData(x, obs=obs), numeric, categories | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("n_covariates", [1, 2]) | ||
| def test_categorical_design_is_full_rank(n_covariates): | ||
| adata, numeric, categories = _categorical_example(n_covariates) | ||
| model = adata.obs[["batch", *categories, "age"]] | ||
| levels = adata.obs["batch"].cat.categories | ||
| design = _design_matrix(model, "batch", levels) | ||
| expected = _design_matrix(numeric, "batch", levels) | ||
| assert design.shape == expected.shape | ||
| assert np.linalg.matrix_rank(design) == design.shape[1] | ||
| expected_columns = [*expected.columns[:2], *numeric.columns[2:], "age"] | ||
| np.testing.assert_allclose(design.to_numpy(), expected[expected_columns].to_numpy()) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("n_covariates", [1, 2]) | ||
| @pytest.mark.parametrize("sparse", [False, True]) | ||
| def test_categorical_combat_matches_reference_coding(n_covariates, sparse): | ||
| adata, numeric, categories = _categorical_example(n_covariates) | ||
| x = adata.X.copy() | ||
| if sparse: | ||
| adata.X = csr_matrix(adata.X) | ||
| reference = AnnData(x.copy(), obs=numeric) | ||
| expected = sc.pp.combat( | ||
| reference, key="batch", covariates=list(numeric.columns[1:]), inplace=False | ||
| ) | ||
| actual = sc.pp.combat( | ||
| adata, key="batch", covariates=[*categories, "age"], inplace=False | ||
| ) | ||
| assert np.isfinite(actual).all() | ||
| np.testing.assert_allclose(actual, expected, rtol=1e-10, atol=1e-10) | ||
| np.testing.assert_array_equal(adata.X.toarray() if sparse else adata.X, x) | ||
| sc.pp.combat(adata, key="batch", covariates=[*categories, "age"]) | ||
| np.testing.assert_allclose(adata.X, expected, rtol=1e-10, atol=1e-10) | ||
|
|
||
|
|
||
| def test_single_level_categorical_covariate_matches_batch_only(): | ||
| adata, _, _ = _categorical_example(1) | ||
| adata.obs["constant"] = pd.Categorical(["same"] * adata.n_obs) | ||
| expected = sc.pp.combat(adata, key="batch", inplace=False) | ||
| actual = sc.pp.combat(adata, key="batch", covariates=["constant"], inplace=False) | ||
| np.testing.assert_allclose(actual, expected, rtol=1e-10, atol=1e-10) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.