forked from BezuidenhoutMC/SeeKAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeeKAT.py
216 lines (169 loc) · 8.19 KB
/
SeeKAT.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
#!/usr/bin/env python
# Tiaan Bezuidenhout, 2020. For inquiries: [email protected]
# NB: REQUIRES Python 3
import argparse
import numpy as np
import matplotlib.pyplot as plt
from sys import stdout
import SK.utils as ut
import SK.coordinates as co
import SK.plotting as Splot
np.seterr(divide='ignore', invalid='ignore')
def parseOptions(parser):
'''Options:
-f Input file with each line a different CB detection.
Should have 3 columns: RA (h:m:s), Dec (d:m:s), S/N
-p PSF of a CB in fits format
--o Fractional sensitivity level at which CBs are tiled to overlap
--r Resolution of PSF in units of arcseconds per pixel
--n Number of beams to consider when creating overlap contours. Will
pick the specified number of beams with the highest S/N values.
--nsig Sets the number of standard deviation contours drawn.
--s Draws known coordinates onto the plot for comparison.
--scalebar Sets the length of the scale bar on the plot in arcseconds.
--ticks Sets the spacing of ticks on the localisation plot.
--clip Sets level below which CB PSF is set equal to zero.
--zoom Automatically zooms in on the TABs.
'''
parser.add_argument('-f', dest='file',
nargs = 1,
type = str,
help="Detections file",
required=True)
parser.add_argument('-c', dest='config',
nargs = 1,
type = str,
help="Configuration (json) file",
required=False)
parser.add_argument('-p',dest='psf',
nargs=1,
type=str,
help="PSF file",
required=True)
parser.add_argument('--o', dest='overlap',
type = float,
help = "Fractional sensitivity level at which the coherent beams overlap",
default = 0.25,
required = False)
parser.add_argument('--r', dest='res',
nargs = 1,
type=float,
help="Distance in arcseconds represented by one pixel of the PSF",
default = 1,
required = True)
parser.add_argument('--n',dest='npairs',
nargs = 1,
type = int,
help='Number of beams to use',
default = [1000000])
parser.add_argument('--nsig',dest='nsig',
nargs = 1,
type = int,
help='Draws uncertainty contours up to this number of standard deviations.',
default = [2])
parser.add_argument('--s', dest='source',
nargs = 1,
type=str,
help="Draws given coordinate location (format: hms,dms) on localisation plot",
required = False)
parser.add_argument('--scalebar', dest='sb',
nargs = 1,
type = float,
help = "Length of scale bar on the localisation plot in arcseconds. Set to 0 to omit altogether",
default = [10],
required = False)
parser.add_argument('--ticks', dest='tickspacing',
nargs = 1,
type = float,
help = "Sets the number of pixels between ticks on the localisation plot",
default = [100],
required = False)
parser.add_argument('--clip', dest='clipping',
nargs = 1,
type = float,
help = "Sets values of the PSF below this number to zero. Helps minimise the influence of low-level sidelobes",
default = [0.08],
required = False)
parser.add_argument('--zoom', dest='autozoom',
help = "Automatically zooms the localisation plot in on the TABs",
action = 'store_true')
parser.add_argument('--fits', dest='fitsOut',
help = "Outputs .fits file of localisation region",
action = 'store_true')
options= parser.parse_args()
return options
def place_beam(j, npairs, c, data, array_height, array_width, psf_ar, options, beam_ar):
stdout.write("\rAdding beam %d/%d..." % (j + 1, npairs + 1))
stdout.flush()
plt.scatter(c.ra.px, c.dec.px, color='black', s=0.2)
comparison_snr = data["SN"][j]
comparison_ar = np.zeros((array_height, array_width))
dec_start = int(np.round(c.dec.px[j])) - int(psf_ar.shape[1] / 2)
dec_end = int(np.round(c.dec.px[j])) + int(psf_ar.shape[1] / 2)
ra_start = int(np.round(c.ra.px[j])) - int(psf_ar.shape[0] / 2)
ra_end = int(np.round(c.ra.px[j])) + int(psf_ar.shape[0] / 2)
comparison_ar[dec_start : dec_end, ra_start : ra_end] = psf_ar
plt.contour(comparison_ar, levels=[options.overlap],
colors='black', linewidths=0.5)
plt.contour(beam_ar, levels=[options.overlap],
colors='black', linewidths=0.5)
return comparison_ar / beam_ar
def make_map(array_height, array_width, c, psf_ar, options, data):
if options.npairs[0] > 2 and options.npairs[0] + 1 <= len(c):
npairs = options.npairs[0] - 1
else:
npairs = len(c) - 1
loglikelihood = np.zeros((array_height, array_width))
nit = 1000 # number of iterations for covariance matrix
fake_snrs = data["SN"][None,:] + np.random.randn(nit * len(c)).reshape(nit, len(c))
# make covariance matrix
beam_snr = data["SN"][0]
beam_snrs_fake = fake_snrs[:,0]
sim_ratios = np.transpose([fake_snrs[:, j] / beam_snrs_fake for j in np.arange(1,npairs+1)])
obs_ratios = np.transpose([data["SN"][j] / beam_snr for j in np.arange(1,npairs+1)])
C = np.cov(sim_ratios, rowvar=False)
# make model and get residuals
stdout.write("\rAdding beam %d/%d..." % (1, npairs+1))
stdout.flush()
beam_ar = np.zeros((array_height, array_width))
beam_snr = data["SN"][0] # NB, beams must be sorted by S/N; highest first!
dec_start = int(np.round(c.dec.px[0])) - int(psf_ar.shape[1] / 2)
dec_end = int(np.round(c.dec.px[0])) + int(psf_ar.shape[1] / 2)
ra_start = int(np.round(c.ra.px[0])) - int(psf_ar.shape[0] / 2)
ra_end = int(np.round(c.ra.px[0])) + int(psf_ar.shape[0] / 2)
beam_ar[dec_start : dec_end, ra_start : ra_end] = psf_ar
plt.contour(beam_ar, levels=[options.overlap],
colors='black', linewidths=0.5, linestyles='dashed') # shows beam sizes
psf_ratios = np.transpose([place_beam(j, npairs, c, data, array_height,
array_width, psf_ar, options, beam_ar)
for j in np.arange(1, npairs+1)], axes=(1,2,0))
resids = np.transpose([obs_ratios[i] - psf_ratios[:,:,i] for i in np.arange(0, npairs)],
axes=(1,2,0))
chi2 = np.sum(resids * np.sum(np.linalg.inv(C)[None,None,:,:] *
resids[:,:,:,None],axis=2),axis=2)
chi2[chi2 == np.inf] = np.nan
loglikelihood = -0.5 * chi2
return loglikelihood
if __name__ == "__main__":
parser = argparse.ArgumentParser()
options = parseOptions(parser)
data, c, boresight = ut.readCoords(options)
psf_ar = ut.readPSF(options.psf[0], options.clipping[0])
c, w, array_width, array_height = co.deg2pix(c, psf_ar,
boresight, options.res[0])
f, ax = plt.subplots(figsize=(10,10))
if options.source:
Splot.plot_known(w, options.source[0])
loglikelihood = make_map(array_height, array_width,
c, psf_ar, options, data)
Splot.make_ticks(ax, array_width, array_height,
w, fineness=options.tickspacing[0])
print("\nPlotting...")
Splot.likelihoodPlot(f, ax, w, loglikelihood, options)
if options.autozoom == True:
ut.autozoom(ax, c, options)
if options.fitsOut == True:
likelihood = ut.norm_likelihood(loglikelihood)
ut.write2fits(w, likelihood)
plt.savefig(options.file[0]+'.png',dpi=300)
plt.show()