-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRFIPainter_tools.py
executable file
·327 lines (300 loc) · 13.1 KB
/
RFIPainter_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import matplotlib
matplotlib.use('TKAgg')
import pylab as plt
from scipy import signal
import aipy as a
import numpy as np
from glob import glob
import random
import pyuvdata
from matplotlib.patches import Rectangle
import h5py
Plot=False
pull_samples=False
def loadH5(file_locs):
f = h5py.File(file_locs)
try:
return f['data'][:],np.logical_not(f['flag'])
except:
return f['uv']['data'][:],np.logical_not(f['uv']['flag'])
def loadPYUVdata(file_locs,suffix):
files = np.array(glob(file_locs+'*'+suffix))
info = {}
ct = 0
rnd_ = np.random.randint(len(files),size=5)
file_cut = files[rnd_]
for f in file_cut:
print(f)
uv = pyuvdata.UVData()
uv.read_miriad(f,run_check_acceptability=False,run_check=False,check_extra=False)
antpairs = uv.get_antpairs()
flag_npz_name = '.'.join(f.split('.')[:5])+'.uvOC.flags.npz'
try:
flag_npz = np.load(flag_npz_name)
except:
#continue
print('Skipping because npz flag file -{}- not found'.format(flag_npz_name))
for i in range(10):
rnd = np.random.randint(len(antpairs))
ap1,ap2 = antpairs[rnd]
bsl = uv.antnums_to_baseline(ap1,ap2)
try:
HERAlabels_ = np.logical_not(flag_npz['flag_array'][bsl == flag_npz['baseline_array']])
except:
HERAlabels_ = 0.
#pass
pos1 = uv.antenna_positions[uv.antenna_numbers==ap1]
pos2 = uv.antenna_positions[uv.antenna_numbers==ap2]
bl_len = np.round(np.sqrt(np.sum((pos1-pos2)**2)),2)
info[f+'_{0}'.format(ct)] = '{0}_blen_{1}'.format(ct,bl_len)#str(ct)+'_blen_'+str() #['antpairs'][ct] = antpairs[rnd] #antpairs[rnd] : ct}
if f == file_cut[0] and i == 0:
HERAdata = [uv.get_data(antpairs[rnd]).squeeze()]
try:
HERAlabels = [HERAlabels_.squeeze()]
except:
HERAlabels = []
else:
HERAdata.append(uv.get_data(antpairs[rnd]).squeeze())
try:
HERAlabels.append(HERAlabels_.squeeze())
except:
HERAlabels = []
ct+=1
del(uv)
print('Dataset size: ',np.shape(HERAdata))
if np.ndim(HERAlabels) > 1:
HERAlabels = np.zeros_like(HERAdata).real
return HERAdata,HERAlabels,info
plt.ion()
options={'y':-1,
'n':100}
class Annotate(object):
def __init__(self):
self.ax = plt.gca()
# self.ax =
self.rect = Rectangle((0,0), 1, 1)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.ax.figure.canvas.mpl_connect('key_press_event', self.quit)
def on_press(self, event):
#print 'press'
self.x0 = event.xdata
self.y0 = event.ydata
def on_release(self, event):
#print 'release'
self.x1 = event.xdata
self.y1 = event.ydata
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
self.ax.figure.canvas.draw()
class Painter:
def __init__(self,data,info,mask=None,delay_only=False):
self.ct = 0
if delay_only:
self.fig2 = plt.figure()
self.canvas2 = self.fig2.canvas
else:
self.fig = plt.figure()
self.fig2 = plt.figure()
self.fig3 = plt.figure()
self.canvas = self.fig.canvas
self.canvas2 = self.fig2.canvas
self.canvas3 = self.fig3.canvas
self.data = np.copy(data)
self.keep = True
self.delay_only = delay_only
if np.any(mask!=None):
self.mask = mask
else:
self.mask = np.ones_like(self.data).astype(int)
self.info = info
self.curr_mask = self.mask[0]
self.undomask = np.copy(self.curr_mask)
self.curr_data = self.data[0]
if delay_only:
self.ax2 = self.fig2.gca()
else:
self.ax = self.fig.gca()
self.ax2 = self.fig2.gca()
self.ax3 = self.fig3.gca()
if delay_only:
self.cid7_2 = self.canvas2.mpl_connect('key_press_event', self.pop)
else:
self.cid1 = self.canvas.mpl_connect('button_press_event', self.on_press)
self.cid2 = self.canvas.mpl_connect('button_release_event', self.on_release)
self.cid3 = self.canvas.mpl_connect('button_press_event', self.undo)
self.cid4 = self.canvas.mpl_connect('key_press_event', self.on_key_press)
self.cid5 = self.canvas.mpl_connect('key_release_event', self.on_key_release)
self.cid6 = self.canvas.mpl_connect('key_press_event', self.next)
self.cid7 = self.canvas.mpl_connect('key_press_event', self.pop)
self.cid1_ = self.canvas3.mpl_connect('button_press_event', self.on_press)
self.cid2_ = self.canvas3.mpl_connect('button_release_event', self.on_release)
self.cid3_ = self.canvas3.mpl_connect('button_press_event', self.undo)
self.cid4_ = self.canvas3.mpl_connect('key_press_event', self.on_key_press)
self.cid5_ = self.canvas3.mpl_connect('key_release_event', self.on_key_release)
self.cid6_ = self.canvas3.mpl_connect('key_press_event', self.next)
self.cid7_ = self.canvas3.mpl_connect('key_press_event', self.pop)
logdata = np.log10(np.abs(self.curr_data))*np.abs(self.curr_mask)
maxD = np.max(np.abs(logdata))
minD = np.min(np.abs(logdata))
if self.delay_only:
self.delay_plot = self.ax2.imshow(np.log10(np.abs(self.delayTrans())),aspect='auto')
self.ax2.set_title('Delay Spectrum')
self.ax2.set_xlabel('Delay Bin')
self.ax2.set_ylabel('Time')
else:
curr_mask = np.copy(self.curr_mask)
curr_mask =np.where(curr_mask == 0, np.nan, curr_mask)
self.amp_plot = self.ax.imshow(np.log10(np.abs(self.curr_data))*np.abs(curr_mask),aspect='auto')
self.ax.set_title('Visibility')
self.ax.set_xlabel('Freq.')
self.ax.set_ylabel('Time')
self.delay_plot = self.ax2.imshow(np.log10(np.abs(self.delayTrans())),aspect='auto')
self.ax2.set_title('Delay Spectrum')
self.ax2.set_xlabel('Delay Bin')
self.ax2.set_ylabel('Time')
self.phs_plot = self.ax3.imshow(np.angle(self.curr_data*curr_mask),aspect='auto')
self.ax3.set_title('Phase')
self.ax3.set_xlabel('Freq')
self.ax3.set_ylabel('Time')
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
def on_key_press(self, event):
if event.key == 'shift':
self.shift_is_held = True
def test(self, event):
if event.key == 'right':
print('Right Arrow')
if event.key == 'n':
print('n')
def pop(self, event):
if event.key == 'p':
if self.delay_only:
self.keep = False
else:
if self.info.has_key(self.ct):
bad_key = [key_ for key_ in self.info.keys() if int(info[key_].split('_')[0]) == self.ct]
self.info[bad_key[0]] = '{0}_blen_{1}'.format(-1,000)
else:
self.info['000_'+str(self.ct)] = '{0}_blen_{1}'.format(-1,000)
self.ct += 1
self.curr_data = self.data[self.ct]
self.curr_mask = self.mask[self.ct]
if self.delay_only:
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
else:
curr_mask = np.copy(self.curr_mask)
curr_mask = np.where(curr_mask == 0, np.nan, curr_mask)
self.amp_plot.set_data(np.log10(np.abs(self.curr_data))*np.abs(curr_mask))
self.canvas.draw()
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
self.phs_plot.set_data(np.angle(self.curr_data*curr_mask))
self.canvas3.draw()
def next(self, event):
if event.key == 'right':
self.data[self.ct] = np.copy(self.curr_data)
self.mask[self.ct] = np.copy(self.curr_mask)
if not self.info.has_key(self.ct):
self.info['000_'+str(self.ct)] = '{0}_blen_{1}'.format(self.ct,000)
self.ct += 1
print(self.ct)
self.curr_data = self.data[self.ct]
self.curr_mask = self.mask[self.ct]
if self.delay_only:
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
else:
curr_mask = np.copy(self.curr_mask)
curr_mask =np.where(curr_mask == 0, np.nan, curr_mask)
self.amp_plot.set_data(np.log10(np.abs(self.curr_data))*np.abs(curr_mask))
self.canvas.draw()
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
self.phs_plot.set_data(np.angle(self.curr_data*curr_mask))
self.canvas3.draw()
def on_key_release(self, event):
if event.key == 'shift':
self.shift_is_held = False
def onclick(self,event):
print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata))
self.undomask = self.mask
self.curr_mask[int(event.ydata)+1,int(event.xdata)+1] = 0
def undo(self, event):
if event.button == 3:
self.curr_mask = self.undomask
logdata = np.log10(np.abs(self.curr_data))*np.abs(self.curr_mask)
minD = np.min(logdata)
maxD = np.max(logdata)
if self.delay_only:
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
else:
curr_mask = np.copy(self.curr_mask)
curr_mask =np.where(curr_mask == 0, np.nan, curr_mask)
self.amp_plot.set_data(np.log10(np.abs(self.curr_data))*np.abs(curr_mask))
self.canvas.draw()
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
self.phs_plot.set_data(np.angle(self.curr_data*curr_mask))
self.canvas3.draw()
def on_press(self, event):
if event.button == 1:
self.undomask = np.copy(self.curr_mask)
self.x0 = event.xdata
self.y0 = event.ydata
def quit(self, event):
if event.button == 'q':
self.canvas.mpl_disconnect(self.cid1)
self.canvas.mpl_disconnect(self.cid2)
self.canvas.mpl_disconnect(self.cid3)
self.canvas.mpl_disconnect(self.cid4)
self.canvas.mpl_disconnect(self.cid5)
self.ax.close()
self.ax2.close()
def on_release(self, event):
if plt.get_current_fig_manager().toolbar.mode != '': return
if event.button == 1:
self.x1 = event.xdata
self.y1 = event.ydata
if self.y0 >= self.y1 and self.x0 >= self.x1:
self.curr_mask[int(self.y1):int(self.y0+1),int(self.x1):int(self.x0+1)] = 0
elif self.y0 >= self.y1 and self.x1 >= self.x0:
self.curr_mask[int(self.y1):int(self.y0+1),int(self.x0):int(self.x1+1)] = 0
elif self.x0 >= self.x1 and self.y1 >= self.y0:
self.curr_mask[int(self.y0):int(self.y1+1),int(self.x0):int(self.x1+1)] = 0
else:
self.curr_mask[int(self.y0):int(self.y1+1),int(self.x0):int(self.x1+1)] = 0
logdata = np.log10(np.abs(self.curr_data))*np.abs(self.curr_mask)
minD = np.min(logdata)
maxD = np.max(logdata)
if self.delay_only:
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
else:
curr_mask = np.copy(self.curr_mask)
curr_mask =np.where(curr_mask == 0, np.nan, curr_mask)
self.amp_plot.set_data(np.log10(np.abs(self.curr_data))*np.abs(curr_mask))
self.canvas.draw()
self.delay_plot.set_data(np.log10(np.abs(self.delayTrans())))
self.canvas2.draw()
self.phs_plot.set_data(np.angle(self.curr_data*curr_mask))
self.canvas3.draw()
def get_data(self):
return self.data,self.mask,self.info
def return_keep(self):
return self.keep
def delayTrans(self):
bh = a.dsp.gen_window(1024,window='blackman-harris')
DATA = np.fft.fft(self.curr_data*np.abs(self.curr_mask)*bh,axis=1)
DATA_ = np.fft.fftshift(DATA,axes=1)
return DATA_