[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 |
---|
[6d55d81] | 11 | |
---|
[c128284] | 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. |
---|
[53b6b74] | 31 | self.sub_menu = "Invariant" |
---|
[c128284] | 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 | |
---|
[f29a433] | 52 | def help(self, evt): |
---|
| 53 | """ |
---|
| 54 | Show a general help dialog. |
---|
| 55 | TODO: replace the text with a nice image |
---|
| 56 | provide more hint on the SLD calculator |
---|
| 57 | """ |
---|
| 58 | from help_panel import HelpWindow |
---|
| 59 | frame = HelpWindow(None, -1) |
---|
| 60 | frame.Show(True) |
---|
| 61 | |
---|
[c128284] | 62 | def get_panels(self, parent): |
---|
| 63 | """ |
---|
| 64 | Create and return the list of wx.Panels for your plug-in. |
---|
| 65 | Define the plug-in perspective. |
---|
| 66 | |
---|
| 67 | Panels should inherit from DefaultPanel defined below, |
---|
| 68 | or should present the same interface. They must define |
---|
| 69 | "window_caption" and "window_name". |
---|
| 70 | |
---|
| 71 | @param parent: parent window |
---|
| 72 | @return: list of panels |
---|
| 73 | """ |
---|
| 74 | ## Save a reference to the parent |
---|
| 75 | self.parent = parent |
---|
| 76 | #add error back to the data |
---|
[272d91e] | 77 | self.parent.Bind(ERR_DATA, self._on_data_error) |
---|
[c128284] | 78 | from invariant_panel import InvariantPanel |
---|
| 79 | self.invariant_panel = InvariantPanel(parent=self.parent) |
---|
| 80 | self.invariant_panel.set_manager(manager=self) |
---|
| 81 | self.perspective.append(self.invariant_panel.window_name) |
---|
| 82 | # Return the list of panels |
---|
| 83 | return [self.invariant_panel] |
---|
| 84 | |
---|
| 85 | def get_tools(self): |
---|
| 86 | """ |
---|
| 87 | Returns a set of menu entries for tools |
---|
| 88 | """ |
---|
| 89 | return [] |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | def get_context_menu(self, graph=None): |
---|
| 93 | """ |
---|
| 94 | This method is optional. |
---|
| 95 | |
---|
| 96 | When the context menu of a plot is rendered, the |
---|
| 97 | get_context_menu method will be called to give you a |
---|
| 98 | chance to add a menu item to the context menu. |
---|
| 99 | |
---|
| 100 | A ref to a Graph object is passed so that you can |
---|
| 101 | investigate the plot content and decide whether you |
---|
| 102 | need to add items to the context menu. |
---|
| 103 | |
---|
| 104 | This method returns a list of menu items. |
---|
| 105 | Each item is itself a list defining the text to |
---|
| 106 | appear in the menu, a tool-tip help text, and a |
---|
| 107 | call-back method. |
---|
| 108 | |
---|
| 109 | @param graph: the Graph object to which we attach the context menu |
---|
| 110 | @return: a list of menu items with call-back function |
---|
| 111 | """ |
---|
| 112 | self.graph = graph |
---|
| 113 | invariant_option = "Compute invariant" |
---|
| 114 | invariant_hint = "Will displays the invariant panel for futher computation" |
---|
| 115 | |
---|
| 116 | for item in self.graph.plottables: |
---|
[518d35d] | 117 | if item.name == graph.selected_plottable : |
---|
| 118 | if issubclass(item.__class__,LoaderData1D): |
---|
| 119 | |
---|
| 120 | if item.name !="$I_{obs}(q)$" and item.name !="$P_{fit}(r)$": |
---|
| 121 | if hasattr(item, "group_id"): |
---|
| 122 | return [[invariant_option, |
---|
| 123 | invariant_hint, self._compute_invariant]] |
---|
[c128284] | 124 | return [] |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | def get_perspective(self): |
---|
| 128 | """ |
---|
| 129 | Get the list of panel names for this perspective |
---|
| 130 | """ |
---|
| 131 | return self.perspective |
---|
| 132 | |
---|
| 133 | def on_perspective(self, event): |
---|
| 134 | """ |
---|
| 135 | Call back function for the perspective menu item. |
---|
| 136 | We notify the parent window that the perspective |
---|
| 137 | has changed. |
---|
| 138 | @param event: menu event |
---|
| 139 | """ |
---|
| 140 | self.parent.set_perspective(self.perspective) |
---|
| 141 | |
---|
| 142 | def post_init(self): |
---|
| 143 | """ |
---|
| 144 | Post initialization call back to close the loose ends |
---|
| 145 | """ |
---|
| 146 | pass |
---|
| 147 | |
---|
| 148 | def set_default_perspective(self): |
---|
| 149 | """ |
---|
| 150 | Call back method that True to notify the parent that the current plug-in |
---|
| 151 | can be set as default perspective. |
---|
| 152 | when returning False, the plug-in is not candidate for an automatic |
---|
| 153 | default perspective setting |
---|
| 154 | """ |
---|
| 155 | return False |
---|
| 156 | |
---|
| 157 | def copy_data(self, item, dy=None): |
---|
| 158 | """ |
---|
| 159 | receive a data 1D and the list of errors on dy |
---|
| 160 | and create a new data1D data |
---|
| 161 | @param return |
---|
| 162 | """ |
---|
| 163 | id = None |
---|
| 164 | if hasattr(item,"id"): |
---|
[6d55d81] | 165 | id = item.id |
---|
[c128284] | 166 | |
---|
| 167 | data = Data1D(x=item.x, y=item.y, dx=None, dy=None) |
---|
| 168 | data.copy_from_datainfo(item) |
---|
| 169 | item.clone_without_data(clone=data) |
---|
| 170 | data.dy = dy |
---|
| 171 | data.name = item.name |
---|
| 172 | ## allow to highlight data when plotted |
---|
[6d55d81] | 173 | data.interactive = item.interactive |
---|
[c128284] | 174 | ## when 2 data have the same id override the 1 st plotted |
---|
| 175 | data.id = id |
---|
| 176 | data.group_id = item.group_id |
---|
| 177 | return data |
---|
| 178 | |
---|
| 179 | def _on_data_error(self, event): |
---|
| 180 | """ |
---|
| 181 | receives and event from plotting plu-gins to store the data name and |
---|
| 182 | their errors of y coordinates for 1Data hide and show error |
---|
| 183 | """ |
---|
| 184 | self.err_dy = event.err_dy |
---|
| 185 | |
---|
| 186 | def _compute_invariant(self, event): |
---|
| 187 | """ |
---|
| 188 | Open the invariant panel to invariant computation |
---|
| 189 | """ |
---|
| 190 | self.panel = event.GetEventObject() |
---|
| 191 | Plugin.on_perspective(self,event=event) |
---|
| 192 | for plottable in self.panel.graph.plottables: |
---|
| 193 | if plottable.name == self.panel.graph.selected_plottable: |
---|
| 194 | ## put the errors values back to the model if the errors were hiden |
---|
| 195 | ## before sending them to the fit engine |
---|
| 196 | if len(self.err_dy)>0: |
---|
| 197 | dy = plottable.dy |
---|
| 198 | if plottable.name in self.err_dy.iterkeys(): |
---|
| 199 | dy = self.err_dy[plottable.name] |
---|
| 200 | data = self.copy_data(plottable, dy) |
---|
| 201 | else: |
---|
| 202 | data = plottable |
---|
[2661d8b] | 203 | # Store reference to data |
---|
| 204 | self.__data = data |
---|
| 205 | # Set the data set to be user for invariant calculation |
---|
[272d91e] | 206 | self.invariant_panel.set_data(data=data) |
---|
[c128284] | 207 | |
---|
[2661d8b] | 208 | def plot_theory(self, data=None, name=None): |
---|
[c128284] | 209 | """ |
---|
[2661d8b] | 210 | Receive a data set and post a NewPlotEvent to parent. |
---|
| 211 | @param data: extrapolated data to be plotted |
---|
[c128284] | 212 | @param name: Data's name to use for the legend |
---|
| 213 | """ |
---|
[2661d8b] | 214 | if data is None: |
---|
| 215 | new_plot = Theory1D(x=[], y=[], dy=None) |
---|
| 216 | else: |
---|
[353f467] | 217 | scale =self.invariant_panel.get_scale() |
---|
| 218 | background = self.invariant_panel.get_background() |
---|
| 219 | |
---|
| 220 | if scale != 0: |
---|
| 221 | # Put back the sacle and bkg for plotting |
---|
[8f2069d5] | 222 | data.y = (data.y + background)/scale |
---|
[353f467] | 223 | new_plot = Theory1D(x=data.x, y=data.y, dy=None) |
---|
| 224 | else: |
---|
| 225 | msg = "Scale can not be zero." |
---|
| 226 | raise ValueError, msg |
---|
| 227 | |
---|
[c128284] | 228 | new_plot.name = name |
---|
[2661d8b] | 229 | new_plot.xaxis(self.__data._xaxis, self.__data._xunit) |
---|
| 230 | new_plot.yaxis(self.__data._yaxis, self.__data._yunit) |
---|
| 231 | new_plot.group_id = self.__data.group_id |
---|
| 232 | new_plot.id = self.__data.id + name |
---|
| 233 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=self.__data.name)) |
---|
[6d55d81] | 234 | |
---|
[18d0bba] | 235 | def plot_data(self, scale, background): |
---|
[6d55d81] | 236 | """ |
---|
| 237 | replot the current data if the user enters a new scale or background |
---|
| 238 | """ |
---|
[18d0bba] | 239 | new_plot = scale * self.__data - background |
---|
| 240 | new_plot.name = self.__data.name |
---|
| 241 | new_plot.group_id = self.__data.group_id |
---|
| 242 | new_plot.id = self.__data.id |
---|
[6d55d81] | 243 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=new_plot.name)) |
---|
[c128284] | 244 | |
---|