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