chemotools 0.0.27__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 (53) 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} +22 -30
  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 +3 -0
  18. chemotools/datasets/_base.py +85 -15
  19. chemotools/datasets/data/coffee_labels.csv +61 -0
  20. chemotools/datasets/data/coffee_spectra.csv +61 -0
  21. chemotools/derivative/__init__.py +4 -2
  22. chemotools/derivative/{norris_william.py → _norris_william.py} +17 -24
  23. chemotools/derivative/{savitzky_golay.py → _savitzky_golay.py} +26 -36
  24. chemotools/feature_selection/__init__.py +4 -0
  25. chemotools/{variable_selection/select_features.py → feature_selection/_index_selector.py} +32 -56
  26. chemotools/{variable_selection/range_cut.py → feature_selection/_range_cut.py} +25 -50
  27. chemotools/scale/__init__.py +5 -3
  28. chemotools/scale/{min_max_scaler.py → _min_max_scaler.py} +20 -27
  29. chemotools/scale/{norm_scaler.py → _norm_scaler.py} +18 -25
  30. chemotools/scale/{point_scaler.py → _point_scaler.py} +27 -32
  31. chemotools/scatter/__init__.py +13 -4
  32. chemotools/scatter/{extended_multiplicative_scatter_correction.py → _extended_multiplicative_scatter_correction.py} +19 -28
  33. chemotools/scatter/{multiplicative_scatter_correction.py → _multiplicative_scatter_correction.py} +19 -17
  34. chemotools/scatter/{robust_normal_variate.py → _robust_normal_variate.py} +15 -23
  35. chemotools/scatter/{standard_normal_variate.py → _standard_normal_variate.py} +21 -26
  36. chemotools/smooth/__init__.py +6 -4
  37. chemotools/smooth/{mean_filter.py → _mean_filter.py} +18 -25
  38. chemotools/smooth/{median_filter.py → _median_filter.py} +32 -24
  39. chemotools/smooth/{savitzky_golay_filter.py → _savitzky_golay_filter.py} +22 -24
  40. chemotools/smooth/{whittaker_smooth.py → _whittaker_smooth.py} +24 -29
  41. {chemotools-0.0.27.dist-info → chemotools-0.1.6.dist-info}/METADATA +19 -16
  42. chemotools-0.1.6.dist-info/RECORD +51 -0
  43. {chemotools-0.0.27.dist-info → chemotools-0.1.6.dist-info}/WHEEL +1 -2
  44. chemotools/utils/check_inputs.py +0 -14
  45. chemotools/variable_selection/__init__.py +0 -2
  46. chemotools-0.0.27.dist-info/RECORD +0 -49
  47. chemotools-0.0.27.dist-info/top_level.txt +0 -2
  48. tests/__init__.py +0 -0
  49. tests/fixtures.py +0 -89
  50. tests/test_datasets.py +0 -30
  51. tests/test_functionality.py +0 -616
  52. tests/test_sklearn_compliance.py +0 -220
  53. {chemotools-0.0.27.dist-info → chemotools-0.1.6.dist-info}/LICENSE +0 -0
