Skip to content

Commit 2320b82

Browse files
committed
test: adding different UCs tests while keeping function empty
1 parent c59b09e commit 2320b82

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

news/morphsqueeze.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Polynomial squeeze of x-axis of morphed data
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>

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
class MorphSqueeze(Morph):
55
"""Squeeze the morph function.
66
7-
This applies a polynomial to squeeze the morph non-linearly. The resulting
8-
squeezed morph is interpolated to the (trimmed) target grid.
9-
Only the overlapping region between the squeezed morph and the target
10-
grid is used. The target is trimmed (or not) accordingly, and the final
11-
outputs (morph and target) are returned on the same grid, defined by this
12-
trimmed target range.
7+
This applies a polynomial to squeeze the morph non-linearly. The morphed
8+
data is returned on the same grid as the unmorphed data.
139
1410
Configuration Variables
1511
-----------------------
@@ -28,9 +24,5 @@ class MorphSqueeze(Morph):
2824

2925
def morph(self, x_morph, y_morph, x_target, y_target):
3026
Morph.morph(self, x_morph, y_morph, x_target, y_target)
31-
if self.squeeze is None:
32-
self.x_morph_out = self.x_morph_in
33-
self.y_morph_out = self.y_morph_in
34-
return self.xyallout
3527

3628
return self.xyallout

tests/test_morphsqueeze.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.2, -0.01, -0.001, -0.001],
11+
# Positive cubic squeeze coefficients
12+
[0.2, 0.01, 0.001, 0.001],
13+
# Positive and negative cubic squeeze coefficients
14+
[0.2, -0.01, 0.002, -0.001],
15+
# Quadratic squeeze coefficients
16+
[-0.2, 0.005, -0.007],
17+
# Linear squeeze coefficients
18+
[0.1, 0.3],
19+
# 4th order squeeze coefficients
20+
[0.2, -0.01, 0.001, -0.001, 0.0004],
21+
# Zeros and non-zeros, the full polynomial is applied
22+
[0, 0.03, 0, -0.001],
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 grid
29+
(np.linspace(0, 10, 101), np.linspace(0, 10, 101)),
30+
# UC4: Target extends beyond morph
31+
(np.linspace(0, 10, 101), np.linspace(-2, 20, 221)),
32+
# UC6: Target extends beyond morph; morph coarser
33+
(np.linspace(0, 10, 101), np.linspace(-2, 20, 421)),
34+
# UC8: Target extends beyond morph; target coarser
35+
(np.linspace(0, 10, 401), np.linspace(-2, 20, 200)),
36+
# UC10: morph starts earlier than target
37+
(np.linspace(-2, 10, 121), np.linspace(0, 20, 201)),
38+
# UC12: morph extends beyond target
39+
(np.linspace(-2, 20, 221), 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+
x_morph_expected = x_morph
51+
y_morph_expected = np.sin(x_morph)
52+
morph = MorphSqueeze()
53+
morph.squeeze = squeeze_coeffs
54+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph(
55+
x_morph, y_morph, x_target, y_target
56+
)
57+
assert np.allclose(y_morph_actual, y_morph_expected)
58+
assert np.allclose(x_morph_actual, x_morph_expected)
59+
assert np.allclose(x_target_actual, x_target)
60+
assert np.allclose(y_target_actual, y_target)

0 commit comments

Comments
 (0)