Skip to content

Commit a48ce43

Browse files
committed
Test: added different grids
1 parent 14d92cb commit a48ce43

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import numpy as np
21
from numpy.polynomial import Polynomial
32
from scipy.interpolate import interp1d
43

@@ -27,17 +26,15 @@ class MorphSqueeze(Morph):
2726

2827
def morph(self, x_morph, y_morph, x_target, y_target):
2928
Morph.morph(self, x_morph, y_morph, x_target, y_target)
30-
if self.squeeze is None or np.allclose(self.squeeze, 0):
29+
if self.squeeze is None:
3130
self.x_morph_out = self.x_morph_in
3231
self.y_morph_out = self.y_morph_in
3332
return self.xyallout
3433

3534
squeeze_polynomial = Polynomial(self.squeeze)
3635
x_squeezed = self.x_morph_in + squeeze_polynomial(self.x_morph_in)
37-
3836
self.y_morph_out = interp1d(
3937
x_squeezed, self.y_morph_in, kind="cubic", bounds_error=False
4038
)(self.x_morph_in)
41-
4239
self.x_morph_out = self.x_morph_in
4340
return self.xyallout

tests/test_morphsqueeze.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33
from numpy.polynomial import Polynomial
4+
from scipy.interpolate import interp1d
45

56
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
67

@@ -21,40 +22,36 @@
2122
[0.1, 0.3],
2223
# 4th order squeeze coefficients
2324
[0.2, -0.01, 0.001, -0.001, 0.0004],
24-
# Zeros and non-zeros, expect 0 + a1x + 0 + a3x**3
25+
# Zeros and non-zeros, the full polynomial is applied
2526
[0, 0.03, 0, -0.001],
2627
# Testing zeros, expect no squeezing
27-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
28+
[0, 0, 0, 0, 0, 0],
2829
],
2930
)
3031
def test_morphsqueeze(squeeze_coeffs):
31-
x_target = np.linspace(0, 10, 1001)
32-
y_target = np.sin(x_target)
33-
34-
x_make = np.linspace(-3, 13, 1601)
35-
lower_idx = np.where(x_make == 0.0)[0][0]
36-
upper_idx = np.where(x_make == 10.0)[0][0]
37-
32+
x_expected = np.linspace(0, 10, 1001)
33+
y_expected = np.sin(x_expected)
34+
x_make = np.linspace(-3, 13, 3250)
3835
squeeze_polynomial = Polynomial(squeeze_coeffs)
3936
x_squeezed = x_make + squeeze_polynomial(x_make)
40-
41-
x_morph = x_make.copy()
4237
y_morph = np.sin(x_squeezed)
43-
4438
morph = MorphSqueeze()
4539
morph.squeeze = squeeze_coeffs
46-
47-
x_actual, y_actual, x_expected, y_expected = morph(
48-
x_morph, y_morph, x_target, y_target
40+
x_actual, y_actual, x_target, y_target = morph(
41+
x_make, y_morph, x_expected, y_expected
4942
)
50-
y_actual = y_actual[lower_idx : upper_idx + 1]
43+
y_actual = interp1d(x_actual, y_actual)(x_target)
44+
x_actual = x_target
5145
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)
5249

5350
# Plotting code used for figures in PR comments
5451
# https://github.com/diffpy/diffpy.morph/pull/180
5552
# plt.figure()
5653
# plt.scatter(x_expected, y_expected, color='black', label='Expected')
57-
# plt.plot(x_morph, y_morph, color='purple', label='morph')
54+
# plt.plot(x_make, y_morph, color='purple', label='morph')
5855
# plt.plot(x_actual, y_actual, '--', color='gold', label='Actual')
5956
# plt.legend()
6057
# plt.show()

0 commit comments

Comments
 (0)