1 | from asap.asapplotter import asapplotter
|
---|
2 | from asap.logging import asaplog, asaplog_post_dec
|
---|
3 |
|
---|
4 | from asap.parameters import rcParams
|
---|
5 | from asap.selector import selector
|
---|
6 | from asap.scantable import scantable
|
---|
7 | import matplotlib.axes
|
---|
8 | from matplotlib.font_manager import FontProperties
|
---|
9 | from matplotlib.text import Text
|
---|
10 |
|
---|
11 | class flagplotter(asapplotter):
|
---|
12 | """
|
---|
13 | The flag plotter
|
---|
14 | Only row based panneling is allowed.
|
---|
15 |
|
---|
16 | Example:
|
---|
17 | scan = asa p.scantable(filename='your_filename',average=False)
|
---|
18 | guiflagger = asap.flagplotter(visible=True)
|
---|
19 | guiflagger.plot(scan)
|
---|
20 | ### flag/Unflag data graphically.
|
---|
21 | guiflagger.save_data(name='flagged_file.asap',format='ASAP')
|
---|
22 |
|
---|
23 | NOTICE:
|
---|
24 | The flagged data is not saved until you explicitly run scantable.save
|
---|
25 | """
|
---|
26 | def __init__(self, visible=None, **kwargs):
|
---|
27 | self._scan = None
|
---|
28 | asapplotter.__init__(self,visible=visible, **kwargs)
|
---|
29 | self._assert_plotter(action='reload')
|
---|
30 | self._plotter._set_window_title('Flag Plotter')
|
---|
31 | self._panelling = 'r'
|
---|
32 | self.set_stacking('scan')
|
---|
33 | self._ismodified = False
|
---|
34 | self._showflagged = False
|
---|
35 | self.set_colors("blue gray",False)
|
---|
36 |
|
---|
37 | def _new_custombar(self):
|
---|
38 | backend = matplotlib.get_backend()
|
---|
39 | # Flag plotter relys on supported GUI backends
|
---|
40 | if not self._visible:
|
---|
41 | asaplog.push("GUI backend is not available")
|
---|
42 | asaplog.post("ERROR")
|
---|
43 | elif backend == "TkAgg":
|
---|
44 | from asap.customgui_tkagg import CustomFlagToolbarTkAgg
|
---|
45 | return CustomFlagToolbarTkAgg(self)
|
---|
46 | elif backend == "Qt4Agg":
|
---|
47 | from asap.customgui_qt4agg import CustomFlagToolbarQT4Agg
|
---|
48 | return CustomFlagToolbarQT4Agg(self)
|
---|
49 | else:
|
---|
50 | asaplog.push("Unsupported backend for interactive flagging. Use either TkAgg or PyQt4Agg")
|
---|
51 | asaplog.post("ERROR")
|
---|
52 |
|
---|
53 | def set_showflagged(self, show):
|
---|
54 | """ Whether or not plotting flagged data"""
|
---|
55 | if type(show) == bool:
|
---|
56 | self._showflagged = show
|
---|
57 | else:
|
---|
58 | raise TypeError, "Input parameter should be a bool."
|
---|
59 |
|
---|
60 | @asaplog_post_dec
|
---|
61 | def _invalid_func(self, name):
|
---|
62 | msg = "Invalid function 'flagplotter."+name+"'"
|
---|
63 | #raise AttributeError(msg)
|
---|
64 | asaplog.push(msg)
|
---|
65 | asaplog.post('ERROR')
|
---|
66 |
|
---|
67 | def set_panelling(self,which='r'):
|
---|
68 | """ This function is not available for the class flagplotter """
|
---|
69 | if which.lower().startswith('r'):
|
---|
70 | return
|
---|
71 | msg = "Pannel setting is fixed to row mode in 'flagplotter'"
|
---|
72 | asaplog.push(msg)
|
---|
73 | asaplog.post('ERROR')
|
---|
74 | self._panelling = 'r'
|
---|
75 |
|
---|
76 | def set_range(self,xstart=None,xend=None,ystart=None,yend=None,refresh=False, offset=None):
|
---|
77 | """ This function is not available for the class flagplotter """
|
---|
78 | msg = "Plot range setting is not allowed in 'flagplotter'"
|
---|
79 | asaplog.push(msg)
|
---|
80 | asaplog.post('ERROR')
|
---|
81 | self._panelling = 'r'
|
---|
82 |
|
---|
83 | def plotazel(self,*args,**kwargs):
|
---|
84 | """ This function is not available for the class flagplotter """
|
---|
85 | self._invalid_func(name='plotazel')
|
---|
86 |
|
---|
87 | def plotpointing(self,*args,**kwargs):
|
---|
88 | """ This function is not available for the class flagplotter """
|
---|
89 | self._invalid_func(name='plotpointing')
|
---|
90 |
|
---|
91 | def plottp(self,*args,**kwargs):
|
---|
92 | """ This function is not available for the class flagplotter """
|
---|
93 | self._invalid_func(name='plottp')
|
---|
94 |
|
---|
95 | def save_data(self, name=None, format=None, overwrite=False):
|
---|
96 | """
|
---|
97 | Store the plotted scantable on disk.
|
---|
98 | This function simply redirects call to scantable.save()
|
---|
99 |
|
---|
100 | Parameters:
|
---|
101 |
|
---|
102 | name: the name of the outputfile. For format "ASCII"
|
---|
103 | this is the root file name (data in 'name'.txt
|
---|
104 | and header in 'name'_header.txt)
|
---|
105 |
|
---|
106 | format: an optional file format. Default is ASAP.
|
---|
107 | Allowed are:
|
---|
108 | * 'ASAP' (save as ASAP [aips++] Table),
|
---|
109 | * 'SDFITS' (save as SDFITS file)
|
---|
110 | * 'ASCII' (saves as ascii text file)
|
---|
111 | * 'MS2' (saves as an casacore MeasurementSet V2)
|
---|
112 | * 'FITS' (save as image FITS - not readable by class)
|
---|
113 | * 'CLASS' (save as FITS readable by CLASS)
|
---|
114 |
|
---|
115 | overwrite: If the file should be overwritten if it exists.
|
---|
116 | The default False is to return with warning
|
---|
117 | without writing the output. USE WITH CARE.
|
---|
118 | """
|
---|
119 | if not self._data:
|
---|
120 | raise RuntimeError("No scantable has been set yet.")
|
---|
121 | # simply calls scantable.save
|
---|
122 | self._data.save(name,format,overwrite)
|
---|
123 |
|
---|
124 | def set_data(self, scan, refresh=True):
|
---|
125 | if self._is_new_scan(scan):
|
---|
126 | self._ismodified = False
|
---|
127 | asapplotter.set_data(self, scan, refresh)
|
---|
128 | set_data.__doc__ = asapplotter.set_data.__doc__
|
---|
129 |
|
---|
130 | @asaplog_post_dec
|
---|
131 | def plot(self, scan=None):
|
---|
132 | if self._is_new_scan(scan):
|
---|
133 | self._ismodified = False
|
---|
134 | if not self._showflagged:
|
---|
135 | self.set_legend(mode=None,refresh=False)
|
---|
136 | elif not self._legendloc:
|
---|
137 | self.set_legend(mode=1,refresh=False)
|
---|
138 | asapplotter.plot(self,scan)
|
---|
139 | plot.__doc__ = asapplotter.plot.__doc__
|
---|
140 |
|
---|
141 | @asaplog_post_dec
|
---|
142 | def _plot(self, scan):
|
---|
143 | self._plot_with_flag(scan,self._showflagged)
|
---|
144 | #asapplotter._plot(self,scan)
|
---|
145 | # rescale x-range of subplots 5% margins
|
---|
146 | ganged = (self._plotter.axes._sharex != None)
|
---|
147 | if ganged:
|
---|
148 | np = 1
|
---|
149 | else:
|
---|
150 | np = len(self._plotter.subplots)
|
---|
151 | for ip in xrange(np):
|
---|
152 | ax = self._plotter.subplots[ip]['axes']
|
---|
153 | lim0 = ax.get_xlim()
|
---|
154 | offset = (lim0[1]-lim0[0])*0.05
|
---|
155 | ax.set_xlim(lim0[0]-offset,lim0[1]+offset)
|
---|
156 | del ax, lim0, offset
|
---|
157 | _plot.__doc__ = asapplotter._plot.__doc__
|
---|
158 |
|
---|
159 |
|
---|
160 | @asaplog_post_dec
|
---|
161 | def _plot_with_flag(self, scan, showflag=False):
|
---|
162 | # total number of panles to plot as a whole
|
---|
163 | nptot = scan.nrow()
|
---|
164 | # remaining panels to plot
|
---|
165 | n = nptot - self._ipanel - 1
|
---|
166 | ganged = False
|
---|
167 | maxpanel = 25
|
---|
168 |
|
---|
169 | if n > 1:
|
---|
170 | ganged = rcParams['plotter.ganged']
|
---|
171 | if self._rows and self._cols:
|
---|
172 | n = min(n,self._rows*self._cols)
|
---|
173 | self._plotter.set_panels(rows=self._rows,cols=self._cols,
|
---|
174 | nplots=n,margin=self._margins,ganged=ganged)
|
---|
175 | else:
|
---|
176 | n = min(n,maxpanel)
|
---|
177 | self._plotter.set_panels(rows=n,cols=0,nplots=n,margin=self._margins,ganged=ganged)
|
---|
178 | else:
|
---|
179 | self._plotter.set_panels(margin=self._margins)
|
---|
180 | #r = 0
|
---|
181 | r = self._startrow
|
---|
182 | # total row number of scantable
|
---|
183 | nr = scan.nrow()
|
---|
184 | panelcount = 0
|
---|
185 | allylim = []
|
---|
186 | allxlim = []
|
---|
187 |
|
---|
188 | while r < nr:
|
---|
189 | # always plot to new panel
|
---|
190 | self._plotter.subplot(panelcount)
|
---|
191 | self._plotter.palette(0)
|
---|
192 | # title and axes labels
|
---|
193 | xlab = self._abcissa and self._abcissa[panelcount] \
|
---|
194 | or scan._getabcissalabel()
|
---|
195 | if self._offset and not self._abcissa:
|
---|
196 | xlab += " (relative)"
|
---|
197 | ylab = self._ordinate and self._ordinate[panelcount] \
|
---|
198 | or scan._get_ordinate_label()
|
---|
199 | self._plotter.set_axes('xlabel', xlab)
|
---|
200 | self._plotter.set_axes('ylabel', ylab)
|
---|
201 | lbl = self._get_label(scan, r, mode='title', userlabel=self._title)
|
---|
202 | if type(lbl) in (list, tuple):
|
---|
203 | if 0 <= panelcount < len(lbl):
|
---|
204 | lbl = lbl[panelcount]
|
---|
205 | else:
|
---|
206 | # get default label
|
---|
207 | lbl = self._get_label(scan, r, 'title')
|
---|
208 | self._plotter.set_axes('title',lbl)
|
---|
209 | panelcount += 1
|
---|
210 | # Now get data to plot
|
---|
211 | y = scan._getspectrum(r)
|
---|
212 | # Check for FLAGROW column
|
---|
213 | mr = scan._getflagrow(r)
|
---|
214 | from numpy import ma, array
|
---|
215 | if mr:
|
---|
216 | ys = ma.masked_array(y,mask=mr)
|
---|
217 | if showflag:
|
---|
218 | yf = ma.masked_array(y, mask=(not mr))
|
---|
219 | else:
|
---|
220 | m = scan._getmask(r)
|
---|
221 | from numpy import logical_not, logical_and
|
---|
222 | if self._maskselection and len(self._usermask) == len(m):
|
---|
223 | if d[self._stacking](r) in self._maskselection[self._stacking]:
|
---|
224 | m = logical_and(m, self._usermask)
|
---|
225 | ys = ma.masked_array(y,mask=logical_not(array(m,copy=False)))
|
---|
226 | if showflag:
|
---|
227 | yf = ma.masked_array(y,mask=m)
|
---|
228 |
|
---|
229 | x = array(scan._getabcissa(r))
|
---|
230 | if self._offset:
|
---|
231 | x += self._offset
|
---|
232 | #llbl = self._get_label(scan, r, mode='legend', userlabel=self._lmap)
|
---|
233 | #if type(llbl) in (list, tuple):
|
---|
234 | # llbl = llbl[0]
|
---|
235 | #self._plotter.set_line(label=llbl)
|
---|
236 | self._plotter.set_line(label="data")
|
---|
237 | #plotit = self._plotter.plot
|
---|
238 | #if self._hist: plotit = self._plotter.hist
|
---|
239 | self._plotter.plot(x,ys)
|
---|
240 | if showflag:
|
---|
241 | self._plotter.set_line(label="flagged")
|
---|
242 | self._plotter.plot(x,yf)
|
---|
243 | ylim = self._minmaxy or [min(y),max(y)]
|
---|
244 | xlim= self._minmaxx or [min(x),max(x)]
|
---|
245 | elif mr or ys.mask.all():
|
---|
246 | ylim = self._minmaxy or []
|
---|
247 | xlim = self._minmaxx or []
|
---|
248 | else:
|
---|
249 | ylim = self._minmaxy or [ma.minimum(ys),ma.maximum(ys)]
|
---|
250 | xlim= self._minmaxx or [min(x),max(x)]
|
---|
251 | allylim += ylim
|
---|
252 | allxlim += xlim
|
---|
253 | if (panelcount == n) or (r == nr-1):
|
---|
254 | break
|
---|
255 | r+=1 # next row
|
---|
256 |
|
---|
257 | # Set x- and y- limts of subplots
|
---|
258 | if ganged:
|
---|
259 | xlim = None
|
---|
260 | ylim = None
|
---|
261 | if len(allylim) > 0:
|
---|
262 | allylim.sort()
|
---|
263 | ylim = allylim[0],allylim[-1]
|
---|
264 | if len(allxlim) > 0:
|
---|
265 | allxlim.sort()
|
---|
266 | xlim = allxlim[0],allxlim[-1]
|
---|
267 | self._plotter.set_limits(xlim=xlim,ylim=ylim)
|
---|
268 |
|
---|
269 | # save the current counter for multi-page plotting
|
---|
270 | self._startrow = r+1
|
---|
271 | self._ipanel += panelcount
|
---|
272 | if self.casabar_exists():
|
---|
273 | if self._ipanel >= nptot-1:
|
---|
274 | self._plotter.figmgr.casabar.disable_next()
|
---|
275 | else:
|
---|
276 | self._plotter.figmgr.casabar.enable_next()
|
---|
277 | if self._ipanel + 1 - panelcount > 0:
|
---|
278 | self._plotter.figmgr.casabar.enable_prev()
|
---|
279 | else:
|
---|
280 | self._plotter.figmgr.casabar.disable_prev()
|
---|
281 |
|
---|
282 |
|
---|
283 |
|
---|
284 | def _get_label(self, scan, row, mode='title', userlabel=None):
|
---|
285 | if isinstance(userlabel, list) and len(userlabel) == 0:
|
---|
286 | userlabel = " "
|
---|
287 | elif not mode.upper().startswith('T'):
|
---|
288 | pms = dict(zip(self._selection.get_pols(), \
|
---|
289 | self._selection.get_poltypes()))
|
---|
290 | if len(pms):
|
---|
291 | poleval = scan._getpollabel(scan.getpol(row), \
|
---|
292 | pms[scan.getpol(row)])
|
---|
293 | else:
|
---|
294 | poleval = scan._getpollabel(scan.getpol(row),scan.poltype())
|
---|
295 | label = "IF%d, POL %s, Scan%d" % \
|
---|
296 | (scan.getif(row),poleval,scan.getscan(row))
|
---|
297 | else:
|
---|
298 | label = "row %d" % (row)
|
---|
299 |
|
---|
300 | return userlabel or label
|
---|
301 |
|
---|
302 |
|
---|
303 | def _is_new_scan(self,scan):
|
---|
304 | if isinstance(scan, scantable):
|
---|
305 | if self._data is not None:
|
---|
306 | if scan != self._data:
|
---|
307 | return True
|
---|
308 | else:
|
---|
309 | return True
|
---|
310 | return False
|
---|