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