Changeset 3f75203 in sasview for src


Ignore:
Timestamp:
Apr 10, 2017 5:43:20 AM (7 years ago)
Author:
krzywon
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
37d461c
Parents:
82ce1d0
git-author:
Jeff Krzywon <krzywon@…> (04/10/17 05:43:20)
git-committer:
krzywon <krzywon@…> (04/10/17 05:43:20)
Message:

Update append value when parameters change and code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/guiframe/local_perspectives/plotting/parameters_panel_slicer.py

    r82ce1d0 r3f75203  
    11 
    22 
     3import os 
    34import wx 
    45import wx.lib.newevent 
     
    78from sas.sasgui.guiframe.utils import format_number 
    89from sas.sasgui.guiframe.events import EVT_SLICER 
    9 from sas.sasgui.guiframe.events import SlicerParameterEvent, SlicerEvent 
     10from sas.sasgui.guiframe.events import SlicerParameterEvent 
    1011from Plotter2D import ModelPanel2D 
    1112apply_params, EVT_APPLY_PARAMS = wx.lib.newevent.NewEvent() 
    12 auto_save, EVT_AUTO_SAVE = wx.lib.newevent.NewEvent() 
     13save_files, EVT_AUTO_SAVE = wx.lib.newevent.NewEvent() 
    1314 
    1415FIT_OPTIONS = ["No fitting", "Fitting", "Batch Fitting"] 
     
    1819                "BoxInteractorY": "SlabY"} 
    1920 
     21 
    2022class SlicerParameterPanel(wx.Dialog): 
    2123    """ 
    22     Panel class to show the slicer parameters 
     24    Panel for dynamically changing slicer parameters and apply the same slicer 
     25    to multiple 2D plot panels 
    2326    """ 
    24     # TODO: show units 
    25     # TODO: order parameters properly 
    2627 
    2728    def __init__(self, parent, *args, **kwargs): 
     
    3435        self.iter = 0 
    3536        self.parent = parent 
     37        self.main_window = parent.parent 
     38        self.data_panel = self.main_window._data_panel 
    3639        self.type = None 
    3740        self.listeners = [] 
     
    4346        self.fitting_options = None 
    4447        self.type_list = [] 
     48        self.loaded_data = [] 
     49        self.always_on = None 
    4550        self.type_select = None 
    4651        self.append_name = None 
    4752        self.data_list = None 
     53        self.default_value = "" 
     54        self.batch_slicer_button = None 
    4855        label = "Right-click on 2D plot for slicer options" 
    4956        title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) 
     
    5158                     flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) 
    5259        # Bindings 
    53         self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) 
    54         self.Bind(EVT_SLICER_PARS, self.onParamChange) 
     60        self.parent.Bind(EVT_SLICER, self.on_evt_slicer) 
     61        self.Bind(EVT_SLICER_PARS, self.on_param_change) 
    5562        self.Bind(EVT_APPLY_PARAMS, self.apply_params_list_and_process) 
    5663        self.Bind(EVT_AUTO_SAVE, self.save_files) 
    5764 
    58     def onEVT_SLICER(self, event): 
     65    def on_evt_slicer(self, event): 
    5966        """ 
    6067        Process EVT_SLICER events 
     
    94101                iy += 1 
    95102                ix = 0 
    96                 if not item in ["count", "errors"]: 
     103                if item not in ["count", "errors"]: 
    97104                    text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) 
    98105                    self.bck.Add(text, (iy, ix), (1, 1), 
     
    105112                    ix = 1 
    106113                    ctl.SetValue(format_number(str(params[item]))) 
    107                     self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) 
     114                    self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter) 
    108115                    self.parameters.append([item, ctl]) 
    109116                    self.bck.Add(ctl, (iy, ix), (1, 1), 
     
    131138            self.bck.Add(text, (iy, ix), (1, 1), 
    132139                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 
    133             self.type_list = ["SectorInteractor", "AnnulusInteractor", 
    134                               "BoxInteractorX", "BoxInteractorY"] 
     140            self.type_list = CONVERT_DICT.keys() 
    135141            self.type_select = wx.ComboBox(parent=self, choices=self.type_list) 
    136             self.type_select.Bind(wx.EVT_COMBOBOX, self.onChangeSlicer) 
     142            self.type_select.Bind(wx.EVT_COMBOBOX, self.on_change_slicer) 
    137143            index = self.type_select.FindString(type) 
    138144            self.type_select.SetSelection(index) 
     
    158164                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 
    159165 
    160             # Checkbox for autosaving data 
     166            # Checkbox to enable saving and fitting options 
    161167            iy += 1 
    162168            self.auto_save = wx.CheckBox(parent=self, id=wx.NewId(), 
     
    169175            save_to = "Save files to:" 
    170176            save = wx.StaticText(self, -1, save_to, style=wx.ALIGN_LEFT) 
    171             self.path = wx.DirPickerCtrl(self, id=wx.NewId(), path="", 
     177            path = os.getcwd() 
     178            self.path = wx.DirPickerCtrl(self, id=wx.NewId(), path=path, 
    172179                                         message=save_to) 
    173180            self.path.Enable(False) 
     
    178185            # Append to file 
    179186            iy += 1 
    180             default_value = "" 
    181             for key in params: 
    182                 default_value += "_{0}".format(key).split(" [")[0] 
    183                 default_value += "-{:.5f}".format(params[key]) 
     187            self.update_file_append(params) 
    184188            append_text = "Append to file name:" 
    185189            append = wx.StaticText(self, -1, append_text, style=wx.ALIGN_LEFT) 
    186190            self.append_name = wx.TextCtrl(parent=self, id=wx.NewId(), 
    187191                                           name="Append to file name:") 
    188             self.append_name.SetValue(default_value) 
     192            append_tool_tip = "Files will be saved as <SlicerType><FileName>" 
     193            append_tool_tip += "<AppendToText>.xml" 
     194            self.append_name.SetToolTipString(append_tool_tip) 
     195            self.append_name.SetValue(self.default_value) 
    189196            self.append_name.Enable(False) 
    190197            self.bck.Add(append, (iy, ix), (1, 1), 
     
    196203            iy += 1 
    197204            fit_text = "Fitting Options:" 
    198             fit_text_item = wx.StaticText(self, -1, fit_text, style=wx.ALIGN_LEFT) 
     205            fit_text_item = wx.StaticText(self, -1, fit_text, 
     206                                          style=wx.ALIGN_LEFT) 
    199207            self.bck.Add(fit_text_item, (iy, ix), (1, 1), 
    200208                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 
     
    221229        self.parent.GetSizer().Layout() 
    222230 
    223     def onParamChange(self, evt): 
     231    def on_param_change(self, evt): 
    224232        """ 
    225233        receive an event end reset value text fields 
     
    233241                    item[1].Refresh() 
    234242 
    235     def onTextEnter(self, evt): 
     243    def on_text_enter(self, evt): 
    236244        """ 
    237245        Parameters have changed 
     
    253261            # Post parameter event 
    254262            # parent here is plotter2D 
     263            self.update_file_append(params) 
     264            self.append_name.SetValue(self.default_value) 
     265            self.append_name.Refresh() 
    255266            event = SlicerParameterEvent(type=self.type, params=params) 
    256267            wx.PostEvent(self.parent, event) 
     
    258269    def on_batch_slicer(self, evt=None): 
    259270        """ 
    260         Method invoked when batch slicing button is pressed 
    261         :param evt: Event triggering hide/show of the batch slicer parameters 
     271        Event triggered when batch slicing button is pressed 
     272        :param evt: Event triggering the batch slicing 
    262273        """ 
    263274        apply_to_list = [] 
    264275        spp = self.parent.parent 
    265276        params = self.parent.slicer.get_params() 
    266         type = self.type_select.GetStringSelection() 
     277        slicer_type = self.type_select.GetStringSelection() 
    267278        save = self.auto_save.IsChecked() 
    268279        append = self.append_name.GetValue() 
     
    277288        # Apply slicer type to selected panels 
    278289        for item in apply_to_list: 
    279             self._apply_slicer_to_plot(item, type) 
     290            self._apply_slicer_to_plot(item, slicer_type) 
    280291 
    281292        # Post an event to apply appropriate slicer params to each slicer 
     293        # Pass all variables, including class variables 
    282294        event_params = apply_params(params=params, apply_to_list=apply_to_list, 
    283                              auto_save=save, append=append, fit=fit, 
    284                              path=path, type=type) 
     295                                    auto_save=save, append=append, fit=fit, 
     296                                    path=path, type=slicer_type) 
    285297        wx.PostEvent(self, event_params) 
    286298 
    287     def onChangeSlicer(self, evt): 
     299    def on_change_slicer(self, evt): 
    288300        """ 
    289301        Event driven slicer change when self.type_select changes 
     
    292304        self._apply_slicer_to_plot(self.parent) 
    293305 
    294     def _apply_slicer_to_plot(self, plot, type=None): 
     306    def _apply_slicer_to_plot(self, plot, slicer_type=None): 
    295307        """ 
    296308        Apply a slicer to *any* plot window, not just parent window 
    297309        :param plot: 2D plot panel to apply a slicer to 
    298         :param type: The type of slicer to apply to the panel 
    299         """ 
    300         # Skip redrawing the current plot if no change 
    301         if self.parent == plot and self.type == type: 
     310        :param slicer_type: The type of slicer to apply to the panel 
     311        """ 
     312        # Skip redrawing the current plot if no change in slicer type 
     313        if self.parent == plot and self.type == slicer_type: 
    302314            return 
    303315        # Do not draw a slicer on a 1D plot 
    304316        if not isinstance(plot, ModelPanel2D): 
    305317            return 
    306         if type is None: 
    307             type = self.type_select.GetStringSelection() 
    308         if type == "SectorInteractor": 
     318        if slicer_type is None: 
     319            slicer_type = self.type_select.GetStringSelection() 
     320        if slicer_type == self.type_list[0]: 
    309321            plot.onSectorQ(None) 
    310         elif type == "AnnulusInteractor": 
     322        elif slicer_type == self.type_list[1]: 
    311323            plot.onSectorPhi(None) 
    312         elif type == "BoxInteractorX": 
     324        elif slicer_type == self.type_list[2]: 
    313325            plot.onBoxavgX(None) 
    314         elif type == "BoxInteractorY": 
     326        elif slicer_type == self.type_list[3]: 
    315327            plot.onBoxavgY(None) 
    316328 
     
    319331        Populate the check list from the currently plotted 2D data 
    320332        """ 
    321         self.checkme = None 
    322         main_window = self.parent.parent 
    323         self.loaded_data = [] 
    324         id = wx.NewId() 
    325333        # Iterate over the loaded plots and find all 2D panels 
    326         for key, value in main_window.plot_panels.iteritems(): 
     334        for key, value in self.main_window.plot_panels.iteritems(): 
    327335            if isinstance(value, ModelPanel2D): 
    328336                self.loaded_data.append(value.data2D.name) 
    329337                if value.data2D.id == self.parent.data2D.id: 
    330338                    # Set current plot panel as uncheckable 
    331                     self.checkme = self.loaded_data.index(value.data2D.name) 
    332         self.data_list = wx.CheckListBox(parent=self, id=id, 
     339                    self.always_on = self.loaded_data.index(value.data2D.name) 
     340        self.data_list = wx.CheckListBox(parent=self, id=wx.NewId(), 
    333341                                         choices=self.loaded_data, 
    334342                                         name="Apply Slicer to 2D Plots:") 
     
    341349        """ 
    342350        Prevent a checkbox item from being unchecked 
    343         :param e: Event triggered when a checkbox list item is checked 
     351        :param evt: Event triggered when a checkbox list item is checked 
    344352        """ 
    345353        if evt is None: 
    346354            return 
    347355        index = evt.GetSelection() 
    348         if index == self.checkme: 
     356        if index == self.always_on: 
    349357            self.data_list.Check(index) 
    350358 
     
    355363                    evt should have attrs plot_list and params 
    356364        """ 
     365        if evt is None: 
     366            return 
    357367        # Apply parameter list to each plot as desired 
    358368        for item in evt.apply_to_list: 
     
    361371        # Post an event to save each data set to file 
    362372        if evt.auto_save: 
    363             event = auto_save(append_to_name=evt.append, path=evt.path, 
    364                               type=evt.type, file_list=evt.apply_to_list, 
    365                               fit=evt.fit) 
     373            event = save_files(append_to_name=evt.append, path=evt.path, 
     374                               type=evt.type, file_list=evt.apply_to_list, 
     375                               fit=evt.fit) 
    366376            wx.PostEvent(self, event) 
    367377 
     
    372382        """ 
    373383 
    374         # Send the event to the end of the wx event queue 
     384        # Events triggered after this event pass other events to wx that are 
     385        # necessary before this event is called. If this is the first time 
     386        # reaching this event, send it to the end of the wx event queue 
    375387        if self.iter == 0: 
    376388            clone = evt.Clone() 
     
    383395        # Start definitions 
    384396        writer = Reader() 
    385         main_window = self.parent.parent 
    386397        data_dic = {} 
    387398        append = evt.append_to_name 
     
    395406 
    396407        # Find the correct plots to save 
    397         for key, plot in main_window.plot_panels.iteritems(): 
     408        for key, plot in self.main_window.plot_panels.iteritems(): 
    398409            if not hasattr(plot, "data2D"): 
    399410                for item in plot.plots: 
     
    404415        # Save files as XML 
    405416        for item, data1d in data_dic.iteritems(): 
    406             base = ('.').join(item.split('.')[:-1]) 
     417            base = '.'.join(item.split('.')[:-1]) 
    407418            file_name = base + append + ".xml" 
    408419            save_to = evt.path + "\\" + file_name 
     
    413424        # Load files into GUI 
    414425        for item in f_path_list: 
    415             main_window.load_data(item) 
     426            self.main_window.load_data(item) 
    416427 
    417428        # Send to fitting 
     
    425436        """ 
    426437        if fit != FIT_OPTIONS[0] and file_list is not None: 
    427             # Method variable definitions 
    428             main_window = self.parent.parent 
    429             datapanel = main_window._data_panel 
    430438            # Set perspective to fitting 
    431             int = datapanel.perspective_cbox.FindString("Fitting") 
    432             datapanel.perspective_cbox.SetSelection(int) 
    433             datapanel._on_perspective_selection(None) 
     439            int = self.data_panel.perspective_cbox.FindString("Fitting") 
     440            self.data_panel.perspective_cbox.SetSelection(int) 
     441            self.data_panel._on_perspective_selection(None) 
    434442            # Unselect all loaded data 
    435             datapanel.selection_cbox.SetValue('Unselect all Data') 
    436             datapanel._on_selection_type(None) 
     443            self.data_panel.selection_cbox.SetValue('Unselect all Data') 
     444            self.data_panel._on_selection_type(None) 
    437445            # Click each sliced data file 
    438446            for f_name in file_list: 
    439447                num = len(f_name) 
    440                 data_list = datapanel.list_cb_data 
     448                data_list = self.data_panel.list_cb_data 
    441449                for key in data_list: 
    442450                    loaded_key = (key[:num]) if len(key) > num else key 
     
    446454                        self.check_item_and_children(data_ctrl=data_ctrl, 
    447455                                                     check_value=True) 
    448             # TODO: Batch fitting 
    449456            # Switch to batch mode if selected 
    450457            if fit == FIT_OPTIONS[2]: 
    451                 datapanel.rb_single_mode.SetValue(False) 
    452                 datapanel.rb_batch_mode.SetValue(True) 
    453                 datapanel.on_batch_mode(None) 
     458                self.data_panel.rb_single_mode.SetValue(False) 
     459                self.data_panel.rb_batch_mode.SetValue(True) 
     460                self.data_panel.on_batch_mode(None) 
    454461            else: 
    455                 datapanel.rb_single_mode.SetValue(True) 
    456                 datapanel.rb_batch_mode.SetValue(False) 
    457                 datapanel.on_single_mode(None) 
     462                self.data_panel.rb_single_mode.SetValue(True) 
     463                self.data_panel.rb_batch_mode.SetValue(False) 
     464                self.data_panel.on_single_mode(None) 
    458465 
    459466            # Post button click event to send data to fitting 
    460467            evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, 
    461                                     datapanel.bt_import.GetId()) 
    462             wx.PostEvent(datapanel, evt) 
     468                                    self.data_panel.bt_import.GetId()) 
     469            wx.PostEvent(self.data_panel, evt) 
    463470 
    464471    def on_auto_save_checked(self, evt=None): 
     
    472479 
    473480    def check_item_and_children(self, data_ctrl, check_value=True): 
    474         self.parent.parent._data_panel.tree_ctrl.CheckItem(data_ctrl, 
    475                                                            check_value) 
     481        self.data_panel.tree_ctrl.CheckItem(data_ctrl, check_value) 
    476482        if data_ctrl.HasChildren(): 
    477483            if check_value and not data_ctrl.IsExpanded(): 
    478                 # Only select children if control is expanded 
    479                 # Always deselect children, regardless (see ticket #259) 
    480484                return 
    481485            for child_ctrl in data_ctrl.GetChildren(): 
    482                 self.tree_ctrl.CheckItem(child_ctrl, check_value) 
     486                self.data_panel.CheckItem(child_ctrl, check_value) 
     487 
     488    def update_file_append(self, params=None): 
     489        """ 
     490        Update default_value when any parameters are changed 
     491        :param params: dictionary of parameters 
     492        """ 
     493        self.default_value = "" 
     494        if params is None: 
     495            params = self.params 
     496        for key in params: 
     497            self.default_value += "_{0}".format(key).split(" [")[0] 
     498            self.default_value += "-{:.2f}".format(params[key]) 
Note: See TracChangeset for help on using the changeset viewer.