source: sasview/sansview/perspectives/fitting/simfitpage.py @ 5062bbf

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 5062bbf was 5062bbf, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

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