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.gui_style import GUIFRAME_ID |
---|
13 | from sas.sasgui.perspectives.corfunc.corfunc_panel import CorfuncPanel |
---|
14 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
15 | from sas.sasgui.perspectives.pr.pr_widgets import DataDialog |
---|
16 | from sas.sasgui.perspectives.corfunc.corfunc_state import Reader |
---|
17 | from sas.sascalc.dataloader.loader import Loader |
---|
18 | import sas.sascalc.dataloader |
---|
19 | from plot_labels import * |
---|
20 | |
---|
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 |
---|
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 | |
---|
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)) |
---|
43 | self.data_id = IQ_DATA_LABEL |
---|
44 | self.corfunc_panel = CorfuncPanel(parent=self.frame) |
---|
45 | self.frame.set_panel(self.corfunc_panel) |
---|
46 | self.corfunc_panel.set_manager(self) |
---|
47 | self._frame_set_helper() |
---|
48 | self.perspective.append(self.corfunc_panel.window_name) |
---|
49 | |
---|
50 | return [self.corfunc_panel] |
---|
51 | |
---|
52 | def set_state(self, state=None, datainfo=None): |
---|
53 | """ |
---|
54 | Callback for CorfuncState reader. Called when a .cor file is loaded |
---|
55 | """ |
---|
56 | self.corfunc_panel.set_state(state=state, data=datainfo) |
---|
57 | |
---|
58 | def set_data(self, data_list=None): |
---|
59 | """ |
---|
60 | Load the data that's been selected |
---|
61 | |
---|
62 | :param data_list: The data to load in |
---|
63 | """ |
---|
64 | if data_list is None: |
---|
65 | data_list = [] |
---|
66 | if len(data_list) >= 1: |
---|
67 | msg = "" |
---|
68 | if len(data_list) == 1: |
---|
69 | data = data_list[0] |
---|
70 | else: |
---|
71 | data_1d_list = [] |
---|
72 | data_2d_list = [] |
---|
73 | err_msg = "" |
---|
74 | |
---|
75 | for data in data_list: |
---|
76 | if data is not None: |
---|
77 | if issubclass(data.__class__, Data1D): |
---|
78 | data_1d_list.append(data) |
---|
79 | else: |
---|
80 | err_msg += "{} type {} \n".format(str(data.name), |
---|
81 | str(data.__class__)) |
---|
82 | data_2d_list.append(data) |
---|
83 | if len(data_2d_list) > 0: |
---|
84 | msg = "Corfunc doesn't support the following data types:\n" |
---|
85 | msg += err_msg |
---|
86 | if len(data_1d_list) == 0: |
---|
87 | msg += "No data recieved" |
---|
88 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
89 | info='error')) |
---|
90 | return |
---|
91 | elif len(data_list) > 1: |
---|
92 | msg = "Corfunc does not allow multiple data\n" |
---|
93 | msg += "Please select one.\n" |
---|
94 | dlg = DataDialog(data_list=data_1d_list, text=msg) |
---|
95 | if dlg.ShowModal() == wx.ID_OK: |
---|
96 | data = dlg.get_data() |
---|
97 | else: |
---|
98 | data = None |
---|
99 | dlg.Destroy() |
---|
100 | |
---|
101 | if data is None: |
---|
102 | msg += "Corfunc recieved no data\n" |
---|
103 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
104 | info='error')) |
---|
105 | return |
---|
106 | if issubclass(data.__class__, Data1D): |
---|
107 | try: |
---|
108 | wx.PostEvent(self.parent, NewPlotEvent(action='remove', |
---|
109 | group_id=GROUP_ID_IQ_DATA, |
---|
110 | id=self.data_id)) |
---|
111 | self.data_id = data.id |
---|
112 | self.corfunc_panel.set_data(data) |
---|
113 | except: |
---|
114 | msg = "Corfunc set_data: " + str(sys.exc_value) |
---|
115 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
116 | info='error')) |
---|
117 | |
---|
118 | def delete_data(self, data): |
---|
119 | """ |
---|
120 | Delete the data from the perspective |
---|
121 | """ |
---|
122 | self.clear_data() |
---|
123 | self.corfunc_panel.set_data(None) |
---|
124 | |
---|
125 | def show_data(self, data, label, reset=False, active_ctrl=None): |
---|
126 | """ |
---|
127 | Show data read from a file |
---|
128 | |
---|
129 | :param data: The data to plot (Data1D) |
---|
130 | :param label: What to label the plot. Also used as the plot ID |
---|
131 | :param reset: If True, all other plottables will be cleared |
---|
132 | """ |
---|
133 | new_plot = Data1D(data.x, copy.deepcopy(data.y), dy=data.dy) |
---|
134 | group_id = "" |
---|
135 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
136 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
137 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
138 | new_plot.y -= self.corfunc_panel.background |
---|
139 | group_id = GROUP_ID_IQ_DATA |
---|
140 | elif label == TRANSFORM_LABEL: |
---|
141 | new_plot.xaxis("{x}", 'A') |
---|
142 | new_plot.yaxis("{\\Gamma}", '') |
---|
143 | group_id = GROUP_ID_TRANSFORM |
---|
144 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
145 | new_plot.id = label |
---|
146 | new_plot.name = label |
---|
147 | new_plot.group_id = group_id |
---|
148 | new_plot.interactive = True |
---|
149 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
150 | # Show data on a linear scale |
---|
151 | new_plot.xtransform = 'x' |
---|
152 | new_plot.ytransform = 'y' |
---|
153 | wx.PostEvent(self.parent, |
---|
154 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
155 | reset=reset)) |
---|
156 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
157 | wx.CallAfter(self.corfunc_panel.plot_qrange, active=active_ctrl, |
---|
158 | leftdown=True) |
---|
159 | |
---|
160 | def clear_data(self): |
---|
161 | wx.PostEvent(self.parent, |
---|
162 | NewPlotEvent(action='delete', group_id=GROUP_ID_TRANSFORM)) |
---|
163 | wx.PostEvent(self.parent, |
---|
164 | NewPlotEvent(action='clear', group_id=GROUP_ID_IQ_DATA)) |
---|