1 | """ |
---|
2 | Corfunc perspective |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import sys |
---|
6 | import logging |
---|
7 | import copy |
---|
8 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
9 | from sas.sasgui.guiframe.gui_manager import MDIFrame |
---|
10 | from sas.sasgui.guiframe.events import StatusEvent |
---|
11 | from sas.sasgui.guiframe.events import NewPlotEvent |
---|
12 | from sas.sasgui.guiframe.events import PlotLimitEvent |
---|
13 | from sas.sasgui.guiframe.gui_style import GUIFRAME_ID |
---|
14 | from sas.sasgui.perspectives.corfunc.corfunc_panel import CorfuncPanel |
---|
15 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
16 | from sas.sasgui.perspectives.pr.pr_widgets import DataDialog |
---|
17 | from sas.sasgui.perspectives.corfunc.corfunc_state import Reader |
---|
18 | from sas.sascalc.dataloader.loader import Loader |
---|
19 | import sas.sascalc.dataloader |
---|
20 | from plot_labels import * |
---|
21 | |
---|
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 |
---|
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 | |
---|
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)) |
---|
44 | self.data_id = IQ_DATA_LABEL |
---|
45 | self.corfunc_panel = CorfuncPanel(parent=self.frame) |
---|
46 | self.frame.set_panel(self.corfunc_panel) |
---|
47 | self.corfunc_panel.set_manager(self) |
---|
48 | self._frame_set_helper() |
---|
49 | self.perspective.append(self.corfunc_panel.window_name) |
---|
50 | |
---|
51 | return [self.corfunc_panel] |
---|
52 | |
---|
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 | |
---|
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 | |
---|
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: |
---|
95 | msg = "" |
---|
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 |
---|
140 | self.corfunc_panel.set_data(data) |
---|
141 | except: |
---|
142 | msg = "Corfunc set_data: " + str(sys.exc_value) |
---|
143 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
144 | info='error')) |
---|
145 | |
---|
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 | |
---|
153 | def show_data(self, data, label, reset=False, active_ctrl=None): |
---|
154 | """ |
---|
155 | Show data read from a file |
---|
156 | |
---|
157 | :param data: The data to plot (Data1D) |
---|
158 | :param label: What to label the plot. Also used as the plot ID |
---|
159 | :param reset: If True, all other plottables will be cleared |
---|
160 | """ |
---|
161 | new_plot = Data1D(data.x, copy.deepcopy(data.y), dy=data.dy) |
---|
162 | group_id = "" |
---|
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}") |
---|
166 | new_plot.y -= self.corfunc_panel.background |
---|
167 | group_id = GROUP_ID_IQ_DATA |
---|
168 | if label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
169 | # Show the extrapolation as a curve instead of points |
---|
170 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
171 | elif label == TRANSFORM_LABEL: |
---|
172 | new_plot.xaxis("{x}", 'A') |
---|
173 | new_plot.yaxis("{\\Gamma}", '') |
---|
174 | group_id = GROUP_ID_TRANSFORM |
---|
175 | # Show the transformation as a curve instead of points |
---|
176 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
177 | new_plot.id = label |
---|
178 | new_plot.name = label |
---|
179 | new_plot.group_id = group_id |
---|
180 | new_plot.interactive = True |
---|
181 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
182 | # Show data on a linear scale |
---|
183 | new_plot.xtransform = 'x' |
---|
184 | new_plot.ytransform = 'y' |
---|
185 | wx.PostEvent(self.parent, |
---|
186 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
187 | reset=reset)) |
---|
188 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
189 | wx.CallAfter(self.corfunc_panel.plot_qrange, active=active_ctrl, |
---|
190 | leftdown=True) |
---|
191 | if label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
192 | # Zoom in to the region we're interested in |
---|
193 | xlim = (min(self.corfunc_panel._extrapolated_data.x), self.corfunc_panel.qmax[1]*1.2) |
---|
194 | wx.CallAfter(wx.PostEvent, self.parent, PlotLimitEvent(id=IQ_DATA_LABEL, group_id=GROUP_ID_IQ_DATA, xlim=xlim)) |
---|
195 | |
---|
196 | def clear_data(self): |
---|
197 | wx.PostEvent(self.parent, |
---|
198 | NewPlotEvent(action='delete', group_id=GROUP_ID_TRANSFORM)) |
---|
199 | wx.PostEvent(self.parent, |
---|
200 | NewPlotEvent(action='clear', group_id=GROUP_ID_IQ_DATA)) |
---|
201 | |
---|
202 | def _on_select_data(self, event): |
---|
203 | panel = event.GetEventObject() |
---|
204 | if not panel.graph.selected_plottable in panel.plots: |
---|
205 | return |
---|
206 | data = panel.plots[panel.graph.selected_plottable] |
---|
207 | self.set_data([data]) |
---|