Ignore:
Timestamp:
Apr 17, 2017 2:39:50 PM (7 years ago)
Author:
krzywon
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:
8ffafd1
Parents:
beba407
git-author:
Jeff Krzywon <krzywon@…> (04/17/17 14:39:50)
git-committer:
krzywon <krzywon@…> (04/17/17 14:39:50)
Message:

Refactor ASCII reader to use FileReader? class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.