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

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 47d93073 was 47d93073, checked in by krzywon, 7 years ago

Apply new parameters before savig data.

  • Property mode set to 100644
File size: 22.3 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            iy += 1
254            self.bck.Add((5, 5), (iy, ix), (1, 1),
255                         wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 5)
256        self.bck.Layout()
257        self.bck.Fit(self)
258        self.parent.GetSizer().Layout()
259
260    def on_param_change(self, evt):
261        """
262        receive an event end reset value text fields
263        inside self.parameters
264        """
265        evt.Skip()
266        if evt.type == "UPDATE":
267            for item in self.parameters:
268                if item[0] in evt.params:
269                    item[1].SetValue("%-5.3g" % evt.params[item[0]])
270                    item[1].Refresh()
271
272    def on_text_enter(self, evt):
273        """
274        Parameters have changed
275        """
276        params = {}
277        has_error = False
278        for item in self.parameters:
279            try:
280                if item[0] == "binning base":
281                    title = self.bin_ctl.GetValue()
282                    params["binning base"] = BINNING_OPTIONS.get(title)
283                    continue
284                params[item[0]] = float(item[1].GetValue())
285                item[1].SetBackgroundColour(
286                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
287                item[1].Refresh()
288            except:
289                has_error = True
290                item[1].SetBackgroundColour("pink")
291                item[1].Refresh()
292
293        if not has_error:
294            # Post parameter event
295            # parent here is plotter2D
296            self.update_file_append(params)
297            self.append_name.SetValue(self.default_value)
298            self.append_name.Refresh()
299            event = SlicerParameterEvent(type=self.type, params=params)
300            wx.PostEvent(self.parent, event)
301
302    def on_batch_slicer(self, evt=None):
303        """
304        Event triggered when batch slicing button is pressed
305        :param evt: Event triggering the batch slicing
306        """
307        apply_to_list = []
308        spp = self.parent.parent
309        params = self.parent.slicer.get_params()
310        slicer_type = self.type_select.GetStringSelection()
311        save = self.auto_save.IsChecked()
312        append = self.append_name.GetValue()
313        path = self.path.GetPath()
314        fit = self.fitting_options.GetValue()
315
316        # Find desired 2D data panels
317        for key, mgr in spp.plot_panels.iteritems():
318            if mgr.graph.prop['title'] in self.data_list.CheckedStrings:
319                apply_to_list.append(mgr)
320
321        # Apply slicer type to selected panels
322        for item in apply_to_list:
323            self._apply_slicer_to_plot(item, slicer_type)
324
325        # Post an event to apply appropriate slicer params to each slicer
326        # Pass all variables, including class variables
327        event_params = apply_params(params=params, apply_to_list=apply_to_list,
328                                    auto_save=save, append=append, fit=fit,
329                                    path=path, type=slicer_type)
330        wx.PostEvent(self, event_params)
331
332    def on_change_slicer(self, evt):
333        """
334        Event driven slicer change when self.type_select changes
335        :param evt: Event triggering this change
336        """
337        self._apply_slicer_to_plot(self.parent)
338
339    def _apply_slicer_to_plot(self, plot, slicer_type=None):
340        """
341        Apply a slicer to *any* plot window, not just parent window
342        :param plot: 2D plot panel to apply a slicer to
343        :param slicer_type: The type of slicer to apply to the panel
344        """
345        # Skip redrawing the current plot if no change in slicer type
346        if self.parent == plot and self.type == slicer_type:
347            return
348        # Do not draw a slicer on a 1D plot
349        if not isinstance(plot, ModelPanel2D):
350            return
351        if slicer_type is None:
352            slicer_type = self.type_select.GetStringSelection()
353        if slicer_type == self.type_list[0]:
354            plot.onSectorQ(None)
355        elif slicer_type == self.type_list[1]:
356            plot.onSectorPhi(None)
357        elif slicer_type == self.type_list[2]:
358            plot.onBoxavgX(None)
359        elif slicer_type == self.type_list[3]:
360            plot.onBoxavgY(None)
361
362    def process_list(self):
363        """
364        Populate the check list from the currently plotted 2D data
365        """
366        # Reinitialize loaded data list on redraw
367        self.loaded_data = []
368        # Iterate over the loaded plots and find all 2D panels
369        for key, value in self.main_window.plot_panels.iteritems():
370            if isinstance(value, ModelPanel2D):
371                self.loaded_data.append(value.data2D.name)
372                if value.data2D.id == self.parent.data2D.id:
373                    # Set current plot panel as uncheckable
374                    self.always_on = self.loaded_data.index(value.data2D.name)
375        self.data_list = wx.CheckListBox(parent=self, id=wx.NewId(),
376                                         choices=self.loaded_data,
377                                         name="Apply Slicer to 2D Plots:")
378        # Check all items by default
379        for item in range(len(self.data_list.Items)):
380            self.data_list.Check(item)
381        self.data_list.Bind(wx.EVT_CHECKLISTBOX, self.on_check_box_list)
382
383    def on_check_box_list(self, evt=None):
384        """
385        Prevent a checkbox item from being unchecked
386        :param evt: Event triggered when a checkbox list item is checked
387        """
388        if evt is None:
389            return
390        index = evt.GetSelection()
391        if index == self.always_on:
392            self.data_list.Check(index)
393
394    def apply_params_list_and_process(self, evt=None):
395        """
396        Event based parameter setting.
397        :param evt: Event triggered to apply parameters to a list of plots
398                    evt should have attrs plot_list and params
399        """
400        if evt is None:
401            return
402        # Apply parameter list to each plot as desired
403        for item in evt.apply_to_list:
404            event = SlicerParameterEvent(type=evt.type, params=evt.params)
405            wx.PostEvent(item, event)
406        # Post an event to save each data set to file
407        if evt.auto_save:
408            event = save_files(append_to_name=evt.append, path=evt.path,
409                               type=evt.type, file_list=evt.apply_to_list,
410                               fit=evt.fit)
411            wx.PostEvent(self, event)
412
413    def save_files(self, evt=None):
414        """
415        Automatically save the sliced data to file.
416        :param evt: Event that triggered the call to the method
417        """
418
419        # Events triggered after this event pass other events to wx that are
420        # necessary before this event is called. If this is the first time
421        # reaching this event, send it to the end of the wx event queue
422        if self.iter < 2:
423            clone = evt.Clone()
424            wx.PostEvent(self, clone)
425            self.iter += 1
426            return
427        if evt is None:
428            return
429
430        # Start definitions
431        writer = Reader()
432        data_dic = {}
433        append = evt.append_to_name
434        names = []
435        f_name_list = []
436        f_path_list = []
437
438        # Get list of 2D data names for saving
439        for f_name in evt.file_list:
440            names.append(f_name.data2D.label)
441
442        # Find the correct plots to save
443        for key, plot in self.main_window.plot_panels.iteritems():
444            if not hasattr(plot, "data2D"):
445                for item in plot.plots:
446                    base = item.replace(CONVERT_DICT[evt.type], "")
447                    if base in names:
448                        data_dic[item] = plot.plots[item]
449
450        # Save files as Text
451        for item, data1d in data_dic.iteritems():
452            base = '.'.join(item.split('.')[:-1])
453            file_name = base + append + ".txt"
454            save_to = evt.path + "\\" + file_name
455            writer.write(save_to, data1d)
456            f_path_list.append(save_to)
457            f_name_list.append(file_name)
458
459        # Load files into GUI
460        for item in f_path_list:
461            self.main_window.load_data(item)
462
463        # Send to fitting
464        self.send_to_fitting(evt.fit, f_name_list)
465
466    def send_to_fitting(self, fit=FIT_OPTIONS[0], file_list=None):
467        """
468        Send a list of data to the fitting perspective
469        :param fit: fit type desired
470        :param file_list: list of loaded file names to send to fit
471        """
472        if fit in FIT_OPTIONS and fit != FIT_OPTIONS[0] and \
473                        file_list is not None:
474            # Set perspective to fitting
475            int = self.data_panel.perspective_cbox.FindString("Fitting")
476            self.data_panel.perspective_cbox.SetSelection(int)
477            self.data_panel._on_perspective_selection(None)
478            # Unselect all loaded data
479            self.data_panel.selection_cbox.SetValue('Unselect all Data')
480            self.data_panel._on_selection_type(None)
481            # Click each sliced data file
482            for f_name in file_list:
483                num = len(f_name)
484                data_list = self.data_panel.list_cb_data
485                for key in data_list:
486                    loaded_key = (key[:num]) if len(key) > num else key
487                    if loaded_key == f_name:
488                        selection = key
489                        data_ctrl = data_list[selection][0]
490                        self.check_item_and_children(data_ctrl=data_ctrl,
491                                                     check_value=True)
492            # Switch to batch mode if selected
493            if fit == FIT_OPTIONS[2]:
494                self.data_panel.rb_single_mode.SetValue(False)
495                self.data_panel.rb_batch_mode.SetValue(True)
496                self.data_panel.on_batch_mode(None)
497            else:
498                self.data_panel.rb_single_mode.SetValue(True)
499                self.data_panel.rb_batch_mode.SetValue(False)
500                self.data_panel.on_single_mode(None)
501
502            # Post button click event to send data to fitting
503            evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId,
504                                    self.data_panel.bt_import.GetId())
505            wx.PostEvent(self.data_panel, evt)
506
507    def on_auto_save_checked(self, evt=None):
508        """
509        Enable/Disable auto append when checkbox is checked
510        :param evt: Event
511        """
512        self.append_name.Enable(self.auto_save.IsChecked())
513        self.path.Enable(self.auto_save.IsChecked())
514        self.fitting_options.Enable(self.auto_save.IsChecked())
515
516    def check_item_and_children(self, data_ctrl, check_value=True):
517        self.data_panel.tree_ctrl.CheckItem(data_ctrl, check_value)
518        if data_ctrl.HasChildren():
519            if check_value and not data_ctrl.IsExpanded():
520                return
521            for child_ctrl in data_ctrl.GetChildren():
522                self.data_panel.CheckItem(child_ctrl, check_value)
523
524    def update_file_append(self, params=None):
525        """
526        Update default_value when any parameters are changed
527        :param params: dictionary of parameters
528        """
529        self.default_value = ""
530        if params is None:
531            params = self.params
532        for key in params:
533            self.default_value += "_{0}".format(key).split(" [")[0]
534            self.default_value += "-{:.2f}".format(params[key])
Note: See TracBrowser for help on using the repository browser.