chemotools 0.1.1__py3-none-any.whl → 0.1.3__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.
- chemotools/{augmenation → augmentation}/baseline_shift.py +1 -1
- chemotools/{augmenation → augmentation}/index_shift.py +4 -3
- chemotools/baseline/__init__.py +8 -8
- chemotools/baseline/{air_pls.py → _air_pls.py} +5 -19
- chemotools/baseline/{ar_pls.py → _ar_pls.py} +2 -15
- chemotools/baseline/{constant_baseline_correction.py → _constant_baseline_correction.py} +6 -15
- chemotools/baseline/{cubic_spline_correction.py → _cubic_spline_correction.py} +11 -13
- chemotools/baseline/{linear_correction.py → _linear_correction.py} +2 -19
- chemotools/baseline/{non_negative.py → _non_negative.py} +2 -16
- chemotools/baseline/{polynomial_correction.py → _polynomial_correction.py} +13 -23
- chemotools/baseline/{subtract_reference.py → _subtract_reference.py} +7 -19
- chemotools/derivative/__init__.py +2 -2
- chemotools/derivative/{norris_william.py → _norris_william.py} +4 -17
- chemotools/derivative/{savitzky_golay.py → _savitzky_golay.py} +2 -16
- chemotools/feature_selection/__init__.py +2 -0
- chemotools/{variable_selection/select_features.py → feature_selection/_index_selector.py} +17 -42
- chemotools/{variable_selection/range_cut.py → feature_selection/_range_cut.py} +15 -44
- chemotools/scale/__init__.py +3 -3
- chemotools/scale/{min_max_scaler.py → _min_max_scaler.py} +7 -20
- chemotools/scale/{norm_scaler.py → _norm_scaler.py} +5 -18
- chemotools/scale/{point_scaler.py → _point_scaler.py} +11 -22
- chemotools/scatter/__init__.py +4 -4
- chemotools/scatter/{extended_multiplicative_scatter_correction.py → _extended_multiplicative_scatter_correction.py} +2 -10
- chemotools/scatter/{multiplicative_scatter_correction.py → _multiplicative_scatter_correction.py} +2 -8
- chemotools/scatter/{robust_normal_variate.py → _robust_normal_variate.py} +2 -16
- chemotools/scatter/{standard_normal_variate.py → _standard_normal_variate.py} +8 -19
- chemotools/smooth/__init__.py +4 -4
- chemotools/smooth/{mean_filter.py → _mean_filter.py} +5 -18
- chemotools/smooth/{median_filter.py → _median_filter.py} +2 -16
- chemotools/smooth/{savitzky_golay_filter.py → _savitzky_golay_filter.py} +3 -16
- chemotools/smooth/{whittaker_smooth.py → _whittaker_smooth.py} +5 -19
- {chemotools-0.1.1.dist-info → chemotools-0.1.3.dist-info}/METADATA +1 -1
- chemotools-0.1.3.dist-info/RECORD +58 -0
- {chemotools-0.1.1.dist-info → chemotools-0.1.3.dist-info}/WHEEL +1 -1
- tests/test_functionality.py +88 -56
- tests/test_sklearn_compliance.py +26 -25
- chemotools/augmenation/spectrum_shift.py +0 -110
- chemotools/variable_selection/__init__.py +0 -2
- chemotools-0.1.1.dist-info/RECORD +0 -59
- /chemotools/{augmenation → augmentation}/__init__.py +0 -0
- /chemotools/{augmenation → augmentation}/exponential_noise.py +0 -0
- /chemotools/{augmenation → augmentation}/normal_noise.py +0 -0
- /chemotools/{augmenation → augmentation}/spectrum_scale.py +0 -0
- /chemotools/{augmenation → augmentation}/uniform_noise.py +0 -0
- {chemotools-0.1.1.dist-info → chemotools-0.1.3.dist-info}/LICENSE +0 -0
- {chemotools-0.1.1.dist-info → chemotools-0.1.3.dist-info}/top_level.txt +0 -0
@@ -106,6 +106,6 @@ class BaselineShift(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
106
106
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
107
107
|
|
108
108
|
def _add_baseline(self, x) -> np.ndarray:
|
109
|
-
adding_factor = self._rng.uniform(low=0, high=
|
109
|
+
adding_factor = self._rng.uniform(low=0, high=self.scale)
|
110
110
|
return np.add(x, adding_factor)
|
111
111
|
|
@@ -7,7 +7,8 @@ from chemotools.utils.check_inputs import check_input
|
|
7
7
|
|
8
8
|
class IndexShift(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
"""
|
10
|
-
Shift the spectrum a given number of indices
|
10
|
+
Shift the spectrum a given number of indices between - shift and + shift drawn
|
11
|
+
from a discrete uniform distribution.
|
11
12
|
|
12
13
|
Parameters
|
13
14
|
----------
|
@@ -35,7 +36,7 @@ class IndexShift(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
35
36
|
"""
|
36
37
|
|
37
38
|
|
38
|
-
def __init__(self, shift: int = 0
|
39
|
+
def __init__(self, shift: int = 0, random_state: int = None):
|
39
40
|
self.shift = shift
|
40
41
|
self.random_state = random_state
|
41
42
|
|
@@ -105,6 +106,6 @@ class IndexShift(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
105
106
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
106
107
|
|
107
108
|
def _shift_spectrum(self, x) -> np.ndarray:
|
108
|
-
shift_amount = self._rng.integers(-self.shift, self.shift
|
109
|
+
shift_amount = self._rng.integers(-self.shift, self.shift, endpoint=True)
|
109
110
|
return np.roll(x, shift_amount)
|
110
111
|
|
chemotools/baseline/__init__.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
from .
|
2
|
-
from .
|
3
|
-
from .
|
4
|
-
from .
|
5
|
-
from .
|
6
|
-
from .
|
7
|
-
from .
|
8
|
-
from .
|
1
|
+
from ._air_pls import AirPls
|
2
|
+
from ._ar_pls import ArPls
|
3
|
+
from ._constant_baseline_correction import ConstantBaselineCorrection
|
4
|
+
from ._cubic_spline_correction import CubicSplineCorrection
|
5
|
+
from ._linear_correction import LinearCorrection
|
6
|
+
from ._non_negative import NonNegative
|
7
|
+
from ._polynomial_correction import PolynomialCorrection
|
8
|
+
from ._subtract_reference import SubtractReference
|
@@ -30,14 +30,6 @@ class AirPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
30
30
|
The number of iterations used to calculate the baseline. Increasing the number of iterations can improve the
|
31
31
|
accuracy of the baseline correction, but also increases the computation time.
|
32
32
|
|
33
|
-
Attributes
|
34
|
-
----------
|
35
|
-
n_features_in_ : int
|
36
|
-
The number of features in the input data.
|
37
|
-
|
38
|
-
_is_fitted : bool
|
39
|
-
A flag indicating whether the estimator has been fitted to data.
|
40
|
-
|
41
33
|
Methods
|
42
34
|
-------
|
43
35
|
fit(X, y=None)
|
@@ -85,13 +77,7 @@ class AirPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
85
77
|
Returns the instance itself.
|
86
78
|
"""
|
87
79
|
# Check that X is a 2D array and has only finite values
|
88
|
-
X =
|
89
|
-
|
90
|
-
# Set the number of features
|
91
|
-
self.n_features_in_ = X.shape[1]
|
92
|
-
|
93
|
-
# Set the fitted attribute to True
|
94
|
-
self._is_fitted = True
|
80
|
+
X = self._validate_data(X)
|
95
81
|
|
96
82
|
return self
|
97
83
|
|
@@ -113,7 +99,7 @@ class AirPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
113
99
|
"""
|
114
100
|
|
115
101
|
# Check that the estimator is fitted
|
116
|
-
check_is_fitted(self, "
|
102
|
+
check_is_fitted(self, "n_features_in_")
|
117
103
|
|
118
104
|
# Check that X is a 2D array and has only finite values
|
119
105
|
X = check_input(X)
|
@@ -132,14 +118,14 @@ class AirPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
132
118
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
133
119
|
|
134
120
|
def _calculate_whittaker_smooth(self, x, w):
|
135
|
-
X = np.
|
121
|
+
X = np.array(x)
|
136
122
|
m = X.size
|
137
123
|
E = eye(m, format="csc")
|
138
124
|
for i in range(self.polynomial_order):
|
139
125
|
E = E[1:] - E[:-1]
|
140
126
|
W = diags(w, 0, shape=(m, m))
|
141
|
-
A = csc_matrix(W + (self.lam * E.T
|
142
|
-
B = csc_matrix(W
|
127
|
+
A = csc_matrix(W + (self.lam * E.T @ E))
|
128
|
+
B = csc_matrix(W @ X.T).toarray().ravel()
|
143
129
|
background = spsolve(A, B)
|
144
130
|
return np.array(background)
|
145
131
|
|
@@ -29,13 +29,6 @@ class ArPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
29
29
|
nr_iterations : int, optional (default=100)
|
30
30
|
The maximum number of iterations for the weight updating scheme.
|
31
31
|
|
32
|
-
Attributes
|
33
|
-
----------
|
34
|
-
n_features_in_ : int
|
35
|
-
The number of input features.
|
36
|
-
|
37
|
-
_is_fitted : bool
|
38
|
-
Whether the estimator has been fitted.
|
39
32
|
|
40
33
|
Methods
|
41
34
|
-------
|
@@ -86,13 +79,7 @@ class ArPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
86
79
|
"""
|
87
80
|
|
88
81
|
# Check that X is a 2D array and has only finite values
|
89
|
-
X =
|
90
|
-
|
91
|
-
# Set the number of features
|
92
|
-
self.n_features_in_ = X.shape[1]
|
93
|
-
|
94
|
-
# Set the fitted attribute to True
|
95
|
-
self._is_fitted = True
|
82
|
+
X = self._validate_data(X)
|
96
83
|
|
97
84
|
return self
|
98
85
|
|
@@ -114,7 +101,7 @@ class ArPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
114
101
|
"""
|
115
102
|
|
116
103
|
# Check that the estimator is fitted
|
117
|
-
check_is_fitted(self, "
|
104
|
+
check_is_fitted(self, "n_features_in_")
|
118
105
|
|
119
106
|
# Check that X is a 2D array and has only finite values
|
120
107
|
X = check_input(X)
|
@@ -30,12 +30,6 @@ class ConstantBaselineCorrection(OneToOneFeatureMixin, BaseEstimator, Transforme
|
|
30
30
|
end_index_ : int
|
31
31
|
The index of the end of the range. It is 1 if the wavenumbers are not provided.
|
32
32
|
|
33
|
-
n_features_in_ : int
|
34
|
-
The number of features in the input data.
|
35
|
-
|
36
|
-
_is_fitted : bool
|
37
|
-
Whether the transformer has been fitted to data.
|
38
|
-
|
39
33
|
Methods
|
40
34
|
-------
|
41
35
|
fit(X, y=None)
|
@@ -46,7 +40,10 @@ class ConstantBaselineCorrection(OneToOneFeatureMixin, BaseEstimator, Transforme
|
|
46
40
|
"""
|
47
41
|
|
48
42
|
def __init__(
|
49
|
-
self,
|
43
|
+
self,
|
44
|
+
start: int = 0,
|
45
|
+
end: int = 1,
|
46
|
+
wavenumbers: np.ndarray = None,
|
50
47
|
) -> None:
|
51
48
|
self.start = start
|
52
49
|
self.end = end
|
@@ -70,13 +67,7 @@ class ConstantBaselineCorrection(OneToOneFeatureMixin, BaseEstimator, Transforme
|
|
70
67
|
The fitted transformer.
|
71
68
|
"""
|
72
69
|
# Check that X is a 2D array and has only finite values
|
73
|
-
X =
|
74
|
-
|
75
|
-
# Set the number of features
|
76
|
-
self.n_features_in_ = X.shape[1]
|
77
|
-
|
78
|
-
# Set the fitted attribute to True
|
79
|
-
self._is_fitted = True
|
70
|
+
X = self._validate_data(X)
|
80
71
|
|
81
72
|
# Set the start and end indices
|
82
73
|
if self.wavenumbers is None:
|
@@ -109,7 +100,7 @@ class ConstantBaselineCorrection(OneToOneFeatureMixin, BaseEstimator, Transforme
|
|
109
100
|
The transformed input data.
|
110
101
|
"""
|
111
102
|
# Check that the estimator is fitted
|
112
|
-
check_is_fitted(self, "
|
103
|
+
check_is_fitted(self, ["start_index_", "end_index_"])
|
113
104
|
|
114
105
|
# Check that X is a 2D array and has only finite values
|
115
106
|
X = check_input(X)
|
@@ -5,9 +5,10 @@ from sklearn.utils.validation import check_is_fitted
|
|
5
5
|
|
6
6
|
from chemotools.utils.check_inputs import check_input
|
7
7
|
|
8
|
+
|
8
9
|
class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
10
|
"""
|
10
|
-
A transformer that corrects a baseline by subtracting a cubic spline through the
|
11
|
+
A transformer that corrects a baseline by subtracting a cubic spline through the
|
11
12
|
points defined by the indices.
|
12
13
|
|
13
14
|
Parameters
|
@@ -32,6 +33,7 @@ class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixi
|
|
32
33
|
Transform the input data by subtracting the constant baseline value.
|
33
34
|
|
34
35
|
"""
|
36
|
+
|
35
37
|
def __init__(self, indices: list = None) -> None:
|
36
38
|
self.indices = indices
|
37
39
|
|
@@ -53,13 +55,7 @@ class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixi
|
|
53
55
|
The fitted transformer.
|
54
56
|
"""
|
55
57
|
# Check that X is a 2D array and has only finite values
|
56
|
-
X =
|
57
|
-
|
58
|
-
# Set the number of features
|
59
|
-
self.n_features_in_ = X.shape[1]
|
60
|
-
|
61
|
-
# Set the fitted attribute to True
|
62
|
-
self._is_fitted = True
|
58
|
+
X = self._validate_data(X)
|
63
59
|
|
64
60
|
if self.indices is None:
|
65
61
|
self.indices_ = [0, len(X[0]) - 1]
|
@@ -89,7 +85,7 @@ class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixi
|
|
89
85
|
The transformed data.
|
90
86
|
"""
|
91
87
|
# Check that the estimator is fitted
|
92
|
-
check_is_fitted(self, "
|
88
|
+
check_is_fitted(self, "indices_")
|
93
89
|
|
94
90
|
# Check that X is a 2D array and has only finite values
|
95
91
|
X = check_input(X)
|
@@ -97,7 +93,9 @@ class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixi
|
|
97
93
|
|
98
94
|
# Check that the number of features is the same as the fitted data
|
99
95
|
if X_.shape[1] != self.n_features_in_:
|
100
|
-
raise ValueError(
|
96
|
+
raise ValueError(
|
97
|
+
f"Expected {self.n_features_in_} features but got {X_.shape[1]}"
|
98
|
+
)
|
101
99
|
|
102
100
|
# Calculate spline baseline correction
|
103
101
|
for i, x in enumerate(X_):
|
@@ -106,7 +104,7 @@ class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixi
|
|
106
104
|
|
107
105
|
def _spline_baseline_correct(self, x: np.ndarray) -> np.ndarray:
|
108
106
|
indices = self.indices_
|
109
|
-
intensity = x[indices]
|
107
|
+
intensity = x[indices]
|
110
108
|
spl = CubicSpline(indices, intensity)
|
111
|
-
baseline = spl(range(len(x)))
|
112
|
-
return x - baseline
|
109
|
+
baseline = spl(range(len(x)))
|
110
|
+
return x - baseline
|
@@ -10,17 +10,6 @@ class LinearCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
10
10
|
A transformer that corrects a baseline by subtracting a linear baseline through the
|
11
11
|
initial and final points of the spectrum.
|
12
12
|
|
13
|
-
Parameters
|
14
|
-
----------
|
15
|
-
|
16
|
-
Attributes
|
17
|
-
----------
|
18
|
-
n_features_in_ : int
|
19
|
-
The number of features in the input data.
|
20
|
-
|
21
|
-
_is_fitted : bool
|
22
|
-
Whether the transformer has been fitted to data.
|
23
|
-
|
24
13
|
Methods
|
25
14
|
-------
|
26
15
|
fit(X, y=None)
|
@@ -68,13 +57,7 @@ class LinearCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
68
57
|
The fitted transformer.
|
69
58
|
"""
|
70
59
|
# Check that X is a 2D array and has only finite values
|
71
|
-
X =
|
72
|
-
|
73
|
-
# Set the number of features
|
74
|
-
self.n_features_in_ = X.shape[1]
|
75
|
-
|
76
|
-
# Set the fitted attribute to True
|
77
|
-
self._is_fitted = True
|
60
|
+
X = self._validate_data(X)
|
78
61
|
|
79
62
|
return self
|
80
63
|
|
@@ -99,7 +82,7 @@ class LinearCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
99
82
|
The transformed data.
|
100
83
|
"""
|
101
84
|
# Check that the estimator is fitted
|
102
|
-
check_is_fitted(self, "
|
85
|
+
check_is_fitted(self, "n_features_in_")
|
103
86
|
|
104
87
|
# Check that X is a 2D array and has only finite values
|
105
88
|
X = check_input(X)
|
@@ -14,14 +14,6 @@ class NonNegative(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
14
14
|
mode : str, optional
|
15
15
|
The mode to use for the non-negative values. Can be "zero" or "abs".
|
16
16
|
|
17
|
-
Attributes
|
18
|
-
----------
|
19
|
-
n_features_in_ : int
|
20
|
-
The number of features in the input data.
|
21
|
-
|
22
|
-
_is_fitted : bool
|
23
|
-
Whether the transformer has been fitted to data.
|
24
|
-
|
25
17
|
Methods
|
26
18
|
-------
|
27
19
|
fit(X, y=None)
|
@@ -52,13 +44,7 @@ class NonNegative(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
52
44
|
The fitted transformer.
|
53
45
|
"""
|
54
46
|
# Check that X is a 2D array and has only finite values
|
55
|
-
X =
|
56
|
-
|
57
|
-
# Set the number of features
|
58
|
-
self.n_features_in_ = X.shape[1]
|
59
|
-
|
60
|
-
# Set the fitted attribute to True
|
61
|
-
self._is_fitted = True
|
47
|
+
X = self._validate_data(X)
|
62
48
|
|
63
49
|
return self
|
64
50
|
|
@@ -80,7 +66,7 @@ class NonNegative(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
80
66
|
The transformed data.
|
81
67
|
"""
|
82
68
|
# Check that the estimator is fitted
|
83
|
-
check_is_fitted(self, "
|
69
|
+
check_is_fitted(self, "n_features_in_")
|
84
70
|
|
85
71
|
# Check that X is a 2D array and has only finite values
|
86
72
|
X = check_input(X)
|
@@ -4,9 +4,10 @@ from sklearn.utils.validation import check_is_fitted
|
|
4
4
|
|
5
5
|
from chemotools.utils.check_inputs import check_input
|
6
6
|
|
7
|
+
|
7
8
|
class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
8
9
|
"""
|
9
|
-
A transformer that subtracts a polynomial baseline from the input data. The polynomial is
|
10
|
+
A transformer that subtracts a polynomial baseline from the input data. The polynomial is
|
10
11
|
fitted to the points in the spectrum specified by the indices parameter.
|
11
12
|
|
12
13
|
Parameters
|
@@ -18,14 +19,6 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
18
19
|
The indices of the points in the spectrum to fit the polynomial to. Defaults to None,
|
19
20
|
which fits the polynomial to all points in the spectrum (equivalent to detrend).
|
20
21
|
|
21
|
-
Attributes
|
22
|
-
----------
|
23
|
-
n_features_in_ : int
|
24
|
-
The number of features in the input data.
|
25
|
-
|
26
|
-
_is_fitted : bool
|
27
|
-
Whether the transformer has been fitted to data.
|
28
|
-
|
29
22
|
Methods
|
30
23
|
-------
|
31
24
|
fit(X, y=None)
|
@@ -37,6 +30,7 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
37
30
|
_baseline_correct_spectrum(x)
|
38
31
|
Subtract the polynomial baseline from a single spectrum.
|
39
32
|
"""
|
33
|
+
|
40
34
|
def __init__(self, order: int = 1, indices: list = None) -> None:
|
41
35
|
self.order = order
|
42
36
|
self.indices = indices
|
@@ -59,13 +53,7 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
59
53
|
The fitted transformer.
|
60
54
|
"""
|
61
55
|
# Check that X is a 2D array and has only finite values
|
62
|
-
X =
|
63
|
-
|
64
|
-
# Set the number of features
|
65
|
-
self.n_features_in_ = X.shape[1]
|
66
|
-
|
67
|
-
# Set the fitted attribute to True
|
68
|
-
self._is_fitted = True
|
56
|
+
X = self._validate_data(X)
|
69
57
|
|
70
58
|
if self.indices is None:
|
71
59
|
self.indices_ = range(0, len(X[0]))
|
@@ -73,8 +61,8 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
73
61
|
self.indices_ = self.indices
|
74
62
|
|
75
63
|
return self
|
76
|
-
|
77
|
-
def transform(self, X: np.ndarray, y:int=0, copy:bool=True) -> np.ndarray:
|
64
|
+
|
65
|
+
def transform(self, X: np.ndarray, y: int = 0, copy: bool = True) -> np.ndarray:
|
78
66
|
"""
|
79
67
|
Transform the input data by subtracting the polynomial baseline.
|
80
68
|
|
@@ -95,7 +83,7 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
95
83
|
The transformed data.
|
96
84
|
"""
|
97
85
|
# Check that the estimator is fitted
|
98
|
-
check_is_fitted(self, "
|
86
|
+
check_is_fitted(self, "indices_")
|
99
87
|
|
100
88
|
# Check that X is a 2D array and has only finite values
|
101
89
|
X = check_input(X)
|
@@ -103,13 +91,15 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
103
91
|
|
104
92
|
# Check that the number of features is the same as the fitted data
|
105
93
|
if X_.shape[1] != self.n_features_in_:
|
106
|
-
raise ValueError(
|
94
|
+
raise ValueError(
|
95
|
+
f"Expected {self.n_features_in_} features but got {X_.shape[1]}"
|
96
|
+
)
|
107
97
|
|
108
98
|
# Calculate polynomial baseline correction
|
109
99
|
for i, x in enumerate(X_):
|
110
100
|
X_[i] = self._baseline_correct_spectrum(x)
|
111
101
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
112
|
-
|
102
|
+
|
113
103
|
def _baseline_correct_spectrum(self, x: np.ndarray) -> np.ndarray:
|
114
104
|
"""
|
115
105
|
Subtract the polynomial baseline from a single spectrum.
|
@@ -126,5 +116,5 @@ class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin
|
|
126
116
|
"""
|
127
117
|
intensity = x[self.indices_]
|
128
118
|
poly = np.polyfit(self.indices_, intensity, self.order)
|
129
|
-
baseline = [np.polyval(poly, i) for i in range(0, len(x))]
|
130
|
-
return x - baseline
|
119
|
+
baseline = [np.polyval(poly, i) for i in range(0, len(x))]
|
120
|
+
return x - baseline
|
@@ -15,14 +15,6 @@ class SubtractReference(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
15
15
|
The reference spectrum to subtract from the input data. If None, the original spectrum
|
16
16
|
is returned.
|
17
17
|
|
18
|
-
Attributes
|
19
|
-
----------
|
20
|
-
n_features_in_ : int
|
21
|
-
The number of features in the input data.
|
22
|
-
|
23
|
-
_is_fitted : bool
|
24
|
-
Whether the transformer has been fitted to data.
|
25
|
-
|
26
18
|
Methods
|
27
19
|
-------
|
28
20
|
fit(X, y=None)
|
@@ -34,6 +26,7 @@ class SubtractReference(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
34
26
|
_subtract_reference(x)
|
35
27
|
Subtract the reference spectrum from a single spectrum.
|
36
28
|
"""
|
29
|
+
|
37
30
|
def __init__(
|
38
31
|
self,
|
39
32
|
reference: np.ndarray = None,
|
@@ -58,20 +51,13 @@ class SubtractReference(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
58
51
|
The fitted transformer.
|
59
52
|
"""
|
60
53
|
# Check that X is a 2D array and has only finite values
|
61
|
-
X =
|
62
|
-
|
63
|
-
# Set the number of features
|
64
|
-
self.n_features_in_ = X.shape[1]
|
65
|
-
|
66
|
-
# Set the fitted attribute to True
|
67
|
-
self._is_fitted = True
|
54
|
+
X = self._validate_data(X)
|
68
55
|
|
69
56
|
# Set the reference
|
70
|
-
|
71
57
|
if self.reference is not None:
|
72
58
|
self.reference_ = self.reference.copy()
|
73
59
|
return self
|
74
|
-
|
60
|
+
|
75
61
|
return self
|
76
62
|
|
77
63
|
def transform(self, X: np.ndarray, y=None) -> np.ndarray:
|
@@ -92,7 +78,7 @@ class SubtractReference(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
92
78
|
The transformed data.
|
93
79
|
"""
|
94
80
|
# Check that the estimator is fitted
|
95
|
-
check_is_fitted(self, "
|
81
|
+
check_is_fitted(self, "n_features_in_")
|
96
82
|
|
97
83
|
# Check that X is a 2D array and has only finite values
|
98
84
|
X = check_input(X)
|
@@ -100,7 +86,9 @@ class SubtractReference(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
100
86
|
|
101
87
|
# Check that the number of features is the same as the fitted data
|
102
88
|
if X_.shape[1] != self.n_features_in_:
|
103
|
-
raise ValueError(
|
89
|
+
raise ValueError(
|
90
|
+
f"Expected {self.n_features_in_} features but got {X_.shape[1]}"
|
91
|
+
)
|
104
92
|
|
105
93
|
if self.reference is None:
|
106
94
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
@@ -1,2 +1,2 @@
|
|
1
|
-
from .
|
2
|
-
from .
|
1
|
+
from ._norris_william import NorrisWilliams
|
2
|
+
from ._savitzky_golay import SavitzkyGolay
|
@@ -22,17 +22,9 @@ class NorrisWilliams(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
22
22
|
The order of the derivative to calculate. Can be 1 or 2. Default is 1.
|
23
23
|
|
24
24
|
mode : str, optional
|
25
|
-
The mode to use for the derivative calculation. Can be "nearest", "constant",
|
25
|
+
The mode to use for the derivative calculation. Can be "nearest", "constant",
|
26
26
|
"reflect", "wrap", "mirror" or "interp". Default is "nearest".
|
27
27
|
|
28
|
-
Attributes
|
29
|
-
----------
|
30
|
-
n_features_in_ : int
|
31
|
-
The number of features in the input data.
|
32
|
-
|
33
|
-
_is_fitted : bool
|
34
|
-
Whether the transformer has been fitted to data.
|
35
|
-
|
36
28
|
Methods
|
37
29
|
-------
|
38
30
|
fit(X, y=None)
|
@@ -41,6 +33,7 @@ class NorrisWilliams(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
41
33
|
transform(X, y=0, copy=True)
|
42
34
|
Transform the input data by calculating the Norris-Williams derivative.
|
43
35
|
"""
|
36
|
+
|
44
37
|
def __init__(
|
45
38
|
self,
|
46
39
|
window_size: int = 5,
|
@@ -71,13 +64,7 @@ class NorrisWilliams(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
71
64
|
The fitted transformer.
|
72
65
|
"""
|
73
66
|
# Check that X is a 2D array and has only finite values
|
74
|
-
X =
|
75
|
-
|
76
|
-
# Set the number of features
|
77
|
-
self.n_features_in_ = X.shape[1]
|
78
|
-
|
79
|
-
# Set the fitted attribute to True
|
80
|
-
self._is_fitted = True
|
67
|
+
X = self._validate_data(X)
|
81
68
|
|
82
69
|
return self
|
83
70
|
|
@@ -99,7 +86,7 @@ class NorrisWilliams(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
99
86
|
The transformed data.
|
100
87
|
"""
|
101
88
|
# Check that the estimator is fitted
|
102
|
-
check_is_fitted(self, "
|
89
|
+
check_is_fitted(self, "n_features_in_")
|
103
90
|
|
104
91
|
# Check that X is a 2D array and has only finite values
|
105
92
|
X = check_input(X)
|
@@ -27,14 +27,6 @@ class SavitzkyGolay(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
27
27
|
The mode to use for the derivative calculation. Can be "nearest", "constant",
|
28
28
|
"reflect", "wrap", "mirror" or "interp". Default is "nearest".
|
29
29
|
|
30
|
-
Attributes
|
31
|
-
----------
|
32
|
-
n_features_in_ : int
|
33
|
-
The number of features in the input data.
|
34
|
-
|
35
|
-
_is_fitted : bool
|
36
|
-
Whether the transformer has been fitted to data.
|
37
|
-
|
38
30
|
Methods
|
39
31
|
-------
|
40
32
|
fit(X, y=None)
|
@@ -74,13 +66,7 @@ class SavitzkyGolay(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
74
66
|
The fitted transformer.
|
75
67
|
"""
|
76
68
|
# Check that X is a 2D array and has only finite values
|
77
|
-
X =
|
78
|
-
|
79
|
-
# Set the number of features
|
80
|
-
self.n_features_in_ = X.shape[1]
|
81
|
-
|
82
|
-
# Set the fitted attribute to True
|
83
|
-
self._is_fitted = True
|
69
|
+
X = self._validate_data(X)
|
84
70
|
|
85
71
|
return self
|
86
72
|
|
@@ -102,7 +88,7 @@ class SavitzkyGolay(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
|
102
88
|
The transformed data.
|
103
89
|
"""
|
104
90
|
# Check that the estimator is fitted
|
105
|
-
check_is_fitted(self, "
|
91
|
+
check_is_fitted(self, "n_features_in_")
|
106
92
|
|
107
93
|
# Check that X is a 2D array and has only finite values
|
108
94
|
X = check_input(X)
|