chemotools 0.0.22__py3-none-any.whl → 0.0.24__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.
@@ -1,4 +1,5 @@
1
1
  import numpy as np
2
+ import pytest
2
3
 
3
4
  from chemotools.baseline import (
4
5
  AirPls,
@@ -10,7 +11,12 @@ from chemotools.baseline import (
10
11
  )
11
12
  from chemotools.derivative import NorrisWilliams, SavitzkyGolay
12
13
  from chemotools.scale import IndexScaler, MinMaxScaler, NormScaler
13
- from chemotools.scatter import MultiplicativeScatterCorrection, StandardNormalVariate
14
+ from chemotools.scatter import (
15
+ ExtendedMultiplicativeScatterCorrection,
16
+ MultiplicativeScatterCorrection,
17
+ RobustNormalVariate,
18
+ StandardNormalVariate,
19
+ )
14
20
  from chemotools.smooth import MeanFilter, MedianFilter, WhittakerSmooth
15
21
  from chemotools.variable_selection import RangeCut
16
22
  from tests.fixtures import (
@@ -78,6 +84,104 @@ def test_constant_baseline_correction_with_wavenumbers():
78
84
  assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
79
85
 
80
86
 
87
+ def test_extended_baseline_correction():
88
+ # Arrange
89
+ spectrum = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).reshape(
90
+ 1, -1
91
+ )
92
+ reference = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
93
+ emsc = ExtendedMultiplicativeScatterCorrection(reference=reference)
94
+
95
+ # Act
96
+ spectrum_emsc = emsc.fit_transform(spectrum)
97
+
98
+ # Assert
99
+ assert np.allclose(spectrum_emsc[0], reference, atol=1e-8)
100
+
101
+
102
+ def test_extended_baseline_correction_with_weights():
103
+ # Arrange
104
+ spectrum = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).reshape(
105
+ 1, -1
106
+ )
107
+ reference = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
108
+ weights = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
109
+ emsc = ExtendedMultiplicativeScatterCorrection(reference=reference, weights=weights)
110
+
111
+ # Act
112
+ spectrum_emsc = emsc.fit_transform(spectrum)
113
+
114
+ # Assert
115
+ assert np.allclose(spectrum_emsc[0], reference, atol=1e-8)
116
+
117
+
118
+ def test_extended_baseline_correction_with_wrong_reference():
119
+ # Arrange
120
+ spectrum = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).reshape(
121
+ 1, -1
122
+ )
123
+ reference = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
124
+
125
+ # Act
126
+ emsc = ExtendedMultiplicativeScatterCorrection(reference=reference)
127
+
128
+ # Assert
129
+ with pytest.raises(ValueError):
130
+ emsc.fit_transform(spectrum)
131
+
132
+
133
+ def test_extended_baseline_correction_with_wrong_weights():
134
+ # Arrange
135
+ spectrum = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]).reshape(
136
+ 1, -1
137
+ )
138
+ weights = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
139
+
140
+ # Act
141
+ emsc = ExtendedMultiplicativeScatterCorrection(weights=weights)
142
+
143
+ # Assert
144
+ with pytest.raises(ValueError):
145
+ emsc.fit_transform(spectrum)
146
+
147
+
148
+ def test_extended_baseline_correction_with_noreference_no_median_no_mean():
149
+ # Arrange
150
+ emsc = ExtendedMultiplicativeScatterCorrection(use_mean=False)
151
+
152
+ # Act & Assert
153
+ with pytest.raises(ValueError):
154
+ emsc.fit_transform(spectrum)
155
+
156
+
157
+ def test_extended_baseline_correction_through_msc(spectrum):
158
+ # EMSC of 0 order should be equivalient to MSC
159
+ # Arrange
160
+ msc = MultiplicativeScatterCorrection()
161
+ emsc = ExtendedMultiplicativeScatterCorrection(order=0)
162
+
163
+ # Act
164
+ spectrum_msc = msc.fit_transform(spectrum)
165
+ spectrum_emsc = emsc.fit_transform(spectrum)
166
+
167
+ # Assert
168
+ assert np.allclose(spectrum_emsc[0], spectrum_msc, atol=1e-8)
169
+
170
+
171
+ def test_extended_baseline_correction_through_msc_median(spectrum):
172
+ # EMSC of 0 order should be equivalient to MSC
173
+ # Arrange
174
+ msc = MultiplicativeScatterCorrection(use_median=True)
175
+ emsc = ExtendedMultiplicativeScatterCorrection(order=0, use_median=True)
176
+
177
+ # Act
178
+ spectrum_msc = msc.fit_transform(spectrum)
179
+ spectrum_emsc = emsc.fit_transform(spectrum)
180
+
181
+ # Assert
182
+ assert np.allclose(spectrum_emsc[0], spectrum_msc, atol=1e-8)
183
+
184
+
81
185
  def test_index_scaler(spectrum):
82
186
  # Arrange
83
187
  index_scaler = IndexScaler(index=0)
@@ -190,7 +294,7 @@ def test_multiplicative_scatter_correction_mean(spectrum, reference_msc_mean):
190
294
 
191
295
  def test_multiplicative_scatter_correction_with_reference(spectrum, reference_msc_mean):
192
296
  # Arrange
193
- msc = MultiplicativeScatterCorrection(reference=reference_msc_mean)
297
+ msc = MultiplicativeScatterCorrection(reference=reference_msc_mean[0])
194
298
 
195
299
  # Act
196
300
  spectrum_corrected = msc.fit_transform(spectrum)
@@ -214,9 +318,7 @@ def test_multiplicative_scatter_correction_with_reference_median(
214
318
  spectrum, reference_msc_median
215
319
  ):
216
320
  # Arrange
