forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_morphio.py
More file actions
194 lines (171 loc) · 6.33 KB
/
test_morphio.py
File metadata and controls
194 lines (171 loc) · 6.33 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
#!/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,
)
# Support Python 2
try:
from future_builtins import filter, zip
except ImportError:
pass
thisfile = locals().get("__file__", "file.py")
tests_dir = Path(thisfile).parent.resolve()
testdata_dir = tests_dir.joinpath("testdata")
testsequence_dir = testdata_dir.joinpath("testsequence")
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")
# Ignore PATH data when comparing files
def ignore_path(line):
# Lines containing FILE PATH data begin with '# from '
if "# from " in line:
return False
# Lines containing DIRECTORY PATH data begin with '# with '
if "# with " in line:
return False
return True
def isfloat(s):
"""True if s is convertible to float."""
try:
float(s)
return True
except ValueError:
pass
return False
class TestApp:
@pytest.fixture
def setup(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_morph_outputs(self, setup, tmp_path):
morph_file = self.testfiles[0]
target_file = self.testfiles[-1]
# Save multiple succinct morphs
tmp_succinct = tmp_path.joinpath("succinct")
tmp_succinct_name = tmp_succinct.resolve().as_posix()
(opts, pargs) = self.parser.parse_args(
[
"--multiple-targets",
"--sort-by",
"temperature",
"-s",
tmp_succinct_name,
"-n",
"--save-names-file",
tssf,
]
)
pargs = [morph_file, testsequence_dir]
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
# Save a single succinct morph
ssm = tmp_succinct.joinpath("single_succinct_morph.cgr")
ssm_name = ssm.resolve().as_posix()
(opts, pargs) = self.parser.parse_args(["-s", ssm_name, "-n"])
pargs = [morph_file, target_file]
single_morph(self.parser, opts, pargs, stdout_flag=False)
# Check the saved files are the same for succinct
common = []
for item in tmp_succinct.glob("**/*.*"):
if item.is_file():
common.append(item.relative_to(tmp_succinct).as_posix())
for file in common:
with open(tmp_succinct.joinpath(file)) as gf:
with open(test_saving_succinct.joinpath(file)) as tf:
generated = filter(ignore_path, gf)
target = filter(ignore_path, tf)
assert all(x == y for x, y in zip(generated, target))
# Save multiple verbose morphs
tmp_verbose = tmp_path.joinpath("verbose")
tmp_verbose_name = tmp_verbose.resolve().as_posix()
(opts, pargs) = self.parser.parse_args(
[
"--multiple-targets",
"--sort-by",
"temperature",
"-s",
tmp_verbose_name,
"-n",
"--save-names-file",
tssf,
"--verbose",
]
)
pargs = [morph_file, testsequence_dir]
multiple_targets(self.parser, opts, pargs, stdout_flag=False)
# Save a single verbose morph
svm = tmp_verbose.joinpath("single_verbose_morph.cgr")
svm_name = svm.resolve().as_posix()
(opts, pargs) = self.parser.parse_args(
["-s", svm_name, "-n", "--verbose"]
)
pargs = [morph_file, target_file]
single_morph(self.parser, opts, pargs, stdout_flag=False)
# Check the saved files are the same for verbose
common = []
for item in tmp_verbose.glob("**/*.*"):
if item.is_file():
common.append(item.relative_to(tmp_verbose).as_posix())
for file in common:
with open(tmp_verbose.joinpath(file)) as gf:
with open(test_saving_verbose.joinpath(file)) as tf:
generated = filter(ignore_path, gf)
target = filter(ignore_path, tf)
assert all(x == y for x, y in zip(generated, target))
def test_morph_squeeze_outputs(self, setup, tmp_path):
# The file squeeze_morph has a squeeze and stretch applied
morph_file = testdata_dir / "squeeze_morph.cgr"
target_file = testdata_dir / "squeeze_target.cgr"
sqr = tmp_path / "squeeze_morph_result.cgr"
sqr_name = sqr.resolve().as_posix()
# Note that stretch and hshift should not be considered
(opts, _) = self.parser.parse_args(
[
"--scale",
"2",
"--squeeze",
"0,-0.001,-0.0001,0.0001",
"--stretch",
"1",
"--hshift",
"1",
"-s",
sqr_name,
"-n",
"--verbose",
]
)
pargs = [morph_file, target_file]
single_morph(self.parser, opts, pargs, stdout_flag=False)
# Check squeeze morph generates the correct output
with open(sqr) as mf:
with open(target_file) as tf:
morphed = filter(ignore_path, mf)
target = filter(ignore_path, tf)
for m, t in zip(morphed, target):
m_row = m.split()
t_row = t.split()
assert len(m_row) == len(t_row)
for idx, _ in enumerate(m_row):
if isfloat(m_row[idx]) and isfloat(t_row[idx]):
assert np.isclose(
float(m_row[idx]), float(t_row[idx])
)
else:
assert m_row[idx] == t_row[idx]