Changeset 5a8cdbb in sasview for test/sasdataloader
- Timestamp:
- Aug 1, 2017 12:02:35 PM (8 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. - Location:
- test/sasdataloader
- Files:
-
- 12 added
- 3 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasdataloader/plugins/test_reader.py
r959eb01 raaf5e49 8 8 copyright 2008, University of Tennessee 9 9 """ 10 from __future__ import print_function 11 10 12 import os 11 13 import numpy as np … … 59 61 reader = Reader() 60 62 output = reader.read("../test/test_data.test") 61 print output.x63 print(output.x) 62 64 63 65 -
test/sasdataloader/test/sequence_tests.py
r959eb01 raaf5e49 1 from __future__ import print_function 2 1 3 from DataLoader.loader import Loader 2 4 from DataLoader.readers.IgorReader import Reader as igor_reader … … 8 10 out2 = Loader().load("MAR07232_rest.ASC") 9 11 if len(out2.detector)>1: 10 print "Consecutive reads using Loader failed"12 print("Consecutive reads using Loader failed") 11 13 12 14 def consecutive_reader(): … … 14 16 out2 = igor_reader().read("MAR07232_rest.ASC") 15 17 if len(out2.detector)>1: 16 print "Consecutive reads using Readers failed"17 print out118 print out218 print("Consecutive reads using Readers failed") 19 print(out1) 20 print(out2) 19 21 20 22 def single_abs(): … … 29 31 single_igor() 30 32 single_abs() 31 print "Test passed"33 print("Test passed") -
test/sasdataloader/test/testplugings.py
r959eb01 r17c9436 3 3 """ 4 4 5 import os 5 6 import unittest 6 7 from sas.sascalc.dataloader.loader import Loader, Registry … … 41 42 # Create module 42 43 import zipfile 43 z = zipfile.PyZipFile("plugins.zip", 'w') 44 f_name = "plugins.zip" 45 z = zipfile.PyZipFile(f_name, 'w') 44 46 z.writepy("../plugins", "") 45 47 z.close() 48 if os.path.isfile(f_name): 49 os.remove(f_name) 46 50 47 51 def testplugin_checksetup(self): -
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) -
test/sasdataloader/test/utest_averaging.py
r8ffafd1 r5a8cdbb 1 1 2 2 import math 3 import os 3 4 import unittest 5 from scipy.stats import binned_statistic_2d 4 6 import numpy as np 7 5 8 import sas.sascalc.dataloader.data_info as data_info 6 9 from sas.sascalc.dataloader.loader import Loader 7 from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg, Ring, get_q,\ 8 CircularAverage, SectorPhi, SectorQ, reader2D_converter, SlabX, SlabY 10 from sas.sascalc.dataloader.manipulations import (Boxavg, Boxsum, 11 CircularAverage, Ring, 12 SectorPhi, SectorQ, SlabX, 13 SlabY, get_q, 14 reader2D_converter) 9 15 10 16 … … 13 19 Test averaging manipulations on a flat distribution 14 20 """ 21 15 22 def setUp(self): 16 23 """ … … 18 25 should return the predefined height of the distribution (1.0). 19 26 """ 20 x_0 = np.ones([100, 100])21 dx_0 = np.ones([100, 100])27 x_0 = np.ones([100, 100]) 28 dx_0 = np.ones([100, 100]) 22 29 23 30 self.data = data_info.Data2D(data=x_0, err_data=dx_0) 24 31 detector = data_info.Detector() 25 32 detector.distance = 1000.0 # mm 26 detector.pixel_size.x = 1.0 # mm27 detector.pixel_size.y = 1.0 # mm33 detector.pixel_size.x = 1.0 # mm 34 detector.pixel_size.y = 1.0 # mm 28 35 29 36 # center in pixel position = (len(x_0)-1)/2 30 detector.beam_center.x = (len(x_0) -1)/2# pixel number31 detector.beam_center.y = (len(x_0) -1)/2# pixel number37 detector.beam_center.x = (len(x_0) - 1) / 2 # pixel number 38 detector.beam_center.y = (len(x_0) - 1) / 2 # pixel number 32 39 self.data.detector.append(detector) 33 40 34 41 source = data_info.Source() 35 source.wavelength = 10.0 # A42 source.wavelength = 10.0 # A 36 43 self.data.source = source 37 44 38 # get_q(dx, dy, det_dist, wavelength) where units are mm, mm, mm, and A 45 # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A 46 # respectively. 39 47 self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 40 48 self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 41 49 42 50 self.qstep = len(x_0) 43 x = np.linspace(start= -1*self.qmax,44 stop=self.qmax,45 num=self.qstep,46 endpoint=True )47 y = np.linspace(start= -1*self.qmax,48 stop=self.qmax,49 num=self.qstep,50 endpoint=True)51 self.data.x_bins =x52 self.data.y_bins =y51 x = np.linspace(start=-1 * self.qmax, 52 stop=self.qmax, 53 num=self.qstep, 54 endpoint=True) 55 y = np.linspace(start=-1 * self.qmax, 56 stop=self.qmax, 57 num=self.qstep, 58 endpoint=True) 59 self.data.x_bins = x 60 self.data.y_bins = y 53 61 self.data = reader2D_converter(self.data) 54 62 … … 57 65 Test ring averaging 58 66 """ 59 r = Ring(r_min=2 *self.qmin, r_max=5*self.qmin,60 center_x=self.data.detector[0].beam_center.x, 67 r = Ring(r_min=2 * self.qmin, r_max=5 * self.qmin, 68 center_x=self.data.detector[0].beam_center.x, 61 69 center_y=self.data.detector[0].beam_center.y) 62 70 r.nbins_phi = 20 … … 70 78 Test sector averaging 71 79 """ 72 r = SectorPhi(r_min=self.qmin, r_max=3 *self.qmin,73 phi_min=0, phi_max=math.pi *2.0)80 r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, 81 phi_min=0, phi_max=math.pi * 2.0) 74 82 r.nbins_phi = 20 75 83 o = r(self.data) … … 81 89 """ 82 90 phi_max = math.pi * 1.5 83 r = SectorPhi(r_min=self.qmin, r_max=3 *self.qmin,91 r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, 84 92 phi_min=0, phi_max=phi_max) 85 93 self.assertEqual(r.phi_max, phi_max) … … 94 102 95 103 def setUp(self): 96 self.data = Loader().load('MAR07232_rest.ASC') 104 filepath = os.path.join(os.path.dirname( 105 os.path.realpath(__file__)), 'MAR07232_rest.ASC') 106 self.data = Loader().load(filepath) 97 107 98 108 def test_ring(self): … … 100 110 Test ring averaging 101 111 """ 102 r = Ring(r_min=.005, r_max=.01, 103 center_x=self.data.detector[0].beam_center.x, 112 r = Ring(r_min=.005, r_max=.01, 113 center_x=self.data.detector[0].beam_center.x, 104 114 center_y=self.data.detector[0].beam_center.y, 105 nbins = 20) 106 # r.nbins_phi = 20 107 108 o = r(self.data) 109 data_list = Loader().load('ring_testdata.txt') 110 answer = data_list[0] 115 nbins=20) 116 ##r.nbins_phi = 20 117 118 o = r(self.data) 119 filepath = os.path.join(os.path.dirname( 120 os.path.realpath(__file__)), 'ring_testdata.txt') 121 answer = Loader().load(filepath) 111 122 112 123 for i in range(r.nbins_phi - 1): … … 115 126 self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) 116 127 117 def test_circular_avg(self): 128 def test_circularavg(self): 129 """ 130 Test circular averaging 131 The test data was not generated by IGOR. 132 """ 133 r = CircularAverage(r_min=.00, r_max=.025, 134 bin_width=0.0003) 135 r.nbins_phi = 20 136 137 o = r(self.data) 138 139 filepath = os.path.join(os.path.dirname( 140 os.path.realpath(__file__)), 'avg_testdata.txt') 141 answer = Loader().load(filepath) 142 for i in range(r.nbins_phi): 143 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 144 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 145 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 146 147 def test_box(self): 118 148 """ 119 149 Test circular averaging 120 150 The test data was not generated by IGOR. 121 151 """ 122 r = CircularAverage(r_min=.00, r_max=.025, 123 bin_width=0.0003) 124 r.nbins_phi = 20 125 o = r(self.data) 126 127 output = Loader().load('avg_testdata.txt') 128 answer = output[0] 129 for i in range(r.nbins_phi): 130 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 131 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 132 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 133 134 def test_box(self): 135 """ 136 Test circular averaging 137 The test data was not generated by IGOR. 138 """ 152 139 153 r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 140 154 s, ds, npoints = r(self.data) 141 155 self.assertAlmostEqual(s, 34.278990899999997, 4) 142 156 self.assertAlmostEqual(ds, 7.8007981835194293, 4) 143 self.assertAlmostEqual(npoints, 324.0000, 4) 157 self.assertAlmostEqual(npoints, 324.0000, 4) 144 158 145 159 r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) … … 153 167 The test data was not generated by IGOR. 154 168 """ 155 r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004) 169 170 r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, 171 y_max=0.0002, bin_width=0.0004) 156 172 r.fold = False 157 173 o = r(self.data) 158 174 159 output = Loader().load('slabx_testdata.txt') 160 answer = output[0] 175 filepath = os.path.join(os.path.dirname( 176 os.path.realpath(__file__)), 'slabx_testdata.txt') 177 answer = Loader().load(filepath) 161 178 for i in range(len(o.x)): 162 179 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 169 186 The test data was not generated by IGOR. 170 187 """ 171 r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004) 188 189 r = SlabY(x_min=.005, x_max=.01, y_min=- 190 0.01, y_max=0.01, bin_width=0.0004) 172 191 r.fold = False 173 192 o = r(self.data) 174 193 175 output = Loader().load('slaby_testdata.txt') 176 answer = output[0] 194 filepath = os.path.join(os.path.dirname( 195 os.path.realpath(__file__)), 'slaby_testdata.txt') 196 answer = Loader().load(filepath) 177 197 for i in range(len(o.x)): 178 198 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 183 203 """ 184 204 Test sector averaging I(phi) 185 When considering the whole azimuthal range (2pi), 205 When considering the whole azimuthal range (2pi), 186 206 the answer should be the same as ring averaging. 187 207 The test data was not generated by IGOR. 188 208 """ 209 189 210 nbins = 19 190 211 phi_min = math.pi / (nbins + 1) … … 198 219 o = r(self.data) 199 220 200 output = Loader().load('ring_testdata.txt') 201 answer = output[0] 202 for i in range(len(o.x)): 203 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 204 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 205 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 206 221 filepath = os.path.join(os.path.dirname( 222 os.path.realpath(__file__)), 'ring_testdata.txt') 223 answer = Loader().load(filepath) 224 for i in range(len(o.x)): 225 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 226 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 227 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 228 207 229 def test_sectorphi_quarter(self): 208 230 """ … … 210 232 The test data was not generated by IGOR. 211 233 """ 212 r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) 213 r.nbins_phi = 20 214 o = r(self.data) 215 216 output = Loader().load('sectorphi_testdata.txt') 217 answer = output[0] 218 for i in range(len(o.x)): 219 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 220 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 221 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 222 234 235 r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) 236 r.nbins_phi = 20 237 o = r(self.data) 238 239 filepath = os.path.join(os.path.dirname( 240 os.path.realpath(__file__)), 'sectorphi_testdata.txt') 241 answer = Loader().load(filepath) 242 for i in range(len(o.x)): 243 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 244 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 245 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 246 223 247 def test_sectorq_full(self): 224 248 """ … … 226 250 The test data was not generated by IGOR. 227 251 """ 228 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) 229 r.nbins_phi = 20 230 o = r(self.data) 231 232 output = Loader().load('sectorq_testdata.txt') 233 answer = output[0] 234 for i in range(len(o.x)): 235 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 236 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 237 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 252 253 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) 254 r.nbins_phi = 20 255 o = r(self.data) 256 257 filepath = os.path.join(os.path.dirname( 258 os.path.realpath(__file__)), 'sectorq_testdata.txt') 259 answer = Loader().load(filepath) 260 for i in range(len(o.x)): 261 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 262 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 263 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 264 265 def test_sectorq_log(self): 266 """ 267 Test sector averaging I(q) 268 The test data was not generated by IGOR. 269 """ 270 271 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0, base=10) 272 r.nbins_phi = 20 273 o = r(self.data) 274 275 expected_binning = np.logspace(np.log10(0.005), np.log10(0.01), 20, base=10) 276 for i in range(len(o.x)): 277 self.assertAlmostEqual(o.x[i], expected_binning[i], 3) 278 279 # TODO: Test for Y values (o.y) 280 # print len(self.data.x_bins) 281 # print len(self.data.y_bins) 282 # print self.data.q_data.shape 283 # data_to_bin = np.array(self.data.q_data) 284 # data_to_bin = data_to_bin.reshape(len(self.data.x_bins), len(self.data.y_bins)) 285 # H, xedges, yedges, binnumber = binned_statistic_2d(self.data.x_bins, self.data.y_bins, data_to_bin, 286 # bins=[[0, math.pi / 2.0], expected_binning], statistic='mean') 287 # xedges_width = (xedges[1] - xedges[0]) 288 # xedges_center = xedges[1:] - xedges_width / 2 289 290 # yedges_width = (yedges[1] - yedges[0]) 291 # yedges_center = yedges[1:] - yedges_width / 2 292 293 # print H.flatten().shape 294 # print o.y.shape 238 295 239 296 -
test/sasdataloader/test/utest_cansas.py
r7432acb r1fc50fb2 46 46 self.schema_1_0 = "cansas1d_v1_0.xsd" 47 47 self.schema_1_1 = "cansas1d_v1_1.xsd" 48 48 self.write_1_0_filename = "isis_1_0_write_test.xml" 49 self.write_1_1_filename = "isis_1_1_write_test.xml" 49 50 50 51 def get_number_of_entries(self, dictionary, name, i): … … 56 57 return name 57 58 58 59 59 def test_invalid_xml(self): 60 60 """ … … 62 62 """ 63 63 invalid = StringIO.StringIO('<a><c></b></a>') 64 reader = XMLreader(invalid) 65 64 XMLreader(invalid) 66 65 67 66 def test_xml_validate(self): … … 82 81 self.assertFalse(xmlschema.validate(invalid)) 83 82 84 85 83 def test_real_xml(self): 86 84 reader = XMLreader(self.xml_valid, self.schema_1_0) … … 90 88 else: 91 89 self.assertFalse(valid) 92 93 90 94 91 def _check_data(self, data): … … 105 102 self.assertTrue(data.meta_data["xmlpreprocess"] is not None) 106 103 107 108 104 def _check_data_1_1(self, data): 109 105 spectrum = data.trans_spectrum[0] 110 106 self.assertTrue(len(spectrum.wavelength) == 138) 111 107 112 113 108 def test_cansas_xml(self): 114 filename = "isis_1_1_write_test.xml"115 109 xmlreader = XMLreader(self.isis_1_1, self.schema_1_1) 116 110 valid = xmlreader.validate_xml() 117 111 xmlreader.set_processing_instructions() 118 112 self.assertTrue(valid) 119 fo = open(self.isis_1_1)120 str = fo.read()121 113 reader_generic = Loader() 122 114 dataloader = reader_generic.load(self.isis_1_1) … … 128 120 self._check_data(cansasreader[i]) 129 121 self._check_data_1_1(cansasreader[i]) 130 reader_generic.save(filename, dataloader[i], None) 131 fo = open(filename) 132 str = fo.read() 122 reader_generic.save(self.write_1_1_filename, dataloader[i], None) 133 123 reader2 = Loader() 134 return_data = reader2.load(filename) 124 self.assertTrue(os.path.isfile(self.write_1_1_filename)) 125 return_data = reader2.load(self.write_1_1_filename) 135 126 written_data = return_data[0] 136 127 self._check_data(written_data) 137 128 if os.path.isfile(self.write_1_1_filename): 129 os.remove(self.write_1_1_filename) 138 130 139 131 def test_double_trans_spectra(self): … … 144 136 for item in data: 145 137 self._check_data(item) 146 147 138 148 139 def test_entry_name_recurse(self): … … 155 146 self.assertTrue(len(d) == 6) 156 147 157 158 148 def test_load_cansas_file(self): 159 valid = []160 149 reader1 = XMLreader(self.xml_valid, self.schema_1_0) 161 150 self.assertTrue(reader1.validate_xml()) … … 173 162 self.assertFalse(reader7.validate_xml()) 174 163 175 176 164 def test_invalid_cansas(self): 177 165 list = self.loader.load(self.cansas1d_notitle) … … 184 172 self.assertTrue(data.detector[0].distance == 4150) 185 173 186 187 174 def test_old_cansas_files(self): 188 175 reader1 = XMLreader(self.cansas1d, self.schema_1_0) 189 176 self.assertTrue(reader1.validate_xml()) 190 177 file_loader = Loader() 191 file 1 = file_loader.load(self.cansas1d)178 file_loader.load(self.cansas1d) 192 179 reader2 = XMLreader(self.cansas1d_units, self.schema_1_0) 193 180 self.assertTrue(reader2.validate_xml()) … … 197 184 self.assertTrue(reader4.validate_xml()) 198 185 199 200 186 def test_save_cansas_v1_0(self): 201 filename = "isis_1_0_write_test.xml"202 187 xmlreader = XMLreader(self.isis_1_0, self.schema_1_0) 203 188 valid = xmlreader.validate_xml() … … 210 195 self._check_data(dataloader[i]) 211 196 self._check_data(cansasreader[i]) 212 reader_generic.save( filename, dataloader[i], None)197 reader_generic.save(self.write_1_0_filename, dataloader[i], None) 213 198 reader2 = Reader() 214 return_data = reader2.read(filename) 199 self.assertTrue(os.path.isfile(self.write_1_0_filename)) 200 return_data = reader2.read(self.write_1_0_filename) 215 201 written_data = return_data[0] 216 xmlwrite = XMLreader(filename, self.schema_1_0)202 XMLreader(self.write_1_0_filename, self.schema_1_0) 217 203 valid = xmlreader.validate_xml() 218 204 self.assertTrue(valid) 219 205 self._check_data(written_data) 220 206 if os.path.isfile(self.write_1_0_filename): 207 os.remove(self.write_1_0_filename) 221 208 222 209 def test_processing_instructions(self): … … 224 211 valid = reader.validate_xml() 225 212 if valid: 226 # #find the processing instructions and make into a dictionary213 # find the processing instructions and make into a dictionary 227 214 dic = self.get_processing_instructions(reader) 228 215 self.assertTrue(dic == {'xml-stylesheet': \ … … 232 219 xmldoc = minidom.parseString(xml) 233 220 234 # #take the processing instructions and put them back in221 # take the processing instructions and put them back in 235 222 xmldoc = self.set_processing_instructions(xmldoc, dic) 236 xml_output = xmldoc.toprettyxml() 237 223 xmldoc.toprettyxml() 238 224 239 225 def set_processing_instructions(self, minidom_object, dic): … … 243 229 minidom_object.insertBefore(pi, xmlroot) 244 230 return minidom_object 245 246 231 247 232 def get_processing_instructions(self, xml_reader_object): -
test/sasdataloader/test/utest_ascii.py
r959eb01 rad92c5a 6 6 7 7 import unittest 8 from sas.sascalc.dataloader.loader import Loader 9 10 import os.path 8 from sas.sascalc.dataloader.loader import Loader 11 9 12 class abs_reader(unittest.TestCase): 10 11 class ABSReaderTests(unittest.TestCase): 13 12 14 13 def setUp(self): 15 14 self.loader = Loader() 16 15 self.f1_list = self.loader.load("ascii_test_1.txt") 16 self.f1 = self.f1_list[0] 17 self.f2_list = self.loader.load("ascii_test_2.txt") 18 self.f2 = self.f2_list[0] 19 self.f3_list = self.loader.load("ascii_test_3.txt") 20 self.f3 = self.f3_list[0] 21 self.f4_list = self.loader.load("ascii_test_4.abs") 22 self.f4 = self.f4_list[0] 23 self.f5_list = self.loader.load("ascii_test_5.txt") 24 self.f5 = self.f5_list[0] 25 17 26 def test_checkdata(self): 18 27 """ 19 28 Test .ABS file loaded as ascii 20 29 """ 21 f = self.loader.load("ascii_test_1.txt")22 30 # The length of the data is 10 23 self.assertEqual(len( f.x), 10)24 self.assertEqual( f.x[0],0.002618)25 self.assertEqual( f.x[9],0.0497)26 self.assertEqual( f.x_unit, '1/A')27 self.assertEqual( f.y_unit, '1/cm')31 self.assertEqual(len(self.f1.x), 10) 32 self.assertEqual(self.f1.x[0],0.002618) 33 self.assertEqual(self.f1.x[9],0.0497) 34 self.assertEqual(self.f1.x_unit, '1/A') 35 self.assertEqual(self.f1.y_unit, '1/cm') 28 36 29 self.assertEqual( f.meta_data['loader'],"ASCII")30 37 self.assertEqual(self.f1.meta_data['loader'],"ASCII") 38 31 39 def test_truncated_1(self): 32 40 """ … … 38 46 as though it were the start of a footer). 39 47 """ 40 # Test .ABS file loaded as ascii 41 f = self.loader.load("ascii_test_2.txt") 42 # The length of the data is 10 43 self.assertEqual(len(f.x), 5) 44 self.assertEqual(f.x[0],0.002618) 45 self.assertEqual(f.x[4],0.02356) 46 48 # The length of the data is 5 49 self.assertEqual(len(self.f2.x), 5) 50 self.assertEqual(self.f2.x[0],0.002618) 51 self.assertEqual(self.f2.x[4],0.02356) 52 47 53 def test_truncated_2(self): 48 54 """ … … 52 58 reading at the first inconsitent line. 53 59 """ 54 # Test .ABS file loaded as ascii55 f = self.loader.load("ascii_test_3.txt")56 60 # The length of the data is 5 57 self.assertEqual(len( f.x), 5)58 self.assertEqual( f.x[0],0.002618)59 self.assertEqual( f.x[4],0.02356)60 61 self.assertEqual(len(self.f3.x), 5) 62 self.assertEqual(self.f3.x[0],0.002618) 63 self.assertEqual(self.f3.x[4],0.02356) 64 61 65 def test_truncated_3(self): 62 66 """ … … 66 70 reading at the last line of header. 67 71 """ 68 # Test .ABS file loaded as ascii69 f = self.loader.load("ascii_test_4.abs")70 72 # The length of the data is 5 71 self.assertEqual(len( f.x), 5)72 self.assertEqual( f.x[0],0.012654)73 self.assertEqual( f.x[4],0.02654)74 73 self.assertEqual(len(self.f4.x), 5) 74 self.assertEqual(self.f4.x[0],0.012654) 75 self.assertEqual(self.f4.x[4],0.02654) 76 75 77 def test_truncated_4(self): 76 78 """ … … 78 80 Only the last 5 2-col lines should be read. 79 81 """ 80 # Test .ABS file loaded as ascii81 f = self.loader.load("ascii_test_5.txt")82 82 # The length of the data is 5 83 self.assertEqual(len( f.x), 5)84 self.assertEqual( f.x[0],0.02879)85 self.assertEqual( f.x[4],0.0497)86 83 self.assertEqual(len(self.f5.x), 5) 84 self.assertEqual(self.f5.x[0],0.02879) 85 self.assertEqual(self.f5.x[4],0.0497) 86 87 87 def test_truncated_5(self): 88 88 """ 89 Test a 6-col ascii file with complex header where one of them has a letter and90 many lines with 2 or 2 columns in the middle of the data section.91 Only last four lines should be read.89 Test a 6-col ascii file with complex header where one of them has a 90 letter and many lines with 2 or 2 columns in the middle of the data 91 section. Will be rejected because fewer than 5 lines. 92 92 """ 93 93 # Test .ABS file loaded as ascii … … 98 98 except: 99 99 self.assertEqual(f, None) 100 100 101 101 if __name__ == '__main__': 102 102 unittest.main() -
test/sasdataloader/test/utest_red2d_reader.py
r959eb01 r248ff73 7 7 import unittest 8 8 from sas.sascalc.dataloader.loader import Loader 9 9 10 10 import os.path 11 11 12 12 class abs_reader(unittest.TestCase): 13 13 14 14 def setUp(self): 15 15 self.loader = Loader() 16 16 17 17 def test_checkdata(self): 18 18 """ 19 19 Test .DAT file loaded as IGOR/DAT 2D Q_map 20 20 """ 21 f = self.loader.load("exp18_14_igor_2dqxqy.dat") 21 f = self.loader.load("exp18_14_igor_2dqxqy.dat")[0] 22 22 # The length of the data is 10 23 23 self.assertEqual(len(f.qx_data), 36864) … … 26 26 self.assertEqual(f.Q_unit, '1/A') 27 27 self.assertEqual(f.I_unit, '1/cm') 28 28 29 29 self.assertEqual(f.meta_data['loader'],"IGOR/DAT 2D Q_map") 30 31 30 31 32 32 if __name__ == '__main__': 33 33 unittest.main() 34
Note: See TracChangeset
for help on using the changeset viewer.