-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_morph_func.py
More file actions
158 lines (140 loc) · 5.45 KB
/
test_morph_func.py
File metadata and controls
158 lines (140 loc) · 5.45 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
import numpy as np
from diffpy.morph.morph_api import morph, morph_default_config
from tests.test_morphstretch import heaviside
def test_morphfunc_verbose():
lb, ub = 1, 2
x_target = np.arange(0.01, 5, 0.01)
y_target = heaviside(x_target, lb, ub)
# expand 30%
stretch = 0.3
x_morph = x_target.copy()
y_morph = heaviside(x_target, lb * (1 + stretch), ub * (1 + stretch))
cfg = morph_default_config(stretch=0.1) # off init
morph(x_morph, y_morph, x_target, y_target, verbose=True, **cfg)
def test_fixed_morph_with_morphfunc():
lb, ub = 1, 2
x_target = np.arange(0.01, 5, 0.01)
y_target = heaviside(x_target, lb, ub)
# expand 30%
stretch = 0.3
x_morph = x_target.copy()
y_morph = heaviside(x_target, lb * (1 + stretch), ub * (1 + stretch))
cfg = morph_default_config(stretch=0.1) # off init
cfg["scale"] = 30
morph(
x_morph,
y_morph,
x_target,
y_target,
verbose=True,
fixed_operations=["scale"],
**cfg,
)
def test_stretch_with_morphfunc():
# use the same setup as test_moprhchain
lb, ub = 1, 2
x_target = np.arange(0.01, 5, 0.01)
y_target = heaviside(x_target, lb, ub)
# expand 30%
stretch = 0.3
x_morph = x_target.copy()
y_morph = heaviside(x_target, lb * (1 + stretch), ub * (1 + stretch))
cfg = morph_default_config(stretch=0.1) # off init
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
morphed_cfg = morph_rv["morphed_config"]
# verified they are morphable
x1, y1, x0, y0 = morph_rv["morph_chain"].xyallout
assert np.allclose(x0, x1)
assert np.allclose(y0, y1)
# verify morphed param
# note: because interpolation, the value might be off by 0.5
# negative sign as we are compress the gref
assert np.allclose(-stretch, morphed_cfg["stretch"], atol=1e-1)
def test_scale_with_morphfunc():
lb, ub = 1, 2
x_target = np.arange(0.01, 5, 0.01)
y_target = heaviside(x_target, lb, ub)
# scale 300%
scale = 3
x_morph = x_target.copy()
y_morph = y_target.copy()
y_morph *= scale
cfg = morph_default_config(scale=1.5) # off init
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
morphed_cfg = morph_rv["morphed_config"]
# verified they are morphable
x1, y1, x0, y0 = morph_rv["morph_chain"].xyallout
assert np.allclose(x0, x1)
assert np.allclose(y0, y1)
# verify morphed param
assert np.allclose(scale, 1 / morphed_cfg["scale"], atol=1e-1)
def test_smear_with_morph_func():
# gaussian func
sigma0 = 0.1
smear = 0.15
sigbroad = (sigma0**2 + smear**2) ** 0.5
r0 = 7 * np.pi / 22.0 * 2
x_target = np.arange(0.01, 5, 0.01)
y_target = np.exp(-0.5 * ((x_target - r0) / sigbroad) ** 2)
x_morph = x_target.copy()
y_morph = np.exp(-0.5 * ((x_morph - r0) / sigma0) ** 2)
cfg = morph_default_config(smear=0.1, scale=1.1, stretch=0.1) # off init
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
morphed_cfg = morph_rv["morphed_config"]
# verified they are morphable
x1, y1, x0, y0 = morph_rv["morph_chain"].xyallout
assert np.allclose(x0, x1)
assert np.allclose(y0, y1, atol=1e-3) # numerical error -> 1e-4
# verify morphed param
assert np.allclose(smear, morphed_cfg["smear"], atol=1e-1)
def test_squeeze_with_morph_func():
squeeze_init = {"a0": 0, "a1": -0.001, "a2": -0.0001, "a3": 0.0001}
x_morph = np.linspace(0, 10, 101)
y_morph = 2 * np.sin(
x_morph + x_morph * 0.01 + 0.0001 * x_morph**2 + 0.001 * x_morph**3
)
expected_squeeze = {"a0": 0, "a1": 0.01, "a2": 0.0001, "a3": 0.001}
expected_scale = 1 / 2
x_target = np.linspace(0, 10, 101)
y_target = np.sin(x_target)
cfg = morph_default_config(scale=1.1, squeeze=squeeze_init)
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
morphed_cfg = morph_rv["morphed_config"]
x_morph_out, y_morph_out, x_target_out, y_target_out = morph_rv[
"morph_chain"
].xyallout
assert np.allclose(x_morph_out, x_target_out)
assert np.allclose(y_morph_out, y_target_out, atol=1e-6)
assert np.allclose(
expected_squeeze["a0"], morphed_cfg["squeeze"]["a0"], atol=1e-6
)
assert np.allclose(
expected_squeeze["a1"], morphed_cfg["squeeze"]["a1"], atol=1e-6
)
assert np.allclose(
expected_squeeze["a2"], morphed_cfg["squeeze"]["a2"], atol=1e-6
)
assert np.allclose(
expected_squeeze["a3"], morphed_cfg["squeeze"]["a3"], atol=1e-6
)
assert np.allclose(expected_scale, morphed_cfg["scale"], atol=1e-6)
def test_funcy_with_morph_func():
def linear_function(x, y, scale, offset):
return (scale * x) * y + offset
x_morph = np.linspace(0, 10, 101)
y_morph = np.sin(x_morph)
x_target = x_morph.copy()
y_target = np.sin(x_target) * 2 * x_target + 0.4
cfg = morph_default_config(funcy={"scale": 1.2, "offset": 0.1})
cfg["function"] = linear_function
morph_rv = morph(x_morph, y_morph, x_target, y_target, **cfg)
morphed_cfg = morph_rv["morphed_config"]
x_morph_out, y_morph_out, x_target_out, y_target_out = morph_rv[
"morph_chain"
].xyallout
assert np.allclose(x_morph_out, x_target_out)
assert np.allclose(y_morph_out, y_target_out, atol=1e-6)
fitted_parameters = morphed_cfg["funcy"]
assert np.allclose(fitted_parameters["scale"], 2, atol=1e-6)
assert np.allclose(fitted_parameters["offset"], 0.4, atol=1e-6)