Changeset 22e8d49 in sasview
- Timestamp:
- Mar 2, 2009 10:12:43 AM (16 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitpage1D.py
rb491d6e r22e8d49 20 20 @note: For Fit to be performed the user should check at least one parameter 21 21 on fit Panel window. 22 23 22 """ 24 23 ## Internal name for the AUI manager … … 26 25 ## Title to appear on top of the window 27 26 window_caption = "Fit Page" 27 28 28 name=None 29 29 … … 34 34 Initialization of the Panel 35 35 """ 36 #Data used for fitting 36 37 self.data = data 38 # flag to allow data2D plot 37 39 self.enable2D=False 40 #fit page manager 38 41 self.manager = None 42 #Store the parent of this panel parent 43 # For this application fitpanel is the parent 39 44 self.parent = parent 45 # Event_owner is the owner of model event 40 46 self.event_owner = None 41 47 #panel interface … … 73 79 self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) 74 80 self.vbox.Add(self.sizer9) 75 76 77 81 #---------sizer 1 draw-------------------------------- 82 #Filling the sizer containing data related fields 78 83 self.DataSource =wx.StaticText(self, -1,str(data.name)) 79 #Filing the sizer containing data related fields80 84 ix = 0 81 85 iy = 1 82 86 self.sizer1.Add(wx.StaticText(self, -1, 'Data Source Name : '),(iy,ix),\ 83 87 (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 84 85 88 ix += 1 86 89 self.sizer1.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) … … 111 114 112 115 #----sizer 3 draw-------------------------------- 116 #Filling the sizer containing instruments smearing info. 113 117 self.disable_smearer = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) 114 118 self.enable_smearer = wx.RadioButton(self, -1, 'Yes', (10, 30)) … … 128 132 129 133 #------------------ sizer 4 draw------------------------ 134 # This sizer contains model list and chisqr value 135 #filling sizer4 130 136 self.modelbox = wx.ComboBox(self, -1) 131 137 self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) … … 133 139 self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) 134 140 self.text1_1.Hide() 135 #filling sizer2136 141 ix = 0 137 142 iy = 1 … … 146 151 self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 147 152 #----------sizer6------------------------------------------------- 153 #Sizer containing info on polydispersity 148 154 self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) 149 155 self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) … … 161 167 iy+=1 162 168 self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 163 164 165 169 #---------sizer 9 draw---------------------------------------- 166 170 … … 170 174 self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) 171 175 self.btFit.SetToolTipString("Perform fit.") 172 ## Q range 173 #print "self.data fitpage1D" , self.data,hasattr(self.data,"data") 176 ## Q range 174 177 # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting 175 178 if not hasattr(self.data,"data"): … … 183 186 radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) 184 187 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 188 190 self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) 189 191 +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax 190 #print "data2D range",self.qmax_x191 192 192 193 self.num_points= 100 193 194 195 194 # The minimum fitting range 196 195 self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 197 196 self.qmin.SetValue(str(format_number(self.qmin_x))) … … 200 199 self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) 201 200 self.qmin.Enable() 202 201 #The maximum fitting range 203 202 self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 204 203 self.qmax.SetValue(str(format_number(self.qmax_x))) … … 207 206 self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) 208 207 self.qmax.Enable() 209 208 #The number of points to plots 210 209 self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 211 210 self.npts.SetValue(format_number(self.num_points)) … … 225 224 self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ 226 225 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 227 #ix += 1228 #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\229 # wx.EXPAND|wx.ADJUST_MINSIZE, 0)230 226 ix = 0 231 227 iy += 1 … … 241 237 ix += 1 242 238 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 252 240 ix =0 253 241 iy+=1 254 242 self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 255 243 256 # contains link between model ,all its parameters, and panel organization244 # Contains link between model ,all its parameters, and panel organization 257 245 self.parameters=[] 246 # Contains list of parameters that cannot be fitted and reference to 247 #panel objects 258 248 self.fixed_param=[] 249 # Contains list of parameters with dispersity and reference to 250 #panel objects 259 251 self.fittable_param=[] 260 252 #list of dispersion paramaters … … 264 256 # model on which the fit would be performed 265 257 self.model=None 258 266 259 self.back_up_model= None 267 260 #dictionary of model name and model class … … 275 268 self.qmax.Enable() 276 269 277 270 278 271 self.vbox.Layout() 279 272 self.vbox.Fit(self) … … 286 279 287 280 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 290 284 """ 291 285 flag=self.checkFitRange() … … 293 287 if flag== True: 294 288 try: 295 #print "compute",self.data.err_data296 289 self.qmin_x = float(self.qmin.GetValue()) 297 290 self.qmax_x = float(self.qmax.GetValue()) 298 291 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 308 302 sum=0 309 310 303 for item in res: 311 304 if numpy.isfinite(item): 312 305 sum +=item 313 #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum314 #print len(res)315 306 self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) 316 307 except: 317 raise318 308 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 319 309 "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 329 312 330 313 def compute_chisqr(self): 331 """ @param fn: function that return model value332 @return residuals333 """334 314 """ 315 compute chi square given a model and data 1D and set the value 316 to the tcChi txtcrl 317 """ 335 318 flag=self.checkFitRange() 336 #print "flag", flag337 319 if flag== True: 338 320 try: … … 343 325 self.qmin_x = float(self.qmin.GetValue()) 344 326 self.qmax_x = float(self.qmax.GetValue()) 345 # print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x327 # return residuals within self.qmin_x and self.qmax_x 346 328 x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] 347 329 if self.qmin_x==None and self.qmax_x==None: … … 354 336 temp=(y[idx] - fx)/dy[idx] 355 337 res= temp*temp 356 338 #sum of residuals 357 339 sum=0 358 340 for item in res: … … 363 345 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 364 346 "Chisqr cannot be compute: %s"% sys.exc_value)) 347 return 365 348 366 349 def _on_select_model(self,event): … … 382 365 if hasattr(item, "name"): 383 366 name = item.name 384 #print "fitpage: _on_select_model model name",name ,event.GetString()367 385 368 if name == None: 386 369 self.qmin.Disable() … … 401 384 except: 402 385 raise #ValueError,"model.name is not equal to model class name" 403 break 386 break 387 388 404 389 def onFit(self,event): 405 390 """ signal for fitting""" … … 411 396 self.qmax_x =float( self.qmax.GetValue()) 412 397 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 #return419 398 self.manager.schedule_for_fit( value=1,fitproblem =None) 420 399 if hasattr(self.data, "data"): 400 #single fit for Data2D 421 401 self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, 422 402 ymin=self.data.ymin, ymax=self.data.ymax, 423 403 xmin=self.data.xmin,xmax=self.data.xmax) 424 #self.btStopFit.Show()425 #self.btFit.Hide()426 404 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 430 408 self.vbox.Layout() 431 409 self.SetScrollbars(20,20,55,40) … … 433 411 self.parent.GetSizer().Layout() 434 412 else: 435 413 wx.PostEvent(self.parent.parent, StatusEvent(status=\ 436 414 "Select at least one parameter to fit ")) 437 415 … … 445 423 flag=self.checkFitRange() 446 424 if flag==True and self.model!=None: 447 #print"fit page",self.xmin.GetValue(),self.xmax.GetValue()448 425 self.manager.redraw_model(float(self.xmin.GetValue())\ 449 426 ,float(self.xmax.GetValue())) … … 473 450 """ 474 451 self.set_model() 475 self.compute_chisqr() 452 try: 453 self.compute_chisqr() 454 except: 455 pass 476 456 477 457 def set_model(self): … … 526 506 else: 527 507 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 534 510 def onsetValues(self,chisqr, out,cov): 535 511 """ … … 540 516 541 517 """ 542 #print "fitting : onsetvalues out",out543 518 self.tcChi.SetLabel(format_number(chisqr)) 544 519 params = {}
Note: See TracChangeset
for help on using the changeset viewer.