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