Changeset 2e94cbde in sasview for Invariant


Ignore:
Timestamp:
Mar 5, 2010 7:59:19 PM (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:
2661d8b
Parents:
f5fda87
Message:

invariant: improve methods to get low/high extrapolated data.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Invariant/invariant.py

    rabf6771 r2e94cbde  
     1""" 
     2This software was developed by the University of Tennessee as part of the 
     3Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
     4project funded by the US National Science Foundation.  
     5 
     6See the license text in license.txt 
     7 
     8copyright 2010, University of Tennessee 
     9""" 
     10 
    111""" 
    212    This module implements invariant and its related computations. 
     
    717        - intro / documentation 
    818        - add unit tests for sufrace/volume computation with and without extrapolation. 
    9         - replace the get_extra_data_* methods 
    1019""" 
    1120import math  
     
    357366        self._high_extrapolation_function = PowerLaw() 
    358367        self._high_extrapolation_power = None 
     368         
     369        # Extrapolation range 
     370        self._low_q_limit = Q_MINIMUM 
    359371         
    360372    def _get_data(self, data): 
     
    520532         
    521533        # Distribution starting point 
    522         q_start = Q_MINIMUM 
     534        self._low_q_limit = Q_MINIMUM 
    523535        if Q_MINIMUM >= qmin: 
    524             q_start = qmin/10 
     536            self._low_q_limit = qmin/10 
    525537         
    526538        data = self._get_extrapolated_data(model=self._low_extrapolation_function, 
    527539                                               npts=INTEGRATION_NSTEPS, 
    528                                                q_start=q_start, q_end=qmin) 
     540                                               q_start=self._low_q_limit, q_end=qmin) 
    529541         
    530542        # Systematic error 
     
    532544        # may not be a Guinier or simple power law. The following is a conservative 
    533545        # estimation for the systematic error. 
    534         err = qmin*qmin*math.fabs((qmin-q_start)*(data.y[0] - data.y[INTEGRATION_NSTEPS-1])) 
     546        err = qmin*qmin*math.fabs((qmin-self._low_q_limit)*(data.y[0] - data.y[INTEGRATION_NSTEPS-1])) 
    535547        return self._get_qstar(data), self._get_qstar_uncertainty(data)+err 
    536548         
     
    549561        qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1)] 
    550562        qmax = self._data.x[x_len] 
    551         q_end = Q_MAXIMUM 
    552563         
    553564        # fit the data with a model to get the appropriate parameters 
     
    560571        data = self._get_extrapolated_data(model=self._high_extrapolation_function, 
    561572                                           npts=INTEGRATION_NSTEPS, 
    562                                            q_start=qmax, q_end=q_end)         
     573                                           q_start=qmax, q_end=Q_MAXIMUM)         
    563574         
    564575        return self._get_qstar(data), self._get_qstar_uncertainty(data) 
    565576     
    566     def get_extra_data_low(self, npts_in=None, q_start=Q_MINIMUM, nsteps=INTEGRATION_NSTEPS): 
    567         """ 
    568            This method generates 2 data sets , the first is a data created during  
    569            low extrapolation . its y is generated from x in [ Q_MINIMUM - the minimum of 
    570            data.x] and the outputs of the extrapolator . 
    571             (data is the data used to compute invariant)  
    572            the second is also data produced during the fit but the x range considered 
    573            is within the reel range of data x. 
    574            x uses is in [minimum of data.x up to npts_in points] 
    575            @param npts_in: the number of first points of data to consider  for computing 
    576            y's coming out of the fit. 
    577            @param q_start: is the minimum value to uses for extrapolated data 
    578            @param npts: the number of point used to create extrapolated data  
     577    def get_extra_data_low(self, npts_in=None, q_start=None, nsteps=20): 
     578        """ 
     579            Returns the extrapolated data used for the loew-Q invariant calculation. 
     580            By default, the distribution will cover the data points used for the  
     581            extrapolation. The number of overlap points is a parameter (npts_in). 
     582            By default, the maximum q-value of the distribution will be   
     583            the minimum q-value used when extrapolating for the purpose of the  
     584            invariant calculation.  
     585             
     586            @param npts_in: number of data points for which the extrapolated data overlap 
     587            @param q_start: is the minimum value to uses for extrapolated data 
     588            @param npts: the number of points in the extrapolated distribution 
    579589            
    580590        """ 
    581         # Create a data from result of the fit for a range outside of the data 
    582         # at low q range 
    583         q_start = max(Q_MINIMUM, q_start) 
    584         qmin = min(self._data.x) 
    585          
    586         if q_start < qmin: 
    587             data_out_range = self._get_extrapolated_data(model=self._low_extrapolation_function, 
    588                                                          npts=nsteps, 
    589                                                          q_start=q_start, q_end=qmin) 
    590         else: 
    591             data_out_range = LoaderData1D(x=numpy.zeros(0), y=numpy.zeros(0)) 
    592              
    593         # Create data from the result of the fit for a range inside data q range for 
    594         # low q 
    595         if npts_in is None : 
     591        # Get extrapolation range 
     592        if q_start is None: 
     593            q_start = self._low_q_limit 
     594             
     595        if npts_in is None: 
    596596            npts_in = self._low_extrapolation_npts 
    597  
    598         x = self._data.x[:npts_in] 
    599         y = self._low_extrapolation_function.evaluate_model(x=x) 
    600         data_in_range = LoaderData1D(x=x, y=y) 
    601          
    602         return data_out_range, data_in_range 
     597        q_end = self._data.x[max(0, npts_in-1)] 
     598         
     599        if q_start >= q_end: 
     600            return numpy.zeros(0), numpy.zeros(0) 
     601 
     602        return self._get_extrapolated_data(model=self._low_extrapolation_function, 
     603                                           npts=nsteps, 
     604                                           q_start=q_start, q_end=q_end) 
    603605           
    604     def get_extra_data_high(self, npts_in=None, q_end=Q_MAXIMUM, nsteps=INTEGRATION_NSTEPS ): 
    605         """ 
    606            This method generates 2 data sets , the first is a data created during  
    607            low extrapolation . its y is generated from x in [ the maximum of 
    608            data.x to  Q_MAXIMUM] and the outputs of the extrapolator . 
    609             (data is the data used to compute invariant)  
    610            the second is also data produced during the fit but the x range considered 
    611            is within the reel range of data x. 
    612            x uses is from maximum of data.x up to npts_in points before data.x maximum. 
    613            @param npts_in: the number of first points of data to consider  for computing 
    614            y's coming out of the fit. 
    615            @param q_end: is the maximum value to uses for extrapolated data 
    616            @param npts: the number of point used to create extrapolated data  
    617             
    618         """ 
    619         #Create a data from result of the fit for a range outside of the data 
    620         # at low q range 
    621         qmax = max(self._data.x) 
    622         if  q_end != Q_MAXIMUM or nsteps != INTEGRATION_NSTEPS: 
    623             if q_end > Q_MAXIMUM: 
    624                q_end = Q_MAXIMUM 
    625             elif q_end <= qmax: 
    626                 q_end = qmax * 10 
    627                  
    628             #compute the new data with the proper result of the fit for different 
    629             #boundary and step, outside of data 
    630             data_out_range = self._get_extrapolated_data(model=self._high_extrapolation_function, 
    631                                                npts=nsteps, 
    632                                                q_start=qmax, q_end=q_end) 
    633         else: 
    634             data_out_range = LoaderData1D(x=numpy.zeros(0), y=numpy.zeros(0)) 
    635          
    636         #Create data from the result of the fit for a range inside data q range for 
    637         #high q 
    638         if npts_in is None : 
     606    def get_extra_data_high(self, npts_in=None, q_end=Q_MAXIMUM, npts=20): 
     607        """ 
     608            Returns the extrapolated data used for the high-Q invariant calculation. 
     609            By default, the distribution will cover the data points used for the  
     610            extrapolation. The number of overlap points is a parameter (npts_in). 
     611            By default, the maximum q-value of the distribution will be Q_MAXIMUM,  
     612            the maximum q-value used when extrapolating for the purpose of the  
     613            invariant calculation.  
     614             
     615            @param npts_in: number of data points for which the extrapolated data overlap 
     616            @param q_end: is the maximum value to uses for extrapolated data 
     617            @param npts: the number of points in the extrapolated distribution 
     618        """ 
     619        # Get extrapolation range 
     620        if npts_in is None: 
    639621            npts_in = self._high_extrapolation_npts 
    640              
    641         x_len = len(self._data.x) 
    642         x = self._data.x[(x_len-npts_in):] 
    643         y = self._high_extrapolation_function.evaluate_model(x=x) 
    644         data_in_range = LoaderData1D(x=x, y=y) 
    645          
    646         return data_out_range, data_in_range 
    647            
     622        npts = len(self._data.x) 
     623        q_start = self._data.x[min(npts, npts-npts_in+1)] 
     624         
     625        if q_start >= q_end: 
     626            return numpy.zeros(0), numpy.zeros(0) 
     627         
     628        return self._get_extrapolated_data(model=self._high_extrapolation_function, 
     629                                           npts=npts, 
     630                                           q_start=q_start, q_end=q_end) 
    648631      
    649632    def set_extrapolation(self, range, npts=4, function=None, power=None): 
     
    716699    def get_surface(self, contrast, porod_const, extrapolation=None): 
    717700        """ 
    718             Compute the surface of the data. 
     701            Compute the specific surface from the data. 
    719702             
    720703            Implementation: 
Note: See TracChangeset for help on using the changeset viewer.