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

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 bf5e985 was bf5e985, checked in by pkienzle, 10 years ago

restore simultaneous fitting for bumps

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