Changeset 2a11d09 in sasview for Invariant/invariant.py


Ignore:
Timestamp:
Jan 11, 2010 7:11:45 AM (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:
f9a1279
Parents:
793e52f
Message:

reverse changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Invariant/invariant.py

    r793e52f r2a11d09  
    6666        """ 
    6767        self.data  = data 
    68         x_len = len(self.data.x) -1 
    69         #fitting range  
    70         self.qmin =  self.data.x[0]     
    71         if self.qmin == 0: 
    72             self.qmin = Q_MINIMUM  
    73      
    74         self.qmax = self.data.x[x_len] 
    75         #Unsmeared q range 
    76         self._qmin_unsmeared = 0 
    77         self._qmax_unsmeared = self.data.x[x_len] 
    78          
    79         #bin for smear data 
    80         self._first_unsmeared_bin = 0 
    81         self._last_unsmeared_bin  = x_len 
    82          
    83         # Identify the bin range for the unsmeared and smeared spaces 
    84         self.idx = (self.data.x >= self.qmin) & (self.data.x <= self.qmax) 
    85         self.idx_unsmeared = (self.data.x >= self._qmin_unsmeared) \ 
    86                             & (self.data.x <= self._qmax_unsmeared) 
    87    
     68         
     69        # Set qmin as the lowest non-zero value 
     70        self.qmin = Q_MINIMUM 
     71        for q_value in self.data.x: 
     72            if q_value>0:  
     73                self.qmin = q_value 
     74                break 
     75        self.qmax = max(self.data.x) 
     76        
    8877        #get the smear object of data 
    8978        self.smearer = smear_selection( self.data ) 
     79        # Set the q-range information to allow smearing 
     80        self.set_fit_range() 
    9081       
    9182    def set_fit_range(self ,qmin=None, qmax=None): 
     
    10192        self._qmax_unsmeared = self.qmax     
    10293         
    103         self._first_unsmeared_bin = 0 
    104         self._last_unsmeared_bin  = len(self.data.x)-1 
    105          
    106         if self.smearer!=None: 
     94        if self.smearer is not None: 
    10795            self._first_unsmeared_bin, self._last_unsmeared_bin = self.smearer.get_bin_range(self.qmin, self.qmax) 
    10896            self._qmin_unsmeared = self.data.x[self._first_unsmeared_bin] 
     
    119107           @param power = a fixed, otherwise None 
    120108        """ 
    121         power = power 
    122109        fx = numpy.zeros(len(self.data.x)) 
    123         one = numpy.ones(len(self.data.x)) 
    124  
    125         #define dy^2 
    126         try: 
    127             sigma = self.data.dy[self.idx_unsmeared ] 
    128         except: 
    129             print "The dy data for Invariant calculation should be prepared before get to FitFunctor.fit()..." 
    130             sigma = one[self.idx_unsmeared ] 
    131         sigma2 = sigma * sigma 
     110 
     111        # Uncertainty 
     112        if type(self.data.y)==numpy.ndarray and len(self.data.y)==len(self.data.x): 
     113            sigma = self.data.y 
     114        else: 
     115            sigma = numpy.ones(len(self.data.x)) 
    132116 
    133117        # Compute theory data f(x) 
    134         fx = self.data.y[self.idx_unsmeared ]/sigma 
     118        fx[self.idx_unsmeared] = self.data.y[self.idx_unsmeared] 
    135119        ## Smear theory data 
    136120        if self.smearer is not None: 
    137121            fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin) 
    138122         
     123        fx[self.idx_unsmeared] = fx[self.idx_unsmeared]/sigma[self.idx_unsmeared] 
     124         
    139125        ##power is given only for function = power_law     
    140126        if power != None: 
     127            sigma2 = sigma * sigma 
    141128            a = -(power) 
    142             b = (numpy.sum(fx/sigma) - a*numpy.sum(self.data.x[self.idx]/sigma2))/numpy.sum(numpy.ones(len(sigma2))/sigma2) 
     129            b = (numpy.sum(fx[self.idx]/sigma[self.idx]) - a*numpy.sum(self.data.x[self.idx]/sigma2[self.idx]))/numpy.sum(numpy.ones(len(sigma2[self.idx]))/sigma2[self.idx]) 
    143130            return a, b 
    144131        else: 
    145             A = numpy.vstack([ self.data.x[self.idx]/sigma, 
    146                                numpy.ones(len(self.data.x[self.idx]))/sigma]).T 
     132            A = numpy.vstack([ self.data.x[self.idx]/sigma[self.idx], 
     133                               numpy.ones(len(self.data.x[self.idx]))/sigma[self.idx]]).T 
    147134            
    148             a, b = numpy.linalg.lstsq(A, fx)[0] 
     135            a, b = numpy.linalg.lstsq(A, fx[self.idx])[0] 
    149136            return a, b 
    150137 
     
    165152            Initialize variables 
    166153            @param data: data must be of type DataLoader.Data1D 
    167             @param contrast: contrast value of type float 
    168             @param pConst: Porod Constant of type float 
     154            @param background: Background value. The data will be corrected before processing 
     155            @param scale: Scaling factor for I(q). The data will be corrected before processing 
    169156        """ 
    170157        # Background and scale should be private data member if the only way to 
     
    202189            #Process only data that inherited from DataLoader.Data_info.Data1D 
    203190            raise ValueError,"Data must be of type DataLoader.Data1D" 
    204         new_data = self._scale * data - self._background 
    205         try: 
    206             #All data should pass here. 
    207             new_data.dy = data.dy 
    208             new_data.dxl = data.dxl 
    209             new_data.dxw = data.dxw         
    210         except: 
    211             #in case... 
    212             new_data.dy = numpy.ones(len(data.x)) 
    213             new_data.dxl = numpy.zeros(len(data.x)) 
    214             new_data.dxw = numpy.zeros(len(data.x))        
     191             
     192        new_data = (self._scale * data) - self._background 
     193         
     194        # Copy data that is not copied by the operations 
     195        #TODO: fix this in DataLoader 
     196        new_data.dxl = data.dxl 
     197        new_data.dxw = data.dxw         
    215198 
    216199        return new_data 
     
    410393            note: if data doesn't contain dy assume dy= math.sqrt(data.y) 
    411394        """ 
    412         if data is None: 
    413             data = self.data 
    414              
    415395        if len(data.x) <= 1 or len(data.y) <= 1 or \ 
    416396            len(self.data.x) != len(self.data.y): 
     
    548528        qmin = self._data.x[0] 
    549529        qmax = self._data.x[self._low_extrapolation_npts - 1] 
    550         try: 
    551             # fit the data with a model to get the appropriate parameters 
    552             a, b = self._fit(function=self._low_extrapolation_function, 
    553                               qmin=qmin, 
    554                               qmax=qmax, 
    555                               power=self._low_extrapolation_power) 
    556         except: 
    557             #raise 
    558             return None 
     530         
     531        # Extrapolate the low-Q data 
     532        #TODO: this fit fails. Fix it. 
     533        a, b = self._fit(function=self._low_extrapolation_function, 
     534                          qmin=qmin, 
     535                          qmax=qmax, 
     536                          power=self._low_extrapolation_power) 
    559537         
    560538        #q_start point 
Note: See TracChangeset for help on using the changeset viewer.