[1724] | 1 | import os
|
---|
| 2 | import matplotlib
|
---|
[1884] | 3 |
|
---|
[1724] | 4 | ######################################
|
---|
| 5 | ## Add CASA custom toolbar ##
|
---|
| 6 | ######################################
|
---|
| 7 | class CustomToolbarCommon:
|
---|
| 8 | def __init__(self,parent):
|
---|
| 9 | self.plotter=parent
|
---|
| 10 | #self.figmgr=self.plotter._plotter.figmgr
|
---|
| 11 |
|
---|
| 12 | ### select the nearest spectrum in pick radius
|
---|
[1826] | 13 | ### and display spectral value on the toolbar.
|
---|
[1724] | 14 | def _select_spectrum(self,event):
|
---|
| 15 | # Do not fire event when in zooming/panning mode
|
---|
| 16 | mode = self.figmgr.toolbar.mode
|
---|
[1826] | 17 | if not mode == '':
|
---|
| 18 | return
|
---|
| 19 | # When selected point is out of panels
|
---|
[1724] | 20 | if event.inaxes == None:
|
---|
[1826] | 21 | return
|
---|
[1724] | 22 | # If not left button
|
---|
| 23 | if event.button != 1:
|
---|
[1826] | 24 | return
|
---|
[1724] | 25 |
|
---|
| 26 | xclick=event.xdata
|
---|
| 27 | yclick=event.ydata
|
---|
| 28 | dist2=1000.
|
---|
| 29 | pickline=None
|
---|
[1826] | 30 | # If the pannel has picable objects
|
---|
| 31 | pflag=False
|
---|
[1724] | 32 | for lin in event.inaxes.lines:
|
---|
[1826] | 33 | if not lin.pickable():
|
---|
| 34 | continue
|
---|
| 35 | pflag=True
|
---|
| 36 | flag,pind = lin.contains(event)
|
---|
| 37 | if not flag:
|
---|
| 38 | continue
|
---|
| 39 | # Get nearest point
|
---|
| 40 | inds = pind['ind']
|
---|
| 41 | xlin = lin.get_xdata()
|
---|
| 42 | ylin = lin.get_ydata()
|
---|
| 43 | for i in inds:
|
---|
| 44 | d2=(xlin[i]-xclick)**2+(ylin[i]-yclick)**2
|
---|
| 45 | if dist2 >= d2:
|
---|
| 46 | dist2 = d2
|
---|
| 47 | pickline = lin
|
---|
| 48 | # No pickcable line in the pannel
|
---|
| 49 | if not pflag:
|
---|
| 50 | return
|
---|
| 51 | # Pickable but too far from mouse position
|
---|
| 52 | elif pickline is None:
|
---|
| 53 | picked='No line selected.'
|
---|
| 54 | self.figmgr.toolbar.set_message(picked)
|
---|
| 55 | return
|
---|
[1724] | 56 | del pind, inds, xlin, ylin
|
---|
[1826] | 57 | # Spectra are Picked
|
---|
[1724] | 58 | theplot = self.plotter._plotter
|
---|
[1826] | 59 | thetoolbar = self.figmgr.toolbar
|
---|
| 60 | thecanvas = self.figmgr.canvas
|
---|
| 61 | # Disconnect the default motion notify event
|
---|
| 62 | # Notice! the other buttons are also diabled!!!
|
---|
| 63 | thecanvas.mpl_disconnect(thetoolbar._idDrag)
|
---|
[1724] | 64 | # Get picked spectrum
|
---|
| 65 | xdata = pickline.get_xdata()
|
---|
| 66 | ydata = pickline.get_ydata()
|
---|
| 67 | titl=pickline.get_label()
|
---|
| 68 | titp=event.inaxes.title.get_text()
|
---|
| 69 | panel0=event.inaxes
|
---|
[1826] | 70 | picked="Selected: '"+titl+"' in panel '"+titp+"'."
|
---|
| 71 | thetoolbar.set_message(picked)
|
---|
| 72 | # Generate a navigation window
|
---|
[1724] | 73 | #naviwin=Navigationwindow(titp,titl)
|
---|
| 74 | #------------------------------------------------------#
|
---|
| 75 | # Show spectrum data at mouse position
|
---|
| 76 | def spec_data(event):
|
---|
[1826] | 77 | # Getting spectrum data of neiboring point
|
---|
| 78 | xclick=event.xdata
|
---|
| 79 | if event.inaxes != panel0:
|
---|
| 80 | return
|
---|
| 81 | ipoint=len(xdata)-1
|
---|
| 82 | for i in range(len(xdata)-1):
|
---|
| 83 | xl=xclick-xdata[i]
|
---|
| 84 | xr=xclick-xdata[i+1]
|
---|
| 85 | if xl*xr <= 0.:
|
---|
| 86 | ipoint = i
|
---|
| 87 | break
|
---|
| 88 | # Output spectral value on the navigation window
|
---|
| 89 | posi='[ %s, %s ]: x = %.2f value = %.2f'\
|
---|
| 90 | %(titl,titp,xdata[ipoint],ydata[ipoint])
|
---|
| 91 | #naviwin.posi.set(posi)
|
---|
| 92 | thetoolbar.set_message(posi)
|
---|
[1724] | 93 | #------------------------------------------------------#
|
---|
| 94 | # Disconnect from mouse events
|
---|
| 95 | def discon(event):
|
---|
[1826] | 96 | #naviwin.window.destroy()
|
---|
| 97 | theplot.register('motion_notify',None)
|
---|
| 98 | # Re-activate the default motion_notify_event
|
---|
| 99 | thetoolbar._idDrag=thecanvas.mpl_connect('motion_notify_event',
|
---|
| 100 | thetoolbar.mouse_move)
|
---|
| 101 | theplot.register('button_release',None)
|
---|
| 102 | return
|
---|
[1724] | 103 | #------------------------------------------------------#
|
---|
| 104 | # Show data value along with mouse movement
|
---|
[1826] | 105 | theplot.register('motion_notify',spec_data)
|
---|
[1724] | 106 | # Finish events when mouse button is released
|
---|
| 107 | theplot.register('button_release',discon)
|
---|
| 108 |
|
---|
| 109 |
|
---|
[1826] | 110 | ### Calculate statistics of the selected area.
|
---|
[1724] | 111 | def _single_mask(self,event):
|
---|
| 112 | # Do not fire event when in zooming/panning mode
|
---|
[1826] | 113 | if not self.figmgr.toolbar.mode == '':
|
---|
| 114 | return
|
---|
[1724] | 115 | # When selected point is out of panels
|
---|
| 116 | if event.inaxes == None:
|
---|
[1826] | 117 | return
|
---|
| 118 | if event.button ==1:
|
---|
| 119 | baseinv=True
|
---|
| 120 | elif event.button == 3:
|
---|
| 121 | baseinv=False
|
---|
| 122 | else:
|
---|
| 123 | return
|
---|
[1724] | 124 |
|
---|
[1826] | 125 | def _calc_stats():
|
---|
| 126 | msk=mymask.get_mask()
|
---|
| 127 | mymask.scan.stats(stat='max',mask=msk)
|
---|
| 128 | mymask.scan.stats(stat='min',mask=msk)
|
---|
| 129 | mymask.scan.stats(stat='sum',mask=msk)
|
---|
| 130 | mymask.scan.stats(stat='mean',mask=msk)
|
---|
| 131 | mymask.scan.stats(stat='median',mask=msk)
|
---|
| 132 | mymask.scan.stats(stat='rms',mask=msk)
|
---|
| 133 | mymask.scan.stats(stat='stddev',mask=msk)
|
---|
[1724] | 134 |
|
---|
[1826] | 135 | # Interactive mask definition
|
---|
[1724] | 136 | from asap.interactivemask import interactivemask
|
---|
[1826] | 137 | mymask=interactivemask(plotter=self.plotter,scan=self.plotter._data)
|
---|
| 138 | # Create initial mask
|
---|
| 139 | mymask.set_basemask(invert=baseinv)
|
---|
| 140 | # Inherit event
|
---|
| 141 | mymask.set_startevent(event)
|
---|
| 142 | # Set callback func
|
---|
| 143 | mymask.set_callback(_calc_stats)
|
---|
| 144 | # Selected mask
|
---|
| 145 | mymask.select_mask(once=True,showmask=False)
|
---|
[1724] | 146 |
|
---|
[1884] | 147 | def _mod_note(self,event):
|
---|
| 148 | # Do not fire event when in zooming/panning mode
|
---|
| 149 | if not self.figmgr.toolbar.mode == '':
|
---|
| 150 | return
|
---|
| 151 | if event.button ==1:
|
---|
| 152 | self.notewin.load_textwindow(event)
|
---|
| 153 | elif event.button == 3 and self._note_picked(event):
|
---|
[1885] | 154 | self.notewin.load_modmenu(event)
|
---|
[1884] | 155 | return
|
---|
| 156 |
|
---|
| 157 | def _note_picked(self,event):
|
---|
| 158 | # just briefly check if any texts are picked
|
---|
| 159 | for textobj in self.canvas.figure.texts:
|
---|
| 160 | if textobj.contains(event)[0]:
|
---|
| 161 | return True
|
---|
| 162 | for ax in self.canvas.figure.axes:
|
---|
| 163 | for textobj in ax.texts:
|
---|
| 164 | if textobj.contains(event)[0]:
|
---|
| 165 | return True
|
---|
| 166 | #print "No text picked"
|
---|
| 167 | return False
|
---|
| 168 |
|
---|
[1913] | 169 | def _new_page(self,next=True):
|
---|
| 170 | self.plotter._plotter.hold()
|
---|
| 171 | self.plotter._plotter.clear()
|
---|
| 172 | self.plotter._plot(self.plotter._data)
|
---|
| 173 | self.plotter._plotter.release()
|
---|
| 174 | self.plotter._plotter.tidy()
|
---|
| 175 | self.plotter._plotter.show(hardrefresh=False)
|
---|
| 176 | pass
|
---|
| 177 |
|
---|
[1724] | 178 | #####################################
|
---|
| 179 | ## Backend dependent Classes ##
|
---|
| 180 | #####################################
|
---|
| 181 | ### TkAgg
|
---|
[1826] | 182 | if matplotlib.get_backend() == 'TkAgg':
|
---|
| 183 | import Tkinter as Tk
|
---|
[1884] | 184 | from notationwindow import NotationWindowTkAgg
|
---|
[1826] | 185 |
|
---|
[1768] | 186 | class CustomToolbarTkAgg(CustomToolbarCommon, Tk.Frame):
|
---|
[1724] | 187 | def __init__(self,parent):
|
---|
| 188 | from asap.asapplotter import asapplotter
|
---|
[1826] | 189 | if not isinstance(parent,asapplotter):
|
---|
| 190 | return False
|
---|
| 191 | if not parent._plotter:
|
---|
| 192 | return False
|
---|
[1724] | 193 | self._p=parent._plotter
|
---|
| 194 | self.figmgr=self._p.figmgr
|
---|
| 195 | self.canvas=self.figmgr.canvas
|
---|
| 196 | self.mode=''
|
---|
| 197 | self.button=True
|
---|
| 198 | self._add_custom_toolbar()
|
---|
[1884] | 199 | self.notewin=NotationWindowTkAgg(master=self.canvas)
|
---|
[1724] | 200 | CustomToolbarCommon.__init__(self,parent)
|
---|
| 201 |
|
---|
| 202 | def _add_custom_toolbar(self):
|
---|
[1768] | 203 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
---|
| 204 | self.bSpec=self._NewButton(master=self,
|
---|
[1724] | 205 | text='spec value',
|
---|
| 206 | command=self.spec_show)
|
---|
[1768] | 207 | self.bStat=self._NewButton(master=self,
|
---|
[1724] | 208 | text='statistics',
|
---|
| 209 | command=self.stat_cal)
|
---|
[1884] | 210 | self.bNote=self._NewButton(master=self,
|
---|
[1888] | 211 | text='notation',
|
---|
[1884] | 212 | command=self.modify_note)
|
---|
[1913] | 213 | self.bPrev=self._NewButton(master=self,
|
---|
| 214 | text='- page',
|
---|
| 215 | command=self.prev_page)
|
---|
| 216 | self.bNext=self._NewButton(master=self,
|
---|
| 217 | text='+ page',
|
---|
| 218 | command=self.next_page)
|
---|
[1914] | 219 | if os.uname()[0] != 'Darwin':
|
---|
| 220 | self.bPrev.config(padx=5)
|
---|
| 221 | self.bNext.config(padx=5)
|
---|
[1768] | 222 | self.bQuit=self._NewButton(master=self,
|
---|
[1724] | 223 | text='Quit',
|
---|
| 224 | command=self.quit,
|
---|
| 225 | side=Tk.RIGHT)
|
---|
[1768] | 226 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
---|
| 227 |
|
---|
[1724] | 228 | self.disable_button()
|
---|
[1768] | 229 | return #self
|
---|
[1724] | 230 |
|
---|
| 231 | def _NewButton(self, master, text, command, side=Tk.LEFT):
|
---|
[1826] | 232 | if os.uname()[0] == 'Darwin':
|
---|
[1724] | 233 | b = Tk.Button(master=master, text=text, command=command)
|
---|
| 234 | else:
|
---|
[1826] | 235 | b = Tk.Button(master=master, text=text, padx=2, pady=2,
|
---|
| 236 | command=command)
|
---|
[1724] | 237 | b.pack(side=side)
|
---|
| 238 | return b
|
---|
[1826] | 239 |
|
---|
[1724] | 240 | def spec_show(self):
|
---|
| 241 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 242 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
---|
| 243 | if self.mode == 'spec': return
|
---|
| 244 | self.bStat.config(relief='raised')
|
---|
| 245 | self.bSpec.config(relief='sunken')
|
---|
[1884] | 246 | self.bNote.config(relief='raised')
|
---|
[1724] | 247 | self.mode='spec'
|
---|
[1885] | 248 | self.notewin.close_widgets()
|
---|
[1724] | 249 | self.__disconnect_event()
|
---|
| 250 | #self.canvas.mpl_connect('button_press_event',self._select_spectrum)
|
---|
| 251 | self._p.register('button_press',self._select_spectrum)
|
---|
| 252 |
|
---|
| 253 | def stat_cal(self):
|
---|
| 254 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 255 | self.figmgr.toolbar.set_message('statistics: select a region')
|
---|
| 256 | if self.mode == 'stat': return
|
---|
| 257 | self.bSpec.config(relief='raised')
|
---|
| 258 | self.bStat.config(relief='sunken')
|
---|
[1884] | 259 | self.bNote.config(relief='raised')
|
---|
[1724] | 260 | self.mode='stat'
|
---|
[1885] | 261 | self.notewin.close_widgets()
|
---|
[1724] | 262 | self.__disconnect_event()
|
---|
| 263 | self._p.register('button_press',self._single_mask)
|
---|
| 264 |
|
---|
[1884] | 265 | def modify_note(self):
|
---|
[1890] | 266 | if not self.figmgr.toolbar.mode == '': return
|
---|
[1884] | 267 | self.figmgr.toolbar.set_message('text: select a position/text')
|
---|
| 268 | if self.mode == 'note': return
|
---|
| 269 | self.bSpec.config(relief='raised')
|
---|
| 270 | self.bStat.config(relief='raised')
|
---|
| 271 | self.bNote.config(relief='sunken')
|
---|
| 272 | self.mode='note'
|
---|
| 273 | self.__disconnect_event()
|
---|
| 274 | self._p.register('button_press',self._mod_note)
|
---|
| 275 |
|
---|
[1913] | 276 | def prev_page(self):
|
---|
| 277 | self.figmgr.toolbar.set_message('plotting the previous page')
|
---|
| 278 | self._new_page(next=False)
|
---|
| 279 |
|
---|
| 280 | def next_page(self):
|
---|
| 281 | self.figmgr.toolbar.set_message('plotting the next page')
|
---|
| 282 | self._new_page(next=True)
|
---|
| 283 |
|
---|
[1724] | 284 | def quit(self):
|
---|
| 285 | self.__disconnect_event()
|
---|
[1765] | 286 | #self.delete_bar()
|
---|
| 287 | self.disable_button()
|
---|
[1724] | 288 | self.figmgr.window.wm_withdraw()
|
---|
| 289 |
|
---|
| 290 | def enable_button(self):
|
---|
| 291 | if self.button: return
|
---|
| 292 | self.bSpec.config(state=Tk.NORMAL)
|
---|
| 293 | self.bStat.config(state=Tk.NORMAL)
|
---|
| 294 | self.button=True
|
---|
| 295 | self.spec_show()
|
---|
[1826] | 296 |
|
---|
[1724] | 297 | def disable_button(self):
|
---|
| 298 | if not self.button: return
|
---|
[1826] | 299 | self.bStat.config(relief='raised', state=Tk.DISABLED)
|
---|
| 300 | self.bSpec.config(relief='raised', state=Tk.DISABLED)
|
---|
[1913] | 301 | self.bPrev.config(state=Tk.DISABLED)
|
---|
| 302 | #self.bNext.config(state=Tk.DISABLED)
|
---|
[1724] | 303 | self.button=False
|
---|
| 304 | self.mode=''
|
---|
| 305 | self.__disconnect_event()
|
---|
| 306 |
|
---|
[1913] | 307 | def enable_next(self):
|
---|
| 308 | self.bNext.config(state=Tk.NORMAL)
|
---|
| 309 |
|
---|
| 310 | def disable_next(self):
|
---|
| 311 | self.bNext.config(state=Tk.DISABLED)
|
---|
| 312 |
|
---|
| 313 | def enable_prev(self):
|
---|
| 314 | self.bPrev.config(state=Tk.NORMAL)
|
---|
| 315 |
|
---|
| 316 | def disable_prev(self):
|
---|
| 317 | self.bPrev.config(state=Tk.DISABLED)
|
---|
| 318 |
|
---|
[1724] | 319 | def delete_bar(self):
|
---|
| 320 | self.__disconnect_event()
|
---|
[1768] | 321 | self.destroy()
|
---|
[1724] | 322 |
|
---|
| 323 | def __disconnect_event(self):
|
---|
| 324 | #idP=self.figmgr.toolbar._idPress
|
---|
| 325 | #idR=self.figmgr.toolbar._idRelease
|
---|
| 326 | #if idP is not None:
|
---|
| 327 | # self.canvas.mpl_disconnect(idP)
|
---|
| 328 | # self.figmgr.toolbar._idPress=None
|
---|
| 329 | #if idR is not None:
|
---|
| 330 | # self.canvas.mpl_disconnect(idR)
|
---|
| 331 | # self.figmgr.toolbar._idRelease=None
|
---|
| 332 | self._p.register('button_press',None)
|
---|
| 333 | self._p.register('button_release',None)
|
---|