Changeset c75a8ed in sasview for Invariant


Ignore:
Timestamp:
Mar 6, 2010 10:18:04 AM (14 years ago)
Author:
Mathieu Doucet <doucetm@…>
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.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
21d99c2
Parents:
2661d8b
Message:

invariant: added unit tests for specific surface and volume fraction. Minor fixes.

Location:
Invariant
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Invariant/invariant.py

    r2e94cbde rc75a8ed  
    1616    TODO:  
    1717        - intro / documentation 
    18         - add unit tests for sufrace/volume computation with and without extrapolation. 
     18 
    1919""" 
    2020import math  
     
    575575        return self._get_qstar(data), self._get_qstar_uncertainty(data) 
    576576     
    577     def get_extra_data_low(self, npts_in=None, q_start=None, nsteps=20): 
     577    def get_extra_data_low(self, npts_in=None, q_start=None, npts=20): 
    578578        """ 
    579579            Returns the extrapolated data used for the loew-Q invariant calculation. 
     
    601601 
    602602        return self._get_extrapolated_data(model=self._low_extrapolation_function, 
    603                                            npts=nsteps, 
     603                                           npts=npts, 
    604604                                           q_start=q_start, q_end=q_end) 
    605605           
     
    620620        if npts_in is None: 
    621621            npts_in = self._high_extrapolation_npts 
    622         npts = len(self._data.x) 
    623         q_start = self._data.x[min(npts, npts-npts_in+1)] 
     622        _npts = len(self._data.x) 
     623        q_start = self._data.x[min(_npts, _npts-npts_in)] 
    624624         
    625625        if q_start >= q_end: 
     
    835835        v, dv = self.get_volume_fraction_with_error(contrast, extrapolation) 
    836836 
    837         s = self.get_surface(contrast=contrast, porod_const=porod_const) 
     837        s = self.get_surface(contrast=contrast, porod_const=porod_const,  
     838                             extrapolation=extrapolation) 
    838839        ds = porod_const * 2 * math.pi * (( dv - 2 * v * dv)/ self._qstar\ 
    839840                 + self._qstar_err * ( v - v**2)) 
  • Invariant/test/utest_data_handling.py

    rbdd162f rc75a8ed  
    281281        self.assertTrue(math.fabs(qs_extr-qstar)<dqs_extr) 
    282282         
     283        def _check_values(to_check, reference, tolerance=0.05): 
     284            self.assertTrue( math.fabs(to_check-reference)/reference < tolerance, msg="Tested value = "+str(to_check) ) 
     285             
     286        # The following values should be replaced by values pulled from IGOR 
     287        # Volume Fraction: 
     288        v, dv = inv.get_volume_fraction_with_error(1, None) 
     289        _check_values(v, 1.88737914186e-15) 
     290 
     291        v_l, dv_l = inv.get_volume_fraction_with_error(1, 'low') 
     292        _check_values(v_l, 1.94289029309e-15) 
     293 
     294        v_h, dv_h = inv.get_volume_fraction_with_error(1, 'high') 
     295        _check_values(v_h, 6.99440505514e-15) 
     296         
     297        v_b, dv_b = inv.get_volume_fraction_with_error(1, 'both') 
     298        _check_values(v_b, 6.99440505514e-15) 
     299         
     300        # Specific Surface: 
     301        s, ds = inv.get_surface_with_error(1, 1, None) 
     302        _check_values(s, 3.1603095786e-09) 
     303 
     304        s_l, ds_l = inv.get_surface_with_error(1, 1, 'low') 
     305        _check_values(s_l, 3.1603095786e-09) 
     306 
     307        s_h, ds_h = inv.get_surface_with_error(1, 1, 'high') 
     308        _check_values(s_h, 3.1603095786e-09) 
     309         
     310        s_b, ds_b = inv.get_surface_with_error(1, 1, 'both') 
     311        _check_values(s_b, 3.1603095786e-09) 
     312         
     313         
    283314    def test_bad_parameter_name(self): 
    284315        """ 
     
    461492         
    462493        qstar = inv.get_qstar(extrapolation='low') 
    463         reel_y = self.data.y 
    464494        test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x) 
    465495        for i in range(len(self.data.x)): 
    466             value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     496            value  = math.fabs(test_y[i]-self.data.y[i])/self.data.y[i] 
    467497            self.assert_(value < 0.001) 
    468498             
     
    470500    """ 
    471501        for a smear data, test that the fitting go through  
    472         reel data for atleast the 2 first points 
     502        real data for atleast the 2 first points 
    473503    """ 
    474504     
     
    483513        y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) 
    484514        dy = y*.1 
    485         dxl = 0.117 * numpy.ones(len(x)) 
    486515        self.data = Data1D(x=x, y=y, dy=dy) 
    487         self.data.dxl = dxl 
    488516        self.npts = len(x)-10 
    489517         
     
    512540         
    513541        qstar = inv.get_qstar(extrapolation='low') 
    514         reel_y = self.data.y 
    515         #Compution the y 's coming out of the invariant when computing extrapolated 
    516         #low data . expect the fit engine to have been already called and the guinier 
    517         # to have the radius and the scale fitted 
     542 
    518543        test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x[:inv._low_extrapolation_npts]) 
    519         self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 
     544        self.assert_(len(test_y) == len(self.data.y[:inv._low_extrapolation_npts])) 
    520545         
    521546        for i in range(inv._low_extrapolation_npts): 
    522             value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     547            value  = math.fabs(test_y[i]-self.data.y[i])/self.data.y[i] 
    523548            self.assert_(value < 0.001) 
    524549             
     
    547572         
    548573        qstar = inv.get_qstar(extrapolation='low') 
    549         reel_y = self.data.y 
    550574        #Compution the y 's coming out of the invariant when computing extrapolated 
    551575        #low data . expect the fit engine to have been already called and the guinier 
    552576        # to have the radius and the scale fitted 
    553         data_out_range, data_in_range= inv.get_extra_data_low()  
     577        data_in_range = inv.get_extra_data_low(q_start=self.data.x[0],  
     578                                               npts = inv._low_extrapolation_npts)  
    554579        test_y = data_in_range.y 
    555         self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 
     580        self.assert_(len(test_y) == len(self.data.y[:inv._low_extrapolation_npts])) 
    556581        for i in range(inv._low_extrapolation_npts): 
    557             value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     582            value  = math.fabs(test_y[i]-self.data.y[i])/self.data.y[i] 
    558583            self.assert_(value < 0.001)     
    559                      
    560         data_out_range, data_in_range= inv.get_extra_data_low(npts_in= 2, nsteps=10, 
    561                                                                q_start= 1e-4)  
    562         test_y = data_in_range.y 
    563         self.assert_(len(test_y))== len(reel_y[:2]) 
    564         for i in range(2): 
    565             value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
    566             self.assert_(value < 0.001)  
    567         #test the data out of range           
    568         test_out_y = data_out_range.y 
    569         #self.assertEqual(len(test_out_y), 10)              
     584        
    570585             
    571586class TestDataExtraHighSlitPowerLaw(unittest.TestCase): 
    572587    """ 
    573588        for a smear data, test that the fitting go through  
    574         reel data for atleast the 2 first points 
     589        real data for atleast the 2 first points 
    575590    """ 
    576591     
     
    586601        dy = y*.1 
    587602        self.data = Data1D(x=x, y=y, dy=dy) 
    588         dxl = 0.117 * numpy.ones(len(x)) 
    589         self.data.dxl = dxl 
    590603        self.npts = 20 
    591604         
     
    616629         
    617630        qstar = inv.get_qstar(extrapolation='high') 
    618         reel_y = self.data.y 
    619         #Compution the y 's coming out of the invariant when computing extrapolated 
    620         #low data . expect the fit engine to have been already called and the power law 
    621         # to have the radius and the scale fitted 
    622         
    623631         
    624632        test_y = inv._high_extrapolation_function.evaluate_model(x=self.data.x[start: ]) 
    625         self.assert_(len(test_y))== len(reel_y[start:]) 
     633        self.assert_(len(test_y) == len(self.data.y[start:])) 
    626634         
    627635        for i in range(len(self.data.x[start:])): 
    628             value  = math.fabs(test_y[i]-reel_y[start+i])/reel_y[start+i] 
     636            value  = math.fabs(test_y[i]-self.data.y[start+i])/self.data.y[start+i] 
    629637            self.assert_(value < 0.001) 
    630638             
     
    654662       
    655663        qstar = inv.get_qstar(extrapolation='high') 
    656         reel_y = self.data.y 
    657         #Compution the y 's coming out of the invariant when computing extrapolated 
    658         #low data . expect the fit engine to have been already called and the power law 
    659         # to have the radius and the scale fitted 
    660664        
    661         data_out_range, data_in_range= inv.get_extra_data_high()  
     665        data_in_range= inv.get_extra_data_high(q_end = max(self.data.x), 
     666                                               npts = inv._high_extrapolation_npts)  
    662667        test_y = data_in_range.y 
    663         self.assert_(len(test_y))== len(reel_y[start:]) 
    664         temp = reel_y[start:] 
     668        self.assert_(len(test_y) == len(self.data.y[start:])) 
     669        temp = self.data.y[start:] 
    665670         
    666671        for i in range(len(self.data.x[start:])): 
    667672            value  = math.fabs(test_y[i]- temp[i])/temp[i] 
    668             self.assert_(value < 0.001)     
    669                      
    670         data_out_range, data_in_range= inv.get_extra_data_high(npts_in=5, nsteps=10, 
    671                                                                q_end= 2)  
    672         test_y = data_in_range.y 
    673         self.assert_(len(test_y)==5) 
    674         temp = reel_y[start:start+5] 
    675          
    676         for i in range(len(self.data.x[start:start+5])): 
    677            
    678             value  = math.fabs(test_y[i]- temp[i])/temp[i] 
    679             self.assert_(value < 0.06)     
    680         #test the data out of range           
    681         test_out_y = data_out_range.y 
    682         self.assertEqual(len(test_out_y), 10)              
     673            self.assert_(value < 0.001)                 
    683674                       
Note: See TracChangeset for help on using the changeset viewer.