-
Notifications
You must be signed in to change notification settings - Fork 19
test: adding a test to unsqueeze squeezed data #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9a53b24
ac388e9
ce863d3
ca69295
a1b6425
14d92cb
a48ce43
d42d234
f47e167
4933b4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * Polynomial squeeze of x-axis of morphed data | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import numpy as np | ||
| import pytest | ||
| from numpy.polynomial import Polynomial | ||
| from scipy.interpolate import interp1d | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "squeeze_coeffs", | ||
| [ | ||
| # The order of coefficients is [a0, a1, a2, ..., an] | ||
| # Negative cubic squeeze coefficients | ||
| [-0.2, -0.01, -0.001, -0.001], | ||
| # Positive cubic squeeze coefficients | ||
| [0.2, 0.01, 0.001, 0.001], | ||
| # Positive and negative cubic squeeze coefficients | ||
| [0.2, -0.01, 0.001, -0.001], | ||
| # Quadratic squeeze coefficients | ||
| [-0.2, 0.005, -0.003], | ||
| # Linear squeeze coefficients | ||
| [0.1, 0.3], | ||
| # 4th order squeeze coefficients | ||
| [0.2, -0.01, 0.001, -0.001, 0.0001], | ||
| ], | ||
| ) | ||
| def test_morphsqueeze(squeeze_coeffs): | ||
| # Uniform x-axis grid. This is the same x-axis for all data. | ||
|
||
| x = np.linspace(0, 10, 1000) | ||
| # Expected uniform target | ||
| y_expected = np.sin(x) | ||
|
|
||
|
||
| # Create polynomial based on a list of values for polynomial coefficients | ||
| squeeze_polynomial = Polynomial(squeeze_coeffs) | ||
| # Apply squeeze parameters to uniform data to get the squeezed data | ||
| x_squeezed = x + squeeze_polynomial(x) | ||
| y_squeezed = np.sin(x_squeezed) | ||
|
|
||
|
||
| # Unsqueeze the data by interpolating back to uniform grid | ||
| y_unsqueezed = interp1d( | ||
| x_squeezed, | ||
| y_squeezed, | ||
| kind="cubic", | ||
| bounds_error=False, | ||
| fill_value="extrapolate", | ||
| )(x) | ||
| y_actual = y_unsqueezed | ||
|
|
||
| # Check that the unsqueezed (actual) data matches the expected data | ||
| # Including tolerance error because of extrapolation error | ||
|
||
| assert np.allclose(y_actual, y_expected, atol=1) | ||
|
|
||
| # This plotting code was used for the comments in the github | ||
| # PR https://github.com/diffpy/diffpy.morph/pull/180 | ||
| # plt.figure(figsize=(7, 4)) | ||
| # plt.plot(x, y_expected, color="black", label="Expected uniform data") | ||
| # plt.plot(x, y_squeezed, "--", color="purple", label="Squeezed data") | ||
| # plt.plot(x, y_unsqueezed, "--", color="gold", label="Unsqueezed data") | ||
| # plt.xlabel("x") | ||
| # plt.ylabel("y") | ||
| # plt.legend() | ||
| # plt.show() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are now going to test 3 or 4 different variants of the same thing (i.e., different representative numbers) so it is time to use @pytest.paramatrize decorator. Please see
diffpy.utilstests for examples.