def test_vis_sample(): msname = os.path.join(os.path.dirname(__file__), 'testfile.ms') #imagename = os.path.join(os.path.dirname(__file__), 'gaussiantestmodel.fits') imagename = os.path.join(os.path.dirname(__file__), 'model.fits') data_vis = import_data_ms(msname) interp_vis = vis_sample(imagefile = imagename, uvfile = msname, mu_RA = 0, mu_DEC = 0) print(interp_vis[10:20]) print(uvgaussian(data_vis.uu[10:20], data_vis.vv[10:20]))
def gen_mdl(pars, name): # calculate a model image foo = img_parser(inc=pars[0], PA=pars[1], dist=140., r0=10., r_l=pars[2], z0=pars[3], zpsi=pars[4], zphi=np.inf, Tb0=pars[5], Tbq=pars[6], Tbeps=np.inf, Tbmax=500., Tbmax_b=500., FOV=2.56, Npix=512) # load the template visibilities dat = np.load('data/template.vis.npz') u, v, vis, wgt = dat['u'], dat['v'], dat['Vis'], dat['Wgt'] # FT and sample the model image onto the (u,v) spacings mvis = vis_sample(imagefile=foo, uu=u, vv=v, mu_RA=pars[7], mu_DEC=pars[8], mod_interp=False) print(mvis.shape) # Save the model and residual visibilities os.system('rm -rf data/' + name + '.vis.npz') np.savez('data/' + name + '.vis.npz', u=u, v=v, Vis=np.squeeze(mvis), Wgt=wgt)
def lnprob(theta): # calculate prior ptheta = np.empty_like(theta) ptheta[0] = lnpU(theta[0], 0., 90.) ptheta[1] = lnpU(theta[1], 0., 360.) ptheta[2] = lnpU(theta[2], 0., 5.) ptheta[3] = lnpU(theta[3], r0, 0.5 * (dist * FOV)) ptheta[4] = lnpU(theta[4], 0., 10.) ptheta[5] = lnpU(theta[5], 0., 1.5) ptheta[6] = lnpU(theta[6], 5., Tbmax) ptheta[7] = lnpU(theta[7], 0., 2.) ptheta[8] = lnpU(theta[8], 5., 50.) ptheta[9] = lnpU(theta[9], 0., 1000.) ptheta[10] = lnpU(theta[10], 3.5, 4.5) ptheta[11] = lnpU(theta[11], -0.2, 0.2) ptheta[12] = lnpU(theta[12], -0.2, 0.2) lnprior = np.sum(ptheta) # generate a model cube mcube = cube_parser(inc=theta[0], PA=theta[1], dist=dist, r0=r0, mstar=theta[2], r_l=theta[3], z0=theta[4], zpsi=theta[5], Tb0=theta[6], Tbq=theta[7], Tbmax=Tbmax, Tbmax_b=theta[8], dV0=theta[9], dVq=0.5 * theta[7], FOV=FOV, Npix=Npix, Vsys=theta[10], restfreq=nu_l, vel=v_LSRK_mid) # sample the FT of the cube onto the observed (u,v) points mvis = vis_sample(imagefile=mcube, mu_RA=theta[11], mu_DEC=theta[12], gcf_holder=gcf, corr_cache=corr, mod_interp=False) # window the visibilities SRF_kernel = np.array([0.0, 0.25, 0.5, 0.25, 0.0]) mvis_re = convolve1d(mvis.real, SRF_kernel, axis=1, mode='nearest') mvis_im = convolve1d(mvis.imag, SRF_kernel, axis=1, mode='nearest') mvis = mvis_re + 1.0j * mvis_im # interpolation fint = interp1d(freq_LSRK_mid, mvis, axis=1, fill_value='extrapolate') mvis = fint(data.freqs) # excise the padded boundary channels to avoid edge effects mvis = mvis[:, chpad:-chpad].T mwgt = data.wgts[chpad:-chpad, :] # clip for binning mvis = mvis[:mvis.shape[0] - (mvis.shape[0] % chbin), :] mwgt = mwgt[:mvis.shape[0] - (mvis.shape[0] % chbin), :] # bin (weighted, decimated average) mvis_bin = np.average(mvis.reshape((-1, chbin, mvis.shape[1])), weights=mwgt.reshape((-1, chbin, mwgt.shape[1])), axis=1) # compute the log-likelihood resid = np.absolute(data_bin.VV - mvis_bin) lnL = -0.5 * np.tensordot(resid, np.dot(Mbin_inv, data_bin.wgts * resid)) # return the posterior return lnL + L0 + lnprior, lnprior
zpsi=theta[5], Tb0=theta[6], Tbq=theta[7], Tbmax=Tbmax, Tbmax_b=theta[8], dV0=theta[9], dVq=0.5 * theta[7], FOV=FOV, Npix=Npix, Vsys=theta[10], restfreq=nu_l, vel=v_LSRK_mid) tvis, gcf, corr = vis_sample(imagefile=foo, uu=data.uu, vv=data.vv, return_gcf=True, return_corr_cache=True, mod_interp=False) ### PRIOR FUNCTIONAL FORMS ### ---------------------- # uniform def lnpU(theta, lo, hi): if ((theta >= lo) and (theta <= hi)): return 0 else: return -np.inf # normal
Tb0=theta[6], Tbq=theta[7], Tbmax=Tbmax, Tbmax_b=theta[8], dV0=theta[9], dVq=theta[10], FOV=FOV, Npix=Npix, Vsys=theta[11], restfreq=restfreq, vel=1e3 * v_LSRK[i, :]) # sample its Fourier Transform on the template (u,v) spacings mvis = vis_sample(imagefile=foo, uu=tvis.uu, vv=tvis.vv, mu_RA=theta[12], mu_DEC=theta[13], mod_interp=False) # populate the appropriate parts of cloned array with these visibilities ix_lo, ix_hi = i * nperstamp, (i + 1) * nperstamp clean_arr[ix_lo:ix_hi, :, 0, 0] = mvis.real[ix_lo:ix_hi, :] clean_arr[ix_lo:ix_hi, :, 1, 0] = mvis.real[ix_lo:ix_hi, :] clean_arr[ix_lo:ix_hi, :, 0, 1] = mvis.imag[ix_lo:ix_hi, :] clean_arr[ix_lo:ix_hi, :, 1, 1] = mvis.imag[ix_lo:ix_hi, :] # the same, but with noise added noisy_arr[ix_lo:ix_hi,:,0,0] = mvis.real[ix_lo:ix_hi,:] + \ noiseXX.real[ix_lo:ix_hi,:] noisy_arr[ix_lo:ix_hi,:,1,0] = mvis.real[ix_lo:ix_hi,:] + \ noiseYY.real[ix_lo:ix_hi,:]