source: sasview/fittingview/src/sans/perspectives/fitting/simfitpage.py @ 7124300

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 7124300 was 1aa6145, checked in by Jae Cho <jhjcho@…>, 13 years ago

added note in fit sizer

  • Property mode set to 100644
File size: 33.9 KB
Line 
1
2import sys,re,string, wx 
3import wx.lib.newevent 
4from sans.guiframe.events import StatusEvent
5from sans.guiframe.panel_base import PanelBase
6from wx.lib.scrolledpanel import ScrolledPanel
7from sans.guiframe.events import PanelOnFocusEvent
8#Control panel width
9if sys.platform.count("darwin")==0:
10    PANEL_WID = 420
11    FONT_VARIANT = 0
12else:
13    PANEL_WID = 490
14    FONT_VARIANT = 1
15           
16def get_fittableParam( model):
17    """
18    return list of fittable parameters name of a model
19   
20    :param model: the model used
21   
22    """
23    fittable_param=[]
24    for item in model.getParamList():
25        if not item  in model.getDispParamList():
26            if not item in model.non_fittable:
27                fittable_param.append(item)
28           
29    for item in model.fixed:
30        fittable_param.append(item)
31       
32    return fittable_param
33
34class SimultaneousFitPage(ScrolledPanel, PanelBase):
35    """
36    Simultaneous fitting panel
37    All that needs to be defined are the
38    two data members window_name and window_caption
39    """
40    ## Internal name for the AUI manager
41    window_name = "simultaneous Fit page"
42    ## Title to appear on top of the window
43    window_caption = "Simultaneous Fit Page"
44   
45   
46    def __init__(self, parent,page_finder ={}, *args, **kwargs):
47        ScrolledPanel.__init__(self, parent,style= wx.FULL_REPAINT_ON_RESIZE )
48        PanelBase.__init__(self, parent)
49        """
50        Simultaneous page display
51        """
52        self.SetupScrolling()
53        ##Font size
54        self.SetWindowVariant(variant = FONT_VARIANT)
55        self.uid = wx.NewId()
56        self.parent = parent
57        ## store page_finder
58        self.page_finder = page_finder
59        ## list contaning info to set constraint
60        ## look like self.constraint_dict[page_id]= page
61        self.constraint_dict={}
62        ## item list 
63        # self.constraints_list=[combobox1, combobox2,=,textcrtl, button ]
64        self.constraints_list=[]
65        ## list of current model
66        self.model_list=[]
67        ## selected mdoel to fit
68        self.model_toFit=[]
69        ## number of constraint
70        self.nb_constraint= 0
71        self.uid = wx.NewId()
72        ## draw page
73        self.define_page_structure()
74        self.draw_page()
75        self.set_layout()
76       
77    def define_page_structure(self):
78        """
79        Create empty sizer for a panel
80        """
81        self.vbox  = wx.BoxSizer(wx.VERTICAL)
82        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
83        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
84        self.sizer3 = wx.BoxSizer(wx.VERTICAL)
85
86        self.sizer1.SetMinSize((PANEL_WID,-1))
87        self.sizer2.SetMinSize((PANEL_WID,-1))
88        self.sizer3.SetMinSize((PANEL_WID,-1))
89        self.vbox.Add(self.sizer1)
90        self.vbox.Add(self.sizer2)
91        self.vbox.Add(self.sizer3)
92       
93    def set_scroll(self):
94        """
95        """
96        #self.SetScrollbars(20,20,25,65)
97        self.Layout() 
98         
99    def set_layout(self):
100        """
101        layout
102        """
103        self.vbox.Layout()
104        self.vbox.Fit(self) 
105        self.SetSizer(self.vbox)
106        self.set_scroll()
107        self.Centre()
108       
109    def onRemove(self, event):
110        """
111        Remove constraint fields
112        """
113        if len(self.constraints_list)==1:
114            self.hide_constraint.SetValue(True)
115            self._hide_constraint()
116            return 
117        if len(self.constraints_list)==0:
118            return 
119        for item in self.constraints_list:
120            length= len(item)
121            if event.GetId()==item[length-2].GetId():
122                sizer= item[length-1]
123                sizer.Clear(True)
124                self.sizer_constraints.Remove(sizer)
125                #self.SetScrollbars(20,20,25,65)
126                self.constraints_list.remove(item)
127                self.nb_constraint -= 1
128                self.sizer2.Layout()
129                self.Layout()
130                break
131        self._onAdd_constraint(None)   
132             
133    def onFit(self, event):
134        """
135        signal for fitting
136       
137        """
138        ## making sure all parameters content a constraint
139        ## validity of the constraint expression is own by fit engine
140        if self.parent._manager._fit_engine != "park" and\
141                    self._manager.sim_page.uid == self.uid: 
142            msg = "The FitEnging will be set to 'Park' fit engine\n"
143            msg += " for the simultaneous fit..."
144            #wx.MessageBox(msg, 'Info')
145            wx.PostEvent(self._manager.parent, StatusEvent(status=\
146                            "Fitting: %s"%msg, info="info"))
147        if self.show_constraint.GetValue():
148            if not self._set_constraint():
149                return
150        ## model was actually selected from this page to be fit
151        if len(self.model_toFit) >= 1 :
152            self.manager._reset_schedule_problem(value=0)
153            for item in self.model_list:
154                if item[0].GetValue():
155                    self.manager.schedule_for_fit(value=1, uid=item[2]) 
156            try:
157                if not self.manager.onFit(uid=self.uid):
158                    return
159            except:
160                msg= "Select at least one parameter to fit in the FitPages."
161                wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
162        else:
163            msg= "Select at least one model check box to fit "
164            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
165           
166    def set_manager(self, manager):
167        """
168        set panel manager
169       
170        :param manager: instance of plugin fitting
171       
172        """
173        self.manager = manager
174       
175    def check_all_model_name(self,event):
176        """
177        check all models names
178        """
179        self.model_toFit = [] 
180        if self.cb1.GetValue()== True:
181            for item in self.model_list:
182                if item[0].IsEnabled():
183                    item[0].SetValue(True)
184                    self.model_toFit.append(item)
185               
186            ## constraint info
187            self._store_model()
188            ## display constraint fields
189            if self.show_constraint.GetValue():
190                self._show_all_constraint() 
191                self._show_constraint()
192                return
193        else:
194            for item in self.model_list:
195                item[0].SetValue(False) 
196               
197            self.model_toFit=[]
198            ##constraint info
199            self._hide_constraint()
200       
201    def check_model_name(self,event):
202        """
203        Save information related to checkbox and their states
204        """
205        self.model_toFit=[]
206        for item in self.model_list:
207            if item[0].GetValue()==True:
208                self.model_toFit.append(item)
209            else:
210                if item in self.model_toFit:
211                    self.model_toFit.remove(item)
212                    self.cb1.SetValue(False)
213       
214        ## display constraint fields
215        if len(self.model_toFit)==1:
216            self._store_model()
217            if self.show_constraint.GetValue() and\
218                             len(self.constraints_list)==0:
219                self._show_all_constraint() 
220                self._show_constraint()
221        elif len(self.model_toFit)< 1:
222            ##constraint info
223            self._hide_constraint()             
224       
225        ## set the value of the main check button         
226        if len(self.model_list)==len(self.model_toFit):
227            self.cb1.SetValue(True)
228            return
229        else:
230            self.cb1.SetValue(False)
231       
232    def draw_page(self):     
233        """
234        Draw a sizer containing couples of data and model
235        """ 
236        self.model_list=[]
237        self.model_toFit=[]
238        self.constraints_list=[]
239        self.constraint_dict={}
240        self.nb_constraint= 0
241       
242        if len(self.model_list)>0:
243            for item in self.model_list:
244                item[0].SetValue(False) 
245                self.manager.schedule_for_fit(value=0, uid=item[2])
246               
247        self.sizer1.Clear(True)   
248        box_description= wx.StaticBox(self, -1,"Fit Combinations")
249        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
250        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
251        sizer_couples = wx.GridBagSizer(5,5)
252        #------------------------------------------------------
253        if len(self.page_finder)==0:
254            msg = " No fit combinations are found! \n\n"
255            msg += " Please load data and set up "
256            msg += "at least two fit panels first..."
257            sizer_title.Add(wx.StaticText(self, -1, msg))
258        else:
259            ## store model 
260            self._store_model()
261       
262            self.cb1 = wx.CheckBox(self, -1,'Select all')
263            self.cb1.SetValue(False)
264            wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name)
265           
266            sizer_title.Add((10,10),0,
267                wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
268            sizer_title.Add(self.cb1,0,
269                wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
270           
271            ## draw list of model and data name
272            self._fill_sizer_model_list(sizer_couples)
273            ## draw the sizer containing constraint info
274            self._fill_sizer_constraint()
275            ## draw fit button
276            self._fill_sizer_fit()
277        #--------------------------------------------------------
278        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=5) 
279        boxsizer1.Add(sizer_couples, 1, flag= wx.TOP|wx.BOTTOM,border=5)
280       
281        self.sizer1.Add(boxsizer1, 1, wx.EXPAND | wx.ALL, 10)
282        self.sizer1.Layout()
283        #self.SetScrollbars(20,20,25,65)
284        self.AdjustScrollbars()
285        self.Layout()
286       
287    def _store_model(self):
288        """
289         Store selected model
290        """
291        if len(self.model_toFit) < 1:
292            return
293        for item in self.model_toFit:
294            model = item[3]
295            page_id= item[2]
296            self.constraint_dict[page_id] = model
297                   
298    def _display_constraint(self, event):
299        """
300        Show fields to add constraint
301        """
302        if len(self.model_toFit)< 1:
303            msg= "Select at least 1 model to add constraint "
304            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
305            ## hide button
306            self._hide_constraint()
307            return
308        if self.show_constraint.GetValue():
309            self._show_all_constraint() 
310            self._show_constraint()
311            self.Layout()
312            return
313        else:
314           self._hide_constraint()
315           self.Layout()
316           return 
317       
318    def _show_all_constraint(self):
319        """
320        Show constraint fields
321        """
322        #for id, model in self.constraint_dict.iteritems():   
323        box_description= wx.StaticBox(self, -1,"Easy Setup ")
324        boxsizer = wx.StaticBoxSizer(box_description, wx.HORIZONTAL)     
325        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL|wx.LEFT|wx.RIGHT|wx.EXPAND)
326        self.model_cbox_left = wx.ComboBox(self, -1, style=wx.CB_READONLY)
327        self.model_cbox_left.Clear()
328        self.model_cbox_right = wx.ComboBox(self, -1, style=wx.CB_READONLY)
329        self.model_cbox_right.Clear()
330        wx.EVT_COMBOBOX(self.model_cbox_left, -1, self._on_select_modelcb)
331        wx.EVT_COMBOBOX(self.model_cbox_right, -1, self._on_select_modelcb)
332        egal_txt= wx.StaticText(self,-1," = ")
333        self.set_button =wx.Button(self, wx.NewId(),'Set All')
334        self.set_button.Bind(wx.EVT_BUTTON, self._on_set_all_equal,
335                                       id=self.set_button.GetId())
336        set_tip = "Add constraints for all the adjustable parameters "
337        set_tip += "(checked in FitPages) if exist."
338        self.set_button.SetToolTipString(set_tip)
339        self.set_button.Disable()
340       
341        for id, model in self.constraint_dict.iteritems():
342            ## check if all parameters have been selected for constraint
343            ## then do not allow add constraint on parameters
344            self.model_cbox_left.Append( str(model.name), model)
345        self.model_cbox_left.Select(0)
346        for id, model in self.constraint_dict.iteritems():
347            ## check if all parameters have been selected for constraint
348            ## then do not allow add constraint on parameters
349            self.model_cbox_right.Append( str(model.name), model)
350        boxsizer.Add(self.model_cbox_left, 
351                             flag= wx.RIGHT|wx.EXPAND, border=10)
352        boxsizer.Add(wx.StaticText(self, -1,".parameters"),
353                             flag= wx.RIGHT|wx.EXPAND, border=5)
354        boxsizer.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND, border=5)
355        boxsizer.Add(self.model_cbox_right, 
356                             flag= wx.RIGHT|wx.EXPAND, border=10)
357        boxsizer.Add(wx.StaticText(self, -1,".parameters"),
358                             flag= wx.RIGHT|wx.EXPAND,border=5)
359        boxsizer.Add((20,-1))
360        boxsizer.Add(self.set_button, flag= wx.RIGHT|wx.EXPAND, border=5)
361        sizer_constraint.Add(boxsizer, flag= wx.RIGHT|wx.EXPAND, border=5)
362        self.sizer_all_constraints.Insert(before=0,
363                             item=sizer_constraint, 
364                             flag= wx.TOP|wx.BOTTOM|wx.EXPAND, border=5)
365
366        self.sizer_all_constraints.Layout()
367        self.sizer2.Layout()
368        #self.SetScrollbars(20,20,25,65)
369   
370    def _on_select_modelcb(self, event):
371        """
372        On select model left or right combobox
373        """
374        event.Skip()
375        flag = True
376        if self.model_cbox_left.GetValue().strip() == '':
377            flag = False
378        if self.model_cbox_right.GetValue().strip() == '':
379            flag = False
380        if self.model_cbox_left.GetValue() ==\
381                self.model_cbox_right.GetValue():
382            flag = False
383        self.set_button.Enable(flag)
384       
385    def _on_set_all_equal(self, event):
386        """
387        On set button
388        """
389        event.Skip()
390        length = len(self.constraints_list)
391        if length < 1:
392            return 
393        param_list = []
394        param_listB = []
395        selection = self.model_cbox_left.GetCurrentSelection()
396        model_left = self.model_cbox_left.GetValue()
397        model = self.model_cbox_left.GetClientData(selection)
398        selectionB = self.model_cbox_right.GetCurrentSelection()
399        model_right = self.model_cbox_right.GetValue()
400        modelB = self.model_cbox_right.GetClientData(selectionB)
401        for id, dic_model in self.constraint_dict.iteritems():
402            if model == dic_model:
403                param_list = self.page_finder[id].get_param2fit()
404            if modelB == dic_model:
405                param_listB = self.page_finder[id].get_param2fit()
406            if len(param_list) > 0 and len(param_listB) > 0:
407                break
408        #param_list= get_fittableParam(model)
409        #param_listB= get_fittableParam(modelB)
410        num_cbox = 0
411        has_param = False
412        for param in param_list:
413            num_cbox += 1
414            if param in param_listB:
415                self.model_cbox.SetStringSelection(model_left)
416                self._on_select_model(None)
417                self.param_cbox.Clear()
418                self.param_cbox.Append( str(param), model)
419                self.param_cbox.SetStringSelection( str(param))
420                self.ctl2.SetValue(str(model_right + "." + str(param)))
421                has_param = True
422                if num_cbox == (len(param_list) + 1):
423                    break
424                self._show_constraint()
425       
426        self.sizer_constraints.Layout()
427        self.sizer2.Layout()
428        self.SetScrollbars(20,20,25,65)
429        self.Layout()
430        if not has_param:
431            msg= " There is no adjustable parameter (checked to fit)"
432            msg += " either one of the models."
433            wx.PostEvent(self.parent.parent, StatusEvent(info="warning", 
434                                                         status= msg ))
435        else:
436            msg= " The constraints are added."
437            wx.PostEvent(self.parent.parent, StatusEvent(info="info", 
438                                                         status= msg ))     
439    def _show_constraint(self):
440        """
441        Show constraint fields
442        """
443        self.btAdd.Show(True)
444        if len(self.constraints_list)!= 0:
445            nb_fit_param = 0
446            for id, model in self.constraint_dict.iteritems():
447                nb_fit_param += len(self.page_finder[id].get_param2fit())
448            ##Don't add anymore
449            if len(self.constraints_list) == nb_fit_param:
450                msg= "Cannot add another constraint .Maximum of number "
451                msg += "Parameters name reached %s"%str(nb_fit_param)
452                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
453                self.sizer_constraints.Layout()
454                self.sizer2.Layout()
455                #self.SetScrollbars(20,20,25,65)
456                return
457        if len(self.model_toFit) < 1 :
458            msg= "Select at least 1 model to add constraint "
459            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
460            self.sizer_constraints.Layout()
461            self.sizer2.Layout()
462            #self.SetScrollbars(20,20,25,65)
463            return
464           
465        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL)
466        model_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY)
467        model_cbox.Clear()
468        param_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY, size=(100,-1),)
469        param_cbox.Hide()
470       
471        #This is for GetCLientData() _on_select_param: Was None return on MAC.
472        self.param_cbox = param_cbox
473       
474        wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param)
475        self.ctl2 = wx.TextCtrl(self, -1)
476        egal_txt= wx.StaticText(self,-1, " = ")
477        self.btRemove = wx.Button(self,wx.NewId(),'Remove')
478        self.btRemove.Bind(wx.EVT_BUTTON, self.onRemove, 
479                                    id=self.btRemove.GetId())
480        self.btRemove.SetToolTipString("Remove constraint.")
481        self.btRemove.Hide()
482        if hasattr(self,"btAdd"):
483            self.btAdd.Hide()
484        for id, model in self.constraint_dict.iteritems():
485            ## check if all parameters have been selected for constraint
486            ## then do not allow add constraint on parameters
487            model_cbox.Append( str(model.name), model)
488           
489        #This is for GetCLientData() passing to self._on_select_param: Was None return on MAC.
490        self.model_cbox = model_cbox
491           
492        wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model)
493        sizer_constraint.Add((5,-1))
494        sizer_constraint.Add(model_cbox, flag= wx.RIGHT|wx.EXPAND,border=10)
495        sizer_constraint.Add(param_cbox, flag= wx.RIGHT|wx.EXPAND,border=5)
496        sizer_constraint.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND,border=5)
497        sizer_constraint.Add(self.ctl2, flag= wx.RIGHT|wx.EXPAND,border=10)
498        sizer_constraint.Add(self.btRemove, flag= wx.RIGHT|wx.EXPAND,border=10)
499     
500        self.sizer_constraints.Insert(before=self.nb_constraint,
501                        item=sizer_constraint, flag= wx.TOP|wx.BOTTOM|wx.EXPAND,
502                        border=5)
503        ##[combobox1, combobox2,=,textcrtl, remove button ]
504        self.constraints_list.append([model_cbox, param_cbox, egal_txt, 
505                                    self.ctl2,self.btRemove,sizer_constraint])
506   
507        self.nb_constraint += 1
508        self.sizer_constraints.Layout()
509        self.sizer2.Layout()
510        #self.SetScrollbars(20,20,25,65)
511       
512    def _hide_constraint(self): 
513        """
514        hide buttons related constraint
515        """ 
516        for id in  self.page_finder.iterkeys():
517            self.page_finder[id].clear_model_param()
518               
519        self.nb_constraint = 0     
520        self.constraint_dict = {}
521        if hasattr(self,"btAdd"):
522            self.btAdd.Hide()
523        self._store_model()
524        self.constraints_list = []   
525        self.sizer_all_constraints.Clear(True) 
526        self.sizer_all_constraints.Layout()         
527        self.sizer_constraints.Clear(True) 
528        self.sizer_constraints.Layout()   
529        self.sizer2.Layout()
530        #self.SetScrollbars(20,20,25,65)
531        #self.AdjustScrollbars()   
532           
533    def _on_select_model(self, event):
534        """
535        fill combox box with list of parameters
536        """
537        ##This way PC/MAC both work, instead of using event.GetClientData().
538        n = self.model_cbox.GetCurrentSelection()
539        model = self.model_cbox.GetClientData(n)
540        for id, dic_model in self.constraint_dict.iteritems():
541            if model == dic_model:
542                param_list = self.page_finder[id].get_param2fit()
543                break
544        #param_list= get_fittableParam(model)
545        length = len(self.constraints_list)
546        if length < 1:
547            return 
548        param_cbox = self.constraints_list[length-1][1]
549        param_cbox.Clear()
550        ## insert only fittable paramaters
551        for param in param_list:
552            param_cbox.Append( str(param), model)
553           
554        param_cbox.Show(True)
555        self.btRemove.Show(True)
556        self.btAdd.Show(True)
557        self.sizer2.Layout()
558        #self.SetScrollbars(20,20,25,65)
559       
560    def _on_select_param(self, event):
561        """
562        Store the appropriate constraint in the page_finder
563        """
564        ##This way PC/MAC both work, instead of using event.GetClientData().
565        n = self.param_cbox.GetCurrentSelection()
566        model = self.param_cbox.GetClientData(n)
567        param = event.GetString()
568     
569        length = len(self.constraints_list)
570        if length < 1:
571            return 
572        egal_txt = self.constraints_list[length-1][2]
573        egal_txt.Show(True)       
574       
575        ctl2 = self.constraints_list[length-1][3]
576        ctl2.Show(True)
577        #self.sizer2.Layout()
578        #self.SetScrollbars(20,20,25,65)
579       
580    def _onAdd_constraint(self, event): 
581        """
582        Add another line for constraint
583        """
584        if not self.show_constraint.GetValue():
585            msg= " Select Yes to add Constraint "
586            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
587            return 
588        ## check that a constraint is added
589        # before allow to add another constraint
590        for item in self.constraints_list:
591            model_cbox = item[0]
592            if model_cbox.GetString(0)=="":
593                msg= " Select a model Name! "
594                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
595                return 
596            param_cbox = item[1]
597            if param_cbox.GetString(0)=="":
598                msg= " Select a parameter Name! "
599                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
600                return 
601            ctl2 = item[3]
602            if ctl2.GetValue().lstrip().rstrip()=="":
603                model= param_cbox.GetClientData(param_cbox.GetCurrentSelection())
604                msg= " Enter a constraint for %s.%s! "%(model.name, 
605                                                        param_cbox.GetString(0))           
606                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
607                return 
608        ## some model or parameters can be constrained
609        self._show_constraint()
610       
611    def _fill_sizer_fit(self):
612        """
613        Draw fit button
614        """
615        self.sizer3.Clear(True)
616        box_description= wx.StaticBox(self, -1,"Fit ")
617        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
618        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
619         
620        self.btFit = wx.Button(self,wx.NewId(),'Fit')
621        self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId())
622        self.btFit.SetToolTipString("Perform fit.")
623        text  = "     Note: Park fitting engine will be used automatically. \n"
624        text += "     This page requires at least one FitPage with a data \n"
625        text += "       and a model set for fitting."
626        #text+= "automatically for more than 2 combinations checked"
627        text_hint = wx.StaticText(self,-1,text)
628       
629        sizer_button.Add(text_hint,  wx.RIGHT|wx.EXPAND, 10)
630        sizer_button.Add(self.btFit, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
631       
632        boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM,border=10)
633        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
634        self.sizer3.Layout()
635        #self.SetScrollbars(20,20,25,65)
636       
637    def _fill_sizer_constraint(self):
638        """
639        Fill sizer containing constraint info
640        """
641        msg = "Select at least 2 model to add constraint "
642        wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
643       
644        self.sizer2.Clear(True)
645        box_description= wx.StaticBox(self, -1,"Fit Constraints")
646        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
647        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
648        self.sizer_all_constraints = wx.BoxSizer(wx.HORIZONTAL)
649        self.sizer_constraints = wx.BoxSizer(wx.VERTICAL)
650        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
651       
652        self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), 
653                                              style=wx.RB_GROUP)
654        self.show_constraint = wx.RadioButton(self, -1, 'Yes', (10, 30))
655        self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint,
656                    id= self.hide_constraint.GetId() )
657        self.Bind(  wx.EVT_RADIOBUTTON, self._display_constraint,
658                         id= self.show_constraint.GetId()    )
659        self.hide_constraint.SetValue(True)
660        sizer_title.Add( wx.StaticText(self,-1," Model") )
661        sizer_title.Add(( 10,10) )
662        sizer_title.Add( wx.StaticText(self,-1," Parameter") )
663        sizer_title.Add(( 10,10) )
664        sizer_title.Add( wx.StaticText(self,-1," Add Constraint?") )
665        sizer_title.Add(( 10,10) )
666        sizer_title.Add( self.show_constraint )
667        sizer_title.Add( self.hide_constraint )
668        sizer_title.Add(( 10,10) )
669       
670        self.btAdd =wx.Button(self,wx.NewId(),'Add')
671        self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint, 
672                        id=self.btAdd.GetId())
673        self.btAdd.SetToolTipString("Add another constraint?")
674        self.btAdd.Hide()
675     
676        text_hint = wx.StaticText(self, -1, 
677                                  "Example: [M0][paramter] = M1.parameter") 
678        sizer_button.Add(text_hint, 0 , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
679        sizer_button.Add(self.btAdd, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
680       
681        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=10)
682        boxsizer1.Add(self.sizer_all_constraints, flag= wx.TOP|wx.BOTTOM, 
683                      border=10)
684        boxsizer1.Add(self.sizer_constraints, flag= wx.TOP|wx.BOTTOM, 
685                      border=10)
686        boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM, border=10)
687       
688        self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
689        self.sizer2.Layout()
690        #self.SetScrollbars(20,20,25,65)
691   
692    def _set_constraint(self):
693        """
694        get values from the constrainst textcrtl ,parses them into model name
695        parameter name and parameters values.
696        store them in a list self.params .when when params is not empty
697        set_model uses it to reset the appropriate model
698        and its appropriates parameters
699        """
700        for item in self.constraints_list:
701            model = item[0].GetClientData(item[0].GetCurrentSelection())
702            param = item[1].GetString(item[1].GetCurrentSelection())
703            constraint = item[3].GetValue().lstrip().rstrip()
704            if param.lstrip().rstrip()=="":
705                param= None
706                msg= " Constraint will be ignored!. missing parameters"
707                msg+= " in combobox to set constraint! "
708                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
709            for id, value in self.constraint_dict.iteritems():
710                if model == value:
711                    if constraint == "":
712                        msg= " Constraint will be ignored!. missing value"
713                        msg+= " in textcrtl to set constraint! "
714                        wx.PostEvent(self.parent.parent, 
715                                     StatusEvent(status= msg ))
716                        constraint = None
717                    if str(param) in self.page_finder[id].get_param2fit():
718                        msg = " Checking constraint for parameter: %s ", param
719                        wx.PostEvent(self.parent.parent, 
720                                    StatusEvent(info="info", status= msg ))
721                    else:
722                        model_name = item[0].GetLabel()
723                        fitpage = self.page_finder[id].get_fit_tab_caption()
724                        msg = "All constrainted parameters must be set "
725                        msg += " adjustable: '%s.%s' "% (model_name, param)
726                        msg += "is NOT checked in '%s'. "% fitpage
727                        msg += " Please check it to fit or"
728                        msg += " remove the line of the constraint."
729                        wx.PostEvent(self.parent.parent, 
730                                StatusEvent(info="error", status= msg ))
731                        return False
732                       
733                    for fid in self.page_finder[id].iterkeys():
734                        self.page_finder[id].set_model_param(param,
735                                                        constraint, fid=fid)
736                    break
737        return True
738   
739    def _fill_sizer_model_list(self,sizer):
740        """
741        Receive a dictionary containing information to display model name
742       
743        :param page_finder: the dictionary containing models information
744       
745        """
746        ix = 0
747        iy = 0
748        list=[]
749        sizer.Clear(True)
750       
751        new_name = wx.StaticText(self, -1, 'New Model Name', 
752                                 style=wx.ALIGN_CENTER)
753        new_name.SetBackgroundColour('orange')
754        sizer.Add(new_name,(iy, ix),(1,1),
755                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
756        ix += 2 
757        model_type = wx.StaticText(self, -1, '  Model Type')
758        model_type.SetBackgroundColour('grey')
759        sizer.Add(model_type,(iy, ix),(1,1),
760                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
761        ix += 1 
762        data_used = wx.StaticText(self, -1, '  Used Data')
763        data_used.SetBackgroundColour('grey')
764        sizer.Add(data_used,(iy, ix),(1,1),
765                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
766        ix += 1 
767        tab_used = wx.StaticText(self, -1, '  Fit Tab')
768        tab_used.SetBackgroundColour('grey')
769        sizer.Add(tab_used,(iy, ix),(1,1),
770                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
771        for id, value in self.page_finder.iteritems():
772            try:
773                ix = 0
774                for fitproblem in value.get_fit_problem():
775                    if  not self.parent.get_page_by_id(id).batch_on:
776                        iy += 1 
777                        data = fitproblem.get_fit_data()
778                        model = fitproblem.get_model()
779                        name = '_'
780                        if model is not None:
781                            name = str(model.name)
782                        cb = wx.CheckBox(self, -1, name)
783                        cb.SetValue(False)
784                        cb.Enable(model is not None and data.is_data)
785                        sizer.Add(cb, (iy, ix), (1, 1), 
786                                   wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
787                        wx.EVT_CHECKBOX(self, cb.GetId(), self.check_model_name)
788                        ix += 2 
789                        type = model.__class__.__name__
790                        model_type = wx.StaticText(self, -1, str(type))
791                        sizer.Add(model_type, (iy, ix), (1, 1), 
792                                  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
793                        name = '-'
794                        if data is not None and data.is_data:
795                            name = str(data.name)
796                        data_used = wx.StaticText(self, -1, name)
797                        ix += 1 
798                        sizer.Add(data_used, (iy, ix), (1, 1), 
799                                  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
800                        ix += 1 
801                        caption = value.get_fit_tab_caption()
802                        tab_caption_used= wx.StaticText(self, -1, str(caption))
803                        sizer.Add(tab_caption_used, (iy, ix), (1, 1), 
804                                  wx.EXPAND|wx.ADJUST_MINSIZE, 0)
805                   
806                        self.model_list.append([cb,value,id,model])
807               
808            except:
809                raise
810                #pass
811        iy += 1
812        sizer.Add((20, 20), (iy, ix), (1, 1), 
813                  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
814        sizer.Layout()   
815   
816
817    def on_set_focus(self, event=None):
818        """
819        The  derivative class is on focus if implemented
820        """
821        if self.parent is not None:
822            if self.parent.parent is not None:
823                wx.PostEvent(self.parent.parent, PanelOnFocusEvent(panel=self))
824            self.page_finder = self.parent._manager.get_page_finder()
825       
Note: See TracBrowser for help on using the repository browser.