forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_morphsqueeze.py
65 lines (52 loc) · 1.99 KB
/
test_morphsqueeze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import pytest
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
class TestMorphSqueeze:
@pytest.fixture
def setup(self):
# Create data for testing
self.x_morph = np.linspace(0, 10, 1000)
self.y_morph = np.sin(self.x_morph)
self.x_target = self.x_morph.copy()
self.y_target = self.y_morph.copy()
return
def test_no_squeeze(self, setup):
"""When both squeeze are zero, the input should be unchanged."""
morph = MorphSqueeze()
morph.squeeze_1 = 0.0
morph.squeeze_2 = 0.0
x_morph, y_morph, x_target, y_target = morph(
self.x_morph, self.y_morph, self.x_target, self.y_target
)
# Verify that the morph output matches the original input
assert np.allclose(x_morph, self.x_morph)
assert np.allclose(y_morph, self.y_morph)
# And the target arrays remain unchanged
assert np.allclose(x_target, self.x_target)
assert np.allclose(y_target, self.y_target)
def test_morph_with_squeeze(self, setup):
"""Test that with a non-zero squeeze,
x_morph is transformed non-linearly."""
morph = MorphSqueeze()
morph.squeeze_1 = -0.07
morph.squeeze_2 = 0.1
x_new, y_new, x_target, y_target = morph(
self.x_morph, self.y_morph, self.x_target, self.y_target
)
# Check that target arrays remain unchanged
assert np.allclose(self.y_target, y_target)
# For this test, we expect:
# x_new = x_morph + squeeze_1 * x_morph ** 2 +
# squeeze_2 * x_morph ** 3
expected_x = (
self.x_morph
+ morph.squeeze_1 * self.x_morph**2
+ morph.squeeze_2 * self.x_morph**3
)
expected_y = np.sin(self.x_morph)
res = sum(np.fabs(expected_y - y_new))
res2 = sum(np.fabs(expected_x - x_new))
assert res < 0.1
assert res2 < 0.1
if __name__ == "__main__":
TestMorphSqueeze()