chemotools 0.0.17__tar.gz → 0.0.19__tar.gz
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-0.0.17 → chemotools-0.0.19}/PKG-INFO +5 -1
- {chemotools-0.0.17 → chemotools-0.0.19}/README.md +4 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/air_pls.py +27 -18
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/constant_baseline_correction.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/cubic_spline_correction.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/linear_correction.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/non_negative.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/polynomial_correction.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/subtract_reference.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/derivative/norris_william.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/derivative/savitzky_golay.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scale/index_scaler.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scale/min_max_scaler.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scale/norm_scaler.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scatter/multiplicative_scatter_correction.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scatter/standard_normal_variate.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/smooth/mean_filter.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/smooth/median_filter.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/smooth/savitzky_golay_filter.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/smooth/whittaker_smooth.py +24 -14
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/variable_selection/range_cut.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools.egg-info/PKG-INFO +5 -1
- {chemotools-0.0.17 → chemotools-0.0.19}/tests/test_functionality.py +2 -2
- {chemotools-0.0.17 → chemotools-0.0.19}/LICENSE +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/baseline/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/derivative/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scale/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scatter/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scatter/extended_multiplicative_scatter_correction.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/smooth/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/utils/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/utils/check_inputs.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools/variable_selection/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools.egg-info/SOURCES.txt +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools.egg-info/dependency_links.txt +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools.egg-info/requires.txt +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/chemotools.egg-info/top_level.txt +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/pyproject.toml +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/setup.cfg +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/setup.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/tests/__init__.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/tests/fixtures.py +0 -0
- {chemotools-0.0.17 → chemotools-0.0.19}/tests/test_sklearn_compliance.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: chemotools
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.19
|
4
4
|
Summary: Package to integrate chemometrics in scikit-learn pipelines
|
5
5
|
Home-page: https://github.com/paucablop/chemotools
|
6
6
|
Author: Pau Cabaneros Lopez
|
@@ -13,10 +13,14 @@ Requires-Python: >=3.9
|
|
13
13
|
Description-Content-Type: text/markdown
|
14
14
|
License-File: LICENSE
|
15
15
|
|
16
|
+

