[c128284] | 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 | """ |
---|
[272d91e] | 10 | import wx |
---|
[c128284] | 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 |
---|
[272d91e] | 66 | self.parent.Bind(ERR_DATA, self._on_data_error) |
---|
[c128284] | 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 |
---|
[272d91e] | 191 | #set the data for the panel |
---|
| 192 | self.invariant_panel.set_data(data=data) |
---|
[c128284] | 193 | |
---|
[272d91e] | 194 | |
---|
| 195 | def _plot_data(self, reel_data, extra_data, name="Unknown"): |
---|
[c128284] | 196 | """ |
---|
| 197 | Receive a data and post a NewPlotEvent to parent |
---|
| 198 | @param data: data created frome xtrapolation to plot |
---|
| 199 | @param name: Data's name to use for the legend |
---|
| 200 | """ |
---|
| 201 | if self.graph is None: |
---|
| 202 | return |
---|
| 203 | #print "went here " |
---|
| 204 | plottable = self.graph.get_plottable(name=name) |
---|
| 205 | if plottable is not None: |
---|
| 206 | self.graph.delete(plottable) |
---|
| 207 | # Create a plottable data |
---|
| 208 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
[272d91e] | 209 | self._plot_helper(new_plot=new_plot, |
---|
| 210 | reel_data=reel_data, |
---|
| 211 | extra_data=extra_data, name=name) |
---|
[c128284] | 212 | |
---|
[272d91e] | 213 | def _plot_theory(self, reel_data, extra_data, name="Unknown"): |
---|
[c128284] | 214 | """ |
---|
| 215 | Receive a data and post a NewPlotEvent to parent |
---|
| 216 | @param data: data created frome xtrapolation to plot |
---|
| 217 | @param name: Data's name to use for the legend |
---|
| 218 | """ |
---|
| 219 | if self.graph is None: |
---|
| 220 | return |
---|
| 221 | plottable = self.graph.get_plottable(name=name) |
---|
| 222 | if plottable is not None: |
---|
| 223 | self.graph.delete(plottable) |
---|
| 224 | # Create a plottable data |
---|
| 225 | new_plot = Theory1D(x=[], y=[], dy=None) |
---|
[272d91e] | 226 | self._plot_helper(new_plot=new_plot, |
---|
| 227 | reel_data=reel_data, |
---|
| 228 | extra_data=extra_data, name=name) |
---|
[c128284] | 229 | |
---|
[272d91e] | 230 | def _plot_helper(self, new_plot, reel_data, extra_data, name): |
---|
[c128284] | 231 | """ |
---|
[272d91e] | 232 | Put value of extra_data (created data) into a new plottable |
---|
| 233 | (new_plot) and assign value of a plotpael of the reel data to |
---|
| 234 | the new plottable so that the new plottable is plot on the sama panel |
---|
| 235 | of the data used for invariant. |
---|
[c128284] | 236 | """ |
---|
[272d91e] | 237 | |
---|
| 238 | new_plot.copy_from_datainfo(extra_data) |
---|
| 239 | extra_data.clone_without_data(clone=new_plot) |
---|
[c128284] | 240 | |
---|
| 241 | new_plot.name = name |
---|
[272d91e] | 242 | title = reel_data.name |
---|
| 243 | new_plot.xaxis(reel_data._xaxis, reel_data._xunit) |
---|
| 244 | new_plot.yaxis(reel_data._yaxis, reel_data._yunit) |
---|
| 245 | new_plot.group_id = reel_data.group_id |
---|
| 246 | new_plot.id = reel_data.id + name |
---|
[c128284] | 247 | ##post data to plot |
---|
| 248 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title)) |
---|
| 249 | |
---|