Changeset 82703a1 in sasview for Invariant


Ignore:
Timestamp:
Jan 15, 2010 12:31:50 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:
aafa962
Parents:
59a41066
Message:

working version of invariant refactored

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Invariant/invariant.py

    r59a41066 r82703a1  
    99from DataLoader.data_info import Data1D as LoaderData1D 
    1010from DataLoader.qsmearing import smear_selection 
    11  
    1211 
    1312# The minimum q-value to be used when extrapolating 
     
    2524        function given some x, y  
    2625    """ 
    27     def set_value(self, a, b): 
     26    def transform_values(self, a, b): 
    2827        """ 
    2928            set private member 
    3029        """ 
     30        return NotImplemented 
     31     
    3132    def transform_value(self, value): 
    3233        """ 
    33             Can use one of the function transform_x2 or transform_logx 
    34         """ 
    35     def transform_x2(self, value): 
    36         """ 
    3734            @param value: float 
    38         """ 
    39         return value **2 
    40      
    41     def transform_to_logx(self, value): 
    42         """ 
    43             @param value: float 
    44         """ 
    45         return math.log(value) 
    46          
     35            return f(value) 
     36        """ 
     37        return NotImplemented 
     38      
    4739    def inverse_transform_value(self, value): 
    4840        """ 
     
    5143            @param value : float 
    5244        """ 
     45        return NotImplemented 
     46     
    5347    def get_extrapolated_data(self, data, q_start=Q_MINIMUM, q_end=Q_MAXIMUM): 
    5448        """ 
    5549            @return extrapolate data create from data 
    5650        """ 
     51        return NotImplemented 
     52     
    5753    def transform_data(self, x): 
    5854        """ 
     
    6157            return x, y transform given a specific function 
    6258        """ 
     59        return NotImplemented 
     60     
    6361    def inverse_transform_data(self, x, y): 
    6462        """ 
    6563            reverse transform x, y given a specific function 
    6664        """ 
     65        return NotImplemented 
     66     
    6767    def transform_error(self, y , dy=None): 
    6868        if dy is None: 
     
    9696        result_data.dxl = dxl 
    9797        result_data.dxw = dxw 
    98          
    99         
    10098        return result_data 
    10199   
     
    111109     
    112110    def transform_value(self, value): 
    113         return self.transform_x2(value) 
    114      
    115     def set_value(self, a, b): 
    116         # b is the radius value of the guinier function 
    117         if b >=0 : 
    118                 raise ValueError("Guinier fit was not converged") 
    119         else: 
    120                 b = math.sqrt(-3 * b) 
     111        """ 
     112            Square input value 
     113            @param value: of type float 
     114        """ 
     115        return value * value 
     116     
     117    def transform_values(self, a, b): 
     118        """ 
     119           assign new value to the scale and the radius 
     120        """ 
     121        b = math.sqrt(-3 * b) 
    121122        a = math.exp(a) 
    122      
    123123        self.scale = a 
    124124        self.radius = b 
     125        return a, b 
    125126         
    126127    def transform_data(self, x): 
    127128        """ 
    128             given a scale and a radius transform x, y using a guinier 
    129             function 
     129            return F(x)= scale* e-((radius*x)**2/3) 
    130130        """ 
    131131        return self._guinier(x) 
     
    133133    def inverse_transform_data(self, x, y): 
    134134        """ 
    135             given a scale and a radius transform x, y using a inverse guinier 
    136             function 
     135            this function finds x, y given equation1: y = ax + b 
     136            and equation2: y1 = scale* e-((radius*x1)**2/3). 
     137            where equation1, equation2 are equivalent 
     138            imply:  x = x1*x1 
     139                    y = ln(y1) 
     140                    b = math.exp(scale) 
     141                    a = -radius**2/3 
     142            @return  x, y 
    137143        """ 
    138144        result_x = numpy.array([i * i for i in x])    
    139145        result_y = numpy.array([math.log(j) for j in y]) 
    140          
    141146        return result_x, result_y  
    142147         
     
    155160        if self.radius <= 0: 
    156161            raise ValueError("Rg expected positive value, but got %s"%self.radius)   
    157  
    158162        value = numpy.array([math.exp(-((self.radius * i)**2/3)) for i in x ])  
    159         
    160163        return self.scale * value 
    161164 
     
    170173        self.power = power 
    171174         
    172     def set_value(self, a, b): 
     175    def transform_values(self, a, b): 
     176        """ 
     177            Assign new value to the scale and the power  
     178        """ 
    173179        b = -1 * b 
    174         if b <= 0: 
    175                 raise ValueError("Power_law fit expected posive power, but got %s"%power) 
    176180        a = math.exp(a) 
     181        self.power = b 
    177182        self.scale = a 
    178         self.power = b 
    179          
    180     def transform_data(self, x, a=None, b=None): 
     183        return a, b 
     184         
     185    def transform_value(self, value): 
     186        """ 
     187            compute log(value) 
     188        """ 
     189        return math.log(value) 
     190         
     191    def transform_data(self, x): 
    181192        """ 
    182193            given a scale and a radius transform x, y using a power_law 
    183194            function 
    184195        """ 
    185         if  a is not None: 
    186             self.scale = a 
    187         if  b is not None: 
    188             self.power = b 
    189              
    190196        return self._power_law(x) 
    191197        
     
    195201            function 
    196202        """ 
    197         result_x = numpy.array([i * i for i in x])    
     203        result_x = numpy.array([math.log(i) for i in x])    
    198204        result_y = numpy.array([math.log(j) for j in y]) 
    199205         
     
    212218        """ 
    213219        if self.power <= 0: 
    214             raise ValueError("Power_law function expected positive power, but got %s"%power) 
     220            raise ValueError("Power_law function expected positive power, but got %s"%self.power) 
    215221        if self.scale <= 0: 
    216222            raise ValueError("scale expected positive value, but got %s"%self.scale)  
     
    297303            
    298304            a, b = numpy.linalg.lstsq(A, fx[self.idx])[0] 
     305             
    299306            return a, b 
    300307 
     
    352359            #Process only data that inherited from DataLoader.Data_info.Data1D 
    353360            raise ValueError,"Data must be of type DataLoader.Data1D" 
    354              
    355         new_data = (self._scale * data) - self._background 
    356          
    357         # Copy data that is not copied by the operations 
    358         #TODO: fix this in DataLoader 
    359         #new_data.dxl = data.dxl 
    360         #new_data.dxw = data.dxw         
    361  
    362         return new_data 
    363          
     361        new_data = (self._scale * data) - self._background    
     362        new_data.dxl = data.dxl 
     363        new_data.dxw = data.dxw  
     364        return  new_data 
     365      
    364366    def _fit(self, function, qmin=Q_MINIMUM, qmax=Q_MAXIMUM, power=None): 
    365367        """ 
     
    386388        fit_data.dxl = self._data.dxl 
    387389        fit_data.dxw = self._data.dxw    
    388         functor = Extrapolator(data=fit_data) 
    389         functor.set_fit_range(qmin=qmin, qmax=qmax) 
    390         b, a = functor.fit(power=power)          
    391         return b, a 
     390        extrapolator = Extrapolator(data=fit_data) 
     391        extrapolator.set_fit_range(qmin=qmin, qmax=qmax) 
     392        b, a = extrapolator.fit(power=power)  
     393         
     394        return function.transform_values(a=a, b=b) 
    392395     
    393396    def _get_qstar(self, data): 
     
    502505        """ 
    503506        if data is None: 
    504             data = self.data 
    505      
     507            data = self._data 
    506508        if data.is_slit_smeared(): 
    507509            return self._get_qstar_smear_uncertainty(data) 
     
    509511            return self._get_qstar_unsmear_uncertainty(data) 
    510512         
    511     def _get_qstar_unsmear_uncertainty(self, data=None): 
     513    def _get_qstar_unsmear_uncertainty(self, data): 
    512514        """ 
    513515            Compute invariant uncertainty with with pinhole data. 
     
    554556                return math.sqrt(sum) 
    555557         
    556     def _get_qstar_smear_uncertainty(self): 
     558    def _get_qstar_smear_uncertainty(self, data): 
    557559        """ 
    558560            Compute invariant uncertainty with slit smeared data. 
     
    571573            note: if data doesn't contain dy assume dy= math.sqrt(data.y) 
    572574        """ 
    573         #if data is None: 
    574         #    data = self._data 
    575              
    576575        if not data.is_slit_smeared(): 
    577576            msg = "_get_qstar_smear_uncertainty need slit smear data " 
     
    659658        qmin = self._data.x[0] 
    660659        qmax = self._data.x[self._low_extrapolation_npts - 1] 
    661          
    662660        # Extrapolate the low-Q data 
    663661        #TODO: this fit fails. Fix it. 
    664         b, a = self._fit(function=self._low_extrapolation_function, 
     662        a, b = self._fit(function=self._low_extrapolation_function, 
    665663                          qmin=qmin, 
    666664                          qmax=qmax, 
    667665                          power=self._low_extrapolation_power) 
    668          
    669666        #q_start point 
    670667        q_start = Q_MINIMUM 
    671668        if Q_MINIMUM >= qmin: 
    672669            q_start = qmin/10 
    673              
    674         self._low_extrapolation_function.set_value(a= a, b=b) 
     670         
    675671        data_min = self._low_extrapolation_function.get_extrapolated_data(data=self._data,  
    676672                                            npts=INTEGRATION_NSTEPS, 
    677673                              q_start=q_start, q_end=qmin, smear_indice=0) 
    678         
    679674        return data_min 
    680675           
     
    693688            @return: a new data of type Data1D 
    694689        """ 
    695         # Data boundaries for fiiting 
     690        # Data boundaries for fitting 
    696691        x_len = len(self._data.x) - 1 
    697         q_start = self._data.x[x_len - (self._high_extrapolation_npts - 1)] 
     692        qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1)] 
    698693        qmax = self._data.x[x_len] 
    699         smear_indice = x_len 
     694        q_end = Q_MAXIMUM 
     695        smear_indice = 0 
     696        if self._data.dxl is not None: 
     697            smear_indice = len(self._data.dxl) - 1 
    700698         
    701699        # fit the data with a model to get the appropriate parameters 
    702         b, a = self._fit(function=self._high_extrapolation_function, 
    703                                qmin=q_start, 
     700        a, b = self._fit(function=self._high_extrapolation_function, 
     701                               qmin=qmin, 
    704702                                qmax=qmax, 
    705703                                power=self._high_extrapolation_power) 
    706    
    707704        #create new Data1D to compute the invariant 
    708         self._high_extrapolation_function.set_value(a=a, b=b) 
    709705        data_max = self._high_extrapolation_function.get_extrapolated_data(data=self._data,  
    710706                                            npts=INTEGRATION_NSTEPS, 
    711                                             q_start=q_start, q_end=qmax, 
     707                                            q_start=qmax, q_end=q_end, 
    712708                                            smear_indice=smear_indice) 
    713709        return data_max 
Note: See TracChangeset for help on using the changeset viewer.