@@ -1,616 +0,0 @@
1
- import numpy as np
2
- import pytest
3
-
4
- from chemotools.baseline import (
5
- AirPls,
6
- ArPls,
7
- ConstantBaselineCorrection,
8
- LinearCorrection,
9
- NonNegative,
10
- SubtractReference,
11
- )
12
- from chemotools.derivative import NorrisWilliams, SavitzkyGolay
13
- from chemotools.scale import MinMaxScaler, NormScaler, PointScaler
14
- from chemotools.scatter import (
15
- ExtendedMultiplicativeScatterCorrection,
16
- MultiplicativeScatterCorrection,
17
- RobustNormalVariate,
18
- StandardNormalVariate,
19
- )
20
- from chemotools.smooth import MeanFilter, MedianFilter, WhittakerSmooth
21
- from chemotools.variable_selection import RangeCut, SelectFeatures
22
- from tests.fixtures import (
23
- spectrum,
24
- spectrum_arpls,
25
- reference_airpls,
26
- reference_arpls,
27
- reference_msc_mean,
28
- reference_msc_median,
29
- reference_sg_15_2,
30
- reference_snv,
31
- reference_whitakker,
32
- )
33
-
34
-
35
- def test_air_pls(spectrum, reference_airpls):
36
- # Arrange
37
- air_pls = AirPls()
38
-
39
- # Act
40
- spectrum_corrected = air_pls.fit_transform(spectrum)
41
-
42
- # Assert
43
- assert np.allclose(spectrum_corrected[0], reference_airpls[0], atol=1e-7)
44
-
45
-
46
- def test_ar_pls(spectrum_arpls, reference_arpls):
47
- # Arrange
48
- arpls = ArPls(1e2, 0.0001)
49
- reference = np.array(spectrum_arpls) - np.array(reference_arpls)
50
-
51
- # Act
52
- spectrum_corrected = arpls.fit_transform(spectrum_arpls)
53
-
54
- # Assert
55
- assert np.allclose(spectrum_corrected[0], reference[0], atol=1e-4)
56
-
57
-
58
- def test_constant_baseline_correction():
59
- # Arrange
60
- spectrum = np.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 1]).reshape(1, -1)
61
- constant_baseline_correction = ConstantBaselineCorrection(start=7, end=8)
62
-
63
- # Act
64
- spectrum_corrected = constant_baseline_correction.fit_transform(spectrum)
65
-
66
- # Assert
67
- expected = np.array([-1, -1, -1, -1, -1, -1, -1, 0, 0, -1])
68
- assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
69
-
70
-
71
- def test_constant_baseline_correction_with_wavenumbers():
72
- # Arrange
73
- spectrum = np.array([1, 1, 1, 1, 1, 1, 1, 2, 2, 1]).reshape(1, -1)
74
- wavenumbers = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
75
- constant_baseline_correction = ConstantBaselineCorrection(
76
- start=9, end=10, wavenumbers=wavenumbers
77
- )
78
-
79
- # Act
80
- spectrum_corrected = constant_baseline_correction.fit_transform(spectrum)
81
-
82
- # Assert
83
- expected = np.array([-1, -1, -1, -1, -1, -1, -1, 0, 0, -1])
84
- assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
85
-
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
-
185
- def test_l1_norm(spectrum):
186
- # Arrange
187
- norm = 1
188
- l1_norm = NormScaler(l_norm=norm)
189
- spectrum_norm = np.linalg.norm(spectrum[0], ord=norm)
190
-
191
- # Act
192
- spectrum_corrected = l1_norm.fit_transform(spectrum)
193
-
194
- # Assert
195
- assert np.allclose(spectrum_corrected[0], spectrum[0] / spectrum_norm, atol=1e-8)
196
-
197
-
198
- def test_l2_norm(spectrum):
199
- # Arrange
200
- norm = 2
201
- l1_norm = NormScaler(l_norm=norm)
202
- spectrum_norm = np.linalg.norm(spectrum[0], ord=norm)
203
-
204
- # Act
205
- spectrum_corrected = l1_norm.fit_transform(spectrum)
206
-
207
- # Assert
208
- assert np.allclose(spectrum_corrected[0], spectrum[0] / spectrum_norm, atol=1e-8)
209
-
210
-
211
- def test_linear_correction(spectrum):
212
- # Arrange
213
- linear_correction = LinearCorrection()
214
-
215
- # Act
216
- spectrum_corrected = linear_correction.fit_transform(spectrum)
217
-
218
- # Assert
219
- assert spectrum_corrected[0][0] == 0
220
- assert spectrum_corrected[-1][0] == 0
221
-
222
-
223
- def test_max_norm(spectrum):
224
- # Arrange
225
- max_norm = MinMaxScaler(use_min=False)
226
-
227
- # Act
228
- spectrum_corrected = max_norm.fit_transform(spectrum)
229
-
230
- # Assert
231
- assert np.allclose(
232
- spectrum_corrected[0], spectrum[0] / np.max(spectrum[0]), atol=1e-8
233
- )
234
-
235
-
236
- def test_mean_filter():
237
- # Arrange
238
- array = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]])
239
- mean_filter = MeanFilter(window_size=2)
240
-
241
- # Act
242
- array_corrected = mean_filter.fit_transform(array)
243
-
244
- # Assert
245
- assert np.allclose(array_corrected[0], [1, 1.5, 2.5, 3.5, 4.5], atol=1e-8)
246
-
247
-
248
- def test_median_filter():
249
- # Arrange
250
- array = np.array([[1.0, 2.0, 30.0, 4.0, 5.0]])
251
- mean_filter = MedianFilter(window_size=3)
252
-
253
- # Act
254
- array_corrected = mean_filter.fit_transform(array)
255
-
256
- # Assert
257
- assert np.allclose(array_corrected[0], [1, 2.0, 4.0, 5.0, 5.0], atol=1e-8)
258
-
259
-
260
- def test_min_norm(spectrum):
261
- # Arrange
262
- min_norm = MinMaxScaler()
263
-
264
- # Act
265
- spectrum_corrected = min_norm.fit_transform(spectrum)
266
-
267
- # Assert
268
- assert np.allclose(
269
- spectrum_corrected[0],
270
- (spectrum[0] - np.min(spectrum[0]))
271
- / (np.max(spectrum[0]) - np.min(spectrum[0])),
272
- atol=1e-8,
273
- )
274
-
275
-
276
- def test_multiplicative_scatter_correction_mean(spectrum, reference_msc_mean):
277
- # Arrange
278
- msc = MultiplicativeScatterCorrection()
279
-
280
- # Act
281
- spectrum_corrected = msc.fit_transform(spectrum)
282
-
283
- # Assert
284
- assert np.allclose(spectrum_corrected[0], reference_msc_mean[0], atol=1e-8)
285
-
286
-
287
- def test_multiplicative_scatter_correction_with_reference(spectrum, reference_msc_mean):
288
- # Arrange
289
- msc = MultiplicativeScatterCorrection(reference=reference_msc_mean[0])
290
-
291
- # Act
292
- spectrum_corrected = msc.fit_transform(spectrum)
293
-
294
- # Assert
295
- assert np.allclose(spectrum_corrected[0], reference_msc_mean[0], atol=1e-8)
296
-
297
-
298
- def test_multiplicative_scatter_correction_median(spectrum, reference_msc_median):
299
- # Arrange
300
- msc = MultiplicativeScatterCorrection(use_median=True)
301
-
302
- # Act
303
- spectrum_corrected = msc.fit_transform(spectrum)
304
-
305
- # Assert
306
- assert np.allclose(spectrum_corrected[0], reference_msc_median[0], atol=1e-8)
307
-
308
-
309
- def test_multiplicative_scatter_correction_with_reference_median(
310
- spectrum, reference_msc_median
311
- ):
312
- # Arrange
313
- msc = MultiplicativeScatterCorrection(
314
- reference=reference_msc_median[0], use_median=True
315
- )
316
-
317
- # Act
318
- spectrum_corrected = msc.fit_transform(spectrum)
319
-
320
- # Assert
321
- assert np.allclose(spectrum_corrected[0], reference_msc_median[0], atol=1e-8)
322
-
323
-
324
- def test_multiplicative_scatter_correction_with_weights(spectrum, reference_msc_mean):
325
- # Arrange
326
- weights = np.ones(len(spectrum[0]))
327
-
328
- msc = MultiplicativeScatterCorrection(weights=weights)
329
-
330
- # Act
331
- spectrum_corrected = msc.fit_transform(spectrum)
332
-
333
- # Assert
334
- assert np.allclose(spectrum_corrected[0], reference_msc_mean[0], atol=1e-8)
335
-
336
-
337
- def test_multiplicative_scatter_correction_with_wrong_weights(
338
- spectrum, reference_msc_mean
339
- ):
340
- # Arrange
341
- weights = np.ones(10)
342
- msc = MultiplicativeScatterCorrection(weights=weights)
343
-
344
- # Act & Assert
345
- with pytest.raises(ValueError):
346
- msc.fit_transform(spectrum)
347
-
348
-
349
- def test_multiplicative_scatter_correction_with_wrong_reference(spectrum):
350
- # Arrange
351
- reference = np.ones(10)
352
- msc = MultiplicativeScatterCorrection(reference=reference)
353
-
354
- # Act & Assert
355
- with pytest.raises(ValueError):
356
- msc.fit_transform(spectrum)
357
-
358
-
359
- def test_multiplicative_scatter_correction_no_mean_no_median_no_reference(spectrum):
360
- # Arrange
361
- reference = np.ones(10)
362
- msc = MultiplicativeScatterCorrection(use_mean=False)
363
-
364
- # Act & Assert
365
- with pytest.raises(ValueError):
366
- msc.fit_transform(spectrum)
367
-
368
-
369
- def test_non_negative_zeroes():
370
- # Arrange
371
- spectrum = np.array([[-1, 0, 1]])
372
- non_negative = NonNegative(mode="zero")
373
-
374
- # Act
375
- spectrum_corrected = non_negative.fit_transform(spectrum)
376
-
377
- # Assert
378
- assert np.allclose(spectrum_corrected[0], [0, 0, 1], atol=1e-8)
379
-
380
-
381
- def test_non_negative_absolute():
382
- # Arrange
383
- spectrum = np.array([[-1, 0, 1]])
384
- non_negative = NonNegative(mode="abs")
385
-
386
- # Act
387
- spectrum_corrected = non_negative.fit_transform(spectrum)
388
-
389
- # Assert
390
- assert np.allclose(spectrum_corrected[0], [1, 0, 1], atol=1e-8)
391
-
392
-
393
- def test_norris_williams_filter_1():
394
- # Arrange
395
- norris_williams_filter = NorrisWilliams()
396
- array = np.ones((1, 10)).reshape(1, -1)
397
-
398
- # Act
399
- spectrum_corrected = norris_williams_filter.fit_transform(array)
400
-
401
- # Assert
402
- assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
403
-
404
-
405
- def test_norris_williams_filter_2():
406
- # Arrange
407
- norris_williams_filter = NorrisWilliams(derivative_order=2)
408
- array = np.ones((1, 10)).reshape(1, -1)
409
-
410
- # Act
411
- spectrum_corrected = norris_williams_filter.fit_transform(array)
412
-
413
- # Assert
414
- assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
415
-
416
-
417
- def test_norris_williams_wrong_filter():
418
- # Arrange
419
- norris_williams_filter = NorrisWilliams(derivative_order=5)
420
- array = np.ones((1, 10)).reshape(1, -1)
421
-
422
- # Act & Assert
423
-
424
- with pytest.raises(ValueError):
425
- norris_williams_filter.fit_transform(array)
426
-
427
-
428
- def test_point_scaler(spectrum):
429
- # Arrange
430
- index_scaler = PointScaler(point=0)
431
- reference_spectrum = [value / spectrum[0][0] for value in spectrum[0]]
432
-
433
- # Act
434
- spectrum_corrected = index_scaler.fit_transform(spectrum)
435
-
436
- # Assert
437
- assert np.allclose(spectrum_corrected[0], reference_spectrum, atol=1e-8)
438
-
439
-
440
- def test_point_scaler_with_wavenumbers():
441
- # Arrange
442
- wavenumbers = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
443
- spectrum = np.array([[10.0, 12.0, 14.0, 16.0, 14.0, 12.0, 10.0, 12.0, 14.0, 16.0]])
444
-
445
- # Act
446
- index_scaler = PointScaler(point=4, wavenumbers=wavenumbers)
447
- spectrum_corrected = index_scaler.fit_transform(spectrum)
448
-
449
- # Assert
450
- assert np.allclose(spectrum_corrected[0], spectrum[0] / spectrum[0][3], atol=1e-8)
451
-
452
-
453
- def test_range_cut_by_index(spectrum):
454
- # Arrange
455
- range_cut = RangeCut(start=0, end=10)
456
-
457
- # Act
458
- spectrum_corrected = range_cut.fit_transform(spectrum)
459
-
460
- # Assert
461
- assert np.allclose(spectrum_corrected[0], spectrum[0][:10], atol=1e-8)
462
-
463
-
464
- def test_range_cut_by_wavenumber():
465
- # Arrange
466
- wavenumbers = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
467
- spectrum = np.array([[10, 12, 14, 16, 14, 12, 10, 12, 14, 16]])
468
- range_cut = RangeCut(start=2.5, end=7.9, wavenumbers=wavenumbers)
469
-
470
- # Act
471
- spectrum_corrected = range_cut.fit_transform(spectrum)
472
-
473
- # Assert
474
- assert np.allclose(spectrum_corrected[0], spectrum[0][1:7], atol=1e-8)
475
-
476
-
477
- def test_range_cut_by_wavenumber_2():
478
- # Arrange
479
- wavenumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
480
- spectrum = np.array([[10, 12, 14, 16, 14, 12, 10, 12, 14, 16]])
481
- range_cut = RangeCut(start=2.5, end=7.9, wavenumbers=wavenumbers)
482
-
483
- # Act
484
- spectrum_corrected = range_cut.fit_transform(spectrum)
485
-
486
- # Assert
487
- assert np.allclose(spectrum_corrected[0], spectrum[0][1:7], atol=1e-8)
488
-
489
-
490
- def test_robust_normal_variate():
491
- # Arrange
492
- spectrum = np.array([2, 3.5, 5, 27, 8, 9]).reshape(1, -1)
493
- reference = np.array([-2.5, -0.5, 1.5, 30.833333, 5.5, 6.83333333])
494
- rnv = RobustNormalVariate()
495
-
496
- # Act
497
- spectrum_corrected = rnv.fit_transform(spectrum)
498
-
499
- # Assert
500
- assert np.allclose(spectrum_corrected[0], reference, atol=1e-8)
501
-
502
-
503
- def test_savizky_golay_filter_1(spectrum, reference_sg_15_2):
504
- # Arrange
505
- savitzky_golay_filter = SavitzkyGolay(
506
- window_size=15, polynomial_order=2, derivate_order=1, mode="interp"
507
- )
508
-
509
- # Act
510
- spectrum_corrected = savitzky_golay_filter.fit_transform(spectrum)
511
-
512
- # Assert
513
- assert np.allclose(spectrum_corrected[0], reference_sg_15_2[0], atol=1e-2)
514
-
515
-
516
- def test_saviszky_golay_filter_2():
517
- # Arrange
518
- savitzky_golay_filter = SavitzkyGolay(
519
- window_size=3, polynomial_order=2, derivate_order=1, mode="interp"
520
- )
521
-
522
- array = np.ones((1, 10)).reshape(1, -1)
523
-
524
- # Act
525
- spectrum_corrected = savitzky_golay_filter.fit_transform(array)
526
-
527
- # Assert
528
- assert np.allclose(spectrum_corrected[0], np.zeros((1, 10)), atol=1e-2)
529
-
530
-
531
- def test_saviszky_golay_filter_3():
532
- # Arrange
533
- savitzky_golay_filter = SavitzkyGolay(
534
- window_size=3, polynomial_order=2, derivate_order=1, mode="interp"
535
- )
536
-
537
- 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)
538
-
539
- # Act
540
- spectrum_corrected = savitzky_golay_filter.fit_transform(array)
541
-
542
- # Assert
543
- assert np.allclose(spectrum_corrected[0], np.ones((1, 10)), atol=1e-2)
544
-
545
-
546
- def test_select_features():
547
- # Arrange
548
- spectrum = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
549
- expected = np.array([[1, 2, 3, 8, 9, 10]])
550
-
551
- # Act
552
- select_features = SelectFeatures(features=np.array([0, 1, 2, 7, 8, 9]))
553
- spectrum_corrected = select_features.fit_transform(spectrum)
554
-
555
- # Assert
556
- assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
557
-
558
-
559
- def test_select_features_with_wavenumbers():
560
- # Arrange
561
- wavenumbers = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
562
- spectrum = np.array([[1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0, 89.0]])
563
- expected = np.array([[1.0, 2.0, 3.0, 34.0, 55.0, 89.0]])
564
-
565
- # Act
566
- select_features = SelectFeatures(
567
- features=np.array([1, 2, 3, 8, 9, 10]), wavenumbers=wavenumbers
568
- )
569
- spectrum_corrected = select_features.fit_transform(spectrum)
570
-
571
- # Assert
572
- assert np.allclose(spectrum_corrected[0], expected, atol=1e-8)
573
-
574
-
575
- def test_standard_normal_variate(spectrum, reference_snv):
576
- # Arrange
577
- snv = StandardNormalVariate()
578
-
579
- # Act
580
- spectrum_corrected = snv.fit_transform(spectrum)
581
-
582
- # Assert
583
- assert np.allclose(spectrum_corrected[0], reference_snv[0], atol=1e-2)
584
-
585
-
586
- def test_subtract_reference(spectrum):
587
- # Arrange
588
- baseline = SubtractReference(reference=spectrum)
589
-
590
- # Act
591
- spectrum_corrected = baseline.fit_transform(spectrum)
592
-
593
- # Assert
594
- assert np.allclose(spectrum_corrected[0], np.zeros(len(spectrum)), atol=1e-8)
595
-
596
-
597
- def test_subtract_reference_without_reference(spectrum):
598
- # Arrange
599
- baseline = SubtractReference()
600
-
601
- # Act
602
- spectrum_corrected = baseline.fit_transform(spectrum)
603
-
604
- # Assert
605
- assert np.allclose(spectrum_corrected[0], spectrum, atol=1e-8)
606
-
607
-
608
- def test_whitakker_smooth(spectrum, reference_whitakker):
609
- # Arrange
610
- whitakker_smooth = WhittakerSmooth()
611
-
612
- # Act
613
- spectrum_corrected = whitakker_smooth.fit_transform(spectrum)
614
-
615
- # Assert
616
- assert np.allclose(spectrum_corrected[0], reference_whitakker[0], atol=1e-8)