|
17
|
+
|
18
|
+
|
16
19
|
[](https://pypi.org/project/chemotools)
|
17
20
|
[](https://pypi.org/project/chemotools)
|
18
21
|
[](https://github.com/paucablop/chemotools/blob/main/LICENSE)
|
19
22
|
[](https://codecov.io/github/paucablop/chemotools)
|
23
|
+
[](https://pepy.tech/project/chemotools)
|
20
24
|
|
21
25
|
# __chemotools__
|
22
26
|
|
@@ -1,7 +1,11 @@
|
|
1
|
+

|
2
|
+
|
3
|
+
|
1
4
|
[](https://pypi.org/project/chemotools)
|
2
5
|
[](https://pypi.org/project/chemotools)
|
3
6
|
[](https://github.com/paucablop/chemotools/blob/main/LICENSE)
|
4
7
|
[](https://codecov.io/github/paucablop/chemotools)
|
8
|
+
[](https://pepy.tech/project/chemotools)
|
5
9
|
|
6
10
|
# __chemotools__
|
7
11
|
|
@@ -1,13 +1,21 @@
|
|
1
1
|
import logging
|
2
2
|
import numpy as np
|
3
|
-
from
|
3
|
+
from scipy.sparse import csc_matrix, eye, diags
|
4
|
+
from scipy.sparse.linalg import spsolve
|
5
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
6
|
from sklearn.utils.validation import check_is_fitted
|
5
7
|
|
6
8
|
from chemotools.utils.check_inputs import check_input
|
7
9
|
|
8
10
|
logger = logging.getLogger(__name__)
|
9
11
|
|
10
|
-
|
12
|
+
# This code is adapted from the following source:
|
13
|
+
# Z.-M. Zhang, S. Chen, and Y.-Z. Liang,
|
14
|
+
# Baseline correction using adaptive iteratively reweighted penalized least squares.
|
15
|
+
# Analyst 135 (5), 1138-1146 (2010).P
|
16
|
+
|
17
|
+
|
18
|
+
class AirPls(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
11
19
|
def __init__(
|
12
20
|
self,
|
13
21
|
nr_iterations: int = 15,
|
@@ -40,7 +48,9 @@ class AirPls(BaseEstimator, TransformerMixin):
|
|
40
48
|
|
41
49
|
# Check that the number of features is the same as the fitted data
|
42
50
|
if X_.shape[1] != self.n_features_in_:
|
43
|
-
raise ValueError(
|
51
|
+
raise ValueError(
|
52
|
+
f"Expected {self.n_features_in_} features but got {X_.shape[1]}"
|
53
|
+
)
|
44
54
|
|
45
55
|
# Calculate the air pls smooth
|
46
56
|
for i, x in enumerate(X_):
|
@@ -49,15 +59,16 @@ class AirPls(BaseEstimator, TransformerMixin):
|
|
49
59
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
50
60
|
|
51
61
|
def _calculate_whittaker_smooth(self, x, w):
|
52
|
-
|
53
|
-
|
54
|
-
|
62
|
+
X = np.matrix(x)
|
63
|
+
m = X.size
|
64
|
+
E = eye(m, format="csc")
|
55
65
|
for i in range(self.polynomial_order):
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
66
|
+
E = E[1:] - E[:-1]
|
67
|
+
W = diags(w, 0, shape=(m, m))
|
68
|
+
A = csc_matrix(W + (self.lam * E.T * E))
|
69
|
+
B = csc_matrix(W * X.T)
|
70
|
+
background = spsolve(A, B)
|
71
|
+
return np.array(background)
|
61
72
|
|
62
73
|
def _calculate_air_pls(self, x):
|
63
74
|
m = x.shape[0]
|
@@ -66,16 +77,16 @@ class AirPls(BaseEstimator, TransformerMixin):
|
|
66
77
|
for i in range(1, self.nr_iterations):
|
67
78
|
z = self._calculate_whittaker_smooth(x, w)
|
68
79
|
d = x - z
|
69
|
-
dssn = np.abs(d[d<0].sum())
|
80
|
+
dssn = np.abs(d[d < 0].sum())
|
70
81
|
|
71
|
-
if dssn < 0.001 * np.abs(x).sum():
|
82
|
+
if dssn < 0.001 * np.abs(x).sum():
|
72
83
|
break
|
73
|
-
|
84
|
+
|
74
85
|
if i == self.nr_iterations - 1:
|
75
86
|
break
|
76
87
|
|
77
|
-
w[d>=0]=0
|
78
|
-
w[d<0] =
|
88
|
+
w[d >= 0] = 0
|
89
|
+
w[d < 0] = np.exp(i * np.abs(d[d < 0]) / dssn)
|
79
90
|
|
80
91
|
negative_d = d[d < 0]
|
81
92
|
if negative_d.size > 0:
|
@@ -84,5 +95,3 @@ class AirPls(BaseEstimator, TransformerMixin):
|
|
84
95
|
w[-1] = w[0]
|
85
96
|
|
86
97
|
return z
|
87
|
-
|
88
|
-
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 ConstantBaselineCorrection(BaseEstimator, TransformerMixin):
|
8
|
+
class ConstantBaselineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(
|
10
10
|
self, wavenumbers: np.ndarray = None, start: int = 0, end: int = 1
|
11
11
|
) -> None:
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
2
|
from scipy.interpolate import CubicSpline
|
3
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
3
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
4
|
from sklearn.utils.validation import check_is_fitted
|
5
5
|
|
6
6
|
from chemotools.utils.check_inputs import check_input
|
7
7
|
|
8
|
-
class CubicSplineCorrection(BaseEstimator, TransformerMixin):
|
8
|
+
class CubicSplineCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(self, indices: np.ndarray = None) -> None:
|
10
10
|
self.indices = indices
|
11
11
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 LinearCorrection(BaseEstimator, TransformerMixin):
|
8
|
+
class LinearCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
|
10
10
|
def _drift_correct_spectrum(self, x: np.ndarray) -> np.ndarray:
|
11
11
|
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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
8
|
|
9
|
-
class NonNegative(BaseEstimator, TransformerMixin):
|
9
|
+
class NonNegative(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
10
10
|
def __init__(self, mode: str = "zero"):
|
11
11
|
self.mode = mode
|
12
12
|
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
from sklearn.utils.validation import check_is_fitted
|
4
4
|
|
5
5
|
from chemotools.utils.check_inputs import check_input
|
6
6
|
|
7
|
-
class PolynomialCorrection(BaseEstimator, TransformerMixin):
|
7
|
+
class PolynomialCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
8
8
|
def __init__(self, order: int = 1, indices: list = None) -> None:
|
9
9
|
self.order = order
|
10
10
|
self.indices = indices
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 SubtractReference(BaseEstimator, TransformerMixin):
|
8
|
+
class SubtractReference(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(
|
10
10
|
self,
|
11
11
|
reference: np.ndarray = None,
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import numpy as np
|
2
2
|
from scipy.ndimage import convolve1d
|
3
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
3
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
4
|
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 NorrisWilliams(BaseEstimator, TransformerMixin):
|
9
|
+
class NorrisWilliams(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
10
10
|
def __init__(
|
11
11
|
self,
|
12
12
|
window_size: int = 5,
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import numpy as np
|
2
2
|
from scipy.signal import savgol_filter
|
3
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
3
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
4
|
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 SavitzkyGolay(BaseEstimator, TransformerMixin):
|
9
|
+
class SavitzkyGolay(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
10
10
|
def __init__(
|
11
11
|
self, window_size: int = 3, polynomial_order: int = 1, derivate_order: int = 1, mode: str = "nearest"
|
12
12
|
) -> None:
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 IndexScaler(BaseEstimator, TransformerMixin):
|
8
|
+
class IndexScaler(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(self, index: int = 0):
|
10
10
|
self.index = index
|
11
11
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 MinMaxScaler(BaseEstimator, TransformerMixin):
|
8
|
+
class MinMaxScaler(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(self, norm: str = 'max'):
|
10
10
|
self.norm = norm
|
11
11
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 NormScaler(BaseEstimator, TransformerMixin):
|
8
|
+
class NormScaler(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(self, l_norm: int = 2):
|
10
10
|
self.l_norm = l_norm
|
11
11
|
|
{chemotools-0.0.17 → chemotools-0.0.19}/chemotools/scatter/multiplicative_scatter_correction.py
RENAMED
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 MultiplicativeScatterCorrection(BaseEstimator, TransformerMixin):
|
8
|
+
class MultiplicativeScatterCorrection(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(
|
10
10
|
self,
|
11
11
|
reference: np.ndarray = None,
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 StandardNormalVariate(BaseEstimator, TransformerMixin):
|
8
|
+
class StandardNormalVariate(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def fit(self, X: np.ndarray, y=None) -> "StandardNormalVariate":
|
10
10
|
# Check that X is a 2D array and has only finite values
|
11
11
|
X = check_input(X)
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import numpy as np
|
2
2
|
from scipy.ndimage import uniform_filter1d
|
3
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
3
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
4
|
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 MeanFilter(BaseEstimator, TransformerMixin):
|
9
|
+
class MeanFilter(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
10
10
|
def __init__(self, window_size: int = 3, mode='nearest') -> None:
|
11
11
|
self.window_size = window_size
|
12
12
|
self.mode = mode
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import numpy as np
|
2
2
|
from scipy.ndimage import median_filter
|
3
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
3
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
4
|
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 MedianFilter(BaseEstimator, TransformerMixin):
|
9
|
+
class MedianFilter(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
10
10
|
def __init__(self, window_size: int = 3, mode: str = 'nearest') -> None:
|
11
11
|
self.window_size = window_size
|
12
12
|
self.mode = mode
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import numpy as np
|
2
2
|
from scipy.signal import savgol_filter
|
3
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
3
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
4
4
|
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 SavitzkyGolayFilter(BaseEstimator, TransformerMixin):
|
9
|
+
class SavitzkyGolayFilter(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
10
10
|
def __init__(
|
11
11
|
self, window_size: int = 3, polynomial_order: int = 1, mode: str = "nearest"
|
12
12
|
) -> None:
|
@@ -1,11 +1,18 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from
|
2
|
+
from scipy.sparse import csc_matrix, eye, diags
|
3
|
+
from scipy.sparse.linalg import spsolve
|
4
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
5
|
from sklearn.utils.validation import check_is_fitted
|
4
6
|
|
5
7
|
from chemotools.utils.check_inputs import check_input
|
6
8
|
|
9
|
+
# This code is adapted from the following source:
|
10
|
+
# Z.-M. Zhang, S. Chen, and Y.-Z. Liang,
|
11
|
+
# Baseline correction using adaptive iteratively reweighted penalized least squares.
|
12
|
+
# Analyst 135 (5), 1138-1146 (2010).
|
7
13
|
|
8
|
-
|
14
|
+
|
15
|
+
class WhittakerSmooth(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
16
|
def __init__(
|
10
17
|
self,
|
11
18
|
lam: float = 1e2,
|
@@ -36,22 +43,25 @@ class WhittakerSmooth(BaseEstimator, TransformerMixin):
|
|
36
43
|
|
37
44
|
# Check that the number of features is the same as the fitted data
|
38
45
|
if X_.shape[1] != self.n_features_in_:
|
39
|
-
raise ValueError(
|
46
|
+
raise ValueError(
|
47
|
+
f"Expected {self.n_features_in_} features but got {X_.shape[1]}"
|
48
|
+
)
|
40
49
|
|
41
50
|
# Calculate the whittaker smooth
|
42
51
|
for i, x in enumerate(X_):
|
43
52
|
X_[i] = self._calculate_whittaker_smooth(x)
|
44
|
-
|
53
|
+
|
45
54
|
return X_.reshape(-1, 1) if X_.ndim == 1 else X_
|
46
55
|
|
47
56
|
def _calculate_whittaker_smooth(self, x):
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
w = np.ones(
|
52
|
-
for i in range(self.differences
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
57
|
+
X = np.matrix(x)
|
58
|
+
m = X.size
|
59
|
+
E = eye(m, format="csc")
|
60
|
+
w = np.ones(m)
|
61
|
+
for i in range(self.differences):
|
62
|
+
E = E[1:] - E[:-1]
|
63
|
+
W = diags(w, 0, shape=(m, m))
|
64
|
+
A = csc_matrix(W + (self.lam * E.T * E))
|
65
|
+
B = csc_matrix(W * X.T)
|
66
|
+
background = spsolve(A, B)
|
67
|
+
return np.array(background)
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import numpy as np
|
2
|
-
from sklearn.base import BaseEstimator, TransformerMixin
|
2
|
+
from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
|
3
3
|
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 RangeCut(BaseEstimator, TransformerMixin):
|
8
|
+
class RangeCut(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
|
9
9
|
def __init__(
|
10
10
|
self,
|
11
11
|
wavenumbers: np.ndarray = None,
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: chemotools
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.19
|
4
4
|
Summary: Package to integrate chemometrics in scikit-learn pipelines
|
5
5
|
Home-page: https://github.com/paucablop/chemotools
|
6
6
|
Author: Pau Cabaneros Lopez
|
@@ -13,10 +13,14 @@ Requires-Python: >=3.9
|
|
13
13
|
Description-Content-Type: text/markdown
|
14
14
|
License-File: LICENSE
|
15
15
|
|
16
|
+

|
17
|
+
|
18
|
+
|
16
19
|
[](https://pypi.org/project/chemotools)
|
17
20
|
[](https://pypi.org/project/chemotools)
|
18
21
|
[](https://github.com/paucablop/chemotools/blob/main/LICENSE)
|
19
22
|
[](https://codecov.io/github/paucablop/chemotools)
|
23
|
+
[](https://pepy.tech/project/chemotools)
|
20
24
|
|
21
25
|
# __chemotools__
|
22
26
|
|
@@ -25,7 +25,7 @@ def test_air_pls(spectrum, reference_airpls):
|
|
25
25
|
spectrum_corrected = air_pls.fit_transform(spectrum)
|
26
26
|
|
27
27
|
# Assert
|
28
|
-
assert np.allclose(spectrum_corrected[0], reference_airpls[0], atol=1e-
|
28
|
+
assert np.allclose(spectrum_corrected[0], reference_airpls[0], atol=1e-7)
|
29
29
|
|
30
30
|
def test_constant_baseline_correction():
|
31
31
|
# Arrange
|
@@ -356,4 +356,4 @@ def test_whitakker_smooth(spectrum, reference_whitakker):
|
|
356
356
|
spectrum_corrected = whitakker_smooth.fit_transform(spectrum)
|
357
357
|
|
358
358
|
# Assert
|
359
|
-
assert np.allclose(spectrum_corrected[0], reference_whitakker[0], atol=1e-
|
359
|
+
assert np.allclose(spectrum_corrected[0], reference_whitakker[0], atol=1e-8)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|