Skip to content

Commit 0cbcee4

Browse files
committed
test: add test for warning extrapolation in morphsqueeze
1 parent bdd29ae commit 0cbcee4

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph
1111

1212

13+
def custom_formatwarning(msg, *args, **kwargs):
14+
return f"{msg}\n"
15+
16+
17+
warnings.formatwarning = custom_formatwarning
18+
19+
1320
class MorphSqueeze(Morph):
1421
"""Squeeze the morph function.
1522
@@ -88,14 +95,19 @@ def morph(self, x_morph, y_morph, x_target, y_target):
8895
self.extrap_index_low = low_extrap[-1] if low_extrap.size else None
8996
self.extrap_index_high = high_extrap[0] if high_extrap.size else None
9097

91-
low_extrap_x = x_squeezed[0] - self.x_morph_in[0]
92-
high_extrap_x = self.x_morph_in[-1] - x_squeezed[-1]
93-
if low_extrap_x > 0 or high_extrap_x > 0:
94-
wmsg = "Extrapolating the morphed function: "
95-
if low_extrap_x > 0:
96-
wmsg += f"extrapolating length in the lowe r {low_extrap_x} "
97-
if high_extrap_x > 0:
98-
wmsg += f"extrapolating length in the high r {high_extrap_x} "
98+
begin_end_sqeeze = min(x_squeezed), max(x_squeezed)
99+
begin_end_in = min(self.x_morph_in), max(self.x_morph_in)
100+
if not (
101+
begin_end_sqeeze[0] <= begin_end_in[0]
102+
and begin_end_in[-1] >= begin_end_in[-1]
103+
):
104+
wmsg = (
105+
"\nExtrapolating the morphed function via CubicSpline:\n"
106+
f"Obtaining grid points between {begin_end_in[0]} and "
107+
f"{begin_end_in[1]}.\n"
108+
f"Points below {begin_end_sqeeze[0]} and "
109+
f"above {begin_end_sqeeze[1]} will be extrapolated."
110+
)
99111
warnings.warn(
100112
wmsg,
101113
UserWarning,

tests/test_morphsqueeze.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,30 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
8585
assert np.allclose(x_morph_actual, x_morph_expected)
8686
assert np.allclose(x_target_actual, x_target)
8787
assert np.allclose(y_target_actual, y_target)
88+
89+
90+
def test_morphsqueeze_extrapolate():
91+
x_morph = np.linspace(0, 10, 101)
92+
y_morph = np.sin(x_morph)
93+
x_target = x_morph
94+
y_target = y_morph
95+
squeeze_coeff = {"a0": 0.01, "a1": -0.0005, "a2": -0.0005, "a3": -1e-6}
96+
morph = MorphSqueeze()
97+
morph.squeeze = squeeze_coeff
98+
coeffs = [squeeze_coeff[f"a{i}"] for i in range(len(squeeze_coeff))]
99+
squeeze_polynomial = Polynomial(coeffs)
100+
x_squeezed = x_morph + squeeze_polynomial(x_morph)
101+
with pytest.warns() as w:
102+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = (
103+
morph(x_morph, y_morph, x_target, y_target)
104+
)
105+
assert len(w) == 1
106+
assert w[0].category is UserWarning
107+
actual_wmsg = str(w[0].message)
108+
expected_wmsg = (
109+
"\nExtrapolating the morphed function via CubicSpline:\n"
110+
f"Obtaining grid points between {x_morph[0]} and {x_morph[-1]}.\n"
111+
f"Points below {x_squeezed[0]} and "
112+
f"above {x_squeezed[-1]} will be extrapolated."
113+
)
114+
assert actual_wmsg == expected_wmsg

0 commit comments

Comments
 (0)