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 issubclass(item.__class__,LoaderData1D): |
---|
107 | |
---|
108 | if item.name !="$I_{obs}(q)$" and item.name !="$P_{fit}(r)$": |
---|
109 | if hasattr(item, "group_id"): |
---|
110 | return [[invariant_option, |
---|
111 | invariant_hint, self._compute_invariant]] |
---|
112 | return [] |
---|
113 | |
---|
114 | |
---|
115 | def get_perspective(self): |
---|
116 | """ |
---|
117 | Get the list of panel names for this perspective |
---|
118 | """ |
---|
119 | return self.perspective |
---|
120 | |
---|
121 | def on_perspective(self, event): |
---|
122 | """ |
---|
123 | Call back function for the perspective menu item. |
---|
124 | We notify the parent window that the perspective |
---|
125 | has changed. |
---|
126 | @param event: menu event |
---|
127 | """ |
---|
128 | self.parent.set_perspective(self.perspective) |
---|
129 | |
---|
130 | def post_init(self): |
---|
131 | """ |
---|
132 | Post initialization call back to close the loose ends |
---|
133 | """ |
---|
134 | pass |
---|
135 | |
---|
136 | def set_default_perspective(self): |
---|
137 | """ |
---|
138 | Call back method that True to notify the parent that the current plug-in |
---|
139 | can be set as default perspective. |
---|
140 | when returning False, the plug-in is not candidate for an automatic |
---|
141 | default perspective setting |
---|
142 | """ |
---|
143 | return False |
---|
144 | |
---|
145 | def copy_data(self, item, dy=None): |
---|
146 | """ |
---|
147 | receive a data 1D and the list of errors on dy |
---|
148 | and create a new data1D data |
---|
149 | @param return |
---|
150 | """ |
---|
151 | id = None |
---|
152 | if hasattr(item,"id"): |
---|
153 | id = copy.deepcopy(item.id) |
---|
154 | |
---|
155 | data = Data1D(x=item.x, y=item.y, dx=None, dy=None) |
---|
156 | data.copy_from_datainfo(item) |
---|
157 | item.clone_without_data(clone=data) |
---|
158 | data.dy = dy |
---|
159 | data.name = item.name |
---|
160 | ## allow to highlight data when plotted |
---|
161 | data.interactive = copy.deepcopy(item.interactive) |
---|
162 | ## when 2 data have the same id override the 1 st plotted |
---|
163 | data.id = id |
---|
164 | data.group_id = item.group_id |
---|
165 | return data |
---|
166 | |
---|
167 | def _on_data_error(self, event): |
---|
168 | """ |
---|
169 | receives and event from plotting plu-gins to store the data name and |
---|
170 | their errors of y coordinates for 1Data hide and show error |
---|
171 | """ |
---|
172 | self.err_dy = event.err_dy |
---|
173 | |
---|
174 | def _compute_invariant(self, event): |
---|
175 | """ |
---|
176 | Open the invariant panel to invariant computation |
---|
177 | """ |
---|
178 | self.panel = event.GetEventObject() |
---|
179 | Plugin.on_perspective(self,event=event) |
---|
180 | for plottable in self.panel.graph.plottables: |
---|
181 | if plottable.name == self.panel.graph.selected_plottable: |
---|
182 | ## put the errors values back to the model if the errors were hiden |
---|
183 | ## before sending them to the fit engine |
---|
184 | if len(self.err_dy)>0: |
---|
185 | dy = plottable.dy |
---|
186 | if plottable.name in self.err_dy.iterkeys(): |
---|
187 | dy = self.err_dy[plottable.name] |
---|
188 | data = self.copy_data(plottable, dy) |
---|
189 | else: |
---|
190 | data = plottable |
---|
191 | # Store reference to data |
---|
192 | self.__data = data |
---|
193 | # Set the data set to be user for invariant calculation |
---|
194 | self.invariant_panel.set_data(data=data) |
---|
195 | |
---|
196 | def plot_theory(self, data=None, name=None): |
---|
197 | """ |
---|
198 | Receive a data set and post a NewPlotEvent to parent. |
---|
199 | @param data: extrapolated data to be plotted |
---|
200 | @param name: Data's name to use for the legend |
---|
201 | """ |
---|
202 | if data is None: |
---|
203 | new_plot = Theory1D(x=[], y=[], dy=None) |
---|
204 | else: |
---|
205 | new_plot = Theory1D(x=data.x, y=data.y, dy=None) |
---|
206 | new_plot.name = name |
---|
207 | new_plot.xaxis(self.__data._xaxis, self.__data._xunit) |
---|
208 | new_plot.yaxis(self.__data._yaxis, self.__data._yunit) |
---|
209 | new_plot.group_id = self.__data.group_id |
---|
210 | new_plot.id = self.__data.id + name |
---|
211 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=self.__data.name)) |
---|
212 | |
---|
213 | |
---|
214 | |
---|