Skip to content

Commit 2c9208a

Browse files
committed
refactor: rename the function to set_extrapolation_info
1 parent e35af9c commit 2c9208a

File tree

5 files changed

+72
-51
lines changed

5 files changed

+72
-51
lines changed

src/diffpy/morph/morph_io.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -416,26 +416,29 @@ def handle_warnings(squeeze_morph):
416416
cutoff_low = extrapolation_info["cutoff_low"]
417417
cutoff_high = extrapolation_info["cutoff_high"]
418418

419-
if is_extrap_low or is_extrap_high:
420-
if not is_extrap_high:
421-
wmsg = (
422-
"Warning: points with grid value below "
423-
f"{cutoff_low} "
424-
f"will be extrapolated."
425-
)
426-
elif not is_extrap_low:
427-
wmsg = (
428-
"Warning: points with grid value above "
429-
f"{cutoff_high} "
430-
f"will be extrapolated."
431-
)
432-
else:
433-
wmsg = (
434-
"Warning: points with grid value below "
435-
f"{cutoff_low} and above "
436-
f"{cutoff_high} "
437-
f"will be extrapolated."
438-
)
419+
if is_extrap_low and is_extrap_high:
420+
wmsg = (
421+
"Warning: points with grid value below "
422+
f"{cutoff_low} and above "
423+
f"{cutoff_high} "
424+
f"are extrapolated."
425+
)
426+
elif is_extrap_low:
427+
wmsg = (
428+
"Warning: points with grid value below "
429+
f"{cutoff_low} "
430+
f"are extrapolated."
431+
)
432+
elif is_extrap_high:
433+
wmsg = (
434+
"Warning: points with grid value above "
435+
f"{cutoff_high} "
436+
f"are extrapolated."
437+
)
438+
else:
439+
wmsg = None
440+
441+
if wmsg:
439442
warnings.warn(
440443
wmsg,
441444
UserWarning,

src/diffpy/morph/morphs/morph.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#
1414
##############################################################################
1515
"""Morph -- base class for defining a morph."""
16-
16+
import numpy
1717

1818
LABEL_RA = "r (A)" # r-grid
1919
LABEL_GR = "G (1/A^2)" # PDF G(r)
@@ -245,22 +245,35 @@ def plotOutputs(self, xylabels=True, **plotargs):
245245
ylabel(self.youtlabel)
246246
return rv
247247

248-
def checkExtrapolation(self, x_true, x_extrapolate):
249-
import numpy
248+
def set_extrapolation_info(self, x_true, x_extrapolate):
249+
"""Set extrapolation information of the concerned morphing
250+
process.
251+
252+
Parameters
253+
----------
254+
x_true : array
255+
original x values
256+
x_extrapolate : array
257+
x values after a morphing process
258+
"""
250259

251260
cutoff_low = min(x_true)
261+
extrap_low_x = numpy.where(x_extrapolate < cutoff_low)[0]
262+
is_extrap_low = False if len(extrap_low_x) == 0 else True
252263
cutoff_high = max(x_true)
253-
low_extrap = numpy.where(x_extrapolate < cutoff_low)[0]
254-
high_extrap = numpy.where(x_extrapolate > cutoff_high)[0]
255-
is_extrap_low = False if len(low_extrap) == 0 else True
256-
is_extrap_high = False if len(high_extrap) == 0 else True
264+
extrap_high_x = numpy.where(x_extrapolate > cutoff_high)[0]
265+
is_extrap_high = False if len(extrap_high_x) == 0 else True
266+
extrap_index_low = extrap_low_x[-1] if is_extrap_low else 0
267+
extrap_index_high = extrap_high_x[0] if is_extrap_high else -1
257268
extrapolation_info = {
258269
"is_extrap_low": is_extrap_low,
259270
"cutoff_low": cutoff_low,
271+
"extrap_index_low": extrap_index_low,
260272
"is_extrap_high": is_extrap_high,
261273
"cutoff_high": cutoff_high,
274+
"extrap_index_high": extrap_index_high,
262275
}
263-
return extrapolation_info
276+
self.extrapolation_info = extrapolation_info
264277

265278
def __getattr__(self, name):
266279
"""Obtain the value from self.config, when normal lookup fails.

src/diffpy/morph/morphs/morphshift.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def morph(self, x_morph, y_morph, x_target, y_target):
5757
r = self.x_morph_in - hshift
5858
self.y_morph_out = numpy.interp(r, self.x_morph_in, self.y_morph_in)
5959
self.y_morph_out += vshift
60-
self.extrapolation_info = self.checkExtrapolation(self.x_morph_in, r)
60+
self.set_extrapolation_info(self.x_morph_in, r)
6161
return self.xyallout
6262

6363

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ def morph(self, x_morph, y_morph, x_target, y_target):
8585
self.y_morph_out = CubicSpline(x_squeezed, self.y_morph_in)(
8686
self.x_morph_in
8787
)
88-
self.extrapolation_info = self.checkExtrapolation(
89-
x_squeezed, self.x_morph_in
90-
)
88+
self.set_extrapolation_info(x_squeezed, self.x_morph_in)
9189

9290
return self.xyallout

tests/test_morphsqueeze.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,54 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
4848
y_target = np.sin(x_target)
4949
y_morph = np.sin(x_morph)
5050
# expected output
51-
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
52-
squeeze_polynomial = Polynomial(coeffs)
53-
x_squeezed = x_morph + squeeze_polynomial(x_morph)
5451
y_morph_expected = y_morph
5552
x_morph_expected = x_morph
5653
x_target_expected = x_target
5754
y_target_expected = y_target
5855
# actual output
59-
morph = MorphSqueeze()
56+
coeffs = [squeeze_coeffs[f"a{i}"] for i in range(len(squeeze_coeffs))]
57+
squeeze_polynomial = Polynomial(coeffs)
58+
x_squeezed = x_morph + squeeze_polynomial(x_morph)
6059
y_morph = np.sin(x_squeezed)
60+
morph = MorphSqueeze()
6161
morph.squeeze = squeeze_coeffs
6262
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph(
6363
x_morph, y_morph, x_target, y_target
6464
)
6565

6666
extrap_low = np.where(x_morph < min(x_squeezed))[0]
6767
extrap_high = np.where(x_morph > max(x_squeezed))[0]
68-
extrap_index_low = extrap_low[-1] if extrap_low.size else None
69-
extrap_index_high = extrap_high[0] if extrap_high.size else None
70-
if extrap_index_low is None:
71-
extrap_index_low = 0
72-
elif extrap_index_high is None:
73-
extrap_index_high = -1
68+
extrap_index_low_expected = extrap_low[-1] if extrap_low.size else 0
69+
extrap_index_high_expected = extrap_high[0] if extrap_high.size else -1
70+
71+
extrapolation_info = morph.extrapolation_info
72+
extrap_index_low_actual = extrapolation_info["extrap_index_low"]
73+
extrap_index_high_actual = extrapolation_info["extrap_index_high"]
74+
7475
assert np.allclose(
75-
y_morph_actual[extrap_index_low + 1 : extrap_index_high],
76-
y_morph_expected[extrap_index_low + 1 : extrap_index_high],
76+
y_morph_actual[
77+
extrap_index_low_expected + 1 : extrap_index_high_expected
78+
],
79+
y_morph_expected[
80+
extrap_index_low_expected + 1 : extrap_index_high_expected
81+
],
7782
atol=1e-6,
7883
)
7984
assert np.allclose(
80-
y_morph_actual[:extrap_index_low],
81-
y_morph_expected[:extrap_index_low],
85+
y_morph_actual[:extrap_index_low_expected],
86+
y_morph_expected[:extrap_index_low_expected],
8287
atol=1e-3,
8388
)
8489
assert np.allclose(
85-
y_morph_actual[extrap_index_high:],
86-
y_morph_expected[extrap_index_high:],
90+
y_morph_actual[extrap_index_high_expected:],
91+
y_morph_expected[extrap_index_high_expected:],
8792
atol=1e-3,
8893
)
8994
assert np.allclose(x_morph_actual, x_morph_expected)
9095
assert np.allclose(x_target_actual, x_target_expected)
9196
assert np.allclose(y_target_actual, y_target_expected)
97+
assert extrap_index_low_actual == extrap_index_low_expected
98+
assert extrap_index_high_actual == extrap_index_high_expected
9299

93100

94101
@pytest.mark.parametrize(
@@ -99,23 +106,23 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
99106
{"a0": 0.01},
100107
lambda x: (
101108
"Warning: points with grid value below "
102-
f"{x[0]} will be extrapolated."
109+
f"{x[0]} are extrapolated."
103110
),
104111
),
105112
# extrapolate above
106113
(
107114
{"a0": -0.01},
108115
lambda x: (
109116
"Warning: points with grid value above "
110-
f"{x[1]} will be extrapolated."
117+
f"{x[1]} are extrapolated."
111118
),
112119
),
113120
# extrapolate below and above
114121
(
115122
{"a0": 0.01, "a1": -0.002},
116123
lambda x: (
117124
"Warning: points with grid value below "
118-
f"{x[0]} and above {x[1]} will be "
125+
f"{x[0]} and above {x[1]} are "
119126
"extrapolated."
120127
),
121128
),

0 commit comments

Comments
 (0)