-
Notifications
You must be signed in to change notification settings - Fork 12
245 feature domain adaption implementation #256
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
base: main
Are you sure you want to change the base?
Changes from all commits
bcd8ea9
c775a32
5799cde
2e1b0cd
c408728
dec6ef2
5239e5d
c626258
d305132
1bc9e25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The :mod:'chemotools.domain_adaption:DirectStandardization' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| module implements a Direct Standardization transformer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Authors: Ruggero Guerrini | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # License: MIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from sklearn.base import BaseEstimator, TransformerMixin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class DirectStandardization(BaseEstimator, TransformerMixin): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ----------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Implement a direct standardization transformer for the calibration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transfer application. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_master contains the reference measurements acquired | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| on the master instrument. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_slave contains the corresponding measurements of the same samples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| acquired on the slave instrument. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The transformer estimates a mapping from the slave space to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| the master space. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| After fitting, new X_slave spectra can be transformed into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| the X_master space. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| T : np.ndarray of shape (n_features, n_features) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The pxp matrix that solver the problem X_slave T = X_master | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using the method of least squares | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Examples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_slave = np.random.randn((100,50)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_portbale = X_slave*2+5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DS = DirectStandardization().fit(X_slave,X_master) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_slave_transf = DS.transform(X_slave) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def fit(self, X_slave: np.ndarray, X_master: np.ndarray) -> "DirectStandardization": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fit the DirectStandardization to the input data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_slave : np.ndarray of shape (n_samples, n_features) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The slave data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_master : np.ndarray of shape (n_samples, n_features) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The master data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self : DirectStandardization | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The fitted model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_slave = np.asarray(X_slave, dtype=float) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_master = np.asarray(X_master, dtype=float) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+67
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| X_slave = np.random.randn((100,50)) | |
| X_portbale = X_slave*2+5 | |
| DS = DirectStandardization().fit(X_slave,X_master) | |
| X_slave_transf = DS.transform(X_slave) | |
| """ | |
| def __init__(self): | |
| pass | |
| def fit(self, X_slave: np.ndarray, X_master: np.ndarray) -> "DirectStandardization": | |
| """ | |
| Fit the DirectStandardization to the input data. | |
| Parameters | |
| ---------- | |
| X_slave : np.ndarray of shape (n_samples, n_features) | |
| The slave data | |
| X_master : np.ndarray of shape (n_samples, n_features) | |
| The master data | |
| Returns | |
| ------- | |
| self : DirectStandardization | |
| The fitted model. | |
| """ | |
| X_slave = np.asarray(X_slave, dtype=float) | |
| X_master = np.asarray(X_master, dtype=float) | |
| X_slave = np.random.randn(100, 50) | |
| X_master = X_slave * 2 + 5 | |
| DS = DirectStandardization().fit(X_slave, X_master) | |
| X_slave_transf = DS.transform(X_slave) | |
| """ | |
| def __init__(self): | |
| self.T = None | |
| def fit(self, X: np.ndarray, y: np.ndarray = None) -> "DirectStandardization": | |
| """ | |
| Fit the DirectStandardization to the input data. | |
| Parameters | |
| ---------- | |
| X : np.ndarray of shape (n_samples, n_features) | |
| The slave data. | |
| y : np.ndarray of shape (n_samples, n_features), default=None | |
| The master data corresponding to ``X``. | |
| Returns | |
| ------- | |
| self : DirectStandardization | |
| The fitted model. | |
| """ | |
| if y is None: | |
| raise ValueError("y must be provided and contain the master spectra") | |
| X_slave = np.asarray(X, dtype=float) | |
| X_master = np.asarray(y, dtype=float) |
Copilot
AI
Apr 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sklearn compatibility, fit()/transform() should use validate_data(...) to enforce 2D numeric input and set n_features_in_, and fitted attributes should follow the trailing-underscore convention (e.g., T_ instead of T). As written, DirectStandardization won't satisfy sklearn's estimator checks and may accept inputs that later fail at @ multiplication.
Copilot
AI
Apr 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message in transform() says "Call train() first", but the public API is fit(). This is confusing for users and makes debugging harder; update the message (and consider raising the standard NotFittedError used by scikit-learn).
Copilot
AI
Apr 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transform() checks if self.T is None, but self.T is never initialized in __init__, so calling transform() before fit() will raise AttributeError instead of the intended error. Use sklearn.utils.validation.check_is_fitted (and a fitted attribute like T_) or initialize the attribute in __init__ and check with hasattr/check_is_fitted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| # License: MIT | ||
|
|
||
| from numbers import Integral | ||
| from typing import Literal | ||
|
|
||
| import numpy as np | ||
| from scipy.ndimage import uniform_filter1d | ||
|
|
@@ -57,17 +58,18 @@ class MeanFilter(TransformerMixin, OneToOneFeatureMixin, BaseEstimator): | |
|
|
||
| _parameter_constraints: dict = { | ||
| "window_length": [Interval(Integral, 3, None, closed="left")], | ||
| "mode": [ | ||
| StrOptions({"nearest", "constant", "reflect", "wrap", "mirror", "interp"}) | ||
| ], | ||
| "mode": [StrOptions({"nearest", "constant", "reflect", "wrap", "mirror"})], | ||
| "window_size": [ | ||
| Interval(Integral, 3, None, closed="left"), | ||
| deprecated_parameter_constraint(), | ||
| ], | ||
| } | ||
|
|
||
| def __init__( | ||
| self, window_length: int = 3, mode="nearest", window_size=DEPRECATED_PARAMETER | ||
| self, | ||
| window_length: int = 3, | ||
| mode: Literal["nearest", "constant", "reflect", "wrap", "mirror"] = "nearest", | ||
| window_size=DEPRECATED_PARAMETER, | ||
|
Comment on lines
59
to
+72
|
||
| ) -> None: | ||
| self.window_length = window_length | ||
| self.window_size = window_size | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,45 @@ | ||||||
| """ | ||||||
| Test for DirectStandardization | ||||||
| """ | ||||||
|
|
||||||
| # Authors: Ruggero Guerrini | ||||||
| # License: MIT | ||||||
| import numpy as np | ||||||
|
|
||||||
| from chemotools.domain_adaption._direct_standardization import ( | ||||||
| DirectStandardization, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def data_diff(dataset_ref, dataset_test): | ||||||
| diff_norm = np.linalg.norm(dataset_ref - dataset_test) | ||||||
| ref_norm = np.linalg.norm(dataset_ref) | ||||||
| difference = diff_norm / ref_norm | ||||||
| return difference | ||||||
|
|
||||||
|
|
||||||
| class Test_Direct_Standardization: | ||||||
| """ | ||||||
| Test that enhanced Direct Standardization maintains sklearn API compatibility | ||||||
| """ | ||||||
|
|
||||||
| def test_shape_consistency_and_improvement(self): | ||||||
| # Arrange - I create a slave linked to my mster | ||||||
|
||||||
| # Arrange - I create a slave linked to my mster | |
| # Arrange - I create a slave linked to my master |
Copilot
AI
Apr 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test claims sklearn API compatibility, but it doesn't run sklearn.utils.estimator_checks.check_estimator like other transformer tests in this repo, and it imports from a private module path. Consider adding a check_estimator(DirectStandardization()) test (or adjusting the estimator to pass) and importing from the public package API to keep tests consistent and catch API regressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module/class docstrings have several documentation issues: the Sphinx role markup in the module docstring is malformed, and the example code contains typos and invalid code (e.g.,
np.random.randn((100,50))and an undefinedX_master). Please fix the markup and make the example runnable so it can be used in generated docs.