[1724] | 1 | import os
|
---|
[1981] | 2 | import matplotlib, numpy
|
---|
| 3 | from asap.logging import asaplog, asaplog_post_dec
|
---|
[1884] | 4 |
|
---|
[1724] | 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
|
---|
[1826] | 14 | ### and display spectral value on the toolbar.
|
---|
[1724] | 15 | def _select_spectrum(self,event):
|
---|
| 16 | # Do not fire event when in zooming/panning mode
|
---|
| 17 | mode = self.figmgr.toolbar.mode
|
---|
[1826] | 18 | if not mode == '':
|
---|
| 19 | return
|
---|
| 20 | # When selected point is out of panels
|
---|
[1724] | 21 | if event.inaxes == None:
|
---|
[1826] | 22 | return
|
---|
[1724] | 23 | # If not left button
|
---|
| 24 | if event.button != 1:
|
---|
[1826] | 25 | return
|
---|
[1724] | 26 |
|
---|
| 27 | xclick=event.xdata
|
---|
| 28 | yclick=event.ydata
|
---|
| 29 | dist2=1000.
|
---|
| 30 | pickline=None
|
---|
[1826] | 31 | # If the pannel has picable objects
|
---|
| 32 | pflag=False
|
---|
[1724] | 33 | for lin in event.inaxes.lines:
|
---|
[1826] | 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
|
---|
[1724] | 57 | del pind, inds, xlin, ylin
|
---|
[1826] | 58 | # Spectra are Picked
|
---|
[1724] | 59 | theplot = self.plotter._plotter
|
---|
[1826] | 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)
|
---|
[1724] | 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
|
---|
[1826] | 71 | picked="Selected: '"+titl+"' in panel '"+titp+"'."
|
---|
| 72 | thetoolbar.set_message(picked)
|
---|
| 73 | # Generate a navigation window
|
---|
[1724] | 74 | #naviwin=Navigationwindow(titp,titl)
|
---|
| 75 | #------------------------------------------------------#
|
---|
| 76 | # Show spectrum data at mouse position
|
---|
| 77 | def spec_data(event):
|
---|
[1826] | 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)
|
---|
[1724] | 94 | #------------------------------------------------------#
|
---|
| 95 | # Disconnect from mouse events
|
---|
| 96 | def discon(event):
|
---|
[1826] | 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
|
---|
[1724] | 104 | #------------------------------------------------------#
|
---|
| 105 | # Show data value along with mouse movement
|
---|
[1826] | 106 | theplot.register('motion_notify',spec_data)
|
---|
[1724] | 107 | # Finish events when mouse button is released
|
---|
| 108 | theplot.register('button_release',discon)
|
---|
| 109 |
|
---|
| 110 |
|
---|
[1826] | 111 | ### Calculate statistics of the selected area.
|
---|
[1724] | 112 | def _single_mask(self,event):
|
---|
| 113 | # Do not fire event when in zooming/panning mode
|
---|
[1826] | 114 | if not self.figmgr.toolbar.mode == '':
|
---|
| 115 | return
|
---|
[1724] | 116 | # When selected point is out of panels
|
---|
| 117 | if event.inaxes == None:
|
---|
[1826] | 118 | return
|
---|
| 119 | if event.button ==1:
|
---|
| 120 | baseinv=True
|
---|
| 121 | elif event.button == 3:
|
---|
| 122 | baseinv=False
|
---|
| 123 | else:
|
---|
| 124 | return
|
---|
[1724] | 125 |
|
---|
[1826] | 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)
|
---|
[1724] | 135 |
|
---|
[1826] | 136 | # Interactive mask definition
|
---|
[1724] | 137 | from asap.interactivemask import interactivemask
|
---|
[1826] | 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)
|
---|
[1724] | 147 |
|
---|
[1884] | 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):
|
---|
[1885] | 155 | self.notewin.load_modmenu(event)
|
---|
[1884] | 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 |
|
---|
[1983] | 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):
|
---|
[2051] | 183 | top = None
|
---|
[1981] | 184 | if self.plotter._startrow <= 0:
|
---|
| 185 | msg = "The page counter is reset due to chages of plot settings. "
|
---|
| 186 | msg += "Plotting from the first page."
|
---|
| 187 | asaplog.push(msg)
|
---|
| 188 | asaplog.post('WARN')
|
---|
[1983] | 189 | goback = False
|
---|
[2051] | 190 | else:
|
---|
| 191 | top = self.plotter._plotter.figure.subplotpars.top
|
---|
| 192 |
|
---|
[1913] | 193 | self.plotter._plotter.hold()
|
---|
[1983] | 194 | if goback:
|
---|
[1981] | 195 | self._set_prevpage_counter()
|
---|
[1945] | 196 | #self.plotter._plotter.clear()
|
---|
[1982] | 197 | self.plotter._plot(self.plotter._data)
|
---|
[1983] | 198 | self.set_pagecounter(self._get_pagenum())
|
---|
[1913] | 199 | self.plotter._plotter.release()
|
---|
| 200 | self.plotter._plotter.tidy()
|
---|
[2051] | 201 | if self.plotter._headstring:
|
---|
| 202 | if top and top != self.plotter._margins[3]:
|
---|
| 203 | # work around for sdplot in CASA. complete checking in future?
|
---|
| 204 | self.plotter._plotter.figure.subplots_adjust(top=top)
|
---|
| 205 | self.plotter.print_header()
|
---|
[1913] | 206 | self.plotter._plotter.show(hardrefresh=False)
|
---|
[2051] | 207 | del top
|
---|
[1913] | 208 |
|
---|
[1983] | 209 | ### calculate the panel ID and start row to plot the previous page
|
---|
[1981] | 210 | def _set_prevpage_counter(self):
|
---|
| 211 | # set row and panel counters to those of the 1st panel of previous page
|
---|
| 212 | maxpanel = 16
|
---|
| 213 | # the ID of the last panel in current plot
|
---|
| 214 | lastpanel = self.plotter._ipanel
|
---|
| 215 | # the number of current subplots
|
---|
| 216 | currpnum = len(self.plotter._plotter.subplots)
|
---|
| 217 | # the nuber of previous subplots
|
---|
| 218 | prevpnum = None
|
---|
| 219 | if self.plotter._rows and self.plotter._cols:
|
---|
| 220 | # when user set layout
|
---|
| 221 | prevpnum = self.plotter._rows*self.plotter._cols
|
---|
| 222 | else:
|
---|
| 223 | # no user specification
|
---|
| 224 | prevpnum = maxpanel
|
---|
| 225 |
|
---|
| 226 | start_ipanel = max(lastpanel-currpnum-prevpnum+1, 0)
|
---|
| 227 | # set the pannel ID of the last panel of prev-prev page
|
---|
| 228 | self.plotter._ipanel = start_ipanel-1
|
---|
| 229 | if self.plotter._panelling == 'r':
|
---|
| 230 | self.plotter._startrow = start_ipanel
|
---|
| 231 | else:
|
---|
| 232 | # the start row number of the next panel
|
---|
| 233 | self.plotter._startrow = self.plotter._panelrows[start_ipanel]
|
---|
| 234 | del lastpanel,currpnum,prevpnum,start_ipanel
|
---|
[1979] | 235 |
|
---|
[1983] | 236 | ### refresh the page counter
|
---|
| 237 | ### refresh the page counter
|
---|
| 238 | def set_pagecounter(self,page):
|
---|
| 239 | nwidth = int(numpy.ceil(numpy.log10(max(page,1))))+1
|
---|
| 240 | nwidth = max(nwidth,4)
|
---|
| 241 | formatstr = '%'+str(nwidth)+'d'
|
---|
| 242 | self.show_pagenum(page,formatstr)
|
---|
| 243 |
|
---|
| 244 | def show_pagenum(self,pagenum,formatstr):
|
---|
| 245 | # passed to backend dependent class
|
---|
| 246 | pass
|
---|
| 247 |
|
---|
[1981] | 248 | def _get_pagenum(self):
|
---|
| 249 | maxpanel = 16
|
---|
[1982] | 250 | # get the ID of last panel in the current page
|
---|
| 251 | idlastpanel = self.plotter._ipanel
|
---|
[1981] | 252 | if self.plotter._rows and self.plotter._cols:
|
---|
| 253 | ppp = self.plotter._rows*self.plotter._cols
|
---|
| 254 | else:
|
---|
| 255 | ppp = maxpanel
|
---|
[1982] | 256 | return int(idlastpanel/ppp)+1
|
---|
[1981] | 257 |
|
---|
[1724] | 258 | #####################################
|
---|
| 259 | ## Backend dependent Classes ##
|
---|
| 260 | #####################################
|
---|
| 261 | ### TkAgg
|
---|
[1826] | 262 | if matplotlib.get_backend() == 'TkAgg':
|
---|
| 263 | import Tkinter as Tk
|
---|
[1884] | 264 | from notationwindow import NotationWindowTkAgg
|
---|
[1826] | 265 |
|
---|
[1768] | 266 | class CustomToolbarTkAgg(CustomToolbarCommon, Tk.Frame):
|
---|
[1724] | 267 | def __init__(self,parent):
|
---|
| 268 | from asap.asapplotter import asapplotter
|
---|
[1826] | 269 | if not isinstance(parent,asapplotter):
|
---|
| 270 | return False
|
---|
| 271 | if not parent._plotter:
|
---|
| 272 | return False
|
---|
[1981] | 273 | self._p = parent._plotter
|
---|
| 274 | self.figmgr = self._p.figmgr
|
---|
| 275 | self.canvas = self.figmgr.canvas
|
---|
| 276 | self.mode = ''
|
---|
| 277 | self.button = True
|
---|
[1983] | 278 | self.pagecount = None
|
---|
| 279 | CustomToolbarCommon.__init__(self,parent)
|
---|
| 280 | self.notewin=NotationWindowTkAgg(master=self.canvas)
|
---|
[1724] | 281 | self._add_custom_toolbar()
|
---|
| 282 |
|
---|
| 283 | def _add_custom_toolbar(self):
|
---|
[1768] | 284 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
---|
[1979] | 285 | #self.bSpec=self._NewButton(master=self,
|
---|
| 286 | # text='spec value',
|
---|
| 287 | # command=self.spec_show)
|
---|
[1981] | 288 | self.bNote=self._NewButton(master=self,
|
---|
| 289 | text='notation',
|
---|
| 290 | command=self.modify_note)
|
---|
| 291 |
|
---|
[1768] | 292 | self.bStat=self._NewButton(master=self,
|
---|
[1724] | 293 | text='statistics',
|
---|
| 294 | command=self.stat_cal)
|
---|
[1983] | 295 | self.bQuit=self._NewButton(master=self,
|
---|
| 296 | text='Quit',
|
---|
| 297 | command=self.quit,
|
---|
| 298 | side=Tk.RIGHT)
|
---|
| 299 |
|
---|
[1981] | 300 | # page change oparations
|
---|
[1983] | 301 | frPage = Tk.Frame(master=self,borderwidth=2,relief=Tk.GROOVE)
|
---|
| 302 | frPage.pack(ipadx=2,padx=10,side=Tk.RIGHT)
|
---|
| 303 | self.lPagetitle = Tk.Label(master=frPage,text='Page:',padx=5)
|
---|
[1981] | 304 | self.lPagetitle.pack(side=Tk.LEFT)
|
---|
[1983] | 305 | self.pagecount = Tk.StringVar(master=frPage)
|
---|
| 306 | self.lPagecount = Tk.Label(master=frPage,
|
---|
| 307 | textvariable=self.pagecount,
|
---|
| 308 | padx=5,bg='white')
|
---|
| 309 | self.lPagecount.pack(side=Tk.LEFT,padx=3)
|
---|
[1981] | 310 |
|
---|
[1983] | 311 | self.bNext=self._NewButton(master=frPage,
|
---|
[1981] | 312 | text=' + ',
|
---|
[1913] | 313 | command=self.next_page)
|
---|
[1983] | 314 | self.bPrev=self._NewButton(master=frPage,
|
---|
[1981] | 315 | text=' - ',
|
---|
| 316 | command=self.prev_page)
|
---|
| 317 |
|
---|
[1914] | 318 | if os.uname()[0] != 'Darwin':
|
---|
[1981] | 319 | self.bPrev.config(padx=5)
|
---|
[1914] | 320 | self.bNext.config(padx=5)
|
---|
[1983] | 321 |
|
---|
[1768] | 322 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
---|
[1983] | 323 | self.pagecount.set(' '*4)
|
---|
[1768] | 324 |
|
---|
[1724] | 325 | self.disable_button()
|
---|
[1768] | 326 | return #self
|
---|
[1724] | 327 |
|
---|
| 328 | def _NewButton(self, master, text, command, side=Tk.LEFT):
|
---|
[1826] | 329 | if os.uname()[0] == 'Darwin':
|
---|
[1724] | 330 | b = Tk.Button(master=master, text=text, command=command)
|
---|
| 331 | else:
|
---|
[1826] | 332 | b = Tk.Button(master=master, text=text, padx=2, pady=2,
|
---|
| 333 | command=command)
|
---|
[1724] | 334 | b.pack(side=side)
|
---|
| 335 | return b
|
---|
[1826] | 336 |
|
---|
[1983] | 337 | def show_pagenum(self,pagenum,formatstr):
|
---|
| 338 | self.pagecount.set(formatstr % (pagenum))
|
---|
| 339 |
|
---|
[1724] | 340 | def spec_show(self):
|
---|
| 341 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 342 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
---|
| 343 | if self.mode == 'spec': return
|
---|
[1979] | 344 | #self.bStat.config(relief='raised')
|
---|
| 345 | #self.bSpec.config(relief='sunken')
|
---|
| 346 | #self.bNote.config(relief='raised')
|
---|
[1724] | 347 | self.mode='spec'
|
---|
[1885] | 348 | self.notewin.close_widgets()
|
---|
[1724] | 349 | self.__disconnect_event()
|
---|
| 350 | self._p.register('button_press',self._select_spectrum)
|
---|
| 351 |
|
---|
| 352 | def stat_cal(self):
|
---|
| 353 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 354 | self.figmgr.toolbar.set_message('statistics: select a region')
|
---|
[1979] | 355 | if self.mode == 'stat':
|
---|
| 356 | # go back to spec mode
|
---|
| 357 | self.bStat.config(relief='raised')
|
---|
| 358 | self.spec_show()
|
---|
| 359 | return
|
---|
| 360 | #self.bSpec.config(relief='raised')
|
---|
[1724] | 361 | self.bStat.config(relief='sunken')
|
---|
[1884] | 362 | self.bNote.config(relief='raised')
|
---|
[1724] | 363 | self.mode='stat'
|
---|
[1885] | 364 | self.notewin.close_widgets()
|
---|
[1724] | 365 | self.__disconnect_event()
|
---|
| 366 | self._p.register('button_press',self._single_mask)
|
---|
| 367 |
|
---|
[1884] | 368 | def modify_note(self):
|
---|
[1890] | 369 | if not self.figmgr.toolbar.mode == '': return
|
---|
[1884] | 370 | self.figmgr.toolbar.set_message('text: select a position/text')
|
---|
[1979] | 371 | if self.mode == 'note':
|
---|
| 372 | self.bNote.config(relief='raised')
|
---|
| 373 | self.mode='none'
|
---|
| 374 | self.spec_show()
|
---|
| 375 | return
|
---|
| 376 | #self.bSpec.config(relief='raised')
|
---|
[1884] | 377 | self.bStat.config(relief='raised')
|
---|
| 378 | self.bNote.config(relief='sunken')
|
---|
| 379 | self.mode='note'
|
---|
| 380 | self.__disconnect_event()
|
---|
| 381 | self._p.register('button_press',self._mod_note)
|
---|
| 382 |
|
---|
[1724] | 383 | def quit(self):
|
---|
| 384 | self.__disconnect_event()
|
---|
[1765] | 385 | #self.delete_bar()
|
---|
| 386 | self.disable_button()
|
---|
[1724] | 387 | self.figmgr.window.wm_withdraw()
|
---|
| 388 |
|
---|
| 389 | def enable_button(self):
|
---|
| 390 | if self.button: return
|
---|
[1979] | 391 | #self.bSpec.config(state=Tk.NORMAL)
|
---|
[1724] | 392 | self.bStat.config(state=Tk.NORMAL)
|
---|
| 393 | self.button=True
|
---|
| 394 | self.spec_show()
|
---|
[1826] | 395 |
|
---|
[1724] | 396 | def disable_button(self):
|
---|
| 397 | if not self.button: return
|
---|
[1981] | 398 | #self.bSpec.config(relief='raised', state=Tk.DISABLED)
|
---|
[1826] | 399 | self.bStat.config(relief='raised', state=Tk.DISABLED)
|
---|
[1981] | 400 | #self.bNext.config(state=Tk.DISABLED)
|
---|
[1921] | 401 | #self.bPrev.config(state=Tk.DISABLED)
|
---|
[1724] | 402 | self.button=False
|
---|
| 403 | self.mode=''
|
---|
| 404 | self.__disconnect_event()
|
---|
| 405 |
|
---|
[1913] | 406 | def enable_next(self):
|
---|
| 407 | self.bNext.config(state=Tk.NORMAL)
|
---|
| 408 |
|
---|
| 409 | def disable_next(self):
|
---|
| 410 | self.bNext.config(state=Tk.DISABLED)
|
---|
| 411 |
|
---|
| 412 | def enable_prev(self):
|
---|
[1981] | 413 | self.bPrev.config(state=Tk.NORMAL)
|
---|
[1913] | 414 |
|
---|
| 415 | def disable_prev(self):
|
---|
[1981] | 416 | self.bPrev.config(state=Tk.DISABLED)
|
---|
[1913] | 417 |
|
---|
[1724] | 418 | def delete_bar(self):
|
---|
| 419 | self.__disconnect_event()
|
---|
[1768] | 420 | self.destroy()
|
---|
[1724] | 421 |
|
---|
| 422 | def __disconnect_event(self):
|
---|
| 423 | self._p.register('button_press',None)
|
---|
| 424 | self._p.register('button_release',None)
|
---|