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