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