Changeset 5a8cdbb in sasview for test/sasdataloader/test/utest_abs_reader.py
- Timestamp:
- Aug 1, 2017 12:02:35 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 511ccb2d
- Parents:
- 248ff73 (diff), bc04647 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasdataloader/test/utest_abs_reader.py
r371b9e2 r5a8cdbb 2 2 Unit tests for data manipulations 3 3 """ 4 from __future__ import print_function 4 5 5 6 import unittest … … 7 8 from sas.sascalc.dataloader.loader import Loader 8 9 from sas.sascalc.dataloader.readers.IgorReader import Reader as IgorReader 9 from sas.sascalc.dataloader.readers.abs_reader import Reader as ABSReader 10 11 12 class ABSReaderTests(unittest.TestCase): 13 10 from sas.sascalc.dataloader.readers.abs_reader import Reader as AbsReader 11 from sas.sascalc.dataloader.readers.hfir1d_reader import Reader as HFIRReader 12 from sas.sascalc.dataloader.readers.danse_reader import Reader as DANSEReader 13 from sas.sascalc.dataloader.readers.cansas_reader import Reader as CANSASReader 14 15 from sas.sascalc.dataloader.data_info import Data1D 16 17 import os.path 18 19 20 class abs_reader(unittest.TestCase): 21 14 22 def setUp(self): 15 self.data_list = ABSReader().read("jan08002.ABS")16 self.data = self.data_list[0]17 23 reader = AbsReader() 24 self.data = reader.read("jan08002.ABS") 25 18 26 def test_abs_checkdata(self): 19 27 """ 20 Check the data content to see whether 28 Check the data content to see whether 21 29 it matches the specific file we loaded. 22 30 Check the units too to see whether the … … 26 34 self.assertEqual(self.data.filename, "jan08002.ABS") 27 35 self.assertEqual(self.data.meta_data['loader'], "IGOR 1D") 28 36 29 37 self.assertEqual(self.data.source.wavelength_unit, 'A') 30 38 self.assertEqual(self.data.source.wavelength, 6.0) 31 39 32 40 self.assertEqual(self.data.detector[0].distance_unit, 'mm') 33 41 self.assertEqual(self.data.detector[0].distance, 1000.0) 34 42 35 43 self.assertEqual(self.data.sample.transmission, 0.5667) 36 44 37 45 self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') 38 46 center_x = 114.58*5.08 … … 40 48 self.assertEqual(self.data.detector[0].beam_center.x, center_x) 41 49 self.assertEqual(self.data.detector[0].beam_center.y, center_y) 42 50 43 51 self.assertEqual(self.data.y_unit, '1/cm') 44 52 self.assertEqual(self.data.x[0], 0.002618) … … 46 54 self.assertEqual(self.data.x[2], 0.01309) 47 55 self.assertEqual(self.data.x[126], 0.5828) 48 56 49 57 self.assertEqual(self.data.y[0], 0.02198) 50 58 self.assertEqual(self.data.y[1], 0.02201) 51 59 self.assertEqual(self.data.y[2], 0.02695) 52 60 self.assertEqual(self.data.y[126], 0.2958) 53 61 54 62 self.assertEqual(self.data.dy[0], 0.002704) 55 63 self.assertEqual(self.data.dy[1], 0.001643) 56 64 self.assertEqual(self.data.dy[2], 0.002452) 57 65 self.assertEqual(self.data.dy[126], 1) 58 66 59 67 def test_checkdata2(self): 60 68 self.assertEqual(self.data.dy[126], 1) 61 69 62 63 class HFIRReaderTests(unittest.TestCase): 70 def test_generic_loader(self): 71 # the generic loader should work as well 72 data = Loader().load("jan08002.ABS") 73 self.assertEqual(data.meta_data['loader'], "IGOR 1D") 74 75 76 class hfir_reader(unittest.TestCase): 64 77 65 78 def setUp(self): 66 self.data = Loader().load("S2-30dq.d1d") 79 reader = HFIRReader() 80 self.data = reader.read("S2-30dq.d1d") 67 81 68 82 def test_hfir_checkdata(self): 69 83 """ 70 Check the data content to see whether 84 Check the data content to see whether 71 85 it matches the specific file we loaded. 72 86 """ … … 76 90 self.assertEqual(len(self.data.x), 134) 77 91 self.assertEqual(len(self.data.y), 134) 78 # Q I dI dQ 92 # Q I dI dQ 79 93 # Point 1: 0.003014 0.003010 0.000315 0.008249 80 94 self.assertEqual(self.data.x[1], 0.003014) … … 83 97 self.assertEqual(self.data.dx[1], 0.008249) 84 98 85 86 class DanseReaderTests(unittest.TestCase): 87 99 def test_generic_loader(self): 100 # the generic loader should work as well 101 data = Loader().load("S2-30dq.d1d") 102 self.assertEqual(data.meta_data['loader'], "HFIR 1D") 103 104 105 class igor_reader(unittest.TestCase): 106 88 107 def setUp(self): 89 self.data = Loader().load("MP_New.sans") 90 91 def test_checkdata(self): 92 """ 93 Check the data content to see whether 108 # the IgorReader should be able to read this filetype 109 # if it can't, stop here. 110 reader = IgorReader() 111 self.data = reader.read("MAR07232_rest.ASC") 112 113 def test_igor_checkdata(self): 114 """ 115 Check the data content to see whether 94 116 it matches the specific file we loaded. 95 117 Check the units too to see whether the … … 97 119 tests won't pass 98 120 """ 121 self.assertEqual(self.data.filename, "MAR07232_rest.ASC") 122 self.assertEqual(self.data.meta_data['loader'], "IGOR 2D") 123 124 self.assertEqual(self.data.source.wavelength_unit, 'A') 125 self.assertEqual(self.data.source.wavelength, 8.4) 126 127 self.assertEqual(self.data.detector[0].distance_unit, 'mm') 128 self.assertEqual(self.data.detector[0].distance, 13705) 129 130 self.assertEqual(self.data.sample.transmission, 0.84357) 131 132 self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') 133 center_x = (68.76 - 1)*5.0 134 center_y = (62.47 - 1)*5.0 135 self.assertEqual(self.data.detector[0].beam_center.x, center_x) 136 self.assertEqual(self.data.detector[0].beam_center.y, center_y) 137 138 self.assertEqual(self.data.I_unit, '1/cm') 139 # 3 points should be suffcient to check that the data is in column 140 # major order. 141 np.testing.assert_almost_equal(self.data.data[0:3], 142 [0.279783, 0.28951, 0.167634]) 143 np.testing.assert_almost_equal(self.data.qx_data[0:3], 144 [-0.01849072, -0.01821785, -0.01794498]) 145 np.testing.assert_almost_equal(self.data.qy_data[0:3], 146 [-0.01677435, -0.01677435, -0.01677435]) 147 148 def test_generic_loader(self): 149 # the generic loader should direct the file to IgorReader as well 150 data = Loader().load("MAR07232_rest.ASC") 151 self.assertEqual(data.meta_data['loader'], "IGOR 2D") 152 153 154 class DanseReaderTests(unittest.TestCase): 155 156 def setUp(self): 157 reader = DANSEReader() 158 self.data = reader.read("MP_New.sans") 159 160 def test_checkdata(self): 161 """ 162 Check the data content to see whether 163 it matches the specific file we loaded. 164 Check the units too to see whether the 165 Data1D defaults changed. Otherwise the 166 tests won't pass 167 """ 99 168 self.assertEqual(self.data.filename, "MP_New.sans") 100 169 self.assertEqual(self.data.meta_data['loader'], "DANSE") 101 170 102 171 self.assertEqual(self.data.source.wavelength_unit, 'A') 103 172 self.assertEqual(self.data.source.wavelength, 7.5) 104 173 105 174 self.assertEqual(self.data.detector[0].distance_unit, 'mm') 106 175 self.assertAlmostEqual(self.data.detector[0].distance, 5414.99, 3) 107 176 108 177 self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') 109 178 center_x = 68.74*5.0 … … 111 180 self.assertEqual(self.data.detector[0].beam_center.x, center_x) 112 181 self.assertEqual(self.data.detector[0].beam_center.y, center_y) 113 182 114 183 self.assertEqual(self.data.I_unit, '1/cm') 115 184 self.assertEqual(self.data.data[0], 1.57831) … … 121 190 self.assertEqual(self.data.err_data[2], 2.06313) 122 191 123 192 def test_generic_loader(self): 193 # the generic loader should work as well 194 data = Loader().load("MP_New.sans") 195 self.assertEqual(data.meta_data['loader'], "DANSE") 196 197 124 198 class cansas_reader(unittest.TestCase): 125 199 126 200 def setUp(self): 201 reader = CANSASReader() 202 data = reader.read("cansas1d.xml") 203 self.data = data[0] 204 205 def test_generic_loader(self): 206 # the generic loader should work as well 127 207 data = Loader().load("cansas1d.xml") 128 self. data = data[0]129 208 self.assertEqual(data[0].meta_data['loader'], "CanSAS XML 1D") 209 130 210 def test_cansas_checkdata(self): 131 211 self.assertEqual(self.data.filename, "cansas1d.xml") 132 212 self._checkdata() 133 213 134 214 def _checkdata(self): 135 215 """ 136 Check the data content to see whether 216 Check the data content to see whether 137 217 it matches the specific file we loaded. 138 218 Check the units too to see whether the … … 140 220 tests won't pass 141 221 """ 142 143 222 self.assertEqual(self.data.run[0], "1234") 144 223 self.assertEqual(self.data.meta_data['loader'], "CanSAS XML 1D") 145 224 146 225 # Data 147 226 self.assertEqual(len(self.data.x), 2) … … 158 237 self.assertEqual(self.data.run_name['1234'], 'run name') 159 238 self.assertEqual(self.data.title, "Test title") 160 239 161 240 # Sample info 162 241 self.assertEqual(self.data.sample.ID, "SI600-new-long") … … 164 243 self.assertEqual(self.data.sample.thickness_unit, 'mm') 165 244 self.assertAlmostEqual(self.data.sample.thickness, 1.03) 166 245 167 246 self.assertAlmostEqual(self.data.sample.transmission, 0.327) 168 247 169 248 self.assertEqual(self.data.sample.temperature_unit, 'C') 170 249 self.assertEqual(self.data.sample.temperature, 0) … … 178 257 self.assertAlmostEqual(self.data.sample.orientation.y, 0.02, 6) 179 258 180 self.assertEqual(self.data.sample.details[0], "http://chemtools.chem.soton.ac.uk/projects/blog/blogs.php/bit_id/2720") 181 self.assertEqual(self.data.sample.details[1], "Some text here") 182 259 self.assertEqual(self.data.sample.details[0], "http://chemtools.chem.soton.ac.uk/projects/blog/blogs.php/bit_id/2720") 260 self.assertEqual(self.data.sample.details[1], "Some text here") 261 183 262 # Instrument info 184 263 self.assertEqual(self.data.instrument, "canSAS instrument") 185 264 186 265 # Source 187 266 self.assertEqual(self.data.source.radiation, "neutron") 188 267 189 268 self.assertEqual(self.data.source.beam_size_unit, "mm") 190 269 self.assertEqual(self.data.source.beam_size_name, "bm") 191 270 self.assertEqual(self.data.source.beam_size.x, 12) 192 271 self.assertEqual(self.data.source.beam_size.y, 13) 193 272 194 273 self.assertEqual(self.data.source.beam_shape, "disc") 195 274 196 275 self.assertEqual(self.data.source.wavelength_unit, "A") 197 276 self.assertEqual(self.data.source.wavelength, 6) 198 277 199 278 self.assertEqual(self.data.source.wavelength_max_unit, "nm") 200 279 self.assertAlmostEqual(self.data.source.wavelength_max, 1.0) … … 203 282 self.assertEqual(self.data.source.wavelength_spread_unit, "percent") 204 283 self.assertEqual(self.data.source.wavelength_spread, 14.3) 205 284 206 285 # Collimation 207 286 _found1 = False … … 209 288 self.assertEqual(self.data.collimation[0].length, 123.) 210 289 self.assertEqual(self.data.collimation[0].name, 'test coll name') 211 290 212 291 for item in self.data.collimation[0].aperture: 213 292 self.assertEqual(item.size_unit,'mm') 214 293 self.assertEqual(item.distance_unit,'mm') 215 294 216 if item.size.x ==50 \217 and item.distance ==11000.0 \218 and item.name =='source' \219 and item.type =='radius':295 if item.size.x == 50 \ 296 and item.distance == 11000.0 \ 297 and item.name == 'source' \ 298 and item.type == 'radius': 220 299 _found1 = True 221 elif item.size.x ==1.0 \222 and item.name =='sample' \223 and item.type =='radius':300 elif item.size.x == 1.0 \ 301 and item.name == 'sample' \ 302 and item.type == 'radius': 224 303 _found2 = True 225 226 if _found1 ==False or _found2==False:227 raise RuntimeError, "Could not find all data %s %s" % (_found1, _found2) 228 304 305 if _found1 == False or _found2 == False: 306 raise RuntimeError, "Could not find all data %s %s" % (_found1, _found2) 307 229 308 # Detector 230 309 self.assertEqual(self.data.detector[0].name, "fictional hybrid") 231 310 self.assertEqual(self.data.detector[0].distance_unit, "mm") 232 311 self.assertEqual(self.data.detector[0].distance, 4150) 233 312 234 313 self.assertEqual(self.data.detector[0].orientation_unit, "degree") 235 314 self.assertAlmostEqual(self.data.detector[0].orientation.x, 1.0, 6) 236 315 self.assertEqual(self.data.detector[0].orientation.y, 0.0) 237 316 self.assertEqual(self.data.detector[0].orientation.z, 0.0) 238 317 239 318 self.assertEqual(self.data.detector[0].offset_unit, "m") 240 319 self.assertEqual(self.data.detector[0].offset.x, .001) 241 320 self.assertEqual(self.data.detector[0].offset.y, .002) 242 321 self.assertEqual(self.data.detector[0].offset.z, None) 243 322 244 323 self.assertEqual(self.data.detector[0].beam_center_unit, "mm") 245 324 self.assertEqual(self.data.detector[0].beam_center.x, 322.64) 246 325 self.assertEqual(self.data.detector[0].beam_center.y, 327.68) 247 326 self.assertEqual(self.data.detector[0].beam_center.z, None) 248 327 249 328 self.assertEqual(self.data.detector[0].pixel_size_unit, "mm") 250 329 self.assertEqual(self.data.detector[0].pixel_size.x, 5) 251 330 self.assertEqual(self.data.detector[0].pixel_size.y, 5) 252 331 self.assertEqual(self.data.detector[0].pixel_size.z, None) 253 332 254 333 # Process 255 334 _found_term1 = False … … 259 338 self.assertTrue(item.date in ['04-Sep-2007 18:35:02', 260 339 '03-SEP-2006 11:42:47']) 261 print item.term340 print(item.term) 262 341 for t in item.term: 263 if t['name']=="ABS:DSTAND" \264 and t['unit'] =='mm' \265 and float(t['value']) ==1.0:342 if (t['name'] == "ABS:DSTAND" 343 and t['unit'] == 'mm' 344 and float(t['value']) == 1.0): 266 345 _found_term2 = True 267 elif t['name']=="radialstep" \268 and t['unit']=='mm' \269 and float(t['value'])==10.0:346 elif (t['name'] == "radialstep" 347 and t['unit'] == 'mm' 348 and float(t['value']) == 10.0): 270 349 _found_term1 = True 271 272 if _found_term1 ==False or _found_term2==False:350 351 if _found_term1 == False or _found_term2 == False: 273 352 raise RuntimeError, "Could not find all process terms %s %s" % (_found_term1, _found_term2) 274 353 275 354 def test_writer(self): 276 from sas.sascalc.dataloader.readers.cansas_reader import Reader 277 r = Reader() 278 x = np.ones(5) 279 y = np.ones(5) 280 dy = np.ones(5) 281 355 r = CANSASReader() 356 282 357 filename = "write_test.xml" 283 358 r.write(filename, self.data) … … 286 361 self.assertEqual(self.data.filename, filename) 287 362 self._checkdata() 363 if os.path.isfile(filename): 364 os.remove(filename) 288 365 289 366 def test_units(self): … … 293 370 """ 294 371 filename = "cansas1d_units.xml" 295 data = Loader().load(filename)372 data = CANSASReader().read(filename) 296 373 self.data = data[0] 297 374 self.assertEqual(self.data.filename, filename) … … 304 381 """ 305 382 filename = "cansas1d_badunits.xml" 306 data = Loader().load(filename)383 data = CANSASReader().read(filename) 307 384 self.data = data[0] 308 385 self.assertEqual(self.data.filename, filename) … … 311 388 # This one should 312 389 self.assertAlmostEqual(self.data.sample.transmission, 0.327) 313 390 314 391 self.assertEqual(self.data.meta_data['loader'], "CanSAS XML 1D") 315 print self.data.errors392 print(self.data.errors) 316 393 self.assertEqual(len(self.data.errors), 1) 317 394 … … 321 398 """ 322 399 filename = "cansas1d_slit.xml" 323 data = Loader().load(filename)400 data = CANSASReader().read(filename) 324 401 self.data = data[0] 325 402 self.assertEqual(self.data.filename, filename) 326 403 self.assertEqual(self.data.run[0], "1234") 327 404 328 405 # Data 329 406 self.assertEqual(len(self.data.x), 2)
Note: See TracChangeset
for help on using the changeset viewer.