combatlearn 0.2.1__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- combatlearn/__init__.py +4 -3
- combatlearn/combat.py +88 -61
- {combatlearn-0.2.1.dist-info → combatlearn-1.0.0.dist-info}/METADATA +24 -3
- combatlearn-1.0.0.dist-info/RECORD +7 -0
- combatlearn-0.2.1.dist-info/RECORD +0 -7
- {combatlearn-0.2.1.dist-info → combatlearn-1.0.0.dist-info}/WHEEL +0 -0
- {combatlearn-0.2.1.dist-info → combatlearn-1.0.0.dist-info}/licenses/LICENSE +0 -0
- {combatlearn-0.2.1.dist-info → combatlearn-1.0.0.dist-info}/top_level.txt +0 -0
combatlearn/__init__.py
CHANGED
combatlearn/combat.py
CHANGED
|
@@ -14,28 +14,17 @@ import numpy as np
|
|
|
14
14
|
import numpy.linalg as la
|
|
15
15
|
import pandas as pd
|
|
16
16
|
from sklearn.base import BaseEstimator, TransformerMixin
|
|
17
|
-
from sklearn.utils.validation import check_is_fitted
|
|
18
17
|
from sklearn.decomposition import PCA
|
|
19
18
|
from sklearn.manifold import TSNE
|
|
19
|
+
import matplotlib
|
|
20
20
|
import matplotlib.pyplot as plt
|
|
21
|
-
|
|
21
|
+
import matplotlib.colors as mcolors
|
|
22
|
+
from typing import Literal, Optional, Union, Dict, Tuple, Any
|
|
22
23
|
import numpy.typing as npt
|
|
23
24
|
import warnings
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
UMAP_AVAILABLE = True
|
|
28
|
-
except ImportError:
|
|
29
|
-
UMAP_AVAILABLE = False
|
|
30
|
-
|
|
31
|
-
try:
|
|
32
|
-
import plotly.graph_objects as go
|
|
33
|
-
from plotly.subplots import make_subplots
|
|
34
|
-
PLOTLY_AVAILABLE = True
|
|
35
|
-
except ImportError:
|
|
36
|
-
PLOTLY_AVAILABLE = False
|
|
37
|
-
|
|
38
|
-
__author__ = "Ettore Rocchi"
|
|
25
|
+
import umap
|
|
26
|
+
import plotly.graph_objects as go
|
|
27
|
+
from plotly.subplots import make_subplots
|
|
39
28
|
|
|
40
29
|
ArrayLike = Union[pd.DataFrame, pd.Series, npt.NDArray[Any]]
|
|
41
30
|
FloatArray = npt.NDArray[np.float64]
|
|
@@ -57,8 +46,9 @@ class ComBatModel:
|
|
|
57
46
|
ignoring the variance (`delta_star`).
|
|
58
47
|
reference_batch : str, optional
|
|
59
48
|
If specified, the batch level to use as reference.
|
|
60
|
-
covbat_cov_thresh : float, default=0.9
|
|
61
|
-
CovBat: cumulative
|
|
49
|
+
covbat_cov_thresh : float or int, default=0.9
|
|
50
|
+
CovBat: cumulative variance threshold (0, 1] to retain PCs, or
|
|
51
|
+
integer >= 1 specifying the number of components directly.
|
|
62
52
|
eps : float, default=1e-8
|
|
63
53
|
Numerical jitter to avoid division-by-zero.
|
|
64
54
|
"""
|
|
@@ -66,19 +56,19 @@ class ComBatModel:
|
|
|
66
56
|
def __init__(
|
|
67
57
|
self,
|
|
68
58
|
*,
|
|
69
|
-
method: Literal["johnson", "fortin", "chen"] = "johnson",
|
|
59
|
+
method: Literal["johnson", "fortin", "chen"] = "johnson",
|
|
70
60
|
parametric: bool = True,
|
|
71
61
|
mean_only: bool = False,
|
|
72
62
|
reference_batch: Optional[str] = None,
|
|
73
63
|
eps: float = 1e-8,
|
|
74
|
-
covbat_cov_thresh: float = 0.9,
|
|
64
|
+
covbat_cov_thresh: Union[float, int] = 0.9,
|
|
75
65
|
) -> None:
|
|
76
66
|
self.method: str = method
|
|
77
67
|
self.parametric: bool = parametric
|
|
78
68
|
self.mean_only: bool = bool(mean_only)
|
|
79
69
|
self.reference_batch: Optional[str] = reference_batch
|
|
80
70
|
self.eps: float = float(eps)
|
|
81
|
-
self.covbat_cov_thresh: float =
|
|
71
|
+
self.covbat_cov_thresh: Union[float, int] = covbat_cov_thresh
|
|
82
72
|
|
|
83
73
|
self._batch_levels: pd.Index
|
|
84
74
|
self._grand_mean: pd.Series
|
|
@@ -95,9 +85,16 @@ class ComBatModel:
|
|
|
95
85
|
self._batch_levels_pc: pd.Index
|
|
96
86
|
self._pc_gamma_star: FloatArray
|
|
97
87
|
self._pc_delta_star: FloatArray
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
88
|
+
|
|
89
|
+
# Validate covbat_cov_thresh
|
|
90
|
+
if isinstance(self.covbat_cov_thresh, float):
|
|
91
|
+
if not (0.0 < self.covbat_cov_thresh <= 1.0):
|
|
92
|
+
raise ValueError("covbat_cov_thresh must be in (0, 1] when float.")
|
|
93
|
+
elif isinstance(self.covbat_cov_thresh, int):
|
|
94
|
+
if self.covbat_cov_thresh < 1:
|
|
95
|
+
raise ValueError("covbat_cov_thresh must be >= 1 when int.")
|
|
96
|
+
else:
|
|
97
|
+
raise TypeError("covbat_cov_thresh must be float or int.")
|
|
101
98
|
|
|
102
99
|
@staticmethod
|
|
103
100
|
def _as_series(
|
|
@@ -335,8 +332,14 @@ class ComBatModel:
|
|
|
335
332
|
X_meanvar_adj = self._transform_fortin(X, batch, disc, cont)
|
|
336
333
|
X_centered = X_meanvar_adj - X_meanvar_adj.mean(axis=0)
|
|
337
334
|
pca = PCA(svd_solver="full", whiten=False).fit(X_centered)
|
|
338
|
-
|
|
339
|
-
|
|
335
|
+
|
|
336
|
+
# Determine number of components based on threshold type
|
|
337
|
+
if isinstance(self.covbat_cov_thresh, int):
|
|
338
|
+
n_pc = min(self.covbat_cov_thresh, len(pca.explained_variance_ratio_))
|
|
339
|
+
else:
|
|
340
|
+
cumulative = np.cumsum(pca.explained_variance_ratio_)
|
|
341
|
+
n_pc = int(np.searchsorted(cumulative, self.covbat_cov_thresh) + 1)
|
|
342
|
+
|
|
340
343
|
self._covbat_pca = pca
|
|
341
344
|
self._covbat_n_pc = n_pc
|
|
342
345
|
|
|
@@ -487,7 +490,8 @@ class ComBatModel:
|
|
|
487
490
|
continuous_covariates: Optional[ArrayLike] = None,
|
|
488
491
|
) -> pd.DataFrame:
|
|
489
492
|
"""Transform the data using fitted ComBat parameters."""
|
|
490
|
-
|
|
493
|
+
if not hasattr(self, "_gamma_star"):
|
|
494
|
+
raise ValueError("This ComBatModel instance is not fitted yet. Call 'fit' before 'transform'.")
|
|
491
495
|
if not isinstance(X, pd.DataFrame):
|
|
492
496
|
X = pd.DataFrame(X)
|
|
493
497
|
idx = X.index
|
|
@@ -599,7 +603,7 @@ class ComBatModel:
|
|
|
599
603
|
"""Chen transform implementation."""
|
|
600
604
|
X_meanvar_adj = self._transform_fortin(X, batch, disc, cont)
|
|
601
605
|
X_centered = X_meanvar_adj - self._covbat_pca.mean_
|
|
602
|
-
scores = self._covbat_pca.transform(X_centered
|
|
606
|
+
scores = self._covbat_pca.transform(X_centered)
|
|
603
607
|
n_pc = self._covbat_n_pc
|
|
604
608
|
scores_adj = scores.copy()
|
|
605
609
|
|
|
@@ -638,7 +642,7 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
638
642
|
mean_only: bool = False,
|
|
639
643
|
reference_batch: Optional[str] = None,
|
|
640
644
|
eps: float = 1e-8,
|
|
641
|
-
covbat_cov_thresh: float = 0.9,
|
|
645
|
+
covbat_cov_thresh: Union[float, int] = 0.9,
|
|
642
646
|
) -> None:
|
|
643
647
|
self.batch = batch
|
|
644
648
|
self.discrete_covariates = discrete_covariates
|
|
@@ -758,7 +762,8 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
758
762
|
- `'original'`: embedding of original data
|
|
759
763
|
- `'transformed'`: embedding of ComBat-transformed data
|
|
760
764
|
"""
|
|
761
|
-
|
|
765
|
+
if not hasattr(self._model, "_gamma_star"):
|
|
766
|
+
raise ValueError("This ComBat instance is not fitted yet. Call 'fit' before 'plot_transformation'.")
|
|
762
767
|
|
|
763
768
|
if n_components not in [2, 3]:
|
|
764
769
|
raise ValueError(f"n_components must be 2 or 3, got {n_components}")
|
|
@@ -767,11 +772,6 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
767
772
|
if plot_type not in ['static', 'interactive']:
|
|
768
773
|
raise ValueError(f"plot_type must be 'static' or 'interactive', got '{plot_type}'")
|
|
769
774
|
|
|
770
|
-
if reduction_method == 'umap' and not UMAP_AVAILABLE:
|
|
771
|
-
raise ImportError("UMAP is not installed. Install with: pip install umap-learn")
|
|
772
|
-
if plot_type == 'interactive' and not PLOTLY_AVAILABLE:
|
|
773
|
-
raise ImportError("Plotly is not installed. Install with: pip install plotly")
|
|
774
|
-
|
|
775
775
|
if not isinstance(X, pd.DataFrame):
|
|
776
776
|
X = pd.DataFrame(X)
|
|
777
777
|
|
|
@@ -796,8 +796,8 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
796
796
|
else:
|
|
797
797
|
umap_params = {'random_state': 42}
|
|
798
798
|
umap_params.update(reduction_kwargs)
|
|
799
|
-
reducer_orig = umap.UMAP(n_components=n_components, **
|
|
800
|
-
reducer_trans = umap.UMAP(n_components=n_components, **
|
|
799
|
+
reducer_orig = umap.UMAP(n_components=n_components, **umap_params)
|
|
800
|
+
reducer_trans = umap.UMAP(n_components=n_components, **umap_params)
|
|
801
801
|
|
|
802
802
|
X_embedded_orig = reducer_orig.fit_transform(X_np)
|
|
803
803
|
X_embedded_trans = reducer_trans.fit_transform(X_trans_np)
|
|
@@ -811,7 +811,7 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
811
811
|
else:
|
|
812
812
|
fig = self._create_interactive_plot(
|
|
813
813
|
X_embedded_orig, X_embedded_trans, batch_vec,
|
|
814
|
-
reduction_method, n_components, title, show_legend
|
|
814
|
+
reduction_method, n_components, cmap, title, show_legend
|
|
815
815
|
)
|
|
816
816
|
|
|
817
817
|
if return_embeddings:
|
|
@@ -844,9 +844,9 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
844
844
|
n_batches = len(unique_batches)
|
|
845
845
|
|
|
846
846
|
if n_batches <= 10:
|
|
847
|
-
colors =
|
|
847
|
+
colors = matplotlib.colormaps.get_cmap(cmap)(np.linspace(0, 1, n_batches))
|
|
848
848
|
else:
|
|
849
|
-
colors =
|
|
849
|
+
colors = matplotlib.colormaps.get_cmap('tab20')(np.linspace(0, 1, n_batches))
|
|
850
850
|
|
|
851
851
|
if n_components == 2:
|
|
852
852
|
ax1 = plt.subplot(1, 2, 1)
|
|
@@ -930,6 +930,7 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
930
930
|
batch_labels: pd.Series,
|
|
931
931
|
method: str,
|
|
932
932
|
n_components: int,
|
|
933
|
+
cmap: str,
|
|
933
934
|
title: Optional[str],
|
|
934
935
|
show_legend: bool) -> Any:
|
|
935
936
|
"""Create interactive plots using plotly."""
|
|
@@ -953,43 +954,69 @@ class ComBat(BaseEstimator, TransformerMixin):
|
|
|
953
954
|
|
|
954
955
|
unique_batches = batch_labels.drop_duplicates()
|
|
955
956
|
|
|
957
|
+
n_batches = len(unique_batches)
|
|
958
|
+
cmap_func = matplotlib.colormaps.get_cmap(cmap)
|
|
959
|
+
color_list = [mcolors.to_hex(cmap_func(i / max(n_batches - 1, 1))) for i in range(n_batches)]
|
|
960
|
+
|
|
961
|
+
batch_to_color = dict(zip(unique_batches, color_list))
|
|
962
|
+
|
|
956
963
|
for batch in unique_batches:
|
|
957
964
|
mask = batch_labels == batch
|
|
958
965
|
|
|
959
966
|
if n_components == 2:
|
|
960
967
|
fig.add_trace(
|
|
961
|
-
go.Scatter(
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
968
|
+
go.Scatter(
|
|
969
|
+
x=X_orig[mask, 0], y=X_orig[mask, 1],
|
|
970
|
+
mode='markers',
|
|
971
|
+
name=f'Batch {batch}',
|
|
972
|
+
marker=dict(
|
|
973
|
+
size=8,
|
|
974
|
+
color=batch_to_color[batch],
|
|
975
|
+
line=dict(width=1, color='black')
|
|
976
|
+
),
|
|
977
|
+
showlegend=False),
|
|
966
978
|
row=1, col=1
|
|
967
979
|
)
|
|
968
980
|
|
|
969
981
|
fig.add_trace(
|
|
970
|
-
go.Scatter(
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
982
|
+
go.Scatter(
|
|
983
|
+
x=X_trans[mask, 0], y=X_trans[mask, 1],
|
|
984
|
+
mode='markers',
|
|
985
|
+
name=f'Batch {batch}',
|
|
986
|
+
marker=dict(
|
|
987
|
+
size=8,
|
|
988
|
+
color=batch_to_color[batch],
|
|
989
|
+
line=dict(width=1, color='black')
|
|
990
|
+
),
|
|
991
|
+
showlegend=show_legend),
|
|
975
992
|
row=1, col=2
|
|
976
993
|
)
|
|
977
994
|
else:
|
|
978
995
|
fig.add_trace(
|
|
979
|
-
go.Scatter3d(
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
996
|
+
go.Scatter3d(
|
|
997
|
+
x=X_orig[mask, 0], y=X_orig[mask, 1], z=X_orig[mask, 2],
|
|
998
|
+
mode='markers',
|
|
999
|
+
name=f'Batch {batch}',
|
|
1000
|
+
marker=dict(
|
|
1001
|
+
size=5,
|
|
1002
|
+
color=batch_to_color[batch],
|
|
1003
|
+
line=dict(width=0.5, color='black')
|
|
1004
|
+
),
|
|
1005
|
+
showlegend=False),
|
|
984
1006
|
row=1, col=1
|
|
985
1007
|
)
|
|
986
1008
|
|
|
987
1009
|
fig.add_trace(
|
|
988
|
-
go.Scatter3d(
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1010
|
+
go.Scatter3d(
|
|
1011
|
+
x=X_trans[mask, 0], y=X_trans[mask, 1], z=X_trans[mask, 2],
|
|
1012
|
+
mode='markers',
|
|
1013
|
+
name=f'Batch {batch}',
|
|
1014
|
+
marker=dict(
|
|
1015
|
+
size=5,
|
|
1016
|
+
color=batch_to_color[batch],
|
|
1017
|
+
line=dict(width=0.5, color='black')
|
|
1018
|
+
),
|
|
1019
|
+
showlegend=show_legend),
|
|
993
1020
|
row=1, col=2
|
|
994
1021
|
)
|
|
995
1022
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: combatlearn
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 1.0.0
|
|
4
4
|
Summary: Batch-effect harmonization for machine learning frameworks.
|
|
5
5
|
Author-email: Ettore Rocchi <ettoreroc@gmail.com>
|
|
6
|
-
License
|
|
6
|
+
License: MIT
|
|
7
7
|
Keywords: machine-learning,harmonization,combat,preprocessing
|
|
8
8
|
Classifier: Development Status :: 3 - Alpha
|
|
9
9
|
Classifier: Intended Audience :: Science/Research
|
|
@@ -19,13 +19,23 @@ Requires-Dist: matplotlib>=3.4
|
|
|
19
19
|
Requires-Dist: plotly>=5.0
|
|
20
20
|
Requires-Dist: nbformat>=4.2
|
|
21
21
|
Requires-Dist: umap-learn>=0.5
|
|
22
|
-
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff>=0.1; extra == "dev"
|
|
26
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
27
|
+
Provides-Extra: docs
|
|
28
|
+
Requires-Dist: mkdocs>=1.5.0; extra == "docs"
|
|
29
|
+
Requires-Dist: mkdocs-material>=9.0.0; extra == "docs"
|
|
30
|
+
Requires-Dist: mkdocstrings[python]>=0.24.0; extra == "docs"
|
|
31
|
+
Requires-Dist: pymdown-extensions>=10.0; extra == "docs"
|
|
23
32
|
Dynamic: license-file
|
|
24
33
|
|
|
25
34
|
# **combatlearn**
|
|
26
35
|
|
|
27
36
|
[](https://www.python.org/)
|
|
28
37
|
[](https://github.com/EttoreRocchi/combatlearn/actions/workflows/test.yaml)
|
|
38
|
+
[](https://combatlearn.readthedocs.io)
|
|
29
39
|
[](https://pepy.tech/projects/combatlearn)
|
|
30
40
|
[](https://pypi.org/project/combatlearn/)
|
|
31
41
|
[](https://github.com/EttoreRocchi/combatlearn/blob/main/LICENSE)
|
|
@@ -95,6 +105,17 @@ print(f"Best CV AUROC: {grid.best_score_:.3f}")
|
|
|
95
105
|
|
|
96
106
|
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)
|
|
97
107
|
|
|
108
|
+
## Documentation
|
|
109
|
+
|
|
110
|
+
**Full documentation is available at [combatlearn.readthedocs.io](https://combatlearn.readthedocs.io)**
|
|
111
|
+
|
|
112
|
+
The documentation includes:
|
|
113
|
+
- [Installation Guide](https://combatlearn.readthedocs.io/en/latest/installation/)
|
|
114
|
+
- [Quick Start Tutorial](https://combatlearn.readthedocs.io/en/latest/quickstart/)
|
|
115
|
+
- [User Guide](https://combatlearn.readthedocs.io/en/latest/user-guide/overview/)
|
|
116
|
+
- [API Reference](https://combatlearn.readthedocs.io/en/latest/api/)
|
|
117
|
+
- [Examples](https://combatlearn.readthedocs.io/en/latest/examples/basic-usage/)
|
|
118
|
+
|
|
98
119
|
## `ComBat` parameters
|
|
99
120
|
|
|
100
121
|
The following section provides a detailed explanation of all parameters available in the scikit-learn-compatible `ComBat` class.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
combatlearn/__init__.py,sha256=ck_EGW8iqLGUebg2wc-h794lwG3uAkHn9GaWjHgUIX4,99
|
|
2
|
+
combatlearn/combat.py,sha256=Hri1XwnfSXWLoC1KD2VkqtNLkZpixI5ax0UrT1HtjyU,38505
|
|
3
|
+
combatlearn-1.0.0.dist-info/licenses/LICENSE,sha256=O34CBRTmdL59PxDYOa6nq1N0-2A9xyXGkBXKbsL1NeY,1070
|
|
4
|
+
combatlearn-1.0.0.dist-info/METADATA,sha256=hJvZEiA_ekTq06wzfOf2p6M_4vwNXGOdoS-K5MvT4P0,8558
|
|
5
|
+
combatlearn-1.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
+
combatlearn-1.0.0.dist-info/top_level.txt,sha256=3cFQv4oj2sh_NKra45cPy8Go0v8W9x9-zkkUibqZCMk,12
|
|
7
|
+
combatlearn-1.0.0.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
combatlearn/__init__.py,sha256=UzqGt-P5ZVBfK6SXGTi-OOgG5Ae5ZJO7ugZhFp3EHCM,98
|
|
2
|
-
combatlearn/combat.py,sha256=g6YnCVWq40j_fMU2OcXrJ1O0MCSyt2owCaZ4gfyF-Pw,37268
|
|
3
|
-
combatlearn-0.2.1.dist-info/licenses/LICENSE,sha256=O34CBRTmdL59PxDYOa6nq1N0-2A9xyXGkBXKbsL1NeY,1070
|
|
4
|
-
combatlearn-0.2.1.dist-info/METADATA,sha256=zYMV3IEi0vgrGuu6dwYwkLH-cCXxQTr9GekUjUGwTgc,7491
|
|
5
|
-
combatlearn-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
-
combatlearn-0.2.1.dist-info/top_level.txt,sha256=3cFQv4oj2sh_NKra45cPy8Go0v8W9x9-zkkUibqZCMk,12
|
|
7
|
-
combatlearn-0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|