chemotools 0.0.22__py3-none-any.whl → 0.1.6__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.
Files changed (60) hide show
  1. chemotools/augmentation/__init__.py +16 -0
  2. chemotools/augmentation/baseline_shift.py +119 -0
  3. chemotools/augmentation/exponential_noise.py +117 -0
  4. chemotools/augmentation/index_shift.py +120 -0
  5. chemotools/augmentation/normal_noise.py +118 -0
  6. chemotools/augmentation/spectrum_scale.py +120 -0
  7. chemotools/augmentation/uniform_noise.py +124 -0
  8. chemotools/baseline/__init__.py +20 -8
  9. chemotools/baseline/{air_pls.py → _air_pls.py} +20 -32
  10. chemotools/baseline/{ar_pls.py → _ar_pls.py} +18 -31
  11. chemotools/baseline/{constant_baseline_correction.py → _constant_baseline_correction.py} +37 -31
  12. chemotools/baseline/{cubic_spline_correction.py → _cubic_spline_correction.py} +26 -19
  13. chemotools/baseline/{linear_correction.py → _linear_correction.py} +19 -28
  14. chemotools/baseline/{non_negative.py → _non_negative.py} +15 -23
  15. chemotools/baseline/{polynomial_correction.py → _polynomial_correction.py} +29 -31
  16. chemotools/baseline/{subtract_reference.py → _subtract_reference.py} +23 -27
  17. chemotools/datasets/__init__.py +5 -0
  18. chemotools/datasets/_base.py +122 -0
  19. chemotools/datasets/data/coffee_labels.csv +61 -0
  20. chemotools/datasets/data/coffee_spectra.csv +61 -0
  21. chemotools/datasets/data/fermentation_hplc.csv +35 -0
  22. chemotools/datasets/data/fermentation_spectra.csv +1630 -0
  23. chemotools/datasets/data/train_hplc.csv +22 -0
  24. chemotools/datasets/data/train_spectra.csv +22 -0
  25. chemotools/derivative/__init__.py +4 -2
  26. chemotools/derivative/{norris_william.py → _norris_william.py} +20 -25
  27. chemotools/derivative/{savitzky_golay.py → _savitzky_golay.py} +26 -36
  28. chemotools/feature_selection/__init__.py +4 -0
  29. chemotools/feature_selection/_index_selector.py +113 -0
  30. chemotools/feature_selection/_range_cut.py +111 -0
  31. chemotools/scale/__init__.py +5 -3
  32. chemotools/scale/{min_max_scaler.py → _min_max_scaler.py} +36 -39
  33. chemotools/scale/{norm_scaler.py → _norm_scaler.py} +18 -25
  34. chemotools/scale/_point_scaler.py +115 -0
  35. chemotools/scatter/__init__.py +13 -2
  36. chemotools/scatter/_extended_multiplicative_scatter_correction.py +183 -0
  37. chemotools/scatter/_multiplicative_scatter_correction.py +169 -0
  38. chemotools/scatter/_robust_normal_variate.py +101 -0
  39. chemotools/scatter/{standard_normal_variate.py → _standard_normal_variate.py} +21 -26
  40. chemotools/smooth/__init__.py +6 -4
  41. chemotools/smooth/{mean_filter.py → _mean_filter.py} +18 -25
  42. chemotools/smooth/{median_filter.py → _median_filter.py} +32 -24
  43. chemotools/smooth/{savitzky_golay_filter.py → _savitzky_golay_filter.py} +22 -24
  44. chemotools/smooth/{whittaker_smooth.py → _whittaker_smooth.py} +24 -29
  45. {chemotools-0.0.22.dist-info → chemotools-0.1.6.dist-info}/METADATA +19 -15
  46. chemotools-0.1.6.dist-info/RECORD +51 -0
  47. {chemotools-0.0.22.dist-info → chemotools-0.1.6.dist-info}/WHEEL +1 -2
  48. chemotools/scale/index_scaler.py +0 -97
  49. chemotools/scatter/extended_multiplicative_scatter_correction.py +0 -33
  50. chemotools/scatter/multiplicative_scatter_correction.py +0 -123
  51. chemotools/utils/check_inputs.py +0 -14
  52. chemotools/variable_selection/__init__.py +0 -1
  53. chemotools/variable_selection/range_cut.py +0 -121
  54. chemotools-0.0.22.dist-info/RECORD +0 -39
  55. chemotools-0.0.22.dist-info/top_level.txt +0 -2
  56. tests/fixtures.py +0 -89
  57. tests/test_functionality.py +0 -397
  58. tests/test_sklearn_compliance.py +0 -192
  59. {tests → chemotools/datasets/data}/__init__.py +0 -0
  60. {chemotools-0.0.22.dist-info → chemotools-0.1.6.dist-info}/LICENSE +0 -0
