Changeset 76c1727 in sasview for Invariant


Ignore:
Timestamp:
Jan 21, 2010 4:42:07 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:
deeba75
Parents:
889d5ab4
Message:

a fix for smear invariant

Location:
Invariant
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Invariant/invariant.py

    raafa962 r76c1727  
    2424        function given some x, y  
    2525    """ 
    26     def linearize_data(self, x, y, dy=None): 
     26     
     27    def linearize_data(self, data): 
    2728        """ 
    2829            Linearize data so that a linear fit can be performed.  
    2930            Filter out the data that can't be transformed. 
    30             @param x: array of q values 
    31             @param y: array of I(q) values 
    32             @param dy: array of dI(q) values 
     31            @param data : LoadData1D instance 
     32        """ 
     33        # Check that the vector lengths are equal 
     34        assert(len(data.x)==len(data.y)) 
     35        if data.dy is not None: 
     36            assert(len(data.x)==len(data.dy)) 
     37            dy = data.dy 
     38        else: 
     39            dy = numpy.array([math.sqrt(math.fabs(j)) for j in data.y]) 
     40        # Transform smear info  
     41        dxl_out = None 
     42        dxw_out = None 
     43        dx_out = None 
     44        first_x = data.x#[0]  
     45        #if first_x == 0: 
     46        #    first_x = data.x[1] 
     47             
     48            
     49        data_points = zip(data.x, data.y, dy) 
     50         
     51        # Transform the data 
     52        output_points = [(self.linearize_q_value(p[0]), 
     53                          math.log(p[1]), 
     54                          p[2]/p[1]) for p in data_points if p[0]>0 and p[1]>0] 
     55         
     56        x_out, y_out, dy_out = zip(*output_points) 
     57        
     58        #Transform smear info     
     59        if data.dxl is not None  and len(data.dxl)>0: 
     60            dxl_out = self.linearize_dq_value(x=x_out, dx=data.dxl[:len(x_out)]) 
     61        if data.dxw is not None and len(data.dxw)>0: 
     62            dxw_out = self.linearize_dq_value(x=x_out, dx=data.dxw[:len(x_out)]) 
     63        if data.dx is not None  and len(data.dx)>0: 
     64            dx_out = self.linearize_dq_value(x=x_out, dx=data.dx[:len(x_out)]) 
     65        x_out = numpy.asarray(x_out) 
     66        y_out = numpy.asarray(y_out) 
     67        dy_out = numpy.asarray(dy_out) 
     68        linear_data = LoaderData1D(x=x_out, y=y_out, dx=dx_out, dy=dy_out) 
     69        linear_data.dxl = dxl_out 
     70        linear_data.dxw = dxw_out 
     71         
     72        return linear_data 
     73    def linearize_dq_value(self, x, dx): 
     74        """ 
     75            Transform the input dq-value for linearization 
    3376        """ 
    3477        return NotImplemented 
    35      
     78 
    3679    def linearize_q_value(self, value): 
    3780        """ 
     
    61104        self.scale = scale 
    62105        self.radius = radius 
    63      
    64     def linearize_data(self, x, y, dy=None): 
    65         """ 
    66             Linearize data so that a linear fit can be performed.  
    67             Filter out the data that can't be transformed. 
    68             @param x: array of q values 
    69             @param y: array of I(q) values 
    70             @param dy: array of dI(q) values 
    71         """ 
    72         # Check that the vector lengths are equal 
    73         assert(len(x)==len(y)) 
    74         if dy is not None: 
    75             assert(len(x)==len(dy)) 
    76         else: 
    77             dy = numpy.array([math.sqrt(math.fabs(j)) for j in y]) 
    78          
    79         data_points = zip(x,y,dy) 
    80          
    81         # Transform the data 
    82         output_points = [(self.linearize_q_value(p[0]), 
    83                           math.log(p[1]), 
    84                           p[2]/p[1]) for p in data_points if p[0]>0 and p[1]>0] 
    85          
    86         x_out, y_out, dy_out = zip(*output_points) 
    87  
    88         return numpy.asarray(x_out), numpy.asarray(y_out), numpy.asarray(dy_out) 
     106         
     107    def linearize_dq_value(self, x, dx): 
     108        """ 
     109            Transform the input dq-value for linearization 
     110        """ 
     111        return numpy.array([2*x[0]*dx[0] for i in xrange(len(x))])  
    89112     
    90113    def linearize_q_value(self, value): 
     
    138161        self.scale = scale 
    139162        self.power = power 
    140          
    141     def linearize_data(self, x, y, dy=None): 
    142         """ 
    143             Linearize data so that a linear fit can be performed.  
    144             Filter out the data that can't be transformed. 
    145             @param x: array of q values 
    146             @param y: array of I(q) values 
    147             @param dy: array of dI(q) values 
    148         """ 
    149         # Check that the vector lengths are equal 
    150         assert(len(x)==len(y)) 
    151         if dy is not None: 
    152             assert(len(x)==len(dy)) 
    153         else: 
    154             dy = numpy.array([math.sqrt(math.fabs(j)) for j in y]) 
    155          
    156         data_points = zip(x,y,dy) 
    157          
    158         # Transform the data 
    159         output_points = [(self.linearize_q_value(p[0]), 
    160                           math.log(p[1]), 
    161                           p[2]/p[1]) for p in data_points if p[0]>0 and p[1]>0] 
    162          
    163         x_out, y_out, dy_out = zip(*output_points) 
    164  
    165         return numpy.asarray(x_out), numpy.asarray(y_out), numpy.asarray(dy_out) 
    166          
     163    
     164    def linearize_dq_value(self, x, dx): 
     165        """ 
     166            Transform the input dq-value for linearization 
     167        """ 
     168        return  numpy.array([dx[0]/x[0] for i in xrange(len(x))])  
     169     
    167170    def linearize_q_value(self, value): 
    168171        """ 
     
    301304        @note: Some computations depends on each others.  
    302305    """ 
    303      
    304      
    305306    def __init__(self, data, background=0, scale=1 ): 
    306307        """ 
     
    365366        """ 
    366367        # Linearize the data in preparation for fitting 
    367         fit_x, fit_y, fit_dy = model.linearize_data(self._data.x, self._data.y, self._data.dy) 
    368        
     368        fit_data = model.linearize_data(self._data) 
    369369        qmin = model.linearize_q_value(qmin) 
    370370        qmax = model.linearize_q_value(qmax) 
    371          
    372         # Create a new Data1D object for processing 
    373         fit_data = LoaderData1D(x=fit_x, y=fit_y, dy=fit_dy) 
    374         fit_data.dxl = self._data.dxl 
    375         fit_data.dxw = self._data.dxw    
     371        # Get coefficient cmoning out of the  fit 
    376372        extrapolator = Extrapolator(data=fit_data) 
    377373        extrapolator.set_fit_range(qmin=qmin, qmax=qmax) 
     
    592588                    sum += (data.x[i] * data.dxl[i] * dy[i] * dxi)**2 
    593589                return math.sqrt(sum) 
    594     
    595     def get_qstar_low(self): 
    596         """ 
    597             Compute the invariant for extrapolated data at low q range. 
    598              
    599             Implementation: 
    600                 data = self.get_extra_data_low() 
    601                 return self._get_qstar() 
    602                  
    603             @return q_star: the invariant for data extrapolated at low q. 
    604         """ 
    605         data = self.get_extra_data_low() 
    606         return self._get_qstar(data=data) 
    607          
    608     def get_qstar_high(self): 
    609         """ 
    610             Compute the invariant for extrapolated data at high q range. 
    611              
    612             Implementation: 
    613                 data = self.get_extra_data_high() 
    614                 return self._get_qstar() 
    615                  
    616             @return q_star: the invariant for data extrapolated at high q. 
    617         """ 
    618         data = self.get_extra_data_high() 
    619         return self._get_qstar(data=data) 
    620590         
    621591    def _get_extrapolated_data(self, model, npts=INTEGRATION_NSTEPS, 
     
    657627        result_data.dxw = dxw 
    658628        return result_data 
    659          
    660     def get_extra_data_low(self): 
     629     
     630    def _get_extra_data_low(self): 
    661631        """ 
    662632            This method creates a new data set from the invariant calculator. 
     
    689659                         qmax=qmax, 
    690660                         power=self._low_extrapolation_power) 
     661        q_start = Q_MINIMUM 
    691662        #q_start point 
    692         q_start = Q_MINIMUM 
    693663        if Q_MINIMUM >= qmin: 
    694664            q_start = qmin/10 
     
    699669        return data_min 
    700670           
    701     def get_extra_data_high(self): 
     671    def _get_extra_data_high(self): 
    702672        """ 
    703673            This method creates a new data from the invariant calculator. 
     
    730700                                               q_start=qmax, q_end=q_end) 
    731701        return data_max 
     702          
     703    def get_qstar_low(self): 
     704        """ 
     705            Compute the invariant for extrapolated data at low q range. 
     706             
     707            Implementation: 
     708                data = self._get_extra_data_low() 
     709                return self._get_qstar() 
     710                 
     711            @return q_star: the invariant for data extrapolated at low q. 
     712        """ 
     713        data = self._get_extra_data_low() 
     714         
     715        return self._get_qstar(data=data) 
     716         
     717    def get_qstar_high(self): 
     718        """ 
     719            Compute the invariant for extrapolated data at high q range. 
     720             
     721            Implementation: 
     722                data = self._get_extra_data_high() 
     723                return self._get_qstar() 
     724                 
     725            @return q_star: the invariant for data extrapolated at high q. 
     726        """ 
     727        data = self._get_extra_data_high() 
     728        return self._get_qstar(data=data) 
     729     
     730    def get_extra_data_low(self, npts_in=None, q_start=Q_MINIMUM, nsteps=INTEGRATION_NSTEPS): 
     731        """ 
     732            This method creates a new data set from the invariant calculator. 
     733             
     734            It will use the extrapolation parameters kept as private data members. 
     735             
     736            self._low_extrapolation_npts is the number of data points to use in to fit. 
     737            self._low_extrapolation_function will be used as the fit function. 
     738             
     739             
     740             
     741            It takes npts first points of data, fits them with a given model 
     742            then uses the new parameters resulting from the fit to create a new data set. 
     743             
     744            The new data first point is Q_MINIMUM. 
     745             
     746            The last point of the new data is the first point of the original data. 
     747            the number of q points of this data is INTEGRATION_NSTEPS. 
     748             
     749            @return: a new data of type Data1D 
     750        """ 
     751        #Create a data from result of the fit for a range outside of the data 
     752        # at low q range 
     753        data_out_range = self._get_extra_data_low() 
     754         
     755        if  q_start != Q_MINIMUM or nsteps != INTEGRATION_NSTEPS: 
     756            qmin = min(self._data.x) 
     757            if q_start < Q_MINIMUM: 
     758                q_start = Q_MINIMUM 
     759            elif q_start >= qmin: 
     760                q_start = qmin/10 
     761                 
     762            #compute the new data with the proper result of the fit for different 
     763            #boundary and step, outside of data 
     764            data_out_range = self._get_extrapolated_data(model=self._low_extrapolation_function, 
     765                                               npts=nsteps, 
     766                                               q_start=q_start, q_end=qmin) 
     767        #Create data from the result of the fit for a range inside data q range for 
     768        #low q 
     769        if npts_in is None : 
     770            npts_in = self._low_extrapolation_npts 
     771 
     772        x = self._data.x[:npts_in] 
     773        y = self._low_extrapolation_function.evaluate_model(x=x) 
     774        dy = None 
     775        dx = None 
     776        dxl = None 
     777        dxw = None 
     778        if self._data.dx is not None: 
     779            dx = self._data.dx[:npts_in] 
     780        if self._data.dy is not None: 
     781            dy = self._data.dy[:npts_in] 
     782        if self._data.dxl is not None and len(self._data.dxl)>0: 
     783            dxl = self._data.dxl[:npts_in] 
     784        if self._data.dxw is not None and len(self._data.dxw)>0: 
     785            dxw = self._data.dxw[:npts_in] 
     786        #Crate new data  
     787        data_in_range = LoaderData1D(x=x, y=y, dx=dx, dy=dy) 
     788        data_in_range.clone_without_data(clone=self._data) 
     789        data_in_range.dxl = dxl 
     790        data_in_range.dxw = dxw 
     791         
     792        return data_out_range, data_in_range 
     793           
     794    def get_extra_data_high(self, npts_in=None, q_end=Q_MAXIMUM, nsteps=INTEGRATION_NSTEPS ): 
     795        """ 
     796            This method creates a new data from the invariant calculator. 
     797             
     798            It takes npts last points of data, fits them with a given model 
     799            (for this function only power_law will be use), then uses 
     800            the new parameters resulting from the fit to create a new data set. 
     801            The first point is the last point of data. 
     802            The last point of the new data is Q_MAXIMUM. 
     803            The number of q points of this data is INTEGRATION_NSTEPS. 
     804 
     805             
     806            @return: a new data of type Data1D 
     807        """ 
     808        #Create a data from result of the fit for a range outside of the data 
     809        # at low q range 
     810        data_out_range = self._get_extra_data_high() 
     811        qmax = max(self._data.x) 
     812        if  q_end != Q_MAXIMUM or nsteps != INTEGRATION_NSTEPS: 
     813            if q_end > Q_MAXIMUM: 
     814               q_end = Q_MAXIMUM 
     815            elif q_end <= qmax: 
     816                q_end = qmax * 10 
     817                 
     818            #compute the new data with the proper result of the fit for different 
     819            #boundary and step, outside of data 
     820            data_out_range = self._get_extrapolated_data(model=self._high_extrapolation_function, 
     821                                               npts=nsteps, 
     822                                               q_start=qmax, q_end=q_end) 
     823        #Create data from the result of the fit for a range inside data q range for 
     824        #high q 
     825        if npts_in is None : 
     826            npts_in = self._high_extrapolation_npts 
     827             
     828        x_len = len(self._data.x) 
     829        x = self._data.x[(x_len-npts_in):] 
     830        y = self._high_extrapolation_function.evaluate_model(x=x) 
     831        dy = None 
     832        dx = None 
     833        dxl = None 
     834        dxw = None 
     835         
     836        if self._data.dx is not None: 
     837            dx = self._data.dx[(x_len-npts_in):] 
     838        if self._data.dy is not None: 
     839            dy = self._data.dy[(x_len-npts_in):] 
     840        if self._data.dxl is not None and len(self._data.dxl)>0: 
     841            dxl = self._data.dxl[(x_len-npts_in):] 
     842        if self._data.dxw is not None and len(self._data.dxw)>0: 
     843            dxw = self._data.dxw[(x_len-npts_in):] 
     844        #Crate new data  
     845        data_in_range = LoaderData1D(x=x, y=y, dx=dx, dy=dy) 
     846        data_in_range.clone_without_data(clone=self._data) 
     847        data_in_range.dxl = dxl 
     848        data_in_range.dxw = dxw 
     849         
     850        return data_out_range, data_in_range 
     851           
    732852      
    733853    def set_extrapolation(self, range, npts=4, function=None, power=None): 
  • Invariant/test/utest_data_handling.py

    r889d5ab4 r76c1727  
    192192    def test_guinier_incompatible_length(self): 
    193193        g = invariant.Guinier() 
    194         self.assertRaises(AssertionError, g.linearize_data, [1],[1,2],None) 
    195         self.assertRaises(AssertionError, g.linearize_data, [1,2],[1,2],[1]) 
     194        data_in = Data1D(x=[1], y=[1,2], dy=None) 
     195        self.assertRaises(AssertionError, g.linearize_data, data_in) 
     196        data_in = Data1D(x=[1,1], y=[1,2], dy=[1]) 
     197        self.assertRaises(AssertionError, g.linearize_data, data_in) 
    196198     
    197199    def test_linearization(self): 
     
    203205        y = numpy.asarray(numpy.asarray([1,1,1,1])) 
    204206        g = invariant.Guinier() 
    205         x_out, y_out, dy_out = g.linearize_data(x,y,None) 
     207        data_in = Data1D(x=x, y=y) 
     208        data_out = g.linearize_data(data_in) 
     209        x_out, y_out, dy_out = data_out.x, data_out.y, data_out.dy 
    206210        self.assertEqual(len(x_out), 3) 
    207211        self.assertEqual(len(y_out), 3) 
     
    261265class TestDataExtraLowSlit(unittest.TestCase): 
    262266    """ 
    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 
     267        for a smear data, test that the fitting go through  
     268        reel data for the 2 first points 
     269    """ 
     270    def setUp(self): 
     271        """ 
     272           Reel data containing slit smear information 
     273           .Use 2 points of data to fit with power_law when exptrapolating 
    272274        """ 
    273275        list = Loader().load("latex_smeared.xml") 
     
    275277        self.data.dxl = list[0].dxl 
    276278        self.data.dxw = list[0].dxw 
     279        self.npts = 2 
    277280         
    278281    def test_low_q(self): 
     
    283286        inv = invariant.InvariantCalculator(data=self.data) 
    284287        # 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) 
     288        inv.set_extrapolation(range='low', npts=self.npts, function='power_law') 
     289         
     290        self.assertEqual(inv._low_extrapolation_npts, self.npts) 
     291        self.assertEqual(inv._low_extrapolation_function.__class__, invariant.PowerLaw) 
    289292         
    290293        # Data boundaries for fiiting 
     
    298301                          power=inv._low_extrapolation_power) 
    299302       
    300          
    301303        qstar = inv.get_qstar(extrapolation='low') 
    302304        reel_y = self.data.y 
     
    304306        #low data . expect the fit engine to have been already called and the guinier 
    305307        # to have the radius and the scale fitted 
    306          
    307         test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x) 
    308         for i in range(len(self.data.x)): 
     308        test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x[:inv._low_extrapolation_npts]) 
     309        #Check any points generated from the reel data and the extrapolation have 
     310        #very close value 
     311        self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 
     312        for i in range(inv._low_extrapolation_npts): 
     313            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     314            self.assert_(value < 0.001) 
     315        data_out_range, data_in_range= inv.get_extra_data_low(npts_in=None) 
     316             
     317class TestDataExtraLowSlitGuinier(unittest.TestCase): 
     318    """ 
     319        for a smear data, test that the fitting go through  
     320        reel data for atleast the 2 first points 
     321    """ 
     322     
     323    def setUp(self): 
     324        """ 
     325            Generate a Guinier distribution. After extrapolating, we will 
     326            verify that we obtain the scale and rg parameters 
     327        """ 
     328        self.scale = 1.5 
     329        self.rg = 30.0 
     330        x = numpy.arange(0.0001, 0.1, 0.0001) 
     331        y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) 
     332        dy = y*.1 
     333        dxl = 0.117 * numpy.ones(len(x)) 
     334        self.data = Data1D(x=x, y=y, dy=dy) 
     335        self.data.dxl = dxl 
     336        self.npts = len(x)-10 
     337         
     338    def test_low_q(self): 
     339        """ 
     340            Invariant with low-Q extrapolation with slit smear 
     341        """ 
     342        # Create invariant object. Background and scale left as defaults. 
     343        inv = invariant.InvariantCalculator(data=self.data) 
     344        # Set the extrapolation parameters for the low-Q range 
     345        inv.set_extrapolation(range='low', npts=self.npts, function='guinier') 
     346         
     347        self.assertEqual(inv._low_extrapolation_npts, self.npts) 
     348        self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) 
     349         
     350        # Data boundaries for fiiting 
     351        qmin = inv._data.x[0] 
     352        qmax = inv._data.x[inv._low_extrapolation_npts - 1] 
     353         
     354        # Extrapolate the low-Q data 
     355        a, b = inv._fit(model=inv._low_extrapolation_function, 
     356                          qmin=qmin, 
     357                          qmax=qmax, 
     358                          power=inv._low_extrapolation_power) 
     359       
     360         
     361        qstar = inv.get_qstar(extrapolation='low') 
     362        reel_y = self.data.y 
     363        #Compution the y 's coming out of the invariant when computing extrapolated 
     364        #low data . expect the fit engine to have been already called and the guinier 
     365        # to have the radius and the scale fitted 
     366        test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x[:inv._low_extrapolation_npts]) 
     367        self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 
     368         
     369        for i in range(inv._low_extrapolation_npts): 
    309370            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
    310371            self.assert_(value < 0.001) 
    311372             
     373    def test_low_data(self): 
     374        """ 
     375            Invariant with low-Q extrapolation with slit smear 
     376        """ 
     377        # Create invariant object. Background and scale left as defaults. 
     378        inv = invariant.InvariantCalculator(data=self.data) 
     379        # Set the extrapolation parameters for the low-Q range 
     380        inv.set_extrapolation(range='low', npts=self.npts, function='guinier') 
     381         
     382        self.assertEqual(inv._low_extrapolation_npts, self.npts) 
     383        self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) 
     384         
     385        # Data boundaries for fiiting 
     386        qmin = inv._data.x[0] 
     387        qmax = inv._data.x[inv._low_extrapolation_npts - 1] 
     388         
     389        # Extrapolate the low-Q data 
     390        a, b = inv._fit(model=inv._low_extrapolation_function, 
     391                          qmin=qmin, 
     392                          qmax=qmax, 
     393                          power=inv._low_extrapolation_power) 
     394       
     395         
     396        qstar = inv.get_qstar(extrapolation='low') 
     397        reel_y = self.data.y 
     398        #Compution the y 's coming out of the invariant when computing extrapolated 
     399        #low data . expect the fit engine to have been already called and the guinier 
     400        # to have the radius and the scale fitted 
     401        data_out_range, data_in_range= inv.get_extra_data_low()  
     402        test_y = data_in_range.y 
     403        self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 
     404        for i in range(inv._low_extrapolation_npts): 
     405            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     406            self.assert_(value < 0.001)     
     407                     
     408        data_out_range, data_in_range= inv.get_extra_data_low(npts_in= 2, nsteps=10, 
     409                                                               q_start= 1e-4)  
     410        test_y = data_in_range.y 
     411        self.assert_(len(test_y))== len(reel_y[:2]) 
     412        for i in range(2): 
     413            value  = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 
     414            self.assert_(value < 0.001)  
     415        #test the data out of range           
     416        test_out_y = data_out_range.y 
     417        self.assertEqual(len(test_out_y), 10)              
    312418             
     419class TestDataExtraHighSlitPowerLaw(unittest.TestCase): 
     420    """ 
     421        for a smear data, test that the fitting go through  
     422        reel data for atleast the 2 first points 
     423    """ 
     424     
     425    def setUp(self): 
     426        """ 
     427            Generate a Guinier distribution. After extrapolating, we will 
     428            verify that we obtain the scale and rg parameters 
     429        """ 
     430        self.scale = 1.5 
     431        self.m = 3.0 
     432        x = numpy.arange(0.0001, 0.1, 0.0001) 
     433        y = numpy.asarray([self.scale * math.pow(q ,-1.0*self.m) for q in x])                 
     434        dy = y*.1 
     435        self.data = Data1D(x=x, y=y, dy=dy) 
     436        dxl = 0.117 * numpy.ones(len(x)) 
     437        self.data.dxl = dxl 
     438        self.npts = 20 
     439         
     440    def test_high_q(self): 
     441        """ 
     442            Invariant with high-Q extrapolation with slit smear 
     443        """ 
     444        # Create invariant object. Background and scale left as defaults. 
     445        inv = invariant.InvariantCalculator(data=self.data) 
     446        # Set the extrapolation parameters for the low-Q range 
     447        inv.set_extrapolation(range='high', npts=self.npts, function='power_law') 
     448         
     449        self.assertEqual(inv._high_extrapolation_npts, self.npts) 
     450        self.assertEqual(inv._high_extrapolation_function.__class__, invariant.PowerLaw) 
     451         
     452        # Data boundaries for fiiting 
     453        xlen = len(self.data.x) 
     454        start =  xlen - inv._high_extrapolation_npts 
     455        qmin = inv._data.x[start] 
     456        qmax = inv._data.x[xlen-1] 
     457         
     458        # Extrapolate the high-Q data 
     459        a, b = inv._fit(model=inv._high_extrapolation_function, 
     460                          qmin=qmin, 
     461                          qmax=qmax, 
     462                          power=inv._high_extrapolation_power) 
     463       
     464         
     465        qstar = inv.get_qstar(extrapolation='high') 
     466        reel_y = self.data.y 
     467        #Compution the y 's coming out of the invariant when computing extrapolated 
     468        #low data . expect the fit engine to have been already called and the power law 
     469        # to have the radius and the scale fitted 
     470        
     471         
     472        test_y = inv._high_extrapolation_function.evaluate_model(x=self.data.x[start: ]) 
     473        self.assert_(len(test_y))== len(reel_y[start:]) 
     474         
     475        for i in range(len(self.data.x[start:])): 
     476            value  = math.fabs(test_y[i]-reel_y[start+i])/reel_y[start+i] 
     477            self.assert_(value < 0.001) 
     478             
     479    def test_high_data(self): 
     480        """ 
     481            Invariant with low-Q extrapolation with slit smear 
     482        """ 
     483        # Create invariant object. Background and scale left as defaults. 
     484        inv = invariant.InvariantCalculator(data=self.data) 
     485        # Set the extrapolation parameters for the low-Q range 
     486        inv.set_extrapolation(range='high', npts=self.npts, function='power_law') 
     487         
     488        self.assertEqual(inv._high_extrapolation_npts, self.npts) 
     489        self.assertEqual(inv._high_extrapolation_function.__class__, invariant.PowerLaw) 
     490         
     491        # Data boundaries for fiiting 
     492        xlen = len(self.data.x) 
     493        start =  xlen - inv._high_extrapolation_npts 
     494        qmin = inv._data.x[start] 
     495        qmax = inv._data.x[xlen-1] 
     496         
     497        # Extrapolate the high-Q data 
     498        a, b = inv._fit(model=inv._high_extrapolation_function, 
     499                          qmin=qmin, 
     500                          qmax=qmax, 
     501                          power=inv._high_extrapolation_power) 
     502       
     503         
     504        qstar = inv.get_qstar(extrapolation='high') 
     505        reel_y = self.data.y 
     506        #Compution the y 's coming out of the invariant when computing extrapolated 
     507        #low data . expect the fit engine to have been already called and the power law 
     508        # to have the radius and the scale fitted 
     509        
     510        data_out_range, data_in_range= inv.get_extra_data_high()  
     511        test_y = data_in_range.y 
     512        self.assert_(len(test_y))== len(reel_y[start:]) 
     513        temp = reel_y[start:] 
     514         
     515        for i in range(len(self.data.x[start:])): 
     516            value  = math.fabs(test_y[i]- temp[i])/temp[i] 
     517            self.assert_(value < 0.001)     
    313518                     
    314              
     519        data_out_range, data_in_range= inv.get_extra_data_high(npts_in=5, nsteps=10, 
     520                                                               q_end= 2)  
     521        test_y = data_in_range.y 
     522        self.assert_(len(test_y)==5) 
     523        temp = reel_y[start:start+5] 
     524         
     525        for i in range(len(self.data.x[start:start+5])): 
     526           
     527            value  = math.fabs(test_y[i]- temp[i])/temp[i] 
     528            self.assert_(value < 0.06)     
     529        #test the data out of range           
     530        test_out_y = data_out_range.y 
     531        self.assertEqual(len(test_out_y), 10)              
     532                       
  • Invariant/test/utest_use_cases.py

    raafa962 r76c1727  
    326326        # Test results 
    327327        self.assertAlmostEquals(qstar, 4.1539e-4, 2) 
    328         self.assertAlmostEquals(v, 0.032164596, 3) 
     328        self.assertAlmostEquals(v, 0.032164596, 2) 
    329329        self.assertAlmostEquals(s , 941.7452, 3) 
    330330         
Note: See TracChangeset for help on using the changeset viewer.