Skip to content

Commit 241b56d

Browse files
committed
test: add convergence test for non-strctily-increasing squeeze morph
1 parent aa0f1d8 commit 241b56d

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ def _set_squeeze_info(self, x, x_sorted):
100100
"and stretch parameter for a1."
101101
)
102102
else:
103-
overlapping_regions = self.get_overlapping_regions(x)
104-
self.squeeze_info["monotonic"] = False
105-
self.squeeze_info["overlapping_regions"] = overlapping_regions
103+
if list(x) != list(x_sorted[::-1]):
104+
overlapping_regions = self.get_overlapping_regions(x)
105+
self.squeeze_info["monotonic"] = False
106+
self.squeeze_info["overlapping_regions"] = (
107+
overlapping_regions
108+
)
106109

107110
def _sort_squeeze(self, x, y):
108111
"""Sort x,y according to the value of x."""

tests/test_morphsqueeze.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,64 @@ def test_morphsqueeze_extrapolate(
173173
single_morph(parser, opts, pargs, stdout_flag=False)
174174

175175

176+
@pytest.mark.parametrize(
177+
"squeeze_coeffs, x_morph",
178+
[
179+
({"a0": 0.01, "a1": -0.99, "a2": 0.01}, np.linspace(-1, 1, 101)),
180+
],
181+
)
182+
def test_sort_squeeze(user_filesystem, squeeze_coeffs, x_morph):
183+
x_target = x_morph
184+
y_target = np.sin(x_target)
185+
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
186+
squeeze_polynomial = Polynomial(coeffs)
187+
x_squeezed = x_morph + squeeze_polynomial(x_morph)
188+
# non-strictly-monotonic
189+
assert not np.all(np.diff(np.sign(np.diff(x_squeezed))) == 0)
190+
# outcome converges when --check-increase is not used
191+
y_morph = np.sin(x_squeezed)
192+
morph = MorphSqueeze()
193+
morph.squeeze = squeeze_coeffs
194+
with pytest.warns() as w:
195+
moreph_results = morphpy.morph_arrays(
196+
np.array([x_morph, y_morph]).T,
197+
np.array([x_target, y_target]).T,
198+
squeeze=[0.01, -0.99, 0.01],
199+
)
200+
assert w[0].category is UserWarning
201+
actual_wmsg = " ".join([str(w[i].message) for i in range(len(w))])
202+
expected_wmsg = (
203+
"Warning: The squeeze morph has interpolated your morphed "
204+
"function from a non-monotonically increasing grid. "
205+
)
206+
assert expected_wmsg in actual_wmsg
207+
expected_coeffs = coeffs
208+
actual_coeffs = [
209+
moreph_results[0]["squeeze"][f"a{i}"]
210+
for i in range(len(moreph_results[0]["squeeze"]))
211+
]
212+
# program exits when --check-increase is used
213+
assert np.allclose(actual_coeffs, expected_coeffs, rtol=1e-2)
214+
with pytest.raises(SystemExit) as excinfo:
215+
morphpy.morph_arrays(
216+
np.array([x_morph, y_morph]).T,
217+
np.array([x_target, y_target]).T,
218+
squeeze=[0.01, -1, 0.01],
219+
check_increase=True,
220+
)
221+
actual_emsg = str(excinfo.value)
222+
expected_emsg = "2"
223+
assert expected_emsg == actual_emsg
224+
225+
176226
@pytest.mark.parametrize(
177227
"squeeze_coeffs, x_morph",
178228
[
179229
({"a0": -1, "a1": -1, "a2": 2}, np.linspace(-1, 1, 101)),
230+
(
231+
{"a0": -1, "a1": -1, "a2": 0, "a3": 0, "a4": 2},
232+
np.linspace(-1, 1, 101),
233+
),
180234
],
181235
)
182236
def test_sort_squeeze_bad(user_filesystem, squeeze_coeffs, x_morph):
@@ -267,7 +321,7 @@ def test_sort_squeeze_bad(user_filesystem, squeeze_coeffs, x_morph):
267321
# x[-1] > x[0], monotonically decreasing regions are overlapping
268322
([0, 10, 7, 12], [[7, 10]]),
269323
# x[-1] < x[0], monotonically increasing regions are overlapping
270-
([0, 5, 2, 4, -10], [[0, 5], [2, 4]])
324+
([0, 5, 2, 4, -10], [[0, 5], [2, 4]]),
271325
],
272326
)
273327
def test_get_overlapping_regions(turning_points, expected_overlapping_regions):

0 commit comments

Comments
 (0)