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

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 c128284 was c128284, checked in by Gervaise Alina <gervyh@…>, 15 years ago

invariant view

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