-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlbi.py
291 lines (230 loc) · 11.1 KB
/
vlbi.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
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as io
import vlbi_utils as utils
from astropy.io import fits
from skimage.feature import register_translation
from scipy.ndimage import fourier_shift
from skimage.measure import compare_ssim
class CHIRP(object):
def __init__(self, oidata, fov, naxis, **kwargs):
"""
Inputs:
- oidata: OIFITS data format extraced by Paul Boley's OIFITS module
containing visibilities and bispectrum measurements.
- fov: Field of view in radians (float).
- naxis: Size of the source image (int).
Optional arguments:
- pulse: Pulse function used in the continuous image representation.
Default is the two-dimensional triangular pulse.
- pulse_ft: Closed-form Fourier transform of the pulse function used
in the continuous image representation. Default is the
Fourier transform of the two-dimensional triangular pulse.
- gmm: A dictionary containing the mixture components of a pre-trained
Gaussian Mixture Model. Default a GMM trained on natural images
taken from https://github.com/achael/eht-imaging/tree/960a79557b
4de7f2776bcfa1aef2c37cea487ab7.
- patch_size: Size of the patches (int) for the EPLL. Default is 8.
- betas: A sorted list in ascending order of weighting parameters (int)
for half quadratic splitting. Default is
[1, 4, 8, 16, 32, 64, 128, 256, 512].
- scales : A sorted list in ascending order of the number of pulse
functions (int) in the continuous image representation.
Default is [20, 23, 26, 29, 34, 38, 43, 49, 56, 64].
"""
self.data = utils.getData(oidata)
self.fov = fov
self.naxis = naxis
self.history = {}
self.res = None
self.__fig, self.__axs, self.__cbar = None, 2*[None], None
pulse = lambda x, y, delta: \
(np.maximum(1 - np.abs(x/delta), 0) / delta) *\
(np.maximum(1 - np.abs(y/delta), 0) / delta)
self.pulse = kwargs.pop('pulse', pulse)
pulse_ft = lambda x, y, delta: \
np.sinc(x * delta)**2 *\
np.sinc(y * delta)**2
self.pulse_ft = kwargs.pop('pulse_ft', pulse_ft)
pdata = io.loadmat('naturalPrior.mat')
gmm = {}
gmm['n_components'] = pdata['nmodels'][0][0]
gmm['weights'] = pdata['mixweights'].flatten()
gmm['covs'] = pdata['covs']
gmm['means'] = pdata['means']
self.gmm = kwargs.pop('gmm', gmm)
self.patch_size = kwargs.pop('patch_size', 8)
self.betas = kwargs.pop('betas',
[1, 4, 8, 16, 32, 64, 128, 256, 512])
self.scales = kwargs.pop('scales',
[20, 23, 26, 29, 34, 39, 44, 50, 56, 64])
if len(kwargs) > 0:
raise Exception('Unrecognized arguments.')
def reconstruct(self, lam = 0.0001, display=False):
"""
Reconstruct the image given the bispectrum measurements.
Inputs:
- lambda: Weighting parameter of the data term (float). Default
is 0.0001.
- display: Flag for displaying partial solutions. Default is False.
Outputs:
- x: A numpy array of shape (N,N) contaning the reconstructed image
coefficients.
"""
self.history = {}
x = utils.initImage(self.data, self.fov, self.scales[0], self.pulse_ft)
if display:
plt.ion()
plt.show()
self.__fig, self.__axs = plt.subplots(1, 2)
plt.subplots_adjust(wspace=-0.1)
self.__display(x, np.zeros_like(x), 'Initialization')
self.history['init'] = x
for scale in self.scales:
x = utils.upscaleImage(x, self.fov, scale, self.pulse)
gammas = (utils.ftVectors(self.data['bi_uvcoord1'], self.fov,
scale, self.pulse_ft),
utils.ftVectors(self.data['bi_uvcoord2'], self.fov,
scale, self.pulse_ft),
utils.ftVectors(self.data['bi_uvcoord3'], self.fov,
scale, self.pulse_ft))
for beta in self.betas:
# (a) solve for Z while keeping x constant
Z = utils.mostLikelyPatches(x, beta, self.data,
self.patch_size, self.gmm)
# (b) solve for x while keeping Z constant
x = utils.taylorExpansion(x, Z, beta, self.data, gammas,
self.patch_size, lam=lam)
if display:
self.__axs[0].clear()
self.__axs[1].clear()
self.__display(x, Z, 'Scale: ' + str(scale) + '\n' +\
r'$\beta$: ' + str(beta))
self.history[scale] = x
if display:
plt.ioff()
self.res = np.rot90(utils.upscaleImage(x, self.fov,
self.naxis, self.pulse),2)
return self.res
def score(self, ref_im):
"""
Align the reconstruction using phase correlation and calculate the MSE
and SSIM.
Inputs:
- ref_im: A numpy array of shape (N,N) contaning a reference image.
Outputs:
- mse: Mean squared error (float).
- ssim: Structural similarity index (float).
"""
if isinstance(self.res, type(None)):
raise Exception('Result is not yet aviable.')
shift = register_translation(ref_im, self.res)[0]
shifted_res = fourier_shift(np.fft.fft2(self.res), shift)
shifted_res = np.real(np.fft.ifft2(shifted_res))
mse = np.linalg.norm(shifted_res - ref_im)
drange = np.max(shifted_res) - np.min(shifted_res)
ssim = compare_ssim(ref_im, shifted_res, data_range=drange)
return mse, ssim
def saveFits(self, filename):
"""
Save the reconstruction in FITS.
Inputs:
- filename: Name for the file to be created (String).
"""
if isinstance(self.res, type(None)):
raise Exception('Result is not yet aviable.')
header = fits.Header()
header['NAXIS1'] = self.naxis
header['NAXIS2'] = self.naxis
header['CTYPE1'] = 'RA---SIN'
header['CTYPE2'] = 'DEC--SIN'
header['CDELT1'] = - self.fov/(np.pi/180 * self.naxis)
header['CDELT2'] = self.fov/(np.pi/180 * self.naxis)
header['BUNIT'] = 'JY/PIXEL'
hdu = fits.PrimaryHDU(self.res, header=header)
hdulist = fits.HDUList([hdu])
hdulist.writeto(filename, overwrite=True)
print("Saved as '%s'." %(filename))
def showResult(self, ref_im=None):
"""
Show the reconstructed image (in comparison to the source image).
Inputs:
- ref_im: A numpy array of shape (N,N) contaning a reference image.
Default is None.
"""
if isinstance(self.res, type(None)):
raise Exception('Result is not yet aviable.')
fov_mas = self.fov * 1e6 * 3600 * 180 / np.pi
ticks = np.linspace(0, self.naxis-1, 7)
ticklabels = np.linspace(fov_mas/2, -fov_mas/2, 7, dtype=int)
if not isinstance(ref_im, type(None)):
fig, axs = plt.subplots(1, 2)
plt.subplots_adjust(wspace=-0.1)
minVal = np.min([ref_im,self.res])
maxVal = np.max([ref_im,self.res])
im = axs[0].imshow(ref_im, cmap='gray', vmin=minVal, vmax=maxVal)
temp = fig.colorbar(im, ax=axs[0], shrink=0.575, label='Jy/pixel')
temp.remove()
axs[0].set_xticks(ticks)
axs[0].set_xticklabels(ticklabels)
axs[0].set_yticks(ticks)
axs[0].set_yticklabels(ticklabels)
axs[0].set_title('Reference')
axs[0].set_xlabel('Right Ascension [$\mu$as]')
axs[0].set_ylabel('Declination [$\mu$as]')
im = axs[1].imshow(self.res, cmap='gray', vmin=minVal, vmax=maxVal)
axs[1].set_title('Reconstruction')
axs[1].set_xticks(ticks)
axs[1].set_xticklabels(ticklabels)
axs[1].set_yticks([])
axs[1].set_xlabel('Right Ascension [$\mu$as]')
fig.colorbar(im, ax=axs[1], shrink=0.575, label='Jy/pixel')
plt.show()
else:
fig, ax = plt.subplots(1, 1)
im = plt.imshow(self.res, cmap='gray')
ax.set_xticks(ticks)
ax.set_xticklabels(ticklabels)
ax.set_yticks(ticks)
ax.set_yticklabels(ticklabels)
ax.set_title('Reconstruction')
ax.set_xlabel('Right Ascension [$\mu$as]')
ax.set_ylabel('Declination [$\mu$as]')
fig.colorbar(im, ax=ax, label='Jy/pixel')
plt.show()
def __display(self, x, Z, title):
"""
Auxiliary method for displaying partial solutions.
"""
minVal = np.min([x,Z])
maxVal = np.max([x,Z])
scale = x.shape[0]
fov_muas = self.fov * 1e6 * 3600 * 180 / np.pi
ticks = np.linspace(0, scale-1, 7)
ticklabels = np.linspace(fov_muas/2, -fov_muas/2, 7, dtype=int)
self.__fig.suptitle(title)
im = self.__axs[0].imshow(np.rot90(Z,2), cmap='gray',
vmin=minVal, vmax=maxVal)
temp = self.__fig.colorbar(im, ax=self.__axs[0], shrink=0.575,
label='Jy/pixel')
temp.remove()
self.__axs[0].set_xticks(ticks)
self.__axs[0].set_xticklabels(ticklabels)
self.__axs[0].set_yticks(ticks)
self.__axs[0].set_yticklabels(ticklabels)
self.__axs[0].set_title('Combined \n Patch Priors')
self.__axs[0].set_xlabel('Right Ascension [$\mu$as]')
self.__axs[0].set_ylabel('Declination [$\mu$as]')
im = self.__axs[1].imshow(np.rot90(x,2), cmap='gray',
vmin=minVal, vmax=maxVal)
self.__axs[1].set_title('Image Coefficients')
self.__axs[1].set_xticks(ticks)
self.__axs[1].set_xticklabels(ticklabels)
self.__axs[1].set_yticks([])
self.__axs[1].set_xlabel('Right Ascension [$\mu$as]')
if(self.__cbar):
self.__cbar.remove()
self.__cbar = self.__fig.colorbar(im, ax=self.__axs[1], shrink=0.575,
label='Jy/pixel')
self.__fig.canvas.draw()
plt.pause(0.001)