forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_profilegenerator.py
91 lines (74 loc) · 2.69 KB
/
test_profilegenerator.py
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
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 The Trustees of Columbia University
# in the City of New York. 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.
#
##############################################################################
"""Tests for profilegenerator module."""
import pickle
import unittest
from numpy import arange, array_equal
from diffpy.srfit.fitbase.profile import Profile
from diffpy.srfit.fitbase.profilegenerator import ProfileGenerator
class TestProfileGenerator(unittest.TestCase):
def setUp(self):
self.gen = ProfileGenerator("test")
self.profile = Profile()
x = arange(0, 10, 0.1)
self.profile.setCalculationPoints(x)
self.gen.setProfile(self.profile)
return
def testOperation(self):
"""Test the operation method."""
gen = self.gen
prof = self.profile
# Try the direct evaluation
val = gen.operation()
self.assertTrue(array_equal(prof.x, val))
# Try evaluation through __call__
val = gen(2 * prof.x)
self.assertTrue(array_equal(2 * prof.x, val))
return
def testUpdate(self):
"""Update and change the profile to make sure generator is flushed."""
gen = self.gen
prof = self.profile
# Make sure attributes get updated with a change in the calculation
# points.
x = arange(0, 9, 0.1)
prof.setCalculationPoints(x)
self.assertTrue(gen._value is None)
val = gen.value
self.assertTrue(array_equal(x, val))
# Verify generated value listens to changes in profile.x.
x3 = x + 3
prof.x = x3
self.assertTrue(array_equal(x3, gen.value))
# Make sure attributes get updated with a new profile.
x = arange(0, 8, 0.1)
prof = Profile()
prof.setCalculationPoints(x)
gen.setProfile(prof)
self.assertTrue(gen._value is None)
self.assertTrue(array_equal(x, gen.value))
return
def test_pickling(self):
"""Test pickling of ProfileGenerator."""
data = pickle.dumps(self.gen)
gen2 = pickle.loads(data)
self.assertEqual("test", gen2.name)
x = self.profile.x
self.assertTrue(array_equal(x, gen2.operation()))
self.assertTrue(array_equal(3 * x, gen2(3 * x)))
return
# End of class TestProfileGenerator
if __name__ == "__main__":
unittest.main()