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

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 f32d144 was f32d144, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Pep-8-ification

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