1 | """ |
---|
2 | Unit tests for data manipulations |
---|
3 | """ |
---|
4 | |
---|
5 | import unittest |
---|
6 | import numpy, math |
---|
7 | from DataLoader.loader import Loader |
---|
8 | from DataLoader.data_info import Data1D |
---|
9 | |
---|
10 | import os.path |
---|
11 | |
---|
12 | class abs_reader(unittest.TestCase): |
---|
13 | |
---|
14 | def setUp(self): |
---|
15 | self.data = Loader().load("jan08002.ABS") |
---|
16 | |
---|
17 | def test_checkdata(self): |
---|
18 | """ |
---|
19 | Check the data content to see whether |
---|
20 | it matches the specific file we loaded. |
---|
21 | Check the units too to see whether the |
---|
22 | Data1D defaults changed. Otherwise the |
---|
23 | tests won't pass |
---|
24 | """ |
---|
25 | self.assertEqual(self.data.filename, "jan08002.ABS") |
---|
26 | |
---|
27 | self.assertEqual(self.data.source.wavelength_unit, 'A') |
---|
28 | self.assertEqual(self.data.source.wavelength, 6.0) |
---|
29 | |
---|
30 | self.assertEqual(self.data.detector[0].distance_unit, 'mm') |
---|
31 | self.assertEqual(self.data.detector[0].distance, 1000.0) |
---|
32 | |
---|
33 | self.assertEqual(self.data.sample.transmission, 0.5667) |
---|
34 | |
---|
35 | self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') |
---|
36 | center_x = 114.58*5.0 |
---|
37 | center_y = 64.22*5.0 |
---|
38 | self.assertEqual(self.data.detector[0].beam_center.x, center_x) |
---|
39 | self.assertEqual(self.data.detector[0].beam_center.y, center_y) |
---|
40 | |
---|
41 | self.assertEqual(self.data.y_unit, '1/cm') |
---|
42 | self.assertEqual(self.data.x[0], 0.002618) |
---|
43 | self.assertEqual(self.data.x[1], 0.007854) |
---|
44 | self.assertEqual(self.data.x[2], 0.01309) |
---|
45 | self.assertEqual(self.data.x[126], 0.5828) |
---|
46 | |
---|
47 | self.assertEqual(self.data.y[0], 0.02198) |
---|
48 | self.assertEqual(self.data.y[1], 0.02201) |
---|
49 | self.assertEqual(self.data.y[2], 0.02695) |
---|
50 | self.assertEqual(self.data.y[126], 0.2958) |
---|
51 | |
---|
52 | self.assertEqual(self.data.dy[0], 0.002704) |
---|
53 | self.assertEqual(self.data.dy[1], 0.001643) |
---|
54 | self.assertEqual(self.data.dy[2], 0.002452) |
---|
55 | self.assertEqual(self.data.dy[126], 1) |
---|
56 | |
---|
57 | def test_checkdata2(self): |
---|
58 | self.assertEqual(self.data.dy[126], 1) |
---|
59 | |
---|
60 | class hfir_reader(unittest.TestCase): |
---|
61 | |
---|
62 | def setUp(self): |
---|
63 | self.data = Loader().load("S2-30dq.d1d") |
---|
64 | |
---|
65 | def test_checkdata(self): |
---|
66 | """ |
---|
67 | Check the data content to see whether |
---|
68 | it matches the specific file we loaded. |
---|
69 | """ |
---|
70 | self.assertEqual(self.data.filename, "S2-30dq.d1d") |
---|
71 | self.assertEqual(len(self.data.x), 134) |
---|
72 | self.assertEqual(len(self.data.y), 134) |
---|
73 | # Q I dI dQ |
---|
74 | # Point 1: 0.003014 0.003010 0.000315 0.008249 |
---|
75 | self.assertEqual(self.data.x[1], 0.003014) |
---|
76 | self.assertEqual(self.data.y[1], 0.003010) |
---|
77 | self.assertEqual(self.data.dy[1], 0.000315) |
---|
78 | self.assertEqual(self.data.dx[1], 0.008249) |
---|
79 | |
---|
80 | |
---|
81 | class igor_reader(unittest.TestCase): |
---|
82 | |
---|
83 | def setUp(self): |
---|
84 | self.data = Loader().load("MAR07232_rest.ASC") |
---|
85 | |
---|
86 | def test_checkdata(self): |
---|
87 | """ |
---|
88 | Check the data content to see whether |
---|
89 | it matches the specific file we loaded. |
---|
90 | Check the units too to see whether the |
---|
91 | Data1D defaults changed. Otherwise the |
---|
92 | tests won't pass |
---|
93 | """ |
---|
94 | self.assertEqual(self.data.filename, "MAR07232_rest.ASC") |
---|
95 | |
---|
96 | self.assertEqual(self.data.source.wavelength_unit, 'A') |
---|
97 | self.assertEqual(self.data.source.wavelength, 8.4) |
---|
98 | |
---|
99 | self.assertEqual(self.data.detector[0].distance_unit, 'mm') |
---|
100 | self.assertEqual(self.data.detector[0].distance, 13705) |
---|
101 | |
---|
102 | self.assertEqual(self.data.sample.transmission, 0.84357) |
---|
103 | |
---|
104 | self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') |
---|
105 | center_x = 68.76*5.0 |
---|
106 | center_y = 62.47*5.0 |
---|
107 | self.assertEqual(self.data.detector[0].beam_center.x, center_x) |
---|
108 | self.assertEqual(self.data.detector[0].beam_center.y, center_y) |
---|
109 | |
---|
110 | self.assertEqual(self.data.I_unit, '1/cm') |
---|
111 | self.assertEqual(self.data.data[0][0], 0.279783) |
---|
112 | self.assertEqual(self.data.data[0][1], 0.28951) |
---|
113 | self.assertEqual(self.data.data[0][2], 0.167634) |
---|
114 | |
---|
115 | class danse_reader(unittest.TestCase): |
---|
116 | |
---|
117 | def setUp(self): |
---|
118 | self.data = Loader().load("MP_New.sans") |
---|
119 | |
---|
120 | def test_checkdata(self): |
---|
121 | """ |
---|
122 | Check the data content to see whether |
---|
123 | it matches the specific file we loaded. |
---|
124 | Check the units too to see whether the |
---|
125 | Data1D defaults changed. Otherwise the |
---|
126 | tests won't pass |
---|
127 | """ |
---|
128 | self.assertEqual(self.data.filename, "MP_New.sans") |
---|
129 | |
---|
130 | self.assertEqual(self.data.source.wavelength_unit, 'A') |
---|
131 | self.assertEqual(self.data.source.wavelength, 7.5) |
---|
132 | |
---|
133 | self.assertEqual(self.data.detector[0].distance_unit, 'mm') |
---|
134 | self.assertAlmostEqual(self.data.detector[0].distance, 5414.99, 3) |
---|
135 | |
---|
136 | self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') |
---|
137 | center_x = 68.74*5.0 |
---|
138 | center_y = 64.77*5.0 |
---|
139 | self.assertEqual(self.data.detector[0].beam_center.x, center_x) |
---|
140 | self.assertEqual(self.data.detector[0].beam_center.y, center_y) |
---|
141 | |
---|
142 | self.assertEqual(self.data.I_unit, '1/cm') |
---|
143 | self.assertEqual(self.data.data[0][0], 1.57831) |
---|
144 | self.assertEqual(self.data.data[0][1], 2.70983) |
---|
145 | self.assertEqual(self.data.data[0][2], 3.83422) |
---|
146 | |
---|
147 | self.assertEqual(self.data.err_data[0][0], 1.37607) |
---|
148 | self.assertEqual(self.data.err_data[0][1], 1.77569) |
---|
149 | self.assertEqual(self.data.err_data[0][2], 2.06313) |
---|
150 | |
---|
151 | |
---|
152 | class cansas_reader(unittest.TestCase): |
---|
153 | |
---|
154 | def setUp(self): |
---|
155 | self.data = Loader().load("cansas1d.xml") |
---|
156 | |
---|
157 | def test_checkdata(self): |
---|
158 | self.assertEqual(self.data.filename, "cansas1d.xml") |
---|
159 | self._checkdata() |
---|
160 | |
---|
161 | def _checkdata(self): |
---|
162 | """ |
---|
163 | Check the data content to see whether |
---|
164 | it matches the specific file we loaded. |
---|
165 | Check the units too to see whether the |
---|
166 | Data1D defaults changed. Otherwise the |
---|
167 | tests won't pass |
---|
168 | """ |
---|
169 | |
---|
170 | self.assertEqual(self.data.run[0], "1234") |
---|
171 | |
---|
172 | # Data |
---|
173 | self.assertEqual(len(self.data.x), 2) |
---|
174 | self.assertEqual(self.data.x_unit, '1/A') |
---|
175 | self.assertEqual(self.data.y_unit, '1/cm') |
---|
176 | self.assertEqual(self.data.x[0], 0.02) |
---|
177 | self.assertEqual(self.data.y[0], 1000) |
---|
178 | self.assertEqual(self.data.dx[0], 0.01) |
---|
179 | self.assertEqual(self.data.dy[0], 3) |
---|
180 | self.assertEqual(self.data.x[1], 0.03) |
---|
181 | self.assertAlmostEquals(self.data.y[1], 1001.0) |
---|
182 | self.assertEqual(self.data.dx[1], 0.02) |
---|
183 | self.assertEqual(self.data.dy[1], 4) |
---|
184 | self.assertEqual(self.data.run_name['1234'], 'run name') |
---|
185 | self.assertEqual(self.data.title, "Test title") |
---|
186 | |
---|
187 | # Sample info |
---|
188 | self.assertEqual(self.data.sample.ID, "SI600-new-long") |
---|
189 | self.assertEqual(self.data.sample.name, "my sample") |
---|
190 | self.assertEqual(self.data.sample.thickness_unit, 'mm') |
---|
191 | self.assertEqual(self.data.sample.thickness, 1.03) |
---|
192 | |
---|
193 | self.assertEqual(self.data.sample.transmission, 0.327) |
---|
194 | |
---|
195 | self.assertEqual(self.data.sample.temperature_unit, 'C') |
---|
196 | self.assertEqual(self.data.sample.temperature, 0) |
---|
197 | |
---|
198 | self.assertEqual(self.data.sample.position_unit, 'mm') |
---|
199 | self.assertEqual(self.data.sample.position.x, 10) |
---|
200 | self.assertEqual(self.data.sample.position.y, 0) |
---|
201 | |
---|
202 | self.assertEqual(self.data.sample.orientation_unit, 'degree') |
---|
203 | self.assertEqual(self.data.sample.orientation.x, 22.5) |
---|
204 | self.assertEqual(self.data.sample.orientation.y, 0.02) |
---|
205 | |
---|
206 | self.assertEqual(self.data.sample.details[0], "http://chemtools.chem.soton.ac.uk/projects/blog/blogs.php/bit_id/2720") |
---|
207 | self.assertEqual(self.data.sample.details[1], "Some text here") |
---|
208 | |
---|
209 | # Instrument info |
---|
210 | self.assertEqual(self.data.instrument, "canSAS instrument") |
---|
211 | |
---|
212 | # Source |
---|
213 | self.assertEqual(self.data.source.radiation, "neutron") |
---|
214 | |
---|
215 | self.assertEqual(self.data.source.beam_size_unit, "mm") |
---|
216 | self.assertEqual(self.data.source.beam_size_name, "bm") |
---|
217 | self.assertEqual(self.data.source.beam_size.x, 12) |
---|
218 | self.assertEqual(self.data.source.beam_size.y, 13) |
---|
219 | |
---|
220 | self.assertEqual(self.data.source.beam_shape, "disc") |
---|
221 | |
---|
222 | self.assertEqual(self.data.source.wavelength_unit, "A") |
---|
223 | self.assertEqual(self.data.source.wavelength, 6) |
---|
224 | |
---|
225 | self.assertEqual(self.data.source.wavelength_max_unit, "nm") |
---|
226 | self.assertEqual(self.data.source.wavelength_max, 1.0) |
---|
227 | self.assertEqual(self.data.source.wavelength_min_unit, "nm") |
---|
228 | self.assertEqual(self.data.source.wavelength_min, 0.22) |
---|
229 | self.assertEqual(self.data.source.wavelength_spread_unit, "percent") |
---|
230 | self.assertEqual(self.data.source.wavelength_spread, 14.3) |
---|
231 | |
---|
232 | # Collimation |
---|
233 | _found1 = False |
---|
234 | _found2 = False |
---|
235 | self.assertEqual(self.data.collimation[0].length, 123.) |
---|
236 | self.assertEqual(self.data.collimation[0].name, 'test coll name') |
---|
237 | |
---|
238 | for item in self.data.collimation[0].aperture: |
---|
239 | self.assertEqual(item.size_unit,'mm') |
---|
240 | self.assertEqual(item.distance_unit,'mm') |
---|
241 | |
---|
242 | if item.size.x==50 \ |
---|
243 | and item.distance==11000.0 \ |
---|
244 | and item.name=='source' \ |
---|
245 | and item.type=='radius': |
---|
246 | _found1 = True |
---|
247 | elif item.size.x==1.0 \ |
---|
248 | and item.name=='sample' \ |
---|
249 | and item.type=='radius': |
---|
250 | _found2 = True |
---|
251 | |
---|
252 | if _found1==False or _found2==False: |
---|
253 | print item.distance |
---|
254 | raise RuntimeError, "Could not find all data %s %s" % (_found1, _found2) |
---|
255 | |
---|
256 | # Detector |
---|
257 | self.assertEqual(self.data.detector[0].name, "fictional hybrid") |
---|
258 | self.assertEqual(self.data.detector[0].distance_unit, "mm") |
---|
259 | self.assertEqual(self.data.detector[0].distance, 4150) |
---|
260 | |
---|
261 | self.assertEqual(self.data.detector[0].orientation_unit, "degree") |
---|
262 | self.assertEqual(self.data.detector[0].orientation.x, 1.0) |
---|
263 | self.assertEqual(self.data.detector[0].orientation.y, 0.0) |
---|
264 | self.assertEqual(self.data.detector[0].orientation.z, 0.0) |
---|
265 | |
---|
266 | self.assertEqual(self.data.detector[0].offset_unit, "m") |
---|
267 | self.assertEqual(self.data.detector[0].offset.x, .001) |
---|
268 | self.assertEqual(self.data.detector[0].offset.y, .002) |
---|
269 | self.assertEqual(self.data.detector[0].offset.z, None) |
---|
270 | |
---|
271 | self.assertEqual(self.data.detector[0].beam_center_unit, "mm") |
---|
272 | self.assertEqual(self.data.detector[0].beam_center.x, 322.64) |
---|
273 | self.assertEqual(self.data.detector[0].beam_center.y, 327.68) |
---|
274 | self.assertEqual(self.data.detector[0].beam_center.z, None) |
---|
275 | |
---|
276 | self.assertEqual(self.data.detector[0].pixel_size_unit, "mm") |
---|
277 | self.assertEqual(self.data.detector[0].pixel_size.x, 5) |
---|
278 | self.assertEqual(self.data.detector[0].pixel_size.y, 5) |
---|
279 | self.assertEqual(self.data.detector[0].pixel_size.z, None) |
---|
280 | |
---|
281 | # Process |
---|
282 | _found_term1 = False |
---|
283 | _found_term2 = False |
---|
284 | for item in self.data.process: |
---|
285 | self.assertTrue(item.name in ['NCNR-IGOR', 'spol']) |
---|
286 | self.assertTrue(item.date in ['04-Sep-2007 18:35:02', |
---|
287 | '03-SEP-2006 11:42:47']) |
---|
288 | for t in item.term: |
---|
289 | if t['name']=="ABS:DSTAND" \ |
---|
290 | and t['unit']=='mm' \ |
---|
291 | and float(t['value'])==1.0: |
---|
292 | _found_term2 = True |
---|
293 | elif t['name']=="radialstep" \ |
---|
294 | and t['unit']=='mm' \ |
---|
295 | and float(t['value'])==10.0: |
---|
296 | _found_term1 = True |
---|
297 | |
---|
298 | if _found_term1==False or _found_term2==False: |
---|
299 | raise RuntimeError, "Could not find all process terms %s %s" % (_found_term1, _found_term2) |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | def test_writer(self): |
---|
305 | from DataLoader.readers.cansas_reader import Reader |
---|
306 | r = Reader() |
---|
307 | x = numpy.ones(5) |
---|
308 | y = numpy.ones(5) |
---|
309 | dy = numpy.ones(5) |
---|
310 | |
---|
311 | filename = "write_test.xml" |
---|
312 | r.write(filename, self.data) |
---|
313 | self.data = Loader().load(filename) |
---|
314 | self.assertEqual(self.data.filename, filename) |
---|
315 | self._checkdata() |
---|
316 | |
---|
317 | def test_units(self): |
---|
318 | """ |
---|
319 | Check units. |
---|
320 | Note that not all units are available. |
---|
321 | """ |
---|
322 | filename = "cansas1d_units.xml" |
---|
323 | self.data = Loader().load(filename) |
---|
324 | self.assertEqual(self.data.filename, filename) |
---|
325 | self._checkdata() |
---|
326 | |
---|
327 | |
---|
328 | def test_slits(self): |
---|
329 | """ |
---|
330 | Check slit data |
---|
331 | """ |
---|
332 | filename = "cansas1d_slit.xml" |
---|
333 | self.data = Loader().load(filename) |
---|
334 | self.assertEqual(self.data.filename, filename) |
---|
335 | self.assertEqual(self.data.run[0], "1234") |
---|
336 | |
---|
337 | # Data |
---|
338 | self.assertEqual(len(self.data.x), 2) |
---|
339 | self.assertEqual(self.data.x_unit, '1/A') |
---|
340 | self.assertEqual(self.data.y_unit, '1/cm') |
---|
341 | self.assertEqual(self.data.x[0], 0.02) |
---|
342 | self.assertEqual(self.data.y[0], 1000) |
---|
343 | self.assertEqual(self.data.dxl[0], 0.005) |
---|
344 | self.assertEqual(self.data.dxw[0], 0.001) |
---|
345 | self.assertEqual(self.data.dy[0], 3) |
---|
346 | self.assertEqual(self.data.x[1], 0.03) |
---|
347 | self.assertAlmostEquals(self.data.y[1], 1001.0) |
---|
348 | self.assertEqual(self.data.dx[1], 0.0) |
---|
349 | self.assertEqual(self.data.dxl[1], 0.005) |
---|
350 | self.assertEqual(self.data.dxw[1], 0.001) |
---|
351 | self.assertEqual(self.data.dy[1], 4) |
---|
352 | self.assertEqual(self.data.run_name['1234'], 'run name') |
---|
353 | self.assertEqual(self.data.title, "Test title") |
---|
354 | |
---|
355 | |
---|
356 | |
---|
357 | if __name__ == '__main__': |
---|
358 | unittest.main() |
---|
359 | |
---|