Changeset 97603c0 in sasview for Invariant


Ignore:
Timestamp:
Jan 20, 2010 5:05:16 PM (14 years ago)
Author:
Gervaise Alina <gervyh@…>
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:
889d5ab4
Parents:
662da312
Message:

add test to evaluate the output of the fit for slit smear data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Invariant/test/utest_data_handling.py

    raafa962 r97603c0  
    1313from DataLoader.data_info import Data1D 
    1414from sans.invariant import invariant 
     15from DataLoader.qsmearing import smear_selection 
    1516     
    1617class TestLinearFit(unittest.TestCase): 
     
    206207        self.assertEqual(len(y_out), 3) 
    207208        self.assertEqual(len(dy_out), 3) 
    208      
     209 
     210     
     211class TestDataExtraLow(unittest.TestCase): 
     212    """ 
     213        Generate a Guinier distribution and verify that the extrapolation 
     214        produce the correct ditribution. Tested if the data generated by the  
     215        invariant calculator is correct 
     216    """ 
     217     
     218    def setUp(self): 
     219        """ 
     220            Generate a Guinier distribution. After extrapolating, we will 
     221            verify that we obtain the scale and rg parameters 
     222        """ 
     223        self.scale = 1.5 
     224        self.rg = 30.0 
     225        x = numpy.arange(0.0001, 0.1, 0.0001) 
     226        y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) 
     227        dy = y*.1 
     228        self.data = Data1D(x=x, y=y, dy=dy) 
     229         
     230    def test_low_q(self): 
     231        """ 
     232            Invariant with low-Q extrapolation with no slit smear 
     233        """ 
     234        # Create invariant object. Background and scale left as defaults. 
     235        inv = invariant.InvariantCalculator(data=self.data) 
     236        # Set the extrapolation parameters for the low-Q range 
     237        inv.set_extrapolation(range='low', npts=20, function='guinier') 
     238         
     239        self.assertEqual(inv._low_extrapolation_npts, 20) 
     240        self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) 
     241         
     242        # Data boundaries for fiiting 
     243        qmin = inv._data.x[0] 
     244        qmax = inv._data.x[inv._low_extrapolation_npts - 1] 
     245         
     246        # Extrapolate the low-Q data 
     247        a, b = inv._fit(model=inv._low_extrapolation_function, 
     248                          qmin=qmin, 
     249                          qmax=qmax, 
     250                          power=inv._low_extrapolation_power) 
     251        self.assertAlmostEqual(self.scale, a, 6) 
     252        self.assertAlmostEqual(self.rg, b, 6) 
     253         
     254        qstar = inv.get_qstar(extrapolation='low') 
     255        reel_y = self.data.y 
     256        test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x) 
     257        for i in range(len(self.data.x)): 
     258            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     259            self.assert_(value < 0.001) 
     260             
     261class TestDataExtraLowSlit(unittest.TestCase): 
     262    """ 
     263        Generate a Guinier distribution and verify that the extrapolation 
     264        produce the correct ditribution. Tested if the data generated by the  
     265        invariant calculator is correct 
     266    """ 
     267     
     268    def setUp(self): 
     269        """ 
     270            Generate a Guinier distribution. After extrapolating, we will 
     271            verify that we obtain the scale and rg parameters 
     272        """ 
     273        list = Loader().load("latex_smeared.xml") 
     274        self.data = list[0] 
     275        self.data.dxl = list[0].dxl 
     276        self.data.dxw = list[0].dxw 
     277         
     278    def test_low_q(self): 
     279        """ 
     280            Invariant with low-Q extrapolation with slit smear 
     281        """ 
     282        # Create invariant object. Background and scale left as defaults. 
     283        inv = invariant.InvariantCalculator(data=self.data) 
     284        # Set the extrapolation parameters for the low-Q range 
     285        inv.set_extrapolation(range='low', npts=20, function='guinier') 
     286         
     287        self.assertEqual(inv._low_extrapolation_npts, 20) 
     288        self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) 
     289         
     290        # Data boundaries for fiiting 
     291        qmin = inv._data.x[0] 
     292        qmax = inv._data.x[inv._low_extrapolation_npts - 1] 
     293         
     294        # Extrapolate the low-Q data 
     295        a, b = inv._fit(model=inv._low_extrapolation_function, 
     296                          qmin=qmin, 
     297                          qmax=qmax, 
     298                          power=inv._low_extrapolation_power) 
     299       
     300         
     301        qstar = inv.get_qstar(extrapolation='low') 
     302        reel_y = self.data.y 
     303        #Compution the y 's coming out of the invariant when computing extrapolated 
     304        #low data . expect the fit engine to have been already called and the guinier 
     305        # to have the radius and the scale fitted 
     306         
     307        test_y = numpy.zeros(len(self.data.x)) 
     308        smearer = smear_selection(self.data) 
     309        first_bin, last_bin = smearer.get_bin_range(min(self.data.x), 
     310                                                         max(self.data.x)) 
     311        test_y[first_bin:last_bin] = inv._low_extrapolation_function.evaluate_model(self.data.x[first_bin:last_bin]) 
     312        test_y = smearer(test_y, first_bin, last_bin)  
     313         
     314        for i in range(len(self.data.x)): 
     315            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     316            self.assert_(value < 0.001) 
     317             
     318        # test data coming out of the invariant  
     319        test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x) 
     320        for i in range(len(self.data.x)): 
     321            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     322            self.assert_(value < 0.001) 
     323             
     324             
     325                     
     326             
Note: See TracChangeset for help on using the changeset viewer.