1 | """ |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
5 | |
---|
6 | See the license text in license.txt |
---|
7 | |
---|
8 | copyright 2009, University of Tennessee |
---|
9 | """ |
---|
10 | import wx |
---|
11 | from sans.invariant import invariant |
---|
12 | |
---|
13 | from DataLoader.data_info import Data1D as LoaderData1D |
---|
14 | from sans.guiframe.dataFitting import Theory1D, Data1D |
---|
15 | |
---|
16 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
17 | from sans.guicomm.events import ERR_DATA |
---|
18 | class Plugin: |
---|
19 | """ |
---|
20 | This class defines the interface for invariant Plugin class |
---|
21 | that can be used by the gui_manager. |
---|
22 | |
---|
23 | """ |
---|
24 | |
---|
25 | def __init__(self, standalone=False): |
---|
26 | """ |
---|
27 | Abstract class for gui_manager Plugins. |
---|
28 | """ |
---|
29 | ## Plug-in name. It will appear on the application menu. |
---|
30 | self.sub_menu = "Invariant" |
---|
31 | |
---|
32 | ## Reference to the parent window. Filled by get_panels() below. |
---|
33 | self.parent = None |
---|
34 | #dictionary containing data name and error on dy of that data |
---|
35 | self.err_dy = {} |
---|
36 | ## List of panels that you would like to open in AUI windows |
---|
37 | # for your plug-in. This defines your plug-in "perspective" |
---|
38 | self.perspective = [] |
---|
39 | |
---|
40 | def populate_menu(self, id, parent): |
---|
41 | """ |
---|
42 | Create and return the list of application menu |
---|
43 | items for the plug-in. |
---|
44 | |
---|
45 | @param id: deprecated. Un-used. |
---|
46 | @param parent: parent window |
---|
47 | @return: plug-in menu |
---|
48 | """ |
---|
49 | return [] |
---|
50 | |
---|
51 | def get_panels(self, parent): |
---|
52 | """ |
---|
53 | Create and return the list of wx.Panels for your plug-in. |
---|
54 | Define the plug-in perspective. |
---|
55 | |
---|
56 | Panels should inherit from DefaultPanel defined below, |
---|
57 | or should present the same interface. They must define |
---|
58 | "window_caption" and "window_name". |
---|
59 | |
---|
60 | @param parent: parent window |
---|
61 | @return: list of panels |
---|
62 | """ |
---|
63 | ## Save a reference to the parent |
---|
64 | self.parent = parent |
---|
65 | #add error back to the data |
---|
66 | self.parent.Bind(ERR_DATA, self._on_data_error) |
---|
67 | from invariant_panel import InvariantPanel |
---|
68 | self.invariant_panel = InvariantPanel(parent=self.parent) |
---|
69 | self.invariant_panel.set_manager(manager=self) |
---|
70 | self.perspective.append(self.invariant_panel.window_name) |
---|
71 | # Return the list of panels |
---|
72 | return [self.invariant_panel] |
---|
73 | |
---|
74 | def get_tools(self): |
---|
75 | """ |
---|
76 | Returns a set of menu entries for tools |
---|
77 | """ |
---|
78 | return [] |
---|
79 | |
---|
80 | |
---|
81 | def get_context_menu(self, graph=None): |
---|
82 | """ |
---|
83 | This method is optional. |
---|
84 | |
---|
85 | When the context menu of a plot is rendered, the |
---|
86 | get_context_menu method will be called to give you a |
---|
87 | chance to add a menu item to the context menu. |
---|
88 | |
---|
89 | A ref to a Graph object is passed so that you can |
---|
90 | investigate the plot content and decide whether you |
---|
91 | need to add items to the context menu. |
---|
92 | |
---|
93 | This method returns a list of menu items. |
---|
94 | Each item is itself a list defining the text to |
---|
95 | appear in the menu, a tool-tip help text, and a |
---|
96 | call-back method. |
---|
97 | |
---|
98 | @param graph: the Graph object to which we attach the context menu |
---|
99 | @return: a list of menu items with call-back function |
---|
100 | """ |
---|
101 | self.graph = graph |
---|
102 | invariant_option = "Compute invariant" |
---|
103 | invariant_hint = "Will displays the invariant panel for futher computation" |
---|
104 | |
---|
105 | for item in self.graph.plottables: |
---|
106 | if item.name == graph.selected_plottable : |
---|
107 | if issubclass(item.__class__,LoaderData1D): |
---|
108 | |
---|
109 | if item.name !="$I_{obs}(q)$" and item.name !="$P_{fit}(r)$": |
---|
110 | if hasattr(item, "group_id"): |
---|
111 | return [[invariant_option, |
---|
112 | invariant_hint, self._compute_invariant]] |
---|
113 | return [] |
---|
114 | |
---|
115 | |
---|
116 | def get_perspective(self): |
---|
117 | """ |
---|
118 | Get the list of panel names for this perspective |
---|
119 | """ |
---|
120 | return self.perspective |
---|
121 | |
---|
122 | def on_perspective(self, event): |
---|
123 | """ |
---|
124 | Call back function for the perspective menu item. |
---|
125 | We notify the parent window that the perspective |
---|
126 | has changed. |
---|
127 | @param event: menu event |
---|
128 | """ |
---|
129 | self.parent.set_perspective(self.perspective) |
---|
130 | |
---|
131 | def post_init(self): |
---|
132 | """ |
---|
133 | Post initialization call back to close the loose ends |
---|
134 | """ |
---|
135 | pass |
---|
136 | |
---|
137 | def set_default_perspective(self): |
---|
138 | """ |
---|
139 | Call back method that True to notify the parent that the current plug-in |
---|
140 | can be set as default perspective. |
---|
141 | when returning False, the plug-in is not candidate for an automatic |
---|
142 | default perspective setting |
---|
143 | """ |
---|
144 | return False |
---|
145 | |
---|
146 | def copy_data(self, item, dy=None): |
---|
147 | """ |
---|
148 | receive a data 1D and the list of errors on dy |
---|
149 | and create a new data1D data |
---|
150 | @param return |
---|
151 | """ |
---|
152 | id = None |
---|
153 | if hasattr(item,"id"): |
---|
154 | id = copy.deepcopy(item.id) |
---|
155 | |
---|
156 | data = Data1D(x=item.x, y=item.y, dx=None, dy=None) |
---|
157 | data.copy_from_datainfo(item) |
---|
158 | item.clone_without_data(clone=data) |
---|
159 | data.dy = dy |
---|
160 | data.name = item.name |
---|
161 | ## allow to highlight data when plotted |
---|
162 | data.interactive = copy.deepcopy(item.interactive) |
---|
163 | ## when 2 data have the same id override the 1 st plotted |
---|
164 | data.id = id |
---|
165 | data.group_id = item.group_id |
---|
166 | return data |
---|
167 | |
---|
168 | def _on_data_error(self, event): |
---|
169 | """ |
---|
170 | receives and event from plotting plu-gins to store the data name and |
---|
171 | their errors of y coordinates for 1Data hide and show error |
---|
172 | """ |
---|
173 | self.err_dy = event.err_dy |
---|
174 | |
---|
175 | def _compute_invariant(self, event): |
---|
176 | """ |
---|
177 | Open the invariant panel to invariant computation |
---|
178 | """ |
---|
179 | self.panel = event.GetEventObject() |
---|
180 | Plugin.on_perspective(self,event=event) |
---|
181 | for plottable in self.panel.graph.plottables: |
---|
182 | if plottable.name == self.panel.graph.selected_plottable: |
---|
183 | ## put the errors values back to the model if the errors were hiden |
---|
184 | ## before sending them to the fit engine |
---|
185 | if len(self.err_dy)>0: |
---|
186 | dy = plottable.dy |
---|
187 | if plottable.name in self.err_dy.iterkeys(): |
---|
188 | dy = self.err_dy[plottable.name] |
---|
189 | data = self.copy_data(plottable, dy) |
---|
190 | else: |
---|
191 | data = plottable |
---|
192 | # Store reference to data |
---|
193 | self.__data = data |
---|
194 | # Set the data set to be user for invariant calculation |
---|
195 | self.invariant_panel.set_data(data=data) |
---|
196 | |
---|
197 | def plot_theory(self, data=None, name=None): |
---|
198 | """ |
---|
199 | Receive a data set and post a NewPlotEvent to parent. |
---|
200 | @param data: extrapolated data to be plotted |
---|
201 | @param name: Data's name to use for the legend |
---|
202 | """ |
---|
203 | if data is None: |
---|
204 | new_plot = Theory1D(x=[], y=[], dy=None) |
---|
205 | else: |
---|
206 | new_plot = Theory1D(x=data.x, y=data.y, dy=None) |
---|
207 | new_plot.name = name |
---|
208 | new_plot.xaxis(self.__data._xaxis, self.__data._xunit) |
---|
209 | new_plot.yaxis(self.__data._yaxis, self.__data._yunit) |
---|
210 | new_plot.group_id = self.__data.group_id |
---|
211 | new_plot.id = self.__data.id + name |
---|
212 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=self.__data.name)) |
---|
213 | |
---|
214 | |
---|
215 | |
---|