|
1 | 1 | import numpy as np |
2 | 2 | import pytest |
3 | 3 | from numpy.polynomial import Polynomial |
4 | | -from scipy.interpolate import interp1d |
| 4 | + |
| 5 | +from diffpy.morph.morphs.morphsqueeze import MorphSqueeze |
5 | 6 |
|
6 | 7 |
|
7 | 8 | @pytest.mark.parametrize( |
|
20 | 21 | [0.1, 0.3], |
21 | 22 | # 4th order squeeze coefficients |
22 | 23 | [0.2, -0.01, 0.001, -0.001, 0.0001], |
| 24 | + # Testing zeros |
| 25 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
23 | 26 | ], |
24 | 27 | ) |
25 | 28 | def test_morphsqueeze(squeeze_coeffs): |
26 | | - # Uniform x-axis grid. This is the same x-axis for all data. |
27 | | - x = np.linspace(0, 10, 1000) |
28 | | - # Expected uniform target |
29 | | - y_expected = np.sin(x) |
30 | 29 |
|
31 | | - # Create polynomial based on a list of values for polynomial coefficients |
| 30 | + x_target = np.linspace(0, 10, 1000) |
| 31 | + y_target = np.sin(x_target) |
| 32 | + |
32 | 33 | squeeze_polynomial = Polynomial(squeeze_coeffs) |
33 | | - # Apply squeeze parameters to uniform data to get the squeezed data |
34 | | - x_squeezed = x + squeeze_polynomial(x) |
35 | | - y_squeezed = np.sin(x_squeezed) |
36 | | - |
37 | | - # Unsqueeze the data by interpolating back to uniform grid |
38 | | - y_unsqueezed = interp1d( |
39 | | - x_squeezed, |
40 | | - y_squeezed, |
41 | | - kind="cubic", |
42 | | - bounds_error=False, |
43 | | - fill_value="extrapolate", |
44 | | - )(x) |
45 | | - y_actual = y_unsqueezed |
46 | | - |
47 | | - # Check that the unsqueezed (actual) data matches the expected data |
| 34 | + x_squeezed = x_target + squeeze_polynomial(x_target) |
| 35 | + |
| 36 | + x_morph = x_target.copy() |
| 37 | + y_morph = np.sin(x_squeezed) |
| 38 | + |
| 39 | + morph = MorphSqueeze() |
| 40 | + morph.squeeze = squeeze_coeffs |
| 41 | + |
| 42 | + x_actual, y_actual, x_expected, y_expected = morph( |
| 43 | + x_morph, y_morph, x_target, y_target |
| 44 | + ) |
| 45 | + |
| 46 | + # Check that the morphed (actual) data matches the expected data |
48 | 47 | # Including tolerance error because of extrapolation error |
49 | | - assert np.allclose(y_actual, y_expected, atol=1) |
50 | | - |
51 | | - # This plotting code was used for the comments in the github |
52 | | - # PR https://github.com/diffpy/diffpy.morph/pull/180 |
53 | | - # plt.figure(figsize=(7, 4)) |
54 | | - # plt.plot(x, y_expected, color="black", label="Expected uniform data") |
55 | | - # plt.plot(x, y_squeezed, "--", color="purple", label="Squeezed data") |
56 | | - # plt.plot(x, y_unsqueezed, "--", color="gold", label="Unsqueezed data") |
57 | | - # plt.xlabel("x") |
58 | | - # plt.ylabel("y") |
59 | | - # plt.legend() |
60 | | - # plt.show() |
| 48 | + assert np.allclose(y_actual, y_expected, atol=0.1) |
0 commit comments