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

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

fix constraint Remove button. Fixes #468. Refs #448.

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