|
27 | 27 | [0, 0, 0, 0, 0, 0],
|
28 | 28 | ],
|
29 | 29 | )
|
30 |
| -def test_morphsqueeze(squeeze_coeffs): |
31 |
| - x_target_expected = np.linspace(0, 10, 101) |
32 |
| - y_target_expected = np.sin(x_target_expected) |
33 |
| - # Different grid for morph data to test inputs with different grids |
34 |
| - # Morph grid must be finer than the target to avoid interpolation issues |
35 |
| - x_morph = np.linspace(-3, 13, 301) |
| 30 | +def test_morphsqueeze_target_extends_beyond_morph(squeeze_coeffs): |
| 31 | + # Target data extends beyond morph and different grids |
| 32 | + x_target = np.linspace(-3, 25, 401) |
| 33 | + y_target = np.sin(x_target) |
| 34 | + x_morph = np.linspace(0, 10, 301) |
36 | 35 | squeeze_polynomial = Polynomial(squeeze_coeffs)
|
37 | 36 | x_squeezed = x_morph + squeeze_polynomial(x_morph)
|
38 | 37 | y_morph = np.sin(x_squeezed)
|
| 38 | + # Trim target data to the region overlapping with the squeezed morph |
| 39 | + x_min = max(float(x_target[0]), float(x_squeezed[0])) |
| 40 | + x_max = min(float(x_target[-1]), float(x_squeezed[-1])) |
| 41 | + min_index = np.where(x_target >= x_min)[0][0] |
| 42 | + max_index = np.where(x_target <= x_max)[0][-1] |
| 43 | + x_morph_expected = x_target[min_index : max_index + 1] |
| 44 | + y_morph_expected = y_target[min_index : max_index + 1] |
39 | 45 | morph = MorphSqueeze()
|
40 | 46 | morph.squeeze = squeeze_coeffs
|
41 | 47 | x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph(
|
42 |
| - x_morph, y_morph, x_target_expected, y_target_expected |
| 48 | + x_morph, y_morph, x_target, y_target |
43 | 49 | )
|
44 |
| - assert np.allclose(y_morph_actual, y_target_expected) |
45 |
| - assert np.allclose(x_morph_actual, x_target_expected) |
46 |
| - assert np.allclose(x_target_actual, x_target_expected) |
47 |
| - assert np.allclose(y_target_actual, y_target_expected) |
| 50 | + assert np.allclose(y_morph_actual, y_morph_expected) |
| 51 | + assert np.allclose(x_morph_actual, x_morph_expected) |
| 52 | + assert np.allclose(x_target_actual, x_morph_expected) |
| 53 | + assert np.allclose(y_target_actual, y_morph_expected) |
| 54 | + |
| 55 | + import matplotlib.pyplot as plt |
| 56 | + |
| 57 | + plt.figure() |
| 58 | + plt.plot(x_target, y_target, color="gray", label="target") |
| 59 | + plt.plot(x_morph, y_morph, color="black", label="morph") |
| 60 | + plt.scatter(x_morph_actual, y_morph_actual, color="gold", label="actual") |
| 61 | + plt.plot( |
| 62 | + x_morph_expected, y_morph_expected, color="purple", label="expected" |
| 63 | + ) |
| 64 | + plt.legend() |
| 65 | + plt.show() |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + "squeeze_coeffs", |
| 70 | + [ |
| 71 | + # The order of coefficients is [a0, a1, a2, ..., an] |
| 72 | + # Negative cubic squeeze coefficients |
| 73 | + [-0.2, -0.01, -0.001, -0.001], |
| 74 | + # Positive cubic squeeze coefficients |
| 75 | + [0.2, 0.01, 0.001, 0.001], |
| 76 | + # Positive and negative cubic squeeze coefficients |
| 77 | + [0.2, -0.01, 0.002, -0.001], |
| 78 | + # Quadratic squeeze coefficients |
| 79 | + [-0.2, 0.005, -0.007], |
| 80 | + # Linear squeeze coefficients |
| 81 | + [0.1, 0.3], |
| 82 | + # 4th order squeeze coefficients |
| 83 | + [0.2, -0.01, 0.001, -0.001, 0.0004], |
| 84 | + # Zeros and non-zeros, the full polynomial is applied |
| 85 | + [0, 0.03, 0, -0.001], |
| 86 | + # Testing zeros, expect no squeezing |
| 87 | + [0, 0, 0, 0, 0, 0], |
| 88 | + ], |
| 89 | +) |
| 90 | +def test_morphsqueeze_morph_extends_beyond_target(squeeze_coeffs): |
| 91 | + # Different grid for morph and target data to test different grids |
| 92 | + x_target = np.linspace(0, 10, 101) |
| 93 | + y_target = np.sin(x_target) |
| 94 | + x_morph = np.linspace(-3, 15, 301) |
| 95 | + squeeze_polynomial = Polynomial(squeeze_coeffs) |
| 96 | + x_squeezed = x_morph + squeeze_polynomial(x_morph) |
| 97 | + y_morph = np.sin(x_squeezed) |
| 98 | + # Trim target data to the region overlapping with the squeezed morph |
| 99 | + x_min = max(float(x_target[0]), float(x_squeezed[0])) |
| 100 | + x_max = min(float(x_target[-1]), float(x_squeezed[-1])) |
| 101 | + min_index = np.where(x_target >= x_min)[0][0] |
| 102 | + max_index = np.where(x_target <= x_max)[0][-1] |
| 103 | + x_morph_expected = x_target[min_index : max_index + 1] |
| 104 | + y_morph_expected = y_target[min_index : max_index + 1] |
| 105 | + morph = MorphSqueeze() |
| 106 | + morph.squeeze = squeeze_coeffs |
| 107 | + x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph( |
| 108 | + x_morph, y_morph, x_target, y_target |
| 109 | + ) |
| 110 | + assert np.allclose(y_morph_actual, y_morph_expected) |
| 111 | + assert np.allclose(x_morph_actual, x_morph_expected) |
| 112 | + assert np.allclose(x_target_actual, x_morph_expected) |
| 113 | + assert np.allclose(y_target_actual, y_morph_expected) |
0 commit comments