def test_sources(packages_path, skip_not_installed): camb = install_test_wrapper(skip_not_installed, get_camb, packages_path) from camb.sources import GaussianSourceWindow pars = camb.set_params(**params) pars.set_for_lmax(500) pars.SourceWindows = [ GaussianSourceWindow(redshift=0.17, source_type='counts', bias=1.2, sigma=0.04)] results = camb.get_results(pars) dic = results.get_source_cls_dict() # noinspection PyDefaultArgument def test_likelihood(_self): assert abs(_self.provider.get_source_Cl()[('source1', 'source1')][100] / dic['W1xW1'][ 100] - 1) < 0.001, \ "CAMB gaussian source window results do not match" return 0 test_likelihood_requires = { 'source_Cl': {'sources': {'source1': {'function': 'gaussian', 'source_type': 'counts', 'bias': 1.2, 'redshift': 0.17, 'sigma': 0.04}}, 'limber': True, 'lmax': 500}} model = _get_model( packages_path, {'external': test_likelihood, 'requires': test_likelihood_requires}, skip_not_installed) model.loglike({})
def testSources(self): from camb.sources import GaussianSourceWindow, SplinedSourceWindow pars = camb.CAMBparams() pars.set_cosmology(H0=64, mnu=0) pars.set_for_lmax(1200) pars.Want_CMB = False pars.SourceWindows = [ GaussianSourceWindow(redshift=0.17, source_type='counts', bias=1.2, sigma=0.04, dlog10Ndm=-0.2), GaussianSourceWindow(redshift=0.5, source_type='lensing', sigma=0.07, dlog10Ndm=0)] pars.SourceTerms.limber_windows = True results = camb.get_results(pars) cls = results.get_source_cls_dict() zs = np.arange(0, 0.5, 0.02) W = np.exp(-(zs - 0.17) ** 2 / 2 / 0.04 ** 2) / np.sqrt(2 * np.pi) / 0.04 pars.SourceWindows[0] = SplinedSourceWindow(bias=1.2, dlog10Ndm=-0.2, z=zs, W=W) results = camb.get_results(pars) cls2 = results.get_source_cls_dict() self.assertTrue(np.allclose(cls2["W1xW1"][2:1200], cls["W1xW1"][2:1200], rtol=1e-3)) pars.SourceWindows = [GaussianSourceWindow(redshift=1089, source_type='lensing', sigma=30)] results = camb.get_results(pars) cls = results.get_source_cls_dict() PP = cls['PxP'] ls = np.arange(0, PP.shape[0]) self.assertTrue(np.allclose(PP / 4 * (ls * (ls + 1)), cls['W1xW1'], rtol=1e-3)) self.assertTrue(np.allclose(PP / 2 * np.sqrt(ls * (ls + 1)), cls['PxW1'], rtol=1e-3)) # test something sharp with redshift distortions (tricky..) from scipy import signal zs = np.arange(1.9689, 2.1057, (2.1057 - 1.9689) / 2000) W = signal.tukey(len(zs), alpha=0.1) pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122) pars.InitPower.set_params(As=2e-9, ns=0.965) pars.set_for_lmax(4000) pars.SourceWindows = [SplinedSourceWindow(z=zs, W=W, source_type='counts')] pars.SourceTerms.counts_redshift = True results = camb.get_results(pars) cls = results.get_source_cls_dict() self.assertAlmostEqual(np.sum(cls['PxW1'][10:3000:20]), 0.00020001, places=5) self.assertAlmostEqual(np.sum(cls['W1xW1'][10:3000:20]), 2.26348, places=3) self.assertAlmostEqual(np.sum(cls['W1xW1'][10]), 0.0001097, places=6)
def get_galaxy_power(l,z=0.7,b=1.): lmax = np.max([np.max(l),2000]) pars = camb.CAMBparams() pars.set_cosmology(H0=67.5, ombh2=0.022, omch2=0.122) pars.InitPower.set_params(As=2e-9, ns=0.965) pars.set_for_lmax(lmax, lens_potential_accuracy=1) pars.Want_CMB = False pars.NonLinear = model.NonLinear_both pars.SourceWindows = [GaussianSourceWindow(redshift=0.7, source_type='counts', sigma=0.1,dlog10Ndm=-0.2, bias = 1.0)] results = camb.get_results(pars) cls = results.get_source_cls_dict() l_model = np.arange(2,lmax+1) cl_model = cls['W1xW1'][2:lmax+1] # Interpolate this onto the provided grid. f = interp1d(l_model,cl_model,kind='cubic') return f(l)
def testAssigments(self): ini = os.path.join(os.path.dirname(__file__), '..', 'inifiles', 'planck_2018.ini') if os.path.exists(ini): pars = camb.read_ini(ini) self.assertTrue(np.abs(camb.get_background(pars).cosmomc_theta() * 100 / 1.040909 - 1) < 2e-5) pars = camb.CAMBparams() pars.set_cosmology(H0=68.5, ombh2=0.022, mnu=0, omch2=0.1) self.assertAlmostEqual(pars.omegam, (0.022 + 0.1) / 0.685 ** 2) with self.assertRaises(AttributeError): # noinspection PyPropertyAccess pars.omegam = 1 pars.InitPower.set_params(ns=0.01) data = camb.CAMBdata() data.Params = pars self.assertEqual(data.Params.InitPower.ns, pars.InitPower.ns) d = dark_energy.DarkEnergyFluid(w=-0.95) pars.DarkEnergy = d self.assertEqual(pars.DarkEnergy.w, -0.95) pars.DarkEnergy = dark_energy.AxionEffectiveFluid(w_n=0.4) data.Params = pars self.assertEqual(pars.DarkEnergy.w_n, 0.4) pars.z_outputs = [0.1, 0.4] self.assertEqual(pars.z_outputs[1], 0.4) pars.z_outputs[0] = 0.3 self.assertEqual(pars.z_outputs[0], 0.3) pars.z_outputs = pars.z_outputs pars.z_outputs = [] pars.z_outputs = None self.assertFalse(len(pars.z_outputs)) with self.assertRaises(TypeError): pars.DarkEnergy = initialpower.InitialPowerLaw() pars.NonLinear = model.NonLinear_both printstr = str(pars) self.assertTrue('Want_CMB_lensing = True' in printstr and "NonLinear = NonLinear_both" in printstr) pars.NonLinear = model.NonLinear_lens self.assertTrue(pars.NonLinear == model.NonLinear_lens) with self.assertRaises(ValueError): pars.NonLinear = 4 pars.nu_mass_degeneracies = np.zeros(3) self.assertTrue(len(pars.nu_mass_degeneracies) == 3) pars.nu_mass_degeneracies = [1, 2, 3] self.assertTrue(pars.nu_mass_degeneracies[1] == 2) pars.nu_mass_degeneracies[1] = 5 self.assertTrue(pars.nu_mass_degeneracies[1] == 5) with self.assertRaises(CAMBParamRangeError): pars.nu_mass_degeneracies = np.zeros(7) pars.nu_mass_eigenstates = 0 self.assertFalse(len((pars.nu_mass_degeneracies[:1]))) pars = camb.set_params(**{'InitPower.ns': 1.2, 'WantTransfer': True}) self.assertEqual(pars.InitPower.ns, 1.2) self.assertTrue(pars.WantTransfer) pars.DarkEnergy = None from camb.sources import GaussianSourceWindow pars = camb.CAMBparams() pars.SourceWindows = [GaussianSourceWindow(), GaussianSourceWindow(redshift=1)] self.assertEqual(pars.SourceWindows[1].redshift, 1) pars.SourceWindows[0].redshift = 2 self.assertEqual(pars.SourceWindows[0].redshift, 2) self.assertTrue(len(pars.SourceWindows) == 2) pars.SourceWindows[0] = GaussianSourceWindow(redshift=3) self.assertEqual(pars.SourceWindows[0].redshift, 3) self.assertTrue('redshift = 3.0' in str(pars)) pars.SourceWindows = pars.SourceWindows[0:1] self.assertTrue(len(pars.SourceWindows) == 1) pars.SourceWindows = [] self.assertTrue(len(pars.SourceWindows) == 0)
def testAssigments(self): ini = os.path.join(os.path.dirname(__file__), '..', 'inifiles', 'planck_2018.ini') if os.path.exists(ini): pars = camb.read_ini(ini) self.assertTrue( np.abs( camb.get_background(pars).cosmomc_theta() * 100 / 1.040909 - 1) < 2e-5) pars = camb.CAMBparams() pars.set_cosmology(H0=68.5, ombh2=0.022, mnu=0, omch2=0.1) self.assertAlmostEqual(pars.omegam, (0.022 + 0.1) / 0.685**2) with self.assertRaises(AttributeError): # noinspection PyPropertyAccess pars.omegam = 1 pars.InitPower.set_params(ns=0.01) data = camb.CAMBdata() data.Params = pars self.assertEqual(data.Params.InitPower.ns, pars.InitPower.ns) d = dark_energy.DarkEnergyFluid(w=-0.95) pars.DarkEnergy = d self.assertEqual(pars.DarkEnergy.w, -0.95) pars.DarkEnergy = dark_energy.AxionEffectiveFluid(w_n=0.4) data.Params = pars self.assertEqual(pars.DarkEnergy.w_n, 0.4) pars.z_outputs = [0.1, 0.4] self.assertEqual(pars.z_outputs[1], 0.4) pars.z_outputs[0] = 0.3 self.assertEqual(pars.z_outputs[0], 0.3) pars.z_outputs = pars.z_outputs pars.z_outputs = [] pars.z_outputs = None # noinspection PyTypeChecker self.assertFalse(len(pars.z_outputs)) with self.assertRaises(TypeError): pars.DarkEnergy = initialpower.InitialPowerLaw() pars.NonLinear = model.NonLinear_both printstr = str(pars) self.assertTrue('Want_CMB_lensing = True' in printstr and "NonLinear = NonLinear_both" in printstr) pars.NonLinear = model.NonLinear_lens self.assertTrue(pars.NonLinear == model.NonLinear_lens) with self.assertRaises(ValueError): pars.NonLinear = 4 pars.nu_mass_degeneracies = np.zeros(3) self.assertTrue(len(pars.nu_mass_degeneracies) == 3) pars.nu_mass_degeneracies = [1, 2, 3] self.assertTrue(pars.nu_mass_degeneracies[1] == 2) pars.nu_mass_degeneracies[1] = 5 self.assertTrue(pars.nu_mass_degeneracies[1] == 5) with self.assertRaises(CAMBParamRangeError): pars.nu_mass_degeneracies = np.zeros(7) pars.nu_mass_eigenstates = 0 self.assertFalse(len((pars.nu_mass_degeneracies[:1]))) pars = camb.set_params(**{'InitPower.ns': 1.2, 'WantTransfer': True}) self.assertEqual(pars.InitPower.ns, 1.2) self.assertTrue(pars.WantTransfer) pars.DarkEnergy = None pars = camb.set_params(**{ 'H0': 67, 'ombh2': 0.002, 'r': 0.1, 'Accuracy.AccurateBB': True }) self.assertEqual(pars.Accuracy.AccurateBB, True) from camb.sources import GaussianSourceWindow pars = camb.CAMBparams() pars.SourceWindows = [ GaussianSourceWindow(), GaussianSourceWindow(redshift=1) ] self.assertEqual(pars.SourceWindows[1].redshift, 1) pars.SourceWindows[0].redshift = 2 self.assertEqual(pars.SourceWindows[0].redshift, 2) self.assertTrue(len(pars.SourceWindows) == 2) pars.SourceWindows[0] = GaussianSourceWindow(redshift=3) self.assertEqual(pars.SourceWindows[0].redshift, 3) self.assertTrue('redshift = 3.0' in str(pars)) pars.SourceWindows = pars.SourceWindows[0:1] self.assertTrue(len(pars.SourceWindows) == 1) pars.SourceWindows = [] self.assertTrue(len(pars.SourceWindows) == 0) params = camb.get_valid_numerical_params() self.assertEqual( params, { 'ombh2', 'deltazrei', 'omnuh2', 'tau', 'omk', 'zrei', 'thetastar', 'nrunrun', 'meffsterile', 'nnu', 'ntrun', 'HMCode_A_baryon', 'HMCode_eta_baryon', 'HMCode_logT_AGN', 'cosmomc_theta', 'YHe', 'wa', 'cs2', 'H0', 'mnu', 'Alens', 'TCMB', 'ns', 'nrun', 'As', 'nt', 'r', 'w', 'omch2' }) params2 = camb.get_valid_numerical_params( dark_energy_model='AxionEffectiveFluid') self.assertEqual(params2.difference(params), {'fde_zc', 'w_n', 'zc', 'theta_i'})