|
16 | 16 | """Tests for pdf package.""" |
17 | 17 |
|
18 | 18 | import unittest |
| 19 | +import pickle |
19 | 20 | import io |
20 | 21 |
|
21 | 22 | import numpy |
@@ -248,6 +249,32 @@ def test_savetxt(self): |
248 | 249 | self.assertEqual(1001, nlines) |
249 | 250 | return |
250 | 251 |
|
| 252 | + |
| 253 | + def test_pickling(self): |
| 254 | + "validate PDFContribution.residual() after pickling." |
| 255 | + from itertools import chain |
| 256 | + from diffpy.structure import loadStructure |
| 257 | + pc = self.pc |
| 258 | + pc.loadData(datafile("ni-q27r100-neutron.gr")) |
| 259 | + ni = loadStructure(datafile("ni.cif")) |
| 260 | + ni.Uisoequiv = 0.003 |
| 261 | + pc.addStructure('ni', ni) |
| 262 | + pc.setCalculationRange(0, 10) |
| 263 | + pc2 = pickle.loads(pickle.dumps(pc)) |
| 264 | + res0 = pc.residual() |
| 265 | + self.assertTrue(numpy.array_equal(res0, pc2.residual())) |
| 266 | + for p in chain(pc.iterPars('Uiso'), pc2.iterPars('Uiso')): |
| 267 | + p.value = 0.004 |
| 268 | + res1 = pc.residual() |
| 269 | + self.assertFalse(numpy.allclose(res0, res1)) |
| 270 | + self.assertTrue(numpy.array_equal(res1, pc2.residual())) |
| 271 | + return |
| 272 | + |
| 273 | + # TODO: remove after fixup release of diffpy.structure |
| 274 | + import sys |
| 275 | + if sys.version_info >= (3, 7): |
| 276 | + test_pickling = unittest.expectedFailure(test_pickling) |
| 277 | + |
251 | 278 | # End of class TestPDFContribution |
252 | 279 |
|
253 | 280 | # ---------------------------------------------------------------------------- |
|
0 commit comments