Ignore:
Timestamp:
Jul 13, 2009 11:26:32 AM (15 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:
342d9197
Parents:
70bf68c
Message:

working on refactoring gui_thread

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sansview/perspectives/fitting/gui_thread.py

    re733619 r077809c  
    99 
    1010from data_util.calcthread import CalcThread 
     11from sans.fit.AbstractFitEngine import FitData2D, FitData1D, SansAssembly 
     12 
    1113 
    1214class CalcChisqr1D(CalcThread): 
     
    1416       Compute chisqr 
    1517    """ 
    16     def __init__(self, x, y,dy, model, 
     18    def __init__(self, data1d, model, 
    1719                 smearer=None, 
    1820                 qmin=None, 
     
    2729                 yieldtime, 
    2830                 worktime) 
    29         self.smearer =smearer 
    30         self.y = numpy.array(y) 
    31         self.x = numpy.array(x) 
    32         self.dy= copy.deepcopy(dy) 
     31         
     32        if model ==None or data1d ==None: 
     33            raise ValueError, "Need data and model to compute chisqr" 
     34         
     35        if data1d.__class__.__name__ !="Data1D": 
     36            msg= str(data1d.__class__.__name__) 
     37            raise ValueError, "need Data1D to compute chisqr. Current class %s"%msg 
     38         
     39        self.fitdata= FitData1D(data1d, smearer=smearer) 
     40        self.fitdata.setFitRange(qmin=qmin,qmax=qmax) 
    3341        self.model = model 
    34         self.qmin = qmin 
    35         self.qmax = qmax 
    36         self.smearer = smearer 
     42        
    3743        self.starttime = 0   
    3844         
     
    5359        self.starttime = time.time() 
    5460        
    55         x,y = [numpy.asarray(v) for v in (self.x,self.y)] 
    56         if self.dy==None or self.dy==[]: 
    57             self.dy= numpy.zeros(len(self.y)) 
    58         self.dy[self.dy==0]=1 
    59          
    60         if self.qmin==0.0 and not numpy.isfinite(self.y[self.qmin]): 
    61             self.qmin = min(self.x[sel.x!=0])       
    62         elif self.qmin==None: 
    63             self.qmin= min(self.x) 
    64          
    65         if self.qmax==None: 
    66             self.qmax= max(self.x) 
    67              
    68         fx = numpy.zeros(len(self.x))  
    69          
    7061        output= None 
    7162        res=[] 
    72         try:  
    73              
    74             for i_x in range(len(self.x)): 
    75                 
    76                 # Check whether we need to bail out 
    77                 self.isquit()   
    78                 
    79                 fx[i_x]=self.model.run(self.x[i_x]) 
    80                  
    81             if self.smearer!=None: 
    82                 fx= self.smearer(fx) 
    83                  
    84             for i_y in range(len(fx)): 
    85                 # Check whether we need to bail out 
    86                 self.isquit()    
    87                 
    88                 temp=(self.y[i_y] - fx[i_y])/self.dy[i_y] 
    89                 res.append(temp*temp) 
    90             #sum of residuals 
     63        try: 
     64            res = self.fitdata.residuals(self.model.run) 
    9165            sum=0 
    9266            for item in res: 
     
    9468                self.isquit()   
    9569                if numpy.isfinite(item): 
    96                     sum +=item 
    97             output = sum/ len(res) 
     70                    sum +=item*item 
     71            if len(res)>0: 
     72                output = sum/ len(res) 
     73             
    9874            elapsed = time.time()-self.starttime 
    9975            self.complete(output= output,  elapsed=elapsed) 
     
    10379            # Real code should not print, but this is an example... 
    10480            raise 
    105         except: 
     81        except:  
    10682            raise 
    10783         
     
    11187    """ 
    11288     
    113     def __init__(self, x_bins, y_bins,data,err_data, model, 
     89    def __init__(self,data2d, model, 
    11490                 qmin, 
    11591                 qmax, 
     
    12399                 yieldtime, 
    124100                 worktime) 
    125        
    126         self.y_bins = y_bins 
    127         self.x_bins = x_bins 
    128         self.data= data 
    129         self.err_data= copy.deepcopy(err_data) 
     101         
     102        if model ==None or data2d ==None: 
     103            raise ValueError, "Need data and model to compute chisqr" 
     104         
     105        if data2d.__class__.__name__ !="Data2D": 
     106            msg= str(data2d.__class__.__name__) 
     107            raise ValueError, "need Data2D to compute chisqr. Current class %s"%msg 
     108         
     109        self.fitdata = FitData2D(data2d) 
     110        self.fitdata.setFitRange(qmin=qmin,qmax=qmax) 
     111      
    130112        self.model = model 
    131         self.qmin = qmin 
    132         self.qmax = qmax 
    133113       
    134114        self.starttime = 0   
     
    149129        """ 
    150130        self.starttime = time.time() 
    151         if self.model ==None: 
    152             return 
    153         if self.data==None: 
    154             return 
    155         if self.err_data==None or self.err_data==[]: 
    156             self.err_data= numpy.zeros(len(self.x_bins),len(self.y_bins)) 
    157              
    158         self.err_data[self.err_data==0]=1 
    159              
     131        
    160132        output= None 
    161133        res=[] 
    162134        try: 
    163            
    164             for i in range(len(self.x_bins)): 
    165                 # Check whether we need to bail out 
    166                 self.isquit()    
    167                 for j in range(len(self.y_bins)): 
    168                     #Check the range containing data between self.qmin_x and self.qmax_x 
    169                     value =  math.pow(self.x_bins[i],2)+ math.pow(self.y_bins[j],2) 
    170                     if value >= math.pow(self.qmin,2) and value <= math.pow(self.qmax,2): 
    171                          
    172                         temp = [self.x_bins[i],self.y_bins[j]] 
    173                         error= self.err_data[j][i] 
    174                         chisqrji = (self.data[j][i]- self.model.runXY(temp ))/error 
    175                         #Vector containing residuals 
    176                         res.append( math.pow(chisqrji,2) ) 
    177   
     135            res = self.fitdata.residuals(self.model.run) 
    178136            sum=0 
    179137            for item in res: 
     
    181139                self.isquit()   
    182140                if numpy.isfinite(item): 
    183                     sum +=item 
    184             output = sum/ len(res) 
     141                    sum +=item*item 
     142            if len(res)>0: 
     143                output = sum/ len(res) 
     144             
    185145            elapsed = time.time()-self.starttime 
    186146            self.complete(output= output,  elapsed=elapsed) 
     
    190150            # Real code should not print, but this is an example... 
    191151            raise 
     152        except:  
     153            raise 
Note: See TracChangeset for help on using the changeset viewer.