source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/parameters_panel_slicer.py @ 6267876

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6267876 was 3f75203, checked in by krzywon, 7 years ago

Update append value when parameters change and code cleanup.

  • Property mode set to 100644
File size: 20.5 KB
Line 
1
2
3import os
4import wx
5import wx.lib.newevent
6from sas.sascalc.dataloader.readers.cansas_reader import Reader
7from sas.sasgui.guiframe.events import EVT_SLICER_PARS
8from sas.sasgui.guiframe.utils import format_number
9from sas.sasgui.guiframe.events import EVT_SLICER
10from sas.sasgui.guiframe.events import SlicerParameterEvent
11from Plotter2D import ModelPanel2D
12apply_params, EVT_APPLY_PARAMS = wx.lib.newevent.NewEvent()
13save_files, EVT_AUTO_SAVE = wx.lib.newevent.NewEvent()
14
15FIT_OPTIONS = ["No fitting", "Fitting", "Batch Fitting"]
16CONVERT_DICT = {"SectorInteractor": "SectorQ",
17                "AnnulusInteractor": "AnnulusPhi",
18                "BoxInteractorX": "SlabX",
19                "BoxInteractorY": "SlabY"}
20
21
22class SlicerParameterPanel(wx.Dialog):
23    """
24    Panel for dynamically changing slicer parameters and apply the same slicer
25    to multiple 2D plot panels
26    """
27
28    def __init__(self, parent, *args, **kwargs):
29        """
30        Dialog window that allow to edit parameters slicer
31        by entering new values
32        """
33        wx.Dialog.__init__(self, parent, *args, **kwargs)
34        self.params = {}
35        self.iter = 0
36        self.parent = parent
37        self.main_window = parent.parent
38        self.data_panel = self.main_window._data_panel
39        self.type = None
40        self.listeners = []
41        self.parameters = []
42        self.bck = wx.GridBagSizer(5, 5)
43        self.SetSizer(self.bck)
44        self.auto_save = None
45        self.path = None
46        self.fitting_options = None
47        self.type_list = []
48        self.loaded_data = []
49        self.always_on = None
50        self.type_select = None
51        self.append_name = None
52        self.data_list = None
53        self.default_value = ""
54        self.batch_slicer_button = None
55        label = "Right-click on 2D plot for slicer options"
56        title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
57        self.bck.Add(title, (0, 0), (1, 2),
58                     flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15)
59        # Bindings
60        self.parent.Bind(EVT_SLICER, self.on_evt_slicer)
61        self.Bind(EVT_SLICER_PARS, self.on_param_change)
62        self.Bind(EVT_APPLY_PARAMS, self.apply_params_list_and_process)
63        self.Bind(EVT_AUTO_SAVE, self.save_files)
64
65    def on_evt_slicer(self, event):
66        """
67        Process EVT_SLICER events
68        When the slicer changes, update the panel
69
70        :param event: EVT_SLICER event
71        """
72        event.Skip()
73        if event.obj_class is None:
74            self.set_slicer(None, None)
75        else:
76            self.set_slicer(event.type, event.params)
77
78    def set_slicer(self, type, params):
79        """
80        Rebuild the panel
81        """
82        self.bck.Clear(True)
83        self.bck.Add((5, 5), (0, 0), (1, 1),
84                     wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
85        self.type = type
86        if type is None:
87            label = "Right-click on 2D plot for slicer options"
88            title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT)
89            self.bck.Add(title, (1, 0), (1, 2),
90                         flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15)
91        else:
92            title = wx.StaticText(self, -1,
93                                  "Slicer Parameters:", style=wx.ALIGN_LEFT)
94            self.bck.Add(title, (1, 0), (1, 2),
95                         flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15)
96            iy = 1
97            self.parameters = []
98            keys = params.keys()
99            keys.sort()
100            for item in keys:
101                iy += 1
102                ix = 0
103                if item not in ["count", "errors"]:
104                    text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT)
105                    self.bck.Add(text, (iy, ix), (1, 1),
106                                 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
107                    ctl = wx.TextCtrl(self, -1, size=(80, 20),
108                                      style=wx.TE_PROCESS_ENTER)
109                    hint_msg = "Modify the value of %s to change" % item
110                    hint_msg += " the 2D slicer"
111                    ctl.SetToolTipString(hint_msg)
112                    ix = 1
113                    ctl.SetValue(format_number(str(params[item])))
114                    self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter)
115                    self.parameters.append([item, ctl])
116                    self.bck.Add(ctl, (iy, ix), (1, 1),
117                                 wx.EXPAND | wx.ADJUST_MINSIZE, 0)
118                    ix = 3
119                    self.bck.Add((20, 20), (iy, ix), (1, 1),
120                                 wx.EXPAND | wx.ADJUST_MINSIZE, 0)
121                else:
122                    text = wx.StaticText(self, -1, item + " : ",
123                                         style=wx.ALIGN_LEFT)
124                    self.bck.Add(text, (iy, ix), (1, 1),
125                                 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
126                    ctl = wx.StaticText(self, -1,
127                                        format_number(str(params[item])),
128                                        style=wx.ALIGN_LEFT)
129                    ix = 1
130                    self.bck.Add(ctl, (iy, ix), (1, 1),
131                                 wx.EXPAND | wx.ADJUST_MINSIZE, 0)
132
133            # Change slicer within the window
134            ix = 0
135            iy += 1
136            txt = "Slicer type:"
137            text = wx.StaticText(self, -1, txt, style=wx.ALIGN_LEFT)
138            self.bck.Add(text, (iy, ix), (1, 1),
139                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
140            self.type_list = CONVERT_DICT.keys()
141            self.type_select = wx.ComboBox(parent=self, choices=self.type_list)
142            self.type_select.Bind(wx.EVT_COMBOBOX, self.on_change_slicer)
143            index = self.type_select.FindString(type)
144            self.type_select.SetSelection(index)
145            self.bck.Add(self.type_select, (iy, 1), (1, 1),
146                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
147
148            # batch slicing parameters
149            title_text = "Batch Slicing Options:"
150            title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT)
151            iy += 1
152            line = wx.StaticLine(self, -1, style=wx.LI_VERTICAL)
153            line.SetSize((60, 60))
154            self.bck.Add(line, (iy, ix), (1, 2),
155                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
156            iy += 1
157            self.bck.Add(title, (iy, ix), (1, 1),
158                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
159
160            # Create a list box with all of the 2D plots
161            iy += 1
162            self.process_list()
163            self.bck.Add(self.data_list, (iy, ix), (1, 1),
164                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
165
166            # Checkbox to enable saving and fitting options
167            iy += 1
168            self.auto_save = wx.CheckBox(parent=self, id=wx.NewId(),
169                                         label="Auto save generated 1D:")
170            self.Bind(wx.EVT_CHECKBOX, self.on_auto_save_checked)
171            self.bck.Add(self.auto_save, (iy, ix), (1, 1),
172                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
173            iy += 1
174            # File browser
175            save_to = "Save files to:"
176            save = wx.StaticText(self, -1, save_to, style=wx.ALIGN_LEFT)
177            path = os.getcwd()
178            self.path = wx.DirPickerCtrl(self, id=wx.NewId(), path=path,
179                                         message=save_to)
180            self.path.Enable(False)
181            self.bck.Add(save, (iy, ix), (1, 1),
182                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
183            self.bck.Add(self.path, (iy, 1), (1, 1),
184                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
185            # Append to file
186            iy += 1
187            self.update_file_append(params)
188            append_text = "Append to file name:"
189            append = wx.StaticText(self, -1, append_text, style=wx.ALIGN_LEFT)
190            self.append_name = wx.TextCtrl(parent=self, id=wx.NewId(),
191                                           name="Append to file name:")
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)
196            self.append_name.Enable(False)
197            self.bck.Add(append, (iy, ix), (1, 1),
198                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
199            self.bck.Add(self.append_name, (iy, 1), (1, 1),
200                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
201
202            # Combobox for selecting fitting options
203            iy += 1
204            fit_text = "Fitting Options:"
205            fit_text_item = wx.StaticText(self, -1, fit_text,
206                                          style=wx.ALIGN_LEFT)
207            self.bck.Add(fit_text_item, (iy, ix), (1, 1),
208                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
209            self.fitting_options = wx.ComboBox(parent=self, choices=FIT_OPTIONS)
210            self.fitting_options.SetSelection(0)
211            self.bck.Add(self.fitting_options, (iy, 1), (1, 1),
212                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
213            self.fitting_options.Enable(False)
214            self.fitting_options.Bind(wx.EVT_COMBOBOX, None)
215
216            # Button to start batch slicing
217            iy += 1
218            button_label = "Apply Slicer to Selected Plots"
219            self.batch_slicer_button = wx.Button(parent=self,
220                                                 label=button_label)
221            self.Bind(wx.EVT_BUTTON, self.on_batch_slicer)
222            self.bck.Add(self.batch_slicer_button, (iy, ix), (1, 1),
223                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
224            iy += 1
225            self.bck.Add((5, 5), (iy, ix), (1, 1),
226                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
227        self.bck.Layout()
228        self.bck.Fit(self)
229        self.parent.GetSizer().Layout()
230
231    def on_param_change(self, evt):
232        """
233        receive an event end reset value text fields
234        inside self.parameters
235        """
236        evt.Skip()
237        if evt.type == "UPDATE":
238            for item in self.parameters:
239                if item[0] in evt.params:
240                    item[1].SetValue("%-5.3g" % evt.params[item[0]])
241                    item[1].Refresh()
242
243    def on_text_enter(self, evt):
244        """
245        Parameters have changed
246        """
247        params = {}
248        has_error = False
249        for item in self.parameters:
250            try:
251                params[item[0]] = float(item[1].GetValue())
252                item[1].SetBackgroundColour(
253                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
254                item[1].Refresh()
255            except:
256                has_error = True
257                item[1].SetBackgroundColour("pink")
258                item[1].Refresh()
259
260        if not has_error:
261            # Post parameter event
262            # parent here is plotter2D
263            self.update_file_append(params)
264            self.append_name.SetValue(self.default_value)
265            self.append_name.Refresh()
266            event = SlicerParameterEvent(type=self.type, params=params)
267            wx.PostEvent(self.parent, event)
268
269    def on_batch_slicer(self, evt=None):
270        """
271        Event triggered when batch slicing button is pressed
272        :param evt: Event triggering the batch slicing
273        """
274        apply_to_list = []
275        spp = self.parent.parent
276        params = self.parent.slicer.get_params()
277        slicer_type = self.type_select.GetStringSelection()
278        save = self.auto_save.IsChecked()
279        append = self.append_name.GetValue()
280        path = self.path.GetPath()
281        fit = self.fitting_options.GetValue()
282
283        # Find desired 2D data panels
284        for key, mgr in spp.plot_panels.iteritems():
285            if mgr.graph.prop['title'] in self.data_list.CheckedStrings:
286                apply_to_list.append(mgr)
287
288        # Apply slicer type to selected panels
289        for item in apply_to_list:
290            self._apply_slicer_to_plot(item, slicer_type)
291
292        # Post an event to apply appropriate slicer params to each slicer
293        # Pass all variables, including class variables
294        event_params = apply_params(params=params, apply_to_list=apply_to_list,
295                                    auto_save=save, append=append, fit=fit,
296                                    path=path, type=slicer_type)
297        wx.PostEvent(self, event_params)
298
299    def on_change_slicer(self, evt):
300        """
301        Event driven slicer change when self.type_select changes
302        :param evt: Event triggering this change
303        """
304        self._apply_slicer_to_plot(self.parent)
305
306    def _apply_slicer_to_plot(self, plot, slicer_type=None):
307        """
308        Apply a slicer to *any* plot window, not just parent window
309        :param plot: 2D plot panel to apply a slicer to
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:
314            return
315        # Do not draw a slicer on a 1D plot
316        if not isinstance(plot, ModelPanel2D):
317            return
318        if slicer_type is None:
319            slicer_type = self.type_select.GetStringSelection()
320        if slicer_type == self.type_list[0]:
321            plot.onSectorQ(None)
322        elif slicer_type == self.type_list[1]:
323            plot.onSectorPhi(None)
324        elif slicer_type == self.type_list[2]:
325            plot.onBoxavgX(None)
326        elif slicer_type == self.type_list[3]:
327            plot.onBoxavgY(None)
328
329    def process_list(self):
330        """
331        Populate the check list from the currently plotted 2D data
332        """
333        # Iterate over the loaded plots and find all 2D panels
334        for key, value in self.main_window.plot_panels.iteritems():
335            if isinstance(value, ModelPanel2D):
336                self.loaded_data.append(value.data2D.name)
337                if value.data2D.id == self.parent.data2D.id:
338                    # Set current plot panel as uncheckable
339                    self.always_on = self.loaded_data.index(value.data2D.name)
340        self.data_list = wx.CheckListBox(parent=self, id=wx.NewId(),
341                                         choices=self.loaded_data,
342                                         name="Apply Slicer to 2D Plots:")
343        # Check all items by default
344        for item in range(len(self.data_list.Items)):
345            self.data_list.Check(item)
346        self.data_list.Bind(wx.EVT_CHECKLISTBOX, self.on_check_box_list)
347
348    def on_check_box_list(self, evt=None):
349        """
350        Prevent a checkbox item from being unchecked
351        :param evt: Event triggered when a checkbox list item is checked
352        """
353        if evt is None:
354            return
355        index = evt.GetSelection()
356        if index == self.always_on:
357            self.data_list.Check(index)
358
359    def apply_params_list_and_process(self, evt=None):
360        """
361        Event based parameter setting.
362        :param evt: Event triggered to apply parameters to a list of plots
363                    evt should have attrs plot_list and params
364        """
365        if evt is None:
366            return
367        # Apply parameter list to each plot as desired
368        for item in evt.apply_to_list:
369            event = SlicerParameterEvent(type=evt.type, params=evt.params)
370            wx.PostEvent(item, event)
371        # Post an event to save each data set to file
372        if evt.auto_save:
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)
376            wx.PostEvent(self, event)
377
378    def save_files(self, evt=None):
379        """
380        Automatically save the sliced data to file.
381        :param evt: Event that triggered the call to the method
382        """
383
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
387        if self.iter == 0:
388            clone = evt.Clone()
389            wx.PostEvent(self, clone)
390            self.iter += 1
391            return
392        if evt is None:
393            return
394
395        # Start definitions
396        writer = Reader()
397        data_dic = {}
398        append = evt.append_to_name
399        names = []
400        f_name_list = []
401        f_path_list = []
402
403        # Get list of 2D data names for saving
404        for f_name in evt.file_list:
405            names.append(f_name.data2D.label)
406
407        # Find the correct plots to save
408        for key, plot in self.main_window.plot_panels.iteritems():
409            if not hasattr(plot, "data2D"):
410                for item in plot.plots:
411                    base = item.replace(CONVERT_DICT[evt.type], "")
412                    if base in names:
413                        data_dic[item] = plot.plots[item]
414
415        # Save files as XML
416        for item, data1d in data_dic.iteritems():
417            base = '.'.join(item.split('.')[:-1])
418            file_name = base + append + ".xml"
419            save_to = evt.path + "\\" + file_name
420            writer.write(save_to, data1d)
421            f_path_list.append(save_to)
422            f_name_list.append(file_name)
423
424        # Load files into GUI
425        for item in f_path_list:
426            self.main_window.load_data(item)
427
428        # Send to fitting
429        self.send_to_fitting(evt.fit, f_name_list)
430
431    def send_to_fitting(self, fit=FIT_OPTIONS[0], file_list=None):
432        """
433        Send a list of data to the fitting perspective
434        :param fit: fit type desired
435        :param file_list: list of loaded file names to send to fit
436        """
437        if fit != FIT_OPTIONS[0] and file_list is not None:
438            # Set perspective to fitting
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)
442            # Unselect all loaded data
443            self.data_panel.selection_cbox.SetValue('Unselect all Data')
444            self.data_panel._on_selection_type(None)
445            # Click each sliced data file
446            for f_name in file_list:
447                num = len(f_name)
448                data_list = self.data_panel.list_cb_data
449                for key in data_list:
450                    loaded_key = (key[:num]) if len(key) > num else key
451                    if loaded_key == f_name:
452                        selection = key
453                        data_ctrl = data_list[selection][0]
454                        self.check_item_and_children(data_ctrl=data_ctrl,
455                                                     check_value=True)
456            # Switch to batch mode if selected
457            if fit == FIT_OPTIONS[2]:
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)
461            else:
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)
465
466            # Post button click event to send data to fitting
467            evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId,
468                                    self.data_panel.bt_import.GetId())
469            wx.PostEvent(self.data_panel, evt)
470
471    def on_auto_save_checked(self, evt=None):
472        """
473        Enable/Disable auto append when checkbox is checked
474        :param evt: Event
475        """
476        self.append_name.Enable(self.auto_save.IsChecked())
477        self.path.Enable(self.auto_save.IsChecked())
478        self.fitting_options.Enable(self.auto_save.IsChecked())
479
480    def check_item_and_children(self, data_ctrl, check_value=True):
481        self.data_panel.tree_ctrl.CheckItem(data_ctrl, check_value)
482        if data_ctrl.HasChildren():
483            if check_value and not data_ctrl.IsExpanded():
484                return
485            for child_ctrl in data_ctrl.GetChildren():
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 TracBrowser for help on using the repository browser.