forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_profile.py
229 lines (195 loc) · 8.25 KB
/
test_profile.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
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
#!/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 refinableobj module."""
import io
import re
import unittest
from numpy import allclose, arange, array, array_equal, ones_like
from utils import datafile
from diffpy.srfit.exceptions import SrFitError
from diffpy.srfit.fitbase.profile import Profile
class TestProfile(unittest.TestCase):
def setUp(self):
self.profile = Profile()
return
def testInit(self):
profile = self.profile
self.assertTrue(profile.xobs is None)
self.assertTrue(profile.yobs is None)
self.assertTrue(profile.dyobs is None)
self.assertTrue(profile.x is None)
self.assertTrue(profile.y is None)
self.assertTrue(profile.dy is None)
self.assertTrue(profile.ycalc is None)
self.assertEqual(profile.meta, {})
return
def testSetObservedProfile(self):
"""Test the setObservedProfile method."""
# Make a profile with defined dy
x = arange(0, 10, 0.1)
y = x
dy = x
prof = self.profile
prof.setObservedProfile(x, y, dy)
self.assertTrue(array_equal(x, prof.xobs))
self.assertTrue(array_equal(y, prof.yobs))
self.assertTrue(array_equal(dy, prof.dyobs))
# Make a profile with undefined dy
x = arange(0, 10, 0.1)
y = x
dy = None
self.profile.setObservedProfile(x, y, dy)
self.assertTrue(array_equal(x, prof.xobs))
self.assertTrue(array_equal(y, prof.yobs))
self.assertTrue(array_equal(ones_like(prof.xobs), prof.dyobs))
# Get the ranged profile to make sure its the same
self.assertTrue(array_equal(x, prof.x))
self.assertTrue(array_equal(y, prof.y))
self.assertTrue(array_equal(ones_like(prof.xobs), prof.dy))
return
def testSetCalculationRange(self):
"""Test the setCalculationRange method."""
x = arange(2, 9.6, 0.5)
y = array(x)
dy = array(x)
prof = self.profile
# Check call before data arrays are present
self.assertRaises(AttributeError, prof.setCalculationRange)
self.assertRaises(AttributeError, prof.setCalculationRange, 0)
self.assertRaises(AttributeError, prof.setCalculationRange, 0, 5)
self.assertRaises(AttributeError, prof.setCalculationRange, 0, 5, 0.2)
# assign data
prof.setObservedProfile(x, y, dy)
# Test normal execution w/o arguments
self.assertTrue(array_equal(x, prof.x))
self.assertTrue(array_equal(y, prof.y))
self.assertTrue(array_equal(dy, prof.dy))
prof.setCalculationRange()
self.assertTrue(array_equal(x, prof.x))
self.assertTrue(array_equal(y, prof.y))
self.assertTrue(array_equal(dy, prof.dy))
# Test a lower bound < xmin
prof.setCalculationRange(xmin=0)
self.assertTrue(array_equal(x, prof.x))
self.assertTrue(array_equal(y, prof.y))
self.assertTrue(array_equal(dy, prof.dy))
# Test an upper bound > xmax
prof.setCalculationRange(xmax=100)
self.assertTrue(array_equal(x, prof.x))
self.assertTrue(array_equal(y, prof.y))
self.assertTrue(array_equal(dy, prof.dy))
# Test xmin > xmax
self.assertRaises(ValueError, prof.setCalculationRange, xmin=10, xmax=3)
# Test xmax - xmin < dx
self.assertRaises(ValueError, prof.setCalculationRange, xmin=3, xmax=3.9, dx=1.0)
# Test dx <= 0
self.assertRaises(ValueError, prof.setCalculationRange, dx=0)
self.assertRaises(ValueError, prof.setCalculationRange, dx=-0.000001)
# using string other than 'obs'
self.assertRaises(ValueError, prof.setCalculationRange, xmin="oobs")
self.assertRaises(ValueError, prof.setCalculationRange, xmax="oobs")
self.assertRaises(ValueError, prof.setCalculationRange, dx="oobs")
# This should be alright
prof.setCalculationRange(3, 5)
prof.setCalculationRange(xmin="obs", xmax=7, dx=0.001)
self.assertEqual(5001, len(prof.x))
self.assertEqual(len(prof.x), len(prof.y))
self.assertEqual(len(prof.x), len(prof.dy))
# Test an internal bound
prof.setCalculationRange(4, 7, dx="obs")
self.assertTrue(array_equal(prof.x, arange(4, 7.1, 0.5)))
self.assertTrue(array_equal(prof.y, arange(4, 7.1, 0.5)))
self.assertTrue(array_equal(prof.y, arange(4, 7.1, 0.5)))
# test setting only one of the bounds
prof.setCalculationRange(xmin=3)
self.assertTrue(array_equal(prof.x, arange(3, 7.1, 0.5)))
self.assertTrue(array_equal(prof.x, prof.y))
self.assertTrue(array_equal(prof.x, prof.dy))
prof.setCalculationRange(xmax=5.1)
self.assertTrue(array_equal(prof.x, arange(3, 5.1, 0.5)))
self.assertTrue(array_equal(prof.x, prof.y))
self.assertTrue(array_equal(prof.x, prof.dy))
prof.setCalculationRange(dx=1)
self.assertTrue(array_equal(prof.x, arange(3, 5.1)))
self.assertTrue(array_equal(prof.x, prof.y))
self.assertTrue(array_equal(prof.x, prof.dy))
# Test a new grid
prof.setCalculationRange(4.2, 7, 0.3)
self.assertTrue(array_equal(prof.x, arange(4.2, 6.901, 0.3)))
self.assertTrue(allclose(prof.x, prof.y))
self.assertTrue(allclose(prof.x, prof.dy))
prof.setCalculationRange(xmin=4.2, xmax=6.001)
self.assertTrue(array_equal(prof.x, arange(4.2, 6.001, 0.3)))
# resample on a clipped grid
prof.setCalculationRange(dx=0.5)
self.assertTrue(array_equal(prof.x, arange(4.5, 6.1, 0.5)))
return
def testSetCalculationPoints(self):
"""Test the setCalculationPoints method."""
prof = self.profile
x = arange(2, 10.5, 0.5)
y = array(x)
dy = array(x)
# Test without data
xcalc = arange(3, 12.2, 0.2)
prof.setCalculationPoints(xcalc)
self.assertTrue(array_equal(xcalc, prof.x))
# Add the data. This should change the bounds of the calculation array.
prof.setObservedProfile(x, y, dy)
self.assertTrue(array_equal(arange(3, 10.1, 0.2), prof.x))
return
def testLoadtxt(self):
"""Test the loadtxt method"""
prof = self.profile
data = datafile("testdata.txt")
def _test(p):
self.assertAlmostEqual(1e-2, p.x[0])
self.assertAlmostEqual(1.105784e-1, p.y[0])
self.assertAlmostEqual(1.802192e-3, p.dy[0])
# Test normal load
prof.loadtxt(data, usecols=(0, 1, 3))
_test(prof)
# Test trying to not set unpack
prof.loadtxt(data, usecols=(0, 1, 3), unpack=False)
_test(prof)
prof.loadtxt(data, float, "#", None, None, 0, (0, 1, 3), False)
_test(prof)
# Try not including dy
prof.loadtxt(data, usecols=(0, 1))
self.assertAlmostEqual(1e-2, prof.x[0])
self.assertAlmostEqual(1.105784e-1, prof.y[0])
self.assertAlmostEqual(1, prof.dy[0])
# Try to include too little
self.assertRaises(ValueError, prof.loadtxt, data, usecols=(0,))
return
def test_savetxt(self):
"Check the savetxt method."
prof = self.profile
self.assertRaises(SrFitError, prof.savetxt, "foo")
xobs = arange(-2, 3.01, 0.25)
yobs = xobs**2
prof.setObservedProfile(xobs, yobs)
prof.ycalc = yobs.copy()
fp = io.BytesIO()
prof.savetxt(fp)
txt = fp.getvalue().decode()
self.assertTrue(re.match(r"^# x +ycalc +y +dy\b", txt))
nlines = len(txt.strip().split("\n"))
self.assertEqual(22, nlines)
return
# End of class TestProfile
# ----------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()