Changeset 2000 for trunk/python
- Timestamp:
- 02/18/11 12:50:32 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/flagtoolbar.py
r1999 r2000 118 118 119 119 120 ### Calculate statistics of the selected area. 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 121 145 @asaplog_post_dec 122 def _statistics(self): 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 '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') 177 #self.plotter._plotter.show(False) 178 self.plotter._plotter.canvas.draw() 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') 201 #self.plotter._plotter.show(False) 202 self.plotter._plotter.canvas.draw() 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 210 sout = "selected region: "+str(self._thisregion['worldx'])+\ 211 "(@row "+str(self._getrownum(self._thisregion['axes']))+")" 212 asaplog.push(sout) 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)) 238 #self.plotter._plotter.show(False) 239 self.plotter._plotter.canvas.draw() 240 asaplog.push("row "+str(irow)+" is selected") 241 #print "selected rows =",self._selpanels 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 292 #self.plotter._plotter.show(False) 293 self.plotter._plotter.canvas.draw() 294 del regions,panels,strow,enrow 295 296 def _clear_selection_plot(self): 297 ### clear up polygons which mark selected spectra and regions ### 298 if len(self._polygons) > 0: 299 for shadow in self._polygons: 300 shadow.remove() 301 self.plotter._plotter.canvas.draw() 302 self._polygons = [] 303 304 def _clearup_selections(self): 305 # clear-up selection and polygons 306 self._selpanels = [] 307 self._selregions = {} 308 self._clear_selection_plot() 309 310 ### clear up selections 311 def cancel_select(self): 312 self.figmgr.toolbar.set_message('selections canceled') 313 # clear-up selection and polygons 314 self._clearup_selections() 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 325 self._flag_operation(rows=self._selpanels, 326 regions=self._selregions,unflag=False) 327 sout = "Flagged:\n" 328 sout += " rows = "+str(self._selpanels)+"\n" 329 sout += " regions: "+str(self._selregions) 330 asaplog.push(sout) 331 del sout 332 self._clearup_selections() 333 self._plot_page(pagemode="current") 334 335 ### unflag selected spectra/regions 336 @asaplog_post_dec 337 def unflag(self): 338 if not self._any_selection(): 339 msg = "No selection to be Flagged" 340 asaplog.push(msg) 341 asaplog.post('WARN') 342 return 343 self._flag_operation(rows=self._selpanels, 344 regions=self._selregions,unflag=True) 345 sout = "Unflagged:\n" 346 sout += " rows = "+str(self._selpanels)+"\n" 347 sout += " regions: "+str(self._selregions) 348 asaplog.push(sout) 349 del sout 350 self._clearup_selections() 351 self._plot_page(pagemode="current") 352 353 ### actual flag operation 354 @asaplog_post_dec 355 def _flag_operation(self,rows=None,regions=None,unflag=False): 356 scan = self.plotter._data 357 if not scan: 358 asaplog.post() 359 asaplog.push("Invalid scantable") 360 asaplog.post("ERROR") 361 if isinstance(rows,list) and len(rows) > 0: 362 scan.flag_row(rows=rows,unflag=unflag) 363 if isinstance(regions,dict) and len(regions) > 0: 364 for srow, masklist in regions.iteritems(): 365 if not isinstance(masklist,list) or len(masklist) ==0: 366 msg = "Ignoring invalid region selection for row = "+srow 367 asaplog.post() 368 asaplog.push(msg) 369 asaplog.post("WARN") 370 continue 371 irow = int(srow) 372 mask = scan.create_mask(masklist,invert=False,row=irow) 373 scan.flag(row=irow,mask=mask,unflag=unflag) 374 del irow, mask 375 del srow, masklist 376 del scan 377 378 ### show statistics of selected spectra/regions 379 @asaplog_post_dec 380 def stat_cal(self): 123 381 if not self._any_selection(): 124 382 msg = "No selection to be calculated" … … 126 384 asaplog.post('WARN') 127 385 return 128 print "Calculate statistics of selected region and spectra"386 #print "Calculate statistics of selected region and spectra" 129 387 self._selected_stats(rows=self._selpanels,regions=self._selregions) 130 print "Statistics: "131 print "- rows: ",self._selpanels132 print "- regions:\n ",self._selregions388 #print "Statistics: " 389 #print "- rows: ",self._selpanels 390 #print "- regions:\n ",self._selregions 133 391 self._clearup_selections() 134 392 … … 202 460 del sout, ssep, maskstr, statvals, key, scan, row, stats, statstr, mask 203 461 204 ### Notation205 def _mod_note(self,event):206 # Do not fire event when in zooming/panning mode207 if not self.figmgr.toolbar.mode == '':208 return209 if event.button ==1:210 self.notewin.load_textwindow(event)211 elif event.button == 3 and self._note_picked(event):212 self.notewin.load_modmenu(event)213 return214 215 def _note_picked(self,event):216 # just briefly check if any texts are picked217 for textobj in self.canvas.figure.texts:218 if textobj.contains(event)[0]:219 return True220 for ax in self.canvas.figure.axes:221 for textobj in ax.texts:222 if textobj.contains(event)[0]:223 return True224 #print "No text picked"225 return False226 227 ### Region/Panel selection & oparations228 ### add regions to selections229 @asaplog_post_dec230 def _add_region(self,event):231 if not self.figmgr.toolbar.mode == '':232 return233 if event.button != 1 or event.inaxes == None:234 return235 # this row resolution assumes row panelling236 irow = int(self._getrownum(event.inaxes))237 if irow in self._selpanels:238 msg = "The whole spectrum is already selected"239 asaplog.post()240 asaplog.push(msg)241 asaplog.post('WARN')242 return243 print "add the region to selections"244 self._thisregion = {'axes': event.inaxes,'xs': event.xdata,245 'worldx': [event.xdata,event.xdata]}246 self.plotter._plotter.register('button_press',None)247 self.plotter._plotter.register('motion_notify', self._xspan_draw)248 self.plotter._plotter.register('button_press', self._xspan_end)249 250 def _xspan_draw(self,event):251 if event.inaxes == self._thisregion['axes']:252 xnow = event.xdata253 self.xdataold = xnow254 else:255 xnow = self.xdataold256 try: self.lastspan257 except AttributeError: pass258 else: self.lastspan.remove()259 260 self.lastspan = self._thisregion['axes'].axvspan(self._thisregion['xs'],xnow,facecolor='0.7')261 #self.plotter._plotter.show(False)262 self.plotter._plotter.canvas.draw()263 del xnow264 265 def _xspan_end(self,event):266 if not self.figmgr.toolbar.mode == '':267 return268 if event.button != 1:269 return270 271 try: self.lastspan272 except AttributeError: pass273 else:274 self.lastspan.remove()275 del self.lastspan276 if event.inaxes == self._thisregion['axes']:277 xdataend = event.xdata278 else:279 xdataend=self.xdataold280 281 self._thisregion['worldx'][1] = xdataend282 lregion = self._thisregion['worldx']283 pregion = self._thisregion['axes'].axvspan(lregion[0],lregion[1],284 facecolor='0.7')285 #self.plotter._plotter.show(False)286 self.plotter._plotter.canvas.draw()287 self._polygons.append(pregion)288 srow = self._getrownum(self._thisregion['axes'])289 irow = int(srow)290 if not self._selregions.has_key(srow):291 self._selregions[srow] = []292 self._selregions[srow].append(lregion)293 del lregion, pregion, xdataend294 print "selected region: ",self._thisregion['worldx'],\295 "(@row ",self._getrownum(self._thisregion['axes']),")"296 297 # release event298 self.plotter._plotter.register('button_press',None)299 self.plotter._plotter.register('motion_notify',None)300 # Clear up region selection301 self._thisregion = None302 self.xdataold = None303 # finally recover region selection event304 self.plotter._plotter.register('button_press',self._add_region)305 306 ### add panels to selections307 @asaplog_post_dec308 def _add_panel(self,event):309 if not self.figmgr.toolbar.mode == '':310 return311 if event.button != 1 or event.inaxes == None:312 return313 selax = event.inaxes314 # this row resolution assumes row panelling315 srow = self._getrownum(selax)316 irow = int(srow)317 if srow:318 self._selpanels.append(irow)319 shadow = Rectangle((0,0),1,1,facecolor='0.7',transform=selax.transAxes,visible=True)320 self._polygons.append(selax.add_patch(shadow))321 #self.plotter._plotter.show(False)322 self.plotter._plotter.canvas.draw()323 print "row", irow, "added to the panel selections"324 print "selected rows =",self._selpanels325 ## check for region selection of the spectra and overwrite it.326 ##!!!! currently disabled for consistency with flag tools !!!!327 #if self._selregions.has_key(srow):328 # self._selregions.pop(srow)329 # msg = "The whole spectrum is selected for row="+srow+". Region selection will be overwritten."330 # asaplog.push(msg)331 332 def _getrownum(self,axis):333 ### returns the row number of selected spectrum as a string ###334 plabel = axis.get_title()335 if plabel.startswith("row "):336 return plabel.strip("row ")337 return None338 339 def _any_selection(self):340 ### returns if users have selected any spectrum or region ###341 if len(self._selpanels) or len(self._selregions):342 return True343 return False344 345 def _plot_selections(self,regions=None,panels=None):346 ### mark panels/spectra selections in the page347 if not self._any_selection() and not (regions or panels):348 return349 regions = regions or self._selregions.copy() or {}350 panels = panels or self._selpanels or []351 if not isinstance(regions,dict):352 asaplog.post()353 asaplog.push("Invalid region specification")354 asaplog.post('ERROR')355 if not isinstance(panels,list):356 asaplog.post()357 asaplog.push("Invalid panel specification")358 asaplog.post('ERROR')359 strow = self._getrownum(self.plotter._plotter.subplots[0]['axes'])360 enrow = self._getrownum(self.plotter._plotter.subplots[-1]['axes'])361 for irow in range(int(strow),int(enrow)+1):362 if regions.has_key(str(irow)):363 ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']364 mlist = regions.pop(str(irow))365 for i in range(len(mlist)):366 self._polygons.append(ax.axvspan(mlist[i][0],mlist[i][1],367 facecolor='0.7'))368 del ax,mlist369 if irow in panels:370 ax = self.plotter._plotter.subplots[irow - int(strow)]['axes']371 shadow = Rectangle((0,0),1,1,facecolor='0.7',372 transform=ax.transAxes,visible=True)373 self._polygons.append(ax.add_patch(shadow))374 del ax,shadow375 #self.plotter._plotter.show(False)376 self.plotter._plotter.canvas.draw()377 del regions,panels,strow,enrow378 379 def _clear_selection_plot(self):380 ### clear up polygons which mark selected spectra and regions ###381 if len(self._polygons) > 0:382 for shadow in self._polygons:383 shadow.remove()384 self.plotter._plotter.canvas.draw()385 self._polygons = []386 387 def _clearup_selections(self):388 # clear-up selection and polygons389 self._selpanels = []390 self._selregions = {}391 self._clear_selection_plot()392 393 ### clear up selections394 def cancel_select(self):395 self.figmgr.toolbar.set_message('selections canceled')396 # clear-up selection and polygons397 self._clearup_selections()398 399 ### flag selected spectra/regions400 @asaplog_post_dec401 def flag(self):402 if not self._any_selection():403 msg = "No selection to be Flagged"404 asaplog.post()405 asaplog.push(msg)406 asaplog.post('WARN')407 return408 self._flag_operation(rows=self._selpanels,409 regions=self._selregions,unflag=False)410 print "Flagged"411 print "- rows: ",self._selpanels412 print "- regions:\n ",self._selregions413 self._clearup_selections()414 self._plot_page(pagemode="current")415 416 ### unflag selected spectra/regions417 @asaplog_post_dec418 def unflag(self):419 if not self._any_selection():420 msg = "No selection to be Flagged"421 asaplog.push(msg)422 asaplog.post('WARN')423 return424 self._flag_operation(rows=self._selpanels,425 regions=self._selregions,unflag=True)426 print "Unflagged:"427 print "- rows: ",self._selpanels428 print "- regions:\n ",self._selregions429 self._clearup_selections()430 self._plot_page(pagemode="current")431 432 ### actual flag operation433 @asaplog_post_dec434 def _flag_operation(self,rows=None,regions=None,unflag=False):435 scan = self.plotter._data436 if not scan:437 asaplog.post()438 asaplog.push("Invalid scantable")439 asaplog.post("ERROR")440 if isinstance(rows,list) and len(rows) > 0:441 scan.flag_row(rows=rows,unflag=unflag)442 if isinstance(regions,dict) and len(regions) > 0:443 for srow, masklist in regions.iteritems():444 if not isinstance(masklist,list) or len(masklist) ==0:445 msg = "Ignoring invalid region selection for row = "+srow446 asaplog.post()447 asaplog.push(msg)448 asaplog.post("WARN")449 continue450 irow = int(srow)451 mask = scan.create_mask(masklist,invert=False,row=irow)452 scan.flag(row=irow,mask=mask,unflag=unflag)453 del irow, mask454 del srow, masklist455 del scan456 457 ### show statistics of selected spectra/regions458 def stat_cal(self):459 print "Calculate statics of selected regions/panels"460 self._statistics()461 462 462 ### Page chages 463 463 ### go to the previous page … … 505 505 if not (pageop in availpage): 506 506 asaplog.post() 507 asaplo t.push("Invalid page operation")508 asaplo t.post("ERROR")507 asaplog.push("Invalid page operation") 508 asaplog.post("ERROR") 509 509 if pageop == "n": 510 510 # nothing necessary to plot the next page
Note:
See TracChangeset
for help on using the changeset viewer.