-
Notifications
You must be signed in to change notification settings - Fork 5
/
baseline_shift.py
111 lines (82 loc) 路 3.14 KB
/
baseline_shift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
from sklearn.utils.validation import check_is_fitted
from chemotools.utils.check_inputs import check_input
class BaselineShift(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
"""
Adds a constant baseline to the data. The baseline is drawn from a one-sided
uniform distribution between 0 and 0 + scale.
Parameters
----------
scale : float, default=0.0
Range of the uniform distribution to draw the baseline factor from.
random_state : int, default=None
The random state to use for the random number generator.
Attributes
----------
n_features_in_ : int
The number of features in the input data.
_is_fitted : bool
Whether the transformer has been fitted to data.
Methods
-------
fit(X, y=None)
Fit the transformer to the input data.
transform(X, y=0, copy=True)
Transform the input data by adding a baseline the spectrum.
"""
def __init__(self, scale: int = 0.0, random_state: int = None):
self.scale = scale
self.random_state = random_state
def fit(self, X: np.ndarray, y=None) -> "BaselineShift":
"""
Fit the transformer to the input data.
Parameters
----------
X : np.ndarray of shape (n_samples, n_features)
The input data to fit the transformer to.
y : None
Ignored.
Returns
-------
self : BaselineShift
The fitted transformer.
"""
# Check that X is a 2D array and has only finite values
X = check_input(X)
# Set the number of features
self.n_features_in_ = X.shape[1]
# Set the fitted attribute to True
self._is_fitted = True
# Instantiate the random number generator
self._rng = np.random.default_rng(self.random_state)
return self
def transform(self, X: np.ndarray, y=None) -> np.ndarray:
"""
Transform the input data by adding a baseline to the spectrum.
Parameters
----------
X : np.ndarray of shape (n_samples, n_features)
The input data to transform.
y : None
Ignored.
Returns
-------
X_ : np.ndarray of shape (n_samples, n_features)
The transformed data.
"""
# Check that the estimator is fitted
check_is_fitted(self, "_is_fitted")
# Check that X is a 2D array and has only finite values
X = check_input(X)
X_ = X.copy()
# Check that the number of features is the same as the fitted data
if X_.shape[1] != self.n_features_in_:
raise ValueError(f"Expected {self.n_features_in_} features but got {X_.shape[1]}")
# Calculate the scaled spectrum
for i, x in enumerate(X_):
X_[i] = self._add_baseline(x)
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
def _add_baseline(self, x) -> np.ndarray:
adding_factor = self._rng.uniform(low=0, high=self.scale)
return np.add(x, adding_factor)