Changeset 22e8d49 in sasview for sansview/perspectives


Ignore:
Timestamp:
Mar 2, 2009 12:12:43 PM (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:
6285a79
Parents:
54905de
Message:

error handle for compute chisqr

File:
1 edited

Legend:

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

    rb491d6e r22e8d49  
    2020        @note: For Fit to be performed the user should check at least one parameter 
    2121        on fit Panel window. 
    22    
    2322    """ 
    2423    ## Internal name for the AUI manager 
     
    2625    ## Title to appear on top of the window 
    2726    window_caption = "Fit Page" 
     27     
    2828    name=None 
    2929     
     
    3434            Initialization of the Panel 
    3535        """ 
     36        #Data used for fitting  
    3637        self.data = data 
     38        # flag to allow data2D plot 
    3739        self.enable2D=False 
     40        #fit page manager  
    3841        self.manager = None 
     42        #Store the parent of this panel parent 
     43        # For this application fitpanel is the parent 
    3944        self.parent  = parent 
     45        # Event_owner is the owner of model event 
    4046        self.event_owner = None 
    4147        #panel interface 
     
    7379        self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) 
    7480        self.vbox.Add(self.sizer9) 
    75         
    76          
    7781        #---------sizer 1 draw-------------------------------- 
     82        #Filling the sizer containing data related fields 
    7883        self.DataSource  =wx.StaticText(self, -1,str(data.name)) 
    79         #Filing the sizer containing data related fields 
    8084        ix = 0 
    8185        iy = 1 
    8286        self.sizer1.Add(wx.StaticText(self, -1, 'Data Source Name : '),(iy,ix),\ 
    8387                 (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 
    84         
    8588        ix += 1 
    8689        self.sizer1.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
     
    111114         
    112115        #----sizer 3 draw-------------------------------- 
     116        #Filling the sizer containing instruments smearing info. 
    113117        self.disable_smearer = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) 
    114118        self.enable_smearer = wx.RadioButton(self, -1, 'Yes', (10, 30)) 
     
    128132             
    129133        #------------------ sizer 4  draw------------------------    
     134        # This sizer contains model list and chisqr value  
     135        #filling sizer4 
    130136        self.modelbox = wx.ComboBox(self, -1) 
    131137        self.tcChi    =  wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) 
     
    133139        self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) 
    134140        self.text1_1.Hide() 
    135         #filling sizer2 
    136141        ix = 0 
    137142        iy = 1 
     
    146151        self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    147152        #----------sizer6------------------------------------------------- 
     153        #Sizer containing info on polydispersity 
    148154        self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) 
    149155        self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) 
     
    161167        iy+=1 
    162168        self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)   
    163  
    164          
    165169        #---------sizer 9 draw---------------------------------------- 
    166170         
     
    170174        self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) 
    171175        self.btFit.SetToolTipString("Perform fit.") 
    172          ## Q range 
    173         #print "self.data fitpage1D" , self.data,hasattr(self.data,"data") 
     176        ## Q range 
    174177        # Reversed to the codes; Need to think  carefully about consistency in q between 2D plot and fitting 
    175178        if not hasattr(self.data,"data"): 
     
    183186            radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) 
    184187            radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) 
    185             #self.qmin_x = 0 
    186             #self.qmax_x = max(radius1, radius2, radius3, radius4) 
    187             self.qmin_x= 0#self.data.xmin 
     188             
     189            self.qmin_x= 0 
    188190            self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) 
    189191                            +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax            
    190             #print "data2D range",self.qmax_x 
    191192         
    192193        self.num_points= 100 
    193           
    194          
    195          
     194        # The minimum fitting range 
    196195        self.qmin    = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 
    197196        self.qmin.SetValue(str(format_number(self.qmin_x))) 
     
    200199        self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) 
    201200        self.qmin.Enable() 
    202          
     201        #The maximum fitting range 
    203202        self.qmax    = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 
    204203        self.qmax.SetValue(str(format_number(self.qmax_x))) 
     
    207206        self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) 
    208207        self.qmax.Enable() 
    209  
     208        #The number of points to plots 
    210209        self.npts    = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 
    211210        self.npts.SetValue(format_number(self.num_points)) 
     
    225224        self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ 
    226225                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    227         #ix += 1 
    228         #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ 
    229         #                    wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    230226        ix = 0 
    231227        iy += 1 
     
    241237        ix += 1 
    242238        self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    243         """ 
    244         id = wx.NewId() 
    245         self.btStopFit =wx.Button(self,id,'Stop') 
    246         self.btStopFit.Bind(wx.EVT_BUTTON, self.onStopFit,id=id) 
    247         self.btStopFit.SetToolTipString("Stop the current fitting job.") 
    248         self.btStopFit.Hide() 
    249         ix += 1 
    250         self.sizer9.Add(self.btStopFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    251         """ 
     239        
    252240        ix =0 
    253241        iy+=1  
    254242        self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    255243         
    256         # contains link between  model ,all its parameters, and panel organization 
     244        # Contains link between  model ,all its parameters, and panel organization 
    257245        self.parameters=[] 
     246        # Contains list of parameters that cannot be fitted and reference to  
     247        #panel objects  
    258248        self.fixed_param=[] 
     249        # Contains list of parameters with dispersity and reference to  
     250        #panel objects  
    259251        self.fittable_param=[] 
    260252        #list of dispersion paramaters 
     
    264256        # model on which the fit would be performed 
    265257        self.model=None 
     258         
    266259        self.back_up_model= None 
    267260        #dictionary of model name and model class 
     
    275268            self.qmax.Enable()  
    276269 
    277         
     270     
    278271        self.vbox.Layout() 
    279272        self.vbox.Fit(self)  
     
    286279    
    287280    def compute_chisqr2D(self): 
    288         """ @param fn: function that return model value 
    289             @return residuals 
     281        """  
     282            compute chi square given a model and data 2D and set the value 
     283            to the tcChi txtcrl 
    290284        """ 
    291285        flag=self.checkFitRange() 
     
    293287        if flag== True: 
    294288            try: 
    295                 #print "compute",self.data.err_data 
    296289                self.qmin_x = float(self.qmin.GetValue()) 
    297290                self.qmax_x = float(self.qmax.GetValue()) 
    298291                for i in range(len(self.data.x_bins)): 
    299                     #if self.data.x_bins[i]>= self.qmin_x and self.data.x_bins[i]<= self.qmax_x: 
    300                         for j in range(len(self.data.y_bins)): 
    301                             if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): 
    302                                 if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): 
    303                             #if self.data.y_bins[j]>= self.qmin_x and self.data.y_bins[j]<= self.qmax_x: 
    304                                     chisqrji=(self.data.data[j][i]- self.model.runXY(\ 
    305                                                                                         [self.data.y_bins[j],self.data.x_bins[i]]))\ 
    306                                                                                         /self.data.err_data[j][i] 
    307                                     res.append( math.pow(chisqrji,2) ) 
     292                    for j in range(len(self.data.y_bins)): 
     293                        #Check the range containing data between self.qmin_x and self.qmax_x 
     294                        if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): 
     295                            if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): 
     296                                chisqrji=(self.data.data[j][i]- self.model.runXY(\ 
     297                                                                                    [self.data.y_bins[j],self.data.x_bins[i]]))\ 
     298                                                                                    /self.data.err_data[j][i] 
     299                                #Vector containing residuals 
     300                                res.append( math.pow(chisqrji,2) ) 
     301                # compute sum of residual 
    308302                sum=0 
    309                 
    310303                for item in res: 
    311304                    if numpy.isfinite(item): 
    312305                        sum +=item 
    313                 #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum 
    314                 #print len(res) 
    315306                self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) 
    316307            except: 
    317                 raise 
    318308                wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 
    319309                            "Chisqr cannot be compute: %s"% sys.exc_value)) 
    320          
    321     def onStopFit(self, event): 
    322         self.manager.stop_fit() 
    323         self.btStopFit.Hide() 
    324         self.btFit.Show(True) 
    325         self.vbox.Layout() 
    326         self.SetScrollbars(20,20,55,40) 
    327         self.Layout() 
    328         self.parent.GetSizer().Layout() 
     310                return 
     311     
    329312         
    330313    def compute_chisqr(self): 
    331         """ @param fn: function that return model value 
    332             @return residuals 
    333         """ 
    334          
     314        """  
     315            compute chi square given a model and data 1D and set the value 
     316            to the tcChi txtcrl 
     317        """ 
    335318        flag=self.checkFitRange() 
    336         #print "flag", flag 
    337319        if flag== True: 
    338320            try: 
     
    343325                    self.qmin_x = float(self.qmin.GetValue()) 
    344326                    self.qmax_x = float(self.qmax.GetValue()) 
    345                     #print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x 
     327                    # return residuals within self.qmin_x and self.qmax_x 
    346328                    x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] 
    347329                    if self.qmin_x==None and self.qmax_x==None:  
     
    354336                        temp=(y[idx] - fx)/dy[idx] 
    355337                        res= temp*temp 
    356                     
     338                    #sum of residuals 
    357339                    sum=0 
    358340                    for item in res: 
     
    363345                wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 
    364346                            "Chisqr cannot be compute: %s"% sys.exc_value)) 
     347                return  
    365348             
    366349    def _on_select_model(self,event): 
     
    382365            if hasattr(item, "name"): 
    383366                name = item.name 
    384             #print "fitpage: _on_select_model model name",name ,event.GetString() 
     367             
    385368            if name == None: 
    386369                self.qmin.Disable() 
     
    401384                except: 
    402385                    raise #ValueError,"model.name is not equal to model class name" 
    403                 break        
     386                break   
     387             
     388                  
    404389    def onFit(self,event): 
    405390        """ signal for fitting""" 
     
    411396        self.qmax_x =float( self.qmax.GetValue()) 
    412397        if len(self.param_toFit) >0 and flag==True: 
    413             #if self.data.name == self.model.__class__.__name__: 
    414                 #print "when here have the same name " 
    415                 #wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
    416                             #"Attempt to fit model %s with itself. fit will\ 
    417                             #not be performed"%self.data.name)) 
    418                 #return  
    419398            self.manager.schedule_for_fit( value=1,fitproblem =None)  
    420399            if hasattr(self.data, "data"): 
     400                #single fit for Data2D 
    421401                self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, 
    422402                                            ymin=self.data.ymin, ymax=self.data.ymax, 
    423403                                            xmin=self.data.xmin,xmax=self.data.xmax) 
    424                 #self.btStopFit.Show() 
    425                 #self.btFit.Hide() 
    426404            else: 
    427                  self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) 
    428                  #self.btStopFit.Show() 
    429                  #self.btFit.Hide() 
     405                #single fit for Data1D 
     406                self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) 
     407                 
    430408            self.vbox.Layout() 
    431409            self.SetScrollbars(20,20,55,40) 
     
    433411            self.parent.GetSizer().Layout() 
    434412        else: 
    435               wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
     413            wx.PostEvent(self.parent.parent, StatusEvent(status=\ 
    436414                            "Select at least one parameter to fit ")) 
    437415   
     
    445423            flag=self.checkFitRange() 
    446424            if flag==True and self.model!=None: 
    447                 #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() 
    448425                self.manager.redraw_model(float(self.xmin.GetValue())\ 
    449426                                               ,float(self.xmax.GetValue())) 
     
    473450        """ 
    474451        self.set_model() 
    475         self.compute_chisqr() 
     452        try: 
     453            self.compute_chisqr() 
     454        except: 
     455            pass 
    476456         
    477457    def set_model(self):  
     
    526506        else: 
    527507            self.cb1.SetValue(False) 
    528         """     
    529         #qmax, qmin Input enabled all the time    
    530         self.qmin.Enable() 
    531         self.qmax.Enable()       
    532         """ 
    533    
     508        
     509        
    534510    def onsetValues(self,chisqr, out,cov): 
    535511        """ 
     
    540516        
    541517        """ 
    542         #print "fitting : onsetvalues out",out 
    543518        self.tcChi.SetLabel(format_number(chisqr)) 
    544519        params = {} 
Note: See TracChangeset for help on using the changeset viewer.