source: sasview/guiframe/local_perspectives/plotting/plotting.py @ 5c4b674

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

working on data panel

  • Property mode set to 100644
File size: 6.7 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 2008, University of Tennessee
12################################################################################
13
14import wx
15import sys
16from sans.guiframe.events import EVT_NEW_PLOT
17from sans.guiframe.events import StatusEvent
18from sans.guiframe.plugin_base import PluginBase
19from sans.guiframe.dataFitting import Data1D
20from sans.guiframe.dataFitting import Data2D
21
22class Plugin(PluginBase):
23    """
24    Plug-in class to be instantiated by the GUI manager
25    """
26   
27    def __init__(self, standalone=False):
28        PluginBase.__init__(self, name="Plotting", standalone=standalone)
29     
30        ## Plot panels
31        self.plot_panels = []
32        self.new_plot_panels = {}
33        self._panel_on_focus = None
34     
35    def set_panel_on_focus(self, panel):
36        """
37        """
38        self._panel_on_focus = panel
39       
40    def is_always_active(self):
41        """
42        return True is this plugin is always active even if the user is
43        switching between perspectives
44        """
45        return True
46   
47    def populate_menu(self, parent):
48        """
49        Create a 'Plot' menu to list the panels
50        available for displaying
51       
52        :param id: next available unique ID for wx events
53        :param parent: parent window
54       
55        """
56        self.menu = wx.Menu()
57       
58        self.menu.Append(wx.NewId(), "No plot available", 
59                             "No plot available")
60        self.menu.FindItemByPosition(0).Enable(False)
61        return [(self.menu, "Plot")]
62   
63    def get_panels(self, parent):
64        """
65        Create and return a list of panel objects
66        """
67        ## Save a reference to the parent
68        self.parent = parent
69        # Connect to plotting events
70        self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event)
71        # We have no initial panels for this plug-in
72        return []
73   
74    def _on_show_panel(self, event):
75        """show plug-in panel"""
76        pass
77   
78    #def _on_plot_event(self, event):
79    #     return profile(self.tested_on_plot_event, event)
80   
81    def _on_plot_event(self, event):
82        """
83        A new plottable is being shipped to the plotting plug-in.
84        Check whether we have a panel to put in on, or create
85        a new one
86       
87        :param event: EVT_NEW_PLOT event
88       
89        """
90        if hasattr(event, 'remove'):
91            group_id = event.group_id
92            id = event.id
93            if group_id in self.new_plot_panels.keys():
94                panel = self.new_plot_panels[group_id]
95                panel.remove_data_by_id(id=id)
96            else:
97                msg = "Panel with GROUP_ID: %s canot be located" % str(group_id)
98                raise ValueError, msg
99            return
100        # Check whether we already have a graph with the same units
101        # as the plottable we just received.
102        data = event.plot
103       
104        group_id_list = data.group_id
105        group_id = None
106        if group_id_list:
107            group_id = group_id_list[len(group_id_list)-1]
108        if group_id in self.new_plot_panels.keys():
109            panel = self.new_plot_panels[group_id]
110            _, x_unit =  data.get_xaxis()
111            _, y_unit =  data.get_yaxis()
112           
113            if x_unit != panel.graph.prop["xunit"] \
114                or  y_unit != panel.graph.prop["yunit"]:
115                msg = "Cannot add %s" % str(data.name)
116                msg += " to panel %s\n" % str(panel.window_caption)
117                msg += "Please edit %s's units, labels" % str(data.name)
118                raise ValueError, msg
119            else:
120                panel.plot_data( data)
121                self.parent.show_panel(panel.uid)   
122        else:
123            # Create a new plot panel if none was available       
124            if issubclass(data.__class__, Data1D):
125                from Plotter1D import ModelPanel1D
126                ## get the data representation label of the data to plot
127                ## when even the user select "change scale"
128                xtransform = data.xtransform
129                ytransform = data.ytransform
130                ## create a plotpanel for 1D Data
131                new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform,
132                         ytransform=ytransform, style=wx.RAISED_BORDER)
133                new_panel.set_manager(self)
134                new_panel.group_id = group_id
135            elif issubclass(data.__class__, Data2D):
136                ##Create a new plotpanel for 2D data
137                from Plotter2D import ModelPanel2D
138                scale = data.scale
139                new_panel = ModelPanel2D(self.parent, id = -1,
140                                    data2d=event.plot, scale = scale, 
141                                    style=wx.RAISED_BORDER)
142                new_panel.set_manager(self)
143                new_panel.group_id = group_id
144            else:
145                msg = "Plotting received unexpected"
146                msg += " data type : %s" % str(data.__class__)
147                raise ValueError, msg
148            ## Set group ID if available
149            ## Assign data properties to the new create panel
150            title = data.title
151            new_panel.window_caption = title
152            new_panel.window_name = title
153            event_id = self.parent.popup_panel(new_panel)
154            #remove the default item in the menu
155            if len(self.new_plot_panels) == 0:
156                self.menu.RemoveItem(self.menu.FindItemByPosition(0))
157            self.menu.Append(event_id, new_panel.window_caption, 
158                             "Show %s plot panel" % new_panel.window_caption)
159            # Set UID to allow us to reference the panel later
160            new_panel.uid = event_id
161            # Ship the plottable to its panel
162            new_panel.plot_data(data)
163            self.plot_panels.append(new_panel)       
164            self.new_plot_panels[new_panel.group_id] = new_panel
165           
166        return
167
168def profile(fn, *args, **kw):
169    import cProfile, pstats, os
170    global call_result
171    def call():
172        global call_result
173        call_result = fn(*args, **kw)
174    cProfile.runctx('call()', dict(call=call), {}, 'profile.txt')
175    stats = pstats.Stats('profile.txt')
176    stats.sort_stats('time')
177    #stats.sort_stats('calls')
178    stats.print_stats()
179    #os.unlink('profile.out')
180    return call_result
181
182   
Note: See TracBrowser for help on using the repository browser.