1 | import os
|
---|
2 | import matplotlib, numpy
|
---|
3 | from asap.logging import asaplog, asaplog_post_dec
|
---|
4 |
|
---|
5 | ######################################
|
---|
6 | ## Add CASA custom toolbar ##
|
---|
7 | ######################################
|
---|
8 | class CustomToolbarCommon:
|
---|
9 | def __init__(self,parent):
|
---|
10 | self.plotter=parent
|
---|
11 | #self.figmgr=self.plotter._plotter.figmgr
|
---|
12 |
|
---|
13 | ### select the nearest spectrum in pick radius
|
---|
14 | ### and display spectral value on the toolbar.
|
---|
15 | def _select_spectrum(self,event):
|
---|
16 | # Do not fire event when in zooming/panning mode
|
---|
17 | mode = self.figmgr.toolbar.mode
|
---|
18 | if not mode == '':
|
---|
19 | return
|
---|
20 | # When selected point is out of panels
|
---|
21 | if event.inaxes == None:
|
---|
22 | return
|
---|
23 | # If not left button
|
---|
24 | if event.button != 1:
|
---|
25 | return
|
---|
26 |
|
---|
27 | xclick=event.xdata
|
---|
28 | yclick=event.ydata
|
---|
29 | dist2=1000.
|
---|
30 | pickline=None
|
---|
31 | # If the pannel has picable objects
|
---|
32 | pflag=False
|
---|
33 | for lin in event.inaxes.lines:
|
---|
34 | if not lin.pickable():
|
---|
35 | continue
|
---|
36 | pflag=True
|
---|
37 | flag,pind = lin.contains(event)
|
---|
38 | if not flag:
|
---|
39 | continue
|
---|
40 | # Get nearest point
|
---|
41 | inds = pind['ind']
|
---|
42 | xlin = lin.get_xdata()
|
---|
43 | ylin = lin.get_ydata()
|
---|
44 | for i in inds:
|
---|
45 | d2=(xlin[i]-xclick)**2+(ylin[i]-yclick)**2
|
---|
46 | if dist2 >= d2:
|
---|
47 | dist2 = d2
|
---|
48 | pickline = lin
|
---|
49 | # No pickcable line in the pannel
|
---|
50 | if not pflag:
|
---|
51 | return
|
---|
52 | # Pickable but too far from mouse position
|
---|
53 | elif pickline is None:
|
---|
54 | picked='No line selected.'
|
---|
55 | self.figmgr.toolbar.set_message(picked)
|
---|
56 | return
|
---|
57 | del pind, inds, xlin, ylin
|
---|
58 | # Spectra are Picked
|
---|
59 | theplot = self.plotter._plotter
|
---|
60 | thetoolbar = self.figmgr.toolbar
|
---|
61 | thecanvas = self.figmgr.canvas
|
---|
62 | # Disconnect the default motion notify event
|
---|
63 | # Notice! the other buttons are also diabled!!!
|
---|
64 | thecanvas.mpl_disconnect(thetoolbar._idDrag)
|
---|
65 | # Get picked spectrum
|
---|
66 | xdata = pickline.get_xdata()
|
---|
67 | ydata = pickline.get_ydata()
|
---|
68 | titl=pickline.get_label()
|
---|
69 | titp=event.inaxes.title.get_text()
|
---|
70 | panel0=event.inaxes
|
---|
71 | picked="Selected: '"+titl+"' in panel '"+titp+"'."
|
---|
72 | thetoolbar.set_message(picked)
|
---|
73 | # Generate a navigation window
|
---|
74 | #naviwin=Navigationwindow(titp,titl)
|
---|
75 | #------------------------------------------------------#
|
---|
76 | # Show spectrum data at mouse position
|
---|
77 | def spec_data(event):
|
---|
78 | # Getting spectrum data of neiboring point
|
---|
79 | xclick=event.xdata
|
---|
80 | if event.inaxes != panel0:
|
---|
81 | return
|
---|
82 | ipoint=len(xdata)-1
|
---|
83 | for i in range(len(xdata)-1):
|
---|
84 | xl=xclick-xdata[i]
|
---|
85 | xr=xclick-xdata[i+1]
|
---|
86 | if xl*xr <= 0.:
|
---|
87 | ipoint = i
|
---|
88 | break
|
---|
89 | # Output spectral value on the navigation window
|
---|
90 | posi='[ %s, %s ]: x = %.2f value = %.2f'\
|
---|
91 | %(titl,titp,xdata[ipoint],ydata[ipoint])
|
---|
92 | #naviwin.posi.set(posi)
|
---|
93 | thetoolbar.set_message(posi)
|
---|
94 | #------------------------------------------------------#
|
---|
95 | # Disconnect from mouse events
|
---|
96 | def discon(event):
|
---|
97 | #naviwin.window.destroy()
|
---|
98 | theplot.register('motion_notify',None)
|
---|
99 | # Re-activate the default motion_notify_event
|
---|
100 | thetoolbar._idDrag=thecanvas.mpl_connect('motion_notify_event',
|
---|
101 | thetoolbar.mouse_move)
|
---|
102 | theplot.register('button_release',None)
|
---|
103 | return
|
---|
104 | #------------------------------------------------------#
|
---|
105 | # Show data value along with mouse movement
|
---|
106 | theplot.register('motion_notify',spec_data)
|
---|
107 | # Finish events when mouse button is released
|
---|
108 | theplot.register('button_release',discon)
|
---|
109 |
|
---|
110 |
|
---|
111 | ### Calculate statistics of the selected area.
|
---|
112 | def _single_mask(self,event):
|
---|
113 | # Do not fire event when in zooming/panning mode
|
---|
114 | if not self.figmgr.toolbar.mode == '':
|
---|
115 | return
|
---|
116 | # When selected point is out of panels
|
---|
117 | if event.inaxes == None:
|
---|
118 | return
|
---|
119 | if event.button ==1:
|
---|
120 | baseinv=True
|
---|
121 | elif event.button == 3:
|
---|
122 | baseinv=False
|
---|
123 | else:
|
---|
124 | return
|
---|
125 |
|
---|
126 | def _calc_stats():
|
---|
127 | msk=mymask.get_mask()
|
---|
128 | mymask.scan.stats(stat='max',mask=msk)
|
---|
129 | mymask.scan.stats(stat='min',mask=msk)
|
---|
130 | mymask.scan.stats(stat='sum',mask=msk)
|
---|
131 | mymask.scan.stats(stat='mean',mask=msk)
|
---|
132 | mymask.scan.stats(stat='median',mask=msk)
|
---|
133 | mymask.scan.stats(stat='rms',mask=msk)
|
---|
134 | mymask.scan.stats(stat='stddev',mask=msk)
|
---|
135 |
|
---|
136 | # Interactive mask definition
|
---|
137 | from asap.interactivemask import interactivemask
|
---|
138 | mymask=interactivemask(plotter=self.plotter,scan=self.plotter._data)
|
---|
139 | # Create initial mask
|
---|
140 | mymask.set_basemask(invert=baseinv)
|
---|
141 | # Inherit event
|
---|
142 | mymask.set_startevent(event)
|
---|
143 | # Set callback func
|
---|
144 | mymask.set_callback(_calc_stats)
|
---|
145 | # Selected mask
|
---|
146 | mymask.select_mask(once=True,showmask=False)
|
---|
147 |
|
---|
148 | def _mod_note(self,event):
|
---|
149 | # Do not fire event when in zooming/panning mode
|
---|
150 | if not self.figmgr.toolbar.mode == '':
|
---|
151 | return
|
---|
152 | if event.button ==1:
|
---|
153 | self.notewin.load_textwindow(event)
|
---|
154 | elif event.button == 3 and self._note_picked(event):
|
---|
155 | self.notewin.load_modmenu(event)
|
---|
156 | return
|
---|
157 |
|
---|
158 | def _note_picked(self,event):
|
---|
159 | # just briefly check if any texts are picked
|
---|
160 | for textobj in self.canvas.figure.texts:
|
---|
161 | if textobj.contains(event)[0]:
|
---|
162 | return True
|
---|
163 | for ax in self.canvas.figure.axes:
|
---|
164 | for textobj in ax.texts:
|
---|
165 | if textobj.contains(event)[0]:
|
---|
166 | return True
|
---|
167 | #print "No text picked"
|
---|
168 | return False
|
---|
169 |
|
---|
170 | ### Page chages
|
---|
171 | ### go to the previous page
|
---|
172 | def prev_page(self):
|
---|
173 | self.figmgr.toolbar.set_message('plotting the previous page')
|
---|
174 | self._new_page(goback=True)
|
---|
175 |
|
---|
176 | ### go to the next page
|
---|
177 | def next_page(self):
|
---|
178 | self.figmgr.toolbar.set_message('plotting the next page')
|
---|
179 | self._new_page(goback=False)
|
---|
180 |
|
---|
181 | ### actual plotting of the new page
|
---|
182 | def _new_page(self,goback=False):
|
---|
183 | if self.plotter._startrow <= 0:
|
---|
184 | msg = "The page counter is reset due to chages of plot settings. "
|
---|
185 | msg += "Plotting from the first page."
|
---|
186 | asaplog.push(msg)
|
---|
187 | asaplog.post('WARN')
|
---|
188 | goback = False
|
---|
189 |
|
---|
190 | self.plotter._plotter.hold()
|
---|
191 | if goback:
|
---|
192 | self._set_prevpage_counter()
|
---|
193 | #self.plotter._plotter.clear()
|
---|
194 | self.plotter._plot(self.plotter._data)
|
---|
195 | self.set_pagecounter(self._get_pagenum())
|
---|
196 | self.plotter._plotter.release()
|
---|
197 | self.plotter._plotter.tidy()
|
---|
198 | self.plotter._plotter.show(hardrefresh=False)
|
---|
199 |
|
---|
200 | ### calculate the panel ID and start row to plot the previous page
|
---|
201 | def _set_prevpage_counter(self):
|
---|
202 | # set row and panel counters to those of the 1st panel of previous page
|
---|
203 | maxpanel = 16
|
---|
204 | # the ID of the last panel in current plot
|
---|
205 | lastpanel = self.plotter._ipanel
|
---|
206 | # the number of current subplots
|
---|
207 | currpnum = len(self.plotter._plotter.subplots)
|
---|
208 | # the nuber of previous subplots
|
---|
209 | prevpnum = None
|
---|
210 | if self.plotter._rows and self.plotter._cols:
|
---|
211 | # when user set layout
|
---|
212 | prevpnum = self.plotter._rows*self.plotter._cols
|
---|
213 | else:
|
---|
214 | # no user specification
|
---|
215 | prevpnum = maxpanel
|
---|
216 |
|
---|
217 | start_ipanel = max(lastpanel-currpnum-prevpnum+1, 0)
|
---|
218 | # set the pannel ID of the last panel of prev-prev page
|
---|
219 | self.plotter._ipanel = start_ipanel-1
|
---|
220 | if self.plotter._panelling == 'r':
|
---|
221 | self.plotter._startrow = start_ipanel
|
---|
222 | else:
|
---|
223 | # the start row number of the next panel
|
---|
224 | self.plotter._startrow = self.plotter._panelrows[start_ipanel]
|
---|
225 | del lastpanel,currpnum,prevpnum,start_ipanel
|
---|
226 |
|
---|
227 | ### refresh the page counter
|
---|
228 | ### refresh the page counter
|
---|
229 | def set_pagecounter(self,page):
|
---|
230 | nwidth = int(numpy.ceil(numpy.log10(max(page,1))))+1
|
---|
231 | nwidth = max(nwidth,4)
|
---|
232 | formatstr = '%'+str(nwidth)+'d'
|
---|
233 | self.show_pagenum(page,formatstr)
|
---|
234 |
|
---|
235 | def show_pagenum(self,pagenum,formatstr):
|
---|
236 | # passed to backend dependent class
|
---|
237 | pass
|
---|
238 |
|
---|
239 | def _get_pagenum(self):
|
---|
240 | maxpanel = 16
|
---|
241 | # get the ID of last panel in the current page
|
---|
242 | idlastpanel = self.plotter._ipanel
|
---|
243 | if self.plotter._rows and self.plotter._cols:
|
---|
244 | ppp = self.plotter._rows*self.plotter._cols
|
---|
245 | else:
|
---|
246 | ppp = maxpanel
|
---|
247 | return int(idlastpanel/ppp)+1
|
---|
248 |
|
---|
249 | #####################################
|
---|
250 | ## Backend dependent Classes ##
|
---|
251 | #####################################
|
---|
252 | ### TkAgg
|
---|
253 | if matplotlib.get_backend() == 'TkAgg':
|
---|
254 | import Tkinter as Tk
|
---|
255 | from notationwindow import NotationWindowTkAgg
|
---|
256 |
|
---|
257 | class CustomToolbarTkAgg(CustomToolbarCommon, Tk.Frame):
|
---|
258 | def __init__(self,parent):
|
---|
259 | from asap.asapplotter import asapplotter
|
---|
260 | if not isinstance(parent,asapplotter):
|
---|
261 | return False
|
---|
262 | if not parent._plotter:
|
---|
263 | return False
|
---|
264 | self._p = parent._plotter
|
---|
265 | self.figmgr = self._p.figmgr
|
---|
266 | self.canvas = self.figmgr.canvas
|
---|
267 | self.mode = ''
|
---|
268 | self.button = True
|
---|
269 | self.pagecount = None
|
---|
270 | CustomToolbarCommon.__init__(self,parent)
|
---|
271 | self.notewin=NotationWindowTkAgg(master=self.canvas)
|
---|
272 | self._add_custom_toolbar()
|
---|
273 |
|
---|
274 | def _add_custom_toolbar(self):
|
---|
275 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
---|
276 | #self.bSpec=self._NewButton(master=self,
|
---|
277 | # text='spec value',
|
---|
278 | # command=self.spec_show)
|
---|
279 | self.bNote=self._NewButton(master=self,
|
---|
280 | text='notation',
|
---|
281 | command=self.modify_note)
|
---|
282 |
|
---|
283 | self.bStat=self._NewButton(master=self,
|
---|
284 | text='statistics',
|
---|
285 | command=self.stat_cal)
|
---|
286 | self.bQuit=self._NewButton(master=self,
|
---|
287 | text='Quit',
|
---|
288 | command=self.quit,
|
---|
289 | side=Tk.RIGHT)
|
---|
290 |
|
---|
291 | # page change oparations
|
---|
292 | frPage = Tk.Frame(master=self,borderwidth=2,relief=Tk.GROOVE)
|
---|
293 | frPage.pack(ipadx=2,padx=10,side=Tk.RIGHT)
|
---|
294 | self.lPagetitle = Tk.Label(master=frPage,text='Page:',padx=5)
|
---|
295 | self.lPagetitle.pack(side=Tk.LEFT)
|
---|
296 | self.pagecount = Tk.StringVar(master=frPage)
|
---|
297 | self.lPagecount = Tk.Label(master=frPage,
|
---|
298 | textvariable=self.pagecount,
|
---|
299 | padx=5,bg='white')
|
---|
300 | self.lPagecount.pack(side=Tk.LEFT,padx=3)
|
---|
301 |
|
---|
302 | self.bNext=self._NewButton(master=frPage,
|
---|
303 | text=' + ',
|
---|
304 | command=self.next_page)
|
---|
305 | self.bPrev=self._NewButton(master=frPage,
|
---|
306 | text=' - ',
|
---|
307 | command=self.prev_page)
|
---|
308 |
|
---|
309 | if os.uname()[0] != 'Darwin':
|
---|
310 | self.bPrev.config(padx=5)
|
---|
311 | self.bNext.config(padx=5)
|
---|
312 |
|
---|
313 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
---|
314 | self.pagecount.set(' '*4)
|
---|
315 |
|
---|
316 | self.disable_button()
|
---|
317 | return #self
|
---|
318 |
|
---|
319 | def _NewButton(self, master, text, command, side=Tk.LEFT):
|
---|
320 | if os.uname()[0] == 'Darwin':
|
---|
321 | b = Tk.Button(master=master, text=text, command=command)
|
---|
322 | else:
|
---|
323 | b = Tk.Button(master=master, text=text, padx=2, pady=2,
|
---|
324 | command=command)
|
---|
325 | b.pack(side=side)
|
---|
326 | return b
|
---|
327 |
|
---|
328 | def show_pagenum(self,pagenum,formatstr):
|
---|
329 | self.pagecount.set(formatstr % (pagenum))
|
---|
330 |
|
---|
331 | def spec_show(self):
|
---|
332 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
333 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
---|
334 | if self.mode == 'spec': return
|
---|
335 | #self.bStat.config(relief='raised')
|
---|
336 | #self.bSpec.config(relief='sunken')
|
---|
337 | #self.bNote.config(relief='raised')
|
---|
338 | self.mode='spec'
|
---|
339 | self.notewin.close_widgets()
|
---|
340 | self.__disconnect_event()
|
---|
341 | self._p.register('button_press',self._select_spectrum)
|
---|
342 |
|
---|
343 | def stat_cal(self):
|
---|
344 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
345 | self.figmgr.toolbar.set_message('statistics: select a region')
|
---|
346 | if self.mode == 'stat':
|
---|
347 | # go back to spec mode
|
---|
348 | self.bStat.config(relief='raised')
|
---|
349 | self.spec_show()
|
---|
350 | return
|
---|
351 | #self.bSpec.config(relief='raised')
|
---|
352 | self.bStat.config(relief='sunken')
|
---|
353 | self.bNote.config(relief='raised')
|
---|
354 | self.mode='stat'
|
---|
355 | self.notewin.close_widgets()
|
---|
356 | self.__disconnect_event()
|
---|
357 | self._p.register('button_press',self._single_mask)
|
---|
358 |
|
---|
359 | def modify_note(self):
|
---|
360 | if not self.figmgr.toolbar.mode == '': return
|
---|
361 | self.figmgr.toolbar.set_message('text: select a position/text')
|
---|
362 | if self.mode == 'note':
|
---|
363 | self.bNote.config(relief='raised')
|
---|
364 | self.mode='none'
|
---|
365 | self.spec_show()
|
---|
366 | return
|
---|
367 | #self.bSpec.config(relief='raised')
|
---|
368 | self.bStat.config(relief='raised')
|
---|
369 | self.bNote.config(relief='sunken')
|
---|
370 | self.mode='note'
|
---|
371 | self.__disconnect_event()
|
---|
372 | self._p.register('button_press',self._mod_note)
|
---|
373 |
|
---|
374 | def quit(self):
|
---|
375 | self.__disconnect_event()
|
---|
376 | #self.delete_bar()
|
---|
377 | self.disable_button()
|
---|
378 | self.figmgr.window.wm_withdraw()
|
---|
379 |
|
---|
380 | def enable_button(self):
|
---|
381 | if self.button: return
|
---|
382 | #self.bSpec.config(state=Tk.NORMAL)
|
---|
383 | self.bStat.config(state=Tk.NORMAL)
|
---|
384 | self.button=True
|
---|
385 | self.spec_show()
|
---|
386 |
|
---|
387 | def disable_button(self):
|
---|
388 | if not self.button: return
|
---|
389 | #self.bSpec.config(relief='raised', state=Tk.DISABLED)
|
---|
390 | self.bStat.config(relief='raised', state=Tk.DISABLED)
|
---|
391 | #self.bNext.config(state=Tk.DISABLED)
|
---|
392 | #self.bPrev.config(state=Tk.DISABLED)
|
---|
393 | self.button=False
|
---|
394 | self.mode=''
|
---|
395 | self.__disconnect_event()
|
---|
396 |
|
---|
397 | def enable_next(self):
|
---|
398 | self.bNext.config(state=Tk.NORMAL)
|
---|
399 |
|
---|
400 | def disable_next(self):
|
---|
401 | self.bNext.config(state=Tk.DISABLED)
|
---|
402 |
|
---|
403 | def enable_prev(self):
|
---|
404 | self.bPrev.config(state=Tk.NORMAL)
|
---|
405 |
|
---|
406 | def disable_prev(self):
|
---|
407 | self.bPrev.config(state=Tk.DISABLED)
|
---|
408 |
|
---|
409 | def delete_bar(self):
|
---|
410 | self.__disconnect_event()
|
---|
411 | self.destroy()
|
---|
412 |
|
---|
413 | def __disconnect_event(self):
|
---|
414 | self._p.register('button_press',None)
|
---|
415 | self._p.register('button_release',None)
|
---|