forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_morphpy.py
More file actions
270 lines (236 loc) · 9.02 KB
/
test_morphpy.py
File metadata and controls
270 lines (236 loc) · 9.02 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
from pathlib import Path
import numpy as np
import pytest
from diffpy.morph.morphapp import create_option_parser, single_morph
from diffpy.morph.morphpy import __get_morph_opts__, morph, morph_arrays
from diffpy.morph.tools import getRw
thisfile = locals().get("__file__", "file.py")
tests_dir = Path(thisfile).parent.resolve()
testdata_dir = tests_dir.joinpath("testdata")
testsequence_dir = testdata_dir.joinpath("testsequence")
nickel_PDF = testdata_dir.joinpath("nickel_ss0.01.cgr")
serial_JSON = testdata_dir.joinpath("testsequence_serialfile.json")
testsaving_dir = testsequence_dir.joinpath("testsaving")
test_saving_succinct = testsaving_dir.joinpath("succinct")
test_saving_verbose = testsaving_dir.joinpath("verbose")
tssf = testdata_dir.joinpath("testsequence_serialfile.json")
class TestMorphpy:
@pytest.fixture
def setup_morph(self):
self.parser = create_option_parser()
filenames = [
"g_174K.gr",
"f_180K.gr",
"e_186K.gr",
"d_192K.gr",
"c_198K.gr",
"b_204K.gr",
"a_210K.gr",
]
self.testfiles = []
self.morphapp_results = {}
# Parse arguments sorting by field
(opts, pargs) = self.parser.parse_args(
[
"--scale",
"1",
"--stretch",
"0",
"-n",
"--sort-by",
"temperature",
]
)
for filename in filenames:
self.testfiles.append(testsequence_dir.joinpath(filename))
# Run multiple single morphs
morph_file = self.testfiles[0]
for target_file in self.testfiles[1:]:
pargs = [morph_file, target_file]
# store in same format of dictionary as multiple_targets
self.morphapp_results.update(
{
target_file.name: single_morph(
self.parser, opts, pargs, stdout_flag=False
)
}
)
return
def test_morph_opts(self, setup_morph):
kwargs = {
"verbose": False,
"pearson": False,
"addpearson": False,
"apply": False,
"reverse": False,
"get_diff": False,
"multiple_morphs": False,
"multiple_targets": False,
}
kwargs_copy = kwargs.copy()
opts, _ = __get_morph_opts__(
self.parser, scale=1, stretch=0, smear=0, plot=False, **kwargs_copy
)
# Special set true/false operations should be removed
# when their input value is False
for opt in kwargs:
if opt == "apply":
assert getattr(opts, "refine")
else:
assert getattr(opts, opt) is None or not getattr(opts, opt)
kwargs = {
"verbose": True,
"pearson": True,
"addpearson": True,
"apply": True,
"reverse": True,
"get_diff": True,
"multiple_morphs": True,
"multiple_targets": True,
}
kwargs_copy = kwargs.copy()
opts, _ = __get_morph_opts__(
self.parser, scale=1, stretch=0, smear=0, plot=False, **kwargs_copy
)
for opt in kwargs:
if opt == "apply":
assert not getattr(opts, "refine")
# These options are not enabled in morphpy
elif opt == "multiple_morphs" or opt == "multiple_targets":
assert getattr(opts, opt) is None or not getattr(opts, opt)
# Special set true/false operations should NOT be removed
# when their input value is True
else:
assert getattr(opts, opt)
def test_morph(self, setup_morph):
morph_results = {}
morph_file = self.testfiles[0]
for target_file in self.testfiles[1:]:
mr, grm = morph(
morph_file,
target_file,
scale=1,
stretch=0,
sort_by="temperature",
)
_, grt = morph(target_file, target_file)
morph_results.update({target_file.name: mr})
class Chain:
xyallout = grm[:, 0], grm[:, 1], grt[:, 0], grt[:, 1]
chain = Chain()
rw = getRw(chain)
del chain
assert np.allclose(
[rw], [self.morphapp_results[target_file.name]["Rw"]]
)
# Check values in dictionaries are approximately equal
for file in morph_results.keys():
morph_params = morph_results[file]
morphapp_params = self.morphapp_results[file]
for key in morph_params.keys():
assert morph_params[key] == pytest.approx(
morphapp_params[key], abs=1e-08
)
def test_exclude(self, setup_morph):
morph_file = self.testfiles[0]
target_file = self.testfiles[-1]
morph_info, _ = morph(
morph_file,
target_file,
scale=1,
stretch=0,
exclude=["scale", "stretch"],
sort_by="temperature",
)
# Nothing should be refined
assert pytest.approx(morph_info["scale"]) == 1
assert pytest.approx(morph_info["stretch"]) == 0
morph_info, _ = morph(
morph_file,
target_file,
scale=1,
stretch=0,
exclude=["scale"],
sort_by="temperature",
)
# Stretch only should be refined
assert pytest.approx(morph_info["scale"]) == 1
assert pytest.approx(morph_info["stretch"]) != 0
morph_info, _ = morph(
morph_file,
target_file,
scale=1,
stretch=0,
exclude=["stretch"],
sort_by="temperature",
)
# Scale only should be refined
assert pytest.approx(morph_info["scale"]) != 1
assert pytest.approx(morph_info["stretch"]) == 0
def test_morphpy(self, setup_morph):
morph_results = {}
morph_file = self.testfiles[0]
for target_file in self.testfiles[1:]:
_, grm0 = morph(morph_file, morph_file)
_, grt = morph(target_file, target_file)
mr, grm = morph_arrays(
grm0, grt, scale=1, stretch=0, sort_by="temperature"
)
morph_results.update({target_file.name: mr})
class Chain:
xyallout = grm[:, 0], grm[:, 1], grt[:, 0], grt[:, 1]
chain = Chain()
rw = getRw(chain)
del chain
assert np.allclose(
[rw], [self.morphapp_results[target_file.name]["Rw"]]
)
# Check values in dictionaries are approximately equal
for file in morph_results.keys():
morph_params = morph_results[file]
morphapp_params = self.morphapp_results[file]
for key in morph_params.keys():
assert morph_params[key] == pytest.approx(
morphapp_params[key], abs=1e-08
)
def test_morphfuncy(self, setup_morph):
def gaussian(x, mu, sigma):
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
sigma * np.sqrt(2 * np.pi)
)
def gaussian_like_function(x, y, mu):
return gaussian((x + y) / 2, mu, 3)
morph_r = np.linspace(0, 100, 1001)
morph_gr = np.linspace(0, 100, 1001)
target_r = np.linspace(0, 100, 1001)
target_gr = 0.5 * gaussian(target_r, 50, 5) + 0.05
morph_info, _ = morph_arrays(
np.array([morph_r, morph_gr]).T,
np.array([target_r, target_gr]).T,
scale=1,
smear=3.75,
vshift=0.01,
funcy=(gaussian_like_function, {"mu": 47.5}),
tolerance=1e-12,
)
assert pytest.approx(morph_info["scale"]) == 0.5
assert pytest.approx(morph_info["vshift"]) == 0.05
assert pytest.approx(abs(morph_info["smear"])) == 4.0
assert pytest.approx(morph_info["funcy"]["mu"]) == 50.0
def test_morphpy_outputs(self, tmp_path):
r = np.linspace(0, 1, 11)
gr = np.linspace(0, 1, 11)
def linear(x, y, s):
return s * (x + y)
morph_info, _ = morph_arrays(
np.array([r, gr]).T,
np.array([r, gr]).T,
squeeze=[1, 2, 3, 4, 5],
funcy=(linear, {"s": 2.5}),
apply=True,
)
print(morph_info)
for i in range(5):
assert pytest.approx(morph_info["squeeze"][f"a{i}"]) == i + 1
assert pytest.approx(morph_info["funcy"]["s"]) == 2.5