forked from diffpy/diffpy.srreal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_debyepdfcalculator.py
More file actions
284 lines (254 loc) · 9.3 KB
/
test_debyepdfcalculator.py
File metadata and controls
284 lines (254 loc) · 9.3 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
#!/usr/bin/env python
"""Unit tests for pdfcalculator.py."""
import pickle
import unittest
import warnings
import numpy
from testutils import _maxNormDiff, loadDiffPyStructure, pickle_with_attr
from diffpy.srreal.pdfcalculator import DebyePDFCalculator, PDFCalculator
from diffpy.srreal.scatteringfactortable import SFTNeutron
##############################################################################
class TestDebyePDFCalculator(unittest.TestCase):
bucky = None
tio2rutile = None
def setUp(self):
self.dpdfc = DebyePDFCalculator()
if not TestDebyePDFCalculator.bucky:
TestDebyePDFCalculator.bucky = loadDiffPyStructure("C60bucky.stru")
if not TestDebyePDFCalculator.tio2rutile:
TestDebyePDFCalculator.tio2rutile = loadDiffPyStructure(
"TiO2_rutile-fit.stru"
)
return
# def tearDown(self):
# return
#
# def test___call__(self):
# """check DebyePDFCalculator.__call__()
# """
# return
#
# def test___init__(self):
# """check DebyePDFCalculator.__init__()
# """
# return
def test___getattr__(self):
"""Check DebyePDFCalculator.__getattr__()"""
self.assertEqual(0.0, self.dpdfc.qmin)
self.dpdfc._setDoubleAttr("qmin", 1.23)
self.assertEqual(1.23, self.dpdfc.qmin)
return
def test___setattr__(self):
"""Check DebyePDFCalculator.__setattr__()"""
self.assertNotEqual(1.23, self.dpdfc._getDoubleAttr("rmin"))
self.dpdfc.rmin = 1.23
self.assertEqual(1.23, self.dpdfc._getDoubleAttr("rmin"))
return
def test__getDoubleAttr(self):
"""Check DebyePDFCalculator._getDoubleAttr()"""
gdba = self.dpdfc._getDoubleAttr
self.assertEqual(1.0, gdba("scale"))
self.assertEqual(0.0, gdba("qdamp"))
self.assertRaises(Exception, gdba, "notanattribute")
return
def test__hasDoubleAttr(self):
"""Check DebyePDFCalculator._hasDoubleAttr()"""
self.assertTrue(self.dpdfc._hasDoubleAttr("scale"))
self.assertFalse(self.dpdfc._hasDoubleAttr("notanattribute"))
return
def test__namesOfDoubleAttributes(self):
"""Check DebyePDFCalculator._namesOfDoubleAttributes()"""
self.assertTrue(type(self.dpdfc._namesOfDoubleAttributes()) is set)
self.assertTrue("qmax" in self.dpdfc._namesOfDoubleAttributes())
return
def test__setDoubleAttr(self):
"""Check DebyePDFCalculator._setDoubleAttr()"""
gdba = self.dpdfc._getDoubleAttr
sdba = self.dpdfc._setDoubleAttr
self.assertEqual(0.0, gdba("rmin"))
sdba("rmin", 3.0)
self.assertEqual(3.0, gdba("rmin"))
return
def test_PDF_C60bucky(self):
"""Check DebyePDFCalculator.pdf for C60 Bucky ball."""
qmax = self.dpdfc.qmax
r0, g0 = PDFCalculator(qmax=qmax)(self.bucky)
r1, g1 = self.dpdfc(self.bucky)
mxnd = _maxNormDiff(g0, g1)
self.assertTrue(mxnd < 0.0006)
return
def test_partial_pdfs(self):
"""Check calculation of partial PDFs."""
dpdfc = self.dpdfc
dpdfc.qmin = 1.0
rutile = self.tio2rutile
r0, g0 = dpdfc(rutile)
# Ti-Ti
dpdfc.maskAllPairs(False)
dpdfc.setTypeMask("Ti", "Ti", True)
r1, g1 = dpdfc(rutile)
self.assertTrue(numpy.array_equal(r0, r1))
dpdfc.invertMask()
r1i, g1i = dpdfc(rutile)
self.assertTrue(numpy.array_equal(r0, r1i))
self.assertTrue(numpy.allclose(g0, g1 + g1i))
# Ti-O
dpdfc.maskAllPairs(False)
dpdfc.setTypeMask("all", "ALL", True)
dpdfc.setTypeMask("Ti", "Ti", False)
dpdfc.setTypeMask("O", "O", False)
r2, g2 = dpdfc(rutile)
self.assertTrue(numpy.array_equal(r0, r2))
dpdfc.invertMask()
r2i, g2i = dpdfc(rutile)
self.assertTrue(numpy.allclose(g0, g2 + g2i))
# Ti-O from type mask
dpdfc.maskAllPairs(True)
dpdfc.setTypeMask("Ti", "Ti", False)
dpdfc.setTypeMask("O", "O", False)
r2t, g2t = dpdfc(rutile)
self.assertTrue(numpy.array_equal(r0, r2t))
self.assertTrue(numpy.array_equal(g2, g2t))
dpdfc.invertMask()
r2ti, g2ti = dpdfc(rutile)
self.assertTrue(numpy.array_equal(g2i, g2ti))
# O-O
dpdfc.maskAllPairs(False)
dpdfc.setTypeMask("O", "O", True)
r3, g3 = dpdfc(rutile)
dpdfc.invertMask()
r3i, g3i = dpdfc(rutile)
self.assertTrue(numpy.allclose(g0, g3 + g3i))
# check the sum of all partials
self.assertTrue(numpy.allclose(g0, g1 + g2 + g3))
return
def test_pickling(self):
"""Check pickling and unpickling of PDFCalculator."""
# New syntax: assign an SFT instance to the property (should not warn)
dpdfc = self.dpdfc
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
dpdfc.scatteringfactortable = SFTNeutron()
self.assertFalse(
any(isinstance(x.message, DeprecationWarning) for x in w)
)
dpdfc.scatteringfactortable.setCustomAs("Na", "Na", 7)
spkl = pickle.dumps(dpdfc)
dpdfc1_new = pickle.loads(spkl)
self.assertEqual(
dpdfc.scatteringfactortable.type(),
dpdfc1_new.scatteringfactortable.type(),
)
self.assertEqual(7.0, dpdfc1_new.scatteringfactortable.lookup("Na"))
# Old syntax: call the deprecated method (should warn)
dpdfc = self.dpdfc
with self.assertWarns(DeprecationWarning):
dpdfc.setScatteringFactorTableByType("N")
dpdfc.scatteringfactortable.setCustomAs("Na", "Na", 7)
dpdfc.addEnvelope("sphericalshape")
dpdfc.debyeprecision = 0.001
dpdfc.delta1 = 0.2
dpdfc.delta2 = 0.3
dpdfc.maxextension = 10.1
dpdfc.qbroad = 0.01
dpdfc.qdamp = 0.05
dpdfc.qmax = 10
dpdfc.qmin = 0.5
dpdfc.rmax = 10.0
dpdfc.rmin = 0.02
dpdfc.rstep = 0.02
dpdfc.scale = 1.1
dpdfc.spdiameter = 13.3
spkl = pickle.dumps(dpdfc)
dpdfc1 = pickle.loads(spkl)
self.assertFalse(dpdfc is dpdfc1)
sft = dpdfc.scatteringfactortable
sft1 = dpdfc1.scatteringfactortable
self.assertEqual(sft.type(), sft1.type())
self.assertEqual(7.0, sft1.lookup("Na"))
for a in dpdfc._namesOfDoubleAttributes():
self.assertEqual(getattr(dpdfc, a), getattr(dpdfc1, a))
self.assertEqual(13.3, dpdfc1.getEnvelope("sphericalshape").spdiameter)
self.assertEqual(
dpdfc._namesOfDoubleAttributes(), dpdfc1._namesOfDoubleAttributes()
)
self.assertEqual(dpdfc.usedenvelopetypes, dpdfc1.usedenvelopetypes)
self.assertRaises(RuntimeError, pickle_with_attr, dpdfc, foo="bar")
return
def test_mask_pickling(self):
"""Check if mask gets properly pickled and restored."""
self.dpdfc.maskAllPairs(False)
self.dpdfc.setPairMask(0, 1, True)
self.dpdfc.setTypeMask("Na", "Cl", True)
self.assertTrue(False is self.dpdfc.getPairMask(0, 0))
self.assertTrue(True is self.dpdfc.getPairMask(0, 1))
self.assertTrue(True is self.dpdfc.getTypeMask("Cl", "Na"))
dpdfc1 = pickle.loads(pickle.dumps(self.dpdfc))
self.assertTrue(False is dpdfc1.getPairMask(0, 0))
self.assertTrue(True is dpdfc1.getPairMask(0, 1))
self.assertTrue(True is self.dpdfc.getTypeMask("Cl", "Na"))
return
def test_pickling_derived_structure(self):
"""Check pickling of DebyePDFCalculator with
DerivedStructureAdapter."""
from testutils import DerivedStructureAdapter
dpdfc = self.dpdfc
stru0 = DerivedStructureAdapter()
dpdfc.setStructure(stru0)
self.assertEqual(1, stru0.cpqcount)
spkl = pickle.dumps(dpdfc)
dpdfc1 = pickle.loads(spkl)
self.assertTrue(stru0 is dpdfc.getStructure())
stru1 = dpdfc1.getStructure()
self.assertTrue(type(stru1) is DerivedStructureAdapter)
self.assertFalse(stru1 is stru0)
self.assertEqual(1, stru1.cpqcount)
return
# def test_getPeakWidthModel(self):
# """check DebyePDFCalculator.getPeakWidthModel()
# """
# return
#
# def test_qgrid(self):
# """check DebyePDFCalculator.qgrid
# """
# return
#
# def test_getRadiationType(self):
# """check DebyePDFCalculator.getRadiationType()
# """
# return
#
# def test_rgrid(self):
# """check DebyePDFCalculator.rgrid
# """
# return
#
# def test_scatteringfactortable(self):
# """check DebyePDFCalculator.scatteringfactortable property
# """
# return
#
# def test_isOptimumQstep(self):
# """check DebyePDFCalculator.isOptimumQstep()
# """
# return
#
# def test_setOptimumQstep(self):
# """check DebyePDFCalculator.setOptimumQstep()
# """
# return
#
# def test_peakwidthmodel(self):
# """check DebyePDFCalculator.setPeakWidthModel()
# """
# return
#
# def test_value(self):
# """check DebyePDFCalculator.value
# """
# return
# End of class TestDebyePDFCalculator
if __name__ == "__main__":
unittest.main()
# End of file