217
- msc = MultiplicativeScatterCorrection(
218
- reference=reference_msc_median, use_median=True
219
- )
321
+ msc = MultiplicativeScatterCorrection(reference=reference_msc_median[0], use_median=True)
220
322
 
221
323
  # Act
222
324
  spectrum_corrected = msc.fit_transform(spectrum)
@@ -225,6 +327,49 @@ def test_multiplicative_scatter_correction_with_reference_median(
225
327
  assert np.allclose(spectrum_corrected[0], reference_msc_median[0], atol=1e-8)
226
328
 
227
329
 
330
+ def test_multiplicative_scatter_correction_with_weights(spectrum, reference_msc_mean):
331
+ # Arrange
332
+ weights = np.ones(len(spectrum[0]))
333
+
334
+ msc = MultiplicativeScatterCorrection(weights=weights)
335
+
336
+ # Act
337
+ spectrum_corrected = msc.fit_transform(spectrum)
338
+
339
+ # Assert
340
+ assert np.allclose(spectrum_corrected[0], reference_msc_mean[0], atol=1e-8)
341
+
342
+
343
+ def test_multiplicative_scatter_correction_with_wrong_weights(spectrum, reference_msc_mean):
344
+ # Arrange
345
+ weights = np.ones(10)
346
+ msc = MultiplicativeScatterCorrection(weights=weights)
347
+
348
+ # Act & Assert
349
+ with pytest.raises(ValueError):
350
+ msc.fit_transform(spectrum)
351
+
352
+
353
+ def test_multiplicative_scatter_correction_with_wrong_reference(spectrum):
354
+ # Arrange
355
+ reference = np.ones(10)
356
+ msc = MultiplicativeScatterCorrection(reference=reference)
357
+
358
+ # Act & Assert
359
+ with pytest.raises(ValueError):
360
+ msc.fit_transform(spectrum)
361
+
362
+
363
+ def test_multiplicative_scatter_correction_no_mean_no_median_no_reference(spectrum):
364
+ # Arrange
365
+ reference = np.ones(10)
366
+ msc = MultiplicativeScatterCorrection(use_mean=False)
367
+
368
+ # Act & Assert
369
+ with pytest.raises(ValueError):
370
+ msc.fit_transform(spectrum)
371
+
372
+
228
373
  def test_non_negative_zeroes():
229
374
  # Arrange
230
375
  spectrum = np.array([[-1, 0, 1]])
@@ -273,6 +418,17 @@ def test_norris_williams_filter_2():
273
418
  assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
274
419
 
275
420
 
421
+ def test_norris_williams_wrong_filter():
422
+ # Arrange
423
+ norris_williams_filter = NorrisWilliams(derivative_order=5)
424
+ array = np.ones((1, 10)).reshape(1, -1)
425
+
426
+ # Act & Assert
427
+
428
+ with pytest.raises(ValueError):
429
+ norris_williams_filter.fit_transform(array)
430
+
431
+
276
432
  def test_range_cut_by_index(spectrum):
277
433
  # Arrange
278
434
  range_cut = RangeCut(start=0, end=10)
@@ -310,6 +466,19 @@ def test_range_cut_by_wavenumber_2():
310
466
  assert np.allclose(spectrum_corrected[0], spectrum[0][1:7], atol=1e-8)
311
467
 
312
468
 
469
+ def test_robust_normal_variate():
470
+ # Arrange
471
+ spectrum = np.array([2, 3.5, 5, 27, 8, 9]).reshape(1, -1)
472
+ reference = np.array([-2.5, -0.5, 1.5, 30.833333, 5.5, 6.83333333])
473
+ rnv = RobustNormalVariate()
474
+
475
+ # Act
476
+ spectrum_corrected = rnv.fit_transform(spectrum)
477
+
478
+ # Assert
479
+ assert np.allclose(spectrum_corrected[0], reference, atol=1e-8)
480
+
481
+
313
482
  def test_savizky_golay_filter_1(spectrum, reference_sg_15_2):
314
483
  # Arrange
315
484
  savitzky_golay_filter = SavitzkyGolay(
@@ -12,7 +12,12 @@ from chemotools.baseline import (
12
12
  )
13
13
  from chemotools.derivative import NorrisWilliams, SavitzkyGolay
14
14
  from chemotools.scale import IndexScaler, MinMaxScaler, NormScaler
15
- from chemotools.scatter import MultiplicativeScatterCorrection, StandardNormalVariate
15
+ from chemotools.scatter import (
16
+ ExtendedMultiplicativeScatterCorrection,
17
+ MultiplicativeScatterCorrection,
18
+ RobustNormalVariate,
19
+ StandardNormalVariate,
20
+ )
16
21
  from chemotools.smooth import (
17
22
  MeanFilter,
18
23
  MedianFilter,
@@ -56,6 +61,13 @@ def test_compliance_cubic_spline_correction():
56
61
  check_estimator(transformer)
57
62
 
58
63
 
64
+ # ExtendedMultiplicativeScatterCorrection
65
+ def test_compliance_extended_multiplicative_scatter_correction():
66
+ # Arrange
67
+ transformer = ExtendedMultiplicativeScatterCorrection()
68
+ # Act & Assert
69
+ check_estimator(transformer)
70
+
59
71
  # IndexScaler
60
72
  def test_compliance_index_scaler():
61
73
  # Arrange
@@ -176,6 +188,13 @@ def test_compliance_range_cut():
176
188
  check_estimator(transformer)
177
189
 
178
190
 
191
+ # RobustNormalVariate
192
+ def test_compliance_robust_normal_variate():
193
+ # Arrange
194
+ transformer = RobustNormalVariate()
195
+ # Act & Assert
196
+ check_estimator(transformer)
197
+
179
198
  # SubtractReference
180
199
  def test_compliance_subtract_reference():
181
200
  # Arrange