-
Notifications
You must be signed in to change notification settings - Fork 1
/
thquality.py
executable file
·187 lines (142 loc) · 5.24 KB
/
thquality.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
#! /usr/bin/env python
from __future__ import division, print_function
import argparse
import sys
from casacore.tables import table, taql
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from numba import njit, prange, float32, float64, complex64
import numpy as np
# Hide > comparison warnings with NaN values
np.warnings.filterwarnings('ignore')
@njit(parallel=True)
def differ(data, model):
diff = np.empty((3, data.shape[0], data.shape[1], data.shape[2]), dtype=data.dtype)
for i in prange(len(data)):
diff[0, i] = data[i] - model[i]
diff[1, i] = np.absolute(data[i]) - np.absolute(model[i])
diff[2, i] = np.angle(data[i]) - np.angle(model[i])
return diff
@njit([float64(complex64[:], float32[:])], parallel=True)
def complexchisquared(arr, weights):
count = 0
for i in prange(len(arr)):
count += (arr[i].real**2 + arr[i].imag**2) * weights[i]
return count / (2 * len(arr))
@njit([float64(float32[:], float32[:])], parallel=True)
def chisquared(arr, weights):
count = 0
for i in prange(len(arr)):
count += arr[i]**2 * weights[i]
return count / len(arr)
def sigma(arr):
return 1.4286 * np.nanmedian(np.abs(arr - np.nanmedian(arr)))
parser = argparse.ArgumentParser()
parser.add_argument('mset')
parser.add_argument('--antenna-sigma', type=float, default=3)
parser.add_argument('--baseline-sigma', type=float, default=3)
parser.add_argument('--data-column', default='CORRECTED_DATA')
parser.add_argument('--model-column', default='MODEL_DATA')
parser.add_argument('--minuv', type=float, default=60)
args = parser.parse_args()
# Extract data from measurement set
print("Reading data into memory...", end="", file=sys.stderr); sys.stderr.flush()
mset = table(args.mset, ack=False)
tbl = taql("select * from $mset where ANTENNA1 <> ANTENNA2 and not FLAG_ROW and sqrt(sumsqr(UVW[:2])) > %f" % args.minuv)
data = tbl.getcol(args.data_column)
model = tbl.getcol(args.model_column)
flags = tbl.getcol("FLAG")
weights = tbl.getcol("WEIGHT_SPECTRUM")
ant1 = tbl.getcol("ANTENNA1")
ant2 = tbl.getcol("ANTENNA2")
print(" Done", file=sys.stderr)
# Calculate difference between data and model
print("Diffing data...", end="", file=sys.stderr); sys.stderr.flush()
data[flags] = np.nan
fulldiff, ampdiff, phasediff = differ(data, model)
ampdiff, phasediff = ampdiff.real, phasediff.real
del data
del model
del flags
print(" Done", file=sys.stderr)
baselines = np.zeros((128, 128))
fulls = np.zeros(128)
amps = np.zeros(128)
phases = np.zeros(128)
baselines[:], fulls[:], amps[:], phases[:] = np.nan, np.nan, np.nan, np.nan
# Calculate baseline summaries
print("Calculating chi squared statistics...", end="", file=sys.stderr); sys.stderr.flush()
for a1 in range(128):
for a2 in range(a1 + 1, 128):
idx = np.any([
np.all([ant1 == a1, ant2 == a2], axis=0),
np.all([ant1 == a2, ant2 == a1], axis=0),
], axis=0)
d = fulldiff[idx][np.isfinite(fulldiff[idx])]
w = weights[idx][np.isfinite(fulldiff[idx])]
if len(d) == 0:
continue
baselines[a1, a2] = complexchisquared(d, w)
# Calculate antenna summaries
for a in range(128):
idx = np.any([ant1 == a, ant2 == a], axis=0)
d = fulldiff[idx][np.isfinite(fulldiff[idx])]
w = weights[idx][np.isfinite(fulldiff[idx])]
if len(d) == 0:
continue
fulls[a] = complexchisquared(d, w)
d = ampdiff[idx][np.isfinite(ampdiff[idx])]
w = weights[idx][np.isfinite(ampdiff[idx])]
amps[a] = chisquared(d, w)
d = phasediff[idx][np.isfinite(phasediff[idx])]
w = weights[idx][np.isfinite(phasediff[idx])]
phases[a] = chisquared(d, w)
print(" Done", file=sys.stderr)
# Baselines
threshold = np.nanmean(baselines) + args.baseline_sigma * sigma(baselines)
flagged = np.where(baselines > threshold)
plt.figure(figsize=(16, 8))
plt.subplot(1, 2, 1)
plt.title("Baseline Chi Squared")
plt.imshow(baselines)
plt.colorbar()
plt.subplot(1, 2, 2)
plt.title("Baseline Chi Squared (flagged baselines masked)")
baselines[flagged] = np.nan
plt.imshow(baselines)
plt.colorbar()
plt.savefig('baselines.png')
with open('badbaselines.txt', 'w') as f:
for a1, a2 in np.transpose(flagged):
print(a1, a2, file=f)
plt.figure(figsize=(12, 12))
# Full
threshold = np.nanmean(fulls) + args.antenna_sigma * sigma(fulls)
fullflagged = np.where(fulls > threshold)
ax = plt.subplot(3, 1, 1)
plt.title("Full ChiSq")
plt.scatter(range(0, 128), fulls)
plt.scatter(fullflagged, fulls[fullflagged])
ax.axhline(threshold, linestyle='--')
# Amps
threshold = np.nanmean(amps) + args.antenna_sigma * sigma(amps)
ampflagged = np.where(amps > threshold)
ax = plt.subplot(3, 1, 2)
plt.title("Amp ChiSq")
plt.scatter(range(0, 128), amps)
plt.scatter(ampflagged, amps[ampflagged])
ax.axhline(threshold, linestyle='--')
# Phases
threshold = np.nanmean(phases) + args.antenna_sigma * sigma(phases)
phaseflagged = np.where(phases > threshold)
ax = plt.subplot(3, 1, 3)
plt.title("Phase ChiSq")
plt.scatter(range(0, 128), phases)
plt.scatter(phaseflagged, phases[phaseflagged])
ax.axhline(threshold, linestyle='--')
flagged = set(list(fullflagged[0]) + list(ampflagged[0]) + list(phaseflagged[0]))
with open('badtiles.txt', 'w') as f:
for tile in flagged:
print(tile, file=f)
plt.savefig('tiles.png')