def test_ensemble(cl, mask, rseed, lmin=0, lmax=100, nside=256, nsample=100): print_message('Ensemble test with my code') print_message('Mask coverage = %f' % (np.average(mask))) #mask = np.logical_not(hp.ud_grade(mask, nside_out = nside)) mask = hp.ud_grade(mask, nside_out=nside) print_message('Mask coverage for nside %d = %f' % (nside, np.average(mask))) np.random.seed(rseed) seeds = np.random.randint(low=0, high=2**32 - 1, size=nsample) st = time.time() lmins = np.arange(lmin, lmax) lmaxs = lmins + 1 bins = xpol.Bins(lmins, lmaxs) xp = xpol.Xpol(mask, bins) print_debug('Time for initalizing x-pol:', time.time() - st) biased_arr = [] unbiased_arr = [] for i, s in enumerate(seeds): print_message('sample #%d' % i) np.random.seed(s) st = time.time() m = hp.synfast(cl, nside=nside, new=True) print_debug('Time for synfast:', time.time() - st) st = time.time() biased, unbiased = xp.get_spectra(m) print_debug('Time for get spectra from x-pol:', time.time() - st) biased_arr.append(biased) unbiased_arr.append(unbiased) #st = time.time() #cl_ana = hp.anafast(m) #print_debug('Time for get anafast:', time.time()-st) biased = np.average(biased_arr, axis=0) unbiased_tmp = np.average(unbiased_arr, axis=0) print(biased.shape) print(unbiased_tmp.shape) unbiased = np.zeros(biased.shape) unbiased[:, lmin:] = unbiased_tmp plt.figure() plt.loglog(np.abs(cl2dl(cl[:3].T))) #plt.loglog(np.abs(cl2dl(cl_ana[:3].T))) plt.loglog(np.abs(cl2dl(biased[:3].T)), 'x-', lw=0.5) plt.loglog(np.abs(cl2dl(unbiased[:3])).T, '+-', lw=0.5) plt.ylim(1e-6, 1e4) plt.xlabel = 'multipole moment ($l$)' plt.ylabel = '$D_l (K^2)' plt.title('X-pol Ensemble')
def test_single(cl, mask, rseed, lmin=0, lmax=100, nside=256): print_message('Single test with my code') print_message('Mask coverage = %f' % (np.average(mask))) #mask = np.logical_not(hp.ud_grade(mask, nside_out = nside)) mask = hp.ud_grade(mask, nside_out=nside) print_message('Mask coverage for nside %d = %f' % (nside, np.average(mask))) np.random.seed(rseed) st = time.time() m = hp.synfast(cl, nside=nside, new=True) mm = hp.ma(m) mm.mask = np.logical_not(mask) hp.mollview(mm.filled()[0]) print_debug('Time for synfast:', time.time() - st) st = time.time() cl_ana = hp.anafast(m) print_debug('Time for get anafast:', time.time() - st) st = time.time() Wl = hp.anafast(mask) Mll = coupling_matrix(Wl, lmin=lmin, lmax=lmax) Mlli = np.linalg.pinv(Mll) print_debug('Time for getting Mll:', time.time() - st) st = time.time() biased = hp.anafast(mm.filled()) print_debug('Time for getting biased spectrum:', time.time() - st) st = time.time() Cl_tmp = np.concatenate(biased[:4, lmin:lmax], 0) unbiased = np.dot(Mlli, Cl_tmp).reshape(4, lmax - lmin) print_debug('Time for get unbiased spectra:', time.time() - st) st = time.time() cl_ana = hp.anafast(m) print_debug('Time for get anafast:', time.time() - st) plt.figure() plt.loglog(np.abs(cl2dl(cl[:3].T))) plt.loglog(np.abs(cl2dl(cl_ana[:3].T)), '*-', lw=0.5) plt.loglog(np.abs(cl2dl(biased[:3].T)), 'x-', lw=0.5) plt.loglog(np.abs(cl2dl(unbiased[:3])).T, '+-', lw=0.5) plt.ylim(1e-6, 1e4) plt.xlabel = 'multipole moment ($l$)' plt.ylabel = '$D_l (K^2)' plt.title("Coupling matrix") return biased, unbiased
def test_single(cl, mask, rseed, lmin=0, lmax=100, nside=256): print_message('Single test with my code') print_message('Mask coverage = %f' % (np.average(mask))) #mask = np.logical_not(hp.ud_grade(mask, nside_out = nside)) mask = hp.ud_grade(mask, nside_out=nside) print_message('Mask coverage for nside %d = %f' % (nside, np.average(mask))) np.random.seed(rseed) st = time.time() m = hp.synfast(cl, nside=nside, new=True) print_debug('Time for synfast:', time.time() - st) st = time.time() cl_ana = hp.anafast(m) print_debug('Time for get anafast:', time.time() - st) st = time.time() lmins = np.arange(lmin, lmax) lmaxs = lmins + 1 bins = xpol.Bins(lmins, lmaxs) xp = xpol.Xpol(mask, bins) print_debug('Time for initalizing x-pol:', time.time() - st) st = time.time() biased, unbiased_tmp = xp.get_spectra(m) print_debug('Time for get spectra from x-pol:', time.time() - st) unbiased = np.zeros(biased.shape) unbiased[:, 2:] = unbiased_tmp plt.figure() plt.loglog(np.abs(cl2dl(cl[:3].T))) plt.loglog(np.abs(cl2dl(cl_ana[:3].T)), '*-', lw=0.5) plt.loglog(np.abs(cl2dl(biased[:3].T)), 'x-', lw=0.5) plt.loglog(np.abs(cl2dl(unbiased[:3])).T, '+-', lw=0.5) plt.ylim(1e-6, 1e4) plt.xlabel = 'multipole moment ($l$)' plt.ylabel = '$D_l (K^2)' plt.title("x-pol") return biased, unbiased
def test_ensemble(cl, mask, rseed, lmin=0, lmax=100, nside=256, nsample=100): print_message('Ensemble test with my code') print_message('Mask coverage = %f' % (np.average(mask))) #mask = np.logical_not(hp.ud_grade(mask, nside_out = nside)) mask = hp.ud_grade(mask, nside_out=nside) print_message('Mask coverage for nside %d = %f' % (nside, np.average(mask))) np.random.seed(rseed) seeds = np.random.randint(low=0, high=2**32 - 1, size=nsample) Wl = hp.anafast(mask, lmax=3 * nside) st = time.time() Mll = coupling_matrix(Wl, lmin=lmin, lmax=lmax) print_debug('Time for getting Mll:', time.time() - st) st = time.time() Mllp = coupling_matrix_fast(Wl, lmin=lmin, lmax=lmax) print_debug('Time for getting Mll_fast:', time.time() - st) plt.matshow(Mll) plt.matshow(Mllp) plt.matshow(Mllp - Mll) plt.show() st = time.time() Mlli = np.linalg.pinv(Mll) print_debug('Time for getting Mlli:', time.time() - st) biased_arr = [] unbiased_arr = [] for i, s in enumerate(seeds): print_message('sample #%d' % i) np.random.seed(s) st = time.time() m = hp.synfast(cl, nside=nside, new=True) mm = hp.ma(m) mm.mask = np.logical_not(mask) print_debug('Time for synfast:', time.time() - st) st = time.time() biased = hp.anafast(mm.filled(), lmax=lmax) print_debug('Time for getting biased spectrum:', time.time() - st) st = time.time() Cl_tmp = np.concatenate(biased[:4, lmin:lmax], 0) unbiased = np.dot(Mlli, Cl_tmp).reshape(4, lmax - lmin) print_debug('Time for get unbiased spectra:', time.time() - st) biased_arr.append(biased) unbiased_arr.append(unbiased) #st = time.time() #cl_ana = hp.anafast(m) #print_debug('Time for get anafast:', time.time()-st) biased = np.average(biased_arr, axis=0) unbiased_tmp = np.average(unbiased_arr, axis=0) unbiased = np.zeros(biased.shape) unbiased[:4, lmin:lmax] = unbiased_tmp print(biased.shape) print(unbiased.shape) plt.figure() plt.loglog(np.abs(cl2dl(cl[:3].T))) #plt.loglog(np.abs(cl2dl(cl_ana[:3].T)), '*-', lw=0.5) plt.loglog(np.abs(cl2dl(biased[:3].T)), 'x-', lw=0.5) plt.loglog(np.abs(cl2dl(unbiased[:3])).T, '+-', lw=0.5) plt.ylim(1e-6, 1e4) plt.xlabel = 'multipole moment ($l$)' plt.ylabel = '$D_l (K^2)' plt.title("Coupling matrix Ensemble") return biased, unbiased
def compare_cmb_noise(): nside = 1024 lmax = 3 * nside - 1 #1500#nside*3 - 1 cl_r01 = spectrum.get_spectrum_camb(lmax, r=0.01) cl_r001 = spectrum.get_spectrum_camb(lmax, r=0.001) nl_wp10 = spectrum.get_spectrum_whitenoise( lmax, wp=10, bw=30, ) # 1.5*60) nl_wp2 = spectrum.get_spectrum_whitenoise( lmax, wp=2, bw=30, ) #1.5*60) print(cl_r01.shape) print(cl_r001.shape) print(nl_wp10.shape) print(nl_wp2.shape) ell = np.arange(lmax + 1) plt.loglog(ell, cl_r01[:3].T * 1e12) plt.loglog(ell, cl_r001[:3].T * 1e12) plt.loglog(ell, nl_wp10[:3].T * 1e12) plt.loglog(ell, nl_wp2[:3].T * 1e12) cls = utils.dl2cl(nl_wp10) m = hp.synfast(cls, nside=nside, new=True, fwhm=0.0 * np.pi / 180) hp.mollview(m[0]) std_wnT = np.std(m[0]) std_wnQ = np.std(m[1]) std_wnU = np.std(m[2]) std_wn = np.std(m) #mr = np.random.random(len(m[0])) mrT = np.random.normal(size=len(m[0]), scale=std_wnT) mrQ = np.random.normal(size=len(m[0]), scale=std_wnQ) mrU = np.random.normal(size=len(m[0]), scale=std_wnU) mr = [mrT, mrQ, mrU] std_wnTr = np.std(mr[0]) std_wnQr = np.std(mr[1]) std_wnUr = np.std(mr[2]) std_wnr = np.std(mr) print('Standard deviations of synthesized noise maps') print(std_wnT, std_wnQ, std_wnU) print(std_wn) print('Standard deviations of Gaussian random maps') print(std_wnTr, std_wnQr, std_wnUr) print(std_wnr) cls_rec = hp.anafast(m, lmax=lmax) cls_ran = hp.anafast(mr, lmax=lmax) names = ['TT', 'EE', 'BB'] for i, name in enumerate(names): plt.figure() plt.loglog(ell, utils.cl2dl(cls)[i].T) plt.loglog(ell, utils.cl2dl(cls_rec)[i].T, '*', label='Synthesized map') plt.loglog(ell, utils.cl2dl(cls_ran)[i].T, '+', label='Gaussian random number map') plt.title(name) plt.show()
def test_n2logLf_TEB(): K2uK = 1e12 clscale = K2uK * 1.0 cls_fid = get_spectrum_camb(lmax, isDl=False) * clscale cls_syn = get_spectrum_camb( lmax, tau=0.0522, As=2.092e-9, r=0.01, isDl=False) * clscale map_syn = hp.synfast(cls_syn, nside=nside, new=True) cls_est = hp.anafast(map_syn, lmax=lmax) cls_ana = get_spectrum_camb(lmax, isDl=False) * clscale Cls_ana = lh.cls2Cls(cls_ana) Cls_est = lh.cls2Cls(cls_est) inv_Cls_fid, det_Cls_fid = lh.invdet_fid(cls_fid) n2logLf = n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid) print('Likelihood for scale %e = %e' % (clscale, n2logLf)) def fit_minuit_1(tau, As, r): cls_ana = get_spectrum_camb(lmax=lmax, tau=tau, As=As, r=r, isDl=False) * clscale Cls_ana = lh.cls2Cls(cls_ana) lk = n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid) print('tau = %e, As = %e, r = %e, lk = %e' % (tau, As, r, lk)) return lk tau0 = 0.0522 tau_limit = (0.02, 0.08) As0 = 2.1e-9 As_limit = (1.5e-9, 2.5e-9) r0 = 0.01 r_limit = (0.0, 0.4) m = Minuit(fit_minuit_1, tau=tau0, As=As0, r=r0, limit_tau=tau_limit, limit_As=As_limit, limit_r=r_limit) st = time.time() res = m.migrad() print('Elapsed time for migrad: %fs' % (time.time() - st)) st = time.time() res = m.hesse() print('Elapsed time for hesse: %fs' % (time.time() - st)) st = time.time() #res = m.minos() print('Elapsed time for minos: %fs' % (time.time() - st)) plt.figure() m.draw_profile('tau') plt.figure() m.draw_profile('As') plt.figure() m.draw_profile('r') tau_min = m.values['tau'] tau_err = m.errors['tau'] cls_min = get_spectrum_camb(lmax=lmax, tau=tau_min, isDl=False) * clscale cls_upp = get_spectrum_camb(lmax=lmax, tau=tau_min + tau_err, isDl=False) * clscale cls_low = get_spectrum_camb(lmax=lmax, tau=tau_min - tau_err, isDl=False) * clscale ell = np.arange(len(cls_est[0])) plt.figure() plt.loglog(ell, cl2dl(cls_est[:3].T), '*') plt.loglog(ell, cl2dl(cls_syn[:3].T), '--', linewidth=1.0) plt.loglog(ell, cl2dl(cls_min[:3].T), '-', linewidth=2.0) plt.loglog(ell, cl2dl(cls_upp[:3].T), '--', linewidth=0.5) plt.loglog(ell, cl2dl(cls_low[:3].T), '--', linewidth=0.5) pprint(res) plt.show()
def main(): st = time.time() maskGalactic = hp.read_map('./masks/HFI_Mask_GalPlane-apo0_2048_R2.00.fits', field=3) maskPoint = hp.read_map('./masks/HFI_Mask_PointSrc_2048_R2.00.fits') hitGB = hp.read_map('./masks/hitMap_GB220_1024.fits') print ('time for reading masks', time.time() - st) st = time.time() maskGB_equ = hitmap2mask(hitGB) maskGB_gal = EQU2GAL(maskGB_equ) maskTr_equ = np.full(maskGB_equ.shape, 1) maskGalactic = hp.ud_grade(maskGalactic, 1024) print ('time for manipulating masks', time.time() - st) m_comb = maskGB_gal * maskGalactic m_comb_equ = GAL2EQU(m_comb) """ hp.mollview(maskGalactic) hp.mollview(maskPoint) hp.mollview(maskGB_equ) hp.mollview(maskGB_gal) hp.mollview(m_comb) hp.mollview(m_comb_equ) """ mask = maskGalactic mask = maskGB_equ st = time.time() Wl = hp.anafast(mask) print ('time for anafast Wl', time.time() - st) st = time.time() Wl_Tr = hp.anafast(maskTr_equ) print ('time for anafast Wl_Tr', time.time() - st) # Cl_camb = get_spectrum_camb(lmax=1000, isDl=False) np.random.seed(42) m = hp.synfast(Cl_camb, nside=1024, new=True) # Cl_full = hp.anafast(m)[:4] mm = hp.ma(m) mm.mask = np.logical_not(mask) # Cl_gbcut = hp.anafast(mm.filled()) st = time.time() Mll = coupling_matrix(Wl) print ('time to calculate coupling matrix', time.time() - st) st = time.time() Mll_Tr = coupling_matrix(Wl_Tr) print ('time to calculate coupling matrix trivial', time.time() - st) plt.matshow(np.log10(Mll)) plt.matshow(np.log10(Mll_Tr)) st = time.time() Mlli = np.linalg.pinv(Mll) print ('time to calculate inverting coupling matrix', time.time() - st) plt.matshow(np.log10(Mlli)) # Cl_ana = Cl_camb[:,:100] Cl_ana = np.concatenate(Cl_ana, 0) Cl_ana_gbcut = np.dot(Mll, Cl_ana) Cl_ana_gbcut_inv = np.dot(Mlli, Cl_ana_gbcut) Cl_ana_gbcut = Cl_ana_gbcut.reshape(4, 100) #len(Cls_gbcut)/4) Cl_ana_gbcut_inv = Cl_ana_gbcut_inv.reshape(4, 100) Cl_ana_Tr = np.dot(Mll_Tr, Cl_ana) Cl_ana_Tr = Cl_ana_Tr.reshape(4, 100) #len(Cls_gbcut)/4) Cl_gbcut_tmp = Cl_gbcut[:4,:100] Cl_gbcut_tmp = np.concatenate(Cl_gbcut_tmp, 0) Cl_gbcut_inv = np.dot(Mlli, Cl_gbcut_tmp) Cl_gbcut_inv = Cl_gbcut_inv.reshape(4, 100) ell = np.arange(2, 100) plt.figure() plt.loglog(ell, cl2dl(Cl_gbcut)[:3,2:100].T, '*') plt.loglog(ell, cl2dl(Cl_ana_gbcut)[:3, 2:100].T) plt.xlabel('Multipole moment, $l$') plt.ylabel('$\l(l+1)/2\pi C_l (K^2)$') plt.figure() plt.loglog(ell, cl2dl(Cl_full)[:3,2:100].T, '*') plt.loglog(ell, cl2dl(Cl_camb)[:3,2:100].T) plt.loglog(ell, cl2dl(Cl_ana_Tr)[:3,2:100].T) plt.xlabel('Multipole moment, $l$') plt.ylabel('$\l(l+1)/2\pi C_l (K^2)$') plt.figure() plt.loglog(ell, cl2dl(Cl_gbcut_inv)[:3, 2:100].T, '*') plt.loglog(ell, cl2dl(Cl_ana_gbcut_inv)[:3, 2:100].T) plt.xlabel('Multipole moment, $l$') plt.ylabel('$\l(l+1)/2\pi C_l (K^2)$') plt.show()