def get_lensed_cls(theory,ells,clkk,lmax): import camb.correlations as corr ellrange = np.arange(0,lmax+2000,1) mulfact = ellrange*(ellrange+1.)/2./np.pi ucltt = theory.uCl('TT',ellrange)*mulfact uclee = theory.uCl('EE',ellrange)*mulfact uclbb = theory.uCl('BB',ellrange)*mulfact uclte = theory.uCl('TE',ellrange)*mulfact from scipy.interpolate import interp1d clkkfunc = interp1d(ells,clkk) clpp = clkkfunc(ellrange)*4./2./np.pi cmbarr = np.vstack((ucltt,uclee,uclbb,uclte)).T #print "Calculating lensed cls..." lcls = corr.lensed_cls(cmbarr,clpp) lmax = lmax+2000 cellrange = ellrange[:lmax].reshape((ellrange[:lmax].size,1)) #cellrange.ravel()[:lmax] lclall = lcls[:lmax,:] with np.errstate(divide='ignore', invalid='ignore'): lclall = np.nan_to_num(lclall/cellrange/(cellrange+1.)*2.*np.pi) cellrange = cellrange.ravel() #clcltt = lcls[:lmax,0] #clcltt = np.nan_to_num(clcltt/cellrange/(cellrange+1.)*2.*np.pi) #print clcltt lpad = lmax dtheory = TheorySpectra() with np.errstate(divide='ignore', invalid='ignore'): mult = np.nan_to_num(1./mulfact) ucltt *= mult uclee *= mult uclte *= mult uclbb *= mult #print cellrange.shape #print ucltt.shape dtheory.loadCls(cellrange,ucltt[:lmax],'TT',lensed=False,interporder="linear",lpad=lpad) dtheory.loadCls(cellrange,uclte[:lmax],'TE',lensed=False,interporder="linear",lpad=lpad) dtheory.loadCls(cellrange,uclee[:lmax],'EE',lensed=False,interporder="linear",lpad=lpad) dtheory.loadCls(cellrange,uclbb[:lmax],'BB',lensed=False,interporder="linear",lpad=lpad) dtheory.loadGenericCls(ells,clkk,"kk",lpad=lpad) lcltt = lclall[:,0] lclee = lclall[:,1] lclbb = lclall[:,2] lclte = lclall[:,3] #lcltt *= mult #lclee *= mult #lclte *= mult #lclbb *= mult dtheory.loadCls(cellrange,lcltt,'TT',lensed=True,interporder="linear",lpad=lpad) dtheory.loadCls(cellrange,lclte,'TE',lensed=True,interporder="linear",lpad=lpad) dtheory.loadCls(cellrange,lclee,'EE',lensed=True,interporder="linear",lpad=lpad) dtheory.loadCls(cellrange,lclbb,'BB',lensed=True,interporder="linear",lpad=lpad) return dtheory
def get_N0_iter(qe_key, nlev_t, nlev_p, beam_fwhm, cls_unl, lmin_ivf, lmax_ivf, itermax, lmax_qlm=None): """Iterative lensing-N0 estimate Calculates iteratively partially lensed spectra and lensing noise levels. This uses the python camb package to get the partially lensed spectra. This makes no assumption on response = 1 / noise hence is about twice as slow as it could be in standard cases. Args: qe_key: QE estimator key nlev_t: temperature noise level (in :math:`\mu `K-arcmin) nlev_p: polarisation noise level (in :math:`\mu `K-arcmin) beam_fwhm: Gaussian beam full width half maximum in arcmin cls_unl(dict): unlensed CMB power spectra lmin_ivf: minimal CMB multipole used in the QE lmax_ivf: maximal CMB multipole used in the QE itermax: number of iterations to perform lmax_qlm(optional): maximum lensing multipole to consider. Defaults to :math:`2 lmax_ivf` Returns Array of shape (itermax + 1, lmax_qlm + 1) with all iterated N0s. First entry is standard N0. #FIXME: this is requiring the full camb python package for the lensed spectra calc. """ assert qe_key in ['p_p', 'p', 'ptt'], qe_key try: from camb.correlations import lensed_cls except ImportError: assert 0, "could not import camb.correlations.lensed_cls" def cls2dls(cls): """Turns cls dict. into camb cl array format""" keys = ['tt', 'ee', 'bb', 'te'] lmax = np.max([len(cl) for cl in cls.values()]) - 1 dls = np.zeros((lmax + 1, 4), dtype=float) refac = np.arange(lmax + 1) * np.arange(1, lmax + 2, dtype=float) / (2. * np.pi) for i, k in enumerate(keys): cl = cls.get(k, np.zeros(lmax + 1, dtype=float)) sli = slice(0, min(len(cl), lmax + 1)) dls[sli, i] = cl[sli] * refac[sli] cldd = np.copy(cls.get('pp', None)) if cldd is not None: cldd *= np.arange(len(cldd)) ** 2 * np.arange(1, len(cldd) + 1, dtype=float) ** 2 / (2. * np.pi) return dls, cldd def dls2cls(dls): """Inverse operation to cls2dls""" assert dls.shape[1] == 4 lmax = dls.shape[0] - 1 cls = {} refac = 2. * np.pi * utils.cli( np.arange(lmax + 1) * np.arange(1, lmax + 2, dtype=float)) for i, k in enumerate(['tt', 'ee', 'bb', 'te']): cls[k] = dls[:, i] * refac return cls if lmax_qlm is None: lmax_qlm = 2 * lmax_ivf lmax_qlm = min(lmax_qlm, 2 * lmax_ivf) lmin_ivf = max(lmin_ivf, 1) transfi2 = utils.cli(hp.gauss_beam(beam_fwhm / 180. / 60. * np.pi, lmax=lmax_ivf)) ** 2 llp2 = np.arange(lmax_qlm + 1, dtype=float) ** 2 * np.arange(1, lmax_qlm + 2, dtype=float) ** 2 / (2. * np.pi) N0s = [] N0 = np.inf for irr, it in utils.enumerate_progress(range(itermax + 1)): dls_unl, cldd = cls2dls(cls_unl) clwf = 0. if it == 0 else cldd[:lmax_qlm + 1] * utils.cli(cldd[:lmax_qlm + 1] + llp2 * N0[:lmax_qlm + 1]) cldd[:lmax_qlm + 1] *= (1. - clwf) cls_plen = dls2cls(lensed_cls(dls_unl, cldd)) cls_ivfs = {} if qe_key in ['ptt', 'p_p', 'p']: cls_ivfs['tt'] = cls_plen['tt'][:lmax_ivf + 1] + (nlev_t * np.pi / 180. / 60.) ** 2 * transfi2 if qe_key in ['p_p', 'p']: cls_ivfs['ee'] = cls_plen['ee'][:lmax_ivf + 1] + (nlev_p * np.pi / 180. / 60.) ** 2 * transfi2 cls_ivfs['bb'] = cls_plen['bb'][:lmax_ivf + 1] + (nlev_p * np.pi / 180. / 60.) ** 2 * transfi2 if qe_key in ['p']: cls_ivfs['te'] = np.copy(cls_plen['te'][:lmax_ivf + 1]) cls_ivfs = utils.cl_inverse(cls_ivfs) for cl in cls_ivfs.values(): cl[:lmin_ivf] *= 0. fal = cls_ivfs n_gg = get_nhl(qe_key, qe_key, cls_plen, cls_ivfs, lmax_ivf, lmax_ivf, lmax_out=lmax_qlm)[0] r_gg = qresp.get_response(qe_key, lmax_ivf, 'p', cls_plen, cls_plen, fal, lmax_qlm=lmax_qlm)[0] N0 = n_gg * utils.cli(r_gg ** 2) N0s.append(N0) return np.array(N0s)
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(nonlinear=True) self.assertEqual(pars.NonLinear, model.NonLinear_pk) pars.set_matter_power(redshifts=[0., 0.17, 3.1], nonlinear=False) data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 3) self.assertAlmostEqual(s8[2], 0.80044, 3) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.697, 2) self.assertAlmostEqual(pk2[-2][-4], 53.47, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) camb.set_halofit_version('mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 824.6, delta=0.5) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) data.get_total_cls(2000) data.get_unlensed_scalar_cls(2500) data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) self.assertTrue(np.all(np.abs(cls_lensed2[2:2000, 2] / cls_lensed[2:2000, 2] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 0] / cls_lensed[2:3000, 0] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 1] / cls_lensed[2:3000, 1] - 1) < 1e-3)) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3))
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) pars.NonLinearModel.set_params(halofit_version='takahashi') self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(nonlinear=True) self.assertEqual(pars.NonLinear, model.NonLinear_pk) pars.set_matter_power(redshifts=[0., 0.17, 3.1], silent=True, nonlinear=False) data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 3) self.assertAlmostEqual(s8[2], 0.80044, 3) fs8 = data.get_fsigma8() self.assertAlmostEqual(fs8[0], 0.2431, 3) self.assertAlmostEqual(fs8[2], 0.424712, 3) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.709, 2) self.assertAlmostEqual(pk2[-2][-4], 56.436, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], silent=True, kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) PKnonlin2 = results.get_matter_power_interpolator(nonlinear=True, extrap_kmax=500) pk_interp2 = PKnonlin2.P(z, kh) self.assertTrue(np.sum((pk_interp / pk_interp2 - 1) ** 2) < 0.005) pars.NonLinearModel.set_params(halofit_version='mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 824.6, delta=0.5) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) data.get_total_cls(2000) data.get_unlensed_scalar_cls(2500) data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) cphi = data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) np.testing.assert_allclose(cls_lensed2[2:2000, 2], cls_lensed[2:2000, 2], rtol=1e-3) np.testing.assert_allclose(cls_lensed2[2:2000, 1], cls_lensed[2:2000, 1], rtol=1e-3) np.testing.assert_allclose(cls_lensed2[2:2000, 0], cls_lensed[2:2000, 0], rtol=1e-3) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3)) pars = camb.CAMBparams() pars.set_cosmology(H0=78, YHe=0.22) pars.set_for_lmax(2000, lens_potential_accuracy=1) pars.WantTensors = True results = camb.get_transfer_functions(pars) from camb import initialpower cls = [] for r in [0, 0.2, 0.4]: inflation_params = initialpower.InitialPowerLaw() inflation_params.set_params(ns=0.96, r=r, nt=0) results.power_spectra_from_transfer(inflation_params, silent=True) cls += [results.get_total_cls(CMB_unit='muK')] self.assertTrue(np.allclose((cls[1] - cls[0])[2:300, 2] * 2, (cls[2] - cls[0])[2:300, 2], rtol=1e-3)) # Check generating tensors and scalars together pars = camb.CAMBparams() pars.set_cosmology(H0=67) lmax = 2000 pars.set_for_lmax(lmax, lens_potential_accuracy=1) pars.InitPower.set_params(ns=0.96, r=0) pars.WantTensors = False results = camb.get_results(pars) cl1 = results.get_total_cls(lmax, CMB_unit='muK') pars.InitPower.set_params(ns=0.96, r=0.1, nt=0) pars.WantTensors = True results = camb.get_results(pars) cl2 = results.get_lensed_scalar_cls(lmax, CMB_unit='muK') ctensor2 = results.get_tensor_cls(lmax, CMB_unit='muK') results = camb.get_transfer_functions(pars) results.Params.InitPower.set_params(ns=1.1, r=1) inflation_params = initialpower.InitialPowerLaw() inflation_params.set_params(ns=0.96, r=0.05, nt=0) results.power_spectra_from_transfer(inflation_params, silent=True) cl3 = results.get_lensed_scalar_cls(lmax, CMB_unit='muK') ctensor3 = results.get_tensor_cls(lmax, CMB_unit='muK') self.assertTrue(np.allclose(ctensor2, ctensor3 * 2, rtol=1e-4)) self.assertTrue(np.allclose(cl1, cl2, rtol=1e-4)) # These are identical because all scalar spectra were identical (non-linear corrections change it otherwise) self.assertTrue(np.allclose(cl1, cl3, rtol=1e-4)) pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_for_lmax(2500) pars.min_l = 2 res = camb.get_results(pars) cls = res.get_lensed_scalar_cls(2000) pars.min_l = 1 res = camb.get_results(pars) cls2 = res.get_lensed_scalar_cls(2000) np.testing.assert_allclose(cls[2:, 0:2], cls2[2:, 0:2], rtol=1e-4) self.assertAlmostEqual(cls2[1, 0], 1.30388e-10, places=13) self.assertAlmostEqual(cls[1, 0], 0)
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) pars.NonLinearModel.set_params(halofit_version='takahashi') self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(nonlinear=True) self.assertEqual(pars.NonLinear, model.NonLinear_pk) pars.set_matter_power(redshifts=[0., 0.17, 3.1], silent=True, nonlinear=False) data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 3) self.assertAlmostEqual(s8[2], 0.80044, 3) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.709, 2) self.assertAlmostEqual(pk2[-2][-4], 56.436, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], silent=True, kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) PKnonlin2 = results.get_matter_power_interpolator(nonlinear=True, extrap_kmax=500) pk_interp2 = PKnonlin2.P(z, kh) self.assertTrue(np.sum((pk_interp / pk_interp2 - 1) ** 2) < 0.005) pars.NonLinearModel.set_params(halofit_version='mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertAlmostEqual(pk[0][160], 824.6, delta=0.5) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) data.get_total_cls(2000) data.get_unlensed_scalar_cls(2500) data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) cphi = data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) np.testing.assert_allclose(cls_lensed2[2:2000, 2], cls_lensed[2:2000, 2], rtol=1e-3) np.testing.assert_allclose(cls_lensed2[2:2000, 1], cls_lensed[2:2000, 1], rtol=1e-3) np.testing.assert_allclose(cls_lensed2[2:2000, 0], cls_lensed[2:2000, 0], rtol=1e-3) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3)) pars = camb.CAMBparams() pars.set_cosmology(H0=78, YHe=0.22) pars.set_for_lmax(2000, lens_potential_accuracy=1) pars.WantTensors = True results = camb.get_transfer_functions(pars) from camb import initialpower cls = [] for r in [0, 0.2, 0.4]: inflation_params = initialpower.InitialPowerLaw() inflation_params.set_params(ns=0.96, r=r, nt=0) results.power_spectra_from_transfer(inflation_params, silent=True) cls += [results.get_total_cls(CMB_unit='muK')] self.assertTrue(np.allclose((cls[1] - cls[0])[2:300, 2] * 2, (cls[2] - cls[0])[2:300, 2], rtol=1e-3)) # Check generating tensors and scalars together pars = camb.CAMBparams() pars.set_cosmology(H0=67) lmax = 2000 pars.set_for_lmax(lmax, lens_potential_accuracy=1) pars.InitPower.set_params(ns=0.96, r=0) pars.WantTensors = False results = camb.get_results(pars) cl1 = results.get_total_cls(lmax, CMB_unit='muK') pars.InitPower.set_params(ns=0.96, r=0.1, nt=0) pars.WantTensors = True results = camb.get_results(pars) cl2 = results.get_lensed_scalar_cls(lmax, CMB_unit='muK') ctensor2 = results.get_tensor_cls(lmax, CMB_unit='muK') results = camb.get_transfer_functions(pars) results.Params.InitPower.set_params(ns=1.1, r=1) inflation_params = initialpower.InitialPowerLaw() inflation_params.set_params(ns=0.96, r=0.05, nt=0) results.power_spectra_from_transfer(inflation_params, silent=True) cl3 = results.get_lensed_scalar_cls(lmax, CMB_unit='muK') ctensor3 = results.get_tensor_cls(lmax, CMB_unit='muK') self.assertTrue(np.allclose(ctensor2, ctensor3 * 2, rtol=1e-4)) self.assertTrue(np.allclose(cl1, cl2, rtol=1e-4)) # These are identical because all scalar spectra were identical (non-linear corrections change it otherwise) self.assertTrue(np.allclose(cl1, cl3, rtol=1e-4)) pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_for_lmax(2500) pars.min_l = 2 res = camb.get_results(pars) cls = res.get_lensed_scalar_cls(2000) pars.min_l = 1 res = camb.get_results(pars) cls2 = res.get_lensed_scalar_cls(2000) np.testing.assert_allclose(cls[2:, 0:2], cls2[2:, 0:2], rtol=1e-4) self.assertAlmostEqual(cls2[1, 0], 1.30388e-10, places=13) self.assertAlmostEqual(cls[1, 0], 0)
def testPowers(self): pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122, mnu=0.07, omk=0) pars.set_dark_energy() # re-set defaults pars.InitPower.set_params(ns=0.965, As=2e-9) self.assertAlmostEqual(pars.scalar_power(1), 1.801e-9, 4) self.assertAlmostEqual(pars.scalar_power([1, 1.5])[0], 1.801e-9, 4) pars.set_matter_power(redshifts=[0., 0.17, 3.1]) pars.NonLinear = model.NonLinear_none data = camb.get_results(pars) kh, z, pk = data.get_matter_power_spectrum(1e-4, 1, 20) kh2, z2, pk2 = data.get_linear_matter_power_spectrum() s8 = data.get_sigma8() self.assertAlmostEqual(s8[0], 0.24686, 4) self.assertAlmostEqual(s8[2], 0.80044, 4) pars.NonLinear = model.NonLinear_both data.calc_power_spectra(pars) kh3, z3, pk3 = data.get_matter_power_spectrum(1e-4, 1, 20) self.assertAlmostEqual(pk[-1][-3], 51.909, 2) self.assertAlmostEqual(pk3[-1][-3], 57.697, 2) self.assertAlmostEqual(pk2[-2][-4], 53.47, 2) camb.set_feedback_level(0) PKnonlin = camb.get_matter_power_interpolator(pars, nonlinear=True) pars.set_matter_power(redshifts=[0, 0.09, 0.15, 0.42, 0.76, 1.5, 2.3, 5.5, 8.9], kmax=10, k_per_logint=5) pars.NonLinear = model.NonLinear_both results = camb.get_results(pars) kh, z, pk = results.get_nonlinear_matter_power_spectrum() pk_interp = PKnonlin.P(z, kh) self.assertTrue(np.sum((pk / pk_interp - 1) ** 2) < 0.005) camb.set_halofit_version('mead') _, _, pk = results.get_nonlinear_matter_power_spectrum(params=pars, var1='delta_cdm', var2='delta_cdm') self.assertTrue(np.abs(pk[0][160] / 232.08 - 1) < 1e-3) lmax = 4000 pars.set_for_lmax(lmax) cls = data.get_cmb_power_spectra(pars) cls_tot = data.get_total_cls(2000) cls_scal = data.get_unlensed_scalar_cls(2500) cls_tensor = data.get_tensor_cls(2000) cls_lensed = data.get_lensed_scalar_cls(3000) cls_phi = data.get_lens_potential_cls(2000) # check lensed CL against python; will only agree well for high lmax as python has no extrapolation template cls_lensed2 = correlations.lensed_cls(cls['unlensed_scalar'], cls['lens_potential'][:, 0], delta_cls=False) self.assertTrue(np.all(np.abs(cls_lensed2[2:2000, 2] / cls_lensed[2:2000, 2] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 0] / cls_lensed[2:3000, 0] - 1) < 1e-3)) self.assertTrue(np.all(np.abs(cls_lensed2[2:3000, 1] / cls_lensed[2:3000, 1] - 1) < 1e-3)) self.assertTrue(np.all(np.abs((cls_lensed2[2:3000, 3] - cls_lensed[2:3000, 3]) / np.sqrt(cls_lensed2[2:3000, 0] * cls_lensed2[2:3000, 1])) < 1e-4)) corr, xvals, weights = correlations.gauss_legendre_correlation(cls['lensed_scalar']) clout = correlations.corr2cl(corr, xvals, weights, 2500) self.assertTrue(np.all(np.abs(clout[2:2300, 2] / cls['lensed_scalar'][2:2300, 2] - 1) < 1e-3))
def get_N0_iter(qe_key: str, nlev_t: float, nlev_p: float, beam_fwhm: float, cls_unl_fid: dict, lmin_ivf, lmax_ivf, itermax, cls_unl_dat=None, lmax_qlm=None, ret_delcls=False, datnoise_cls: dict or None = None, unlQE=False, version='1'): """Iterative lensing-N0 estimate Calculates iteratively partially lensed spectra and lensing noise levels. This uses the python camb package to get the partially lensed spectra. This makes no assumption on response = 1 / noise hence is about twice as slow as it could be in standard cases. Args: qe_key: QE estimator key nlev_t: temperature noise level (in :math:`\mu `K-arcmin) nlev_p: polarisation noise level (in :math:`\mu `K-arcmin) beam_fwhm: Gaussian beam full width half maximum in arcmin cls_unl_fid(dict): unlensed CMB power spectra lmin_ivf: minimal CMB multipole used in the QE lmax_ivf: maximal CMB multipole used in the QE itermax: number of iterations to perform lmax_qlm(optional): maximum lensing multipole to consider. Defaults to :math:`2 lmax_ivf` ret_delcls(optional): returns the partially delensed CMB cls as well if set datnoise_cls(optional): feeds in custom noise spectra to the data. The nlevs and beam only apply to the filtering in this case Returns Array of shape (itermax + 1, lmax_qlm + 1) with all iterated N0s. First entry is standard N0. Note: This assumes the unlensed spectra are known #FIXME: this is requiring the full camb python package for the lensed spectra calc. """ assert qe_key in ['p_p', 'p', 'ptt'], qe_key try: from camb.correlations import lensed_cls except ImportError: assert 0, "could not import camb.correlations.lensed_cls" if lmax_qlm is None: lmax_qlm = 2 * lmax_ivf lmax_qlm = min(lmax_qlm, 2 * lmax_ivf) lmin_ivf = max(lmin_ivf, 1) transfi2 = utils.cli( hp.gauss_beam(beam_fwhm / 180. / 60. * np.pi, lmax=lmax_ivf))**2 llp2 = np.arange(lmax_qlm + 1, dtype=float)**2 * np.arange( 1, lmax_qlm + 2, dtype=float)**2 / (2. * np.pi) if datnoise_cls is None: datnoise_cls = dict() if qe_key in ['ptt', 'p']: datnoise_cls['tt'] = (nlev_t * np.pi / 180. / 60.)**2 * transfi2 if qe_key in ['p_p', 'p']: datnoise_cls['ee'] = (nlev_p * np.pi / 180. / 60.)**2 * transfi2 datnoise_cls['bb'] = (nlev_p * np.pi / 180. / 60.)**2 * transfi2 N0s_biased = [] N0s_unbiased = [] N1s_biased = [] N1s_unbiased = [] delcls_fid = [] delcls_true = [] N0_unbiased = np.inf N1_unbiased = np.inf dls_unl_fid, cldd_fid = cls2dls(cls_unl_fid) cls_len_fid = dls2cls(lensed_cls(dls_unl_fid, cldd_fid)) if cls_unl_dat is None: cls_unl_dat = cls_unl_fid cls_len_true = cls_len_fid else: dls_unl_true, cldd_true = cls2dls(cls_unl_dat) cls_len_true = dls2cls(lensed_cls(dls_unl_true, cldd_true)) cls_plen_true = cls_len_true for irr, it in utils.enumerate_progress(range(itermax + 1)): dls_unl_true, cldd_true = cls2dls(cls_unl_dat) dls_unl_fid, cldd_fid = cls2dls(cls_unl_fid) if it == 0: rho_sqd_phi = 0. else: # The cross-correlation coefficient is identical for the Rfid-biased QE or the rescaled one rho_sqd_phi = np.zeros(len(cldd_true)) rho_sqd_phi[:lmax_qlm + 1] = cldd_true[:lmax_qlm + 1] * utils.cli( cldd_true[:lmax_qlm + 1] + llp2 * (N0_unbiased[:lmax_qlm + 1] + N1_unbiased[:lmax_qlm + 1])) if 'wE' in version: assert qe_key in ['p_p'] if it == 0: print('including imperfect knowledge of E in iterations') slic = slice(lmin_ivf, lmax_ivf + 1) rho_sqd_E = np.zeros(len(dls_unl_true[:, 1])) rho_sqd_E[slic] = cls_unl_dat['ee'][slic] * utils.cli( cls_plen_true['ee'][slic] + datnoise_cls['ee'][slic]) dls_unl_fid[:, 1] *= rho_sqd_E dls_unl_true[:, 1] *= rho_sqd_E cldd_fid *= rho_sqd_phi cldd_true *= rho_sqd_phi cls_plen_fid_resolved = dls2cls(lensed_cls(dls_unl_fid, cldd_fid)) cls_plen_true_resolved = dls2cls( lensed_cls(dls_unl_true, cldd_true)) cls_plen_fid = { ck: cls_len_fid[ck] - (cls_plen_fid_resolved[ck] - cls_unl_fid[ck][:len(cls_len_fid[ck])]) for ck in cls_len_fid.keys() } cls_plen_true = { ck: cls_len_true[ck] - (cls_plen_true_resolved[ck] - cls_unl_dat[ck][:len(cls_len_true[ck])]) for ck in cls_len_true.keys() } else: cldd_true *= (1. - rho_sqd_phi) # The true residual lensing spec. cldd_fid *= (1. - rho_sqd_phi ) # What I think the residual lensing spec is cls_plen_fid = dls2cls(lensed_cls(dls_unl_fid, cldd_fid)) cls_plen_true = dls2cls(lensed_cls(dls_unl_true, cldd_true)) cls_filt = cls_plen_fid if not unlQE else cls_unl_fid cls_w = cls_plen_fid if not unlQE else cls_unl_fid cls_f = cls_plen_true fal = {} dat_delcls = {} if qe_key in ['ptt', 'p']: fal['tt'] = cls_filt['tt'][:lmax_ivf + 1] + ( nlev_t * np.pi / 180. / 60.)**2 * transfi2 dat_delcls['tt'] = cls_plen_true['tt'][:lmax_ivf + 1] + datnoise_cls['tt'] if qe_key in ['p_p', 'p']: fal['ee'] = cls_filt['ee'][:lmax_ivf + 1] + ( nlev_p * np.pi / 180. / 60.)**2 * transfi2 fal['bb'] = cls_filt['bb'][:lmax_ivf + 1] + ( nlev_p * np.pi / 180. / 60.)**2 * transfi2 dat_delcls['ee'] = cls_plen_true['ee'][:lmax_ivf + 1] + datnoise_cls['ee'] dat_delcls['bb'] = cls_plen_true['bb'][:lmax_ivf + 1] + datnoise_cls['bb'] if qe_key in ['p']: fal['te'] = np.copy(cls_filt['te'][:lmax_ivf + 1]) dat_delcls['te'] = np.copy(cls_plen_true['te'][:lmax_ivf + 1]) fal = utils.cl_inverse(fal) for cl in fal.values(): cl[:lmin_ivf] *= 0. for cl in dat_delcls.values(): cl[:lmin_ivf] *= 0. cls_ivfs_arr = utils.cls_dot([fal, dat_delcls, fal]) cls_ivfs = dict() for i, a in enumerate(['t', 'e', 'b']): for j, b in enumerate(['t', 'e', 'b'][i:]): if np.any(cls_ivfs_arr[i, j + i]): cls_ivfs[a + b] = cls_ivfs_arr[i, j + i] n_gg = get_nhl(qe_key, qe_key, cls_w, cls_ivfs, lmax_ivf, lmax_ivf, lmax_out=lmax_qlm)[0] r_gg_true = qresp.get_response(qe_key, lmax_ivf, 'p', cls_w, cls_f, fal, lmax_qlm=lmax_qlm)[0] r_gg_fid = qresp.get_response( qe_key, lmax_ivf, 'p', cls_w, cls_w, fal, lmax_qlm=lmax_qlm)[0] if cls_f is not cls_w else r_gg_true N0_biased = n_gg * utils.cli( r_gg_fid** 2) # N0 of possibly biased (by Rtrue / Rfid) QE estimator N0_unbiased = n_gg * utils.cli( r_gg_true**2 ) # N0 of QE estimator after rescaling by Rfid / Rtrue to make it unbiased N0s_biased.append(N0_biased) N0s_unbiased.append(N0_unbiased) cls_plen_true['pp'] = cldd_true * utils.cli( np.arange(len(cldd_true))**2 * np.arange(1, len(cldd_true) + 1, dtype=float)**2 / (2. * np.pi)) cls_plen_fid['pp'] = cldd_fid * utils.cli( np.arange(len(cldd_fid))**2 * np.arange(1, len(cldd_fid) + 1, dtype=float)**2 / (2. * np.pi)) if 'wN1' in version: if it == 0: print('Adding n1 in iterations') from lensitbiases import n1_fft from scipy.interpolate import UnivariateSpline as spl lib = n1_fft.n1_fft(fal, cls_w, cls_f, np.copy(cls_plen_true['pp']), lminbox=50, lmaxbox=5000, k2l=None) n1_Ls = np.arange(50, lmax_qlm + 1, 50) if lmax_qlm not in n1_Ls: n1_Ls = np.append(n1_Ls, lmax_qlm) n1 = np.array( [lib.get_n1(qe_key, L, do_n1mat=False) for L in n1_Ls]) N1_biased = spl(n1_Ls, n1_Ls**2 * (n1_Ls * 1. + 1)**2 * n1 / r_gg_fid[n1_Ls]**2, k=2, s=0, ext='zeros')(np.arange(len(N0_unbiased))) N1_biased *= utils.cli( np.arange(lmax_qlm + 1)**2 * np.arange(1, lmax_qlm + 2, dtype=float)**2) N1_unbiased = N1_biased * (r_gg_fid * utils.cli(r_gg_true))**2 else: N1_biased = np.zeros(lmax_qlm + 1, dtype=float) N1_unbiased = np.zeros(lmax_qlm + 1, dtype=float) delcls_fid.append(cls_plen_fid) delcls_true.append(cls_plen_true) N1s_biased.append(N1_biased) N1s_unbiased.append(N1_unbiased) return (np.array(N0s_biased), np.array(N0s_unbiased)) if not ret_delcls else ( (np.array(N0s_biased), np.array(N0s_unbiased), delcls_fid, delcls_true))
def get_N0_iter(qe_key: str, nlev_t: float or np.ndarray, nlev_p: float or np.ndarray, beam_fwhm: float, cls_unl_fid: dict, lmin_cmb, lmax_cmb, itermax, cls_unl_dat=None, lmax_qlm=None, ret_delcls=False, datnoise_cls: dict or None = None): r"""Iterative lensing-N0 estimate Calculates iteratively partially lensed spectra and lensing noise levels. This uses the python camb package to get the partially lensed spectra. At each iteration this takes out the resolved part of the lenses and recomputes a N0 Args: qe_key: QE estimator key nlev_t: temperature noise level (in :math:`\mu `K-arcmin) (an array can be passed for scale-dependent noise level) nlev_p: polarisation noise level (in :math:`\mu `K-arcmin)(an array can be passed for scale-dependent noise level) beam_fwhm: Gaussian beam full width half maximum in arcmin cls_unl_fid(dict): unlensed CMB power spectra lmin_cmb: minimal CMB multipole used in the QE lmax_cmb: maximal CMB multipole used in the QE itermax: number of iterations to perform lmax_qlm(optional): maximum lensing multipole to consider. Defaults to 2 lmax_ivf ret_delcls(optional): returns the partially delensed CMB cls as well if set datnoise_cls(optional): feeds in custom noise spectra to the data. The nlevs and beam only apply to the filtering in this case Returns Array of shape (itermax + 1, lmax_qlm + 1) with all iterated N0s. First entry is standard N0. Note: this is requiring camb python package for the lensed spectra calc. """ assert qe_key in ['p_p', 'p', 'ptt'], qe_key try: from camb.correlations import lensed_cls except ImportError: assert 0, "could not import camb.correlations.lensed_cls" if isinstance(lmax_cmb, dict): lmaxs_ivf = lmax_cmb print("Seeing lmax's:") for s in lmaxs_ivf.keys(): print(s + ': ' + str(lmaxs_ivf[s])) else: lmaxs_ivf = {s: lmax_cmb for s in ['t', 'e', 'b']} lmin_ivf = lmin_cmb lmax_ivf = np.max(list(lmaxs_ivf.values())) if lmax_qlm is None: lmax_qlm = 2 * lmax_ivf lmax_qlm = min(lmax_qlm, 2 * lmax_ivf) lmin_ivf = max(lmin_ivf, 1) transfi2 = utils.cli( hp.gauss_beam(beam_fwhm / 180. / 60. * np.pi, lmax=lmax_ivf))**2 llp2 = np.arange(lmax_qlm + 1, dtype=float)**2 * np.arange( 1, lmax_qlm + 2, dtype=float)**2 / (2. * np.pi) if datnoise_cls is None: datnoise_cls = dict() if qe_key in ['ptt', 'p']: datnoise_cls['tt'] = (nlev_t * np.pi / 180. / 60.)**2 * transfi2 if qe_key in ['p_p', 'p']: datnoise_cls['ee'] = (nlev_p * np.pi / 180. / 60.)**2 * transfi2 datnoise_cls['bb'] = (nlev_p * np.pi / 180. / 60.)**2 * transfi2 N0s_biased = [] N0s_unbiased = [] delcls_fid = [] delcls_true = [] N0_unbiased = np.inf if cls_unl_dat is None: cls_unl_dat = cls_unl_fid for irr, it in utils.enumerate_progress(range(itermax + 1)): dls_unl_true, cldd_true = cls2dls(cls_unl_dat) dls_unl_fid, cldd_fid = cls2dls(cls_unl_fid) if it == 0: rho_sqd_phi = 0. else: # The cross-correlation coefficient is identical for the Rfid-biased QE or the rescaled one rho_sqd_phi = np.zeros(len(cldd_true)) rho_sqd_phi[:lmax_qlm + 1] = cldd_true[:lmax_qlm + 1] * utils.cli( cldd_true[:lmax_qlm + 1] + llp2 * N0_unbiased[:lmax_qlm + 1]) cldd_true *= (1. - rho_sqd_phi) # The true residual lensing spec. cldd_fid *= (1. - rho_sqd_phi ) # What I think the residual lensing spec is cls_plen_fid = dls2cls(lensed_cls(dls_unl_fid, cldd_fid)) cls_plen_true = dls2cls(lensed_cls(dls_unl_true, cldd_true)) cls_filt = cls_plen_fid cls_f = cls_plen_true fal = {} dat_delcls = {} if qe_key in ['ptt', 'p']: fal['tt'] = cls_filt['tt'][:lmax_ivf + 1] + ( nlev_t * np.pi / 180. / 60.)**2 * transfi2 dat_delcls['tt'] = cls_plen_true['tt'][:lmax_ivf + 1] + datnoise_cls['ee'] if qe_key in ['p_p', 'p']: fal['ee'] = cls_filt['ee'][:lmax_ivf + 1] + ( nlev_p * np.pi / 180. / 60.)**2 * transfi2 fal['bb'] = cls_filt['bb'][:lmax_ivf + 1] + ( nlev_p * np.pi / 180. / 60.)**2 * transfi2 dat_delcls['ee'] = cls_plen_true['ee'][:lmax_ivf + 1] + datnoise_cls['ee'] dat_delcls['bb'] = cls_plen_true['bb'][:lmax_ivf + 1] + datnoise_cls['bb'] if qe_key in ['p']: fal['te'] = np.copy(cls_filt['te'][:lmax_ivf + 1]) dat_delcls['te'] = np.copy(cls_plen_true['te'][:lmax_ivf + 1]) for spec in fal.keys(): fal[spec][min(lmaxs_ivf[spec[0]], lmaxs_ivf[spec[1]]) + 1:] *= 0 for spec in dat_delcls.keys(): dat_delcls[spec][min(lmaxs_ivf[spec[0]], lmaxs_ivf[spec[1]]) + 1:] *= 0 fal = utils.cl_inverse(fal) for cl in fal.values(): cl[:lmin_ivf] *= 0. for cl in dat_delcls.values(): cl[:lmin_ivf] *= 0. cls_ivfs = utils.cls_dot([fal, dat_delcls, fal], ret_dict=True) cls_w = deepcopy(cls_plen_fid) for spec in cls_w.keys(): # in principle not necessary cls_w[spec][:lmin_ivf] *= 0. cls_w[spec][min(lmaxs_ivf[spec[0]], lmaxs_ivf[spec[1]]) + 1:] *= 0 n_gg = nhl.get_nhl(qe_key, qe_key, cls_w, cls_ivfs, lmax_ivf, lmax_ivf, lmax_out=lmax_qlm)[0] r_gg_true = qresp.get_response(qe_key, lmax_ivf, 'p', cls_w, cls_f, fal, lmax_qlm=lmax_qlm)[0] r_gg_fid = qresp.get_response( qe_key, lmax_ivf, 'p', cls_w, cls_w, fal, lmax_qlm=lmax_qlm)[0] if cls_f is not cls_w else r_gg_true N0_biased = n_gg * utils.cli( r_gg_fid** 2) # N0 of possibly biased (by Rtrue / Rfid) QE estimator N0_unbiased = n_gg * utils.cli( r_gg_true**2 ) # N0 of QE estimator after rescaling by Rfid / Rtrue to make it unbiased N0s_biased.append(N0_biased) N0s_unbiased.append(N0_unbiased) cls_plen_true['pp'] = cldd_true * utils.cli( np.arange(len(cldd_true))**2 * np.arange(1, len(cldd_true) + 1, dtype=float)**2 / (2. * np.pi)) cls_plen_fid['pp'] = cldd_fid * utils.cli( np.arange(len(cldd_fid))**2 * np.arange(1, len(cldd_fid) + 1, dtype=float)**2 / (2. * np.pi)) delcls_fid.append(cls_plen_fid) delcls_true.append(cls_plen_true) return (np.array(N0s_biased), np.array(N0s_unbiased)) if not ret_delcls else ( (np.array(N0s_biased), np.array(N0s_unbiased), delcls_fid, delcls_true))