1 | """ |
---|
2 | Unit tests for data manipulations |
---|
3 | """ |
---|
4 | #TODO: what happens if you add a Data1D to a Data2D? |
---|
5 | |
---|
6 | import unittest |
---|
7 | import numpy, math |
---|
8 | from sans.dataloader.loader import Loader |
---|
9 | from sans.guiframe.dataFitting import Data1D, Data2D |
---|
10 | from sans.guiframe.dataFitting import Data1D as Theory1D |
---|
11 | |
---|
12 | import os.path |
---|
13 | |
---|
14 | class data_info_tests(unittest.TestCase): |
---|
15 | |
---|
16 | def setUp(self): |
---|
17 | self.data = Loader().load("cansas1d.xml") |
---|
18 | |
---|
19 | def test_clone1D(self): |
---|
20 | """ |
---|
21 | Test basic cloning |
---|
22 | """ |
---|
23 | clone = self.data.clone_without_data() |
---|
24 | |
---|
25 | for i in range(len(self.data.detector)): |
---|
26 | self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance) |
---|
27 | |
---|
28 | class theory1d_tests(unittest.TestCase): |
---|
29 | |
---|
30 | def setUp(self): |
---|
31 | self.data = Loader().load("cansas1d.xml") |
---|
32 | |
---|
33 | def test_clone_theory1D(self): |
---|
34 | """ |
---|
35 | Test basic cloning |
---|
36 | """ |
---|
37 | theory = Theory1D(x=[], y=[], dy=None) |
---|
38 | theory.clone_without_data(clone=self.data) |
---|
39 | theory.copy_from_datainfo(data1d=self.data) |
---|
40 | for i in range(len(self.data.detector)): |
---|
41 | self.assertEqual(self.data.detector[i].distance, theory.detector[i].distance) |
---|
42 | |
---|
43 | for i in range(len(self.data.x)): |
---|
44 | self.assertEqual(self.data.x[i], theory.x[i]) |
---|
45 | self.assertEqual(self.data.y[i], theory.y[i]) |
---|
46 | self.assertEqual(self.data.dy[i], theory.dy[i]) |
---|
47 | |
---|
48 | class manip_tests(unittest.TestCase): |
---|
49 | |
---|
50 | def setUp(self): |
---|
51 | # Create two data sets to play with |
---|
52 | x_0 = numpy.ones(5) |
---|
53 | for i in range(5): |
---|
54 | x_0[i] = x_0[i]*(i+1.0) |
---|
55 | |
---|
56 | y_0 = 2.0*numpy.ones(5) |
---|
57 | dy_0 = 0.5*numpy.ones(5) |
---|
58 | self.data = Data1D(x_0, y_0, dy=dy_0) |
---|
59 | |
---|
60 | x = self.data.x |
---|
61 | y = numpy.ones(5) |
---|
62 | dy = numpy.ones(5) |
---|
63 | self.data2 = Data1D(x, y, dy=dy) |
---|
64 | |
---|
65 | |
---|
66 | def test_load(self): |
---|
67 | """ |
---|
68 | Test whether the test file was loaded properly |
---|
69 | """ |
---|
70 | # There should be 5 entries in the file |
---|
71 | self.assertEqual(len(self.data.x), 5) |
---|
72 | |
---|
73 | for i in range(5): |
---|
74 | # The x values should be from 1 to 5 |
---|
75 | self.assertEqual(self.data.x[i], float(i+1)) |
---|
76 | |
---|
77 | # All y-error values should be 0.5 |
---|
78 | self.assertEqual(self.data.dy[i], 0.5) |
---|
79 | |
---|
80 | # All y values should be 2.0 |
---|
81 | self.assertEqual(self.data.y[i], 2.0) |
---|
82 | |
---|
83 | def test_add(self): |
---|
84 | result = self.data2+self.data |
---|
85 | for i in range(5): |
---|
86 | self.assertEqual(result.y[i], 3.0) |
---|
87 | self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) |
---|
88 | |
---|
89 | def test_sub(self): |
---|
90 | result = self.data2-self.data |
---|
91 | for i in range(5): |
---|
92 | self.assertEqual(result.y[i], -1.0) |
---|
93 | self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) |
---|
94 | |
---|
95 | def test_mul(self): |
---|
96 | result = self.data2*self.data |
---|
97 | for i in range(5): |
---|
98 | self.assertEqual(result.y[i], 2.0) |
---|
99 | self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
100 | |
---|
101 | def test_div(self): |
---|
102 | result = self.data2/self.data |
---|
103 | for i in range(5): |
---|
104 | self.assertEqual(result.y[i], 0.5) |
---|
105 | self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
106 | |
---|
107 | def test_radd(self): |
---|
108 | result = self.data+3.0 |
---|
109 | for i in range(5): |
---|
110 | self.assertEqual(result.y[i], 5.0) |
---|
111 | self.assertEqual(result.dy[i], 0.5) |
---|
112 | |
---|
113 | result = 3.0+self.data |
---|
114 | for i in range(5): |
---|
115 | self.assertEqual(result.y[i], 5.0) |
---|
116 | self.assertEqual(result.dy[i], 0.5) |
---|
117 | |
---|
118 | def test_rsub(self): |
---|
119 | result = self.data-3.0 |
---|
120 | for i in range(5): |
---|
121 | self.assertEqual(result.y[i], -1.0) |
---|
122 | self.assertEqual(result.dy[i], 0.5) |
---|
123 | |
---|
124 | result = 3.0-self.data |
---|
125 | for i in range(5): |
---|
126 | self.assertEqual(result.y[i], 1.0) |
---|
127 | self.assertEqual(result.dy[i], 0.5) |
---|
128 | |
---|
129 | def test_rmul(self): |
---|
130 | result = self.data*3.0 |
---|
131 | for i in range(5): |
---|
132 | self.assertEqual(result.y[i], 6.0) |
---|
133 | self.assertEqual(result.dy[i], 1.5) |
---|
134 | |
---|
135 | result = 3.0*self.data |
---|
136 | for i in range(5): |
---|
137 | self.assertEqual(result.y[i], 6.0) |
---|
138 | self.assertEqual(result.dy[i], 1.5) |
---|
139 | |
---|
140 | def test_rdiv(self): |
---|
141 | result = self.data/4.0 |
---|
142 | for i in range(5): |
---|
143 | self.assertEqual(result.y[i], 0.5) |
---|
144 | self.assertEqual(result.dy[i], 0.125) |
---|
145 | |
---|
146 | result = 6.0/self.data |
---|
147 | for i in range(5): |
---|
148 | self.assertEqual(result.y[i], 3.0) |
---|
149 | self.assertEqual(result.dy[i], 6.0*0.5/4.0) |
---|
150 | |
---|
151 | class manip_2D(unittest.TestCase): |
---|
152 | |
---|
153 | def setUp(self): |
---|
154 | # Create two data sets to play with |
---|
155 | x_0 = 2.0*numpy.ones(25) |
---|
156 | dx_0 = 0.5*numpy.ones(25) |
---|
157 | qx_0 = numpy.arange(25) |
---|
158 | qy_0 = numpy.arange(25) |
---|
159 | mask_0 = numpy.zeros(25) |
---|
160 | dqx_0 = numpy.arange(25)/100 |
---|
161 | dqy_0 = numpy.arange(25)/100 |
---|
162 | q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) |
---|
163 | self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, |
---|
164 | qy_data=qy_0, q_data=q_0, mask=mask_0, |
---|
165 | dqx_data=dqx_0, dqy_data=dqy_0) |
---|
166 | |
---|
167 | y = numpy.ones(25) |
---|
168 | dy = numpy.ones(25) |
---|
169 | qx = numpy.arange(25) |
---|
170 | qy = numpy.arange(25) |
---|
171 | mask = numpy.zeros(25) |
---|
172 | q = numpy.sqrt(qx * qx + qy * qy) |
---|
173 | self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, |
---|
174 | q_data=q, mask=mask) |
---|
175 | |
---|
176 | |
---|
177 | def test_load(self): |
---|
178 | """ |
---|
179 | Test whether the test file was loaded properly |
---|
180 | """ |
---|
181 | # There should be 5 entries in the file |
---|
182 | self.assertEqual(numpy.size(self.data.data), 25) |
---|
183 | |
---|
184 | for i in range(25): |
---|
185 | # All y-error values should be 0.5 |
---|
186 | self.assertEqual(self.data.err_data[i], 0.5) |
---|
187 | |
---|
188 | # All y values should be 2.0 |
---|
189 | self.assertEqual(self.data.data[i], 2.0) |
---|
190 | |
---|
191 | def test_add(self): |
---|
192 | result = self.data2+self.data |
---|
193 | for i in range(25): |
---|
194 | self.assertEqual(result.data[i], 3.0) |
---|
195 | self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) |
---|
196 | |
---|
197 | def test_sub(self): |
---|
198 | result = self.data2-self.data |
---|
199 | for i in range(25): |
---|
200 | self.assertEqual(result.data[i], -1.0) |
---|
201 | self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) |
---|
202 | |
---|
203 | def test_mul(self): |
---|
204 | result = self.data2*self.data |
---|
205 | for i in range(25): |
---|
206 | self.assertEqual(result.data[i], 2.0) |
---|
207 | self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
208 | |
---|
209 | def test_div(self): |
---|
210 | result = self.data2/self.data |
---|
211 | for i in range(25): |
---|
212 | self.assertEqual(result.data[i], 0.5) |
---|
213 | self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
214 | |
---|
215 | def test_radd(self): |
---|
216 | result = self.data+3.0 |
---|
217 | for i in range(25): |
---|
218 | self.assertEqual(result.data[i], 5.0) |
---|
219 | self.assertEqual(result.err_data[i], 0.5) |
---|
220 | |
---|
221 | result = 3.0+self.data |
---|
222 | for i in range(25): |
---|
223 | self.assertEqual(result.data[i], 5.0) |
---|
224 | self.assertEqual(result.err_data[i], 0.5) |
---|
225 | |
---|
226 | def test_rsub(self): |
---|
227 | result = self.data-3.0 |
---|
228 | for i in range(25): |
---|
229 | self.assertEqual(result.data[i], -1.0) |
---|
230 | self.assertEqual(result.err_data[i], 0.5) |
---|
231 | |
---|
232 | result = 3.0-self.data |
---|
233 | for i in range(25): |
---|
234 | self.assertEqual(result.data[i], 1.0) |
---|
235 | self.assertEqual(result.err_data[i], 0.5) |
---|
236 | |
---|
237 | def test_rmul(self): |
---|
238 | result = self.data*3.0 |
---|
239 | for i in range(25): |
---|
240 | self.assertEqual(result.data[i], 6.0) |
---|
241 | self.assertEqual(result.err_data[i], 1.5) |
---|
242 | |
---|
243 | result = 3.0*self.data |
---|
244 | for i in range(25): |
---|
245 | self.assertEqual(result.data[i], 6.0) |
---|
246 | self.assertEqual(result.err_data[i], 1.5) |
---|
247 | |
---|
248 | def test_rdiv(self): |
---|
249 | result = self.data/4.0 |
---|
250 | for i in range(25): |
---|
251 | self.assertEqual(result.data[i], 0.5) |
---|
252 | self.assertEqual(result.err_data[i], 0.125) |
---|
253 | |
---|
254 | result = 6.0/self.data |
---|
255 | for i in range(25): |
---|
256 | self.assertEqual(result.data[i], 3.0) |
---|
257 | self.assertEqual(result.err_data[i], 6.0*0.5/4.0) |
---|
258 | |
---|
259 | class extra_manip_2D(unittest.TestCase): |
---|
260 | |
---|
261 | def setUp(self): |
---|
262 | # Create two data sets to play with |
---|
263 | x_0 = 2.0*numpy.ones(25) |
---|
264 | dx_0 = 0.5*numpy.ones(25) |
---|
265 | qx_0 = numpy.arange(25) |
---|
266 | qy_0 = numpy.arange(25) |
---|
267 | mask_0 = numpy.zeros(25) |
---|
268 | dqx_0 = numpy.arange(25)/100 |
---|
269 | dqy_0 = numpy.arange(25)/100 |
---|
270 | q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) |
---|
271 | self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, |
---|
272 | qy_data=qy_0, q_data=q_0, mask=mask_0, |
---|
273 | dqx_data=dqx_0, dqy_data=dqy_0) |
---|
274 | |
---|
275 | y = numpy.ones(25) |
---|
276 | dy = numpy.ones(25) |
---|
277 | qx = numpy.arange(25) |
---|
278 | qy = numpy.arange(25) |
---|
279 | mask = numpy.zeros(25) |
---|
280 | q = numpy.sqrt(qx * qx + qy * qy) |
---|
281 | self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, |
---|
282 | q_data=q, mask=mask) |
---|
283 | |
---|
284 | |
---|
285 | def test_load(self): |
---|
286 | """ |
---|
287 | Test whether the test file was loaded properly |
---|
288 | """ |
---|
289 | # There should be 5 entries in the file |
---|
290 | self.assertEqual(numpy.size(self.data.data), 25) |
---|
291 | |
---|
292 | for i in range(25): |
---|
293 | # All y-error values should be 0.5 |
---|
294 | self.assertEqual(self.data.err_data[i], 0.5) |
---|
295 | |
---|
296 | # All y values should be 2.0 |
---|
297 | self.assertEqual(self.data.data[i], 2.0) |
---|
298 | |
---|
299 | def test_add(self): |
---|
300 | result = self.data2+self.data |
---|
301 | for i in range(25): |
---|
302 | self.assertEqual(result.data[i], 3.0) |
---|
303 | self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) |
---|
304 | |
---|
305 | def test_sub(self): |
---|
306 | result = self.data2-self.data |
---|
307 | for i in range(25): |
---|
308 | self.assertEqual(result.data[i], -1.0) |
---|
309 | self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) |
---|
310 | |
---|
311 | def test_mul(self): |
---|
312 | result = self.data2*self.data |
---|
313 | for i in range(25): |
---|
314 | self.assertEqual(result.data[i], 2.0) |
---|
315 | self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
316 | |
---|
317 | def test_div(self): |
---|
318 | result = self.data2/self.data |
---|
319 | for i in range(25): |
---|
320 | self.assertEqual(result.data[i], 0.5) |
---|
321 | self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
322 | |
---|
323 | def test_radd(self): |
---|
324 | result = self.data+3.0 |
---|
325 | for i in range(25): |
---|
326 | self.assertEqual(result.data[i], 5.0) |
---|
327 | self.assertEqual(result.err_data[i], 0.5) |
---|
328 | |
---|
329 | result = 3.0+self.data |
---|
330 | for i in range(25): |
---|
331 | self.assertEqual(result.data[i], 5.0) |
---|
332 | self.assertEqual(result.err_data[i], 0.5) |
---|
333 | |
---|
334 | def test_rsub(self): |
---|
335 | result = self.data-3.0 |
---|
336 | for i in range(25): |
---|
337 | self.assertEqual(result.data[i], -1.0) |
---|
338 | self.assertEqual(result.err_data[i], 0.5) |
---|
339 | |
---|
340 | result = 3.0-self.data |
---|
341 | for i in range(25): |
---|
342 | self.assertEqual(result.data[i], 1.0) |
---|
343 | self.assertEqual(result.err_data[i], 0.5) |
---|
344 | |
---|
345 | def test_rmul(self): |
---|
346 | result = self.data*3.0 |
---|
347 | for i in range(25): |
---|
348 | self.assertEqual(result.data[i], 6.0) |
---|
349 | self.assertEqual(result.err_data[i], 1.5) |
---|
350 | |
---|
351 | result = 3.0*self.data |
---|
352 | for i in range(25): |
---|
353 | self.assertEqual(result.data[i], 6.0) |
---|
354 | self.assertEqual(result.err_data[i], 1.5) |
---|
355 | |
---|
356 | def test_rdiv(self): |
---|
357 | result = self.data/4.0 |
---|
358 | for i in range(25): |
---|
359 | self.assertEqual(result.data[i], 0.5) |
---|
360 | self.assertEqual(result.err_data[i], 0.125) |
---|
361 | |
---|
362 | result = 6.0/self.data |
---|
363 | for i in range(25): |
---|
364 | self.assertEqual(result.data[i], 3.0) |
---|
365 | self.assertEqual(result.err_data[i], 6.0*0.5/4.0) |
---|
366 | |
---|
367 | if __name__ == '__main__': |
---|
368 | unittest.main() |
---|
369 | |
---|