forked from diffpy/diffpy.structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_p_cif.py
More file actions
416 lines (370 loc) · 15 KB
/
test_p_cif.py
File metadata and controls
416 lines (370 loc) · 15 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python
##############################################################################
#
# diffpy.structure by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2007 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Unit tests for diffpy.structure.parsers.p_cif module."""
import unittest
import numpy
import pytest
from diffpy.structure import Structure
from diffpy.structure.parsers import getParser
from diffpy.structure.parsers.p_cif import P_cif, getSymOp, leading_float
from diffpy.structure.structureerrors import StructureFormatError
# ----------------------------------------------------------------------------
class TestRoutines(unittest.TestCase):
def setUp(self):
return
def tearDown(self):
return
def test_leading_float(self):
"""Check leading_float()"""
self.assertEqual(0.37, leading_float("0.37(3)"))
self.assertEqual(0.37, leading_float("0.37ab\ncd"))
self.assertRaises(ValueError, leading_float, "q1")
return
def test_getSymOp(self):
"""Check getSymOp()"""
from diffpy.structure.spacegroups import Rot_X_mY_Z, SymOp, Tr_0_12_12
op = getSymOp("x,1/2-y,1/2+z")
op_std = SymOp(Rot_X_mY_Z, Tr_0_12_12)
self.assertEqual(str(op_std), str(op))
from diffpy.structure.spacegroups import Rot_mX_mXY_Z, Tr_0_0_12
op1 = getSymOp("-x,-x+y,1/2+z")
op1_std = SymOp(Rot_mX_mXY_Z, Tr_0_0_12)
self.assertEqual(str(op1_std), str(op1))
return
# End of class TestRoutines
# ----------------------------------------------------------------------------
class TestP_cif(unittest.TestCase):
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.datafile = datafile
self.pbteciffile = datafile("PbTe.cif")
self.badciffile = datafile("LiCl-bad.cif")
self.graphiteciffile = datafile("graphite.cif")
self.cdsebulkpdffitfile = datafile("CdSe_bulk.stru")
self.teiciffile = datafile("TeI.cif")
self.refciffile = datafile("Ni_ref.cif")
self.places = 6
def setUp(self):
self.ptest = P_cif()
self.pfile = P_cif()
return
def tearDown(self):
return
def test_parse(self):
"""Check P_cif.parse()"""
with open(self.pbteciffile) as fp1:
sgood = fp1.read()
with open(self.badciffile) as fp2:
sbad = fp2.read()
pfile, ptest = self.pfile, self.ptest
stru_check = pfile.parseFile(self.pbteciffile)
stru = ptest.parse(sgood)
self.assertEqual(str(stru_check), str(stru))
self.assertEqual(str(stru_check.lattice), str(stru.lattice))
self.assertEqual(pfile.spacegroup.short_name, ptest.spacegroup.short_name)
ptestb = P_cif()
self.assertRaises(StructureFormatError, ptestb.parse, sbad)
return
def test_parseLines(self):
"""Check P_cif.parseLines()"""
with open(self.pbteciffile) as fp1:
goodlines = fp1.readlines()
with open(self.badciffile) as fp2:
badlines = fp2.readlines()
pfile, ptest = self.pfile, self.ptest
stru_check = pfile.parseFile(self.pbteciffile)
stru = ptest.parseLines(goodlines)
self.assertEqual(str(stru_check), str(stru))
self.assertEqual(str(stru_check.lattice), str(stru.lattice))
self.assertEqual(pfile.spacegroup.short_name, ptest.spacegroup.short_name)
ptest2 = P_cif()
self.assertRaises(StructureFormatError, ptest2.parseLines, badlines)
return
def test_parseFile(self):
"""Check P_cif.parseFile()"""
# pbteciffile
stru = self.pfile.parseFile(self.pbteciffile)
self.assertEqual(8, len(stru))
self.assertEqual(6.461, stru.lattice.a)
self.assertEqual(6.461, stru.lattice.b)
self.assertEqual(6.461, stru.lattice.c)
self.assertEqual(90.0, stru.lattice.alpha)
self.assertEqual(90.0, stru.lattice.beta)
self.assertEqual(90.0, stru.lattice.gamma)
self.assertEqual("Fm-3m", self.pfile.spacegroup.short_name)
a0 = stru[0]
self.assertEqual(0.5, a0.x)
self.assertEqual(0.5, a0.y)
self.assertEqual(0.5, a0.z)
self.assertEqual(False, a0.anisotropy)
self.assertEqual(1.0, a0.occupancy)
self.assertEqual(0.0225566, a0.Uisoequiv)
# badciffile
pfile2 = P_cif()
self.assertRaises(StructureFormatError, pfile2.parseFile, self.badciffile)
# graphite
pgraphite = P_cif()
graphite = pgraphite.parseFile(self.graphiteciffile)
self.assertEqual(4, len(graphite))
c1 = graphite[0]
self.assertEqual(str, type(c1.element))
self.assertEqual("C", c1.element)
self.assertEqual(str, type(c1.label))
self.assertEqual("C1", c1.label)
# filename with unicode encoding
hasbs = "\\" in self.graphiteciffile
uciffile = self.graphiteciffile.replace("\\", "/")
if hasbs: # pragma: no cover
uciffile = uciffile.replace("/", "\\")
ugraphite = P_cif().parseFile(uciffile)
self.assertEqual(4, len(ugraphite))
# File with full space group name
ptei = P_cif().parseFile(self.teiciffile)
self.assertEqual(16, len(ptei))
return
# def test__parseCifBlock(self):
# """check P_cif._parseCifBlock()
# """
# return
#
# def test__parse_lattice(self):
# """check P_cif._parse_lattice()
# """
# return
#
# def test__parse_atom_site_label(self):
# """check P_cif._parse_atom_site_label()
# """
# return
#
# def test__parse_atom_site_aniso_label(self):
# """check P_cif._parse_atom_site_aniso_label()
# """
# return
#
# def test__parse_space_group_symop_operation_xyz(self):
# """check P_cif._parse_space_group_symop_operation_xyz()
# """
# return
#
# def test__expandAsymmetricUnit(self):
# """check P_cif._expandAsymmetricUnit()
# """
# return
#
# def test_toLines(self):
# """check P_cif.toLines()
# """
# return
#
# def test_tostring(self):
# """check P_cif.tostring()
# """
# return
def test_write_and_read(self):
"""High-level check of P_cif.tostring()"""
# high-level check
stru_check = Structure()
stru_check.read(self.cdsebulkpdffitfile)
s_s = stru_check.writeStr("cif")
stru = Structure()
stru.readStr(s_s, "cif")
self.assertAlmostEqual(4.2352, stru.lattice.a, self.places)
self.assertAlmostEqual(4.2352, stru.lattice.b, self.places)
self.assertAlmostEqual(6.90603, stru.lattice.c, self.places)
self.assertEqual(4, len(stru))
a0 = stru[0]
self.assertEqual("Cd", a0.element)
self.assertTrue(numpy.allclose([0.3334, 0.6667, 0.0], a0.xyz))
self.assertTrue(a0.anisotropy)
self.assertAlmostEqual(0.01303, a0.U[0, 0])
self.assertAlmostEqual(0.01303, a0.U[1, 1])
self.assertAlmostEqual(0.01402, a0.U[2, 2])
a3 = stru[3]
self.assertEqual("Se", a3.element)
self.assertTrue(numpy.allclose([0.6666, 0.333300, 0.87667], a3.xyz))
self.assertAlmostEqual(0.015673, a3.U[0, 0])
self.assertAlmostEqual(0.015673, a3.U[1, 1])
self.assertAlmostEqual(0.046164, a3.U[2, 2])
return
def test_write_structure_and_read_structure(self):
"""High-level check of P_cif.tostring()"""
# high-level check
stru_check = Structure()
stru_check.read(self.cdsebulkpdffitfile)
s_s = stru_check.write_structure("cif")
stru = Structure()
stru.read_structure(s_s, "cif")
self.assertAlmostEqual(4.2352, stru.lattice.a, self.places)
self.assertAlmostEqual(4.2352, stru.lattice.b, self.places)
self.assertAlmostEqual(6.90603, stru.lattice.c, self.places)
self.assertEqual(4, len(stru))
a0 = stru[0]
self.assertEqual("Cd", a0.element)
self.assertTrue(numpy.allclose([0.3334, 0.6667, 0.0], a0.xyz))
self.assertTrue(a0.anisotropy)
self.assertAlmostEqual(0.01303, a0.U[0, 0])
self.assertAlmostEqual(0.01303, a0.U[1, 1])
self.assertAlmostEqual(0.01402, a0.U[2, 2])
a3 = stru[3]
self.assertEqual("Se", a3.element)
self.assertTrue(numpy.allclose([0.6666, 0.333300, 0.87667], a3.xyz))
self.assertAlmostEqual(0.015673, a3.U[0, 0])
self.assertAlmostEqual(0.015673, a3.U[1, 1])
self.assertAlmostEqual(0.046164, a3.U[2, 2])
return
def test_eps(self):
"""Test the P_cif.eps coordinates resolution."""
pcif = P_cif()
pcif.eps = 1e-8
grph = pcif.parseFile(self.graphiteciffile)
self.assertEqual(8, len(grph))
self.assertTrue(all(a.label.startswith("C1") for a in grph[:2]))
self.assertTrue(all(a.label.startswith("C2") for a in grph[2:]))
pcif2 = P_cif()
pcif2.eps = 1e-3
grph2 = pcif2.parseFile(self.graphiteciffile)
self.assertEqual(4, len(grph2))
return
def test_unknown_occupancy(self):
"test CIF file with unknown occupancy data"
stru = self.ptest.parseFile(self.datafile("TeI-unkocc.cif"))
self.assertTrue(numpy.array_equal(16 * [1], stru.occupancy))
return
def test_unknown_spacegroup_number(self):
"test CIF file with unknown space group symbol"
from diffpy.structure.spacegroups import GetSpaceGroup, _hashSymOpList
with open(self.pbteciffile) as fp:
lines = fp.readlines()
self.assertTrue(lines[16].startswith("_symmetry_space_group"))
self.assertTrue(lines[17].startswith("_symmetry_space_group"))
lines[16:18] = [
"_space_group_IT_number ?\n",
"_symmetry_space_group_name_H-M ?\n",
"_symmetry_space_group_name_Hall ?\n",
]
ciftxt = "".join(lines)
stru = self.ptest.parse(ciftxt)
self.assertEqual(8, len(stru))
h225 = _hashSymOpList(GetSpaceGroup(225).iter_symops())
sgcif = self.ptest.spacegroup
self.assertEqual(h225, _hashSymOpList(sgcif.iter_symops()))
return
def test_nosites_cif(self):
"""Test reading of CIF file with no valid sites."""
ptest = self.ptest
stru = ptest.parseFile(self.datafile("nosites.cif"))
self.assertEqual(0, len(stru))
self.assertEqual(10.413, stru.lattice.a)
self.assertEqual(10.413, stru.lattice.b)
self.assertEqual(10.413, stru.lattice.c)
return
def test_badspacegroup_cif(self):
"""Test reading of CIF file with unrecognized space group."""
ptest = self.ptest
filename = self.datafile("badspacegroup.cif")
self.assertRaises(StructureFormatError, ptest.parseFile, filename)
return
def test_custom_spacegroup_cif(self):
"""Test parsing of nonstandard symops-defined space group."""
pfile = self.pfile
filename = self.datafile("customsg.cif")
pfile.parseFile(filename)
sg = pfile.spacegroup
self.assertEqual("CIF data", sg.short_name)
self.assertEqual(6, len(sg.symop_list))
return
def test_spacegroup_isotropy(self):
"verify site isotropy due to site symmetry."
# remove the _atom_site_thermal_displace_type field
with open(self.pbteciffile) as fp:
lines = [line.replace(" Uiso ", " ") for line in fp if "_atom_site_thermal_displace_type" not in line]
ciftxt = "".join(lines)
ptest = self.ptest
stru = ptest.parse(ciftxt)
self.assertFalse(any(stru.anisotropy))
self.assertTrue(all(not a.anisotropy for a in ptest.asymmetric_unit))
return
def test_spacegroup_anisotropy(self):
"verify site anisotropy due to site symmetry."
stru = self.ptest.parseFile(self.graphiteciffile)
self.assertTrue(all(stru.anisotropy))
return
def test_spacegroup_ref(self):
"verify space group reference"
pfile = self.pfile
pfile.parseFile(self.refciffile)
sg = pfile.spacegroup
self.assertEqual("Fm-3m", sg.short_name)
return
def test_adp_type_ani(self):
"verify adp type override to anisotropic"
with open(self.pbteciffile) as fp:
ciftxt = fp.read()
ciftxt = ciftxt.replace(" Uiso ", " Uani ")
stru = self.ptest.parse(ciftxt)
self.assertTrue(all(stru.anisotropy))
return
def test_adp_type_iso(self):
"verify adp type override to isotropic"
with open(self.graphiteciffile) as fp:
lines = fp.readlines()
lines.insert(-2, "_atom_site_adp_type\n")
lines[-2] = lines[-2].rstrip() + " Uiso\n"
lines[-1] = lines[-1].rstrip() + " Uiso\n"
ciftxt = "".join(lines)
stru = self.ptest.parse(ciftxt)
self.assertFalse(any(a.anisotropy for a in stru))
return
def test_adp_aniso_label(self):
"verify ADP type setting from _atom_site_aniso_label loop"
with open(self.teiciffile) as fp:
lines = [line.replace(" Uani ", " ") for line in fp if "_atom_site_adp_type" not in line]
ciftxt = "".join(lines)
stru = self.ptest.parse(ciftxt)
self.assertTrue(all(stru.anisotropy))
return
def test_unknown_aniso(self):
"test CIF file with unknown values in the aniso block."
with open(self.teiciffile) as fp:
lines = fp.readlines()
lines[-4:] = [" ? ? ? ? ? ? ?\n"]
ciftxt = "".join(lines)
stru = self.ptest.parse(ciftxt)
self.assertEqual(16, len(stru))
self.assertTrue(all(stru.anisotropy))
return
def test_curly_brace(self):
"verify loading of a CIF file with unquoted curly brace"
ptest = self.ptest
stru = ptest.parseFile(self.datafile("curlybrackets.cif"))
self.assertEqual(20, len(stru))
return
def test_getParser(self):
"""Test passing of eps keyword argument by getParser
function."""
pcif = getParser("cif", eps=1e-6)
grph = pcif.parseFile(self.graphiteciffile)
self.assertEqual(8, len(grph))
self.assertTrue(all(a.label.startswith("C1") for a in grph[:2]))
self.assertTrue(all(a.label.startswith("C2") for a in grph[2:]))
pcif2 = getParser("cif")
grph2 = pcif2.parseFile(self.graphiteciffile)
self.assertEqual(4, len(grph2))
return
# End of class TestP_cif
# ----------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()