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

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

Moving things around. Will definitely not build.

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