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