Skip to content

Commit 026984a

Browse files
committed
chore: update warning message for extrapolation in morphsqueeze.py
1 parent 0cbcee4 commit 026984a

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,25 @@ def morph(self, x_morph, y_morph, x_target, y_target):
9494
high_extrap = np.where(self.x_morph_in > x_squeezed[-1])[0]
9595
self.extrap_index_low = low_extrap[-1] if low_extrap.size else None
9696
self.extrap_index_high = high_extrap[0] if high_extrap.size else None
97-
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-
)
97+
below_extrap = min(x_morph) < min(x_squeezed)
98+
above_extrap = max(x_morph) > max(x_squeezed)
99+
if below_extrap or above_extrap:
100+
if not above_extrap:
101+
wmsg = (
102+
"Warning: points with grid value below "
103+
f"{min(x_squeezed)} will be extrapolated."
104+
)
105+
elif not below_extrap:
106+
wmsg = (
107+
"Warning: points with grid value above "
108+
f"{max(x_squeezed)} will be extrapolated."
109+
)
110+
else:
111+
wmsg = (
112+
"Warning: points with grid value below "
113+
f"{min(x_squeezed)} and above {max(x_squeezed)} will be "
114+
"extrapolated."
115+
)
111116
warnings.warn(
112117
wmsg,
113118
UserWarning,

tests/test_morphsqueeze.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,44 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
8787
assert np.allclose(y_target_actual, y_target)
8888

8989

90-
def test_morphsqueeze_extrapolate():
90+
@pytest.mark.parametrize(
91+
"squeeze_coeffs, wmsg_gen",
92+
[
93+
# extrapolate below
94+
(
95+
{"a0": 0.01},
96+
lambda x: (
97+
"Warning: points with grid value below "
98+
f"{x[0]} will be extrapolated."
99+
),
100+
),
101+
# extrapolate above
102+
(
103+
{"a0": -0.01},
104+
lambda x: (
105+
"Warning: points with grid value above "
106+
f"{x[1]} will be extrapolated."
107+
),
108+
),
109+
# extrapolate below and above
110+
(
111+
{"a0": 0.01, "a1": -0.002},
112+
lambda x: (
113+
"Warning: points with grid value below "
114+
f"{x[0]} and above {x[1]} will be "
115+
"extrapolated."
116+
),
117+
),
118+
],
119+
)
120+
def test_morphsqueeze_extrapolate(squeeze_coeffs, wmsg_gen):
91121
x_morph = np.linspace(0, 10, 101)
92122
y_morph = np.sin(x_morph)
93123
x_target = x_morph
94124
y_target = y_morph
95-
squeeze_coeff = {"a0": 0.01, "a1": -0.0005, "a2": -0.0005, "a3": -1e-6}
96125
morph = MorphSqueeze()
97-
morph.squeeze = squeeze_coeff
98-
coeffs = [squeeze_coeff[f"a{i}"] for i in range(len(squeeze_coeff))]
126+
morph.squeeze = squeeze_coeffs
127+
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
99128
squeeze_polynomial = Polynomial(coeffs)
100129
x_squeezed = x_morph + squeeze_polynomial(x_morph)
101130
with pytest.warns() as w:
@@ -105,10 +134,5 @@ def test_morphsqueeze_extrapolate():
105134
assert len(w) == 1
106135
assert w[0].category is UserWarning
107136
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-
)
137+
expected_wmsg = wmsg_gen([min(x_squeezed), max(x_squeezed)])
114138
assert actual_wmsg == expected_wmsg

0 commit comments

Comments
 (0)