Changeset f001bc9 in sasview for test


Ignore:
Timestamp:
Aug 22, 2017 2:55:17 PM (7 years ago)
Author:
GitHub <noreply@…>
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:
fca1f50, 17e257b5, f9ba422, 783c1b5
Parents:
a06ee7e (diff), dcb91cf (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.
git-author:
Jeff Krzywon <krzywon@…> (08/22/17 14:55:17)
git-committer:
GitHub <noreply@…> (08/22/17 14:55:17)
Message:

Merge pull request #95 from lewisodriscoll/ticket-876

Refactor File Loaders - fixes #889 #950 #849 #876 #922 #983 #970

Location:
test
Files:
4 added
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • test/fileconverter/test/cansas1d.xml

    rc476457 rdcb91cf  
    2626                                <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 
    2727                        </Idata> 
     28      <Idata> 
     29                                <Q unit="1/A">0.04</Q> 
     30                                <I unit="1/cm">1002</I> 
     31                                <Idev unit="1/cm">4</Idev> 
     32                                <Qdev unit="1/A">0.02</Qdev> 
     33                                <Qmean unit="1/A"><!-- Qmean is optional --></Qmean> 
     34                                <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 
     35                        </Idata> 
     36      <Idata> 
     37                                <Q unit="1/A">0.05</Q> 
     38                                <I unit="1/cm">1003</I> 
     39                                <Idev unit="1/cm">4</Idev> 
     40                                <Qdev unit="1/A">0.02</Qdev> 
     41                                <Qmean unit="1/A"><!-- Qmean is optional --></Qmean> 
     42                                <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 
     43                        </Idata> 
     44      <Idata> 
     45                                <Q unit="1/A">0.06</Q> 
     46                                <I unit="1/cm">1004</I> 
     47                                <Idev unit="1/cm">4</Idev> 
     48                                <Qdev unit="1/A">0.02</Qdev> 
     49                                <Qmean unit="1/A"><!-- Qmean is optional --></Qmean> 
     50                                <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 
     51                        </Idata> 
    2852                </SASdata> 
    2953                <SASsample name='my sample'> 
     
    4771                                Some text here 
    4872                        </details> 
    49                          
     73 
    5074                </SASsample> 
    5175                <SASinstrument> 
  • test/fileconverter/test/utest_nxcansas_writer.py

    r17c9436 r5a8cdbb  
    11from sas.sascalc.file_converter.nxcansas_writer import NXcanSASWriter 
    2 from sas.sascalc.dataloader.readers.cansas_reader import Reader as XMLReader 
    3 from sas.sascalc.dataloader.readers.red2d_reader import Reader as DATReader 
    4 from sas.sascalc.dataloader.readers.cansas_reader_HDF5 import Reader as HDF5Reader 
     2from sas.sascalc.dataloader.loader import Loader 
    53 
    64import os 
     
    1412 
    1513    def setUp(self): 
     14        self.loader = Loader() 
    1615        self.writer = NXcanSASWriter() 
    1716        self.read_file_1d = "cansas1d.xml" 
     
    1918        self.read_file_2d = "exp18_14_igor_2dqxqy.dat" 
    2019        self.write_file_2d = "export2d.h5" 
    21         self.hdf5_reader = HDF5Reader() 
    2220 
    23         xml_reader = XMLReader() 
    24         self.data_1d = xml_reader.read(self.read_file_1d)[0] 
     21        self.data_1d = self.loader.load(self.read_file_1d)[0] 
    2522 
    26         dat_reader = DATReader() 
    27         self.data_2d = dat_reader.read(self.read_file_2d) 
     23        self.data_2d = self.loader.load(self.read_file_2d)[0] 
    2824        self.data_2d.detector[0].name = '' 
    2925        self.data_2d.source.radiation = 'neutron' 
     
    3127    def test_write_1d(self): 
    3228        self.writer.write([self.data_1d], self.write_file_1d) 
    33         data = self.hdf5_reader.read(self.write_file_1d) 
     29        data = self.loader.load(self.write_file_1d) 
    3430        self.assertTrue(len(data) == 1) 
    3531        data = data[0] 
     
    4137    def test_write_2d(self): 
    4238        self.writer.write([self.data_2d], self.write_file_2d) 
    43         data = self.hdf5_reader.read(self.write_file_2d) 
     39        data = self.loader.load(self.write_file_2d) 
    4440        self.assertTrue(len(data) == 1) 
    4541        data = data[0] 
  • test/sascalculator/test/utest_slit_length_calculator.py

    r959eb01 rb09095a  
    55import unittest 
    66from sas.sascalc.dataloader.readers.ascii_reader import Reader 
    7 from  sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator as calculator 
     7from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator \ 
     8    as calculator 
    89 
    9 import os.path 
    1010 
    11 class slit_calculator(unittest.TestCase): 
     11class SlitCalculator(unittest.TestCase): 
    1212     
    1313    def setUp(self): 
     
    1515        self.reader = Reader() 
    1616         
    17     def test_slitlength_calculation(self): 
     17    def test_slit_length_calculation(self): 
    1818        """ 
    1919            Test slit_length_calculator" 
    2020        """ 
    21         f = self.reader.read("beam profile.DAT") 
     21        list = self.reader.read("beam profile.DAT") 
     22        self.assertTrue(len(list) == 1) 
     23        f = list[0] 
    2224        cal = calculator() 
    2325        cal.set_data(f.x,f.y) 
    24         slitlength = cal.calculate_slit_length() 
     26        slit_length = cal.calculate_slit_length() 
    2527         
    2628        # The value "5.5858" was obtained by manual calculation. 
    2729        # It turns out our slit length is FWHM/2 
    28         self.assertAlmostEqual(slitlength,5.5858/2, 3) 
     30        self.assertAlmostEqual(slit_length, 5.5858/2, 3) 
    2931         
    3032         
  • test/sasdataloader/test/utest_abs_reader.py

    rc551bb3 rce8c7bd  
    55 
    66import unittest 
    7 import math 
    87import numpy as np 
    98from sas.sascalc.dataloader.loader import Loader 
    10 from sas.sascalc.dataloader.readers.IgorReader import Reader as IgorReader 
    119from sas.sascalc.dataloader.readers.abs_reader import Reader as AbsReader 
    12 from sas.sascalc.dataloader.readers.hfir1d_reader import Reader as HFIRReader 
    1310from sas.sascalc.dataloader.readers.danse_reader import Reader as DANSEReader 
    1411from sas.sascalc.dataloader.readers.cansas_reader import Reader as CANSASReader 
    1512 
    1613from sas.sascalc.dataloader.data_info import Data1D 
    17   
     14 
    1815import os.path 
    1916 
    2017 
    2118class abs_reader(unittest.TestCase): 
    22      
     19 
    2320    def setUp(self): 
    2421        reader = AbsReader() 
     
    2724    def test_abs_checkdata(self): 
    2825        """ 
    29             Check the data content to see whether  
     26            Check the data content to see whether 
    3027            it matches the specific file we loaded. 
    3128            Check the units too to see whether the 
     
    3532        self.assertEqual(self.data.filename, "jan08002.ABS") 
    3633        self.assertEqual(self.data.meta_data['loader'], "IGOR 1D") 
    37          
     34 
    3835        self.assertEqual(self.data.source.wavelength_unit, 'A') 
    3936        self.assertEqual(self.data.source.wavelength, 6.0) 
    40          
     37 
    4138        self.assertEqual(self.data.detector[0].distance_unit, 'mm') 
    4239        self.assertEqual(self.data.detector[0].distance, 1000.0) 
    43          
     40 
    4441        self.assertEqual(self.data.sample.transmission, 0.5667) 
    45          
     42 
    4643        self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') 
    47         center_x = 114.58*5.0 
    48         center_y = 64.22*5.0 
     44        center_x = 114.58*5.08 
     45        center_y = 64.22*5.08 
    4946        self.assertEqual(self.data.detector[0].beam_center.x, center_x) 
    5047        self.assertEqual(self.data.detector[0].beam_center.y, center_y) 
    51          
     48 
    5249        self.assertEqual(self.data.y_unit, '1/cm') 
    5350        self.assertEqual(self.data.x[0], 0.002618) 
     
    5552        self.assertEqual(self.data.x[2], 0.01309) 
    5653        self.assertEqual(self.data.x[126], 0.5828) 
    57          
     54 
    5855        self.assertEqual(self.data.y[0], 0.02198) 
    5956        self.assertEqual(self.data.y[1], 0.02201) 
    6057        self.assertEqual(self.data.y[2], 0.02695) 
    6158        self.assertEqual(self.data.y[126], 0.2958) 
    62          
     59 
    6360        self.assertEqual(self.data.dy[0], 0.002704) 
    6461        self.assertEqual(self.data.dy[1], 0.001643) 
    6562        self.assertEqual(self.data.dy[2], 0.002452) 
    6663        self.assertEqual(self.data.dy[126], 1) 
    67          
     64 
    6865    def test_checkdata2(self): 
    6966        self.assertEqual(self.data.dy[126], 1) 
     
    7471        self.assertEqual(data.meta_data['loader'], "IGOR 1D") 
    7572 
    76  
    77 class hfir_reader(unittest.TestCase): 
     73class DanseReaderTests(unittest.TestCase): 
    7874 
    7975    def setUp(self): 
    80         reader = HFIRReader() 
    81         self.data = reader.read("S2-30dq.d1d") 
    82  
    83     def test_hfir_checkdata(self): 
    84         """ 
    85             Check the data content to see whether  
    86             it matches the specific file we loaded. 
    87         """ 
    88         self.assertEqual(self.data.filename, "S2-30dq.d1d") 
    89         # THIS FILE FORMAT IS CURRENTLY READ BY THE ASCII READER 
    90         self.assertEqual(self.data.meta_data['loader'], "HFIR 1D") 
    91         self.assertEqual(len(self.data.x), 134) 
    92         self.assertEqual(len(self.data.y), 134) 
    93         #          Q           I               dI          dQ   
    94         # Point 1: 0.003014    0.003010        0.000315    0.008249 
    95         self.assertEqual(self.data.x[1], 0.003014) 
    96         self.assertEqual(self.data.y[1], 0.003010) 
    97         self.assertEqual(self.data.dy[1], 0.000315) 
    98         self.assertEqual(self.data.dx[1], 0.008249) 
    99  
    100     def test_generic_loader(self): 
    101         # the generic loader should work as well 
    102         data = Loader().load("S2-30dq.d1d") 
    103         self.assertEqual(data.meta_data['loader'], "HFIR 1D") 
    104  
    105  
    106 class igor_reader(unittest.TestCase): 
    107      
    108     def setUp(self): 
    109         # the IgorReader should be able to read this filetype 
    110         # if it can't, stop here. 
    111         reader = IgorReader() 
    112         self.data = reader.read("MAR07232_rest.ASC") 
    113  
    114     def test_igor_checkdata(self): 
    115         """ 
    116             Check the data content to see whether  
     76        reader = DANSEReader() 
     77        self.data = reader.read("MP_New.sans") 
     78 
     79    def test_checkdata(self): 
     80        """ 
     81            Check the data content to see whether 
    11782            it matches the specific file we loaded. 
    11883            Check the units too to see whether the 
     
    12085            tests won't pass 
    12186        """ 
    122         self.assertEqual(self.data.filename, "MAR07232_rest.ASC") 
    123         self.assertEqual(self.data.meta_data['loader'], "IGOR 2D") 
    124          
    125         self.assertEqual(self.data.source.wavelength_unit, 'A') 
    126         self.assertEqual(self.data.source.wavelength, 8.4) 
    127          
    128         self.assertEqual(self.data.detector[0].distance_unit, 'mm') 
    129         self.assertEqual(self.data.detector[0].distance, 13705) 
    130          
    131         self.assertEqual(self.data.sample.transmission, 0.84357) 
    132          
    133         self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') 
    134         center_x = (68.76 - 1)*5.0 
    135         center_y = (62.47 - 1)*5.0 
    136         self.assertEqual(self.data.detector[0].beam_center.x, center_x) 
    137         self.assertEqual(self.data.detector[0].beam_center.y, center_y) 
    138          
    139         self.assertEqual(self.data.I_unit, '1/cm') 
    140         # 3 points should be suffcient to check that the data is in column 
    141         # major order. 
    142         np.testing.assert_almost_equal(self.data.data[0:3], 
    143                                        [0.279783, 0.28951, 0.167634]) 
    144         np.testing.assert_almost_equal(self.data.qx_data[0:3], 
    145                                        [-0.01849072, -0.01821785, -0.01794498]) 
    146         np.testing.assert_almost_equal(self.data.qy_data[0:3], 
    147                                        [-0.01677435, -0.01677435, -0.01677435]) 
    148  
    149     def test_generic_loader(self): 
    150         # the generic loader should direct the file to IgorReader as well 
    151         data = Loader().load("MAR07232_rest.ASC") 
    152         self.assertEqual(data.meta_data['loader'], "IGOR 2D") 
    153  
    154  
    155 class danse_reader(unittest.TestCase): 
    156      
    157     def setUp(self): 
    158         reader = DANSEReader() 
    159         self.data = reader.read("MP_New.sans") 
    160  
    161     def test_checkdata(self): 
    162         """ 
    163             Check the data content to see whether  
    164             it matches the specific file we loaded. 
    165             Check the units too to see whether the 
    166             Data1D defaults changed. Otherwise the 
    167             tests won't pass 
    168         """ 
    16987        self.assertEqual(self.data.filename, "MP_New.sans") 
    17088        self.assertEqual(self.data.meta_data['loader'], "DANSE") 
    171          
     89 
    17290        self.assertEqual(self.data.source.wavelength_unit, 'A') 
    17391        self.assertEqual(self.data.source.wavelength, 7.5) 
    174          
     92 
    17593        self.assertEqual(self.data.detector[0].distance_unit, 'mm') 
    17694        self.assertAlmostEqual(self.data.detector[0].distance, 5414.99, 3) 
    177          
     95 
    17896        self.assertEqual(self.data.detector[0].beam_center_unit, 'mm') 
    17997        center_x = 68.74*5.0 
     
    18199        self.assertEqual(self.data.detector[0].beam_center.x, center_x) 
    182100        self.assertEqual(self.data.detector[0].beam_center.y, center_y) 
    183          
     101 
    184102        self.assertEqual(self.data.I_unit, '1/cm') 
    185103        self.assertEqual(self.data.data[0], 1.57831) 
     
    196114        self.assertEqual(data.meta_data['loader'], "DANSE") 
    197115 
    198   
     116 
    199117class cansas_reader(unittest.TestCase): 
    200      
     118 
    201119    def setUp(self): 
    202120        reader = CANSASReader() 
     
    212130        self.assertEqual(self.data.filename, "cansas1d.xml") 
    213131        self._checkdata() 
    214          
     132 
    215133    def _checkdata(self): 
    216134        """ 
    217             Check the data content to see whether  
     135            Check the data content to see whether 
    218136            it matches the specific file we loaded. 
    219137            Check the units too to see whether the 
     
    223141        self.assertEqual(self.data.run[0], "1234") 
    224142        self.assertEqual(self.data.meta_data['loader'], "CanSAS XML 1D") 
    225          
     143 
    226144        # Data 
    227145        self.assertEqual(len(self.data.x), 2) 
     
    238156        self.assertEqual(self.data.run_name['1234'], 'run name') 
    239157        self.assertEqual(self.data.title, "Test title") 
    240          
     158 
    241159        # Sample info 
    242160        self.assertEqual(self.data.sample.ID, "SI600-new-long") 
     
    244162        self.assertEqual(self.data.sample.thickness_unit, 'mm') 
    245163        self.assertAlmostEqual(self.data.sample.thickness, 1.03) 
    246          
     164 
    247165        self.assertAlmostEqual(self.data.sample.transmission, 0.327) 
    248          
     166 
    249167        self.assertEqual(self.data.sample.temperature_unit, 'C') 
    250168        self.assertEqual(self.data.sample.temperature, 0) 
     
    258176        self.assertAlmostEqual(self.data.sample.orientation.y, 0.02, 6) 
    259177 
    260         self.assertEqual(self.data.sample.details[0], "http://chemtools.chem.soton.ac.uk/projects/blog/blogs.php/bit_id/2720")  
    261         self.assertEqual(self.data.sample.details[1], "Some text here")  
    262          
     178        self.assertEqual(self.data.sample.details[0], "http://chemtools.chem.soton.ac.uk/projects/blog/blogs.php/bit_id/2720") 
     179        self.assertEqual(self.data.sample.details[1], "Some text here") 
     180 
    263181        # Instrument info 
    264182        self.assertEqual(self.data.instrument, "canSAS instrument") 
    265          
     183 
    266184        # Source 
    267185        self.assertEqual(self.data.source.radiation, "neutron") 
    268          
     186 
    269187        self.assertEqual(self.data.source.beam_size_unit, "mm") 
    270188        self.assertEqual(self.data.source.beam_size_name, "bm") 
    271189        self.assertEqual(self.data.source.beam_size.x, 12) 
    272190        self.assertEqual(self.data.source.beam_size.y, 13) 
    273          
     191 
    274192        self.assertEqual(self.data.source.beam_shape, "disc") 
    275          
     193 
    276194        self.assertEqual(self.data.source.wavelength_unit, "A") 
    277195        self.assertEqual(self.data.source.wavelength, 6) 
    278          
     196 
    279197        self.assertEqual(self.data.source.wavelength_max_unit, "nm") 
    280198        self.assertAlmostEqual(self.data.source.wavelength_max, 1.0) 
     
    283201        self.assertEqual(self.data.source.wavelength_spread_unit, "percent") 
    284202        self.assertEqual(self.data.source.wavelength_spread, 14.3) 
    285          
     203 
    286204        # Collimation 
    287205        _found1 = False 
     
    289207        self.assertEqual(self.data.collimation[0].length, 123.) 
    290208        self.assertEqual(self.data.collimation[0].name, 'test coll name') 
    291          
     209 
    292210        for item in self.data.collimation[0].aperture: 
    293211            self.assertEqual(item.size_unit,'mm') 
     
    303221                and item.type == 'radius': 
    304222                _found2 = True 
    305                  
     223 
    306224        if _found1 == False or _found2 == False: 
    307             raise RuntimeError, "Could not find all data %s %s" % (_found1, _found2)  
    308              
     225            raise RuntimeError, "Could not find all data %s %s" % (_found1, _found2) 
     226 
    309227        # Detector 
    310228        self.assertEqual(self.data.detector[0].name, "fictional hybrid") 
    311229        self.assertEqual(self.data.detector[0].distance_unit, "mm") 
    312230        self.assertEqual(self.data.detector[0].distance, 4150) 
    313          
     231 
    314232        self.assertEqual(self.data.detector[0].orientation_unit, "degree") 
    315233        self.assertAlmostEqual(self.data.detector[0].orientation.x, 1.0, 6) 
    316234        self.assertEqual(self.data.detector[0].orientation.y, 0.0) 
    317235        self.assertEqual(self.data.detector[0].orientation.z, 0.0) 
    318          
     236 
    319237        self.assertEqual(self.data.detector[0].offset_unit, "m") 
    320238        self.assertEqual(self.data.detector[0].offset.x, .001) 
    321239        self.assertEqual(self.data.detector[0].offset.y, .002) 
    322240        self.assertEqual(self.data.detector[0].offset.z, None) 
    323          
     241 
    324242        self.assertEqual(self.data.detector[0].beam_center_unit, "mm") 
    325243        self.assertEqual(self.data.detector[0].beam_center.x, 322.64) 
    326244        self.assertEqual(self.data.detector[0].beam_center.y, 327.68) 
    327245        self.assertEqual(self.data.detector[0].beam_center.z, None) 
    328          
     246 
    329247        self.assertEqual(self.data.detector[0].pixel_size_unit, "mm") 
    330248        self.assertEqual(self.data.detector[0].pixel_size.x, 5) 
    331249        self.assertEqual(self.data.detector[0].pixel_size.y, 5) 
    332250        self.assertEqual(self.data.detector[0].pixel_size.z, None) 
    333          
     251 
    334252        # Process 
    335253        _found_term1 = False 
     
    349267                      and float(t['value']) == 10.0): 
    350268                    _found_term1 = True 
    351                      
     269 
    352270        if _found_term1 == False or _found_term2 == False: 
    353271            raise RuntimeError, "Could not find all process terms %s %s" % (_found_term1, _found_term2) 
    354          
     272 
    355273    def test_writer(self): 
    356274        r = CANSASReader() 
     
    364282        if os.path.isfile(filename): 
    365283            os.remove(filename) 
    366          
     284 
    367285    def test_units(self): 
    368286        """ 
     
    375293        self.assertEqual(self.data.filename, filename) 
    376294        self._checkdata() 
    377          
     295 
    378296    def test_badunits(self): 
    379297        """ 
     
    389307        # This one should 
    390308        self.assertAlmostEqual(self.data.sample.transmission, 0.327) 
    391          
     309 
    392310        self.assertEqual(self.data.meta_data['loader'], "CanSAS XML 1D") 
    393311        print(self.data.errors) 
     
    403321        self.assertEqual(self.data.filename, filename) 
    404322        self.assertEqual(self.data.run[0], "1234") 
    405          
     323 
    406324        # Data 
    407325        self.assertEqual(len(self.data.x), 2) 
  • test/sasdataloader/test/utest_ascii.py

    r959eb01 rad92c5a  
    66 
    77import unittest 
    8 from sas.sascalc.dataloader.loader import  Loader 
    9   
    10 import os.path 
     8from sas.sascalc.dataloader.loader import Loader 
    119 
    12 class abs_reader(unittest.TestCase): 
     10 
     11class ABSReaderTests(unittest.TestCase): 
    1312     
    1413    def setUp(self): 
    1514        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 
    1726    def test_checkdata(self): 
    1827        """ 
    1928            Test .ABS file loaded as ascii 
    2029        """ 
    21         f = self.loader.load("ascii_test_1.txt") 
    2230        # 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') 
    2836         
    29         self.assertEqual(f.meta_data['loader'],"ASCII") 
    30          
     37        self.assertEqual(self.f1.meta_data['loader'],"ASCII") 
     38 
    3139    def test_truncated_1(self): 
    3240        """ 
     
    3846            as though it were the start of a footer). 
    3947        """ 
    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 
    4753    def test_truncated_2(self): 
    4854        """ 
     
    5258            reading at the first inconsitent line. 
    5359        """ 
    54         # Test .ABS file loaded as ascii 
    55         f = self.loader.load("ascii_test_3.txt") 
    5660        # 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 
    6165    def test_truncated_3(self): 
    6266        """ 
     
    6670            reading at the last line of header. 
    6771        """ 
    68         # Test .ABS file loaded as ascii 
    69         f = self.loader.load("ascii_test_4.abs") 
    7072        # 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 
    7577    def test_truncated_4(self): 
    7678        """ 
     
    7880            Only the last 5 2-col lines should be read. 
    7981        """ 
    80         # Test .ABS file loaded as ascii 
    81         f = self.loader.load("ascii_test_5.txt") 
    8282        # 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 
    8787    def test_truncated_5(self): 
    8888        """ 
    89             Test a 6-col ascii file with complex header where one of them has a letter and  
    90             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. 
    9292        """ 
    9393        # Test .ABS file loaded as ascii 
     
    9898        except: 
    9999            self.assertEqual(f, None) 
    100          
     100 
    101101if __name__ == '__main__': 
    102102    unittest.main() 
  • test/sasdataloader/test/utest_averaging.py

    re123eb9 r2a52b0e  
    4646        # respectively. 
    4747        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 
    48  
    4948        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 
    5049 
     
    104103    def setUp(self): 
    105104        filepath = os.path.join(os.path.dirname( 
    106             os.path.realpath(__file__)), 'MAR07232_rest.ASC') 
    107         self.data = Loader().load(filepath) 
     105            os.path.realpath(__file__)), 'MAR07232_rest.h5') 
     106        self.data = Loader().load(filepath)[0] 
    108107 
    109108    def test_ring(self): 
     
    120119        filepath = os.path.join(os.path.dirname( 
    121120            os.path.realpath(__file__)), 'ring_testdata.txt') 
    122         answer = Loader().load(filepath) 
     121        answer = Loader().load(filepath)[0] 
    123122 
    124123        for i in range(r.nbins_phi - 1): 
     
    140139        filepath = os.path.join(os.path.dirname( 
    141140            os.path.realpath(__file__)), 'avg_testdata.txt') 
    142         answer = Loader().load(filepath) 
     141        answer = Loader().load(filepath)[0] 
    143142        for i in range(r.nbins_phi): 
    144143            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    176175        filepath = os.path.join(os.path.dirname( 
    177176            os.path.realpath(__file__)), 'slabx_testdata.txt') 
    178         answer = Loader().load(filepath) 
     177        answer = Loader().load(filepath)[0] 
    179178        for i in range(len(o.x)): 
    180179            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    195194        filepath = os.path.join(os.path.dirname( 
    196195            os.path.realpath(__file__)), 'slaby_testdata.txt') 
    197         answer = Loader().load(filepath) 
     196        answer = Loader().load(filepath)[0] 
    198197        for i in range(len(o.x)): 
    199198            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    204203        """ 
    205204            Test sector averaging I(phi) 
    206             When considering the whole azimuthal range (2pi),  
     205            When considering the whole azimuthal range (2pi), 
    207206            the answer should be the same as ring averaging. 
    208207            The test data was not generated by IGOR. 
     
    222221        filepath = os.path.join(os.path.dirname( 
    223222            os.path.realpath(__file__)), 'ring_testdata.txt') 
    224         answer = Loader().load(filepath) 
     223        answer = Loader().load(filepath)[0] 
    225224        for i in range(len(o.x)): 
    226225            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    240239        filepath = os.path.join(os.path.dirname( 
    241240            os.path.realpath(__file__)), 'sectorphi_testdata.txt') 
    242         answer = Loader().load(filepath) 
     241        answer = Loader().load(filepath)[0] 
    243242        for i in range(len(o.x)): 
    244243            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    258257        filepath = os.path.join(os.path.dirname( 
    259258            os.path.realpath(__file__)), 'sectorq_testdata.txt') 
    260         answer = Loader().load(filepath) 
     259        answer = Loader().load(filepath)[0] 
    261260        for i in range(len(o.x)): 
    262261            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    277276        for i in range(len(o.x)): 
    278277            self.assertAlmostEqual(o.x[i], expected_binning[i], 3) 
    279          
     278 
    280279        # TODO: Test for Y values (o.y) 
    281280        # print len(self.data.x_bins) 
     
    288287        # xedges_width = (xedges[1] - xedges[0]) 
    289288        # xedges_center = xedges[1:] - xedges_width / 2 
    290          
     289 
    291290        # yedges_width = (yedges[1] - yedges[0]) 
    292291        # yedges_center = yedges[1:] - yedges_width / 2 
    293          
     292 
    294293        # print H.flatten().shape 
    295294        # print o.y.shape 
    296          
     295 
    297296 
    298297if __name__ == '__main__': 
  • test/sasdataloader/test/utest_red2d_reader.py

    r959eb01 r248ff73  
    77import unittest 
    88from sas.sascalc.dataloader.loader import  Loader 
    9   
     9 
    1010import os.path 
    1111 
    1212class abs_reader(unittest.TestCase): 
    13      
     13 
    1414    def setUp(self): 
    1515        self.loader = Loader() 
    16          
     16 
    1717    def test_checkdata(self): 
    1818        """ 
    1919            Test .DAT file loaded as IGOR/DAT 2D Q_map 
    2020        """ 
    21         f = self.loader.load("exp18_14_igor_2dqxqy.dat") 
     21        f = self.loader.load("exp18_14_igor_2dqxqy.dat")[0] 
    2222        # The length of the data is 10 
    2323        self.assertEqual(len(f.qx_data),  36864) 
     
    2626        self.assertEqual(f.Q_unit, '1/A') 
    2727        self.assertEqual(f.I_unit, '1/cm') 
    28          
     28 
    2929        self.assertEqual(f.meta_data['loader'],"IGOR/DAT 2D Q_map") 
    30          
    31          
     30 
     31 
    3232if __name__ == '__main__': 
    3333    unittest.main() 
    34     
  • test/sasinvariant/test/utest_use_cases.py

    r959eb01 rb09095a  
    66import unittest 
    77from sas.sascalc.dataloader.loader import  Loader 
    8  
    98from sas.sascalc.invariant import invariant 
     9 
    1010 
    1111class Data1D: 
    1212    pass 
    13      
     13 
     14 
    1415class TestLineFit(unittest.TestCase): 
    1516    """ 
     
    1718    """ 
    1819    def setUp(self): 
    19         self.data = Loader().load("linefittest.txt") 
    20          
     20        self.data_list = Loader().load("linefittest.txt") 
     21        self.data = self.data_list[0] 
     22 
    2123    def test_fit_line_data(self): 
    2224        """  
    2325            Fit_Test_1: test linear fit, ax +b, without fixed 
    2426        """ 
    25          
     27 
    2628        # Create invariant object. Background and scale left as defaults. 
    2729        fit = invariant.Extrapolator(data=self.data) 
    2830         
    29         ##Without holding 
     31        # Without holding 
    3032        p, dp = fit.fit(power=None) 
    3133 
     
    3335        self.assertAlmostEquals(p[0], 2.3983,3) 
    3436        self.assertAlmostEquals(p[1], 0.87833,3) 
    35  
    3637 
    3738    def test_fit_line_data_fixed(self): 
     
    3940            Fit_Test_2: test linear fit, ax +b, with 'a' fixed 
    4041        """ 
    41          
     42 
    4243        # Create invariant object. Background and scale left as defaults. 
    4344        fit = invariant.Extrapolator(data=self.data) 
    44          
    45         #With holding a = -power =4 
     45 
     46        # With holding a = -power =4 
    4647        p, dp = fit.fit(power=-4) 
    4748 
     
    4950        self.assertAlmostEquals(p[0], 4) 
    5051        self.assertAlmostEquals(p[1], -4.0676,3) 
    51          
     52 
     53 
    5254class TestLineFitNoweight(unittest.TestCase): 
    5355    """ 
     
    5557    """ 
    5658    def setUp(self): 
    57         self.data = Loader().load("linefittest_no_weight.txt") 
    58          
     59        self.data_list = Loader().load("linefittest_no_weight.txt") 
     60        self.data = self.data_list[0] 
     61 
    5962    def skip_test_fit_line_data_no_weight(self): 
    6063        """  
    6164            Fit_Test_1: test linear fit, ax +b, without fixed 
    6265        """ 
    63          
     66 
    6467        # Create invariant object. Background and scale left as defaults. 
    6568        fit = invariant.Extrapolator(data=self.data) 
    66          
    67         ##Without holding 
     69 
     70        # Without holding 
    6871        p, dp = fit.fit(power=None) 
    6972 
     
    7174        self.assertAlmostEquals(p[0], 2.4727,3) 
    7275        self.assertAlmostEquals(p[1], 0.6,3) 
    73  
    7476 
    7577    def test_fit_line_data_fixed_no_weight(self): 
     
    7779            Fit_Test_2: test linear fit, ax +b, with 'a' fixed 
    7880        """ 
    79          
     81 
    8082        # Create invariant object. Background and scale left as defaults. 
    8183        fit = invariant.Extrapolator(data=self.data) 
    82          
     84 
    8385        #With holding a = -power =4 
    8486        p, dp = fit.fit(power=-4) 
     
    8789        self.assertAlmostEquals(p[0], 4) 
    8890        self.assertAlmostEquals(p[1], -7.8,3) 
    89                  
     91 
     92 
    9093class TestInvPolySphere(unittest.TestCase): 
    9194    """ 
     
    9396    """ 
    9497    def setUp(self): 
    95         self.data = Loader().load("PolySpheres.txt") 
    96          
     98        self.data_list = Loader().load("PolySpheres.txt") 
     99        self.data = self.data_list[0] 
     100 
    97101    def test_wrong_data(self): 
    98102        """ test receiving Data1D not of type loader""" 
    99  
    100  
    101103        self.assertRaises(ValueError,invariant.InvariantCalculator, Data1D()) 
    102          
     104 
    103105    def test_use_case_1(self): 
    104106        """ 
     
    107109        # Create invariant object. Background and scale left as defaults. 
    108110        inv = invariant.InvariantCalculator(data=self.data) 
    109          
     111 
    110112        # We have to be able to tell the InvariantCalculator whether we want the 
    111113        # extrapolation or not. By default, when the user doesn't specify, we 
    112         # should compute Q* without extrapolation. That's what should be done in __init__. 
    113          
     114        # should compute Q* without extrapolation. That's what should be done 
     115        # in __init__. 
     116 
    114117        # We call get_qstar() with no argument, which signifies that we do NOT 
    115118        # want extrapolation. 
    116119        qstar = inv.get_qstar() 
    117          
     120 
    118121        # The volume fraction and surface use Q*. That means that the following  
    119122        # methods should check that Q* has been computed. If not, it should  
     
    121124        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 
    122125        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 
    123          
     126 
    124127        # Test results 
    125128        self.assertAlmostEquals(qstar, 7.48959e-5,2) 
    126129        self.assertAlmostEquals(v, 0.005644689, 4) 
    127130        self.assertAlmostEquals(s , 941.7452, 3) 
    128          
     131 
    129132    def test_use_case_2(self): 
    130133        """ 
    131             Invariant without extrapolation. Invariant, volume fraction and surface  
    132             are given with errors. 
    133         """ 
    134         # Create invariant object. Background and scale left as defaults. 
    135         inv = invariant.InvariantCalculator(data=self.data) 
    136          
     134        Invariant without extrapolation. Invariant, volume fraction and surface  
     135        are given with errors. 
     136        """ 
     137        # Create invariant object. Background and scale left as defaults. 
     138        inv = invariant.InvariantCalculator(data=self.data) 
     139 
    137140        # Get the invariant with errors 
    138141        qstar, qstar_err = inv.get_qstar_with_error() 
    139          
     142 
    140143        # The volume fraction and surface use Q*. That means that the following  
    141144        # methods should check that Q* has been computed. If not, it should  
     
    147150        self.assertAlmostEquals(v, 0.005644689, 1) 
    148151        self.assertAlmostEquals(s , 941.7452, 3) 
    149         
    150          
     152 
    151153    def test_use_case_3(self): 
    152154        """ 
     
    155157        # Create invariant object. Background and scale left as defaults. 
    156158        inv = invariant.InvariantCalculator(data=self.data) 
    157          
     159 
    158160        # Set the extrapolation parameters for the low-Q range 
    159          
     161 
    160162        # The npts parameter should have a good default. 
    161163        # The range parameter should be 'high' or 'low' 
    162164        # The function parameter should default to None. If it is None, 
    163         #    the method should pick a good default (Guinier at low-Q and 1/q^4 at high-Q). 
    164         #    The method should also check for consistency of the extrapolation and function 
    165         #    parameters. For instance, you might not want to allow 'high' and 'guinier'. 
     165        #    the method should pick a good default 
     166        #    (Guinier at low-Q and 1/q^4 at high-Q). 
     167        #    The method should also check for consistency of the extrapolation 
     168        #    and function parameters. For instance, you might not want to allow 
     169        #    'high' and 'guinier'. 
    166170        # The power parameter (not shown below) should default to 4. 
    167171        inv.set_extrapolation(range='low', npts=10, function='guinier') 
    168          
    169         # The version of the call without error 
    170         # At this point, we could still compute Q* without extrapolation by calling 
    171         # get_qstar with arguments, or with extrapolation=None. 
     172 
     173        # The version of the call without error 
     174        # At this point, we could still compute Q* without extrapolation by 
     175        # calling get_qstar with arguments, or with extrapolation=None. 
    172176        qstar = inv.get_qstar(extrapolation='low') 
    173          
     177 
    174178        # The version of the call with error 
    175179        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') 
     
    178182        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 
    179183        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 
    180          
     184 
    181185        # Test results 
    182186        self.assertAlmostEquals(qstar, 7.49e-5, 1) 
    183187        self.assertAlmostEquals(v, 0.005648401, 4) 
    184188        self.assertAlmostEquals(s , 941.7452, 3) 
    185              
     189 
    186190    def test_use_case_4(self): 
    187191        """ 
     
    190194        # Create invariant object. Background and scale left as defaults. 
    191195        inv = invariant.InvariantCalculator(data=self.data) 
     196 
     197        # Set the extrapolation parameters for the high-Q range 
     198        inv.set_extrapolation(range='high', npts=10, function='power_law', 
     199                              power=4) 
    192200         
    193         # Set the extrapolation parameters for the high-Q range 
    194         inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 
    195          
    196         # The version of the call without error 
    197         # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 
     201        # The version of the call without error 
     202        # The function parameter defaults to None, then is picked to be 
     203        # 'power_law' for extrapolation='high' 
    198204        qstar = inv.get_qstar(extrapolation='high') 
    199          
     205 
    200206        # The version of the call with error 
    201207        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') 
     
    204210        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 
    205211        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 
    206          
     212 
    207213        # Test results 
    208214        self.assertAlmostEquals(qstar, 7.49e-5,2) 
    209215        self.assertAlmostEquals(v, 0.005952674, 3) 
    210216        self.assertAlmostEquals(s , 941.7452, 3) 
    211          
     217 
    212218    def test_use_case_5(self): 
    213219        """ 
     
    216222        # Create invariant object. Background and scale left as defaults. 
    217223        inv = invariant.InvariantCalculator(data=self.data) 
    218          
     224 
    219225        # Set the extrapolation parameters for the low- and high-Q ranges 
    220226        inv.set_extrapolation(range='low', npts=10, function='guinier') 
    221         inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 
    222          
    223         # The version of the call without error 
    224         # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 
     227        inv.set_extrapolation(range='high', npts=10, function='power_law', 
     228                              power=4) 
     229 
     230        # The version of the call without error 
     231        # The function parameter defaults to None, then is picked to be 
     232        # 'power_law' for extrapolation='high' 
    225233        qstar = inv.get_qstar(extrapolation='both') 
    226234         
     
    231239        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 
    232240        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 
    233          
     241 
    234242        # Test results 
    235243        self.assertAlmostEquals(qstar, 7.88981e-5,2) 
    236244        self.assertAlmostEquals(v, 0.005952674, 3) 
    237245        self.assertAlmostEquals(s , 941.7452, 3) 
    238        
     246 
    239247    def test_use_case_6(self): 
    240248        """ 
     
    243251        # Create invariant object. Background and scale left as defaults. 
    244252        inv = invariant.InvariantCalculator(data=self.data) 
    245          
     253 
    246254        # Set the extrapolation parameters for the high-Q range 
    247255        inv.set_extrapolation(range='low', npts=10, function='power_law', power=4) 
    248          
     256 
    249257        # The version of the call without error 
    250258        # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 
    251259        qstar = inv.get_qstar(extrapolation='low') 
    252          
     260 
    253261        # The version of the call with error 
    254262        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') 
     
    257265        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 
    258266        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 
    259          
     267 
    260268        # Test results 
    261269        self.assertAlmostEquals(qstar, 7.49e-5,2) 
    262270        self.assertAlmostEquals(v, 0.005952674, 3) 
    263271        self.assertAlmostEquals(s , 941.7452, 3) 
    264          
     272 
     273 
    265274class TestInvPinholeSmear(unittest.TestCase): 
    266275    """ 
     
    271280        list = Loader().load("latex_smeared.xml") 
    272281        self.data_q_smear = list[0] 
    273      
     282 
    274283    def test_use_case_1(self): 
    275284        """ 
     
    278287        inv = invariant.InvariantCalculator(data=self.data_q_smear) 
    279288        qstar = inv.get_qstar() 
    280          
     289 
    281290        v = inv.get_volume_fraction(contrast=2.6e-6) 
    282291        s = inv.get_surface(contrast=2.6e-6, porod_const=2) 
     
    285294        self.assertAlmostEquals(v, 0.115352622, 2) 
    286295        self.assertAlmostEquals(s , 941.7452, 3 ) 
    287          
     296 
    288297    def test_use_case_2(self): 
    289298        """ 
     
    293302        # Create invariant object. Background and scale left as defaults. 
    294303        inv = invariant.InvariantCalculator(data=self.data_q_smear) 
    295          
     304 
    296305        # Get the invariant with errors 
    297306        qstar, qstar_err = inv.get_qstar_with_error() 
     
    303312        self.assertAlmostEquals(v, 0.115352622, 2) 
    304313        self.assertAlmostEquals(s , 941.7452, 3 ) 
    305         
     314 
    306315    def test_use_case_3(self): 
    307316        """ 
     
    319328        v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 
    320329        s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 
    321          
     330 
    322331        # Test results 
    323332        self.assertAlmostEquals(qstar, 0.00138756,2) 
    324333        self.assertAlmostEquals(v, 0.117226896,2) 
    325334        self.assertAlmostEquals(s ,941.7452, 3) 
    326        
     335 
    327336    def test_use_case_4(self): 
    328337        """ 
     
    337346        # The version of the call with error 
    338347        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') 
    339          
    340         # Get the volume fraction and surface 
    341         # WHY SHOULD THIS FAIL? 
    342         #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6) 
    343          
    344         # Check that an exception is raised when the 'surface' is not defined 
    345         # WHY SHOULD THIS FAIL? 
    346         #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2) 
    347348 
    348349        # Test results 
    349350        self.assertAlmostEquals(qstar, 0.0045773,2) 
    350         
     351 
    351352    def test_use_case_5(self): 
    352353        """ 
     
    357358        # Set the extrapolation parameters for the low- and high-Q ranges 
    358359        inv.set_extrapolation(range='low', npts=10, function='guinier') 
    359         inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 
    360         # The version of the call without error 
    361         # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 
     360        inv.set_extrapolation(range='high', npts=10, function='power_law', 
     361                              power=4) 
     362        # The version of the call without error 
     363        # The function parameter defaults to None, then is picked to be 
     364        # 'power_law' for extrapolation='high' 
    362365        qstar = inv.get_qstar(extrapolation='both') 
    363366        # The version of the call with error 
    364367        qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') 
    365          
    366         # Get the volume fraction and surface 
    367         # WHY SHOULD THIS FAIL? 
    368         #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6) 
    369         #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2) 
    370          
     368 
    371369        # Test results 
    372370        self.assertAlmostEquals(qstar, 0.00460319,3) 
Note: See TracChangeset for help on using the changeset viewer.