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