source: sasview/invariantview/perspectives/invariant/invariant.py @ d84a90c

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since d84a90c was d7a39e5, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

  • Property mode set to 100644
File size: 8.9 KB
Line 
1
2
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
14import wx
15
16from sans.invariant import invariant
17
18from DataLoader.data_info import Data1D as LoaderData1D
19from sans.guiframe.dataFitting import Theory1D, Data1D
20
21from sans.guicomm.events import NewPlotEvent, StatusEvent
22from sans.guicomm.events import ERR_DATA
23
24class Plugin:
25    """
26    This class defines the interface for invariant Plugin class
27    that can be used by the gui_manager.
28         
29    """
30   
31    def __init__(self, standalone=False):
32        """
33        Abstract class for gui_manager Plugins.
34        """
35        ## Plug-in name. It will appear on the application menu.
36        self.sub_menu = "Invariant"
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        """
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       
56        """
57        return []
58   
59    def help(self, evt):
60        """
61        Show a general help dialog.
62   
63        """
64        from help_panel import  HelpWindow
65        frame = HelpWindow(None, -1)   
66        frame.Show(True)
67       
68    def get_panels(self, parent):
69        """
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       
81        """
82        ## Save a reference to the parent
83        self.parent = parent
84        #add error back to the data
85        self.parent.Bind(ERR_DATA, self._on_data_error)
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        """
95        Returns a set of menu entries for tools
96        """
97        return []
98       
99   
100    def get_context_menu(self, graph=None):
101        """
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
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:
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]]
134        return []   
135
136   
137    def get_perspective(self):
138        """
139        Get the list of panel names for this perspective
140        """
141        return self.perspective
142   
143    def on_perspective(self, event):
144        """
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       
151        """
152        self.parent.set_perspective(self.perspective)
153   
154    def post_init(self):
155        """
156        Post initialization call back to close the loose ends
157        """
158        pass
159   
160    def set_default_perspective(self):
161        """
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
166        """
167        return False
168   
169    def copy_data(self, item, dy=None):
170        """
171        receive a data 1D and the list of errors on dy
172        and create a new data1D data
173       
174        """
175        id = None
176        if hasattr(item,"id"):
177            id = item.id
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
185        data.interactive = item.interactive
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        """
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
195        """
196        self.err_dy = event.err_dy
197       
198    def _compute_invariant(self, event):   
199        """
200        Open the invariant panel to invariant computation
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
215                # Store reference to data
216                self.__data = data
217                # Set the data set to be user for invariant calculation
218                self.invariant_panel.set_data(data=data)
219       
220    def plot_theory(self, data=None, name=None):
221        """
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       
227        """
228        if data is None:
229            new_plot = Theory1D(x=[], y=[], dy=None)
230        else:
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
236                data.y = (data.y + background)/scale
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
242        new_plot.name = name
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))
248       
249    def plot_data(self, scale, background):
250        """
251        replot the current data if the user enters a new scale or background
252        """
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
257        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=new_plot.name))
258       
Note: See TracBrowser for help on using the repository browser.