Skip to content

Commit b9d73d5

Browse files
committed
func/test: adding function and test for squeeze morph
1 parent 089fd38 commit b9d73d5

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

news/morphsqueeze.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
**Changed:**
66

7-
* Nothing changed
7+
* Added cubic and quadratic terms, as well as
8+
* squeeze_1 and squeeze_2 parameters
89

910
**Deprecated:**
1011

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MorphSqueeze(Morph):
1414
yinlabel = LABEL_GR # This label need to change to be more generic
1515
xoutlabel = LABEL_RA # This label need to change to be more generic
1616
youtlabel = LABEL_GR # This label need to change to be more generic
17-
parnames = ["squeeze"]
17+
parnames = ["squeeze_1", "squeeze_2"]
1818

1919
def morph(self, x_morph, y_morph, x_target, y_target):
2020
"""Resample arrays onto the specified grid using a non-linear squeeze.
@@ -35,12 +35,17 @@ def morph(self, x_morph, y_morph, x_target, y_target):
3535
# Initialize the parent class to set up attributes
3636
Morph.morph(self, x_morph, y_morph, x_target, y_target)
3737

38-
# If squeeze is zero, return original output
39-
if self.squeeze == 0:
38+
# If squeeze_1 and squeeze_2 are zero, return original output
39+
if self.squeeze_1 == 0 and self.squeeze_2 == 0:
4040
return self.xyallout
4141

4242
# Compute new x positions using the non-linear squeeze transformation:
43-
new_x = self.x_morph_in + self.squeeze * np.sin(self.x_morph_in)
43+
new_x = (
44+
self.x_morph_in
45+
+ self.squeeze_1 * self.x_morph_in**2
46+
+ self.squeeze_2 * self.x_morph_in**3
47+
)
48+
4449
self.x_morph_out = new_x
4550

4651
# Interpolate the y-values at the new x positions.

tests/test_morphsqueeze.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
class TestMorphSqueeze:
88
@pytest.fixture
99
def setup(self):
10-
# Create a sine-wave for testing
11-
self.x_morph = np.linspace(0, 2 * np.pi, 1000)
10+
# Create data for testing
11+
self.x_morph = np.linspace(0, 10, 1000)
1212
self.y_morph = np.sin(self.x_morph)
1313
self.x_target = self.x_morph.copy()
1414
self.y_target = self.y_morph.copy()
1515
return
1616

1717
def test_no_squeeze(self, setup):
18-
"""When squeeze is zero, the input should be unchanged."""
18+
"""When both squeeze are zero, the input should be unchanged."""
1919
morph = MorphSqueeze()
20-
morph.squeeze = 0.0
20+
morph.squeeze_1 = 0.0
21+
morph.squeeze_2 = 0.0
2122

2223
x_morph, y_morph, x_target, y_target = morph(
2324
self.x_morph, self.y_morph, self.x_target, self.y_target
@@ -34,7 +35,9 @@ def test_morph_with_squeeze(self, setup):
3435
"""Test that with a non-zero squeeze,
3536
x_morph is transformed non-linearly."""
3637
morph = MorphSqueeze()
37-
morph.squeeze = 0.7
38+
morph.squeeze_1 = -0.07
39+
morph.squeeze_2 = 0.1
40+
3841
x_new, y_new, x_target, y_target = morph(
3942
self.x_morph, self.y_morph, self.x_target, self.y_target
4043
)
@@ -43,9 +46,14 @@ def test_morph_with_squeeze(self, setup):
4346
assert np.allclose(self.y_target, y_target)
4447

4548
# For this test, we expect:
46-
# x_new = x_morph + squeeze_factor * sin(x_morph)
47-
expected_x = self.x_morph + morph.squeeze * np.sin(self.x_morph)
48-
expected_y = np.sin(expected_x)
49+
# x_new = x_morph + squeeze_1 * x_morph ** 2 +
50+
# squeeze_2 * x_morph ** 3
51+
expected_x = (
52+
self.x_morph
53+
+ morph.squeeze_1 * self.x_morph**2
54+
+ morph.squeeze_2 * self.x_morph**3
55+
)
56+
expected_y = np.interp(expected_x, self.x_morph, self.y_morph)
4957

5058
# Allow for some tolerance because of numerical interpolation if used
5159
res = sum(np.fabs(expected_y - y_new))

0 commit comments

Comments
 (0)