source: trunk/python/casatoolbar.py@ 1863

Last change on this file since 1863 was 1826, checked in by Malte Marquarding, 14 years ago

Tidy up of imports (now imported from asap.). Also fixed some whitespace/tab issues

File size: 8.9 KB
Line 
1import os
2import matplotlib
3######################################
4## Add CASA custom toolbar ##
5######################################
6class CustomToolbarCommon:
7 def __init__(self,parent):
8 self.plotter=parent
9 #self.figmgr=self.plotter._plotter.figmgr
10
11 ### select the nearest spectrum in pick radius
12 ### and display spectral value on the toolbar.
13 def _select_spectrum(self,event):
14 # Do not fire event when in zooming/panning mode
15 mode = self.figmgr.toolbar.mode
16 if not mode == '':
17 return
18 # When selected point is out of panels
19 if event.inaxes == None:
20 return
21 # If not left button
22 if event.button != 1:
23 return
24
25 xclick=event.xdata
26 yclick=event.ydata
27 dist2=1000.
28 pickline=None
29 # If the pannel has picable objects
30 pflag=False
31 for lin in event.inaxes.lines:
32 if not lin.pickable():
33 continue
34 pflag=True
35 flag,pind = lin.contains(event)
36 if not flag:
37 continue
38 # Get nearest point
39 inds = pind['ind']
40 xlin = lin.get_xdata()
41 ylin = lin.get_ydata()
42 for i in inds:
43 d2=(xlin[i]-xclick)**2+(ylin[i]-yclick)**2
44 if dist2 >= d2:
45 dist2 = d2
46 pickline = lin
47 # No pickcable line in the pannel
48 if not pflag:
49 return
50 # Pickable but too far from mouse position
51 elif pickline is None:
52 picked='No line selected.'
53 self.figmgr.toolbar.set_message(picked)
54 return
55 del pind, inds, xlin, ylin
56 # Spectra are Picked
57 theplot = self.plotter._plotter
58 thetoolbar = self.figmgr.toolbar
59 thecanvas = self.figmgr.canvas
60 # Disconnect the default motion notify event
61 # Notice! the other buttons are also diabled!!!
62 thecanvas.mpl_disconnect(thetoolbar._idDrag)
63 # Get picked spectrum
64 xdata = pickline.get_xdata()
65 ydata = pickline.get_ydata()
66 titl=pickline.get_label()
67 titp=event.inaxes.title.get_text()
68 panel0=event.inaxes
69 picked="Selected: '"+titl+"' in panel '"+titp+"'."
70 thetoolbar.set_message(picked)
71 # Generate a navigation window
72 #naviwin=Navigationwindow(titp,titl)
73 #------------------------------------------------------#
74 # Show spectrum data at mouse position
75 def spec_data(event):
76 # Getting spectrum data of neiboring point
77 xclick=event.xdata
78 if event.inaxes != panel0:
79 return
80 ipoint=len(xdata)-1
81 for i in range(len(xdata)-1):
82 xl=xclick-xdata[i]
83 xr=xclick-xdata[i+1]
84 if xl*xr <= 0.:
85 ipoint = i
86 break
87 # Output spectral value on the navigation window
88 posi='[ %s, %s ]: x = %.2f value = %.2f'\
89 %(titl,titp,xdata[ipoint],ydata[ipoint])
90 #naviwin.posi.set(posi)
91 thetoolbar.set_message(posi)
92 #------------------------------------------------------#
93 # Disconnect from mouse events
94 def discon(event):
95 #naviwin.window.destroy()
96 theplot.register('motion_notify',None)
97 # Re-activate the default motion_notify_event
98 thetoolbar._idDrag=thecanvas.mpl_connect('motion_notify_event',
99 thetoolbar.mouse_move)
100 theplot.register('button_release',None)
101 return
102 #------------------------------------------------------#
103 # Show data value along with mouse movement
104 theplot.register('motion_notify',spec_data)
105 # Finish events when mouse button is released
106 theplot.register('button_release',discon)
107
108
109 ### Calculate statistics of the selected area.
110 def _single_mask(self,event):
111 # Do not fire event when in zooming/panning mode
112 if not self.figmgr.toolbar.mode == '':
113 return
114 # When selected point is out of panels
115 if event.inaxes == None:
116 return
117 if event.button ==1:
118 baseinv=True
119 elif event.button == 3:
120 baseinv=False
121 else:
122 return
123
124 def _calc_stats():
125 msk=mymask.get_mask()
126 mymask.scan.stats(stat='max',mask=msk)
127 mymask.scan.stats(stat='min',mask=msk)
128 mymask.scan.stats(stat='sum',mask=msk)
129 mymask.scan.stats(stat='mean',mask=msk)
130 mymask.scan.stats(stat='median',mask=msk)
131 mymask.scan.stats(stat='rms',mask=msk)
132 mymask.scan.stats(stat='stddev',mask=msk)
133
134 # Interactive mask definition
135 from asap.interactivemask import interactivemask
136 mymask=interactivemask(plotter=self.plotter,scan=self.plotter._data)
137 # Create initial mask
138 mymask.set_basemask(invert=baseinv)
139 # Inherit event
140 mymask.set_startevent(event)
141 # Set callback func
142 mymask.set_callback(_calc_stats)
143 # Selected mask
144 mymask.select_mask(once=True,showmask=False)
145
146#####################################
147## Backend dependent Classes ##
148#####################################
149### TkAgg
150if matplotlib.get_backend() == 'TkAgg':
151 import Tkinter as Tk
152
153class CustomToolbarTkAgg(CustomToolbarCommon, Tk.Frame):
154 def __init__(self,parent):
155 from asap.asapplotter import asapplotter
156 if not isinstance(parent,asapplotter):
157 return False
158 if not parent._plotter:
159 return False
160 self._p=parent._plotter
161 self.figmgr=self._p.figmgr
162 self.canvas=self.figmgr.canvas
163 self.mode=''
164 self.button=True
165 self._add_custom_toolbar()
166 CustomToolbarCommon.__init__(self,parent)
167
168 def _add_custom_toolbar(self):
169 Tk.Frame.__init__(self,master=self.figmgr.window)
170 self.bSpec=self._NewButton(master=self,
171 text='spec value',
172 command=self.spec_show)
173 self.bStat=self._NewButton(master=self,
174 text='statistics',
175 command=self.stat_cal)
176 self.bQuit=self._NewButton(master=self,
177 text='Quit',
178 command=self.quit,
179 side=Tk.RIGHT)
180 self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
181
182 self.disable_button()
183 return #self
184
185 def _NewButton(self, master, text, command, side=Tk.LEFT):
186 if os.uname()[0] == 'Darwin':
187 b = Tk.Button(master=master, text=text, command=command)
188 else:
189 b = Tk.Button(master=master, text=text, padx=2, pady=2,
190 command=command)
191 b.pack(side=side)
192 return b
193
194 def spec_show(self):
195 if not self.figmgr.toolbar.mode == '' or not self.button: return
196 self.figmgr.toolbar.set_message('spec value: drag on a spec')
197 if self.mode == 'spec': return
198 self.bStat.config(relief='raised')
199 self.bSpec.config(relief='sunken')
200 self.mode='spec'
201 self.__disconnect_event()
202 #self.canvas.mpl_connect('button_press_event',self._select_spectrum)
203 self._p.register('button_press',self._select_spectrum)
204
205 def stat_cal(self):
206 if not self.figmgr.toolbar.mode == '' or not self.button: return
207 self.figmgr.toolbar.set_message('statistics: select a region')
208 if self.mode == 'stat': return
209 self.bSpec.config(relief='raised')
210 self.bStat.config(relief='sunken')
211 self.mode='stat'
212 self.__disconnect_event()
213 self._p.register('button_press',self._single_mask)
214
215 def quit(self):
216 self.__disconnect_event()
217 #self.delete_bar()
218 self.disable_button()
219 self.figmgr.window.wm_withdraw()
220
221 def enable_button(self):
222 if self.button: return
223 self.bSpec.config(state=Tk.NORMAL)
224 self.bStat.config(state=Tk.NORMAL)
225 self.button=True
226 self.spec_show()
227
228 def disable_button(self):
229 if not self.button: return
230 self.bStat.config(relief='raised', state=Tk.DISABLED)
231 self.bSpec.config(relief='raised', state=Tk.DISABLED)
232 self.button=False
233 self.mode=''
234 self.__disconnect_event()
235
236 def delete_bar(self):
237 self.__disconnect_event()
238 self.destroy()
239
240 def __disconnect_event(self):
241 #idP=self.figmgr.toolbar._idPress
242 #idR=self.figmgr.toolbar._idRelease
243 #if idP is not None:
244 # self.canvas.mpl_disconnect(idP)
245 # self.figmgr.toolbar._idPress=None
246 #if idR is not None:
247 # self.canvas.mpl_disconnect(idR)
248 # self.figmgr.toolbar._idRelease=None
249 self._p.register('button_press',None)
250 self._p.register('button_release',None)
Note: See TracBrowser for help on using the repository browser.