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 igor_reader(unittest.TestCase): |
---|
61 | |
---|
62 | def setUp(self): |
---|
63 | self.data = Loader().load("MAR07232_rest.ASC") |
---|
64 | |
---|
65 | def test_checkdata(self): |
---|
66 | """ |
---|
67 | Check the data content to see whether |
---|
68 | it matches the specific file we loaded. |
---|
69 | Check the units too to see whether the |
---|
70 | Data1D defaults changed. Otherwise the |
---|
71 | tests won't pass |
---|
72 | """ |
---|
73 | self.assertEqual(self.data.filename, "MAR07232_rest.ASC") |
---|
74 | |
---|
75 | self.assertEqual(self.data.source.wavelength_unit, 'A') |
---|
76 | self.assertEqual(self.data.source.wavelength, 8.4) |
---|
77 | |
---|
78 | self.assertEqual(self.data.detector[0].distance_unit, 'mm') |
---|
79 | self.assertEqual(self.data.detector[0].distance, 13705) |
---|
80 | |
---|
81 | self.assertEqual(self.data.sample.transmission, 0.84357) |
---|
82 | |
---|
83 | self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') |
---|
84 | center_x = 68.76*5.0 |
---|
85 | center_y = 62.47*5.0 |
---|
86 | self.assertEqual(self.data.detector[0].beam_center.x, center_x) |
---|
87 | self.assertEqual(self.data.detector[0].beam_center.y, center_y) |
---|
88 | |
---|
89 | self.assertEqual(self.data.I_unit, '1/cm') |
---|
90 | self.assertEqual(self.data.data[0][0], 0.279783) |
---|
91 | self.assertEqual(self.data.data[0][1], 0.28951) |
---|
92 | self.assertEqual(self.data.data[0][2], 0.167634) |
---|
93 | |
---|
94 | class danse_reader(unittest.TestCase): |
---|
95 | |
---|
96 | def setUp(self): |
---|
97 | self.data = Loader().load("MP_New.sans") |
---|
98 | |
---|
99 | def test_checkdata(self): |
---|
100 | """ |
---|
101 | Check the data content to see whether |
---|
102 | it matches the specific file we loaded. |
---|
103 | Check the units too to see whether the |
---|
104 | Data1D defaults changed. Otherwise the |
---|
105 | tests won't pass |
---|
106 | """ |
---|
107 | self.assertEqual(self.data.filename, "MP_New.sans") |
---|
108 | |
---|
109 | self.assertEqual(self.data.source.wavelength_unit, 'A') |
---|
110 | self.assertEqual(self.data.source.wavelength, 7.5) |
---|
111 | |
---|
112 | self.assertEqual(self.data.detector[0].distance_unit, 'mm') |
---|
113 | self.assertAlmostEqual(self.data.detector[0].distance, 5414.99, 3) |
---|
114 | |
---|
115 | self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') |
---|
116 | center_x = 68.74*5.0 |
---|
117 | center_y = 64.77*5.0 |
---|
118 | self.assertEqual(self.data.detector[0].beam_center.x, center_x) |
---|
119 | self.assertEqual(self.data.detector[0].beam_center.y, center_y) |
---|
120 | |
---|
121 | self.assertEqual(self.data.I_unit, '1/cm') |
---|
122 | self.assertEqual(self.data.data[0][0], 1.57831) |
---|
123 | self.assertEqual(self.data.data[0][1], 2.70983) |
---|
124 | self.assertEqual(self.data.data[0][2], 3.83422) |
---|
125 | |
---|
126 | self.assertEqual(self.data.err_data[0][0], 1.37607) |
---|
127 | self.assertEqual(self.data.err_data[0][1], 1.77569) |
---|
128 | self.assertEqual(self.data.err_data[0][2], 2.06313) |
---|
129 | |
---|
130 | |
---|
131 | class cansas_reader(unittest.TestCase): |
---|
132 | |
---|
133 | def setUp(self): |
---|
134 | self.data = Loader().load("cansas1d.xml") |
---|
135 | |
---|
136 | def test_checkdata(self): |
---|
137 | self.assertEqual(self.data.filename, "cansas1d.xml") |
---|
138 | self._checkdata() |
---|
139 | |
---|
140 | def _checkdata(self): |
---|
141 | """ |
---|
142 | Check the data content to see whether |
---|
143 | it matches the specific file we loaded. |
---|
144 | Check the units too to see whether the |
---|
145 | Data1D defaults changed. Otherwise the |
---|
146 | tests won't pass |
---|
147 | """ |
---|
148 | |
---|
149 | self.assertEqual(self.data.run[0], "1234") |
---|
150 | |
---|
151 | # Data |
---|
152 | self.assertEqual(len(self.data.x), 2) |
---|
153 | self.assertEqual(self.data.x_unit, '1/A') |
---|
154 | self.assertEqual(self.data.y_unit, '1/cm') |
---|
155 | self.assertEqual(self.data.x[0], 0.02) |
---|
156 | self.assertEqual(self.data.y[0], 1000) |
---|
157 | self.assertEqual(self.data.dx[0], 0.01) |
---|
158 | self.assertEqual(self.data.dy[0], 3) |
---|
159 | self.assertEqual(self.data.x[1], 0.03) |
---|
160 | self.assertAlmostEquals(self.data.y[1], 1001.0) |
---|
161 | self.assertEqual(self.data.dx[1], 0.02) |
---|
162 | self.assertEqual(self.data.dy[1], 4) |
---|
163 | self.assertEqual(self.data.run_name['1234'], 'run name') |
---|
164 | self.assertEqual(self.data.title, "Test title") |
---|
165 | |
---|
166 | # Sample info |
---|
167 | self.assertEqual(self.data.sample.ID, "SI600-new-long") |
---|
168 | self.assertEqual(self.data.sample.name, "my sample") |
---|
169 | self.assertEqual(self.data.sample.thickness_unit, 'mm') |
---|
170 | self.assertEqual(self.data.sample.thickness, 1.03) |
---|
171 | |
---|
172 | self.assertEqual(self.data.sample.transmission, 0.327) |
---|
173 | |
---|
174 | self.assertEqual(self.data.sample.temperature_unit, 'C') |
---|
175 | self.assertEqual(self.data.sample.temperature, 0) |
---|
176 | |
---|
177 | self.assertEqual(self.data.sample.position_unit, 'mm') |
---|
178 | self.assertEqual(self.data.sample.position.x, 10) |
---|
179 | self.assertEqual(self.data.sample.position.y, 0) |
---|
180 | |
---|
181 | self.assertEqual(self.data.sample.orientation_unit, 'degree') |
---|
182 | self.assertEqual(self.data.sample.orientation.x, 22.5) |
---|
183 | self.assertEqual(self.data.sample.orientation.y, 0.02) |
---|
184 | |
---|
185 | self.assertEqual(self.data.sample.details[0], "http://chemtools.chem.soton.ac.uk/projects/blog/blogs.php/bit_id/2720") |
---|
186 | self.assertEqual(self.data.sample.details[1], "Some text here") |
---|
187 | |
---|
188 | # Instrument info |
---|
189 | self.assertEqual(self.data.instrument, "canSAS instrument") |
---|
190 | |
---|
191 | # Source |
---|
192 | self.assertEqual(self.data.source.radiation, "neutron") |
---|
193 | |
---|
194 | self.assertEqual(self.data.source.beam_size_unit, "mm") |
---|
195 | self.assertEqual(self.data.source.beam_size_name, "bm") |
---|
196 | self.assertEqual(self.data.source.beam_size.x, 12) |
---|
197 | self.assertEqual(self.data.source.beam_size.y, 13) |
---|
198 | |
---|
199 | self.assertEqual(self.data.source.beam_shape, "disc") |
---|
200 | |
---|
201 | self.assertEqual(self.data.source.wavelength_unit, "A") |
---|
202 | self.assertEqual(self.data.source.wavelength, 6) |
---|
203 | |
---|
204 | self.assertEqual(self.data.source.wavelength_max_unit, "nm") |
---|
205 | self.assertEqual(self.data.source.wavelength_max, 1.0) |
---|
206 | self.assertEqual(self.data.source.wavelength_min_unit, "nm") |
---|
207 | self.assertEqual(self.data.source.wavelength_min, 0.22) |
---|
208 | self.assertEqual(self.data.source.wavelength_spread_unit, "percent") |
---|
209 | self.assertEqual(self.data.source.wavelength_spread, 14.3) |
---|
210 | |
---|
211 | # Collimation |
---|
212 | _found1 = False |
---|
213 | _found2 = False |
---|
214 | self.assertEqual(self.data.collimation[0].length, 123.) |
---|
215 | self.assertEqual(self.data.collimation[0].name, 'test coll name') |
---|
216 | |
---|
217 | for item in self.data.collimation[0].aperture: |
---|
218 | self.assertEqual(item.size_unit,'mm') |
---|
219 | self.assertEqual(item.distance_unit,'mm') |
---|
220 | |
---|
221 | if item.size.x==50 \ |
---|
222 | and item.distance==11000.0 \ |
---|
223 | and item.name=='source' \ |
---|
224 | and item.type=='radius': |
---|
225 | _found1 = True |
---|
226 | elif item.size.x==1.0 \ |
---|
227 | and item.name=='sample' \ |
---|
228 | and item.type=='radius': |
---|
229 | _found2 = True |
---|
230 | |
---|
231 | if _found1==False or _found2==False: |
---|
232 | print item.distance |
---|
233 | raise RuntimeError, "Could not find all data %s %s" % (_found1, _found2) |
---|
234 | |
---|
235 | # Detector |
---|
236 | self.assertEqual(self.data.detector[0].name, "fictional hybrid") |
---|
237 | self.assertEqual(self.data.detector[0].distance_unit, "mm") |
---|
238 | self.assertEqual(self.data.detector[0].distance, 4150) |
---|
239 | |
---|
240 | self.assertEqual(self.data.detector[0].orientation_unit, "degree") |
---|
241 | self.assertEqual(self.data.detector[0].orientation.x, 1.0) |
---|
242 | self.assertEqual(self.data.detector[0].orientation.y, 0.0) |
---|
243 | self.assertEqual(self.data.detector[0].orientation.z, 0.0) |
---|
244 | |
---|
245 | self.assertEqual(self.data.detector[0].offset_unit, "m") |
---|
246 | self.assertEqual(self.data.detector[0].offset.x, .001) |
---|
247 | self.assertEqual(self.data.detector[0].offset.y, .002) |
---|
248 | self.assertEqual(self.data.detector[0].offset.z, None) |
---|
249 | |
---|
250 | self.assertEqual(self.data.detector[0].beam_center_unit, "mm") |
---|
251 | self.assertEqual(self.data.detector[0].beam_center.x, 322.64) |
---|
252 | self.assertEqual(self.data.detector[0].beam_center.y, 327.68) |
---|
253 | self.assertEqual(self.data.detector[0].beam_center.z, None) |
---|
254 | |
---|
255 | self.assertEqual(self.data.detector[0].pixel_size_unit, "mm") |
---|
256 | self.assertEqual(self.data.detector[0].pixel_size.x, 5) |
---|
257 | self.assertEqual(self.data.detector[0].pixel_size.y, 5) |
---|
258 | self.assertEqual(self.data.detector[0].pixel_size.z, None) |
---|
259 | |
---|
260 | # Process |
---|
261 | _found_term1 = False |
---|
262 | _found_term2 = False |
---|
263 | for item in self.data.process: |
---|
264 | self.assertTrue(item.name in ['NCNR-IGOR', 'spol']) |
---|
265 | self.assertTrue(item.date in ['04-Sep-2007 18:35:02', |
---|
266 | '03-SEP-2006 11:42:47']) |
---|
267 | for t in item.term: |
---|
268 | if t['name']=="ABS:DSTAND" \ |
---|
269 | and t['unit']=='mm' \ |
---|
270 | and float(t['value'])==1.0: |
---|
271 | _found_term2 = True |
---|
272 | elif t['name']=="radialstep" \ |
---|
273 | and t['unit']=='mm' \ |
---|
274 | and float(t['value'])==10.0: |
---|
275 | _found_term1 = True |
---|
276 | |
---|
277 | if _found_term1==False or _found_term2==False: |
---|
278 | raise RuntimeError, "Could not find all process terms %s %s" % (_found_term1, _found_term2) |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | def test_writer(self): |
---|
284 | from DataLoader.readers.cansas_reader import Reader |
---|
285 | r = Reader() |
---|
286 | x = numpy.ones(5) |
---|
287 | y = numpy.ones(5) |
---|
288 | dy = numpy.ones(5) |
---|
289 | |
---|
290 | filename = "write_test.xml" |
---|
291 | r.write(filename, self.data) |
---|
292 | self.data = Loader().load(filename) |
---|
293 | self.assertEqual(self.data.filename, filename) |
---|
294 | self._checkdata() |
---|
295 | |
---|
296 | def test_units(self): |
---|
297 | """ |
---|
298 | Check units. |
---|
299 | Note that not all units are available. |
---|
300 | """ |
---|
301 | filename = "cansas1d_units.xml" |
---|
302 | self.data = Loader().load(filename) |
---|
303 | self.assertEqual(self.data.filename, filename) |
---|
304 | self._checkdata() |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | if __name__ == '__main__': |
---|
311 | unittest.main() |
---|
312 | |
---|