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 | |
---|
20 | |
---|
21 | GROUP_ID_IQ_DATA = r"$I(q)$" |
---|
22 | IQ_DATA_LABEL = r"$I_{obs}(q)$" |
---|
23 | IQ_EXTRAPOLATED_DATA_LABEL = r"$I_{extrap}(q)$" |
---|
24 | |
---|
25 | GROUP_ID_TRANSFORM = r"$\Gamma(x)$" |
---|
26 | TRANSFORM_LABEL = r"$\Gamma(x)$" |
---|
27 | |
---|
28 | |
---|
29 | class Plugin(PluginBase): |
---|
30 | """ |
---|
31 | This class defines the interface for a plugin class for a correlation |
---|
32 | function perspective |
---|
33 | """ |
---|
34 | |
---|
35 | def __init__(self): |
---|
36 | PluginBase.__init__(self, name="Correlation Function") |
---|
37 | logging.info("Correlation function plug-in started") |
---|
38 | self._always_active = True |
---|
39 | self.state_reader = Reader(self.set_state) |
---|
40 | self._extensions = '.cor' |
---|
41 | l = Loader() |
---|
42 | l.associate_file_reader('.cor', self.state_reader) |
---|
43 | |
---|
44 | def get_panels(self, parent): |
---|
45 | """ |
---|
46 | Define the GUI panels |
---|
47 | """ |
---|
48 | self.parent = parent |
---|
49 | self.frame = MDIFrame(self.parent, None, 'None', (100,200)) |
---|
50 | self.data_id = IQ_DATA_LABEL |
---|
51 | self.corfunc_panel = CorfuncPanel(parent=self.frame) |
---|
52 | self.frame.set_panel(self.corfunc_panel) |
---|
53 | self.corfunc_panel.set_manager(self) |
---|
54 | self.perspective.append(self.corfunc_panel.window_name) |
---|
55 | |
---|
56 | return [self.corfunc_panel] |
---|
57 | |
---|
58 | def set_state(self, state=None, datainfo=None): |
---|
59 | """ |
---|
60 | Callback for CorfuncState reader. Called when a .cor file is loaded |
---|
61 | """ |
---|
62 | self.corfunc_panel.set_state(state=state, data=datainfo) |
---|
63 | |
---|
64 | def set_data(self, data_list=None): |
---|
65 | """ |
---|
66 | Load the data that's been selected |
---|
67 | |
---|
68 | :param data_list: The data to load in |
---|
69 | """ |
---|
70 | if data_list is None: |
---|
71 | data_list = [] |
---|
72 | if len(data_list) >= 1: |
---|
73 | msg = "" |
---|
74 | if len(data_list) == 1: |
---|
75 | data = data_list[0] |
---|
76 | else: |
---|
77 | data_1d_list = [] |
---|
78 | data_2d_list = [] |
---|
79 | err_msg = "" |
---|
80 | |
---|
81 | for data in data_list: |
---|
82 | if data is not None: |
---|
83 | if issubclass(data.__class__, Data1D): |
---|
84 | data_1d_list.append(data) |
---|
85 | else: |
---|
86 | err_msg += "{} type {} \n".format(str(data.name), |
---|
87 | str(data.__class__)) |
---|
88 | data_2d_list.append(data) |
---|
89 | if len(data_2d_list) > 0: |
---|
90 | msg = "Corfunc doesn't support the following data types:\n" |
---|
91 | msg += err_msg |
---|
92 | if len(data_1d_list) == 0: |
---|
93 | msg += "No data recieved" |
---|
94 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
95 | info='error')) |
---|
96 | return |
---|
97 | elif len(data_list) > 1: |
---|
98 | msg = "Corfunc does not allow multiple data\n" |
---|
99 | msg += "Please select one.\n" |
---|
100 | dlg = DataDialog(data_list=data_1d_list, text=msg) |
---|
101 | if dlg.ShowModal() == wx.ID_OK: |
---|
102 | data = dlg.get_data() |
---|
103 | else: |
---|
104 | data = None |
---|
105 | dlg.Destroy() |
---|
106 | |
---|
107 | if data is None: |
---|
108 | msg += "Corfunc recieved no data\n" |
---|
109 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
110 | info='error')) |
---|
111 | return |
---|
112 | if issubclass(data.__class__, Data1D): |
---|
113 | try: |
---|
114 | wx.PostEvent(self.parent, NewPlotEvent(action='remove', |
---|
115 | group_id=GROUP_ID_IQ_DATA, |
---|
116 | id=self.data_id)) |
---|
117 | self.data_id = data.id |
---|
118 | self.corfunc_panel.set_data(data) |
---|
119 | except: |
---|
120 | msg = "Corfunc set_data: " + str(sys.exc_value) |
---|
121 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
122 | info='error')) |
---|
123 | |
---|
124 | def show_data(self, data, label, reset=False): |
---|
125 | """ |
---|
126 | Show data read from a file |
---|
127 | |
---|
128 | :param data: The data to plot (Data1D) |
---|
129 | :param label: What to label the plot. Also used as the plot ID |
---|
130 | :param reset: If True, all other plottables will be cleared |
---|
131 | """ |
---|
132 | new_plot = Data1D(data.x, copy.deepcopy(data.y), dy=data.dy) |
---|
133 | group_id = "" |
---|
134 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
135 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
136 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
137 | new_plot.y -= self.corfunc_panel.background |
---|
138 | group_id = GROUP_ID_IQ_DATA |
---|
139 | elif label == TRANSFORM_LABEL: |
---|
140 | new_plot.xaxis("{x}", 'A') |
---|
141 | new_plot.yaxis("{\\Gamma}", '') |
---|
142 | group_id = GROUP_ID_TRANSFORM |
---|
143 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
144 | new_plot.id = label |
---|
145 | new_plot.name = label |
---|
146 | new_plot.group_id = group_id |
---|
147 | new_plot.interactive = True |
---|
148 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
149 | # Show data on a linear scale |
---|
150 | new_plot.xtransform = 'x' |
---|
151 | new_plot.ytransform = 'y' |
---|
152 | wx.PostEvent(self.parent, |
---|
153 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
154 | reset=reset)) |
---|