[c23f303] | 1 | """ |
---|
| 2 | Corfunc perspective |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
[7858575] | 5 | import sys |
---|
[c23f303] | 6 | import logging |
---|
[3b8efec] | 7 | import copy |
---|
[c23f303] | 8 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
| 9 | from sas.sasgui.guiframe.gui_manager import MDIFrame |
---|
[7858575] | 10 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 11 | from sas.sasgui.guiframe.events import NewPlotEvent |
---|
[6ffa0dd] | 12 | from sas.sasgui.guiframe.events import PlotLimitEvent |
---|
[7858575] | 13 | from sas.sasgui.guiframe.gui_style import GUIFRAME_ID |
---|
[c23f303] | 14 | from sas.sasgui.perspectives.corfunc.corfunc_panel import CorfuncPanel |
---|
[7858575] | 15 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 16 | from sas.sasgui.perspectives.pr.pr_widgets import DataDialog |
---|
[e02d8f6] | 17 | from sas.sasgui.perspectives.corfunc.corfunc_state import Reader |
---|
| 18 | from sas.sascalc.dataloader.loader import Loader |
---|
| 19 | import sas.sascalc.dataloader |
---|
[37e7223] | 20 | from plot_labels import * |
---|
[911dbe4] | 21 | |
---|
[c23f303] | 22 | |
---|
| 23 | class Plugin(PluginBase): |
---|
| 24 | """ |
---|
| 25 | This class defines the interface for a plugin class for a correlation |
---|
| 26 | function perspective |
---|
| 27 | """ |
---|
| 28 | |
---|
| 29 | def __init__(self): |
---|
| 30 | PluginBase.__init__(self, name="Correlation Function") |
---|
| 31 | logging.info("Correlation function plug-in started") |
---|
| 32 | self._always_active = True |
---|
[e02d8f6] | 33 | self.state_reader = Reader(self.set_state) |
---|
| 34 | self._extensions = '.cor' |
---|
| 35 | |
---|
[c23f303] | 36 | def get_panels(self, parent): |
---|
| 37 | """ |
---|
| 38 | Define the GUI panels |
---|
| 39 | """ |
---|
| 40 | self.parent = parent |
---|
| 41 | self.frame = MDIFrame(self.parent, None, 'None', (100,200)) |
---|
[7858575] | 42 | self.data_id = IQ_DATA_LABEL |
---|
[c23f303] | 43 | self.corfunc_panel = CorfuncPanel(parent=self.frame) |
---|
| 44 | self.frame.set_panel(self.corfunc_panel) |
---|
| 45 | self.corfunc_panel.set_manager(self) |
---|
[173ee3a] | 46 | self._frame_set_helper() |
---|
[c23f303] | 47 | self.perspective.append(self.corfunc_panel.window_name) |
---|
| 48 | |
---|
[96d293da] | 49 | l = Loader() |
---|
| 50 | l.associate_file_reader('.cor', self.state_reader) |
---|
| 51 | |
---|
[c23f303] | 52 | return [self.corfunc_panel] |
---|
[7858575] | 53 | |
---|
[1925a8e] | 54 | def get_context_menu(self, plotpanel=None): |
---|
| 55 | """ |
---|
| 56 | Get the context menu items available for Corfunc. |
---|
| 57 | |
---|
| 58 | :param plotpanel: A Plotter1D panel |
---|
| 59 | |
---|
| 60 | :return: a list of menu items with call-back function |
---|
| 61 | |
---|
| 62 | :note: if Data1D was generated from Theory1D |
---|
| 63 | the fitting option is not allowed |
---|
| 64 | """ |
---|
| 65 | graph = plotpanel.graph |
---|
| 66 | if graph.selected_plottable not in plotpanel.plots: |
---|
| 67 | return [] |
---|
| 68 | data = plotpanel.plots[graph.selected_plottable] |
---|
| 69 | if data.id == IQ_DATA_LABEL or data.id == IQ_EXTRAPOLATED_DATA_LABEL or data.id == TRANSFORM_LABEL: |
---|
| 70 | return [] |
---|
| 71 | item = plotpanel.plots[graph.selected_plottable] |
---|
| 72 | if item.__class__.__name__ is "Data2D": |
---|
| 73 | return [] |
---|
| 74 | elif item.__class__.__name__ is "Data1D": |
---|
| 75 | return [["Select data in corfunc", |
---|
| 76 | "Send this data to the correlation function perspective", |
---|
| 77 | self._on_select_data]] |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
[e02d8f6] | 81 | def set_state(self, state=None, datainfo=None): |
---|
| 82 | """ |
---|
| 83 | Callback for CorfuncState reader. Called when a .cor file is loaded |
---|
| 84 | """ |
---|
[96d293da] | 85 | if isinstance(datainfo, list): |
---|
| 86 | data = datainfo[0] |
---|
| 87 | else: |
---|
| 88 | data = datainfo |
---|
| 89 | self.corfunc_panel.set_state(state=state, data=data) |
---|
| 90 | self.on_perspective(event=None) |
---|
| 91 | data = self.parent.create_gui_data(data, None) |
---|
| 92 | self.parent.add_data({ data.id: data }) |
---|
[e02d8f6] | 93 | |
---|
[7858575] | 94 | def set_data(self, data_list=None): |
---|
| 95 | """ |
---|
| 96 | Load the data that's been selected |
---|
| 97 | |
---|
| 98 | :param data_list: The data to load in |
---|
| 99 | """ |
---|
| 100 | if data_list is None: |
---|
| 101 | data_list = [] |
---|
| 102 | if len(data_list) >= 1: |
---|
[2196f102] | 103 | msg = "" |
---|
[7858575] | 104 | if len(data_list) == 1: |
---|
| 105 | data = data_list[0] |
---|
| 106 | else: |
---|
| 107 | data_1d_list = [] |
---|
| 108 | data_2d_list = [] |
---|
| 109 | err_msg = "" |
---|
| 110 | |
---|
| 111 | for data in data_list: |
---|
| 112 | if data is not None: |
---|
| 113 | if issubclass(data.__class__, Data1D): |
---|
| 114 | data_1d_list.append(data) |
---|
| 115 | else: |
---|
| 116 | err_msg += "{} type {} \n".format(str(data.name), |
---|
| 117 | str(data.__class__)) |
---|
| 118 | data_2d_list.append(data) |
---|
| 119 | if len(data_2d_list) > 0: |
---|
| 120 | msg = "Corfunc doesn't support the following data types:\n" |
---|
| 121 | msg += err_msg |
---|
| 122 | if len(data_1d_list) == 0: |
---|
| 123 | msg += "No data recieved" |
---|
| 124 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 125 | info='error')) |
---|
| 126 | return |
---|
| 127 | elif len(data_list) > 1: |
---|
| 128 | msg = "Corfunc does not allow multiple data\n" |
---|
| 129 | msg += "Please select one.\n" |
---|
| 130 | dlg = DataDialog(data_list=data_1d_list, text=msg) |
---|
| 131 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 132 | data = dlg.get_data() |
---|
| 133 | else: |
---|
| 134 | data = None |
---|
| 135 | dlg.Destroy() |
---|
| 136 | |
---|
| 137 | if data is None: |
---|
| 138 | msg += "Corfunc recieved no data\n" |
---|
| 139 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 140 | info='error')) |
---|
| 141 | return |
---|
| 142 | if issubclass(data.__class__, Data1D): |
---|
| 143 | try: |
---|
| 144 | wx.PostEvent(self.parent, NewPlotEvent(action='remove', |
---|
| 145 | group_id=GROUP_ID_IQ_DATA, |
---|
| 146 | id=self.data_id)) |
---|
| 147 | self.data_id = data.id |
---|
[e02d8f6] | 148 | self.corfunc_panel.set_data(data) |
---|
[7858575] | 149 | except: |
---|
| 150 | msg = "Corfunc set_data: " + str(sys.exc_value) |
---|
| 151 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 152 | info='error')) |
---|
| 153 | |
---|
[da19985] | 154 | def delete_data(self, data): |
---|
| 155 | """ |
---|
| 156 | Delete the data from the perspective |
---|
| 157 | """ |
---|
| 158 | self.clear_data() |
---|
| 159 | self.corfunc_panel.set_data(None) |
---|
| 160 | |
---|
[02a8779] | 161 | def show_data(self, data, label, reset=False, active_ctrl=None): |
---|
[7858575] | 162 | """ |
---|
| 163 | Show data read from a file |
---|
| 164 | |
---|
| 165 | :param data: The data to plot (Data1D) |
---|
[6970e51] | 166 | :param label: What to label the plot. Also used as the plot ID |
---|
[7858575] | 167 | :param reset: If True, all other plottables will be cleared |
---|
| 168 | """ |
---|
[dc72638] | 169 | new_plot = Data1D(data.x, copy.deepcopy(data.y), dy=data.dy) |
---|
[911dbe4] | 170 | group_id = "" |
---|
[3b8efec] | 171 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
| 172 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 173 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
[dc72638] | 174 | new_plot.y -= self.corfunc_panel.background |
---|
[5a525e1] | 175 | # Show data on a log(Q)/I scale |
---|
| 176 | new_plot.ytransform = 'y' |
---|
[911dbe4] | 177 | group_id = GROUP_ID_IQ_DATA |
---|
[6f343e3] | 178 | if label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
[204f628] | 179 | # Show the extrapolation as a curve instead of points |
---|
[6f343e3] | 180 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[911dbe4] | 181 | elif label == TRANSFORM_LABEL: |
---|
| 182 | new_plot.xaxis("{x}", 'A') |
---|
| 183 | new_plot.yaxis("{\\Gamma}", '') |
---|
[5a525e1] | 184 | # Show transform on a linear scale |
---|
| 185 | new_plot.xtransform = 'x' |
---|
| 186 | new_plot.ytransform = 'y' |
---|
[911dbe4] | 187 | group_id = GROUP_ID_TRANSFORM |
---|
[204f628] | 188 | # Show the transformation as a curve instead of points |
---|
[6f343e3] | 189 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[3b8efec] | 190 | new_plot.id = label |
---|
| 191 | new_plot.name = label |
---|
[911dbe4] | 192 | new_plot.group_id = group_id |
---|
[7858575] | 193 | new_plot.interactive = True |
---|
[911dbe4] | 194 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
[7858575] | 195 | wx.PostEvent(self.parent, |
---|
[911dbe4] | 196 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
| 197 | reset=reset)) |
---|
[02a8779] | 198 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
| 199 | wx.CallAfter(self.corfunc_panel.plot_qrange, active=active_ctrl, |
---|
| 200 | leftdown=True) |
---|
[6ffa0dd] | 201 | if label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
[204f628] | 202 | # Zoom in to the region we're interested in |
---|
[6ffa0dd] | 203 | xlim = (min(self.corfunc_panel._extrapolated_data.x), self.corfunc_panel.qmax[1]*1.2) |
---|
| 204 | wx.CallAfter(wx.PostEvent, self.parent, PlotLimitEvent(id=IQ_DATA_LABEL, group_id=GROUP_ID_IQ_DATA, xlim=xlim)) |
---|
[1150083] | 205 | |
---|
| 206 | def clear_data(self): |
---|
| 207 | wx.PostEvent(self.parent, |
---|
| 208 | NewPlotEvent(action='delete', group_id=GROUP_ID_TRANSFORM)) |
---|
| 209 | wx.PostEvent(self.parent, |
---|
| 210 | NewPlotEvent(action='clear', group_id=GROUP_ID_IQ_DATA)) |
---|
[1925a8e] | 211 | |
---|
| 212 | def _on_select_data(self, event): |
---|
| 213 | panel = event.GetEventObject() |
---|
| 214 | if not panel.graph.selected_plottable in panel.plots: |
---|
| 215 | return |
---|
| 216 | data = panel.plots[panel.graph.selected_plottable] |
---|
| 217 | self.set_data([data]) |
---|