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