forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_morphapp.py
More file actions
369 lines (340 loc) · 12.6 KB
/
test_morphapp.py
File metadata and controls
369 lines (340 loc) · 12.6 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
from pathlib import Path
import numpy as np
import pytest
from diffpy.morph.morphapp import (
create_option_parser,
multiple_targets,
single_morph,
)
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 TestApp:
@pytest.fixture
def setup_parser(self):
self.parser = create_option_parser()
@pytest.fixture
def setup_morphsequence(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 = []
for filename in filenames:
self.testfiles.append(testsequence_dir.joinpath(filename))
return
def test_parser_numerical(self, setup_parser):
renamed_dests = {"slope": "baselineslope"}
# Check values parsed correctly
n_names = [
"--xmin",
"--xmax",
"--scale",
"--smear",
"--stretch",
"--slope",
"--qdamp",
]
n_values = [
"2.5",
"40",
"2.1",
"-0.8",
"0.0000005",
"-0.0000005",
".00000003",
]
n_names.extend(
[
"--radius",
"--pradius",
"--iradius",
"--ipradius",
"--pmin",
"--pmax",
]
)
n_values.extend(["+0.5", "-0.2", "+.3", "-.1", "2.5", "40"])
n_names.extend(["--lwidth", "--maglim", "--mag"])
n_values.extend(["1.6", "50", "5"])
n_total = len(n_names)
n_input = []
for idx in range(n_total):
n_input.append(n_names[idx])
n_input.append(n_values[idx])
n_input.append("leftover") # One leftover
n_opts, n_args = self.parser.parse_args(n_input)
n_opts_dict = vars(n_opts)
for idx in range(n_total):
n_parsed_name = n_names[idx][2:]
n_parsed_val = n_opts_dict.get(n_parsed_name)
if n_parsed_val is None:
assert (
n_parsed_name in renamed_dests
) # Ensure .get() failed due to destination renaming
n_parsed_name = renamed_dests.get(n_parsed_name)
n_parsed_val = n_opts_dict.get(n_parsed_name)
assert isinstance(n_parsed_val, float) # Check if value is a float
assert n_parsed_val == float(
n_values[idx]
) # Check correct value parsed
assert len(n_args) == 1 # Check one leftover
def test_parser_systemexits(self, capsys, setup_parser):
# ###Basic tests for any variety of morphing###
# Ensure only two pargs given for morphing
(opts, pargs) = self.parser.parse_args(["toofewfiles"])
with pytest.raises(SystemExit):
single_morph(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "You must supply MORPHFILE and TARGETFILE." in err
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "You must supply a FILE and DIRECTORY." in err
(opts, pargs) = self.parser.parse_args(["too", "many", "files"])
with pytest.raises(SystemExit):
single_morph(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert (
"Too many arguments. Make sure you only supply MORPHFILE and "
"TARGETFILE." in err
)
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert (
"Too many arguments. You must only supply a FILE and a DIRECTORY."
in err
)
# Make sure xmax greater than xmin
(opts, pargs) = self.parser.parse_args(
[f"{nickel_PDF}", f"{nickel_PDF}", "--xmin", "10", "--xmax", "1"]
)
with pytest.raises(SystemExit):
single_morph(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "xmin must be less than xmax" in err
# ###Tests exclusive to multiple morphs###
# Make sure we save to a directory that exists
# (user must create the directory if non-existing)
(opts, pargs) = self.parser.parse_args(
[
f"{nickel_PDF}",
f"{nickel_PDF}",
"-s",
"/nonexisting_directory/no_way_this_exists/nonexisting_path",
]
)
with pytest.raises(SystemExit):
single_morph(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "Unable to save to designated location." in err
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "is not a directory." in err
# Ensure first parg is a FILE and second parg is a DIRECTORY
(opts, pargs) = self.parser.parse_args(
[f"{nickel_PDF}", f"{nickel_PDF}"]
)
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
(opts, pargs) = self.parser.parse_args(
[f"{testsequence_dir}", f"{testsequence_dir}"]
)
_, err = capsys.readouterr()
assert "is not a directory." in err
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "is not a file." in err
# Try sorting by non-existing field
(opts, pargs) = self.parser.parse_args(
[f"{nickel_PDF}", f"{testsequence_dir}", "--sort-by", "fake_field"]
)
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "The requested field is missing from a file header." in err
(opts, pargs) = self.parser.parse_args(
[
f"{nickel_PDF}",
f"{testsequence_dir}",
"--sort-by",
"fake_field",
"--serial-file",
f"{serial_JSON}",
]
)
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "The requested field was not found in the metadata file." in err
# Try plotting an unknown parameter
(opts, pargs) = self.parser.parse_args(
[
f"{nickel_PDF}",
f"{testsequence_dir}",
"--plot-parameter",
"unknown",
]
)
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "Cannot find specified plot parameter. No plot shown." in err
# Try plotting an unrefined parameter
(opts, pargs) = self.parser.parse_args(
[
f"{nickel_PDF}",
f"{testsequence_dir}",
"--plot-parameter",
"stretch",
]
)
with pytest.raises(SystemExit):
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert (
"The plot parameter is missing values for at "
"least one morph and target pair. "
"No plot shown." in err
)
# Pass a non-float list to squeeze
(opts, pargs) = self.parser.parse_args(
[
f"{nickel_PDF}",
f"{nickel_PDF}",
"--squeeze",
"1,a,0",
]
)
with pytest.raises(SystemExit):
single_morph(self.parser, opts, pargs, stdout_flag=False)
_, err = capsys.readouterr()
assert "a could not be converted to float." in err
def test_morphsequence(self, setup_morphsequence):
# Parse arguments sorting by field
(opts, pargs) = self.parser.parse_args(
[
"--scale",
"1",
"--stretch",
"0",
"-n",
"--sort-by",
"temperature",
]
)
# Run multiple single morphs
single_results = {}
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
single_results.update(
{
target_file.name: single_morph(
self.parser, opts, pargs, stdout_flag=False
)
}
)
pargs = [morph_file, testsequence_dir]
# Run a morph sequence
sequence_results = multiple_targets(
self.parser, opts, pargs, stdout_flag=False
)
# Compare results
assert sequence_results == single_results
# Check using a serial file produces the same result
s_file = tssf.resolve().as_posix()
(opts, pargs) = self.parser.parse_args(
[
"--scale",
"1",
"--stretch",
"0",
"-n",
"--sort-by",
"temperature",
"--serial-file",
s_file,
]
)
pargs = [morph_file, testsequence_dir]
s_sequence_results = multiple_targets(
self.parser, opts, pargs, stdout_flag=False
)
assert s_sequence_results == sequence_results
def test_morphsmear(self, setup_parser, tmp_path):
def gaussian(x, mu, sigma):
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
sigma * np.sqrt(2 * np.pi)
)
# Generate the test files
x_grid = np.linspace(1, 101, 1001)
# Gaussian with STD 3 (morph)
g2 = gaussian(x_grid, 51, 3)
mf = tmp_path / "morph.txt"
with open(mf, "w") as f:
np.savetxt(f, np.array([x_grid, g2]).T)
# Gaussian with STD 5 (target)
g3 = gaussian(x_grid, 51, 5)
tf = tmp_path / "target.txt"
with open(tf, "w") as f:
np.savetxt(f, np.array([x_grid, g3]).T)
# Gaussian with STD 3 and baseline slope -0.5 (PDF morph)
g2_bl = gaussian(x_grid, 51, 3) / x_grid - 0.5 * x_grid
pmf = tmp_path / "pdf_morph.txt"
with open(pmf, "w") as f:
np.savetxt(f, np.array([x_grid, g2_bl]).T)
# Gaussian with STD 5 with baseline slope -0.5 (PDF target)
g3_bl = gaussian(x_grid, 51, 5) / x_grid - 0.5 * x_grid
ptf = tmp_path / "pdf_target.txt"
with open(ptf, "w") as f:
np.savetxt(f, np.array([x_grid, g3_bl]).T)
# No PDF smear (should not activate baseline slope)
(opts, _) = self.parser.parse_args(
[
"--smear",
"1",
"-n",
]
)
pargs = [mf, tf]
smear_results = single_morph(
self.parser, opts, pargs, stdout_flag=False
)
# Variances add, and 3^2+4^2=5^2
assert pytest.approx(abs(smear_results["smear"])) == 4.0
assert pytest.approx(smear_results["Rw"]) == 0.0
# PDF-specific smear (should activate baseline slope)
(opts, _) = self.parser.parse_args(
[
"--smear",
"100",
"--smear-pdf",
"1",
"-n",
]
)
pargs = [pmf, ptf]
pdf_smear_results = single_morph(
self.parser, opts, pargs, stdout_flag=False
)
assert pytest.approx(abs(pdf_smear_results["smear"])) == 4.0
assert pytest.approx(pdf_smear_results["Rw"]) == 0.0