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