def test_classy(): ''' Test a default astropy cosmology ''' from skypy.power_spectrum import classy Pl15massless = Planck15.clone(name='Planck 15 massless neutrino', m_nu=[0., 0., 0.]*u.eV) redshift = [0.0, 1.0] wavenumber = np.logspace(-4.0, np.log10(2.0), 200) pzk = classy(wavenumber, redshift, Pl15massless) assert pzk.shape == (len(redshift), len(wavenumber)) assert allclose(pzk, test_pzk, rtol=1.e-4) # also check redshifts are ordered correctly redshift = [1.0, 0.0] pzk = classy(wavenumber, redshift, Pl15massless) assert pzk.shape == (len(redshift), len(wavenumber)) assert allclose(pzk, test_pzk[np.argsort(redshift)], rtol=1.e-4) # also check scalar arguments are treated correctly redshift = 1.0 wavenumber = 1.e-1 pzk = classy(wavenumber, redshift, Pl15massless) assert np.isscalar(pzk)
def __init__(self,cosmo_params=cosmo_fid,pk_params=pk_params,cosmo=cosmo,cosmo_h=None): self.cosmo_params=cosmo_params self.pk_params=pk_params self.cosmo=cosmo if not cosmo_h: self.cosmo_h=cosmo.clone(H0=100) else: self.cosmo_h=cosmo_h
def __init__(self,cosmo_params=cosmo_fid,pk_params=pk_params,cosmo=cosmo,cosmo_h=None): self.cosmo_params=cosmo_params self.pk_params=pk_params self.cosmo=cosmo if not cosmo_h: self.cosmo_h=cosmo.clone(H0=100) else: self.cosmo_h=cosmo_h if not pk_params is None: self.kh=np.logspace(np.log10(pk_params['kmin']),np.log10(pk_params['kmax']), pk_params['nk'])
def __init__(self,cosmo_params=cosmo_fid,pk_params=pk_params,cosmo=cosmo, silence_camb=False,pk_func=None,SSV_cov=False,scenario=None, logger=None): self.logger=logger self.cosmo_params=cosmo_params self.pk_params=pk_params self.cosmo=cosmo self.silence_camb=silence_camb self.cosmo_h=cosmo.clone(H0=100) #self.pk_func=self.camb_pk_too_many_z if pk_func is None else pk_func #self.pk_func=self.ccl_pk if pk_func is None else pk_func self.pk_func=self.class_pk if pk_func is None else getattr(self,pk_func) self.SSV_cov=SSV_cov self.scenario = scenario self.pk=None if not pk_params is None: self.kh=np.logspace(np.log10(pk_params['kmin']),np.log10(pk_params['kmax']), pk_params['nk'])
def test_dk_deta(): (cnv.dk_deta(10) * 1 / units.MHz).to("littleh/Mpc") cosmo = Planck15.clone(H0=Planck15.H0 / 1.1) assert cnv.dk_deta(10, cosmo) > cnv.dk_deta(10)
def test_dk_du(): cnv.dk_du(10).to("littleh/Mpc") cosmo = Planck15.clone(H0=Planck15.H0 / 1.1) assert cnv.dk_du(10, cosmo) > cnv.dk_du(10)
def test_dl_df(): (cnv.dL_df(10) * 1 * units.MHz).to("Mpc/littleh") cosmo = Planck15.clone(H0=Planck15.H0 / 1.1) assert cnv.dL_df(10, cosmo) < cnv.dL_df(10)
def test_dL_dth(): (cnv.dL_dth(10) * units.rad).to("Mpc/littleh") cosmo = Planck15.clone(H0=Planck15.H0 / 1.1) assert cnv.dL_dth(10, cosmo) < cnv.dL_dth(10)
""" A collection of functions that are used to generate mock survey data (galaxy, shear and CMB samples). Functions for LSST, DESI, DES, KiDS and CMB surveys. """ from scipy.stats import norm as gaussian import copy import numpy as np #from lensing_utils import * from skylens.tracer_utils import * from astropy.cosmology import Planck15 as cosmo from astropy.table import Table cosmo_h_PL = cosmo.clone(H0=100) from dask.distributed import Client, get_client import healpy as hp import sys sys.path.append('./ForQuE/') #from cmb import * #from cmb_lensing_rec import * cosmo_h = cosmo.clone(H0=100) d2r = np.pi / 180. nz_F = 3600. / d2r**2 #convert nz from arcmin^2 to rd ^2 def galaxy_shot_noise_calc(zg1=None, zg2=None): SN = np.sum(zg1['W'] * zg2['W'] * zg1['nz'] * nz_F) #FIXME: Check this #Assumption: ns(z)=ns*pzg*dzg SN /= np.sum(zg1['nz'] * nz_F * zg1['W'])
def cosmo(self): """Return an astropy cosmology object for this cosmology.""" return Planck15.clone(H0=self.hlittle * 100, Om0=self.OMm, Ob0=self.OMb)
def fit_nbody(cosmo, state, stages, nc, pm_nc_factor=1, name="NBody"): """ Integrate the evolution of the state across the givent stages Parameters: ----------- cosmo: cosmology Cosmological parameter object state: tensor (3, batch_size, npart, 3) Input state stages: array Array of scale factors nc: int, or list of ints Number of cells pm_nc_factor: int Upsampling factor for computing pgdparams: array list of pgdparameters [alpha, kl, ks] of size len(stages) - 1 Returns ------- state: tensor (3, batch_size, npart, 3), or list of states Integrated state to final condition, or list of intermediate steps """ with tf.name_scope(name): state = tf.convert_to_tensor(state, name="state") # Create a simple Planck15 cosmology without neutrinos, and makes sure sigma8 # is matched nbdykit_cosmo = Cosmology.from_astropy(Planck15.clone(m_nu=0 * u.eV)) nbdykit_cosmo = nbdykit_cosmo.match(sigma8=cosmo.sigma8.numpy()) if isinstance(nc, int): nc = [nc, nc, nc] # Unrolling leapfrog integration to make tf Autograph happy if len(stages) == 0: return state ai = stages[0] # first force calculation for jump starting state = tfpm.force(cosmo, state, nc, pm_nc_factor=pm_nc_factor) k, _ = flowpm.power_spectrum(tf.zeros([1] + [FLAGS.nc] * 3), boxsize=np.array([FLAGS.box_size] * 3), kmin=np.pi / FLAGS.box_size, dk=2 * np.pi / FLAGS.box_size) params = tf.Variable([FLAGS.alpha0, FLAGS.kl0, FLAGS.ks0], dtype=tf.float32) optimizer = tf.keras.optimizers.Adam(learning_rate=FLAGS.learning_rate) x, p, f = ai, ai, ai pgdparams = [] scale_factors = [] # Loop through the stages for i in range(len(stages) - 1): a0 = stages[i] a1 = stages[i + 1] ah = (a0 * a1)**0.5 # Kick step state = tfpm.kick(cosmo, state, p, f, ah) p = ah # Drift step state = tfpm.drift(cosmo, state, x, p, a1) # Let's compute the target power spectrum at that scale factor target_pk = HalofitPower(nbdykit_cosmo, 1. / a1 - 1.)(k).astype('float32') for j in range(FLAGS.niter if i == 0 else FLAGS.niter_refine): optimizer.minimize(partial(pgd_loss, params, state, target_pk), params) if j % 10 == 0: loss, pk = pgd_loss(params, state, target_pk, return_pk=True) if j == 0: pk0 = pk print("step %d, loss:" % j, loss) pgdparams.append(params.numpy()) scale_factors.append(a1) print("Sim step %d, fitted params (alpha, kl, ks)" % i, pgdparams[-1]) plt.loglog(k, target_pk, "k") plt.loglog(k, pk0, ':', label='starting') plt.loglog(k, pk, '--', label='after n steps') plt.grid(which='both') plt.savefig('PGD_fit_%0.2f.png' % a1) plt.close() # Optional PGD correction step state = tfpm.pgd(state, params, nc, pm_nc_factor=pm_nc_factor) x = a1 # Force state = tfpm.force(cosmo, state, nc, pm_nc_factor=pm_nc_factor) f = a1 # Kick again state = tfpm.kick(cosmo, state, p, f, a1) p = a1 return state, scale_factors, pgdparams
def cosmology(self): return Planck15.clone(h=self.hlittle, Om0=self.OMm, Ob0=self.OMb)
args = parser.parse_args() settings = yaml.load(open(args.config, 'rb')) correlations = args.process print('%d correlations to process: ' % len(correlations), correlations) nc = len(correlations) # Decant into dictionaries cosmo_fid = {} for name in settings['cosmology'].keys(): cosmo_fid[name] = settings['cosmology'][name] print(name, cosmo_fid[name]) pk_params = settings['general'] cosmo = Planck15.clone() #cosmo_fid=dict({'h':cosmo.h,'Omb':cosmo.Ob0,'Omd':cosmo.Om0-cosmo.Ob0,'s8':0.817,'Om':cosmo.Om0,'As':2.12e-09,'mnu':cosmo.m_nu[-1].value,'Omk':cosmo.Ok0,'tau':0.06,'ns':0.965,'w':-1,'wa':0}) # Generate the matter power spectrum # using CCL rather than class, just because this works on my laptop if (args.redshift == -1.0): zlim = settings['general']['redshift'] else: zlim = args.redshift print('Generating theory for z=%3.3f' % zlim) #PS=Power_Spectra(cosmo_params=cosmo_fid,pk_params=pk_params) z = [zlim, zlim + 0.001] #pk,kh =PS.class_pk(z=z) #pk,kh = PS.ccl_pk(z=z)
m_plot = np.linspace(np.amin(mock['M']), np.amax(mock['M']), 10) plt.plot(m_plot, 0.21 - 0.03 * m_plot, ls='--', color='red') plt.tight_layout(pad=0.3) plt.savefig('mabs_vs_gr.pdf') plt.savefig('mabs_vs_gr.png', dpi=300) plt.close() # %% mock = mock[mock['g-r'] > 0.21 - 0.03 * mock['M']] # %% ngal_cut_list = [5e-4, 1e-3, 2e-3] cosmology = Planck15.clone(H0=100) boxsize = 2000 redshift = 0.19 n_threads = 4 def xi_from_dd(dd, n, boxsize, s_bins, mu_bins): rr = (n**2 / boxsize**3 * 4 * np.pi / 3 * np.diff(s_bins**3) / (len(mu_bins) - 1)) return (dd.reshape((len(s_bins) - 1, len(mu_bins) - 1)) / rr[:, np.newaxis] - 1) for ngal in xolmis.NGAL: table = Table()
import pyccl import math import numpy as np from scipy.interpolate import interp1d from astropy.cosmology import Planck15 as cosmo from astropy.constants import c,G from astropy import units as u from scipy.integrate import quad as scipy_int1d c=c.to(u.km/u.second) cosmo_fid=dict({'h':0.7,'s8':0.8,'Omc':0.3,'Og':0.0,'Omb':0.0,'Om':0.3,'m_nu':[0,0,0], 'Neff':3.046,'Omk':0,'tau':0.06,'ns':0.96, 'Tcmb0':0}) # Reset astropy cosmology parameters to cosmo_fid. This is assuming flat LCDM and hence Omega_k=0 cosmo=cosmo.clone(H0=cosmo_fid['h']*100,Ob0=cosmo_fid['Omb'],Om0=cosmo_fid['Om'], m_nu=cosmo_fid['m_nu']*u.eV,Neff=cosmo_fid['Neff'],Tcmb0=cosmo_fid['Tcmb0']) cosmo_h=cosmo.clone(H0=100) pk_params={'non_linear':0,'kmax':1E3,'kmin':5.e-5,'nk':5000} class Power_Spectra(): def __init__(self,cosmo_params=cosmo_fid,pk_params=pk_params,cosmo=cosmo,cosmo_h=None): self.cosmo_params=cosmo_params self.pk_params=pk_params self.cosmo=cosmo if not cosmo_h: self.cosmo_h=cosmo.clone(H0=100) else: self.cosmo_h=cosmo_h
def test_X2Y(): cnv.X2Y(10).to("Mpc^3 / (littleh^3 sr GHz)") cosmo = Planck15.clone(H0=Planck15.H0 / 1.1) assert cnv.X2Y(10, cosmo) < cnv.X2Y(10)
import contextlib import logging from astropy.cosmology import Planck15 from os import path from pathlib import Path from ._utils import StructInstanceWrapper, StructWithDefaults from .c_21cmfast import ffi, lib logger = logging.getLogger("21cmFAST") # Cosmology is from https://arxiv.org/pdf/1807.06209.pdf # Table 2, last column. [TT,TE,EE+lowE+lensing+BAO] Planck18 = Planck15.clone( Om0=(0.02242 + 0.11933) / 0.6766**2, Ob0=0.02242 / 0.6766**2, H0=67.66, ) class GlobalParams(StructInstanceWrapper): """ Global parameters for 21cmFAST. This is a thin wrapper over an allocated C struct, containing parameter values which are used throughout various computations within 21cmFAST. It is a singleton; that is, a single python (and C) object exists, and no others should be created. This object is not "passed around", rather its values are accessed throughout the code. Parameters in this struct are considered to be options that should usually not have
from camb import model, initialpower #import pyccl import os,sys from classy import Class sys.path.insert(0,'./') import numpy as np from scipy.interpolate import interp1d from astropy.cosmology import Planck15 as cosmo from astropy import units as u from scipy.integrate import quad as scipy_int1d import pandas as pd from scipy import interpolate cosmo_h=cosmo.clone(H0=100) #c=c.to(u.km/u.second) cosmo_fid=dict({'h':cosmo.h,'Omb':cosmo.Ob0,'Omd':cosmo.Om0-cosmo.Ob0,'s8':0.817,'Om':cosmo.Om0, 'Ase9':2.2,'mnu':cosmo.m_nu[-1].value,'Omk':cosmo.Ok0,'tau':0.06,'ns':0.965, 'w':-1,'wa':0}) cosmo_fid['Oml']=1.-cosmo_fid['Om']-cosmo_fid['Omk'] pk_params={'non_linear':1,'kmax':30,'kmin':3.e-4,'nk':5000,'scenario':'eagle'} # baryonic scenario option: # "owls_AGN","owls_DBLIMFV1618","owls_NOSN","owls_NOSN_NOZCOOL","owls_NOZCOOL","owls_REF","owls_WDENS" # "owls_WML1V848","owls_WML4","illustris","mb2","eagle","HzAGN" Bins_z_HzAGN = np.array([4.9285,4.249,3.7384,3.33445, 3.00295,1.96615,1.02715,0.519195,0.22878,0.017865,0.0])
@author: Denise """ import numpy as np import flowpm from flowpm.tfbackground import dchioverda, rad_comoving_distance, a_of_chi as a_of_chi_tf, transverse_comoving_distance as trans_comoving_distance, angular_diameter_distance as ang_diameter_distance from numpy.testing import assert_allclose from scipy import interpolate from nbodykit.cosmology import Cosmology from astropy.cosmology import Planck15 import astropy.units as u # Create a simple Planck15 cosmology without neutrinos, and makes sure sigma8 # is matched ref_cosmo = Cosmology.from_astropy(Planck15.clone(m_nu=0 * u.eV)) ref_cosmo = ref_cosmo.match(sigma8=flowpm.cosmology.Planck15().sigma8.numpy()) def test_radial_comoving_distance(): """ This function tests the function computing the radial comoving distance. """ cosmo_tf = flowpm.cosmology.Planck15() a = np.logspace(-2, 0.0) z = 1 / a - 1 radial = rad_comoving_distance(cosmo_tf, a) radial_astr = ref_cosmo.comoving_distance(z)
def main(_): cosmology = flowpm.cosmology.Planck15() # Create a simple Planck15 cosmology without neutrinos, and makes sure sigma8 # is matched nbdykit_cosmo = Cosmology.from_astropy(Planck15.clone(m_nu=0 * u.eV)) nbdykit_cosmo = nbdykit_cosmo.match(sigma8=cosmology.sigma8.numpy()) # Compute the k vectora that will be needed in the PGD fit k, _ = flowpm.power_spectrum(tf.zeros([1] + [FLAGS.nc] * 3), boxsize=np.array([FLAGS.box_size] * 3), kmin=np.pi / FLAGS.box_size, dk=2 * np.pi / FLAGS.box_size) # Create some initial conditions klin = tf.constant(np.logspace(-4, 1, 512), dtype=tf.float32) pk = linear_matter_power(cosmology, klin) pk_fun = lambda x: tf.cast( tf.reshape( interpolate.interp_tf(tf.reshape(tf.cast(x, tf.float32), [-1]), klin, pk), x.shape), tf.complex64) initial_conditions = flowpm.linear_field( [FLAGS.nc, FLAGS.nc, FLAGS.nc], [FLAGS.box_size, FLAGS.box_size, FLAGS.box_size], pk_fun, batch_size=FLAGS.batch_size) initial_state = flowpm.lpt_init(cosmology, initial_conditions, FLAGS.a_init) stages = np.linspace(FLAGS.a_init, 1., FLAGS.nsteps, endpoint=True) print('Starting simulation') # Run the Nbody states = flowpm.nbody(cosmology, initial_state, stages, [FLAGS.nc, FLAGS.nc, FLAGS.nc], pm_nc_factor=FLAGS.B, return_intermediate_states=True) print('Simulation done') # Initialize PGD params alpha = tf.Variable([FLAGS.alpha0], dtype=tf.float32) scales = tf.Variable([FLAGS.kl0, FLAGS.ks0], dtype=tf.float32) optimizer = tf.keras.optimizers.Adam(learning_rate=FLAGS.learning_rate) params = [] scale_factors = [] # We begin by fitting the last time step for j, (a, state) in enumerate(states[::-1]): # Let's compute the target power spectrum at that scale factor target_pk = HalofitPower(nbdykit_cosmo, 1. / a - 1.)(k).astype('float32') for i in range(FLAGS.niter if j == 0 else FLAGS.niter_refine): optimizer.minimize( partial(pgd_loss, alpha, scales, state, target_pk), [alpha] if (FLAGS.fix_scales and j > 0) else [alpha, scales]) if i % 10 == 0: loss, pk = pgd_loss(alpha, scales, state, target_pk, return_pk=True) if i == 0: pk0 = pk print("step %d, loss:" % i, loss) params.append(np.concatenate([alpha.numpy(), scales.numpy()])) scale_factors.append(a) print("Fitted params (alpha, kl, ks)", params[-1]) plt.loglog(k, target_pk, "k") plt.loglog(k, pk0, ':', label='starting') plt.loglog(k, pk, '--', label='after n steps') plt.grid(which='both') plt.savefig('PGD_fit_%0.2f.png' % a) plt.close() pickle.dump( { 'B': FLAGS.B, 'nsteps': FLAGS.nsteps, 'params': params, 'scale_factors': scale_factors, 'cosmology': cosmology.to_dict(), 'boxsize': FLAGS.box_size, 'nc': FLAGS.nc }, open(FLAGS.filename, "wb"))