forked from diffpy/diffpy.structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_p_discus.py
More file actions
149 lines (132 loc) · 5.34 KB
/
test_p_discus.py
File metadata and controls
149 lines (132 loc) · 5.34 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
#!/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
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Unit tests for diffpy.structure.parsers.p_discus module."""
import re
import unittest
import pytest
from diffpy.structure import Structure
from diffpy.structure.parsers import StructureFormatError
# ----------------------------------------------------------------------------
class TestP_discus(unittest.TestCase):
"""Test Parser for PDFFit file format."""
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.datafile = datafile
def setUp(self):
self.stru = Structure()
self.format = "discus"
self.places = 8
def test_read_discus_Ni(self):
"""Check reading of Ni structure in discus format."""
stru = self.stru
stru.read(self.datafile("Ni-discus.stru"), self.format)
f_title = "structure Ni FCC"
self.assertEqual(f_title, stru.title)
self.assertEqual("Fm-3m", stru.pdffit["spcgr"])
# cell record
abcABG = (3.52, 3.52, 3.52, 90.0, 90.0, 90.0)
self.assertEqual(abcABG, stru.lattice.abcABG())
# ncell
self.assertEqual([1, 1, 1, 4], stru.pdffit["ncell"])
self.assertEqual(4, len(stru))
# first atom
a0 = stru[0]
self.assertEqual((0.0, 0.0, 0.0), tuple(a0.xyz))
self.assertTrue(not a0.anisotropy)
Biso0 = 0.1
self.assertAlmostEqual(Biso0, a0.Bisoequiv, self.places)
return
def test_except_other_formats(self):
"""Check exceptions when reading files in other formats."""
badfiles = [
"LiCl-bad.cif",
"PbTe.cif",
"arginine.pdb",
"ZnSb_RT_Q28X_VM_20_fxiso.rstr",
"Ni-bad.stru",
"Ni.stru",
"BubbleRaftShort.xcfg",
"bucky-bad1.xyz",
"bucky-bad2.xyz",
"bucky-plain-bad.xyz",
"bucky-plain.xyz",
"bucky-raw.xyz",
"bucky.xyz",
"hexagon-raw-bad.xyz",
"hexagon-raw.xyz",
]
for ft in badfiles:
ff = self.datafile(ft)
self.assertRaises(StructureFormatError, self.stru.read, ff, format=self.format)
return
def test_ignored_lines(self):
"""Check skipping of ignored lines in the header."""
r1 = "ignored record 1\n"
r2 = "ignored record 2\n"
with open(self.datafile("Ni-discus.stru")) as fp:
ni_lines = fp.readlines()
ni_lines.insert(2, r1)
ni_lines.insert(4, r2)
s_s1 = "".join(ni_lines)
p = self.stru.read_structure(s_s1, self.format)
self.assertEqual([r1.rstrip(), r2.rstrip()], p.ignored_lines)
ni_lines.append(r1)
s_s2 = "".join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.read_structure, s_s2, self.format)
return
def test_spdiameter_parsing(self):
"""Check parsing of spdiameter record from a file."""
stru = self.stru
stru.read(self.datafile("Ni-discus.stru"), self.format)
self.assertEqual(0, stru.pdffit["spdiameter"])
snoshape = stru.write_structure(format=self.format)
self.assertTrue(not re.search("(?m)^shape", snoshape))
# produce a string with non-zero spdiameter
stru.pdffit["spdiameter"] = 13
s13 = stru.write_structure(format=self.format)
self.assertTrue(re.search("(?m)^shape +sphere, ", s13))
stru13 = Structure()
stru13.read_structure(s13)
self.assertEqual(13, stru13.pdffit["spdiameter"])
with open(self.datafile("Ni.stru")) as fp:
ni_lines = fp.readlines()
ni_lines.insert(3, "shape invalid, 7\n")
sbad = "".join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.read_structure, sbad, format=self.format)
return
def test_stepcut_parsing(self):
"""Check parsing of stepcut record from a file."""
stru = self.stru
stru.read(self.datafile("Ni-discus.stru"), self.format)
self.assertEqual(0, stru.pdffit["stepcut"])
snoshape = stru.write_structure(format=self.format)
self.assertTrue(not re.search("(?m)^shape", snoshape))
# produce a string with non-zero stepcut
stru.pdffit["stepcut"] = 13
s13 = stru.write_structure(format=self.format)
self.assertTrue(re.search("(?m)^shape +stepcut, ", s13))
stru13 = Structure()
stru13.read_structure(s13)
self.assertEqual(13, stru13.pdffit["stepcut"])
with open(self.datafile("Ni.stru")) as fp:
ni_lines = fp.readlines()
ni_lines.insert(3, "shape invalid, 7\n")
sbad = "".join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.read_structure, sbad, format=self.format)
return
# End of class TestP_discus
# ----------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()