| 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
|
|---|
| 159 | #print "add the region to selections"
|
|---|
| 160 | #self._thisregion = {'axes': event.inaxes,'xs': event.xdata,
|
|---|
| 161 | self._thisregion = {'axes': event.inaxes,'xs': event.x,
|
|---|
| 162 | 'worldx': [event.xdata,event.xdata]}
|
|---|
| 163 | self.plotter._plotter.register('button_press',None)
|
|---|
| 164 | self.plotter._plotter.register('motion_notify', self._xspan_draw)
|
|---|
| 165 | self.plotter._plotter.register('button_press', self._xspan_end)
|
|---|
| 166 |
|
|---|
| 167 | def _xspan_draw(self,event):
|
|---|
| 168 | if event.inaxes == self._thisregion['axes']:
|
|---|
| 169 | #xnow = event.xdata
|
|---|
| 170 | xnow = event.x
|
|---|
| 171 | self.xdataold = xnow
|
|---|
| 172 | else:
|
|---|
| 173 | xnow = self.xdataold
|
|---|
| 174 | try: self.lastspan
|
|---|
| 175 | except AttributeError: pass
|
|---|
| 176 | #else: self.lastspan.remove()
|
|---|
| 177 | else: self._remove_span(self.lastspan)
|
|---|
| 178 |
|
|---|
| 179 | #self.lastspan = self._thisregion['axes'].axvspan(self._thisregion['xs'],xnow,facecolor='0.7')
|
|---|
| 180 | self.lastspan = self._draw_span(self._thisregion['axes'],self._thisregion['xs'],xnow,fill="#555555",stipple="gray50")
|
|---|
| 181 | #self.plotter._plotter.show(False)
|
|---|
| 182 | #self.plotter._plotter.canvas.draw()
|
|---|
| 183 | del xnow
|
|---|
| 184 |
|
|---|
| 185 | def _draw_span(self,axes,x0,x1,**kwargs):
|
|---|
| 186 | pass
|
|---|
| 187 |
|
|---|
| 188 | def _remove_span(self,span):
|
|---|
| 189 | pass
|
|---|
| 190 |
|
|---|
| 191 | @asaplog_post_dec
|
|---|
| 192 | def _xspan_end(self,event):
|
|---|
| 193 | if not self.figmgr.toolbar.mode == '':
|
|---|
| 194 | return
|
|---|
| 195 | if event.button != 1:
|
|---|
| 196 | return
|
|---|
| 197 |
|
|---|
| 198 | try: self.lastspan
|
|---|
| 199 | except AttributeError: pass
|
|---|
| 200 | else:
|
|---|
| 201 | #self.lastspan.remove()
|
|---|
| 202 | self._remove_span(self.lastspan)
|
|---|
| 203 | del self.lastspan
|
|---|
| 204 | if event.inaxes == self._thisregion['axes']:
|
|---|
| 205 | xdataend = event.xdata
|
|---|
| 206 | else:
|
|---|
| 207 | xdataend=self.xdataold
|
|---|
| 208 |
|
|---|
| 209 | self._thisregion['worldx'][1] = xdataend
|
|---|
| 210 | lregion = self._thisregion['worldx']
|
|---|
| 211 | pregion = self._thisregion['axes'].axvspan(lregion[0],lregion[1],
|
|---|
| 212 | facecolor='0.7')
|
|---|
| 213 | #self.plotter._plotter.show(False)
|
|---|
| 214 | self.plotter._plotter.canvas.draw()
|
|---|
| 215 | self._polygons.append(pregion)
|
|---|
| 216 | srow = self._getrownum(self._thisregion['axes'])
|
|---|
| 217 | irow = int(srow)
|
|---|
| 218 | if not self._selregions.has_key(srow):
|
|---|
| 219 | self._selregions[srow] = []
|
|---|
| 220 | self._selregions[srow].append(lregion)
|
|---|
| 221 | del lregion, pregion, xdataend
|
|---|
| 222 | sout = "selected region: "+str(self._thisregion['worldx'])+\
|
|---|
| 223 | "(@row "+str(self._getrownum(self._thisregion['axes']))+")"
|
|---|
| 224 | asaplog.push(sout)
|
|---|
| 225 |
|
|---|
| 226 | # release event
|
|---|
| 227 | self.plotter._plotter.register('button_press',None)
|
|---|
| 228 | self.plotter._plotter.register('motion_notify',None)
|
|---|
| 229 | # Clear up region selection
|
|---|
| 230 | self._thisregion = None
|
|---|
| 231 | self.xdataold = None
|
|---|
| 232 | # finally recover region selection event
|
|---|
| 233 | self.plotter._plotter.register('button_press',self._add_region)
|
|---|
| 234 |
|
|---|
| 235 | ### add panels to selections
|
|---|
| 236 | @asaplog_post_dec
|
|---|
| 237 | def _add_panel(self,event):
|
|---|
| 238 | if not self.figmgr.toolbar.mode == '':
|
|---|
| 239 | return
|
|---|
| 240 | if event.button != 1 or event.inaxes == None:
|
|---|
| 241 | return
|
|---|
| 242 | selax = event.inaxes
|
|---|
| 243 | # this row resolution assumes row panelling
|
|---|
| 244 | srow = self._getrownum(selax)
|
|---|
| 245 | irow = int(srow)
|
|---|
| 246 | if srow:
|
|---|
| 247 | self._selpanels.append(irow)
|
|---|
| 248 | shadow = Rectangle((0,0),1,1,facecolor='0.7',transform=selax.transAxes,visible=True)
|
|---|
| 249 | self._polygons.append(selax.add_patch(shadow))
|
|---|
| 250 | #self.plotter._plotter.show(False)
|
|---|
| 251 | self.plotter._plotter.canvas.draw()
|
|---|
| 252 | asaplog.push("row "+str(irow)+" is selected")
|
|---|
| 253 | #print "selected rows =",self._selpanels
|
|---|
| 254 | ## check for region selection of the spectra and overwrite it.
|
|---|
| 255 | ##!!!! currently disabled for consistency with flag tools !!!!
|
|---|
| 256 | #if self._selregions.has_key(srow):
|
|---|
| 257 | # self._selregions.pop(srow)
|
|---|
| 258 | # msg = "The whole spectrum is selected for row="+srow+". Region selection will be overwritten."
|
|---|
| 259 | # asaplog.push(msg)
|
|---|
| 260 |
|
|---|
| 261 | def _getrownum(self,axis):
|
|---|
| 262 | ### returns the row number of selected spectrum as a string ###
|
|---|
| 263 | plabel = axis.get_title()
|
|---|
| 264 | if plabel.startswith("row "):
|
|---|
| 265 | return plabel.strip("row ")
|
|---|
| 266 | return None
|
|---|
| 267 |
|
|---|
| 268 | def _any_selection(self):
|
|---|
| 269 | ### returns if users have selected any spectrum or region ###
|
|---|
| 270 | if len(self._selpanels) or len(self._selregions):
|
|---|
| 271 | return True
|
|---|
| 272 | return False
|
|---|
| 273 |
|
|---|
| 274 | def _plot_selections(self,regions=None,panels=None):
|
|---|
| 275 | ### mark panels/spectra selections in the page
|
|---|
| 276 | if not self._any_selection() and not (regions or panels):
|
|---|
| 277 | return
|
|---|
| 278 | regions = regions or self._selregions.copy() or {}
|
|---|
| 279 | panels = panels or self._selpanels or []
|
|---|
| 280 | if not isinstance(regions,dict):
|
|---|
| 281 | asaplog.post()
|
|---|
| 282 | asaplog.push("Invalid region specification")
|
|---|
| 283 | asaplog.post('ERROR')
|
|---|
| 284 | if not isinstance(panels,list):
|
|---|
| 285 | asaplog.post()
|
|---|
| 286 | asaplog.push("Invalid panel specification")
|
|---|
| 287 | asaplog.post('ERROR')
|
|---|
| 288 | strow = self._getrownum(self.plotter._plotter.subplots[0]['axes'])
|
|---|
| 289 | enrow = self._getrownum(self.plotter._plotter.subplots[-1]['axes'])
|
|---|
| 290 | for irow in range(int(strow),int(enrow)+1):
|
|---|
| 291 | if regions.has_key(str(irow)):
|
|---|
| 292 | ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']
|
|---|
| 293 | mlist = regions.pop(str(irow))
|
|---|
| 294 | for i in range(len(mlist)):
|
|---|
| 295 | self._polygons.append(ax.axvspan(mlist[i][0],mlist[i][1],
|
|---|
| 296 | facecolor='0.7'))
|
|---|
| 297 | del ax,mlist
|
|---|
| 298 | if irow in panels:
|
|---|
| 299 | ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']
|
|---|
| 300 | shadow = Rectangle((0,0),1,1,facecolor='0.7',
|
|---|
| 301 | transform=ax.transAxes,visible=True)
|
|---|
| 302 | self._polygons.append(ax.add_patch(shadow))
|
|---|
| 303 | del ax,shadow
|
|---|
| 304 | #self.plotter._plotter.show(False)
|
|---|
| 305 | self.plotter._plotter.canvas.draw()
|
|---|
| 306 | del regions,panels,strow,enrow
|
|---|
| 307 |
|
|---|
| 308 | def _clear_selection_plot(self, refresh=True):
|
|---|
| 309 | ### clear up polygons which mark selected spectra and regions ###
|
|---|
| 310 | if len(self._polygons) > 0:
|
|---|
| 311 | for shadow in self._polygons:
|
|---|
| 312 | shadow.remove()
|
|---|
| 313 | if refresh: self.plotter._plotter.canvas.draw()
|
|---|
| 314 | self._polygons = []
|
|---|
| 315 |
|
|---|
| 316 | def _clearup_selections(self, refresh=True):
|
|---|
| 317 | # clear-up selection and polygons
|
|---|
| 318 | self._selpanels = []
|
|---|
| 319 | self._selregions = {}
|
|---|
| 320 | self._clear_selection_plot(refresh=refresh)
|
|---|
| 321 |
|
|---|
| 322 | ### clear up selections
|
|---|
| 323 | def cancel_select(self):
|
|---|
| 324 | self.figmgr.toolbar.set_message('selections canceled')
|
|---|
| 325 | # clear-up selection and polygons
|
|---|
| 326 | self._clearup_selections(refresh=True)
|
|---|
| 327 |
|
|---|
| 328 | ### flag selected spectra/regions
|
|---|
| 329 | @asaplog_post_dec
|
|---|
| 330 | def flag(self):
|
|---|
| 331 | if not self._any_selection():
|
|---|
| 332 | msg = "No selection to be Flagged"
|
|---|
| 333 | asaplog.post()
|
|---|
| 334 | asaplog.push(msg)
|
|---|
| 335 | asaplog.post('WARN')
|
|---|
| 336 | return
|
|---|
| 337 | self._pause_buttons(operation="start",msg="Flagging data...")
|
|---|
| 338 | self._flag_operation(rows=self._selpanels,
|
|---|
| 339 | regions=self._selregions,unflag=False)
|
|---|
| 340 | sout = "Flagged:\n"
|
|---|
| 341 | sout += " rows = "+str(self._selpanels)+"\n"
|
|---|
| 342 | sout += " regions: "+str(self._selregions)
|
|---|
| 343 | asaplog.push(sout)
|
|---|
| 344 | del sout
|
|---|
| 345 | self.plotter._ismodified = True
|
|---|
| 346 | self._clearup_selections(refresh=False)
|
|---|
| 347 | self._plot_page(pagemode="current")
|
|---|
| 348 | self._pause_buttons(operation="end")
|
|---|
| 349 |
|
|---|
| 350 | ### unflag selected spectra/regions
|
|---|
| 351 | @asaplog_post_dec
|
|---|
| 352 | def unflag(self):
|
|---|
| 353 | if not self._any_selection():
|
|---|
| 354 | msg = "No selection to be Flagged"
|
|---|
| 355 | asaplog.push(msg)
|
|---|
| 356 | asaplog.post('WARN')
|
|---|
| 357 | return
|
|---|
| 358 | self._pause_buttons(operation="start",msg="Unflagging data...")
|
|---|
| 359 | self._flag_operation(rows=self._selpanels,
|
|---|
| 360 | regions=self._selregions,unflag=True)
|
|---|
| 361 | sout = "Unflagged:\n"
|
|---|
| 362 | sout += " rows = "+str(self._selpanels)+"\n"
|
|---|
| 363 | sout += " regions: "+str(self._selregions)
|
|---|
| 364 | asaplog.push(sout)
|
|---|
| 365 | del sout
|
|---|
| 366 | self.plotter._ismodified = True
|
|---|
| 367 | self._clearup_selections(refresh=False)
|
|---|
| 368 | self._plot_page(pagemode="current")
|
|---|
| 369 | self._pause_buttons(operation="end")
|
|---|
| 370 |
|
|---|
| 371 | ### actual flag operation
|
|---|
| 372 | @asaplog_post_dec
|
|---|
| 373 | def _flag_operation(self,rows=None,regions=None,unflag=False):
|
|---|
| 374 | scan = self.plotter._data
|
|---|
| 375 | if not scan:
|
|---|
| 376 | asaplog.post()
|
|---|
| 377 | asaplog.push("Invalid scantable")
|
|---|
| 378 | asaplog.post("ERROR")
|
|---|
| 379 | if isinstance(rows,list) and len(rows) > 0:
|
|---|
| 380 | scan.flag_row(rows=rows,unflag=unflag)
|
|---|
| 381 | if isinstance(regions,dict) and len(regions) > 0:
|
|---|
| 382 | for srow, masklist in regions.iteritems():
|
|---|
| 383 | if not isinstance(masklist,list) or len(masklist) ==0:
|
|---|
| 384 | msg = "Ignoring invalid region selection for row = "+srow
|
|---|
| 385 | asaplog.post()
|
|---|
| 386 | asaplog.push(msg)
|
|---|
| 387 | asaplog.post("WARN")
|
|---|
| 388 | continue
|
|---|
| 389 | irow = int(srow)
|
|---|
| 390 | mask = scan.create_mask(masklist,invert=False,row=irow)
|
|---|
| 391 | scan.flag(row=irow,mask=mask,unflag=unflag)
|
|---|
| 392 | del irow, mask
|
|---|
| 393 | del srow, masklist
|
|---|
| 394 | del scan
|
|---|
| 395 |
|
|---|
| 396 | ### show statistics of selected spectra/regions
|
|---|
| 397 | @asaplog_post_dec
|
|---|
| 398 | def stat_cal(self):
|
|---|
| 399 | if not self._any_selection():
|
|---|
| 400 | msg = "No selection to be calculated"
|
|---|
| 401 | asaplog.push(msg)
|
|---|
| 402 | asaplog.post('WARN')
|
|---|
| 403 | return
|
|---|
| 404 | #print "Calculate statistics of selected region and spectra"
|
|---|
| 405 | self._selected_stats(rows=self._selpanels,regions=self._selregions)
|
|---|
| 406 | #print "Statistics: "
|
|---|
| 407 | #print "- rows: ",self._selpanels
|
|---|
| 408 | #print "- regions:\n ",self._selregions
|
|---|
| 409 | self._clearup_selections(refresh=True)
|
|---|
| 410 |
|
|---|
| 411 | @asaplog_post_dec
|
|---|
| 412 | def _selected_stats(self,rows=None,regions=None):
|
|---|
| 413 | scan = self.plotter._data
|
|---|
| 414 | if not scan:
|
|---|
| 415 | asaplog.post()
|
|---|
| 416 | asaplog.push("Invalid scantable")
|
|---|
| 417 | asaplog.post("ERROR")
|
|---|
| 418 | mathobj = stmath( rcParams['insitu'] )
|
|---|
| 419 | statval = {}
|
|---|
| 420 | statstr = ['max', 'min', 'mean', 'median', 'sum', 'stddev', 'rms']
|
|---|
| 421 | if isinstance(rows,list) and len(rows) > 0:
|
|---|
| 422 | for irow in rows:
|
|---|
| 423 | for stat in statstr:
|
|---|
| 424 | #statval[stat] = scan.stats(stat=stat,row=irow)[0]
|
|---|
| 425 | statval[stat] = mathobj._statsrow(scan,[],stat,irow)[0]
|
|---|
| 426 | self._print_stats(scan,irow,statval,statstr=statstr)
|
|---|
| 427 | del irow
|
|---|
| 428 | if isinstance(regions,dict) and len(regions) > 0:
|
|---|
| 429 | for srow, masklist in regions.iteritems():
|
|---|
| 430 | if not isinstance(masklist,list) or len(masklist) ==0:
|
|---|
| 431 | msg = "Ignoring invalid region selection for row = "+srow
|
|---|
| 432 | asaplog.post()
|
|---|
| 433 | asaplog.push(msg)
|
|---|
| 434 | asaplog.post("WARN")
|
|---|
| 435 | continue
|
|---|
| 436 | irow = int(srow)
|
|---|
| 437 | mask = scan.create_mask(masklist,invert=False,row=irow)
|
|---|
| 438 | for stat in statstr:
|
|---|
| 439 | #statval[stat] = scan.stats(stat=stat,row=irow,mask = mask)[0]
|
|---|
| 440 | statval[stat] = mathobj._statsrow(scan,mask,stat,irow)[0]
|
|---|
| 441 | self._print_stats(scan,irow,statval,statstr=statstr,mask=masklist)
|
|---|
| 442 | del irow, mask
|
|---|
| 443 | del srow, masklist
|
|---|
| 444 | del scan, statval, mathobj
|
|---|
| 445 |
|
|---|
| 446 | @asaplog_post_dec
|
|---|
| 447 | def _print_stats(self,scan,row,stats,statstr=None,mask=None):
|
|---|
| 448 | if not isinstance(scan, scantable):
|
|---|
| 449 | asaplog.post()
|
|---|
| 450 | asaplog.push("Invalid scantable")
|
|---|
| 451 | asaplog.post("ERROR")
|
|---|
| 452 | if row < 0 or row > scan.nrow():
|
|---|
| 453 | asaplog.post()
|
|---|
| 454 | asaplog.push("Invalid row number")
|
|---|
| 455 | asaplog.post("ERROR")
|
|---|
| 456 | if not isinstance(stats,dict) or len(stats) == 0:
|
|---|
| 457 | asaplog.post()
|
|---|
| 458 | asaplog.push("Invalid statistic value")
|
|---|
| 459 | asaplog.post("ERROR")
|
|---|
| 460 | maskstr = "All"
|
|---|
| 461 | if mask:
|
|---|
| 462 | maskstr = str(mask)
|
|---|
| 463 | ssep = "-"*70+"\n"
|
|---|
| 464 | sout = ssep
|
|---|
| 465 | sout += ("Row=%d Scan=%d IF=%d Pol=%d Time=%s mask=%s" % \
|
|---|
| 466 | (row, scan.getscan(row), scan.getif(row), scan.getpol(row), scan.get_time(row),maskstr))
|
|---|
| 467 | sout += "\n"
|
|---|
| 468 | statvals = []
|
|---|
| 469 | if not len(statstr):
|
|---|
| 470 | statstr = stats.keys()
|
|---|
| 471 | for key in statstr:
|
|---|
| 472 | sout += key.ljust(10)
|
|---|
| 473 | statvals.append(stats.pop(key))
|
|---|
| 474 | sout += "\n"
|
|---|
| 475 | sout += ("%f "*len(statstr) % tuple(statvals))
|
|---|
| 476 | sout += "\n"+ssep
|
|---|
| 477 | asaplog.push(sout)
|
|---|
| 478 | del sout, ssep, maskstr, statvals, key, scan, row, stats, statstr, mask
|
|---|
| 479 |
|
|---|
| 480 | ### Page chages
|
|---|
| 481 | ### go to the previous page
|
|---|
| 482 | def prev_page(self):
|
|---|
| 483 | #self.figmgr.toolbar.set_message('plotting the previous page')
|
|---|
| 484 | self._pause_buttons(operation="start",msg='plotting the previous page')
|
|---|
| 485 | self._clear_selection_plot(refresh=False)
|
|---|
| 486 | self._plot_page(pagemode="prev")
|
|---|
| 487 | self._plot_selections()
|
|---|
| 488 | self._pause_buttons(operation="end")
|
|---|
| 489 |
|
|---|
| 490 | ### go to the next page
|
|---|
| 491 | def next_page(self):
|
|---|
| 492 | #self.figmgr.toolbar.set_message('plotting the next page')
|
|---|
| 493 | self._pause_buttons(operation="start",msg='plotting the next page')
|
|---|
| 494 | self._clear_selection_plot(refresh=False)
|
|---|
| 495 | self._plot_page(pagemode="next")
|
|---|
| 496 | self._plot_selections()
|
|---|
| 497 | self._pause_buttons(operation="end")
|
|---|
| 498 |
|
|---|
| 499 | ### actual plotting of the new page
|
|---|
| 500 | def _plot_page(self,pagemode="next"):
|
|---|
| 501 | if self.plotter._startrow <= 0:
|
|---|
| 502 | msg = "The page counter is reset due to chages of plot settings. "
|
|---|
| 503 | msg += "Plotting from the first page."
|
|---|
| 504 | asaplog.post()
|
|---|
| 505 | asaplog.push(msg)
|
|---|
| 506 | asaplog.post('WARN')
|
|---|
| 507 | goback = False
|
|---|
| 508 |
|
|---|
| 509 | self.plotter._plotter.hold()
|
|---|
| 510 | self.plotter._plotter.legend(1)
|
|---|
| 511 | #if goback:
|
|---|
| 512 | # self._set_prevpage_counter()
|
|---|
| 513 | self._set_plot_counter(pagemode)
|
|---|
| 514 | #self.plotter._plotter.clear()
|
|---|
| 515 | self.plotter._plot(self.plotter._data)
|
|---|
| 516 | self.set_pagecounter(self._get_pagenum())
|
|---|
| 517 | self.plotter._plotter.release()
|
|---|
| 518 | self.plotter._plotter.tidy()
|
|---|
| 519 | self.plotter._plotter.show(hardrefresh=False)
|
|---|
| 520 |
|
|---|
| 521 | ### calculate the panel ID and start row to plot a page
|
|---|
| 522 | #def _set_prevpage_counter(self):
|
|---|
| 523 | def _set_plot_counter(self, pagemode):
|
|---|
| 524 | ## page operation should be either "previous", "current", or "next"
|
|---|
| 525 | availpage = ["p","c","n"]
|
|---|
| 526 | pageop = pagemode[0].lower()
|
|---|
| 527 | if not (pageop in availpage):
|
|---|
| 528 | asaplog.post()
|
|---|
| 529 | asaplog.push("Invalid page operation")
|
|---|
| 530 | asaplog.post("ERROR")
|
|---|
| 531 | if pageop == "n":
|
|---|
| 532 | # nothing necessary to plot the next page
|
|---|
| 533 | return
|
|---|
| 534 | # set row and panel counters to those of the 1st panel of previous page
|
|---|
| 535 | maxpanel = 16
|
|---|
| 536 | # the ID of the last panel in current plot
|
|---|
| 537 | lastpanel = self.plotter._ipanel
|
|---|
| 538 | # the number of current subplots
|
|---|
| 539 | currpnum = len(self.plotter._plotter.subplots)
|
|---|
| 540 |
|
|---|
| 541 | # the nuber of previous subplots
|
|---|
| 542 | start_ipanel = None
|
|---|
| 543 | if pageop == "c":
|
|---|
| 544 | start_ipanel = max(lastpanel-currpnum+1, 0)
|
|---|
| 545 | else:
|
|---|
| 546 | ## previous page
|
|---|
| 547 | prevpnum = None
|
|---|
| 548 | if self.plotter._rows and self.plotter._cols:
|
|---|
| 549 | # when user set layout
|
|---|
| 550 | prevpnum = self.plotter._rows*self.plotter._cols
|
|---|
| 551 | else:
|
|---|
| 552 | # no user specification
|
|---|
| 553 | prevpnum = maxpanel
|
|---|
| 554 | start_ipanel = max(lastpanel-currpnum-prevpnum+1, 0)
|
|---|
| 555 | del prevpnum
|
|---|
| 556 |
|
|---|
| 557 | # set the pannel ID of the last panel of the prev(-prev) page
|
|---|
| 558 | self.plotter._ipanel = start_ipanel-1
|
|---|
| 559 | if self.plotter._panelling == 'r':
|
|---|
| 560 | self.plotter._startrow = start_ipanel
|
|---|
| 561 | else:
|
|---|
| 562 | # the start row number of the next panel
|
|---|
| 563 | self.plotter._startrow = self.plotter._panelrows[start_ipanel]
|
|---|
| 564 | del lastpanel,currpnum,start_ipanel
|
|---|
| 565 |
|
|---|
| 566 | ### refresh the page counter
|
|---|
| 567 | def set_pagecounter(self,page):
|
|---|
| 568 | nwidth = int(numpy.ceil(numpy.log10(max(page,1))))+1
|
|---|
| 569 | nwidth = max(nwidth,4)
|
|---|
| 570 | formatstr = '%'+str(nwidth)+'d'
|
|---|
| 571 | self.show_pagenum(page,formatstr)
|
|---|
| 572 |
|
|---|
| 573 | def show_pagenum(self,pagenum,formatstr):
|
|---|
| 574 | # passed to backend dependent class
|
|---|
| 575 | pass
|
|---|
| 576 |
|
|---|
| 577 | def _get_pagenum(self):
|
|---|
| 578 | maxpanel = 16
|
|---|
| 579 | # get the ID of last panel in the current page
|
|---|
| 580 | idlastpanel = self.plotter._ipanel
|
|---|
| 581 | if self.plotter._rows and self.plotter._cols:
|
|---|
| 582 | ppp = self.plotter._rows*self.plotter._cols
|
|---|
| 583 | else:
|
|---|
| 584 | ppp = maxpanel
|
|---|
| 585 | return int(idlastpanel/ppp)+1
|
|---|
| 586 |
|
|---|
| 587 | # pause buttons for slow operations. implemented at a backend dependent class
|
|---|
| 588 | def _pause_buttons(self,operation="end",msg=""):
|
|---|
| 589 | pass
|
|---|
| 590 |
|
|---|
| 591 | #####################################
|
|---|
| 592 | ## Backend dependent Classes ##
|
|---|
| 593 | #####################################
|
|---|
| 594 | ### TkAgg
|
|---|
| 595 | if matplotlib.get_backend() == 'TkAgg':
|
|---|
| 596 | import Tkinter as Tk
|
|---|
| 597 | from notationwindow import NotationWindowTkAgg
|
|---|
| 598 |
|
|---|
| 599 | class CustomFlagToolbarTkAgg(CustomFlagToolbarCommon, Tk.Frame):
|
|---|
| 600 | def __init__(self,parent):
|
|---|
| 601 | from asap.asapplotter import asapplotter
|
|---|
| 602 | if not isinstance(parent,asapplotter):
|
|---|
| 603 | return False
|
|---|
| 604 | if not parent._plotter:
|
|---|
| 605 | return False
|
|---|
| 606 | self._p = parent._plotter
|
|---|
| 607 | self.figmgr = self._p.figmgr
|
|---|
| 608 | self.canvas = self.figmgr.canvas
|
|---|
| 609 | self.mode = ''
|
|---|
| 610 | self.button = True
|
|---|
| 611 | self.pagecount = None
|
|---|
| 612 | CustomFlagToolbarCommon.__init__(self,parent)
|
|---|
| 613 | self.notewin=NotationWindowTkAgg(master=self.canvas)
|
|---|
| 614 | self._add_custom_toolbar()
|
|---|
| 615 |
|
|---|
| 616 | def _add_custom_toolbar(self):
|
|---|
| 617 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
|---|
| 618 | #self.bSpec = self._NewButton(master=self,
|
|---|
| 619 | # text='spec value',
|
|---|
| 620 | # command=self.spec_show)
|
|---|
| 621 |
|
|---|
| 622 | self.bRegion = self._NewButton(master=self,
|
|---|
| 623 | text='region',
|
|---|
| 624 | command=self.select_region)
|
|---|
| 625 | self.bPanel = self._NewButton(master=self,
|
|---|
| 626 | text='panel',
|
|---|
| 627 | command=self.select_panel)
|
|---|
| 628 | self.bClear = self._NewButton(master=self,
|
|---|
| 629 | text='clear',
|
|---|
| 630 | command=self.cancel_select)
|
|---|
| 631 | self.bFlag = self._NewButton(master=self,
|
|---|
| 632 | text='flag',
|
|---|
| 633 | command=self.flag)
|
|---|
| 634 | self.bUnflag = self._NewButton(master=self,
|
|---|
| 635 | text='unflag',
|
|---|
| 636 | command=self.unflag)
|
|---|
| 637 |
|
|---|
| 638 | self.bStat = self._NewButton(master=self,
|
|---|
| 639 | text='statistics',
|
|---|
| 640 | command=self.stat_cal)
|
|---|
| 641 |
|
|---|
| 642 | self.bNote = self._NewButton(master=self,
|
|---|
| 643 | text='notation',
|
|---|
| 644 | command=self.modify_note)
|
|---|
| 645 |
|
|---|
| 646 | self.bQuit = self._NewButton(master=self,
|
|---|
| 647 | text='Quit',
|
|---|
| 648 | #file="stock_close.ppm",
|
|---|
| 649 | command=self.quit,
|
|---|
| 650 | side=Tk.RIGHT)
|
|---|
| 651 |
|
|---|
| 652 | # page change oparations
|
|---|
| 653 | frPage = Tk.Frame(master=self,borderwidth=2,relief=Tk.GROOVE)
|
|---|
| 654 | frPage.pack(ipadx=2,padx=10,side=Tk.RIGHT)
|
|---|
| 655 | self.lPagetitle = Tk.Label(master=frPage,text='Page:',padx=5)
|
|---|
| 656 | #width=8,anchor=Tk.E,padx=5)
|
|---|
| 657 | self.lPagetitle.pack(side=Tk.LEFT)
|
|---|
| 658 | self.pagecount = Tk.StringVar(master=frPage)
|
|---|
| 659 | self.lPagecount = Tk.Label(master=frPage,
|
|---|
| 660 | textvariable=self.pagecount,
|
|---|
| 661 | padx=5,bg='white')
|
|---|
| 662 | self.lPagecount.pack(side=Tk.LEFT,padx=3)
|
|---|
| 663 |
|
|---|
| 664 | self.bNext=self._NewButton(master=frPage,
|
|---|
| 665 | text=' + ',
|
|---|
| 666 | #file="hand.ppm",
|
|---|
| 667 | command=self.next_page)
|
|---|
| 668 | self.bPrev=self._NewButton(master=frPage,
|
|---|
| 669 | text=' - ',
|
|---|
| 670 | command=self.prev_page)
|
|---|
| 671 |
|
|---|
| 672 | if os.uname()[0] != 'Darwin':
|
|---|
| 673 | self.bPrev.config(padx=5)
|
|---|
| 674 | self.bNext.config(padx=5)
|
|---|
| 675 |
|
|---|
| 676 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
|---|
| 677 | self.pagecount.set(' '*4)
|
|---|
| 678 |
|
|---|
| 679 | self.disable_button()
|
|---|
| 680 | return #self
|
|---|
| 681 |
|
|---|
| 682 | def _NewButton(self, master, text, command, side=Tk.LEFT,file=None):
|
|---|
| 683 | img = None
|
|---|
| 684 | if file:
|
|---|
| 685 | file = os.path.join(matplotlib.rcParams['datapath'], 'images', file)
|
|---|
| 686 | img = Tk.PhotoImage(master=master, file=file)
|
|---|
| 687 |
|
|---|
| 688 | if os.uname()[0] == 'Darwin':
|
|---|
| 689 | b = Tk.Button(master=master, text=text, image=img,
|
|---|
| 690 | command=command)
|
|---|
| 691 | if img: b.image = img
|
|---|
| 692 | else:
|
|---|
| 693 | b = Tk.Button(master=master, text=text, image=img, padx=2, pady=2,
|
|---|
| 694 | command=command)
|
|---|
| 695 | if img: b.image = img
|
|---|
| 696 | b.pack(side=side)
|
|---|
| 697 | return b
|
|---|
| 698 |
|
|---|
| 699 | def show_pagenum(self,pagenum,formatstr):
|
|---|
| 700 | self.pagecount.set(formatstr % (pagenum))
|
|---|
| 701 |
|
|---|
| 702 | def spec_show(self):
|
|---|
| 703 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
|---|
| 704 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
|---|
| 705 | if self.mode == 'spec': return
|
|---|
| 706 | self.mode = 'spec'
|
|---|
| 707 | self.notewin.close_widgets()
|
|---|
| 708 | self.__disconnect_event()
|
|---|
| 709 | self._p.register('button_press',self._select_spectrum)
|
|---|
| 710 |
|
|---|
| 711 | def modify_note(self):
|
|---|
| 712 | if not self.figmgr.toolbar.mode == '': return
|
|---|
| 713 | self.figmgr.toolbar.set_message('text: select a position/text')
|
|---|
| 714 | if self.mode == 'note':
|
|---|
| 715 | self.bNote.config(relief='raised')
|
|---|
| 716 | self.mode = 'none'
|
|---|
| 717 | self.spec_show()
|
|---|
| 718 | return
|
|---|
| 719 | self.bNote.config(relief='sunken')
|
|---|
| 720 | self.bRegion.config(relief='raised')
|
|---|
| 721 | self.bPanel.config(relief='raised')
|
|---|
| 722 | self.mode = 'note'
|
|---|
| 723 | self.__disconnect_event()
|
|---|
| 724 | self._p.register('button_press',self._mod_note)
|
|---|
| 725 |
|
|---|
| 726 | def select_region(self):
|
|---|
| 727 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
|---|
| 728 | self.figmgr.toolbar.set_message('select rectangle regions')
|
|---|
| 729 | if self.mode == 'region':
|
|---|
| 730 | self.bRegion.config(relief='raised')
|
|---|
| 731 | self.mode = 'none'
|
|---|
| 732 | self.spec_show()
|
|---|
| 733 | return
|
|---|
| 734 | self.bNote.config(relief='raised')
|
|---|
| 735 | self.bRegion.config(relief='sunken')
|
|---|
| 736 | self.bPanel.config(relief='raised')
|
|---|
| 737 | self.mode = 'region'
|
|---|
| 738 | self.notewin.close_widgets()
|
|---|
| 739 | self.__disconnect_event()
|
|---|
| 740 | self._p.register('button_press',self._add_region)
|
|---|
| 741 |
|
|---|
| 742 | def select_panel(self):
|
|---|
| 743 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
|---|
| 744 | self.figmgr.toolbar.set_message('select panels')
|
|---|
| 745 | if self.mode == 'panel':
|
|---|
| 746 | self.bPanel.config(relief='raised')
|
|---|
| 747 | self.mode = 'none'
|
|---|
| 748 | self.spec_show()
|
|---|
| 749 | return
|
|---|
| 750 | self.bNote.config(relief='raised')
|
|---|
| 751 | self.bRegion.config(relief='raised')
|
|---|
| 752 | self.bPanel.config(relief='sunken')
|
|---|
| 753 | self.mode = 'panel'
|
|---|
| 754 | self.notewin.close_widgets()
|
|---|
| 755 | self.__disconnect_event()
|
|---|
| 756 | self._p.register('button_press',self._add_panel)
|
|---|
| 757 |
|
|---|
| 758 | def quit(self):
|
|---|
| 759 | self.__disconnect_event()
|
|---|
| 760 | #self.delete_bar()
|
|---|
| 761 | self.disable_button()
|
|---|
| 762 | self.figmgr.window.wm_withdraw()
|
|---|
| 763 |
|
|---|
| 764 | def enable_button(self):
|
|---|
| 765 | if self.button: return
|
|---|
| 766 | self.bRegion.config(state=Tk.NORMAL)
|
|---|
| 767 | self.bPanel.config(state=Tk.NORMAL)
|
|---|
| 768 | self.bClear.config(state=Tk.NORMAL)
|
|---|
| 769 | self.bFlag.config(state=Tk.NORMAL)
|
|---|
| 770 | self.bUnflag.config(state=Tk.NORMAL)
|
|---|
| 771 | self.bStat.config(state=Tk.NORMAL)
|
|---|
| 772 | self.button = True
|
|---|
| 773 | self.spec_show()
|
|---|
| 774 |
|
|---|
| 775 | def disable_button(self):
|
|---|
| 776 | ## disable buttons which don't work for plottp
|
|---|
| 777 | if not self.button: return
|
|---|
| 778 | self.bRegion.config(relief='raised')
|
|---|
| 779 | self.bPanel.config(relief='raised')
|
|---|
| 780 | self.bRegion.config(state=Tk.DISABLED)
|
|---|
| 781 | self.bPanel.config(state=Tk.DISABLED)
|
|---|
| 782 | self.bClear.config(state=Tk.DISABLED)
|
|---|
| 783 | self.bFlag.config(state=Tk.DISABLED)
|
|---|
| 784 | self.bUnflag.config(state=Tk.DISABLED)
|
|---|
| 785 | self.bStat.config(state=Tk.DISABLED)
|
|---|
| 786 | self.bNext.config(state=Tk.DISABLED)
|
|---|
| 787 | self.bPrev.config(state=Tk.DISABLED)
|
|---|
| 788 | self.button = False
|
|---|
| 789 | self.mode = ''
|
|---|
| 790 | self.notewin.close_widgets()
|
|---|
| 791 | self.__disconnect_event()
|
|---|
| 792 |
|
|---|
| 793 | def enable_next(self):
|
|---|
| 794 | self.bNext.config(state=Tk.NORMAL)
|
|---|
| 795 |
|
|---|
| 796 | def disable_next(self):
|
|---|
| 797 | self.bNext.config(state=Tk.DISABLED)
|
|---|
| 798 |
|
|---|
| 799 | def enable_prev(self):
|
|---|
| 800 | self.bPrev.config(state=Tk.NORMAL)
|
|---|
| 801 |
|
|---|
| 802 | def disable_prev(self):
|
|---|
| 803 | self.bPrev.config(state=Tk.DISABLED)
|
|---|
| 804 |
|
|---|
| 805 | # pause buttons for slow operations
|
|---|
| 806 | def _pause_buttons(self,operation="end",msg=""):
|
|---|
| 807 | buttons = ["bRegion","bPanel","bClear","bFlag","bUnflag","bStat",
|
|---|
| 808 | "bNote","bQuit"]
|
|---|
| 809 | if operation == "start":
|
|---|
| 810 | state=Tk.DISABLED
|
|---|
| 811 | else:
|
|---|
| 812 | state=Tk.NORMAL
|
|---|
| 813 | for btn in buttons:
|
|---|
| 814 | getattr(self,btn).config(state=state)
|
|---|
| 815 | self.figmgr.toolbar.set_message(msg)
|
|---|
| 816 |
|
|---|
| 817 | def delete_bar(self):
|
|---|
| 818 | self.__disconnect_event()
|
|---|
| 819 | self.destroy()
|
|---|
| 820 |
|
|---|
| 821 | def __disconnect_event(self):
|
|---|
| 822 | self._p.register('button_press',None)
|
|---|
| 823 | self._p.register('button_release',None)
|
|---|
| 824 |
|
|---|
| 825 | def _draw_span(self,axes,x0,x1,**kwargs):
|
|---|
| 826 | height = self._p.figure.bbox.height
|
|---|
| 827 | y0 = height - axes.bbox.y0
|
|---|
| 828 | y1 = height - axes.bbox.y1
|
|---|
| 829 | return self._p.canvas._tkcanvas.create_rectangle(x0,y0,x1,y1,**kwargs)
|
|---|
| 830 |
|
|---|
| 831 | def _remove_span(self,span):
|
|---|
| 832 | self._p.canvas._tkcanvas.delete(span)
|
|---|