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

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249unittest-saveload
Last change on this file since a20a255 was a20a255, checked in by krzywon, 6 years ago

Add a help button to the slicer parameters panel.

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