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 | |
---|
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)) |
---|
42 | self.data_id = IQ_DATA_LABEL |
---|
43 | self.corfunc_panel = CorfuncPanel(parent=self.frame) |
---|
44 | self.frame.set_panel(self.corfunc_panel) |
---|
45 | self.corfunc_panel.set_manager(self) |
---|
46 | self._frame_set_helper() |
---|
47 | self.perspective.append(self.corfunc_panel.window_name) |
---|
48 | |
---|
49 | l = Loader() |
---|
50 | l.associate_file_reader('.cor', self.state_reader) |
---|
51 | |
---|
52 | return [self.corfunc_panel] |
---|
53 | |
---|
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 | |
---|
81 | def set_state(self, state=None, datainfo=None): |
---|
82 | """ |
---|
83 | Callback for CorfuncState reader. Called when a .cor file is loaded |
---|
84 | """ |
---|
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 }) |
---|
93 | |
---|
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: |
---|
103 | msg = "" |
---|
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 |
---|
148 | self.corfunc_panel.set_data(data) |
---|
149 | except: |
---|
150 | msg = "Corfunc set_data: " + str(sys.exc_value) |
---|
151 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
152 | info='error')) |
---|
153 | |
---|
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 | |
---|
161 | def show_data(self, data, label, reset=False, active_ctrl=None): |
---|
162 | """ |
---|
163 | Show data read from a file |
---|
164 | |
---|
165 | :param data: The data to plot (Data1D) |
---|
166 | :param label: What to label the plot. Also used as the plot ID |
---|
167 | :param reset: If True, all other plottables will be cleared |
---|
168 | """ |
---|
169 | new_plot = Data1D(data.x, copy.deepcopy(data.y), dy=data.dy) |
---|
170 | group_id = "" |
---|
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}") |
---|
174 | new_plot.y -= self.corfunc_panel.background |
---|
175 | # Show data on a log(Q)/I scale |
---|
176 | new_plot.ytransform = 'y' |
---|
177 | group_id = GROUP_ID_IQ_DATA |
---|
178 | if label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
179 | # Show the extrapolation as a curve instead of points |
---|
180 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
181 | elif label == TRANSFORM_LABEL: |
---|
182 | new_plot.xaxis("{x}", 'A') |
---|
183 | new_plot.yaxis("{\\Gamma}", '') |
---|
184 | # Show transform on a linear scale |
---|
185 | new_plot.xtransform = 'x' |
---|
186 | new_plot.ytransform = 'y' |
---|
187 | group_id = GROUP_ID_TRANSFORM |
---|
188 | # Show the transformation as a curve instead of points |
---|
189 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
190 | new_plot.id = label |
---|
191 | new_plot.name = label |
---|
192 | new_plot.group_id = group_id |
---|
193 | new_plot.interactive = True |
---|
194 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
195 | wx.PostEvent(self.parent, |
---|
196 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
197 | reset=reset)) |
---|
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) |
---|
201 | if label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
202 | # Zoom in to the region we're interested in |
---|
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)) |
---|
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)) |
---|
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]) |
---|