- Timestamp:
- Mar 25, 2009 3:05:23 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:
- ac11e40
- Parents:
- c7ac15e
- Location:
- sansview/perspectives/fitting
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitpage.py
rc77d859 r2140e68 354 354 ## error occured on chisqr computation 355 355 pass 356 357 356 def get_range(self): 357 """ 358 return the fitting range 359 """ 360 return self.qmin_x , self.qmax_x 358 361 359 362 def get_param_list(self): … … 432 435 if self.enable_smearer.GetValue(): 433 436 from DataLoader.qsmearing import smear_selection 434 smear = smear_selection( self.data )437 smear = smear_selection( self.data ) 435 438 if hasattr(self.data,"dxl"): 436 439 msg= ": Resolution smearing parameters" … … 443 446 wx.PostEvent(self.manager.parent, StatusEvent(status=\ 444 447 "Data contains smearing information %s"%msg)) 445 self.manager.set_smearer(smear, qmin= self.qmin_x, qmax= self.qmax_x)448 self.manager.set_smearer(smear, qmin= self.qmin_x, qmax= self.qmax_x) 446 449 447 450 -
sansview/perspectives/fitting/fitpanel.py
rc77d859 r2140e68 63 63 page_number = self.GetSelection() 64 64 if self.sim_page != selected_page and selected_page!=self.about_page: 65 # remove the check box link to the model name of this page (selected_page)66 if self.sim_page !=None :67 self.sim_page.remove_model(selected_page)68 65 #remove that page from page_finder of fitting module 69 66 page_finder=self.manager.get_page_finder() … … 72 69 del page_finder[page] 73 70 break 71 # remove the check box link to the model name of this page (selected_page) 72 if self.sim_page !=None : 73 self.sim_page.draw_page(page_finder) 74 74 75 #Delete the page from notebook 75 76 if selected_page.name in self.fit_page_name: -
sansview/perspectives/fitting/fitproblem.py
rbb18ef1 r2140e68 1 from sans.fit.AbstractFitEngine import Model2 1 3 2 class FitProblem: … … 11 10 12 11 """ 13 @ self.data :is the data selected to perform the fit 14 @ self.theory_name: the name of the theory created with self.model 15 @ self.model_list: is a list containing a model as first element 16 and its name assign example [lineModel, M0] 12 contains information about data and model to fit 17 13 """ 18 14 ## data used for fitting 19 15 self.fit_data=None 20 ## list containing couple of model and its name21 self.model _list=[]16 ## the current model 17 self.model = None 22 18 ## if 1 this fit problem will be selected to fit , if 0 23 19 ## it will not be selected for fit … … 30 26 ## axis unit info and so on see plottables definition 31 27 self.plotted_data=None 28 ## fitting range 29 self.qmin = None 30 self.qmax = None 32 31 33 32 … … 52 51 return self.name_per_page 53 52 54 def set_model(self,model ,name):53 def set_model(self,model): 55 54 """ 56 55 associates each model with its new created name … … 58 57 @param name: name created for model 59 58 """ 59 self.model= model 60 60 61 if len(self.model_list)>0 and name==None:62 self.model_list=[model,self.model_list[1]]63 else:64 self.model_list=[model,name]65 61 62 def get_model(self): 63 """ @return: saved model """ 64 return self.model 65 66 66 67 67 def add_plotted_data(self,data): … … 72 72 self.plotted_data = data 73 73 74 74 75 def get_plotted_data(self): 76 """ @return: list of data dList""" 77 return self.plotted_data 78 79 75 80 def add_fit_data(self,data): 76 81 """ … … 80 85 self.fit_data = data 81 86 82 def get_model(self): 83 """ @return: saved model """ 84 return self.model_list 85 86 def get_plotted_data(self): 87 """ @return: list of data dList""" 88 return self.plotted_data 89 90 87 91 88 def get_fit_data(self): 92 89 return self.fit_data 93 90 94 95 def get_theory(self):96 """ @return the name of theory for plotting purpose"""97 return self.theory_name98 99 100 def set_theory(self,name):101 """102 Set theory name103 @param name: name of the theory104 """105 self.theory_name = name106 107 91 108 def set_model_param(self,name,value ):92 def set_model_param(self,name,value=None): 109 93 """ 110 94 Store the name and value of a parameter of this fitproblem's model … … 122 106 123 107 124 def reset_model(self,model):125 """126 reset a model when parameter has changed127 @param value: new model128 """129 self.model_list[0]=model130 131 132 108 def schedule_tofit(self, schedule=0): 133 109 """ … … 136 112 self.schedule=schedule 137 113 114 138 115 def get_scheduled(self): 139 116 """ return true or false if a problem as being schedule for fitting""" 140 117 return self.schedule 141 118 119 120 def set_range(self, qmin=None, qmax=None): 121 """ 122 set fitting range 123 """ 124 self.qmin = qmin 125 self.qmax = qmax 126 127 128 def get_range(self): 129 """ 130 @return fitting range: 131 """ 132 return self.qmin, self.qmax 142 133 143 134 def clear_model_param(self): … … 147 138 self.list_param=[] 148 139 140 141 149 142 -
sansview/perspectives/fitting/fitting.py
r6859338 r2140e68 120 120 Create a page to access simultaneous fit option 121 121 """ 122 if self.sim_page !=None: 123 msg= "Simultaneous Fit page already opened" 124 wx.PostEvent(self.parent, StatusEvent(status= msg)) 125 return 126 122 127 self.sim_page= self.fit_panel.add_sim_page() 123 self.sim_page. add_model(self.page_finder)128 self.sim_page.draw_page(self.page_finder) 124 129 125 130 … … 299 304 300 305 301 def on_single_fit(self,id= None,qmin=None, qmax=None):306 def on_single_fit(self,id=0,qmin=None, qmax=None): 302 307 """ 303 308 perform fit for the current page and return chisqr,out and cov … … 311 316 self.fitter = Fit(self._fit_engine) 312 317 #Setting an id to store model and data in fit engine 313 self.fit_id = 0 314 if id!=None: 315 self.fit_id = id 316 318 self.fit_id = id 319 317 320 page_fitted = None 318 fit_problem = None321 319 322 #Get information (model , data) related to the page on 320 323 #with the fit will be perform … … 326 329 value = self.page_finder[current_pg] 327 330 metadata = value.get_fit_data() 328 list = value.get_model() 329 model = list[0] 331 model = value.get_model() 330 332 smearer = value.get_smearer() 331 333 … … 394 396 return 395 397 396 def on_simul_fit(self, id= None,qmin=None,qmax=None):398 def on_simul_fit(self, id=0,qmin=None,qmax=None): 397 399 """ 398 400 perform fit for all the pages selected on simpage and return chisqr,out and cov … … 404 406 """ 405 407 ##Setting an id to store model and data 406 self.fit_id= 0 407 #Setting an id to store model and data 408 if id!=None: 409 self.fit_id= id 408 self.fit_id= id 409 410 410 ## count the number of fitproblem schedule to fit 411 411 fitproblem_count= 0 … … 413 413 if value.get_scheduled()==1: 414 414 fitproblem_count += 1 415 415 416 ## if simultaneous fit change automatically the engine to park 416 417 if fitproblem_count >1: … … 424 425 if value.get_scheduled()==1: 425 426 metadata = value.get_fit_data() 426 list = value.get_model() 427 model= list[0] 427 model = value.get_model() 428 smearer = value.get_smearer() 429 qmin , qmax = value.get_range() 428 430 ## store page 429 431 cpage= page … … 441 443 ## create a park model and reset parameter value if constraint 442 444 ## is given 443 new_model=Model(model) 444 param=value.get_model_param() 445 445 new_model = Model(model) 446 param = value.get_model_param() 446 447 if len(param)>0: 447 448 for item in param: 448 449 param_value = item[1] 449 450 param_name = item[0] 450 451 new_model.parameterset[ param_name].set( param_value ) 452 453 self.fitter.set_model(new_model, self.fit_id, pars) 451 ## check if constraint 452 if param_value !=None: 453 new_model.parameterset[ param_name].set( param_value ) 454 455 self.fitter.set_model(model= new_model, Uid=self.fit_id, pars=pars) 454 456 ## check that non -zero value are send as dy in the fit engine 455 457 dy=[] … … 469 471 metadata.x=numpy.zeros(len(x)) 470 472 metadata.x=x 471 472 self.fitter.set_data(metadata,self.fit_id,qmin,qmax) 473 474 self.fitter.set_data( data= metadata, smearer=smearer, 475 Uid=self.fit_id, qmin=qmin, qmax=qmax) 473 476 self.fitter.select_problem_for_fit(Uid= self.fit_id, 474 477 value= value.get_scheduled()) … … 486 489 if self.calc_fit!= None and self.calc_fit.isrunning(): 487 490 self.calc_fit.stop() 488 491 ## perform single fit 489 492 if fitproblem_count==1: 490 493 self.calc_fit=FitThread(parent =self.parent, … … 492 495 qmin=qmin, 493 496 qmax=qmax, 494 ymin= ymin,495 ymax= ymax,496 497 cpage=cpage, 497 498 pars= pars, … … 505 506 qmin=qmin, 506 507 qmax=qmax, 507 ymin= ymin,508 ymax= ymax,509 508 completefn= self._simul_fit_completed, 510 509 updatefn=None) … … 565 564 qstep=qstep) 566 565 567 568 566 def _fit_helper(self, page, id ): 567 """ 568 helper for fitting 569 """ 570 #set an engine to perform fit 571 from sans.fit.Fitting import Fit 572 self.fitter = Fit(self._fit_engine) 573 #Setting an id to store model and data in fit engine 574 self.fit_id = id 575 page_fitted = None 576 577 578 569 579 def _onSelect(self,event): 570 580 """ … … 629 639 for page, value in self.page_finder.iteritems(): 630 640 if page==cpage : 631 list = value.get_model() 632 model= list[0] 641 model= value.get_model() 633 642 break 634 643 i = 0 … … 643 652 ## plot the current model with new param 644 653 metadata = self.page_finder[cpage].get_fit_data() 645 list = self.page_finder[cpage].get_model() 646 model = list[0] 654 model = self.page_finder[cpage].get_model() 655 656 #Replot models 657 msg= "Single Fit completed. plotting... %s:"%model.name 658 wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) 647 659 self.draw_model( model=model, data= metadata,qmin= qmin, qmax= qmax) 648 660 … … 653 665 654 666 655 def _simul_fit_completed(self,result,qmin,qmax, elapsed,pars=None,cpage=None, 656 xmin=None, xmax=None, ymin=None, ymax=None): 667 def _simul_fit_completed(self,result,qmin,qmax, elapsed=None,pars=None,cpage=None): 657 668 """ 658 669 Parameter estimation completed, … … 662 673 """ 663 674 if cpage!=None: 664 self._single_fit_completed(result=result,pars=pars,cpage=cpage, 665 qmin=qmin,qmax=qmax, 666 ymin=ymin, ymax=ymax, xmin=xmin, xmax=xmax) 675 self._single_fit_completed(result=result, pars=pars, cpage=cpage, 676 qmin=qmin, qmax=qmax) 667 677 return 668 678 else: … … 673 683 for page, value in self.page_finder.iteritems(): 674 684 if value.get_scheduled()==1: 675 list = value.get_model() 676 model= list[0] 677 685 model = value.get_model() 686 metadata = value.get_plotted_data() 678 687 small_out = [] 679 688 small_cov = [] … … 692 701 page.onsetValues(result.fitness, small_out,small_cov) 693 702 #Replot models 694 msg= "Si ngle Fit completed plotting%s:"%model.name703 msg= "Simultaneous Fit completed. plotting... %s:"%model.name 695 704 wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) 696 self.plot_helper(currpage= page,qmin= qmin,qmax= qmax, 697 xmin=xmin, xmax=xmax, 698 ymin=ymin, ymax=ymax) 705 self.draw_model( model=model, data= metadata,qmin= qmin, qmax= qmax) 706 699 707 except: 700 708 msg= "Simultaneous Fit completed but Following error occurred: " … … 755 763 if model ==None: 756 764 return 757 name = model.name765 758 766 sim_page=self.sim_page 759 767 current_pg = self.fit_panel.get_current_page() … … 762 770 if current_pg != sim_page: 763 771 764 if len(self.page_finder[current_pg].get_model())==0:772 if self.page_finder[current_pg].get_model()== None : 765 773 766 774 model.name="M"+str(self.index_model) 767 775 self.index_model += 1 768 776 else: 769 model.name= self.page_finder[current_pg].get_model()[0].name770 777 771 try:772 metadata=self.page_finder[current_pg].get_plotted_data()773 M_name = model.name+"= "+name+"("+metadata.name+")"774 except:775 M_name = model.name+"= "+name778 model.name= self.page_finder[current_pg].get_model().name 779 780 781 metadata = self.page_finder[current_pg].get_plotted_data() 782 776 783 # save the name containing the data name with the appropriate model 777 self.page_finder[current_pg].set_model(model,M_name) 778 784 self.page_finder[current_pg].set_model(model) 779 785 # save model name 780 786 self.draw_model( model=model, data= metadata) 781 787 782 788 if self.sim_page!=None: 783 self.sim_page. add_model(self.page_finder)789 self.sim_page.draw_page(self.page_finder) 784 790 785 791 … … 940 946 err_image[err_image==0]= 1 941 947 theory= Data2D(image= image , err_image= err_image) 948 theory.name= model.name 942 949 943 950 if data ==None: 944 951 self._fill_default_model2D(theory= theory, qmax=qmax,qstep=qstep, qmin= qmin) 945 theory.name= model.name952 946 953 else: 947 theory.name= data.name948 954 theory.id= "Model" 949 955 theory.group_id= "Model"+data.name -
sansview/perspectives/fitting/simfitpage.py
r925a30e r2140e68 1 1 2 import sys,re,string, wx 2 import sys,re,string, wx 3 import wx.lib.newevent 3 4 from sans.guicomm.events import StatusEvent 5 6 class FitConstraint: 7 """ 8 Contains info on selected model to fit 9 via simultaneous page 10 """ 11 def __init__(self, model, page=None): 12 """ 13 initialization 14 """ 15 self.model= model 16 self.page = page 17 self.fittable_param =[] 18 self.selected_params=[] 19 self._set_fittableParam() 20 21 def _set_fittableParam(self): 22 """ 23 fill self.fittable_param 24 """ 25 for item in self.model.getParamList(): 26 if not item in self.model.getDispParamList(): 27 self.fittable_param.append(item) 28 for item in self.model.fixed: 29 self.fittable_param.append(item) 30 4 31 5 32 class SimultaneousFitPage(wx.ScrolledWindow): … … 15 42 16 43 17 def __init__(self, parent, *args, **kwargs):44 def __init__(self, parent,page_finder ={}, *args, **kwargs): 18 45 wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) 19 46 """ … … 21 48 """ 22 49 self.parent = parent 23 self.page_finder={} 24 self.sizer3 = wx.GridBagSizer(5,5) 25 self.sizer1 = wx.GridBagSizer(5,5) 26 self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) 50 ## store page_finder 51 self.page_finder=page_finder 52 ## list contaning info to set constraint 53 ## look like self.constraint_dict[page]=[ model_name, parameter_name, constraint(string)] 54 self.constraint_dict={} 55 ## item list self.constraints_list=[combobox1, combobox2,=,textcrtl, button ] 56 self.constraints_list=[] 57 ## list of current model 58 self.model_list=[] 59 ## selected mdoel to fit 60 self.model_toFit=[] 61 62 63 ## draw 64 self.define_page_structure() 65 self.draw_page(self.page_finder) 66 67 self.set_layout() 68 69 70 71 def define_page_structure(self): 72 """ 73 Create empty sizer for a panel 74 """ 27 75 self.vbox = wx.BoxSizer(wx.VERTICAL) 28 self.vbox.Add(self.sizer3) 76 self.sizer1 = wx.BoxSizer(wx.VERTICAL) 77 self.sizer2 = wx.BoxSizer(wx.VERTICAL) 78 29 79 self.vbox.Add(self.sizer1) 30 80 self.vbox.Add(self.sizer2) 31 id = wx.NewId() 32 self.btFit =wx.Button(self,id,'Constraint Fit') 33 self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) 34 self.btFit.SetToolTipString("Perform fit.") 35 ix = 0 36 iy = 1 37 self.cb1 = wx.CheckBox(self, -1,'Models', (10, 10)) 38 self.sizer3.Add(self.cb1,(iy, ix),(1,1),\ 39 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 40 wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_model_name) 41 42 text=wx.StaticText(self, -1, 'Constraint') 43 self.sizer2.Add(text,0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) 44 45 self.ctl2 = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE) 46 47 self.sizer2.Add(self.ctl2, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) 48 49 self.sizer2.Add(self.btFit, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) 50 self.params=[] 51 self.model_list=[] 52 self.model_toFit=[] 53 81 82 def set_scroll(self): 83 self.SetScrollbars(20,20,200,100) 84 self.Layout() 85 86 def set_layout(self): 87 """ 88 layout 89 """ 54 90 self.vbox.Layout() 55 91 self.vbox.Fit(self) 56 92 self.SetSizer(self.vbox) 57 self.SetScrollbars(20,20,55,40) 93 94 self.set_scroll() 58 95 self.Centre() 59 60 96 61 97 def onFit(self,event): 62 98 """ signal for fitting""" 63 for page in self.page_finder.iterkeys(): 64 page.set_model_parameter() 99 ## making sure all parameters content a constraint 100 ## validity of the constraint expression is own by fit engine 101 self._set_constraint() 102 ## get the fit range of very fit problem 103 for page, value in self.page_finder.iteritems(): 104 qmin, qmax= page.get_range() 105 value.set_range(qmin, qmax) 106 ## model was actually selected from this page to be fit 65 107 if len(self.model_toFit) >= 1 : 66 self._onTextEnter() 67 self.set_model() 68 self.manager._on_simul_fit() 108 self.manager.on_simul_fit() 69 109 else: 70 wx.PostEvent(self.parent.Parent, StatusEvent(status=\71 "Select at least one model to fit "))110 msg= "Select at least one model to fit " 111 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 72 112 73 113 … … 79 119 """ 80 120 self.manager = manager 81 self. add_model( self.manager.page_finder)82 83 84 def select_all_model_name(self,event):121 self.draw_page( self.manager.page_finder) 122 123 124 def check_all_model_name(self,event): 85 125 """ 86 126 check all models names … … 92 132 self.manager.schedule_for_fit( value=1,fitproblem =item[1]) 93 133 self.model_toFit.append(item) 134 135 ## constraint info 136 self._store_model() 137 ## display constraint fields 138 if self.show_constraint.GetValue(): 139 self.sizer_couples.Clear(True) 140 self._show_constraint() 141 return 94 142 else: 95 143 for item in self.model_list: … … 98 146 99 147 self.model_toFit=[] 100 101 102 def add_model(self,page_finder): 103 """ 104 Receive a dictionary containing information to display model name 105 @param page_finder: the dictionary containing models information 106 """ 107 if len(self.model_list)>0: 108 for item in self.model_list: 109 item[0].SetValue(False) 110 self.manager.schedule_for_fit( value=0,fitproblem =item[1]) 111 self.model_list=[] 112 self.model_toFit=[] 113 self.sizer1.Clear(True) 114 self.page_finder=page_finder 115 self.cb1.SetValue(False) 116 ix = 0 117 iy = 1 118 list=[] 119 for page, value in page_finder.iteritems(): 120 try: 121 list = value.get_model() 122 model = list[0] 123 modelname = list[1] 124 cb = wx.CheckBox(self, -1, modelname, (10, 10)) 125 cb.SetValue(False) 126 self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 127 ix = 0 128 iy += 1 129 wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) 130 self.model_list.append([cb,value,page,modelname]) 131 except: 132 pass 133 iy +=1 134 self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 135 self.sizer1.Layout() 136 self.vbox.Layout() 137 138 139 def remove_model(self,delpage): 140 """ 141 Remove a checkbox and the name related to a model selected on page delpage 142 @param delpage: the page removed 143 """ 144 self.model_list=[] 145 self.model_toFit=[] 146 self.sizer1.Clear(True) 147 148 self.cb1.SetValue(False) 149 ix = 0 150 iy = 1 151 list=[] 152 for page, value in self.page_finder.iteritems(): 153 try: 154 if page!= delpage: 155 list = value.get_model() 156 model = list[0] 157 modelname = list[1] 158 cb = wx.CheckBox(self, -1, modelname, (10, 10)) 159 cb.SetValue(False) 160 self.sizer1.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 161 ix = 0 162 iy += 1 163 wx.EVT_CHECKBOX(self, cb.GetId(), self.select_model_name) 164 self.model_list.append([cb,value,page,modelname]) 165 except: 166 pass 167 iy +=1 168 self.sizer1.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 169 self.sizer1.Layout() 170 self.vbox.Layout() 171 172 173 def select_model_name(self,event): 174 """ 175 Save interformation related to checkbox and their states 148 ##constraint info 149 self._hide_constraint() 150 151 152 def check_model_name(self,event): 153 """ 154 Save information related to checkbox and their states 176 155 """ 177 156 self.model_toFit=[] … … 185 164 self.model_toFit.remove(item) 186 165 self.cb1.SetValue(False) 187 166 ## display constraint fields 167 if len(self.model_toFit)>=2: 168 self._store_model() 169 if self.show_constraint.GetValue(): 170 self._show_constraint() 171 172 173 ## set the value of the main check button 188 174 if len(self.model_list)==len(self.model_toFit): 189 175 self.cb1.SetValue(True) 176 ## constraint info 177 self._store_model() 178 ## display constraint fields 179 if self.show_constraint.GetValue(): 180 self.sizer_couples.Clear(True) 181 self._show_constraint() 182 return 190 183 else: 191 184 self.cb1.SetValue(False) 192 193 185 ##constraint info 186 self._hide_constraint() 187 188 189 def draw_page(self, page_finder={}): 190 """ 191 Draw a sizer containing couples of data and model 192 """ 193 ## receive a new page_finder 194 self.page_finder = page_finder 195 196 self.model_list=[] 197 self.model_toFit=[] 198 if len(self.model_list)>0: 199 for item in self.model_list: 200 item[0].SetValue(False) 201 self.manager.schedule_for_fit( value=0,fitproblem =item[1]) 202 203 self.sizer1.Clear(True) 204 205 206 box_description= wx.StaticBox(self, -1,"Fit Couples") 207 boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) 208 sizer_title = wx.BoxSizer(wx.HORIZONTAL) 209 sizer_couples = wx.GridBagSizer(5,5) 210 211 #------------------------------------------------------ 212 if len(self.page_finder)==0: 213 sizer_title.Add(wx.StaticText(self,-1," No fit couple available !")) 214 else: 215 ## store model 216 self._store_model() 217 218 self.cb1 = wx.CheckBox(self, -1,'Select all Fit Couples') 219 self.cb1.SetValue(False) 220 wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name) 221 222 sizer_title.Add((15,15)) 223 sizer_title.Add(self.cb1) 224 ## draw list of model name 225 self._fill_sizer_model_list(sizer_couples) 226 ## draw the sizer containing constraint info 227 self._fill_sizer_constraint() 228 #-------------------------------------------------------- 229 boxsizer1.Add((5, 5)) 230 boxsizer1.Add(sizer_title) 231 boxsizer1.Add((5, 5)) 232 boxsizer1.Add(sizer_couples) 233 boxsizer1.Add((5,5)) 234 235 self.sizer1.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) 236 self.sizer1.Layout() 237 self.SetScrollbars(20,20,200,100) 238 self.AdjustScrollbars() 239 240 def _store_model(self): 241 """ 242 Store selected model 243 """ 244 if len(self.model_toFit) < 2: 245 return 246 for page, value in self.page_finder.iteritems(): 247 model = value.get_model() 248 for item in self.model_toFit: 249 if model in item and not model in self.constraint_dict.keys(): 250 self.constraint_dict[model]=FitConstraint(model=model, page=page) 251 252 253 def _display_constraint(self, event): 254 """ 255 Show fields to add constraint 256 """ 257 if len(self.model_toFit)< 2: 258 msg= "Select at least 2 models to add constraint " 259 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 260 ## hide button 261 self._hide_constraint() 262 return 263 if self.show_constraint.GetValue(): 264 self.sizer_couples.Clear(True) 265 self._show_constraint() 266 return 267 else: 268 self._hide_constraint() 269 return 270 194 271 195 def set_model(self): 196 """ 197 set_model take values in self.params which are the values 198 entered by the user and try to assign them into the model 199 concerned in self.manager. page.finder 200 """ 201 if len(self.params) >0: 202 for item in self.model_toFit: 203 list=item[1].get_model() 204 model=list[0] 205 param_list=model.getParamList() 206 if self.params !=[]: 207 for element in self.params: 208 if model.name == str(element[0]): 209 for item in param_list: 210 if item==str(element[1]): 211 self.manager.set_page_finder(model.name,element[1], 212 str(element[2])) 213 214 215 def _onTextEnter(self): 272 def _show_constraint(self): 273 """ 274 Show constraint fields 275 """ 276 flag = False 277 if len(self.constraints_list)!= 0: 278 nb_fit_param = 0 279 for value in self.constraint_dict.values(): 280 nb_fit_param += len(value.fittable_param) 281 ##Don't add anymore 282 if len(self.constraints_list) == nb_fit_param: 283 flag = True 284 if len(self.model_toFit) < 2 or flag: 285 msg= "Select at least 2 model to add constraint " 286 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 287 self.sizer_couples.Layout() 288 self.sizer2.Layout() 289 self.SetScrollbars(20,20,200,100) 290 return 291 292 sizer_constraint = wx.BoxSizer(wx.HORIZONTAL) 293 294 model_cbox = wx.ComboBox(self, -1) 295 model_cbox.Clear() 296 297 param_cbox = wx.ComboBox(self, -1) 298 wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param) 299 param_cbox.Hide() 300 301 ctl2 = wx.TextCtrl(self, -1) 302 ctl2.Bind(wx.EVT_TEXT_ENTER,self._onTextEnter) 303 ctl2.Hide() 304 305 egal_txt= wx.StaticText(self,-1," = ") 306 egal_txt.Hide() 307 308 309 for model, value in self.constraint_dict.iteritems(): 310 ## check if all parameters have been selected for constraint 311 ## then do not allow add constraint on parameters 312 if len(value.fittable_param)!= len(value.selected_params): 313 ## the user can apply constraint only on model select to fit 314 model_cbox.Append( str(model.name), model) 315 316 317 wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model) 318 319 ##[combobox1, combobox2,=,textcrtl ] 320 self.constraints_list.append([model_cbox, param_cbox, egal_txt, ctl2]) 321 322 sizer_constraint.Add(model_cbox) 323 sizer_constraint.Add((10,10)) 324 sizer_constraint.Add(param_cbox) 325 sizer_constraint.Add(egal_txt) 326 sizer_constraint.Add(ctl2) 327 sizer_constraint.Add((10,10)) 328 329 330 self.sizer_couples.Add(sizer_constraint) 331 self.sizer_couples.Add((10,10)) 332 self.sizer_couples.Layout() 333 self.sizer2.Layout() 334 self.SetScrollbars(20,20,200,100) 335 336 def _hide_constraint(self): 337 """ 338 hide buttons related constraint 339 """ 340 if len(self.constraint_dict)>0: 341 for value in self.constraint_dict.itervalues(): 342 value.selected_params=[] 343 param = value.fittable_param## list of parameter name 344 ## reset the constraint to None on fitproblem 345 for item in param: 346 self.page_finder[value.page].set_model_param(item,None) 347 348 self.btAdd.Hide() 349 self.constraints_list=[] 350 self.sizer_couples.Clear(True) 351 self.sizer_couples.Layout() 352 self.sizer2.Layout() 353 self.SetScrollbars(20,20,200,100) 354 self.AdjustScrollbars() 355 356 357 358 def _on_select_model(self, event): 359 """ 360 fill combox box with list of parameters 361 """ 362 model = event.GetClientData() 363 param_list= self.constraint_dict[model].fittable_param 364 length = len(self.constraints_list) 365 if length < 1: 366 return 367 368 param_cbox = self.constraints_list[length-1][1] 369 param_cbox.Clear() 370 ## insert only fittable paramaters 371 for param in param_list: 372 if not param in self.constraint_dict[model].selected_params: 373 param_cbox.Append( str(param), model) 374 param_cbox.Show(True) 375 376 377 self.sizer2.Layout() 378 self.SetScrollbars(20,20,200,100) 379 380 381 def _on_select_param(self, event): 382 """ 383 Store the appropriate constraint in the page_finder 384 """ 385 model = event.GetClientData() 386 param = event.GetString() 387 self.constraint_dict[model].selected_params.append(param) 388 389 length = len(self.constraints_list) 390 if length < 1: 391 return 392 egal_txt = self.constraints_list[length-1][2] 393 egal_txt.Show(True) 394 395 ctl2 = self.constraints_list[length-1][3] 396 ctl2.Show(True) 397 398 self.btAdd.Show(True) 399 self.sizer2.Layout() 400 self.SetScrollbars(20,20,200,100) 401 402 403 def _onAdd_constraint(self, event): 404 """ 405 Add another line for constraint 406 """ 407 msg= " " 408 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 409 410 ## check that a constraint is added before allow to add another cosntraint 411 412 for item in self.constraints_list: 413 model_cbox = item[0] 414 if model_cbox.GetString(0)=="": 415 msg= " Select a model Name! " 416 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 417 return 418 param_cbox = item[1] 419 if param_cbox.GetString(0)=="": 420 msg= " Select a parameter Name! " 421 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 422 return 423 ctl2 = item[3] 424 if ctl2.GetValue().lstrip().rstrip()=="": 425 msg= " Enter a constraint for %s.%s! "%(model_cbox.GetString(0), 426 param_cbox.GetString(0)) 427 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 428 return 429 430 ## some model or parameters can be constrained 431 self._show_constraint() 432 433 434 435 436 437 def _fill_sizer_constraint(self): 438 """ 439 Fill sizer containing constraint info 440 """ 441 msg= "Select at least 2 model to add constraint " 442 wx.PostEvent(self.parent.Parent, StatusEvent(status= msg )) 443 444 self.sizer2.Clear(True) 445 446 box_description= wx.StaticBox(self, -1,"Fit Constraints") 447 boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) 448 sizer_title = wx.BoxSizer(wx.HORIZONTAL) 449 self.sizer_couples = wx.BoxSizer(wx.VERTICAL) 450 sizer_button = wx.BoxSizer(wx.HORIZONTAL) 451 452 self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) 453 self.show_constraint = wx.RadioButton(self, -1, 'Yes', (10, 30)) 454 455 self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint, 456 id= self.hide_constraint.GetId() ) 457 458 self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint, 459 id= self.show_constraint.GetId() ) 460 461 462 463 464 sizer_title.Add( wx.StaticText(self,-1," Model") ) 465 sizer_title.Add(( 10,10) ) 466 sizer_title.Add( wx.StaticText(self,-1," Parameter") ) 467 sizer_title.Add(( 10,10) ) 468 sizer_title.Add( wx.StaticText(self,-1," Add Constraint?") ) 469 sizer_title.Add(( 10,10) ) 470 sizer_title.Add( self.show_constraint ) 471 sizer_title.Add( self.hide_constraint ) 472 sizer_title.Add(( 10,10) ) 473 474 475 476 ## Draw combobox box related to model name and model parameters 477 if self.show_constraint.GetValue(): 478 self._fill_sizer_constraint_helper(self.sizer_couples) 479 480 self.btAdd =wx.Button(self,wx.NewId(),'Add') 481 self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint,id= self.btAdd.GetId()) 482 self.btAdd.SetToolTipString("Add another constraint?") 483 self.btAdd.Hide() 484 485 self.btFit = wx.Button(self,wx.NewId(),'Fit') 486 self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId()) 487 self.btFit.SetToolTipString("Perform fit.") 488 489 text_hint = wx.StaticText(self,-1,"Example: M0.paramter = M1.parameter") 490 sizer_button.Add(text_hint, 0 , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) 491 sizer_button.Add(self.btAdd, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) 492 sizer_button.Add(self.btFit, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10) 493 494 boxsizer1.Add(sizer_title) 495 boxsizer1.Add((10,10)) 496 boxsizer1.Add(self.sizer_couples) 497 boxsizer1.Add((10,10)) 498 boxsizer1.Add(sizer_button) 499 500 self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) 501 self.sizer2.Layout() 502 self.SetScrollbars(20,20,200,100) 503 504 505 506 def _onTextEnter(self, event): 507 """ 508 callback function for textcrtl 509 """ 510 self.btAdd.Show(True) 511 self.sizer2.Layout() 512 self.SetScrollbars(20,20,200,100) 513 self._set_constraint() 514 515 def _set_constraint(self): 216 516 """ 217 517 get values from the constrainst textcrtl ,parses them into model name … … 220 520 uses it to reset the appropriate model and its appropriates parameters 221 521 """ 222 value= self.ctl2.GetValue() 223 if value: 224 self.params=[] 522 for item in self.constraints_list: 523 model = item[0].GetClientData(0) 524 param = item[1].GetString(0) 525 constraint = item[3].GetValue().lstrip().rstrip() 526 if model in self.constraint_dict.keys(): 527 page = self.constraint_dict[model].page 528 if constraint != "": 529 self.page_finder[page].set_model_param(param,constraint) 530 531 532 533 def _fill_sizer_model_list(self,sizer): 534 """ 535 Receive a dictionary containing information to display model name 536 @param page_finder: the dictionary containing models information 537 """ 538 ix = 0 539 iy = 0 540 list=[] 541 sizer.Clear(True) 542 543 new_name = wx.StaticText(self, -1, 'New Model Name', style=wx.ALIGN_CENTER) 544 new_name.SetBackgroundColour('orange') 545 sizer.Add(new_name,(iy, ix),(1,1), 546 wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 547 548 ix +=2 549 model_type = wx.StaticText(self, -1, ' Model Type') 550 model_type.SetBackgroundColour('grey') 551 sizer.Add(model_type,(iy, ix),(1,1), 552 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 553 ix +=1 554 data_used = wx.StaticText(self, -1, ' Used Data') 555 data_used.SetBackgroundColour('grey') 556 sizer.Add(data_used,(iy, ix),(1,1), 557 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 558 559 for page, value in self.page_finder.iteritems(): 225 560 try: 226 expression='[\s,;]' 227 if re.search(expression,value) !=None: 228 word=re.split(expression,value) 229 for item in word: 230 self.params.append(self.parser_helper(item)) 231 else: 232 self.params.append(self.parser_helper(value)) 561 ix = 0 562 iy += 1 563 model = value.get_model() 564 cb = wx.CheckBox(self, -1, str(model.name)) 565 cb.SetValue(False) 566 sizer.Add( cb,( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 567 wx.EVT_CHECKBOX(self, cb.GetId(), self.check_model_name) 568 569 570 ix +=2 571 type = model.__class__.__name__ 572 model_type = wx.StaticText(self, -1, str(type)) 573 sizer.Add(model_type,( iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 574 575 ix +=1 576 data = value.get_fit_data() 577 data_used= wx.StaticText(self, -1, str(data.name)) 578 sizer.Add(data_used,( iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) 579 580 581 self.model_list.append([cb,value,page,model]) 582 233 583 except: 234 wx.PostEvent(self.parent.Parent, StatusEvent(status="Constraint Error: %s" % sys.exc_value)) 235 236 237 def parser_helper(self,value): 238 """ 239 @return param:a list containing the name of a model ,its parameters 240 value and name extracted from the constrainst controlbox 241 """ 242 if string.find(value, "=") !=-1: 243 model_param= re.split("=",value) 244 param_name=model_param[0] 245 param_value=model_param[1] 246 247 if string.find(param_name,".")!=-1: 248 param_names= re.split("\.",param_name) 249 model_name=param_names[0] 250 param_name=param_names[1] 251 param=[str(model_name),param_name,str(param_value)] 252 return param 253 else: 254 wx.PostEvent(self.parent.Parent, 255 StatusEvent(status="cannot evaluate this expression")) 256 return 257 else: 258 wx.PostEvent(self.parent.Parent, StatusEvent(status="Missing '=' in expression")) 259 584 pass 585 iy +=1 586 sizer.Add((20,20),( iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 587 sizer.Layout() 588 260 589 590 591 592 class HelpWindow(wx.Frame): 593 def __init__(self, parent, id, title): 594 wx.Frame.__init__(self, parent, id, title, size=(570, 400)) 595 596 page_finder ={} 597 ## create random data 598 from danse.common.plottools.plottables import Data1D 599 data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) 600 data.name="mydata.txt" 601 ## create model 602 from sans.models.CylinderModel import CylinderModel 603 model = CylinderModel() 604 model.name="M0" 605 606 607 from fitproblem import FitProblem 608 page_finder["page"]= FitProblem() 609 ## fill the page_finder 610 page_finder["page"].add_fit_data(data) 611 page_finder["page"].set_model(model) 612 self.page = SimultaneousFitPage(self, page_finder=page_finder) 613 614 615 616 self.Centre() 617 self.Show(True) 618 619 620 621 if __name__=="__main__": 622 app = wx.App() 623 HelpWindow(None, -1, 'HelpWindow') 624 app.MainLoop() 261 625
Note: See TracChangeset
for help on using the changeset viewer.