|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from numpy.polynomial import Polynomial |
| 4 | + |
| 5 | +from diffpy.morph.morphs.morphsqueeze import MorphSqueeze |
| 6 | + |
| 7 | +squeeze_coeffs_list = [ |
| 8 | + # The order of coefficients is [a0, a1, a2, ..., an] |
| 9 | + # Negative cubic squeeze coefficients |
| 10 | + [-0.2, -0.01, -0.001, -0.001], |
| 11 | + # Positive cubic squeeze coefficients |
| 12 | + [0.2, 0.01, 0.001, 0.001], |
| 13 | + # Positive and negative cubic squeeze coefficients |
| 14 | + [0.2, -0.01, 0.002, -0.001], |
| 15 | + # Quadratic squeeze coefficients |
| 16 | + [-0.2, 0.005, -0.007], |
| 17 | + # Linear squeeze coefficients |
| 18 | + [0.1, 0.3], |
| 19 | + # 4th order squeeze coefficients |
| 20 | + [0.2, -0.01, 0.001, -0.001, 0.0004], |
| 21 | + # Zeros and non-zeros, the full polynomial is applied |
| 22 | + [0, 0.03, 0, -0.001], |
| 23 | + # Testing zeros, expect no squeezing |
| 24 | + [0, 0, 0, 0, 0, 0], |
| 25 | +] |
| 26 | +morph_target_grids = [ |
| 27 | + # UCs from issue 181: https://github.com/diffpy/diffpy.morph/issues/181 |
| 28 | + # UC2: Same grid |
| 29 | + (np.linspace(0, 10, 101), np.linspace(0, 10, 101)), |
| 30 | + # UC4: Target extends beyond morph |
| 31 | + (np.linspace(0, 10, 101), np.linspace(-2, 20, 221)), |
| 32 | + # UC6: Target extends beyond morph; morph coarser |
| 33 | + (np.linspace(0, 10, 101), np.linspace(-2, 20, 421)), |
| 34 | + # UC8: Target extends beyond morph; target coarser |
| 35 | + (np.linspace(0, 10, 401), np.linspace(-2, 20, 200)), |
| 36 | + # UC10: morph starts earlier than target |
| 37 | + (np.linspace(-2, 10, 121), np.linspace(0, 20, 201)), |
| 38 | + # UC12: morph extends beyond target |
| 39 | + (np.linspace(-2, 20, 221), np.linspace(0, 10, 101)), |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("x_morph, x_target", morph_target_grids) |
| 44 | +@pytest.mark.parametrize("squeeze_coeffs", squeeze_coeffs_list) |
| 45 | +def test_morphsqueeze(x_morph, x_target, squeeze_coeffs): |
| 46 | + y_target = np.sin(x_target) |
| 47 | + squeeze_polynomial = Polynomial(squeeze_coeffs) |
| 48 | + x_squeezed = x_morph + squeeze_polynomial(x_morph) |
| 49 | + y_morph = np.sin(x_squeezed) |
| 50 | + x_morph_expected = x_morph |
| 51 | + y_morph_expected = np.sin(x_morph) |
| 52 | + morph = MorphSqueeze() |
| 53 | + morph.squeeze = squeeze_coeffs |
| 54 | + x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph( |
| 55 | + x_morph, y_morph, x_target, y_target |
| 56 | + ) |
| 57 | + assert np.allclose(y_morph_actual, y_morph_expected) |
| 58 | + assert np.allclose(x_morph_actual, x_morph_expected) |
| 59 | + assert np.allclose(x_target_actual, x_target) |
| 60 | + assert np.allclose(y_target_actual, y_target) |
0 commit comments