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