-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_parsers.py
More file actions
339 lines (290 loc) · 10.6 KB
/
test_parsers.py
File metadata and controls
339 lines (290 loc) · 10.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
#!/usr/bin/env python
##############################################################################
#
# diffpy.structure by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2006 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas, Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Unit tests for structure.parsers module."""
import os
import re
import tempfile
import unittest
import numpy
import pytest
from diffpy.structure import Atom, Lattice, Structure
from diffpy.structure.structureerrors import StructureFormatError
# ----------------------------------------------------------------------------
class TestP_xyz(unittest.TestCase):
"""test Parser for xyz file format"""
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.datafile = datafile
def setUp(self):
self.stru = Structure()
self.format = "xyz"
self.tmpnames = []
return
def tearDown(self):
for f in self.tmpnames:
os.remove(f)
return
def mktmpfile(self):
with tempfile.NamedTemporaryFile(delete=False) as ftmp:
self.tmpnames.append(ftmp.name)
return self.tmpnames[-1]
def test_read_xyz(self):
"""check reading of normal xyz file"""
stru = self.stru
stru.read(self.datafile("bucky.xyz"), self.format)
s_els = [a.element for a in stru]
self.assertEqual(stru.title, "bucky-ball")
self.assertEqual(s_els, 60 * ["C"])
return
def test_read_xyz_bad(self):
"""check exceptions when reading invalid xyz file"""
stru = self.stru
self.assertRaises(StructureFormatError, stru.read, self.datafile("bucky-bad1.xyz"), self.format)
self.assertRaises(StructureFormatError, stru.read, self.datafile("bucky-bad2.xyz"), self.format)
self.assertRaises(StructureFormatError, stru.read, self.datafile("bucky-plain.xyz"), self.format)
self.assertRaises(StructureFormatError, stru.read, self.datafile("hexagon-raw.xy"), self.format)
return
def test_writeStr_xyz(self):
"""check string representation of normal xyz file"""
stru = self.stru
stru.title = "test of writeStr"
stru.lattice = Lattice(1.0, 2.0, 3.0, 90.0, 90.0, 90.0)
stru[:] = [Atom("H", [1.0, 1.0, 1.0]), Atom("Cl", [3.0, 2.0, 1.0])]
s1 = stru.writeStr(self.format)
s1 = re.sub("[ \t]+", " ", s1)
s0 = "2\n%s\nH 1 2 3\nCl 3 4 3\n" % stru.title
self.assertEqual(s1, s0)
return
def test_write_xyz(self):
"""check writing of normal xyz file"""
stru = self.stru
stru.title = "test of writeStr"
stru.lattice = Lattice(1.0, 2.0, 3.0, 90.0, 90.0, 90.0)
stru[:] = [Atom("H", [1.0, 1.0, 1.0]), Atom("Cl", [3.0, 2.0, 1.0])]
filename = self.mktmpfile()
stru.write(filename, self.format)
with open(filename) as fp:
f_s = fp.read()
f_s = re.sub("[ \t]+", " ", f_s)
s_s = "2\n%s\nH 1 2 3\nCl 3 4 3\n" % stru.title
self.assertEqual(f_s, s_s)
return
# End of class TestP_xyz
# ----------------------------------------------------------------------------
class TestP_rawxyz(unittest.TestCase):
"""test Parser for rawxyz file format"""
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.datafile = datafile
def setUp(self):
self.stru = Structure()
self.format = "rawxyz"
return
def test_read_plainxyz(self):
"""check reading of a plain xyz file"""
stru = self.stru
stru.read(self.datafile("bucky-plain.xyz"), self.format)
s_els = [a.element for a in stru]
self.assertEqual(stru.title, "bucky-plain")
self.assertEqual(s_els, 60 * ["C"])
return
def test_read_plainxyz_bad(self):
"""check exceptions when reading invalid plain xyz file"""
stru = self.stru
self.assertRaises(StructureFormatError, stru.read, self.datafile("bucky-plain-bad.xyz"), self.format)
return
def test_read_rawxyz(self):
"""check reading of raw xyz file"""
stru = self.stru
stru.read(self.datafile("bucky-raw.xyz"), self.format)
s_els = [a.element for a in stru]
self.assertEqual(stru.title, "bucky-raw")
self.assertEqual(s_els, 60 * [""])
stru.read(self.datafile("hexagon-raw.xyz"), self.format)
zs = [a.xyz[-1] for a in stru]
self.assertEqual(zs, 6 * [0.0])
return
def test_read_rawxyz_bad(self):
"""check exceptions when reading unsupported xy file"""
stru = self.stru
self.assertRaises(StructureFormatError, stru.read, self.datafile("hexagon-raw-bad.xyz"), self.format)
self.assertRaises(StructureFormatError, stru.read, self.datafile("hexagon-raw.xy"), self.format)
return
def test_writeStr_rawxyz(self):
"""check writing of normal xyz file"""
stru = self.stru
stru.title = "test of writeStr"
stru.lattice = Lattice(1.0, 2.0, 3.0, 90.0, 90.0, 90.0)
# plain version
stru[:] = [Atom("H", [1.0, 1.0, 1.0])]
s1 = stru.writeStr(self.format)
s1 = re.sub("[ \t]+", " ", s1)
s0 = "H 1 2 3\n"
# brutal raw version
stru[0].element = ""
s1 = stru.writeStr(self.format)
s0 = "1 2 3\n"
self.assertEqual(s1, s0)
return
# End of class TestP_rawxyz
# ----------------------------------------------------------------------------
class TestP_pdb(unittest.TestCase):
"""test Parser for PDB file format"""
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.datafile = datafile
def setUp(self):
self.stru = Structure()
self.format = "pdb"
self.places = 3
def test_read_pdb_arginine(self):
"""check reading of arginine PDB file"""
stru = self.stru
stru.read(self.datafile("arginine.pdb"), self.format)
f_els = [
"N",
"C",
"C",
"O",
"C",
"C",
"C",
"N",
"C",
"N",
"N",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"H",
"O",
"H",
]
s_els = [a.element for a in stru]
self.assertEqual(s_els, f_els)
s_lat = [
stru.lattice.a,
stru.lattice.b,
stru.lattice.c,
stru.lattice.alpha,
stru.lattice.beta,
stru.lattice.gamma,
]
f_lat = [1.0, 1.0, 1.0, 90.0, 90.0, 90.0]
self.assertEqual(s_lat, f_lat)
a0 = stru[0]
self.assertTrue(numpy.allclose(a0.xyz, [0.735, 2.219, 1.389]))
def test_rwStr_pdb_CdSe(self):
"""check conversion to PDB file format"""
stru = self.stru
stru.read(self.datafile("CdSe_bulk.stru"), "pdffit")
s = stru.writeStr(self.format)
# all lines should be 80 characters long
linelens = [len(line) for line in s.split("\n") if line != ""]
self.assertEqual(linelens, len(linelens) * [80])
# now clean and re-read structure
stru = Structure()
stru.readStr(s, self.format)
s_els = [a.element for a in stru]
f_els = ["Cd", "Cd", "Se", "Se"]
self.assertEqual(s_els, f_els)
s_lat = [
stru.lattice.a,
stru.lattice.b,
stru.lattice.c,
stru.lattice.alpha,
stru.lattice.beta,
stru.lattice.gamma,
]
f_lat = [4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0]
self.assertTrue(numpy.allclose(s_lat, f_lat, atol=5e-4))
a0 = stru[0]
s_Uii = [a0.U[i, i] for i in range(3)]
f_Uii = [0.01303035, 0.01303035, 0.01401959]
self.assertTrue(numpy.allclose(s_Uii, f_Uii, atol=5e-4))
s_sigUii = [a0.sigU[i, i] for i in range(3)]
f_sigUii = [0.00011127, 0.00011127, 0.00019575]
self.assertTrue(numpy.allclose(s_sigUii, f_sigUii, atol=5e-4))
s_title = stru.title
f_title = "Cell structure file of CdSe #186"
self.assertEqual(s_title, f_title)
# End of class TestP_pdb
# ----------------------------------------------------------------------------
class TestP_xcfg(unittest.TestCase):
"""test Parser for XCFG file format"""
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.datafile = datafile
def setUp(self):
self.stru = Structure()
self.format = "xcfg"
self.places = 6
def test_read_xcfg(self):
"""check reading of BubbleRaft XCFG file"""
stru = self.stru
stru.read(self.datafile("BubbleRaftShort.xcfg"), self.format)
f_els = 500 * ["Ar"]
s_els = [a.element for a in stru]
self.assertEqual(s_els, f_els)
self.assertAlmostEqual(stru.distance(82, 357), 47.5627, 3)
s_lat = [
stru.lattice.a,
stru.lattice.b,
stru.lattice.c,
stru.lattice.alpha,
stru.lattice.beta,
stru.lattice.gamma,
]
f_lat = [127.5, 119.5, 3.0, 90.0, 90.0, 90.0]
self.assertTrue(numpy.allclose(s_lat, f_lat))
return
def test_rwStr_xcfg_CdSe(self):
"""check conversion to XCFG file format"""
stru = self.stru
stru.read(self.datafile("CdSe_bulk.stru"), "pdffit")
s = stru.writeStr(self.format)
stru = Structure()
stru.readStr(s, self.format)
s_els = [a.element for a in stru]
f_els = ["Cd", "Cd", "Se", "Se"]
self.assertEqual(s_els, f_els)
s_lat = [
stru.lattice.a,
stru.lattice.b,
stru.lattice.c,
stru.lattice.alpha,
stru.lattice.beta,
stru.lattice.gamma,
]
f_lat = [4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0]
self.assertTrue(numpy.allclose(s_lat, f_lat))
a0 = stru[0]
s_Uii = [a0.U[i, i] for i in range(3)]
f_Uii = [0.01303035, 0.01303035, 0.01401959]
self.assertTrue(numpy.allclose(s_Uii, f_Uii))
# End of class TestP_xcfg
# ----------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()