|
| 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.01, -0.0005, -0.0005, -1e-6], |
| 11 | + # Positive cubic squeeze coefficients |
| 12 | + [0.2, 0.01, 0.001, 0.0001], |
| 13 | + # Positive and negative cubic squeeze coefficients |
| 14 | + [0.2, -0.01, 0.002, -0.0001], |
| 15 | + # Quadratic squeeze coefficients |
| 16 | + [-0.2, 0.005, -0.0004], |
| 17 | + # Linear squeeze coefficients |
| 18 | + [0.1, 0.3], |
| 19 | + # 4th order squeeze coefficients |
| 20 | + [0.2, -0.01, 0.001, -0.001, 0.0001], |
| 21 | + # Zeros and non-zeros, the full polynomial is applied |
| 22 | + [0, 0.03, 0, -0.0001], |
| 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 range and same grid density |
| 29 | + (np.linspace(0, 10, 101), np.linspace(0, 10, 101)), |
| 30 | + # UC4: Target range wider than morph, same grid density |
| 31 | + (np.linspace(0, 10, 101), np.linspace(-2, 20, 221)), |
| 32 | + # UC6: Target range wider than morph, target grid density finer than morph |
| 33 | + (np.linspace(0, 10, 101), np.linspace(-2, 20, 421)), |
| 34 | + # UC8: Target range wider than morph, morph grid density finer than target |
| 35 | + (np.linspace(0, 10, 401), np.linspace(-2, 20, 200)), |
| 36 | + # UC10: Morph range starts and ends earlier than target, same grid density |
| 37 | + (np.linspace(-2, 10, 121), np.linspace(0, 20, 201)), |
| 38 | + # UC12: Morph range wider than target, same grid density |
| 39 | + (np.linspace(-2, 20, 201), 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 | + low_extrap = np.where(x_morph < x_squeezed[0])[0] |
| 51 | + high_extrap = np.where(x_morph > x_squeezed[-1])[0] |
| 52 | + extrap_index_low_expected = low_extrap[-1] if low_extrap.size else None |
| 53 | + extrap_index_high_expected = high_extrap[0] if high_extrap.size else None |
| 54 | + x_morph_expected = x_morph |
| 55 | + y_morph_expected = np.sin(x_morph) |
| 56 | + morph = MorphSqueeze() |
| 57 | + morph.squeeze = squeeze_coeffs |
| 58 | + x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph( |
| 59 | + x_morph, y_morph, x_target, y_target |
| 60 | + ) |
| 61 | + extrap_index_low = morph.extrap_index_low |
| 62 | + extrap_index_high = morph.extrap_index_high |
| 63 | + if extrap_index_low is None: |
| 64 | + extrap_index_low = 0 |
| 65 | + elif extrap_index_high is None: |
| 66 | + extrap_index_high = -1 |
| 67 | + assert np.allclose( |
| 68 | + y_morph_actual[extrap_index_low + 1 : extrap_index_high], |
| 69 | + y_morph_expected[extrap_index_low + 1 : extrap_index_high], |
| 70 | + atol=1e-6, |
| 71 | + ) |
| 72 | + assert np.allclose( |
| 73 | + y_morph_actual[:extrap_index_low], |
| 74 | + y_morph_expected[:extrap_index_low], |
| 75 | + atol=1e-3, |
| 76 | + ) |
| 77 | + assert np.allclose( |
| 78 | + y_morph_actual[extrap_index_high:], |
| 79 | + y_morph_expected[extrap_index_high:], |
| 80 | + atol=1e-3, |
| 81 | + ) |
| 82 | + assert morph.extrap_index_low == extrap_index_low_expected |
| 83 | + assert morph.extrap_index_high == extrap_index_high_expected |
| 84 | + assert np.allclose(x_morph_actual, x_morph_expected) |
| 85 | + assert np.allclose(x_target_actual, x_target) |
| 86 | + assert np.allclose(y_target_actual, y_target) |
0 commit comments