Skip to content

Commit 4c6d993

Browse files
committed
func: not returning extrapolation indices
1 parent 234b387 commit 4c6d993

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class MorphSqueeze(Morph):
1616
xoutlabel = LABEL_RA
1717
youtlabel = LABEL_GR
1818
parnames = ["squeeze"]
19+
# extrap_index_low: last index before interpolation region
20+
# extrap_index_high: first index after interpolation region
21+
extrap_index_low = None
22+
extrap_index_high = None
1923

2024
def morph(self, x_morph, y_morph, x_target, y_target):
2125
"""Squeeze the morph function.
@@ -32,13 +36,10 @@ def morph(self, x_morph, y_morph, x_target, y_target):
3236
3337
Returns
3438
-------
35-
A tuple (x_morph_out, y_morph_out, x_target_out, y_target_out,
36-
min_index, max_index) where the target values remain the same and
37-
the morph data is shifted according to the squeeze. The morphed
38-
data is returned on the same grid as the unmorphed data.
39-
The min_index and max_index are the last index before the
40-
interpolated region and the first index after the interpolated
41-
region, respectively. If there is no extrapolation it returns None.
39+
A tuple (x_morph_out, y_morph_out, x_target_out, y_target_out)
40+
where the target values remain the same and the morph data is
41+
shifted according to the squeeze. The morphed data is returned on
42+
the same grid as the unmorphed data.
4243
4344
Example
4445
-------
@@ -66,8 +67,8 @@ def morph(self, x_morph, y_morph, x_target, y_target):
6667
self.y_morph_out = CubicSpline(x_squeezed, self.y_morph_in)(
6768
self.x_morph_in
6869
)
69-
left_extrap = np.where(self.x_morph_in < x_squeezed[0])[0]
70-
right_extrap = np.where(self.x_morph_in > x_squeezed[-1])[0]
71-
min_index = left_extrap[-1] if left_extrap.size else None
72-
max_index = right_extrap[0] if right_extrap.size else None
73-
return self.xyallout + (min_index, max_index)
70+
low_extrap = np.where(self.x_morph_in < x_squeezed[0])[0]
71+
high_extrap = np.where(self.x_morph_in > x_squeezed[-1])[0]
72+
self.extrap_index_low = low_extrap[-1] if low_extrap.size else None
73+
self.extrap_index_high = high_extrap[0] if high_extrap.size else None
74+
return self.xyallout

tests/test_morphsqueeze.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,40 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
4747
squeeze_polynomial = Polynomial(squeeze_coeffs)
4848
x_squeezed = x_morph + squeeze_polynomial(x_morph)
4949
y_morph = np.sin(x_squeezed)
50+
low_extrap = np.where(x_morph < x_squeezed[0])[0]
51+
high_extrap = np.where(x_morph > x_squeezed[-1])[0]
52+
extrap_index_low_expected = low_extrap[-1] if low_extrap.size else None
53+
extrap_index_high_expected = high_extrap[0] if high_extrap.size else None
5054
x_morph_expected = x_morph
5155
y_morph_expected = np.sin(x_morph)
5256
morph = MorphSqueeze()
5357
morph.squeeze = squeeze_coeffs
54-
(
55-
x_morph_actual,
56-
y_morph_actual,
57-
x_target_actual,
58-
y_target_actual,
59-
low_extrap_idx,
60-
high_extrap_idx,
61-
) = morph(x_morph, y_morph, x_target, y_target)
62-
if low_extrap_idx is None and high_extrap_idx is None:
63-
assert np.allclose(y_morph_actual, y_morph_expected, atol=1e-6)
64-
else:
65-
interp_start = low_extrap_idx + 1 if low_extrap_idx is not None else 0
66-
interp_end = (
67-
high_extrap_idx
68-
if high_extrap_idx is not None
69-
else len(y_morph_actual)
70-
)
71-
assert np.allclose(
72-
y_morph_actual[interp_start:interp_end],
73-
y_morph_expected[interp_start:interp_end],
74-
atol=1e-6,
75-
)
58+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph(
59+
x_morph, y_morph, x_target, y_target
60+
)
61+
extrap_index_low = morph.extrap_index_low
62+
extrap_index_high = morph.extrap_index_high
63+
if extrap_index_low is None:
64+
extrap_index_low = 0
65+
elif extrap_index_high is None:
66+
extrap_index_high = -1
67+
assert np.allclose(
68+
y_morph_actual[extrap_index_low + 1 : extrap_index_high],
69+
y_morph_expected[extrap_index_low + 1 : extrap_index_high],
70+
atol=1e-6,
71+
)
72+
assert np.allclose(
73+
y_morph_actual[:extrap_index_low],
74+
y_morph_expected[:extrap_index_low],
75+
atol=1e-3,
76+
)
77+
assert np.allclose(
78+
y_morph_actual[extrap_index_high:],
79+
y_morph_expected[extrap_index_high:],
80+
atol=1e-3,
81+
)
82+
assert morph.extrap_index_low == extrap_index_low_expected
83+
assert morph.extrap_index_high == extrap_index_high_expected
7684
assert np.allclose(x_morph_actual, x_morph_expected)
7785
assert np.allclose(x_target_actual, x_target)
7886
assert np.allclose(y_target_actual, y_target)

0 commit comments

Comments
 (0)