Skip to content

Commit 9a53b24

Browse files
committed
test: adding a test to unsqueeze squeezed data
1 parent d1c2695 commit 9a53b24

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

news/test_squeeze.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Squeeze test: given a squeezed signal, unsqueezing recovers the original
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

tests/test_morphsqueeze.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)