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 show_data(self, data, label, reset=False, active_ctrl=None): |
---|
119 | """ |
---|
120 | Show data read from a file |
---|
121 | |
---|
122 | :param data: The data to plot (Data1D) |
---|
123 | :param label: What to label the plot. Also used as the plot ID |
---|
124 | :param reset: If True, all other plottables will be cleared |
---|
125 | """ |
---|
126 | new_plot = Data1D(data.x, copy.deepcopy(data.y), dy=data.dy) |
---|
127 | group_id = "" |
---|
128 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
129 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
130 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
131 | new_plot.y -= self.corfunc_panel.background |
---|
132 | group_id = GROUP_ID_IQ_DATA |
---|
133 | elif label == TRANSFORM_LABEL: |
---|
134 | new_plot.xaxis("{x}", 'A') |
---|
135 | new_plot.yaxis("{\\Gamma}", '') |
---|
136 | group_id = GROUP_ID_TRANSFORM |
---|
137 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
138 | new_plot.id = label |
---|
139 | new_plot.name = label |
---|
140 | new_plot.group_id = group_id |
---|
141 | new_plot.interactive = True |
---|
142 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
143 | # Show data on a linear scale |
---|
144 | new_plot.xtransform = 'x' |
---|
145 | new_plot.ytransform = 'y' |
---|
146 | wx.PostEvent(self.parent, |
---|
147 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
148 | reset=reset)) |
---|
149 | if label == IQ_EXTRAPOLATED_DATA_LABEL or label == TRANSFORM_LABEL: |
---|
150 | self.parent.update_theory(data_id=label, theory=new_plot) |
---|
151 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
152 | wx.CallAfter(self.corfunc_panel.plot_qrange, active=active_ctrl, |
---|
153 | leftdown=True) |
---|
154 | |
---|
155 | def clear_data(self): |
---|
156 | wx.PostEvent(self.parent, |
---|
157 | NewPlotEvent(action='delete', group_id=GROUP_ID_TRANSFORM)) |
---|
158 | wx.PostEvent(self.parent, |
---|
159 | NewPlotEvent(action='clear', group_id=GROUP_ID_IQ_DATA)) |
---|