[1996] | 1 | import os
|
---|
| 2 | import matplotlib, numpy
|
---|
| 3 | from matplotlib.patches import Rectangle
|
---|
| 4 | from asap.parameters import rcParams
|
---|
| 5 | from asap.logging import asaplog, asaplog_post_dec
|
---|
| 6 | from asap import scantable
|
---|
| 7 | from asap._asap import stmath
|
---|
| 8 |
|
---|
| 9 | ###########################################
|
---|
| 10 | ## Add CASA custom Flag toolbar ##
|
---|
| 11 | ###########################################
|
---|
| 12 | class CustomFlagToolbarCommon:
|
---|
| 13 | def __init__(self,parent):
|
---|
| 14 | self.plotter=parent
|
---|
| 15 | #self.figmgr=self.plotter._plotter.figmgr
|
---|
| 16 | self._selregions = {}
|
---|
| 17 | self._selpanels = []
|
---|
| 18 | self._polygons = []
|
---|
| 19 | self._thisregion = None
|
---|
| 20 | self.xdataold=None
|
---|
| 21 |
|
---|
| 22 | ### select the nearest spectrum in pick radius
|
---|
| 23 | ### and display spectral value on the toolbar.
|
---|
| 24 | def _select_spectrum(self,event):
|
---|
| 25 | # Do not fire event when in zooming/panning mode
|
---|
| 26 | mode = self.figmgr.toolbar.mode
|
---|
| 27 | if not mode == '':
|
---|
| 28 | return
|
---|
| 29 | # When selected point is out of panels
|
---|
| 30 | if event.inaxes == None:
|
---|
| 31 | return
|
---|
| 32 | # If not left button
|
---|
| 33 | if event.button != 1:
|
---|
| 34 | return
|
---|
| 35 |
|
---|
| 36 | xclick=event.xdata
|
---|
| 37 | yclick=event.ydata
|
---|
| 38 | dist2=1000.
|
---|
| 39 | pickline=None
|
---|
| 40 | # If the pannel has picable objects
|
---|
| 41 | pflag=False
|
---|
| 42 | for lin in event.inaxes.lines:
|
---|
| 43 | if not lin.pickable():
|
---|
| 44 | continue
|
---|
| 45 | pflag=True
|
---|
| 46 | flag,pind = lin.contains(event)
|
---|
| 47 | if not flag:
|
---|
| 48 | continue
|
---|
| 49 | # Get nearest point
|
---|
| 50 | inds = pind['ind']
|
---|
| 51 | xlin = lin.get_xdata()
|
---|
| 52 | ylin = lin.get_ydata()
|
---|
| 53 | for i in inds:
|
---|
| 54 | d2=(xlin[i]-xclick)**2+(ylin[i]-yclick)**2
|
---|
| 55 | if dist2 >= d2:
|
---|
| 56 | dist2 = d2
|
---|
| 57 | pickline = lin
|
---|
| 58 | # No pickcable line in the pannel
|
---|
| 59 | if not pflag:
|
---|
| 60 | return
|
---|
| 61 | # Pickable but too far from mouse position
|
---|
| 62 | elif pickline is None:
|
---|
| 63 | picked='No line selected.'
|
---|
| 64 | self.figmgr.toolbar.set_message(picked)
|
---|
| 65 | return
|
---|
| 66 | del pind, inds, xlin, ylin
|
---|
| 67 | # Spectra are Picked
|
---|
| 68 | theplot = self.plotter._plotter
|
---|
| 69 | thetoolbar = self.figmgr.toolbar
|
---|
| 70 | thecanvas = self.figmgr.canvas
|
---|
| 71 | # Disconnect the default motion notify event
|
---|
| 72 | # Notice! the other buttons are also diabled!!!
|
---|
| 73 | thecanvas.mpl_disconnect(thetoolbar._idDrag)
|
---|
| 74 | # Get picked spectrum
|
---|
| 75 | xdata = pickline.get_xdata()
|
---|
| 76 | ydata = pickline.get_ydata()
|
---|
| 77 | titl=pickline.get_label()
|
---|
| 78 | titp=event.inaxes.title.get_text()
|
---|
| 79 | panel0=event.inaxes
|
---|
| 80 | picked="Selected: '"+titl+"' in panel '"+titp+"'."
|
---|
| 81 | thetoolbar.set_message(picked)
|
---|
| 82 | # Generate a navigation window
|
---|
| 83 | #naviwin=Navigationwindow(titp,titl)
|
---|
| 84 | #------------------------------------------------------#
|
---|
| 85 | # Show spectrum data at mouse position
|
---|
| 86 | def spec_data(event):
|
---|
| 87 | # Getting spectrum data of neiboring point
|
---|
| 88 | xclick=event.xdata
|
---|
| 89 | if event.inaxes != panel0:
|
---|
| 90 | return
|
---|
| 91 | ipoint=len(xdata)-1
|
---|
| 92 | for i in range(len(xdata)-1):
|
---|
| 93 | xl=xclick-xdata[i]
|
---|
| 94 | xr=xclick-xdata[i+1]
|
---|
| 95 | if xl*xr <= 0.:
|
---|
| 96 | ipoint = i
|
---|
| 97 | break
|
---|
| 98 | # Output spectral value on the navigation window
|
---|
| 99 | posi='[ %s, %s ]: x = %.2f value = %.2f'\
|
---|
| 100 | %(titl,titp,xdata[ipoint],ydata[ipoint])
|
---|
| 101 | #naviwin.posi.set(posi)
|
---|
| 102 | thetoolbar.set_message(posi)
|
---|
| 103 | #------------------------------------------------------#
|
---|
| 104 | # Disconnect from mouse events
|
---|
| 105 | def discon(event):
|
---|
| 106 | #naviwin.window.destroy()
|
---|
| 107 | theplot.register('motion_notify',None)
|
---|
| 108 | # Re-activate the default motion_notify_event
|
---|
| 109 | thetoolbar._idDrag=thecanvas.mpl_connect('motion_notify_event',
|
---|
| 110 | thetoolbar.mouse_move)
|
---|
| 111 | theplot.register('button_release',None)
|
---|
| 112 | return
|
---|
| 113 | #------------------------------------------------------#
|
---|
| 114 | # Show data value along with mouse movement
|
---|
| 115 | theplot.register('motion_notify',spec_data)
|
---|
| 116 | # Finish events when mouse button is released
|
---|
| 117 | theplot.register('button_release',discon)
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | ### Notation
|
---|
| 121 | def _mod_note(self,event):
|
---|
| 122 | # Do not fire event when in zooming/panning mode
|
---|
| 123 | if not self.figmgr.toolbar.mode == '':
|
---|
| 124 | return
|
---|
| 125 | if event.button ==1:
|
---|
| 126 | self.notewin.load_textwindow(event)
|
---|
| 127 | elif event.button == 3 and self._note_picked(event):
|
---|
| 128 | self.notewin.load_modmenu(event)
|
---|
| 129 | return
|
---|
| 130 |
|
---|
| 131 | def _note_picked(self,event):
|
---|
| 132 | # just briefly check if any texts are picked
|
---|
| 133 | for textobj in self.canvas.figure.texts:
|
---|
| 134 | if textobj.contains(event)[0]:
|
---|
| 135 | return True
|
---|
| 136 | for ax in self.canvas.figure.axes:
|
---|
| 137 | for textobj in ax.texts:
|
---|
| 138 | if textobj.contains(event)[0]:
|
---|
| 139 | return True
|
---|
| 140 | #print "No text picked"
|
---|
| 141 | return False
|
---|
| 142 |
|
---|
| 143 | ### Region/Panel selection & oparations
|
---|
| 144 | ### add regions to selections
|
---|
| 145 | @asaplog_post_dec
|
---|
| 146 | def _add_region(self,event):
|
---|
| 147 | if not self.figmgr.toolbar.mode == '':
|
---|
| 148 | return
|
---|
| 149 | if event.button != 1 or event.inaxes == None:
|
---|
| 150 | return
|
---|
| 151 | # this row resolution assumes row panelling
|
---|
| 152 | irow = int(self._getrownum(event.inaxes))
|
---|
| 153 | if irow in self._selpanels:
|
---|
| 154 | msg = "The whole spectrum is already selected"
|
---|
| 155 | asaplog.post()
|
---|
| 156 | asaplog.push(msg)
|
---|
| 157 | asaplog.post('WARN')
|
---|
| 158 | return
|
---|
[2000] | 159 | #print "add the region to selections"
|
---|
[1996] | 160 | self._thisregion = {'axes': event.inaxes,'xs': event.xdata,
|
---|
| 161 | 'worldx': [event.xdata,event.xdata]}
|
---|
| 162 | self.plotter._plotter.register('button_press',None)
|
---|
| 163 | self.plotter._plotter.register('motion_notify', self._xspan_draw)
|
---|
| 164 | self.plotter._plotter.register('button_press', self._xspan_end)
|
---|
| 165 |
|
---|
| 166 | def _xspan_draw(self,event):
|
---|
| 167 | if event.inaxes == self._thisregion['axes']:
|
---|
| 168 | xnow = event.xdata
|
---|
| 169 | self.xdataold = xnow
|
---|
| 170 | else:
|
---|
| 171 | xnow = self.xdataold
|
---|
| 172 | try: self.lastspan
|
---|
| 173 | except AttributeError: pass
|
---|
| 174 | else: self.lastspan.remove()
|
---|
| 175 |
|
---|
| 176 | self.lastspan = self._thisregion['axes'].axvspan(self._thisregion['xs'],xnow,facecolor='0.7')
|
---|
[1999] | 177 | #self.plotter._plotter.show(False)
|
---|
| 178 | self.plotter._plotter.canvas.draw()
|
---|
[1996] | 179 | del xnow
|
---|
| 180 |
|
---|
| 181 | def _xspan_end(self,event):
|
---|
| 182 | if not self.figmgr.toolbar.mode == '':
|
---|
| 183 | return
|
---|
| 184 | if event.button != 1:
|
---|
| 185 | return
|
---|
| 186 |
|
---|
| 187 | try: self.lastspan
|
---|
| 188 | except AttributeError: pass
|
---|
| 189 | else:
|
---|
| 190 | self.lastspan.remove()
|
---|
| 191 | del self.lastspan
|
---|
| 192 | if event.inaxes == self._thisregion['axes']:
|
---|
| 193 | xdataend = event.xdata
|
---|
| 194 | else:
|
---|
| 195 | xdataend=self.xdataold
|
---|
| 196 |
|
---|
| 197 | self._thisregion['worldx'][1] = xdataend
|
---|
| 198 | lregion = self._thisregion['worldx']
|
---|
| 199 | pregion = self._thisregion['axes'].axvspan(lregion[0],lregion[1],
|
---|
| 200 | facecolor='0.7')
|
---|
[1999] | 201 | #self.plotter._plotter.show(False)
|
---|
| 202 | self.plotter._plotter.canvas.draw()
|
---|
[1996] | 203 | self._polygons.append(pregion)
|
---|
| 204 | srow = self._getrownum(self._thisregion['axes'])
|
---|
| 205 | irow = int(srow)
|
---|
| 206 | if not self._selregions.has_key(srow):
|
---|
| 207 | self._selregions[srow] = []
|
---|
| 208 | self._selregions[srow].append(lregion)
|
---|
| 209 | del lregion, pregion, xdataend
|
---|
[2000] | 210 | sout = "selected region: "+str(self._thisregion['worldx'])+\
|
---|
| 211 | "(@row "+str(self._getrownum(self._thisregion['axes']))+")"
|
---|
| 212 | asaplog.push(sout)
|
---|
[1996] | 213 |
|
---|
| 214 | # release event
|
---|
| 215 | self.plotter._plotter.register('button_press',None)
|
---|
| 216 | self.plotter._plotter.register('motion_notify',None)
|
---|
| 217 | # Clear up region selection
|
---|
| 218 | self._thisregion = None
|
---|
| 219 | self.xdataold = None
|
---|
| 220 | # finally recover region selection event
|
---|
| 221 | self.plotter._plotter.register('button_press',self._add_region)
|
---|
| 222 |
|
---|
| 223 | ### add panels to selections
|
---|
| 224 | @asaplog_post_dec
|
---|
| 225 | def _add_panel(self,event):
|
---|
| 226 | if not self.figmgr.toolbar.mode == '':
|
---|
| 227 | return
|
---|
| 228 | if event.button != 1 or event.inaxes == None:
|
---|
| 229 | return
|
---|
| 230 | selax = event.inaxes
|
---|
| 231 | # this row resolution assumes row panelling
|
---|
| 232 | srow = self._getrownum(selax)
|
---|
| 233 | irow = int(srow)
|
---|
| 234 | if srow:
|
---|
| 235 | self._selpanels.append(irow)
|
---|
| 236 | shadow = Rectangle((0,0),1,1,facecolor='0.7',transform=selax.transAxes,visible=True)
|
---|
| 237 | self._polygons.append(selax.add_patch(shadow))
|
---|
[1999] | 238 | #self.plotter._plotter.show(False)
|
---|
| 239 | self.plotter._plotter.canvas.draw()
|
---|
[2000] | 240 | asaplog.push("row "+str(irow)+" is selected")
|
---|
| 241 | #print "selected rows =",self._selpanels
|
---|
[1996] | 242 | ## check for region selection of the spectra and overwrite it.
|
---|
| 243 | ##!!!! currently disabled for consistency with flag tools !!!!
|
---|
| 244 | #if self._selregions.has_key(srow):
|
---|
| 245 | # self._selregions.pop(srow)
|
---|
| 246 | # msg = "The whole spectrum is selected for row="+srow+". Region selection will be overwritten."
|
---|
| 247 | # asaplog.push(msg)
|
---|
| 248 |
|
---|
| 249 | def _getrownum(self,axis):
|
---|
| 250 | ### returns the row number of selected spectrum as a string ###
|
---|
| 251 | plabel = axis.get_title()
|
---|
| 252 | if plabel.startswith("row "):
|
---|
| 253 | return plabel.strip("row ")
|
---|
| 254 | return None
|
---|
| 255 |
|
---|
| 256 | def _any_selection(self):
|
---|
| 257 | ### returns if users have selected any spectrum or region ###
|
---|
| 258 | if len(self._selpanels) or len(self._selregions):
|
---|
| 259 | return True
|
---|
| 260 | return False
|
---|
| 261 |
|
---|
| 262 | def _plot_selections(self,regions=None,panels=None):
|
---|
| 263 | ### mark panels/spectra selections in the page
|
---|
| 264 | if not self._any_selection() and not (regions or panels):
|
---|
| 265 | return
|
---|
| 266 | regions = regions or self._selregions.copy() or {}
|
---|
| 267 | panels = panels or self._selpanels or []
|
---|
| 268 | if not isinstance(regions,dict):
|
---|
| 269 | asaplog.post()
|
---|
| 270 | asaplog.push("Invalid region specification")
|
---|
| 271 | asaplog.post('ERROR')
|
---|
| 272 | if not isinstance(panels,list):
|
---|
| 273 | asaplog.post()
|
---|
| 274 | asaplog.push("Invalid panel specification")
|
---|
| 275 | asaplog.post('ERROR')
|
---|
| 276 | strow = self._getrownum(self.plotter._plotter.subplots[0]['axes'])
|
---|
| 277 | enrow = self._getrownum(self.plotter._plotter.subplots[-1]['axes'])
|
---|
| 278 | for irow in range(int(strow),int(enrow)+1):
|
---|
| 279 | if regions.has_key(str(irow)):
|
---|
| 280 | ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']
|
---|
| 281 | mlist = regions.pop(str(irow))
|
---|
| 282 | for i in range(len(mlist)):
|
---|
| 283 | self._polygons.append(ax.axvspan(mlist[i][0],mlist[i][1],
|
---|
| 284 | facecolor='0.7'))
|
---|
| 285 | del ax,mlist
|
---|
| 286 | if irow in panels:
|
---|
| 287 | ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']
|
---|
| 288 | shadow = Rectangle((0,0),1,1,facecolor='0.7',
|
---|
| 289 | transform=ax.transAxes,visible=True)
|
---|
| 290 | self._polygons.append(ax.add_patch(shadow))
|
---|
| 291 | del ax,shadow
|
---|
[1999] | 292 | #self.plotter._plotter.show(False)
|
---|
| 293 | self.plotter._plotter.canvas.draw()
|
---|
[1996] | 294 | del regions,panels,strow,enrow
|
---|
| 295 |
|
---|
[2062] | 296 | def _clear_selection_plot(self, refresh=True):
|
---|
[1996] | 297 | ### clear up polygons which mark selected spectra and regions ###
|
---|
[1998] | 298 | if len(self._polygons) > 0:
|
---|
[1996] | 299 | for shadow in self._polygons:
|
---|
| 300 | shadow.remove()
|
---|
[2062] | 301 | if refresh: self.plotter._plotter.canvas.draw()
|
---|
[1996] | 302 | self._polygons = []
|
---|
| 303 |
|
---|
[2062] | 304 | def _clearup_selections(self, refresh=True):
|
---|
[1996] | 305 | # clear-up selection and polygons
|
---|
| 306 | self._selpanels = []
|
---|
| 307 | self._selregions = {}
|
---|
[2062] | 308 | self._clear_selection_plot(refresh=refresh)
|
---|
[1996] | 309 |
|
---|
| 310 | ### clear up selections
|
---|
| 311 | def cancel_select(self):
|
---|
| 312 | self.figmgr.toolbar.set_message('selections canceled')
|
---|
| 313 | # clear-up selection and polygons
|
---|
[2062] | 314 | self._clearup_selections(refresh=True)
|
---|
[1996] | 315 |
|
---|
| 316 | ### flag selected spectra/regions
|
---|
| 317 | @asaplog_post_dec
|
---|
| 318 | def flag(self):
|
---|
| 319 | if not self._any_selection():
|
---|
| 320 | msg = "No selection to be Flagged"
|
---|
| 321 | asaplog.post()
|
---|
| 322 | asaplog.push(msg)
|
---|
| 323 | asaplog.post('WARN')
|
---|
| 324 | return
|
---|
[2062] | 325 | self._pause_buttons(operation="start",msg="Flagging data...")
|
---|
[1996] | 326 | self._flag_operation(rows=self._selpanels,
|
---|
| 327 | regions=self._selregions,unflag=False)
|
---|
[2000] | 328 | sout = "Flagged:\n"
|
---|
| 329 | sout += " rows = "+str(self._selpanels)+"\n"
|
---|
| 330 | sout += " regions: "+str(self._selregions)
|
---|
| 331 | asaplog.push(sout)
|
---|
| 332 | del sout
|
---|
[2062] | 333 | self._clearup_selections(refresh=False)
|
---|
[1996] | 334 | self._plot_page(pagemode="current")
|
---|
[2062] | 335 | self._pause_buttons(operation="end")
|
---|
[1996] | 336 |
|
---|
| 337 | ### unflag selected spectra/regions
|
---|
| 338 | @asaplog_post_dec
|
---|
| 339 | def unflag(self):
|
---|
| 340 | if not self._any_selection():
|
---|
| 341 | msg = "No selection to be Flagged"
|
---|
| 342 | asaplog.push(msg)
|
---|
| 343 | asaplog.post('WARN')
|
---|
| 344 | return
|
---|
[2062] | 345 | self._pause_buttons(operation="start",msg="Unflagging data...")
|
---|
[1996] | 346 | self._flag_operation(rows=self._selpanels,
|
---|
| 347 | regions=self._selregions,unflag=True)
|
---|
[2000] | 348 | sout = "Unflagged:\n"
|
---|
| 349 | sout += " rows = "+str(self._selpanels)+"\n"
|
---|
| 350 | sout += " regions: "+str(self._selregions)
|
---|
| 351 | asaplog.push(sout)
|
---|
| 352 | del sout
|
---|
[2062] | 353 | self._clearup_selections(refresh=False)
|
---|
[1996] | 354 | self._plot_page(pagemode="current")
|
---|
[2062] | 355 | self._pause_buttons(operation="end")
|
---|
[1996] | 356 |
|
---|
| 357 | ### actual flag operation
|
---|
| 358 | @asaplog_post_dec
|
---|
| 359 | def _flag_operation(self,rows=None,regions=None,unflag=False):
|
---|
| 360 | scan = self.plotter._data
|
---|
| 361 | if not scan:
|
---|
| 362 | asaplog.post()
|
---|
| 363 | asaplog.push("Invalid scantable")
|
---|
| 364 | asaplog.post("ERROR")
|
---|
| 365 | if isinstance(rows,list) and len(rows) > 0:
|
---|
| 366 | scan.flag_row(rows=rows,unflag=unflag)
|
---|
| 367 | if isinstance(regions,dict) and len(regions) > 0:
|
---|
| 368 | for srow, masklist in regions.iteritems():
|
---|
| 369 | if not isinstance(masklist,list) or len(masklist) ==0:
|
---|
| 370 | msg = "Ignoring invalid region selection for row = "+srow
|
---|
| 371 | asaplog.post()
|
---|
| 372 | asaplog.push(msg)
|
---|
| 373 | asaplog.post("WARN")
|
---|
| 374 | continue
|
---|
| 375 | irow = int(srow)
|
---|
| 376 | mask = scan.create_mask(masklist,invert=False,row=irow)
|
---|
| 377 | scan.flag(row=irow,mask=mask,unflag=unflag)
|
---|
| 378 | del irow, mask
|
---|
| 379 | del srow, masklist
|
---|
| 380 | del scan
|
---|
| 381 |
|
---|
| 382 | ### show statistics of selected spectra/regions
|
---|
[2000] | 383 | @asaplog_post_dec
|
---|
[1996] | 384 | def stat_cal(self):
|
---|
[2000] | 385 | if not self._any_selection():
|
---|
| 386 | msg = "No selection to be calculated"
|
---|
| 387 | asaplog.push(msg)
|
---|
| 388 | asaplog.post('WARN')
|
---|
| 389 | return
|
---|
| 390 | #print "Calculate statistics of selected region and spectra"
|
---|
| 391 | self._selected_stats(rows=self._selpanels,regions=self._selregions)
|
---|
| 392 | #print "Statistics: "
|
---|
| 393 | #print "- rows: ",self._selpanels
|
---|
| 394 | #print "- regions:\n ",self._selregions
|
---|
[2062] | 395 | self._clearup_selections(refresh=True)
|
---|
[1996] | 396 |
|
---|
[2000] | 397 | @asaplog_post_dec
|
---|
| 398 | def _selected_stats(self,rows=None,regions=None):
|
---|
| 399 | scan = self.plotter._data
|
---|
| 400 | if not scan:
|
---|
| 401 | asaplog.post()
|
---|
| 402 | asaplog.push("Invalid scantable")
|
---|
| 403 | asaplog.post("ERROR")
|
---|
| 404 | mathobj = stmath( rcParams['insitu'] )
|
---|
| 405 | statval={}
|
---|
| 406 | statstr = ['max', 'min', 'mean', 'median', 'sum', 'stddev', 'rms']
|
---|
| 407 | if isinstance(rows,list) and len(rows) > 0:
|
---|
| 408 | for irow in rows:
|
---|
| 409 | for stat in statstr:
|
---|
| 410 | #statval[stat] = scan.stats(stat=stat,row=irow)[0]
|
---|
| 411 | statval[stat] = mathobj._statsrow(scan,[],stat,irow)[0]
|
---|
| 412 | self._print_stats(scan,irow,statval,statstr=statstr)
|
---|
| 413 | del irow
|
---|
| 414 | if isinstance(regions,dict) and len(regions) > 0:
|
---|
| 415 | for srow, masklist in regions.iteritems():
|
---|
| 416 | if not isinstance(masklist,list) or len(masklist) ==0:
|
---|
| 417 | msg = "Ignoring invalid region selection for row = "+srow
|
---|
| 418 | asaplog.post()
|
---|
| 419 | asaplog.push(msg)
|
---|
| 420 | asaplog.post("WARN")
|
---|
| 421 | continue
|
---|
| 422 | irow = int(srow)
|
---|
| 423 | mask = scan.create_mask(masklist,invert=False,row=irow)
|
---|
| 424 | for stat in statstr:
|
---|
| 425 | #statval[stat] = scan.stats(stat=stat,row=irow,mask = mask)[0]
|
---|
| 426 | statval[stat] = mathobj._statsrow(scan,mask,stat,irow)[0]
|
---|
| 427 | self._print_stats(scan,irow,statval,statstr=statstr,mask=masklist)
|
---|
| 428 | del irow, mask
|
---|
| 429 | del srow, masklist
|
---|
| 430 | del scan, statval, mathobj
|
---|
| 431 |
|
---|
| 432 | @asaplog_post_dec
|
---|
| 433 | def _print_stats(self,scan,row,stats,statstr=None,mask=None):
|
---|
| 434 | if not isinstance(scan, scantable):
|
---|
| 435 | asaplog.post()
|
---|
| 436 | asaplog.push("Invalid scantable")
|
---|
| 437 | asaplog.post("ERROR")
|
---|
| 438 | if row < 0 or row > scan.nrow():
|
---|
| 439 | asaplog.post()
|
---|
| 440 | asaplog.push("Invalid row number")
|
---|
| 441 | asaplog.post("ERROR")
|
---|
| 442 | if not isinstance(stats,dict) or len(stats) == 0:
|
---|
| 443 | asaplog.post()
|
---|
| 444 | asaplog.push("Invalid statistic value")
|
---|
| 445 | asaplog.post("ERROR")
|
---|
| 446 | maskstr = "All"
|
---|
| 447 | if mask:
|
---|
| 448 | maskstr = str(mask)
|
---|
| 449 | ssep = "-"*70+"\n"
|
---|
| 450 | sout = ssep
|
---|
| 451 | sout += ("Row=%d Scan=%d IF=%d Pol=%d Time=%s mask=%s" % \
|
---|
| 452 | (row, scan.getscan(row), scan.getif(row), scan.getpol(row), scan.get_time(row),maskstr))
|
---|
| 453 | sout += "\n"
|
---|
| 454 | statvals = []
|
---|
| 455 | if not len(statstr):
|
---|
| 456 | statstr = stats.keys()
|
---|
| 457 | for key in statstr:
|
---|
| 458 | sout += key.ljust(10)
|
---|
| 459 | statvals.append(stats.pop(key))
|
---|
| 460 | sout += "\n"
|
---|
| 461 | sout += ("%f "*len(statstr) % tuple(statvals))
|
---|
| 462 | sout += "\n"+ssep
|
---|
| 463 | asaplog.push(sout)
|
---|
| 464 | del sout, ssep, maskstr, statvals, key, scan, row, stats, statstr, mask
|
---|
| 465 |
|
---|
[1996] | 466 | ### Page chages
|
---|
| 467 | ### go to the previous page
|
---|
| 468 | def prev_page(self):
|
---|
[2062] | 469 | #self.figmgr.toolbar.set_message('plotting the previous page')
|
---|
| 470 | self._pause_buttons(operation="start",msg='plotting the previous page')
|
---|
| 471 | self._clear_selection_plot(refresh=False)
|
---|
[1996] | 472 | self._plot_page(pagemode="prev")
|
---|
| 473 | self._plot_selections()
|
---|
[2062] | 474 | self._pause_buttons(operation="end")
|
---|
[1996] | 475 |
|
---|
| 476 | ### go to the next page
|
---|
| 477 | def next_page(self):
|
---|
[2062] | 478 | #self.figmgr.toolbar.set_message('plotting the next page')
|
---|
| 479 | self._pause_buttons(operation="start",msg='plotting the next page')
|
---|
| 480 | self._clear_selection_plot(refresh=False)
|
---|
[1996] | 481 | self._plot_page(pagemode="next")
|
---|
| 482 | self._plot_selections()
|
---|
[2062] | 483 | self._pause_buttons(operation="end")
|
---|
[1996] | 484 |
|
---|
| 485 | ### actual plotting of the new page
|
---|
| 486 | def _plot_page(self,pagemode="next"):
|
---|
| 487 | if self.plotter._startrow <= 0:
|
---|
| 488 | msg = "The page counter is reset due to chages of plot settings. "
|
---|
| 489 | msg += "Plotting from the first page."
|
---|
| 490 | asaplog.post()
|
---|
| 491 | asaplog.push(msg)
|
---|
| 492 | asaplog.post('WARN')
|
---|
| 493 | goback = False
|
---|
| 494 |
|
---|
| 495 | self.plotter._plotter.hold()
|
---|
| 496 | self.plotter._plotter.legend(1)
|
---|
| 497 | #if goback:
|
---|
| 498 | # self._set_prevpage_counter()
|
---|
| 499 | self._set_plot_counter(pagemode)
|
---|
| 500 | #self.plotter._plotter.clear()
|
---|
| 501 | self.plotter._plot(self.plotter._data)
|
---|
| 502 | self.set_pagecounter(self._get_pagenum())
|
---|
| 503 | self.plotter._plotter.release()
|
---|
| 504 | self.plotter._plotter.tidy()
|
---|
| 505 | self.plotter._plotter.show(hardrefresh=False)
|
---|
| 506 |
|
---|
| 507 | ### calculate the panel ID and start row to plot a page
|
---|
| 508 | #def _set_prevpage_counter(self):
|
---|
| 509 | def _set_plot_counter(self, pagemode):
|
---|
| 510 | ## page operation should be either "previous", "current", or "next"
|
---|
| 511 | availpage = ["p","c","n"]
|
---|
| 512 | pageop = pagemode[0].lower()
|
---|
| 513 | if not (pageop in availpage):
|
---|
| 514 | asaplog.post()
|
---|
[2000] | 515 | asaplog.push("Invalid page operation")
|
---|
| 516 | asaplog.post("ERROR")
|
---|
[1996] | 517 | if pageop == "n":
|
---|
| 518 | # nothing necessary to plot the next page
|
---|
| 519 | return
|
---|
| 520 | # set row and panel counters to those of the 1st panel of previous page
|
---|
| 521 | maxpanel = 16
|
---|
| 522 | # the ID of the last panel in current plot
|
---|
| 523 | lastpanel = self.plotter._ipanel
|
---|
| 524 | # the number of current subplots
|
---|
| 525 | currpnum = len(self.plotter._plotter.subplots)
|
---|
| 526 |
|
---|
| 527 | # the nuber of previous subplots
|
---|
| 528 | start_ipanel = None
|
---|
| 529 | if pageop == "c":
|
---|
| 530 | start_ipanel = max(lastpanel-currpnum+1, 0)
|
---|
| 531 | else:
|
---|
| 532 | ## previous page
|
---|
| 533 | prevpnum = None
|
---|
| 534 | if self.plotter._rows and self.plotter._cols:
|
---|
| 535 | # when user set layout
|
---|
| 536 | prevpnum = self.plotter._rows*self.plotter._cols
|
---|
| 537 | else:
|
---|
| 538 | # no user specification
|
---|
| 539 | prevpnum = maxpanel
|
---|
| 540 | start_ipanel = max(lastpanel-currpnum-prevpnum+1, 0)
|
---|
| 541 | del prevpnum
|
---|
| 542 |
|
---|
| 543 | # set the pannel ID of the last panel of the prev(-prev) page
|
---|
| 544 | self.plotter._ipanel = start_ipanel-1
|
---|
| 545 | if self.plotter._panelling == 'r':
|
---|
| 546 | self.plotter._startrow = start_ipanel
|
---|
| 547 | else:
|
---|
| 548 | # the start row number of the next panel
|
---|
| 549 | self.plotter._startrow = self.plotter._panelrows[start_ipanel]
|
---|
| 550 | del lastpanel,currpnum,start_ipanel
|
---|
| 551 |
|
---|
| 552 | ### refresh the page counter
|
---|
| 553 | def set_pagecounter(self,page):
|
---|
| 554 | nwidth = int(numpy.ceil(numpy.log10(max(page,1))))+1
|
---|
| 555 | nwidth = max(nwidth,4)
|
---|
| 556 | formatstr = '%'+str(nwidth)+'d'
|
---|
| 557 | self.show_pagenum(page,formatstr)
|
---|
| 558 |
|
---|
| 559 | def show_pagenum(self,pagenum,formatstr):
|
---|
| 560 | # passed to backend dependent class
|
---|
| 561 | pass
|
---|
| 562 |
|
---|
| 563 | def _get_pagenum(self):
|
---|
| 564 | maxpanel = 16
|
---|
| 565 | # get the ID of last panel in the current page
|
---|
| 566 | idlastpanel = self.plotter._ipanel
|
---|
| 567 | if self.plotter._rows and self.plotter._cols:
|
---|
| 568 | ppp = self.plotter._rows*self.plotter._cols
|
---|
| 569 | else:
|
---|
| 570 | ppp = maxpanel
|
---|
| 571 | return int(idlastpanel/ppp)+1
|
---|
| 572 |
|
---|
[2062] | 573 | # pause buttons for slow operations. implemented at a backend dependent class
|
---|
| 574 | def _pause_buttons(self,operation="end",msg=""):
|
---|
| 575 | pass
|
---|
| 576 |
|
---|
[1996] | 577 | #####################################
|
---|
| 578 | ## Backend dependent Classes ##
|
---|
| 579 | #####################################
|
---|
| 580 | ### TkAgg
|
---|
| 581 | if matplotlib.get_backend() == 'TkAgg':
|
---|
| 582 | import Tkinter as Tk
|
---|
| 583 | from notationwindow import NotationWindowTkAgg
|
---|
| 584 |
|
---|
| 585 | class CustomFlagToolbarTkAgg(CustomFlagToolbarCommon, Tk.Frame):
|
---|
| 586 | def __init__(self,parent):
|
---|
| 587 | from asap.asapplotter import asapplotter
|
---|
| 588 | if not isinstance(parent,asapplotter):
|
---|
| 589 | return False
|
---|
| 590 | if not parent._plotter:
|
---|
| 591 | return False
|
---|
| 592 | self._p = parent._plotter
|
---|
| 593 | self.figmgr = self._p.figmgr
|
---|
| 594 | self.canvas = self.figmgr.canvas
|
---|
| 595 | self.mode = ''
|
---|
| 596 | self.button = True
|
---|
| 597 | self.pagecount = None
|
---|
| 598 | CustomFlagToolbarCommon.__init__(self,parent)
|
---|
| 599 | self.notewin=NotationWindowTkAgg(master=self.canvas)
|
---|
| 600 | self._add_custom_toolbar()
|
---|
| 601 |
|
---|
| 602 | def _add_custom_toolbar(self):
|
---|
| 603 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
---|
| 604 | #self.bSpec=self._NewButton(master=self,
|
---|
| 605 | # text='spec value',
|
---|
| 606 | # command=self.spec_show)
|
---|
| 607 |
|
---|
| 608 | self.bRegion=self._NewButton(master=self,
|
---|
| 609 | text='region',
|
---|
| 610 | command=self.select_region)
|
---|
| 611 | self.bPanel=self._NewButton(master=self,
|
---|
| 612 | text='panel',
|
---|
| 613 | command=self.select_panel)
|
---|
| 614 | self.bClear=self._NewButton(master=self,
|
---|
| 615 | text='clear',
|
---|
| 616 | command=self.cancel_select)
|
---|
| 617 | self.bFlag=self._NewButton(master=self,
|
---|
| 618 | text='flag',
|
---|
| 619 | command=self.flag)
|
---|
| 620 | self.bUnflag=self._NewButton(master=self,
|
---|
| 621 | text='unflag',
|
---|
| 622 | command=self.unflag)
|
---|
| 623 |
|
---|
| 624 | self.bStat=self._NewButton(master=self,
|
---|
| 625 | text='statistics',
|
---|
| 626 | command=self.stat_cal)
|
---|
| 627 |
|
---|
| 628 | self.bNote=self._NewButton(master=self,
|
---|
| 629 | text='notation',
|
---|
| 630 | command=self.modify_note)
|
---|
| 631 |
|
---|
| 632 | self.bQuit=self._NewButton(master=self,
|
---|
| 633 | text='Quit',
|
---|
| 634 | #file="stock_close.ppm",
|
---|
| 635 | command=self.quit,
|
---|
| 636 | side=Tk.RIGHT)
|
---|
| 637 |
|
---|
| 638 | # page change oparations
|
---|
| 639 | frPage = Tk.Frame(master=self,borderwidth=2,relief=Tk.GROOVE)
|
---|
| 640 | frPage.pack(ipadx=2,padx=10,side=Tk.RIGHT)
|
---|
| 641 | self.lPagetitle = Tk.Label(master=frPage,text='Page:',padx=5)
|
---|
| 642 | #width=8,anchor=Tk.E,padx=5)
|
---|
| 643 | self.lPagetitle.pack(side=Tk.LEFT)
|
---|
| 644 | self.pagecount = Tk.StringVar(master=frPage)
|
---|
| 645 | self.lPagecount = Tk.Label(master=frPage,
|
---|
| 646 | textvariable=self.pagecount,
|
---|
| 647 | padx=5,bg='white')
|
---|
| 648 | self.lPagecount.pack(side=Tk.LEFT,padx=3)
|
---|
| 649 |
|
---|
| 650 | self.bNext=self._NewButton(master=frPage,
|
---|
| 651 | text=' + ',
|
---|
| 652 | #file="hand.ppm",
|
---|
| 653 | command=self.next_page)
|
---|
| 654 | self.bPrev=self._NewButton(master=frPage,
|
---|
| 655 | text=' - ',
|
---|
| 656 | command=self.prev_page)
|
---|
| 657 |
|
---|
| 658 | if os.uname()[0] != 'Darwin':
|
---|
| 659 | self.bPrev.config(padx=5)
|
---|
| 660 | self.bNext.config(padx=5)
|
---|
| 661 |
|
---|
| 662 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
---|
| 663 | self.pagecount.set(' '*4)
|
---|
| 664 |
|
---|
| 665 | self.disable_button()
|
---|
| 666 | return #self
|
---|
| 667 |
|
---|
| 668 | def _NewButton(self, master, text, command, side=Tk.LEFT,file=None):
|
---|
| 669 | img = None
|
---|
| 670 | if file:
|
---|
| 671 | file = os.path.join(matplotlib.rcParams['datapath'], 'images', file)
|
---|
| 672 | img = Tk.PhotoImage(master=master, file=file)
|
---|
| 673 |
|
---|
| 674 | if os.uname()[0] == 'Darwin':
|
---|
| 675 | b = Tk.Button(master=master, text=text, image=img,
|
---|
| 676 | command=command)
|
---|
| 677 | if img: b.image = img
|
---|
| 678 | else:
|
---|
| 679 | b = Tk.Button(master=master, text=text, image=img, padx=2, pady=2,
|
---|
| 680 | command=command)
|
---|
| 681 | if img: b.image = img
|
---|
| 682 | b.pack(side=side)
|
---|
| 683 | return b
|
---|
| 684 |
|
---|
| 685 | def show_pagenum(self,pagenum,formatstr):
|
---|
| 686 | self.pagecount.set(formatstr % (pagenum))
|
---|
| 687 |
|
---|
| 688 | def spec_show(self):
|
---|
| 689 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 690 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
---|
| 691 | if self.mode == 'spec': return
|
---|
| 692 | self.mode='spec'
|
---|
| 693 | self.notewin.close_widgets()
|
---|
| 694 | self.__disconnect_event()
|
---|
| 695 | self._p.register('button_press',self._select_spectrum)
|
---|
| 696 |
|
---|
| 697 | def modify_note(self):
|
---|
| 698 | if not self.figmgr.toolbar.mode == '': return
|
---|
| 699 | self.figmgr.toolbar.set_message('text: select a position/text')
|
---|
| 700 | if self.mode == 'note':
|
---|
| 701 | self.bNote.config(relief='raised')
|
---|
| 702 | self.mode='none'
|
---|
| 703 | self.spec_show()
|
---|
| 704 | return
|
---|
| 705 | self.bNote.config(relief='sunken')
|
---|
| 706 | self.bRegion.config(relief='raised')
|
---|
| 707 | self.bPanel.config(relief='raised')
|
---|
| 708 | self.mode='note'
|
---|
| 709 | self.__disconnect_event()
|
---|
| 710 | self._p.register('button_press',self._mod_note)
|
---|
| 711 |
|
---|
| 712 | def select_region(self):
|
---|
| 713 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 714 | self.figmgr.toolbar.set_message('select rectangle regions')
|
---|
| 715 | if self.mode == 'region':
|
---|
| 716 | self.bRegion.config(relief='raised')
|
---|
| 717 | self.mode='none'
|
---|
| 718 | self.spec_show()
|
---|
| 719 | return
|
---|
| 720 | self.bNote.config(relief='raised')
|
---|
| 721 | self.bRegion.config(relief='sunken')
|
---|
| 722 | self.bPanel.config(relief='raised')
|
---|
| 723 | self.mode='region'
|
---|
| 724 | self.notewin.close_widgets()
|
---|
| 725 | self.__disconnect_event()
|
---|
| 726 | self._p.register('button_press',self._add_region)
|
---|
| 727 |
|
---|
| 728 | def select_panel(self):
|
---|
| 729 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
| 730 | self.figmgr.toolbar.set_message('select panels')
|
---|
| 731 | if self.mode == 'panel':
|
---|
| 732 | self.bPanel.config(relief='raised')
|
---|
| 733 | self.mode='none'
|
---|
| 734 | self.spec_show()
|
---|
| 735 | return
|
---|
| 736 | self.bNote.config(relief='raised')
|
---|
| 737 | self.bRegion.config(relief='raised')
|
---|
| 738 | self.bPanel.config(relief='sunken')
|
---|
| 739 | self.mode='panel'
|
---|
| 740 | self.notewin.close_widgets()
|
---|
| 741 | self.__disconnect_event()
|
---|
| 742 | self._p.register('button_press',self._add_panel)
|
---|
| 743 |
|
---|
| 744 | def quit(self):
|
---|
| 745 | self.__disconnect_event()
|
---|
| 746 | #self.delete_bar()
|
---|
| 747 | self.disable_button()
|
---|
| 748 | self.figmgr.window.wm_withdraw()
|
---|
| 749 |
|
---|
| 750 | def enable_button(self):
|
---|
| 751 | if self.button: return
|
---|
| 752 | self.bRegion.config(state=Tk.NORMAL)
|
---|
| 753 | self.bPanel.config(state=Tk.NORMAL)
|
---|
| 754 | self.bClear.config(state=Tk.NORMAL)
|
---|
| 755 | self.bFlag.config(state=Tk.NORMAL)
|
---|
| 756 | self.bUnflag.config(state=Tk.NORMAL)
|
---|
| 757 | self.bStat.config(state=Tk.NORMAL)
|
---|
| 758 | self.button=True
|
---|
| 759 | self.spec_show()
|
---|
| 760 |
|
---|
| 761 | def disable_button(self):
|
---|
| 762 | ## disable buttons which don't work for plottp
|
---|
| 763 | if not self.button: return
|
---|
[2062] | 764 | self.bRegion.config(relief='raised')
|
---|
| 765 | self.bPanel.config(relief='raised')
|
---|
| 766 | self.bRegion.config(state=Tk.DISABLED)
|
---|
| 767 | self.bPanel.config(state=Tk.DISABLED)
|
---|
[1996] | 768 | self.bClear.config(state=Tk.DISABLED)
|
---|
| 769 | self.bFlag.config(state=Tk.DISABLED)
|
---|
| 770 | self.bUnflag.config(state=Tk.DISABLED)
|
---|
| 771 | self.bStat.config(state=Tk.DISABLED)
|
---|
| 772 | self.bNext.config(state=Tk.DISABLED)
|
---|
| 773 | self.bPrev.config(state=Tk.DISABLED)
|
---|
| 774 | self.button=False
|
---|
| 775 | self.mode=''
|
---|
| 776 | self.notewin.close_widgets()
|
---|
| 777 | self.__disconnect_event()
|
---|
| 778 |
|
---|
| 779 | def enable_next(self):
|
---|
| 780 | self.bNext.config(state=Tk.NORMAL)
|
---|
| 781 |
|
---|
| 782 | def disable_next(self):
|
---|
| 783 | self.bNext.config(state=Tk.DISABLED)
|
---|
| 784 |
|
---|
| 785 | def enable_prev(self):
|
---|
| 786 | self.bPrev.config(state=Tk.NORMAL)
|
---|
| 787 |
|
---|
| 788 | def disable_prev(self):
|
---|
| 789 | self.bPrev.config(state=Tk.DISABLED)
|
---|
| 790 |
|
---|
[2062] | 791 | # pause buttons for slow operations
|
---|
| 792 | def _pause_buttons(self,operation="end",msg=""):
|
---|
| 793 | buttons = ["bRegion","bPanel","bClear","bFlag","bUnflag","bStat",
|
---|
| 794 | "bNote","bQuit"]
|
---|
| 795 | if operation == "start":
|
---|
| 796 | state=Tk.DISABLED
|
---|
| 797 | else:
|
---|
| 798 | state=Tk.NORMAL
|
---|
| 799 | for btn in buttons:
|
---|
| 800 | getattr(self,btn).config(state=state)
|
---|
| 801 | self.figmgr.toolbar.set_message(msg)
|
---|
| 802 |
|
---|
| 803 |
|
---|
[1996] | 804 | def delete_bar(self):
|
---|
| 805 | self.__disconnect_event()
|
---|
| 806 | self.destroy()
|
---|
| 807 |
|
---|
| 808 | def __disconnect_event(self):
|
---|
| 809 | self._p.register('button_press',None)
|
---|
| 810 | self._p.register('button_release',None)
|
---|