Changeset d23544dc in sasview
- Timestamp:
- Jan 14, 2009 5:25:32 PM (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:
- 1b6bb09
- Parents:
- a19cd4d
- Location:
- sansview/perspectives/fitting
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitpage1D.py
r0aac36f rd23544dc 24 24 return "%-5.3g" % value 25 25 26 27 class FitPage1D(wx.ScrolledWindow): 26 from modelpage import format_number 27 from modelpage import ModelPage 28 class FitPage1D(ModelPage): 28 29 """ 29 30 FitPanel class contains fields allowing to display results when … … 202 203 203 204 204 205 206 207 208 209 210 def set_owner(self,owner):211 """212 set owner of fitpage213 @param owner: the class responsible of plotting214 """215 self.event_owner = owner216 217 205 218 def set_manager(self, manager): 219 """ 220 set panel manager 221 @param manager: instance of plugin fitting 222 """ 223 self.manager = manager 206 224 207 225 226 def onClose(self,event):227 """ close the page associated with this panel"""228 self.GrandParent.onClose()229 230 208 231 209 def compute_chisqr(self): … … 273 251 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 274 252 "Select at least on parameter to fit ")) 275 def populate_box(self, dict): 276 """ 277 Populate each combox box of each page 278 @param page: the page to populate 279 """ 280 id=0 281 self.model_list_box=dict 282 list_name=[] 283 for item in self.model_list_box.itervalues(): 284 name = item.__name__ 285 if hasattr(item, "name"): 286 name = item.name 287 list_name.append(name) 288 list_name.sort() 289 for name in list_name: 290 self.modelbox.Insert(name,int(id)) 291 id+=1 292 wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) 293 return 0 294 253 295 254 296 255 def _on_select_model(self,event): … … 361 320 return flag 362 321 363 364 def get_model_box(self):365 """ return reference to combox box self.model"""366 return self.modelbox367 322 368 323 … … 543 498 for item in self.parameters: 544 499 try: 500 self.text2_3.Hide() 545 501 item[2].Hide() 546 502 item[3].Clear() … … 567 523 568 524 if is_modified: 569 self.manager.redraw_model( 570 qmin=self.qmin, qmax=self.qmax, 571 ) 572 #self.manager.draw_model(self,model,description=None, 573 # enable1D=True,qmin=None,qmax=None, qstep=None) 574 525 self.manager.redraw_model(qmin=self.qmin, qmax=self.qmax) 526 575 527 def select_all_param(self,event): 576 528 """ -
sansview/perspectives/fitting/fitpage2D.py
r0aac36f rd23544dc 9 9 _BOX_WIDTH = 80 10 10 11 def format_number(value, high=False): 12 """ 13 Return a float in a standardized, human-readable formatted string 14 """ 15 try: 16 value = float(value) 17 except: 18 print "returning 0" 19 return "0" 11 from modelpage import format_number 12 from fitpage1D import FitPage1D 20 13 21 if high: 22 return "%-6.4g" % value 23 else: 24 return "%-5.3g" % value 25 26 27 class FitPage2D(wx.ScrolledWindow): 14 class FitPage2D(FitPage1D): 28 15 """ 29 16 FitPanel class contains fields allowing to display results when … … 49 36 #panel interface 50 37 self.vbox = wx.BoxSizer(wx.VERTICAL) 38 self.sizer6 = wx.GridBagSizer(5,5) 51 39 self.sizer5 = wx.GridBagSizer(5,5) 52 40 self.sizer4 = wx.GridBagSizer(5,5) … … 54 42 self.sizer2 = wx.GridBagSizer(5,5) 55 43 self.sizer1 = wx.GridBagSizer(5,5) 44 56 45 #self.DataSource = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) 57 46 #self.DataSource.SetToolTipString("name of data to fit") … … 70 59 self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) 71 60 self.vbox.Add(self.sizer5) 61 self.vbox.Add(self.sizer6) 72 62 self.vbox.Add(self.sizer4) 73 63 self.vbox.Add(self.sizer1) … … 174 164 # contains link between model ,all its parameters, and panel organization 175 165 self.parameters=[] 166 self.fixed_param=[] 176 167 #contains link between a model and selected parameters to fit 177 168 self.param_toFit=[] … … 191 182 self.Centre() 192 183 193 194 def set_owner(self,owner):195 """196 set owner of fitpage197 @param owner: the class responsible of plotting198 """199 self.event_owner=owner200 184 201 202 def set_manager(self, manager):203 """204 set panel manager205 @param manager: instance of plugin fitting206 """207 self.manager = manager208 209 210 def onClose(self,event):211 """ close the page associated with this panel"""212 self.GrandParent.onClose()213 214 185 215 186 def compute_chisqr(self): … … 225 196 ymin = float(self.ymin.GetValue()) 226 197 ymax = float(self.ymax.GetValue()) 227 228 for i in range(len(self.data.y_bins)):229 if self.data.y_bins[i]>= ymin and self.data.y_bins[i]<= ymax:230 for j in range(len(self.data.x_bins)):231 if self.data.x_bins[j]>= xmin and self.data.x_bins[j]<= xmax:198 for i in range(len(self.data.x_bins)): 199 if self.data.x_bins[i]>= xmin and self.data.x_bins[i]<= xmax: 200 for j in range(len(self.data.y_bins)): 201 if self.data.y_bins[j]>= ymin and self.data.y_bins[j]<= ymax: 202 print "data error ",self.data.err_data[j][i] 232 203 res.append( (self.data.data[j][i]- self.model.runXY(\ 233 [self.data.x_bins[ j],self.data.y_bins[i]]))\204 [self.data.x_bins[i],self.data.y_bins[j]]))\ 234 205 /self.data.err_data[j][i] ) 235 206 sum=0 … … 238 209 if numpy.isfinite(item): 239 210 sum +=item 211 print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum 212 #print res 240 213 self.tcChi.SetValue(format_number(math.fabs(sum))) 241 214 except: 242 215 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 243 216 "Chisqr cannot be compute: %s"% sys.exc_value)) 244 245 246 def onFit(self,event): 247 """ signal for fitting""" 248 flag=self.checkFitRange() 249 self.set_manager(self.manager) 250 251 qmin=float(self.xmin.GetValue()) 252 qmax =float( self.xmax.GetValue()) 253 if len(self.param_toFit) >0 and flag==True: 254 self.manager.schedule_for_fit( value=1,fitproblem =None) 255 self.manager._on_single_fit(qmin=qmin,qmax=qmax) 256 else: 257 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 258 "Select at least on parameter to fit ")) 259 def populate_box(self, dict): 260 """ 261 Populate each combox box of each page 262 @param page: the page to populate 263 """ 264 id=0 265 self.model_list_box=dict 266 list_name=[] 267 for item in self.model_list_box.itervalues(): 268 name = item.__name__ 269 if hasattr(item, "name"): 270 name = item.name 271 list_name.append(name) 272 list_name.sort() 273 for name in list_name: 274 self.modelbox.Insert(name,int(id)) 275 id+=1 276 wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) 277 return 0 278 279 280 def _on_select_model(self,event): 281 """ 282 react when a model is selected from page's combo box 283 post an event to its owner to draw an appropriate theory 284 """ 285 self.btFit.SetFocus() 286 for item in self.model_list_box.itervalues(): 287 name = item.__name__ 288 if hasattr(item, "name"): 289 name = item.name 290 #print "fitpage: _on_select_model model name",name ,event.GetString() 291 if name ==event.GetString(): 292 try: 293 evt = ModelEventbox(model=item(),name=name) 294 wx.PostEvent(self.event_owner, evt) 295 except: 296 raise #ValueError,"model.name is not equal to model class name" 297 break 298 299 def _onTextEnter(self,event): 300 """ 301 set a flag to determine if the fitting range entered by the user is valid 302 """ 303 304 try: 305 flag=self.checkFitRange() 306 if flag==True and self.model!=None: 307 print"fit page",self.xmin.GetValue(),self.xmax.GetValue() 308 self.manager.redraw_model(float(self.xmin.GetValue())\ 309 ,float(self.xmax.GetValue())) 310 except: 311 312 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 313 "Drawing Error:wrong value entered %s"% sys.exc_value)) 314 315 def checkFitRange(self): 316 """ 317 Check the validity of fitting range 318 @note: xmin should always be less than xmax or else each control box 319 background is colored in pink. 320 """ 321 322 flag = True 323 valueMin = self.xmin.GetValue() 324 valueMax = self.xmax.GetValue() 325 # Check for possible values entered 326 print "fitpage: checkfitrange:",valueMin,valueMax 327 try: 328 if (float(valueMax)> float(valueMin)): 329 self.xmax.SetBackgroundColour(wx.WHITE) 330 self.xmin.SetBackgroundColour(wx.WHITE) 331 else: 332 flag = False 333 self.xmin.SetBackgroundColour("pink") 334 self.xmax.SetBackgroundColour("pink") 335 except: 336 flag = False 337 self.xmin.SetBackgroundColour("pink") 338 self.xmax.SetBackgroundColour("pink") 339 340 self.xmin.Refresh() 341 self.xmax.Refresh() 342 return flag 343 344 345 def get_model_box(self): 346 """ return reference to combox box self.model""" 347 return self.modelbox 348 349 350 def get_param_list(self): 351 """ 352 @return self.param_toFit: list containing references to TextCtrl 353 checked.Theses TextCtrl will allow reference to parameters to fit. 354 @raise: if return an empty list of parameter fit will nnote work 355 properly so raise ValueError,"missing parameter to fit" 356 """ 357 if self.param_toFit !=[]: 358 return self.param_toFit 359 else: 360 raise ValueError,"missing parameter to fit" 361 362 363 def set_panel(self,model): 364 """ 365 Build the panel from the model content 366 @param model: the model selected in combo box for fitting purpose 367 """ 368 self.sizer2.Clear(True) 369 self.sizer5.Clear(True) 370 self.parameters = [] 371 self.param_toFit=[] 372 self.model = model 373 keys = self.model.getParamList() 374 #print "fitpage1D : dispersion list",self.model.getDispParamList() 375 keys.sort() 376 disp_list=self.model.getDispParamList() 377 ip=0 378 iq=1 379 if len(disp_list)>0: 380 disp = wx.StaticText(self, -1, 'Dispersion') 381 self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 382 ip += 1 383 values = wx.StaticText(self, -1, 'Values') 384 self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 385 386 disp_list.sort() 387 iy = 1 388 ix = 0 389 self.cb1 = wx.CheckBox(self, -1,'Parameters', (10, 10)) 390 wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) 391 self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ 392 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 393 ix +=1 394 self.text2_2 = wx.StaticText(self, -1, 'Values') 395 self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ 396 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 397 ix +=2 398 self.text2_3 = wx.StaticText(self, -1, 'Errors') 399 self.sizer2.Add(self.text2_3,(iy, ix),(1,1),\ 400 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 401 self.text2_3.Hide() 402 ix +=1 403 self.text2_4 = wx.StaticText(self, -1, 'Units') 404 self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ 405 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 406 self.text2_4.Hide() 407 408 for item in keys: 409 if not item in disp_list: 410 iy += 1 411 ix = 0 412 413 cb = wx.CheckBox(self, -1, item, (10, 10)) 414 cb.SetValue(False) 415 self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 416 wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) 417 418 ix += 1 419 value= self.model.getParam(item) 420 ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 421 ctl1.SetValue(str (format_number(value))) 422 ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) 423 ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) 424 self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) 425 ix += 1 426 text2=wx.StaticText(self, -1, '+/-') 427 self.sizer2.Add(text2,(iy, ix),(1,1),\ 428 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 429 text2.Hide() 430 ix += 1 431 ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 432 self.sizer2.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 433 ctl2.Hide() 434 ix +=1 435 # Units 436 try: 437 units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) 438 except: 439 units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) 440 self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 441 else: 442 ip = 0 443 iq += 1 444 cb = wx.CheckBox(self, -1, item, (10, 10)) 445 cb.SetValue(False) 446 self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 447 wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) 448 449 ip += 1 450 value= self.model.getParam(item) 451 ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 452 ctl1.SetValue(str (format_number(value))) 453 ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) 454 ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) 455 self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 456 457 #save data 458 self.parameters.append([cb,ctl1,text2,ctl2]) 459 460 iy+=1 461 self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 462 463 #Display units text on panel 464 for item in keys: 465 if self.model.details[item][0]!='': 466 self.text2_4.Show() 467 break 468 else: 469 self.text2_4.Hide() 470 #Disable or enable fit button 471 472 if not (len(self.param_toFit ) >0): 473 self.xmin.Disable() 474 self.xmax.Disable() 475 self.ymin.Disable() 476 self.ymax.Disable() 477 else: 478 self.xmin.Enable() 479 self.xmax.Enable() 480 self.ymin.Enable() 481 self.ymax.Enable() 482 483 self.compute_chisqr() 484 self.vbox.Layout() 485 self.SetScrollbars(20,20,55,40) 486 self.Layout() 487 488 self.GrandParent.GetSizer().Layout() 489 490 491 492 def _onparamEnter(self,event): 493 """ 494 when enter value on panel redraw model according to changed 495 """ 496 self.set_model_parameter() 497 self.compute_chisqr() 498 499 def set_model_parameter(self): 500 """ 501 this method redraws the model according to parameters values changes 502 and the reset model according to paramaters changes 503 """ 504 print "went here",len(self.parameters) ,self.model 505 if len(self.parameters) !=0 and self.model !=None: 506 # Flag to register when a parameter has changed. 507 is_modified = False 508 for item in self.parameters: 509 try: 510 item[2].Hide() 511 item[3].Clear() 512 item[3].Hide() 513 name=str(item[0].GetLabelText()) 514 value= float(item[1].GetValue()) 515 # If the value of the parameter has changed, 516 # update the model and set the is_modified flag 517 if value != self.model.getParam(name): 518 self.model.setParam(name,value) 519 is_modified = True 520 except: 521 wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ 522 "Drawing Error:wrong value entered : %s"% sys.exc_value)) 523 # Here we should check whether the boundaries have been modified. 524 # If qmin and qmax have been modified, update qmin and qmax and 525 # set the is_modified flag to True 526 if float(self.xmin.GetValue()) != self.qmin: 527 self.qmin = float(self.xmin.GetValue()) 528 is_modified = True 529 if float(self.xmax.GetValue()) != self.qmax: 530 self.qmax = float(self.xmax.GetValue()) 531 is_modified = True 532 533 if is_modified: 534 535 self.manager.redraw_model( 536 qmin=self.qmin, qmax=self.qmax, 537 ) 538 #self.manager.draw_model(self,model,description=None, 539 # enable1D=True,qmin=None,qmax=None, qstep=None) 540 541 def select_all_param(self,event): 542 """ 543 set to true or false all checkBox given the main checkbox value cb1 544 """ 545 self.param_toFit=[] 546 if self.parameters !=[]: 547 if self.cb1.GetValue()==True: 548 for item in self.parameters: 549 item[0].SetValue(True) 550 list= [item[0],item[1],item[2],item[3]] 551 self.param_toFit.append(list ) 552 553 if not (len(self.param_toFit ) >0): 554 self.xmin.Disable() 555 self.xmax.Disable() 556 self.ymin.Disable() 557 self.ymax.Disable() 558 else: 559 self.xmin.Enable() 560 self.xmax.Enable() 561 self.ymin.Enable() 562 self.ymax.Enable() 563 else: 564 for item in self.parameters: 565 item[0].SetValue(False) 566 self.param_toFit=[] 567 568 self.xmin.Disable() 569 self.xmax.Disable() 570 self.ymin.Disable() 571 self.ymax.Disable() 572 573 574 def select_param(self,event): 575 """ 576 Select TextCtrl checked for fitting purpose and stores them 577 in self.param_toFit=[] list 578 """ 579 self.param_toFit=[] 580 for item in self.parameters: 581 if item[0].GetValue()==True: 582 list= [item[0],item[1],item[2],item[3]] 583 self.param_toFit.append(list ) 584 else: 585 if item in self.param_toFit: 586 self.param_toFit.remove(item) 587 if len(self.parameters)==len(self.param_toFit): 588 self.cb1.SetValue(True) 589 else: 590 self.cb1.SetValue(False) 591 592 if not (len(self.param_toFit ) >0): 593 self.xmin.Disable() 594 self.xmax.Disable() 595 self.ymin.Disable() 596 self.ymax.Disable() 597 else: 598 self.xmin.Enable() 599 self.xmax.Enable() 600 self.ymin.Enable() 601 self.ymax.Enable() 602 603 604 605 606 def onsetValues(self,chisqr, out,cov): 607 """ 608 Build the panel from the fit result 609 @param chisqr:Value of the goodness of fit metric 610 @param out:list of parameter with the best value found during fitting 611 @param cov:Covariance matrix 612 613 """ 614 #print "fitting : onsetvalues out",out 615 self.tcChi.Clear() 616 self.tcChi.SetValue(format_number(chisqr)) 617 params = {} 618 is_modified = False 619 has_error = False 620 if out.__class__==numpy.float64: 621 self.param_toFit[0][1].SetValue(format_number(out)) 622 self.param_toFit[0][1].Refresh() 623 if cov !=None : 624 self.text2_3.Show() 625 self.param_toFit[0][2].Show() 626 self.param_toFit[0][3].Clear() 627 self.param_toFit[0][3].SetValue(format_number(cov[0])) 628 self.param_toFit[0][3].Show() 629 #out is a list : set parameters and errors in TextCtrl 630 else: 631 i=0 632 #print "fitpage: list param model",list 633 #for item in self.param_toFit: 634 # print "fitpage: list display",item[0].GetLabelText() 635 for item in self.param_toFit: 636 if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): 637 #item[1].SetValue(format_number(out[i])) 638 item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) 639 item[1].Refresh() 640 if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): 641 self.text2_3.Show() 642 item[2].Show() 643 item[3].Clear() 644 item[3].SetValue(format_number(cov[i])) 645 item[3].Show() 646 i+=1 647 648 self.vbox.Layout() 649 self.SetScrollbars(20,20,55,40) 650 self.Layout() 651 self.GrandParent.GetSizer().Layout() 652 217 218 -
sansview/perspectives/fitting/fitting.py
re8d1066 rd23544dc 120 120 #create the fitting panel 121 121 return [self.fit_panel] 122 122 def _on_show_panel(self, event): 123 print "_on_show_panel: fitting" 123 124 124 125 def get_perspective(self): … … 653 654 except: 654 655 raise 655 def update(self, output): 656 print "Got an update" 656 def update(self, output,elapsed): 657 print "Got an update", elapsed 658 wx.PostEvent(self.parent, StatusEvent(status="Plot \ 659 updating ... %g sec" % elapsed)) 657 660 658 661 def complete(self, output, elapsed, model, qmin, qmax): -
sansview/perspectives/fitting/modelpage.py
rd15c0202 rd23544dc 187 187 call manager to plot model in 2D 188 188 """ 189 if self.enable2D== True: 190 print "modelpage already plot", 189 191 # If the 2D display is not currently enabled, plot the model in 2D 190 192 # and set the enable2D flag. … … 197 199 qmin=float(self.qmin), 198 200 qmax=float(self.qmax)) 201 199 202 200 203 def populate_box(self, dict): … … 225 228 post an event to its owner to draw an appropriate theory 226 229 """ 227 #print "modelpage: self.model_list_box ",self.model_list_box230 self.model_view.SetFocus() 228 231 for item in self.model_list_box.itervalues(): 229 232 name = item.__name__ … … 239 242 self.manager.draw_model(model, name) 240 243 241 self.model_view.SetFocus()244 242 245 243 246 def get_model_box(self):
Note: See TracChangeset
for help on using the changeset viewer.