source: sasview/src/sas/perspectives/fitting/simfitpage.py @ 2d88fc4

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 2d88fc4 was acf8e4a5, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

reference BumpsFit? directly and remove fit engine selection layer

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