Changeset bb18ef1 in sasview for sansview/perspectives/fitting/modelpage.py
- Timestamp:
- Mar 18, 2009 11:41:18 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:
- 35eeea8
- Parents:
- 8cfdd5e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/modelpage.py
raae8d23 rbb18ef1 1 1 import sys 2 2 import wx 3 import wx.lib 3 import wx.lib.newevent 4 4 import numpy 5 5 import copy … … 28 28 window_caption = "Fit Page" 29 29 name="" 30 31 30 def __init__(self, parent,model,name, *args, **kwargs): 32 31 wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) … … 36 35 # model on which the fit would be performed 37 36 self.model=model 38 39 ## Data member to store the dispersion object created 37 # Data member to store the dispersion object created 40 38 self._disp_obj_dict = {} 41 39 self.back_up_model= model.clone() 42 40 #list of dispersion paramaters 43 41 self.disp_list=[] … … 86 84 #------------------ sizer 4 draw------------------------ 87 85 88 86 ## structure combox box 87 self.structbox = wx.ComboBox(self, -1) 89 88 # define combox box 90 89 self.modelbox = wx.ComboBox(self, -1) 91 # preview selected model name 92 self.prevmodel_name=name 93 #print "model view prev_model",name 94 self.modelbox.SetValue(self.prevmodel_name) 90 95 91 #enable model 2D draw 96 92 self.enable2D= False 93 self.fitrange= True 97 94 #filling sizer2 98 95 ix = 0 … … 102 99 ix += 1 103 100 self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 101 ix +=1 102 self.text_mult= wx.StaticText(self,-1,' x ') 103 self.sizer4.Add(self.text_mult,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 104 ix += 1 105 self.sizer4.Add(self.structbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 106 107 if hasattr(model ,"model2"): 108 name= model.model2.name 109 110 self.structbox.SetValue(name) 111 if hasattr(model ,"model1"): 112 name= model.model1.name 113 items = self.modelbox.GetItems() 114 self.modelbox.SetValue(name) 115 else: 116 #print "model view prev_model",name 117 self.modelbox.SetValue( name ) 104 118 ix += 1 105 119 id = wx.NewId() … … 190 204 iy+=1 191 205 self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 192 #----------sizer 10 draw------------------------------------------------------ 193 """ 194 id = wx.NewId() 195 self.btClose =wx.Button(self,id,'Close') 196 self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) 197 self.btClose.SetToolTipString("Close page.") 198 199 ix= 3 200 iy= 1 201 self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 202 ix +=1 203 self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) 204 ix =0 205 iy+=1 206 self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 207 """ 206 208 207 # contains link between model ,all its parameters, and panel organization 209 208 self.parameters=[] … … 221 220 self.set_panel(self.model) 222 221 self.theta_cb=None 223 222 224 223 225 224 self.vbox.Layout() … … 231 230 self.Layout() 232 231 self.parent.GetSizer().Layout() 233 234 def _set_dispersion(self, par, disp_model): 235 """ 236 Utility method to set a dispersion model while making 237 sure that the dispersion model object doesn't go out 238 of scope. The models should be cleaned up so we don't 239 have to do this. 240 """ 241 # Store the object to make it persist outside the scope of this method 242 #TODO: refactor model to clean this up? 243 self._disp_obj_dict[par] = disp_model 244 245 # Set the new model as the dispersion object for the selected parameter 246 self.model.set_dispersion(par, disp_model) 247 248 def set_model_description(self, model): 249 250 if model !=None and str(model.description)!=""and self.data==None: 251 self.sizer11.Clear(True) 252 self.box_description= wx.StaticBox(self, -1, 'Model Description') 253 boxsizer1 = wx.StaticBoxSizer(self.box_description, wx.VERTICAL) 254 boxsizer1.SetMinSize((320,20)) 255 self.description = wx.StaticText(self,-1,str(model.description)) 256 boxsizer1.Add(self.description, 0, wx.EXPAND) 257 self.sizer11.Add(boxsizer1,1, wx.EXPAND | wx.ALL, 2) 232 def set_model_description(self, model=None): 233 234 if model !=None: 235 description="description" 236 else: 237 description="" 238 if hasattr(model,description.lower())and self.data==None: 239 self.sizer11.Clear(True) 240 self.box_description= wx.StaticBox(self, -1, 'Model Description') 241 boxsizer1 = wx.StaticBoxSizer(self.box_description, wx.VERTICAL) 242 boxsizer1.SetMinSize((320,20)) 243 self.description = wx.StaticText(self,-1,str(model.description)) 244 boxsizer1.Add(self.description, 0, wx.EXPAND) 245 self.sizer11.Add(boxsizer1,1, wx.EXPAND | wx.ALL, 2) 258 246 259 247 … … 278 266 @param page: the page to populate 279 267 """ 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 290 for name in list_name: 291 self.modelbox.Insert(name,int(id)) 292 id+=1 293 wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) 268 269 self.model_list_box = dict.get_list() 270 271 for item , mylist in self.model_list_box.iteritems(): 272 separator= "---%s---"%item 273 self.modelbox.Append(separator,"separator") 274 275 for models in mylist: 276 model= models() 277 name = model.__class__.__name__ 278 if hasattr(model, "name"): 279 name = model.name 280 self.modelbox.Append(name,models) 281 wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) 282 if item == "Structure Factors" : 283 for structs in mylist: 284 struct= structs() 285 name = struct.__class__.__name__ 286 if hasattr(struct, "name"): 287 name = struct.name 288 self.structbox.Append(name,structs) 289 wx.EVT_COMBOBOX(self.structbox,-1, self._on_select_model) 290 291 294 292 return 0 295 293 296 294 297 295 def Set_DipersParam(self, event): 298 """ 299 This method is called when the user changes the state 300 of the "dispersity" radio buttons. 301 302 #TODO: correct the spelling mistake in the name of this method, start name with lowercase. 303 """ 296 if self.model ==None: 297 msg= " Select non - model value:%s !"%self.model 298 wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) 299 return 304 300 if self.enable_disp.GetValue(): 305 # The user selected to use dispersion/averaging306 301 if len(self.disp_list)==0: 307 # This model contains no parameter to which we can apply dispersion/averaging308 302 ix=0 309 303 iy=1 … … 319 313 return 320 314 else: 321 # Show the default dispersion/averaging sub-panel 315 if self.data !=None and self.model !=None: # allow to recognize data panel from model panel 316 317 318 self.cb1.SetValue(False) 319 self.select_all_param_helper() 320 322 321 self.populate_disp_box() 323 322 self.set_panel_dispers(self.disp_list) 324 323 325 324 else: 326 # The user selected not to use dispersion/averaging 327 # Make sure all parameters have the default Gaussian 328 # dispersion object with only a single point (no dispersion). 329 for p in self.model.dispersion.keys(): 330 disp_model = GaussianDispersion() 331 self._set_dispersion(p, disp_model) 332 333 # Redraw the model 334 self._draw_model() 325 if self.data !=None and self.model!=None: 326 if self.cb1.GetValue(): 327 self.select_all_param_helper() 328 329 if self.back_up_model!=None: 330 keys = self.back_up_model.getDispParamList() 331 keys.sort() 332 #disperse param into the initial state 333 for item in keys: 334 value= self.back_up_model.getParam(item) 335 self.model.setParam(item, value) 336 self._draw_model() 337 335 338 336 339 self.fittable_param=[] … … 359 362 for k,v in self.polydisp.iteritems(): 360 363 if str(v)=="MyModel": 361 364 # Remove the option until the rest of the code is ready for it 362 365 #self.disp_box.Insert("Select customized Model",id) 363 366 pass … … 433 436 # If the 2D display is not currently enabled, plot the model in 2D 434 437 # and set the enable2D flag. 435 if self.enable2D==False: 436 self.enable2D=True 438 if self.fitrange: 439 self.enable2D = True 440 441 if self.enable2D: 437 442 self._draw_model() 438 443 self.model_view.Disable() 444 439 445 440 446 … … 449 455 450 456 self.set_panel(model) 451 self._draw_model(name) 452 453 # Select the model from the combo box 454 items = self.modelbox.GetItems() 455 for i in range(len(items)): 456 if items[i]==name: 457 self.modelbox.SetSelection(i) 458 self.model_view.SetFocus() 457 self._draw_model() 458 459 if hasattr(model ,"model2"): 460 name= model.model2.name 461 items = self.structbox.GetItems() 462 for i in range(len(items)): 463 if items[i]==name: 464 self.structbox.SetSelection(i) 465 466 if hasattr(model ,"model1"): 467 name= model.model1.name 468 items = self.modelbox.GetItems() 469 for i in range(len(items)): 470 if items[i]==name: 471 self.modelbox.SetSelection(i) 472 else: 473 # Select the model from the combo box 474 items = self.modelbox.GetItems() 475 for i in range(len(items)): 476 if items[i]==name: 477 self.modelbox.SetSelection(i) 478 self.structbox.SetValue("No Structure") 479 480 self.model_view.SetFocus() 459 481 460 482 def _on_select_Disp(self,event): … … 478 500 self.Layout() 479 501 self.parent.GetSizer().Layout() 480 for item in self.model_list_box.itervalues(): 481 name = item.__name__ 482 if hasattr(item, "name"): 483 name = item.name 484 if name ==event.GetString(): 485 model=item() 486 self.model= model 487 self.set_panel(model) 488 self.name= name 489 self.model_view.SetFocus() 490 self.parent.model_page.name=name 491 self.parent.draw_model_name=name 502 form_factor =self.modelbox.GetClientData(self.modelbox.GetCurrentSelection()) 503 struct_factor =self.structbox.GetClientData(self.structbox.GetCurrentSelection()) 504 505 if form_factor!="separator": 506 if struct_factor != None and struct_factor.__name__ != "NoStructure": 507 from sans.models.MultiplicationModel import MultiplicationModel 508 self.model= MultiplicationModel(form_factor(),struct_factor()) 509 else: 510 self.model= form_factor() 511 else: 512 self.model=None 513 msg= " Select non - model value:%s !Please select another model"%name 514 wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) 492 515 493 self._draw_model(name) 494 516 self.set_model_description(self.model) 517 self.set_panel(self.model) 518 self.name= self.model.name 519 self.model_view.SetFocus() 520 self.parent.model_page.name= self.name 521 self.parent.draw_model_name= self.name 522 self._draw_model() 495 523 496 524 def get_model_box(self): … … 522 550 self.param_toFit=[] 523 551 self.fixed_param=[] 524 self.model = model 525 526 self.set_model_description( self.model) 527 528 keys = self.model.getParamList() 529 #list of dispersion paramaters 530 self.disp_list=self.model.getDispParamList() 531 532 keys.sort() 533 ik=0 534 im=1 535 536 iy = 1 537 ix = 0 538 self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) 539 if self.data!=None: 540 wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) 541 self.cb1.SetValue(False) 542 else: 543 self.cb1.Disable() 544 self.cb1.Hide() 545 546 self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ 547 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 548 ix +=1 549 self.text2_2 = wx.StaticText(self, -1, 'Values') 550 self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ 551 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 552 ix +=2 553 self.text2_3 = wx.StaticText(self, -1, 'Errors') 554 self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ 555 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 556 self.text2_3.Hide() 557 558 559 ix +=1 560 self.text2_4 = wx.StaticText(self, -1, 'Units') 561 self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ 562 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 563 self.text2_4.Hide() 564 disp_list=self.model.getDispParamList() 565 for item in keys: 566 if not item in disp_list: 567 iy += 1 568 ix = 0 569 570 cb = wx.CheckBox(self, -1, item, (10, 10)) 571 if self.data!=None: 572 cb.SetValue(False) 573 wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) 552 if model !=None: 553 self.model = model 554 555 self.set_model_description( self.model) 556 557 keys = self.model.getParamList() 558 #list of dispersion paramaters 559 self.disp_list=self.model.getDispParamList() 560 561 keys.sort() 562 ik=0 563 im=1 564 565 iy = 1 566 ix = 0 567 self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) 568 if self.data!=None: 569 wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) 570 self.cb1.SetValue(False) 571 else: 572 self.cb1.Disable() 573 self.cb1.Hide() 574 575 self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ 576 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 577 ix +=1 578 self.text2_2 = wx.StaticText(self, -1, 'Values') 579 self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ 580 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 581 ix +=2 582 self.text2_3 = wx.StaticText(self, -1, 'Errors') 583 self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ 584 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 585 self.text2_3.Hide() 586 587 588 ix +=1 589 self.text2_4 = wx.StaticText(self, -1, 'Units') 590 self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ 591 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 592 self.text2_4.Hide() 593 disp_list=self.model.getDispParamList() 594 for item in keys: 595 if not item in disp_list: 596 iy += 1 597 ix = 0 598 599 cb = wx.CheckBox(self, -1, item, (10, 10)) 600 if self.data!=None: 601 cb.SetValue(False) 602 wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) 603 else: 604 cb.Disable() 605 self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 606 607 ix += 1 608 value= self.model.getParam(item) 609 ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 610 ctl1.SetValue(str (format_number(value))) 611 ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) 612 ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) 613 self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) 614 615 ix += 1 616 text2=wx.StaticText(self, -1, '+/-') 617 self.sizer5.Add(text2,(iy, ix),(1,1),\ 618 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 619 text2.Hide() 620 ix += 1 621 ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 622 self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 623 ctl2.Hide() 624 625 ix +=1 626 # Units 627 try: 628 units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) 629 except: 630 units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) 631 self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 632 633 self.parameters.append([cb,ctl1,text2,ctl2]) 634 635 iy+=1 636 self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 637 638 #Display units text on panel 639 for item in keys: 640 if self.model.details[item][0]!='': 641 self.text2_4.Show() 642 break 574 643 else: 575 cb.Disable() 576 self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 577 578 ix += 1 579 value= self.model.getParam(item) 580 ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 581 ctl1.SetValue(str (format_number(value))) 582 ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) 583 ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) 584 self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) 585 586 ix += 1 587 text2=wx.StaticText(self, -1, '+/-') 588 self.sizer5.Add(text2,(iy, ix),(1,1),\ 589 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 590 text2.Hide() 591 ix += 1 592 ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) 593 self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 594 ctl2.Hide() 595 596 ix +=1 597 # Units 598 try: 599 units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) 600 except: 601 units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) 602 self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 603 604 self.parameters.append([cb,ctl1,text2,ctl2]) 605 606 iy+=1 607 self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 608 609 #Display units text on panel 610 for item in keys: 611 if self.model.details[item][0]!='': 612 self.text2_4.Show() 613 break 614 else: 615 self.text2_4.Hide() 616 644 self.text2_4.Hide() 645 617 646 self.vbox.Layout() 618 647 self.SetScrollbars(20,20,55,40) … … 700 729 701 730 # Create the dispersion objects 702 # Store the objects to make them persist outside the scope of this method703 #TODO: refactor model to clean this up?704 self._disp_obj_dict[p+'values'] = values705 self._disp_obj_dict[p+'weights'] = weights706 731 disp_model = ArrayDispersion() 707 732 disp_model.set_weights(values, weights) 708 self._set_dispersion(p, disp_model) 733 # Store the object to make it persist outside the scope of this method 734 #TODO: refactor model to clean this up? 735 self._disp_obj_dict[p] = disp_model 736 737 # Set the new model as the dispersion object for the selected parameter 738 self.model.set_dispersion(p, disp_model) 739 709 740 710 741 # Redraw the model … … 714 745 # The parameter was un-selected. Go back to Gaussian model (with 0 pts) 715 746 disp_model = GaussianDispersion() 716 self._set_dispersion(p, disp_model) 747 # Store the object to make it persist outside the scope of this method 748 #TODO: refactor model to clean this up? 749 self._disp_obj_dict[p] = disp_model 750 751 # Set the new model as the dispersion object for the selected parameter 752 self.model.set_dispersion(p, disp_model) 717 753 718 754 # Redraw the model … … 733 769 iy=1 734 770 ### this will become a separate method 735 #TODO: don't hard code text values to be shown on the interface736 771 if type== "Select customized Model": 737 772 ix=0 … … 787 822 name3=item+".nsigmas" 788 823 iy += 1 789 790 # Make sure the dispersion model is Gaussian791 if not self.model.dispersion[item]['type'] == 'gaussian':792 disp_model = GaussianDispersion()793 self._set_dispersion(item, disp_model)794 795 # Create the interface796 824 for p in self.model.dispersion[item].keys(): 797 825 #print "name 1 2 3", name1, name2, name3 … … 966 994 # If qmin and qmax have been modified, update qmin and qmax and 967 995 # set the is_modified flag to True 968 if float(self.qmin.GetValue()) != self.qmin_x: 969 self.qmin_x = float(self.qmin.GetValue()) 970 is_modified = True 971 if float(self.qmax.GetValue()) != self.qmax_x: 972 self.qmax_x = float(self.qmax.GetValue()) 973 is_modified = True 974 996 from sans.guiframe.utils import check_value 997 if check_value( self.qmin, self.qmax): 998 if float(self.qmin.GetValue()) != self.qmin_x: 999 self.qmin_x = float(self.qmin.GetValue()) 1000 is_modified = True 1001 if float(self.qmax.GetValue()) != self.qmax_x: 1002 self.qmax_x = float(self.qmax.GetValue()) 1003 is_modified = True 1004 self.fitrange = True 1005 else: 1006 self.fitrange = False 975 1007 if float(self.npts.GetValue()) != self.num_points: 976 1008 self.num_points = float(self.npts.GetValue()) … … 980 1012 self._draw_model() 981 1013 982 def _draw_model(self , name=None):1014 def _draw_model(self): 983 1015 """ 984 1016 Method to draw or refresh a plotted model. … … 988 1020 [Note to coder: This way future changes will be done in only one place.] 989 1021 """ 990 if name==None: 991 name= self.model.name 992 993 self.manager.draw_model(self.model, name, data=self.data, 994 qmin=self.qmin_x, qmax=self.qmax_x, 995 qstep= self.num_points, 996 enable2D=self.enable2D) 1022 if self.model !=None: 1023 self.manager.draw_model(self.model, self.model.name, data=self.data, 1024 qmin=self.qmin_x, qmax=self.qmax_x, 1025 qstep= self.num_points, 1026 enable2D=self.enable2D) 997 1027 998 1028 def select_param(self,event):
Note: See TracChangeset
for help on using the changeset viewer.