@@ -1,121 +0,0 @@
1
- import numpy as np
2
- from sklearn.base import BaseEstimator, TransformerMixin, OneToOneFeatureMixin
3
- from sklearn.utils.validation import check_is_fitted
4
-
5
- from chemotools.utils.check_inputs import check_input
6
-
7
-
8
- class RangeCut(OneToOneFeatureMixin, BaseEstimator, TransformerMixin):
9
- """
10
- A transformer that cuts the input data to a specified range. The range is specified:
11
- - by the indices of the start and end of the range,
12
- - by the wavenumbers of the start and end of the range. In this case, the wavenumbers
13
- must be provided to the transformer when it is initialised. If the wavenumbers
14
- are not provided, the indices will be used instead. The wavenumbers must be
15
- provided in ascending order.
16
-
17
- Parameters
18
- ----------
19
- wavenumbers : array-like, optional
20
- The wavenumbers of the input data. If not provided, the indices will be used
21
- instead. Default is None. If provided, the wavenumbers must be provided in
22
- ascending order.
23
-
24
- start : int, optional
25
- The index or wavenumber of the start of the range. Default is 0.
26
-
27
- end : int, optional
28
- The index or wavenumber of the end of the range. Default is -1.
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
- Methods
39
- -------
40
- fit(X, y=None)
41
- Fit the transformer to the input data.
42
-
43
- transform(X, y=0, copy=True)
44
- Transform the input data by cutting it to the specified range.
45
- """
46
- def __init__(
47
- self,
48
- wavenumbers: np.ndarray = None,
49
- start: int = 0,
50
- end: int = -1,
51
- ):
52
- self.wavenumbers = wavenumbers
53
- self.start = self._find_index(start)
54
- self.end = self._find_index(end)
55
-
56
- def fit(self, X: np.ndarray, y=None) -> "RangeCut":
57
- """
58
- Fit the transformer to the input data.
59
-
60
- Parameters
61
- ----------
62
- X : array-like of shape (n_samples, n_features)
63
- The input data to fit the transformer to.
64
-
65
- y : None
66
- Ignored.
67
-
68
- Returns
69
- -------
70
- self : RangeCut
71
- The fitted transformer.
72
- """
73
- # Check that X is a 2D array and has only finite values
74
- X = check_input(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
81
-
82
- return self
83
-
84
- def transform(self, X: np.ndarray, y=None) -> np.ndarray:
85
- """
86
- Transform the input data by cutting it to the specified range.
87
-
88
- Parameters
89
- ----------
90
- X : array-like of shape (n_samples, n_features)
91
- The input data to transform.
92
-
93
- y : None
94
- Ignored.
95
-
96
- Returns
97
- -------
98
- X_ : np.ndarray of shape (n_samples, n_features)
99
- The transformed data.
100
- """
101
- # Check that the estimator is fitted
102
- check_is_fitted(self, "_is_fitted")
103
-
104
- # Check that X is a 2D array and has only finite values
105
- X = check_input(X)
106
- X_ = X.copy()
107
-
108
- # Check that the number of features is the same as the fitted data
109
- if X_.shape[1] != self.n_features_in_:
110
- raise ValueError(
111
- f"Expected {self.n_features_in_} features but got {X_.shape[1]}"
112
- )
113
-
114
- # Range cut the spectra
115
- return X_[:, self.start : self.end]
116
-
117
- def _find_index(self, target: float) -> int:
118
- if self.wavenumbers is None:
119
- return target
120
- wavenumbers = np.array(self.wavenumbers)
121
- return np.argmin(np.abs(wavenumbers - target))
@@ -1,39 +0,0 @@
1
- chemotools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- chemotools/baseline/__init__.py,sha256=W61mEZU_9-sVGRkP2MJOIhd6e9KsOS1BYjxm1NOMIyM,373
3
- chemotools/baseline/air_pls.py,sha256=qn03l66IrxW7woFbRmRqCmfZHzQ7KKW8A7ogTxTHKo0,5443
4
- chemotools/baseline/ar_pls.py,sha256=OY2cpU2X6KIBR9ag3PAJXo_uQbniIV58zbUJxCxvZWs,4736
5
- chemotools/baseline/constant_baseline_correction.py,sha256=vkLrAETn71imA5v5x-KvI2B_QSwyjdAq7vzbf7reQyY,3727
6
- chemotools/baseline/cubic_spline_correction.py,sha256=PCHqR7TAhbdlTZrxgedlk0PU0kRUwQd_jymh0g-ieo8,3311
7
- chemotools/baseline/linear_correction.py,sha256=6Sw2n4QTvIDKWRdJpFD48hMvOEwqbctUAQLF1WwcoXs,3381
8
- chemotools/baseline/non_negative.py,sha256=17_82l95U9kgoQ3Pdz3-jGv8B51JzqPdHODt6PegWRw,2864
9
- chemotools/baseline/polynomial_correction.py,sha256=caP866fwZb7PASyz6oezgg8hdZtFMT0EimK89TGSTSc,4059
10
- chemotools/baseline/subtract_reference.py,sha256=Pht87XadXK0URq2fun66OHaUk_cx56AkF84ta3VJy_8,3441
11
- chemotools/derivative/__init__.py,sha256=x2F0IJ-uCbEYFoXFbZl_RTPCbSq82vqGOwlM9R_2Klo,84
12
- chemotools/derivative/norris_william.py,sha256=M7Uax3Aq44CbqmRO0GvP34NVxfRr9NYh5QRwhIAjpNU,5015
13
- chemotools/derivative/savitzky_golay.py,sha256=fFzQRVGVXQIUkHp1x9dqfLVPlyStubIhSj9aGfZKuXY,3745
14
- chemotools/scale/__init__.py,sha256=qRDhHXhkwXrr0a9ctKVpjp8X8H8Wcu2pelavehv-8ik,115
15
- chemotools/scale/index_scaler.py,sha256=GsSVEfhVud-ZSVF7YwJBbix976W4a-1SXtbjUtQdqZ4,2661
16
- chemotools/scale/min_max_scaler.py,sha256=zjhPhP5PcLh796VhNxo73INutGkUThe08B6IxMVD3X8,2850
17
- chemotools/scale/norm_scaler.py,sha256=qNs-npf5Jqcp8RYqt88_5-zwd-yIo-J1jItgUTFeozs,2699
18
- chemotools/scatter/__init__.py,sha256=n47sqTpKK4N8bt6howklP8Z2ceZrkASTqfztwfGlEfc,137
19
- chemotools/scatter/extended_multiplicative_scatter_correction.py,sha256=Xe2tEF06zXe4Kuox9NyR6e2jkCBx4zkZaJc9zgwxngE,1073
20
- chemotools/scatter/multiplicative_scatter_correction.py,sha256=Qme8lSQdyaUmQ2vbtVUTpOkm5tZvDHBCOuPlQ3dGMIU,3955
21
- chemotools/scatter/standard_normal_variate.py,sha256=wmK_8ea2CvoLaGebBFKr8zAU7QjGbaKAg04y6iZ4sDc,2681
22
- chemotools/smooth/__init__.py,sha256=Kwg3jVnl-W-efTHMR6-6hQsTp-An1lYQ1lZFj6sNMtg,176
23
- chemotools/smooth/mean_filter.py,sha256=fcC4EjO57Br3I9SJqWDJRxPxAv2WjjmXTECdBmBYXLI,2953
24
- chemotools/smooth/median_filter.py,sha256=5tR931HIej-yrw1SoV9t09gi55QKbZ3eCTeO-EjNSU8,2966
25
- chemotools/smooth/savitzky_golay_filter.py,sha256=OlkW4-gHsgk7HFf7yeweKkL6aOZpNqMSbUpvKjC66KY,3523
26
- chemotools/smooth/whittaker_smooth.py,sha256=OVEYEstsURgkLbjwRiBWeN_XNs_JOFeD60uyZsVtrHQ,3664
27
- chemotools/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
- chemotools/utils/check_inputs.py,sha256=fRAV4HIaGamdj_PNXSNnl7LurXytACNTGO51rhPpMUY,512
29
- chemotools/variable_selection/__init__.py,sha256=E5WmqGRkM6XgzmhTolP3Tu9KyCtEDk_Jcc1bEHvxVEg,31
30
- chemotools/variable_selection/range_cut.py,sha256=Gh6flGp616k8gFBNBzxjfz49lncrFulMWukTOPExDTg,3709
31
- tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
32
- tests/fixtures.py,sha256=Xa-Vd62Kd1fyWg3PLUSP6iIkOK8etrbyOkMJTn3dvX8,1933
33
- tests/test_functionality.py,sha256=LsoRlbcWYeK2DJD3Z6S6mo6omFHKCw7dFhzO_jBuP0I,10557
34
- tests/test_sklearn_compliance.py,sha256=IBsAQ-U5vo11ezfiutXY5HU9DoorxIkTF_6EXa2Fwbc,4100
35
- chemotools-0.0.22.dist-info/LICENSE,sha256=qtyOy2wDQVX9hxp58h3T-6Lmfv-mSCHoSRkcLUdM9bg,1070
36
- chemotools-0.0.22.dist-info/METADATA,sha256=W3QxPFeaCLaiG9-YOXKwnAJs40DoPRA7sLGURnSdOxQ,4993
37
- chemotools-0.0.22.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
38
- chemotools-0.0.22.dist-info/top_level.txt,sha256=eNcNcKSdo-1H_2gwSDrS__dr7BM3R73Cnn-pBiW5FEw,17
39
- chemotools-0.0.22.dist-info/RECORD,,
@@ -1,2 +0,0 @@
1
- chemotools
2
- tests
tests/fixtures.py DELETED
@@ -1,89 +0,0 @@
1
- import numpy as np
2
- import os
3
- import pytest
4
-
5
-
6
- test_directory = os.path.dirname(os.path.abspath(__file__))
7
-
8
- path_to_resources = os.path.join(test_directory, "resources")
9
-
10
-
11
- @pytest.fixture
12
- def spectrum() -> np.ndarray:
13
- return [
14
- np.loadtxt(
15
- os.path.join(path_to_resources, "spectrum.csv"), delimiter=","
16
- ).tolist()
17
- ]
18
-
19
-
20
- @pytest.fixture
21
- def spectrum_arpls() -> np.ndarray:
22
- return [
23
- np.loadtxt(
24
- os.path.join(path_to_resources, "spectrum_arpls.csv"), delimiter=","
25
- ).tolist()
26
- ]
27
-
28
-
29
- @pytest.fixture
30
- def reference_airpls() -> np.ndarray:
31
- return [
32
- np.loadtxt(
33
- os.path.join(path_to_resources, "reference_airpls.csv"), delimiter=","
34
- ).tolist()
35
- ]
36
-
37
-
38
- @pytest.fixture
39
- def reference_arpls() -> np.ndarray:
40
- return [
41
- np.loadtxt(
42
- os.path.join(path_to_resources, "reference_arpls.csv"), delimiter=","
43
- ).tolist()
44
- ]
45
-
46
-
47
- @pytest.fixture
48
- def reference_msc_mean() -> np.ndarray:
49
- return [
50
- np.loadtxt(
51
- os.path.join(path_to_resources, "reference_msc_mean.csv"), delimiter=","
52
- ).tolist()
53
- ]
54
-
55
-
56
- @pytest.fixture
57
- def reference_msc_median() -> np.ndarray:
58
- return [
59
- np.loadtxt(
60
- os.path.join(path_to_resources, "reference_msc_median.csv"), delimiter=","
61
- ).tolist()
62
- ]
63
-
64
-
65
- @pytest.fixture
66
- def reference_sg_15_2() -> np.ndarray:
67
- return [
68
- np.loadtxt(
69
- os.path.join(path_to_resources, "reference_sg_15_2.csv"), delimiter=","
70
- ).tolist()
71
- ]
72
-
73
-
74
- @pytest.fixture
75
- def reference_snv() -> np.ndarray:
76
- return [
77
- np.loadtxt(
78
- os.path.join(path_to_resources, "reference_snv.csv"), delimiter=","
79
- ).tolist()
80
- ]
81
-
82
-
83
- @pytest.fixture
84
- def reference_whitakker() -> np.ndarray:
85
- return [
86
- np.loadtxt(
87
- os.path.join(path_to_resources, "reference_whitakker.csv"), delimiter=","
88
- ).tolist()
89
- ]
@@ -1,397 +0,0 @@
1
- import numpy as np
2
-
3
- from chemotools.baseline import (
4
- AirPls,
5
- ArPls,
6
- ConstantBaselineCorrection,
7
- LinearCorrection,
8
- NonNegative,
9
- SubtractReference,
10
- )
11
- from chemotools.derivative import NorrisWilliams, SavitzkyGolay
12
- from chemotools.scale import IndexScaler, MinMaxScaler, NormScaler
13
- from chemotools.scatter import MultiplicativeScatterCorrection, StandardNormalVariate
14
- from chemotools.smooth import MeanFilter, MedianFilter, WhittakerSmooth
15
- from chemotools.variable_selection import RangeCut
16
- from tests.fixtures import (
17
- spectrum,
18
- spectrum_arpls,
19
- reference_airpls,
20
- reference_arpls,
21
- reference_msc_mean,
22
- reference_msc_median,
23
- reference_sg_15_2,
24
- reference_snv,
25
- reference_whitakker,
26
- )
27
-
28
-
29
- def test_air_pls(spectrum, reference_airpls):
30
- # Arrange
31
- air_pls = AirPls()
32
-
33
- # Act
34
- spectrum_corrected = air_pls.fit_transform(spectrum)
35
-
36
- # Assert
37
- assert np.allclose(spectrum_corrected[0], reference_airpls[0], atol=1e-7)
38
-
39
-
40
- def test_ar_pls(spectrum_arpls, reference_arpls):
41
- # Arrange
42
- arpls = ArPls(1e2, 0.0001)
43
- reference = np.array(spectrum_arpls) - np.array(reference_arpls)
44
-
45
- # Act
46
- spectrum_corrected = arpls.fit_transform(spectrum_arpls)
47
-
48
- # Assert
49
- assert np.allclose(spectrum_corrected[0], reference[0], atol=1e-4)
50
-
51
-
52
- def test_constant_baseline_correction():
53
- # Arrange
54
- spectrum = np.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 1]).reshape(1, -1)
55
- constant_baseline_correction = ConstantBaselineCorrection(start=7, end=8)
56
-
57
- # Act
58
- spectrum_corrected = constant_baseline_correction.fit_transform(spectrum)
59
-
60
- # Assert
61
- expected = np.array([-1, -1, -1, -1, -1, -1, -1, 0, 0, -1])
62
- assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
63
-
64
-
65
- def test_constant_baseline_correction_with_wavenumbers():
66
- # Arrange
67
- spectrum = np.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 1]).reshape(1, -1)
68
- wavenumbers = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
69
- constant_baseline_correction = ConstantBaselineCorrection(
70
- wavenumbers=wavenumbers, start=9, end=10
71
- )
72
-
73
- # Act
74
- spectrum_corrected = constant_baseline_correction.fit_transform(spectrum)
75
-
76
- # Assert
77
- expected = np.array([-1, -1, -1, -1, -1, -1, -1, 0, 0, -1])
78
- assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
79
-
80
-
81
- def test_index_scaler(spectrum):
82
- # Arrange
83
- index_scaler = IndexScaler(index=0)
84
- reference_spectrum = [value / spectrum[0][0] for value in spectrum[0]]
85
- # Act
86
- spectrum_corrected = index_scaler.fit_transform(spectrum)
87
-
88
- # Assert
89
- assert np.allclose(spectrum_corrected[0], reference_spectrum, atol=1e-8)
90
-
91
-
92
- def test_l1_norm(spectrum):
93
- # Arrange
94
- norm = 1
95
- l1_norm = NormScaler(l_norm=norm)
96
- spectrum_norm = np.linalg.norm(spectrum[0], ord=norm)
97
-
98
- # Act
99
- spectrum_corrected = l1_norm.fit_transform(spectrum)
100
-
101
- # Assert
102
- assert np.allclose(spectrum_corrected[0], spectrum[0] / spectrum_norm, atol=1e-8)
103
-
104
-
105
- def test_l2_norm(spectrum):
106
- # Arrange
107
- norm = 2
108
- l1_norm = NormScaler(l_norm=norm)
109
- spectrum_norm = np.linalg.norm(spectrum[0], ord=norm)
110
-
111
- # Act
112
- spectrum_corrected = l1_norm.fit_transform(spectrum)
113
-
114
- # Assert
115
- assert np.allclose(spectrum_corrected[0], spectrum[0] / spectrum_norm, atol=1e-8)
116
-
117
-
118
- def test_linear_correction(spectrum):
119
- # Arrange
120
- linear_correction = LinearCorrection()
121
-
122
- # Act
123
- spectrum_corrected = linear_correction.fit_transform(spectrum)
124
-
125
- # Assert
126
- assert spectrum_corrected[0][0] == 0
127
- assert spectrum_corrected[-1][0] == 0
128
-
129
-
130
- def test_max_norm(spectrum):
131
- # Arrange
132
- max_norm = MinMaxScaler(norm="max")
133
-
134
- # Act
135
- spectrum_corrected = max_norm.fit_transform(spectrum)
136
-
137
- # Assert
138
- assert np.allclose(
139
- spectrum_corrected[0], spectrum[0] / np.max(spectrum[0]), atol=1e-8
140
- )
141
-
142
-
143
- def test_mean_filter():
144
- # Arrange
145
- array = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]])
146
- mean_filter = MeanFilter(window_size=2)
147
-
148
- # Act
149
- array_corrected = mean_filter.fit_transform(array)
150
-
151
- # Assert
152
- assert np.allclose(array_corrected[0], [1, 1.5, 2.5, 3.5, 4.5], atol=1e-8)
153
-
154
-
155
- def test_median_filter():
156
- # Arrange
157
- array = np.array([[1.0, 2.0, 30.0, 4.0, 5.0]])
158
- mean_filter = MedianFilter(window_size=3)
159
-
160
- # Act
161
- array_corrected = mean_filter.fit_transform(array)
162
-
163
- # Assert
164
- assert np.allclose(array_corrected[0], [1, 2.0, 4.0, 5.0, 5.0], atol=1e-8)
165
-
166
-
167
- def test_min_norm(spectrum):
168
- # Arrange
169
- min_norm = MinMaxScaler(norm="min")
170
-
171
- # Act
172
- spectrum_corrected = min_norm.fit_transform(spectrum)
173
-
174
- # Assert
175
- assert np.allclose(
176
- spectrum_corrected[0], spectrum[0] / np.min(spectrum[0]), atol=1e-8
177
- )
178
-
179
-
180
- def test_multiplicative_scatter_correction_mean(spectrum, reference_msc_mean):
181
- # Arrange
182
- msc = MultiplicativeScatterCorrection()
183
-
184
- # Act
185
- spectrum_corrected = msc.fit_transform(spectrum)
186
-
187
- # Assert
188
- assert np.allclose(spectrum_corrected[0], reference_msc_mean[0], atol=1e-8)
189
-
190
-
191
- def test_multiplicative_scatter_correction_with_reference(spectrum, reference_msc_mean):
192
- # Arrange
193
- msc = MultiplicativeScatterCorrection(reference=reference_msc_mean)
194
-
195
- # Act
196
- spectrum_corrected = msc.fit_transform(spectrum)
197
-
198
- # Assert
199
- assert np.allclose(spectrum_corrected[0], reference_msc_mean[0], atol=1e-8)
200
-
201
-
202
- def test_multiplicative_scatter_correction_median(spectrum, reference_msc_median):
203
- # Arrange
204
- msc = MultiplicativeScatterCorrection(use_median=True)
205
-
206
- # Act
207
- spectrum_corrected = msc.fit_transform(spectrum)
208
-
209
- # Assert
210
- assert np.allclose(spectrum_corrected[0], reference_msc_median[0], atol=1e-8)
211
-
212
-
213
- def test_multiplicative_scatter_correction_with_reference_median(
214
- spectrum, reference_msc_median
215
- ):
216
- # Arrange
217
- msc = MultiplicativeScatterCorrection(
218
- reference=reference_msc_median, use_median=True
219
- )
220
-
221
- # Act
222
- spectrum_corrected = msc.fit_transform(spectrum)
223
-
224
- # Assert
225
- assert np.allclose(spectrum_corrected[0], reference_msc_median[0], atol=1e-8)
226
-
227
-
228
- def test_non_negative_zeroes():
229
- # Arrange
230
- spectrum = np.array([[-1, 0, 1]])
231
- non_negative = NonNegative(mode="zero")
232
-
233
- # Act
234
- spectrum_corrected = non_negative.fit_transform(spectrum)
235
-
236
- # Assert
237
- assert np.allclose(spectrum_corrected[0], [0, 0, 1], atol=1e-8)
238
-
239
-
240
- def test_non_negative_absolute():
241
- # Arrange
242
- spectrum = np.array([[-1, 0, 1]])
243
- non_negative = NonNegative(mode="abs")
244
-
245
- # Act
246
- spectrum_corrected = non_negative.fit_transform(spectrum)
247
-
248
- # Assert
249
- assert np.allclose(spectrum_corrected[0], [1, 0, 1], atol=1e-8)
250
-
251
-
252
- def test_norris_williams_filter_1():
253
- # Arrange
254
- norris_williams_filter = NorrisWilliams()
255
- array = np.ones((1, 10)).reshape(1, -1)
256
-
257
- # Act
258
- spectrum_corrected = norris_williams_filter.fit_transform(array)
259
-
260
- # Assert
261
- assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
262
-
263
-
264
- def test_norris_williams_filter_2():
265
- # Arrange
266
- norris_williams_filter = NorrisWilliams(derivative_order=2)
267
- array = np.ones((1, 10)).reshape(1, -1)
268
-
269
- # Act
270
- spectrum_corrected = norris_williams_filter.fit_transform(array)
271
-
272
- # Assert
273
- assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
274
-
275
-
276
- def test_range_cut_by_index(spectrum):
277
- # Arrange
278
- range_cut = RangeCut(start=0, end=10)
279
-
280
- # Act
281
- spectrum_corrected = range_cut.fit_transform(spectrum)
282
-
283
- # Assert
284
- assert np.allclose(spectrum_corrected[0], spectrum[0][:10], atol=1e-8)
285
-
286
-
287
- def test_range_cut_by_wavenumber():
288
- # Arrange
289
- wavenumbers = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
290
- spectrum = np.array([[10, 12, 14, 16, 14, 12, 10, 12, 14, 16]])
291
- range_cut = RangeCut(wavenumbers, start=2.5, end=7.9)
292
-
293
- # Act
294
- spectrum_corrected = range_cut.fit_transform(spectrum)
295
-
296
- # Assert
297
- assert np.allclose(spectrum_corrected[0], spectrum[0][1:7], atol=1e-8)
298
-
299
-
300
- def test_range_cut_by_wavenumber_2():
301
- # Arrange
302
- wavenumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
303
- spectrum = np.array([[10, 12, 14, 16, 14, 12, 10, 12, 14, 16]])
304
- range_cut = RangeCut(wavenumbers, start=2.5, end=7.9)
305
-
306
- # Act
307
- spectrum_corrected = range_cut.fit_transform(spectrum)
308
-
309
- # Assert
310
- assert np.allclose(spectrum_corrected[0], spectrum[0][1:7], atol=1e-8)
311
-
312
-
313
- def test_savizky_golay_filter_1(spectrum, reference_sg_15_2):
314
- # Arrange
315
- savitzky_golay_filter = SavitzkyGolay(
316
- window_size=15, polynomial_order=2, derivate_order=1, mode="interp"
317
- )
318
-
319
- # Act
320
- spectrum_corrected = savitzky_golay_filter.fit_transform(spectrum)
321
-
322
- # Assert
323
- assert np.allclose(spectrum_corrected[0], reference_sg_15_2[0], atol=1e-2)
324
-
325
-
326
- def test_saviszky_golay_filter_2():
327
- # Arrange
328
- savitzky_golay_filter = SavitzkyGolay(
329
- window_size=3, polynomial_order=2, derivate_order=1, mode="interp"
330
- )
331
-
332
- array = np.ones((1, 10)).reshape(1, -1)
333
-
334
- # Act
335
- spectrum_corrected = savitzky_golay_filter.fit_transform(array)
336
-
337
- # Assert
338
- assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
339
-
340
-
341
- def test_saviszky_golay_filter_3():
342
- # Arrange
343
- savitzky_golay_filter = SavitzkyGolay(
344
- window_size=3, polynomial_order=2, derivate_order=1, mode="interp"
345
- )
346
-
347
- array = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]).reshape(1, -1)
348
-
349
- # Act
350
- spectrum_corrected = savitzky_golay_filter.fit_transform(array)
351
-
352
- # Assert
353
- assert np.allclose(spectrum_corrected[0], np.ones((1, 10)), atol=1e-2)
354
-
355
-
356
- def test_standard_normal_variate(spectrum, reference_snv):
357
- # Arrange
358
- snv = StandardNormalVariate()
359
-
360
- # Act
361
- spectrum_corrected = snv.fit_transform(spectrum)
362
-
363
- # Assert
364
- assert np.allclose(spectrum_corrected[0], reference_snv[0], atol=1e-2)
365
-
366
-
367
- def test_subtract_reference(spectrum):
368
- # Arrange
369
- baseline = SubtractReference(reference=spectrum)
370
-
371
- # Act
372
- spectrum_corrected = baseline.fit_transform(spectrum)
373
-
374
- # Assert
375
- assert np.allclose(spectrum_corrected[0], np.zeros(len(spectrum)), atol=1e-8)
376
-
377
-
378
- def test_subtract_reference_without_reference(spectrum):
379
- # Arrange
380
- baseline = SubtractReference()
381
-
382
- # Act
383
- spectrum_corrected = baseline.fit_transform(spectrum)
384
-
385
- # Assert
386
- assert np.allclose(spectrum_corrected[0], spectrum, atol=1e-8)
387
-
388
-
389
- def test_whitakker_smooth(spectrum, reference_whitakker):
390
- # Arrange
391
- whitakker_smooth = WhittakerSmooth()
392
-
393
- # Act
394
- spectrum_corrected = whitakker_smooth.fit_transform(spectrum)
395
-
396
- # Assert
397
- assert np.allclose(spectrum_corrected[0], reference_whitakker[0], atol=1e-8)