Skip to content

Commit d42d234

Browse files
committed
Test: changed names, coarsed grid and added comment
1 parent a48ce43 commit d42d234

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from numpy.polynomial import Polynomial
2-
from scipy.interpolate import interp1d
2+
from scipy.interpolate import CubicSpline
33

44
from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph
55

@@ -33,8 +33,11 @@ def morph(self, x_morph, y_morph, x_target, y_target):
3333

3434
squeeze_polynomial = Polynomial(self.squeeze)
3535
x_squeezed = self.x_morph_in + squeeze_polynomial(self.x_morph_in)
36-
self.y_morph_out = interp1d(
37-
x_squeezed, self.y_morph_in, kind="cubic", bounds_error=False
38-
)(self.x_morph_in)
39-
self.x_morph_out = self.x_morph_in
36+
self.y_morph_out = CubicSpline(x_squeezed, self.y_morph_in)(
37+
self.x_morph_in
38+
)
39+
self.y_morph_out = CubicSpline(self.x_morph_in, self.y_morph_out)(
40+
self.x_target_in
41+
)
42+
self.x_morph_out = self.x_target_in
4043
return self.xyallout

tests/test_morphsqueeze.py

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
import pytest
33
from numpy.polynomial import Polynomial
4-
from scipy.interpolate import interp1d
54

65
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
76

@@ -29,29 +28,20 @@
2928
],
3029
)
3130
def test_morphsqueeze(squeeze_coeffs):
32-
x_expected = np.linspace(0, 10, 1001)
33-
y_expected = np.sin(x_expected)
34-
x_make = np.linspace(-3, 13, 3250)
31+
x_target_expected = np.linspace(0, 10, 101)
32+
y_target_expected = np.sin(x_target_expected)
33+
# Different grid for morph data to test inputs with different grids
34+
# Morph grid must be finer than the target to avoid interpolation issues
35+
x_morph = np.linspace(-3, 13, 301)
3536
squeeze_polynomial = Polynomial(squeeze_coeffs)
36-
x_squeezed = x_make + squeeze_polynomial(x_make)
37+
x_squeezed = x_morph + squeeze_polynomial(x_morph)
3738
y_morph = np.sin(x_squeezed)
3839
morph = MorphSqueeze()
3940
morph.squeeze = squeeze_coeffs
40-
x_actual, y_actual, x_target, y_target = morph(
41-
x_make, y_morph, x_expected, y_expected
41+
x_morph_actual, y_morph_actual, x_target_actual, y_target_actual = morph(
42+
x_morph, y_morph, x_target_expected, y_target_expected
4243
)
43-
y_actual = interp1d(x_actual, y_actual)(x_target)
44-
x_actual = x_target
45-
assert np.allclose(y_actual, y_expected)
46-
assert np.allclose(x_actual, x_expected)
47-
assert np.allclose(x_target, x_expected)
48-
assert np.allclose(y_target, y_expected)
49-
50-
# Plotting code used for figures in PR comments
51-
# https://github.com/diffpy/diffpy.morph/pull/180
52-
# plt.figure()
53-
# plt.scatter(x_expected, y_expected, color='black', label='Expected')
54-
# plt.plot(x_make, y_morph, color='purple', label='morph')
55-
# plt.plot(x_actual, y_actual, '--', color='gold', label='Actual')
56-
# plt.legend()
57-
# plt.show()
44+
assert np.allclose(y_morph_actual, y_target_expected)
45+
assert np.allclose(x_morph_actual, x_target_expected)
46+
assert np.allclose(x_target_actual, x_target_expected)
47+
assert np.allclose(y_target_actual, y_target_expected)

0 commit comments

Comments
 (0)