source: sasview/sansview/perspectives/fitting/simfitpage.py @ 4ce74917

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 4ce74917 was 4ce74917, checked in by Gervaise Alina <gervyh@…>, 13 years ago

remove guicomm

  • Property mode set to 100644
File size: 23.2 KB
Line 
1
2import sys,re,string, wx 
3import wx.lib.newevent 
4from sans.guiframe.events import StatusEvent
5   
6#Control panel width
7if sys.platform.count("darwin")==0:
8    PANEL_WID = 420
9    FONT_VARIANT = 0
10else:
11    PANEL_WID = 490
12    FONT_VARIANT = 1
13           
14def get_fittableParam( model):
15    """
16    return list of fittable parameters name of a model
17   
18    :param model: the model used
19   
20    """
21    fittable_param=[]
22   
23    for item in model.getParamList():
24        if not item  in model.getDispParamList():
25            if not item in model.non_fittable:
26                fittable_param.append(item)
27           
28    for item in model.fixed:
29        fittable_param.append(item)
30       
31    return fittable_param
32
33class SimultaneousFitPage(wx.ScrolledWindow):
34    """
35    Simultaneous fitting panel
36    All that needs to be defined are the
37    two data members window_name and window_caption
38    """
39    ## Internal name for the AUI manager
40    window_name = "simultaneous Fit page"
41    ## Title to appear on top of the window
42    window_caption = "Simultaneous Fit Page"
43   
44   
45    def __init__(self, parent,page_finder ={}, *args, **kwargs):
46        wx.ScrolledWindow.__init__(self, parent,style= wx.FULL_REPAINT_ON_RESIZE )
47        """
48        Simultaneous page display
49        """
50        ##Font size
51        self.SetWindowVariant(variant = FONT_VARIANT)
52       
53        self.parent = parent
54        ## store page_finder
55        self.page_finder = page_finder
56        ## list contaning info to set constraint
57        ## look like self.constraint_dict[page]= page
58        self.constraint_dict={}
59        ## item list  self.constraints_list=[combobox1, combobox2,=,textcrtl, button ]
60        self.constraints_list=[]
61        ## list of current model
62        self.model_list=[]
63        ## selected mdoel to fit
64        self.model_toFit=[]
65        ## number of constraint
66        self.nb_constraint= 0
67        ## draw page
68        self.define_page_structure()
69        self.draw_page()
70        self.set_layout()
71       
72    def define_page_structure(self):
73        """
74        Create empty sizer for a panel
75        """
76        self.vbox  = wx.BoxSizer(wx.VERTICAL)
77        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
78        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
79        self.sizer3 = wx.BoxSizer(wx.VERTICAL)
80
81        self.sizer1.SetMinSize((PANEL_WID,-1))
82        self.sizer2.SetMinSize((PANEL_WID,-1))
83        self.sizer3.SetMinSize((PANEL_WID,-1))
84        self.vbox.Add(self.sizer1)
85        self.vbox.Add(self.sizer2)
86        self.vbox.Add(self.sizer3)
87       
88    def set_scroll(self):
89        """
90        """
91        self.SetScrollbars(20,20,25,65)
92        self.Layout() 
93         
94    def set_layout(self):
95        """
96        layout
97        """
98        self.vbox.Layout()
99        self.vbox.Fit(self) 
100        self.SetSizer(self.vbox)
101        self.set_scroll()
102        self.Centre()
103       
104    def onRemove(self, event):
105        """
106        Remove constraint fields
107        """
108        if len(self.constraints_list)==1:
109            self.hide_constraint.SetValue(True)
110            self._hide_constraint()
111            return 
112        if len(self.constraints_list)==0:
113            return 
114        for item in self.constraints_list:
115            length= len(item)
116            if event.GetId()==item[length-2].GetId():
117                sizer= item[length-1]
118                sizer.Clear(True)
119                self.sizer_constraints.Remove(sizer)
120             
121                self.sizer2.Layout()
122                self.SetScrollbars(20,20,25,65)
123                self.constraints_list.remove(item)
124                self.nb_constraint -= 1
125                break
126               
127    def onFit(self,event):
128        """
129        signal for fitting
130       
131        """
132        ## making sure all parameters content a constraint
133        ## validity of the constraint expression is own by fit engine
134        if self.show_constraint.GetValue():
135            self._set_constraint()
136        ## get the fit range of very fit problem       
137        for page, value in self.page_finder.iteritems():
138            qmin, qmax= page.get_range()
139            value.set_range(qmin, qmax)
140        ## model was actually selected from this page to be fit
141        if len(self.model_toFit) >= 1 :
142            self.manager._reset_schedule_problem( value=0)
143            for item in self.model_list:
144                if item[0].GetValue():
145                    self.manager.schedule_for_fit( value=1,fitproblem =item[1]) 
146            self.manager.onFit()
147        else:
148            msg= "Select at least one model to fit "
149            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
150           
151    def set_manager(self, manager):
152        """
153        set panel manager
154       
155        :param manager: instance of plugin fitting
156       
157        """
158        self.manager = manager
159       
160    def check_all_model_name(self,event):
161        """
162        check all models names
163        """
164        self.model_toFit=[] 
165        if self.cb1.GetValue()==True:
166            for item in self.model_list:
167                item[0].SetValue(True)
168                self.model_toFit.append(item)
169               
170            ## constraint info
171            self._store_model()
172            ## display constraint fields
173            if self.show_constraint.GetValue():
174                self._show_constraint()
175                return
176        else:
177            for item in self.model_list:
178                item[0].SetValue(False) 
179               
180            self.model_toFit=[]
181            ##constraint info
182            self._hide_constraint()
183       
184    def check_model_name(self,event):
185        """
186        Save information related to checkbox and their states
187        """
188        self.model_toFit=[]
189        for item in self.model_list:
190            if item[0].GetValue()==True:
191                self.model_toFit.append(item)
192            else:
193                if item in self.model_toFit:
194                    self.model_toFit.remove(item)
195                    self.cb1.SetValue(False)
196       
197        ## display constraint fields
198        if len(self.model_toFit)==2:
199            self._store_model()
200            if self.show_constraint.GetValue() and len(self.constraints_list)==0:
201                self._show_constraint()
202        elif len(self.model_toFit)< 2:
203            ##constraint info
204            self._hide_constraint()             
205       
206        ## set the value of the main check button         
207        if len(self.model_list)==len(self.model_toFit):
208            self.cb1.SetValue(True)
209            return
210        else:
211            self.cb1.SetValue(False)
212       
213    def draw_page(self):     
214        """
215        Draw a sizer containing couples of data and model
216        """ 
217        self.model_list=[]
218        self.model_toFit=[]
219        self.constraints_list=[]
220        self.constraint_dict={}
221        self.nb_constraint= 0
222       
223        if len(self.model_list)>0:
224            for item in self.model_list:
225                item[0].SetValue(False) 
226                self.manager.schedule_for_fit( value=0,fitproblem =item[1])
227               
228        self.sizer1.Clear(True)   
229        box_description= wx.StaticBox(self, -1,"Fit Combinations")
230        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
231        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
232        sizer_couples = wx.GridBagSizer(5,5)
233        #------------------------------------------------------
234        if len(self.page_finder)==0:
235            sizer_title.Add(wx.StaticText(self,-1," No fit combinations are found!"))
236        else:
237            ## store model 
238            self._store_model()
239       
240            self.cb1 = wx.CheckBox(self, -1,'Select all')
241            self.cb1.SetValue(False)
242            wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name)
243           
244            sizer_title.Add((10,10),0,
245                wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
246            sizer_title.Add(self.cb1,0,
247                wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
248           
249            ## draw list of model and data name
250            self._fill_sizer_model_list(sizer_couples)
251            ## draw the sizer containing constraint info
252            self._fill_sizer_constraint()
253            ## draw fit button
254            self._fill_sizer_fit()
255        #--------------------------------------------------------
256        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=5) 
257        boxsizer1.Add(sizer_couples, flag= wx.TOP|wx.BOTTOM,border=5)
258       
259        self.sizer1.Add(boxsizer1,1, wx.EXPAND | wx.ALL, 10)
260        self.sizer1.Layout()
261        self.SetScrollbars(20,20,25,65)
262        self.AdjustScrollbars()
263       
264    def _store_model(self):
265        """
266         Store selected model
267        """
268        if len(self.model_toFit) < 2:
269            return
270        for item in self.model_toFit:
271            model = item[3]
272            page= item[2]
273            self.constraint_dict[page] = model
274                   
275    def _display_constraint(self, event):
276        """
277        Show fields to add constraint
278        """
279        if len(self.model_toFit)< 2:
280            msg= "Select at least 2 models to add constraint "
281            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
282            ## hide button
283            self._hide_constraint()
284            return
285        if self.show_constraint.GetValue():
286            self._show_constraint()
287            return
288        else:
289           self._hide_constraint()
290           return 
291           
292    def _show_constraint(self):
293        """
294        Show constraint fields
295        """
296        self.btAdd.Show(True)
297        if len(self.constraints_list)!= 0:
298            nb_fit_param = 0
299            for model in self.constraint_dict.values():
300                nb_fit_param += len(get_fittableParam(model))
301            ##Don't add anymore
302            if len(self.constraints_list) == nb_fit_param:
303                msg= "Cannot add another constraint .Maximum of number "
304                msg += "Parameters name reached %s"%str(nb_fit_param)
305                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
306                self.sizer_constraints.Layout()
307                self.sizer2.Layout()
308                self.SetScrollbars(20,20,25,65)
309                return
310           
311        if len(self.model_toFit) < 2 :
312            msg= "Select at least 2 model to add constraint "
313            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
314            self.sizer_constraints.Layout()
315            self.sizer2.Layout()
316            self.SetScrollbars(20,20,25,65)
317            return
318           
319        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL)
320        model_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY)
321        model_cbox.Clear()
322        param_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY)
323        param_cbox.Hide()
324       
325        #This is for GetCLientData() _on_select_param: Was None return on MAC.
326        self.param_cbox = param_cbox
327       
328        wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param)
329        ctl2 = wx.TextCtrl(self, -1)
330        egal_txt= wx.StaticText(self,-1," = ")
331        btRemove = wx.Button(self,wx.NewId(),'Remove')
332        btRemove.Bind(wx.EVT_BUTTON, self.onRemove,id= btRemove.GetId())
333        btRemove.SetToolTipString("Remove constraint.")
334       
335        for page,model in self.constraint_dict.iteritems():
336            ## check if all parameters have been selected for constraint
337            ## then do not allow add constraint on parameters
338            model_cbox.Append( str(model.name), model)
339           
340        #This is for GetCLientData() passing to self._on_select_param: Was None return on MAC.
341        self.model_cbox = model_cbox
342           
343        wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model)
344   
345        sizer_constraint.Add(model_cbox, flag= wx.RIGHT|wx.EXPAND,border=10)
346        sizer_constraint.Add(param_cbox, flag= wx.RIGHT|wx.EXPAND,border=5)
347        sizer_constraint.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND,border=5)
348        sizer_constraint.Add(ctl2, flag= wx.RIGHT|wx.EXPAND,border=10)
349        sizer_constraint.Add(btRemove, flag= wx.RIGHT|wx.EXPAND,border=10)
350     
351        self.sizer_constraints.Insert(before=self.nb_constraint,
352                                      item=sizer_constraint, flag= wx.TOP|wx.BOTTOM|wx.EXPAND,
353                                   border=5)
354        ##[combobox1, combobox2,=,textcrtl, remove button ]
355        self.constraints_list.append([model_cbox, param_cbox, egal_txt, ctl2,btRemove,sizer_constraint])
356   
357        self.nb_constraint += 1
358        self.sizer_constraints.Layout()
359        self.sizer2.Layout()
360        self.SetScrollbars(20,20,25,65)
361       
362    def _hide_constraint(self): 
363        """
364        hide buttons related constraint
365        """ 
366        for page in  self.page_finder.iterkeys():
367            self.page_finder[page].clear_model_param()
368               
369        self.nb_constraint =0     
370        self.constraint_dict={}
371        if hasattr(self,"btAdd"):
372            self.btAdd.Hide()
373        self._store_model()
374        self.constraints_list=[]         
375        self.sizer_constraints.Clear(True) 
376        self.sizer_constraints.Layout()   
377        self.sizer2.Layout()
378        self.SetScrollbars(20,20,25,65)
379        self.AdjustScrollbars()   
380           
381    def _on_select_model(self, event):
382        """
383        fill combox box with list of parameters
384        """
385        ##This way PC/MAC both work, instead of using event.GetClientData().
386        n = self.model_cbox.GetCurrentSelection()
387        model = self.model_cbox.GetClientData(n)
388       
389        param_list= get_fittableParam(model)
390        length = len(self.constraints_list)
391        if length < 1:
392            return 
393        param_cbox = self.constraints_list[length-1][1]
394        param_cbox.Clear()
395        ## insert only fittable paramaters
396        for param in param_list:
397            param_cbox.Append( str(param), model)
398           
399        param_cbox.Show(True)
400        self.sizer2.Layout()
401        self.SetScrollbars(20,20,25,65)
402       
403    def _on_select_param(self, event):
404        """
405        Store the appropriate constraint in the page_finder
406        """
407        ##This way PC/MAC both work, instead of using event.GetClientData().
408        n = self.param_cbox.GetCurrentSelection()
409        model = self.param_cbox.GetClientData(n)
410        param = event.GetString()
411     
412        length = len(self.constraints_list)
413        if length < 1:
414            return 
415        egal_txt = self.constraints_list[length-1][2]
416        egal_txt.Show(True)       
417       
418        ctl2 = self.constraints_list[length-1][3]
419        ctl2.Show(True)
420        #self.sizer2.Layout()
421        self.SetScrollbars(20,20,25,65)
422       
423    def _onAdd_constraint(self, event): 
424        """
425        Add another line for constraint
426        """
427        if not self.show_constraint.GetValue():
428            msg= " Select Yes to add Constraint "
429            wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
430            return 
431        ## check that a constraint is added before allow to add another cosntraint
432        for item in self.constraints_list:
433            model_cbox = item[0]
434            if model_cbox.GetString(0)=="":
435                msg= " Select a model Name! "
436                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
437                return 
438            param_cbox = item[1]
439            if param_cbox.GetString(0)=="":
440                msg= " Select a parameter Name! "
441                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
442                return 
443            ctl2 = item[3]
444            if ctl2.GetValue().lstrip().rstrip()=="":
445                model= param_cbox.GetClientData(param_cbox.GetCurrentSelection())
446                msg= " Enter a constraint for %s.%s! "%(model.name,param_cbox.GetString(0))           
447                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
448                return 
449        ## some model or parameters can be constrained
450        self._show_constraint()
451       
452    def _fill_sizer_fit(self):
453        """
454        Draw fit button
455        """
456        self.sizer3.Clear(True)
457        box_description= wx.StaticBox(self, -1,"Fit ")
458        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
459        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
460         
461        self.btFit = wx.Button(self,wx.NewId(),'Fit')
462        self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId())
463        self.btFit.SetToolTipString("Perform fit.")
464       
465        text= "Hint: Park fitting engine will be selected \n"
466        text+= "automatically for more than 2 combinations checked"
467        text_hint = wx.StaticText(self,-1,text)
468       
469        sizer_button.Add(text_hint,  wx.RIGHT|wx.EXPAND, 10)
470        sizer_button.Add(self.btFit, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
471       
472        boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM,border=10)
473        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
474        self.sizer3.Layout()
475        self.SetScrollbars(20,20,25,65)
476       
477    def _fill_sizer_constraint(self):
478        """
479        Fill sizer containing constraint info
480        """
481        msg = "Select at least 2 model to add constraint "
482        wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
483       
484        self.sizer2.Clear(True)
485        box_description= wx.StaticBox(self, -1,"Fit Constraints")
486        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
487        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
488        self.sizer_constraints = wx.BoxSizer(wx.VERTICAL)
489        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
490       
491        self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP)
492        self.show_constraint = wx.RadioButton(self, -1, 'Yes', (10, 30))
493        self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint,
494                    id= self.hide_constraint.GetId() )
495        self.Bind(  wx.EVT_RADIOBUTTON, self._display_constraint,
496                         id= self.show_constraint.GetId()    )
497        self.hide_constraint.SetValue(True)
498        sizer_title.Add( wx.StaticText(self,-1," Model") )
499        sizer_title.Add(( 10,10) )
500        sizer_title.Add( wx.StaticText(self,-1," Parameter") )
501        sizer_title.Add(( 10,10) )
502        sizer_title.Add( wx.StaticText(self,-1," Add Constraint?") )
503        sizer_title.Add(( 10,10) )
504        sizer_title.Add( self.show_constraint )
505        sizer_title.Add( self.hide_constraint )
506        sizer_title.Add(( 10,10) )
507       
508        self.btAdd =wx.Button(self,wx.NewId(),'Add')
509        self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint,id= self.btAdd.GetId())
510        self.btAdd.SetToolTipString("Add another constraint?")
511        self.btAdd.Hide()
512     
513        text_hint = wx.StaticText(self,-1,"Example: [M0][paramter] = M1.parameter") 
514        sizer_button.Add(text_hint, 0 , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
515        sizer_button.Add(self.btAdd, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
516       
517        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=10)
518        boxsizer1.Add(self.sizer_constraints, flag= wx.TOP|wx.BOTTOM,border=10)
519        boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM,border=10)
520       
521        self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
522        self.sizer2.Layout()
523        self.SetScrollbars(20,20,25,65)
524   
525    def _set_constraint(self):
526        """
527        get values from the constrainst textcrtl ,parses them into model name
528        parameter name and parameters values.
529        store them in a list self.params .when when params is not empty set_model
530        uses it to reset the appropriate model and its appropriates parameters
531        """
532        for item in self.constraints_list:
533            model = item[0].GetClientData(item[0].GetCurrentSelection())
534            param = item[1].GetString(item[1].GetCurrentSelection())
535            constraint = item[3].GetValue().lstrip().rstrip()
536            if param.lstrip().rstrip()=="":
537                param= None
538                msg= " Constraint will be ignored!. missing parameters in combobox"
539                msg+= " to set constraint! "
540                wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
541            for page , value in self.constraint_dict.iteritems():
542                if model == value:
543                    if constraint == "":
544                        msg= " Constraint will be ignored!. missing value in textcrtl"
545                        msg+= " to set constraint! "
546                        wx.PostEvent(self.parent.Parent, StatusEvent(status= msg ))
547                        constraint = None
548                    self.page_finder[page].set_model_param(param,constraint)
549                    break
550   
551    def _fill_sizer_model_list(self,sizer):
552        """
553        Receive a dictionary containing information to display model name
554       
555        :param page_finder: the dictionary containing models information
556       
557        """
558        ix = 0
559        iy = 0
560        list=[]
561        sizer.Clear(True)
562       
563        new_name = wx.StaticText(self, -1, 'New Model Name', style=wx.ALIGN_CENTER)
564        new_name.SetBackgroundColour('orange')
565        sizer.Add(new_name,(iy, ix),(1,1),
566                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
567        ix += 2 
568        model_type = wx.StaticText(self, -1, '  Model Type')
569        model_type.SetBackgroundColour('grey')
570        sizer.Add(model_type,(iy, ix),(1,1),
571                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
572        ix += 1 
573        data_used = wx.StaticText(self, -1, '  Used Data')
574        data_used.SetBackgroundColour('grey')
575        sizer.Add(data_used,(iy, ix),(1,1),
576                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
577       
578        for page, value in self.page_finder.iteritems():
579            try:
580                ix = 0
581                iy += 1 
582                model = value.get_model()
583                cb = wx.CheckBox(self, -1, str(model.name))
584                cb.SetValue(False)
585                sizer.Add( cb,( iy,ix),(1,1),  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
586                wx.EVT_CHECKBOX(self, cb.GetId(), self.check_model_name)
587               
588                ix += 2 
589                type = model.__class__.__name__
590                model_type = wx.StaticText(self, -1, str(type))
591                sizer.Add(model_type,( iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
592               
593                ix += 1 
594                data = value.get_fit_data()
595                data_used= wx.StaticText(self, -1, str(data.name))
596                sizer.Add(data_used,( iy,ix),(1,1),  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
597               
598                self.model_list.append([cb,value,page,model])
599               
600            except:
601                pass
602        iy += 1
603        sizer.Add((20,20),( iy,ix),(1,1),  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
604        sizer.Layout()   
Note: See TracBrowser for help on using the repository browser.