|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +def test_morphsqueeze(): |
| 6 | + """ |
| 7 | + Test that we can unsqueeze squeezed data. |
| 8 | + The test inputs are an expected uniform target (e.g. synchrotron data) |
| 9 | + and a squeezed version of the target (e.g. XFEL data). The squeezed data |
| 10 | + is created by applying a nonlinear distortion to the uniform target. |
| 11 | + Both input data are in the same uniform x-axis grid. |
| 12 | + Then we unsqueeze the squeezed data by doing the inverse transformation |
| 13 | + using interpolatiion. |
| 14 | + Finally we check that the unsqueezed data matches the expected uniform |
| 15 | + target. |
| 16 | + """ |
| 17 | + |
| 18 | + # Uniform x-axis grid. This is the same x-axis for all data. |
| 19 | + x = np.linspace(0, 10, 1000) |
| 20 | + # Expected uniform target |
| 21 | + y_expected = np.sin(x) |
| 22 | + |
| 23 | + # Apply squeeze parameters to uniform data to get the squeezed data |
| 24 | + squeeze_1 = 0.003 |
| 25 | + squeeze_2 = 0.004 |
| 26 | + x_squeezed = x + squeeze_1 * x**2 + squeeze_2 * x**3 |
| 27 | + y_squeezed = np.sin(x_squeezed) |
| 28 | + |
| 29 | + # Unsqueeze the data by interpolating back to uniform grid |
| 30 | + y_unsqueezed = np.interp(x, x_squeezed, y_squeezed) |
| 31 | + y_actual = y_unsqueezed |
| 32 | + |
| 33 | + # Check that the unsqueezed (actual) data matches the expected data |
| 34 | + # Including tolerance error because I was having issues |
| 35 | + # with y_actual == y_expected. I think is because interpolation? |
| 36 | + assert np.allclose(y_actual, y_expected, atol=1e-3) |
| 37 | + |
| 38 | + # Plot to verify what we are doing |
| 39 | + plt.figure(figsize=(7, 4)) |
| 40 | + plt.plot(x, y_expected, color="black", label="Expected uniform data") |
| 41 | + plt.plot(x, y_squeezed, "--", color="purple", label="Squeezed data") |
| 42 | + plt.plot(x, y_unsqueezed, "--", color="gold", label="Unsqueezed data") |
| 43 | + plt.xlabel("x") |
| 44 | + plt.ylabel("y") |
| 45 | + plt.legend() |
| 46 | + plt.show() |
0 commit comments