def get_cl_ccl(pars, ell_bp): cosmo = get_cosmo_ccl(pars) clust = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(z, pz), bias=(z, bz)) lens = ccl.WeakLensingTracer(cosmo, dndz=(z, pz)) ell = np.arange(nell) cl0 = np.zeros(nell) * 0. cls = np.zeros([3, 3, nell]) cls[0, 0, :] = ccl.angular_cl(cosmo, clust, clust, ell) cls[0, 1, :] = ccl.angular_cl(cosmo, clust, lens, ell) cls[0, 2, :] = cl0 cls[1, 0, :] = cls[0, 1, :] cls[1, 1, :] = ccl.angular_cl(cosmo, lens, lens, ell) cls[1, 2, :] = cl0 cls[2, 0, :] = cls[0, 2, :] cls[2, 1, :] = cls[1, 2, :] cls[2, 2, :] = cl0 cl_flat = np.concatenate( [cls[0, 0, ell_bp], cls[0, 1, ell_bp], cls[1, 1, ell_bp]]) return cl_flat
def get_cl(dtype): config = get_config(dtype) cosmo_pars = config['cosmo'] cosmo = ccl.Cosmology(**cosmo_pars) if config['dtype'] == 'generic': return np.ones(3 * config['nside']) if config['dtype'] == 'galaxy_density': z, nz = np.loadtxt('xcell/tests/data/DESY1gc_dndz_bin0.txt', usecols=(1, 3), unpack=True) b = np.ones_like(z) tracer = ccl.NumberCountsTracer(cosmo, dndz=(z, nz), bias=(z, b), has_rsd=None) elif config['dtype'] == 'galaxy_shear': z, nz = np.loadtxt('xcell/tests/data/Nz_DIR_z0.1t0.3.asc', usecols=(0, 1), unpack=True) tracer = ccl.WeakLensingTracer(cosmo, dndz=(z, nz)) elif config['dtype'] == 'cmb_convergence': tracer = ccl.CMBLensingTracer(cosmo, z_source=1100) elif config['dtype'] == 'cmb_tSZ': tracer = ccl.tSZTracer(cosmo, z_max=3.) cl = ccl.angular_cl(cosmo, tracer, tracer, np.arange(3 * config['nside'])) return cl
def evaluate_cl_ij(self, f, cosmo): # galaxy tracer tracer = ccl.NumberCountsTracer( cosmo, has_rsd=self.cl_params['has_rsd'], dndz=(self.z_s, self.nz_s), bias=(self.z_s, self.bz_s), mag_bias=self.cl_params['has_magnification']) #tracer = ccl.WeakLensingTracer(cosmo, dndz=(self.z_s, self.nz_s), has_shear=True) ks = self.power_ij['ks'] pk_a = np.zeros((len(self.a_s), len(self.power_ij['ks']))) for k in range(len(self.a_s)): pk = np.zeros(len(ks)) for i in range(len(self.fields)): for j in range(len(self.fields)): if i > j: continue p_this = (f.flatten()[i] * f.flatten()[j] * np.array([ self.power_ij[k][r'$(' + self.fields[i] + ',' + self.fields[j] + r')$'] ])).flatten() pk += p_this if i != j: pk += p_this pk_a[k, :] = pk ells, cl = project_Cl(cosmo, tracer, pk_a, ks, self.a_s) cl = np.interp(self.x, ells, cl) return cl
def init_cl_ij(self, cosmo): """ Compute theoretical prediction for the angular power spectra templates """ # galaxy tracer tracer = ccl.NumberCountsTracer( cosmo, has_rsd=self.cl_params['has_rsd'], dndz=(self.z_s, self.nz_s), bias=(self.z_s, self.b_s), mag_bias=self.cl_params['has_magnification']) #tracer = ccl.WeakLensingTracer(cosmo, dndz=(self.z_s, self.nz_s), has_shear=True) # templates as a function of redshift Pk_tmps_a = self.power_ij ks = Pk_tmps_a['ks'] # obtain the Cl_ij templates Cl_ij = np.zeros((len(self.fields), len(self.fields), len(self.x))) for i in range(len(self.fields)): for j in range(len(self.fields)): if i > j: continue Pk_a_s = np.array([ Pk_tmps_a[k][r'$(' + self.fields[i] + ',' + self.fields[j] + r')$'] for k in range(len(self.a_s)) ]) ells, Cl_tmp = project_Cl(cosmo, tracer, Pk_a_s, ks, self.a_s) Cl_ij[i, j, :] = np.interp(self.x, ells, Cl_tmp) if i != j: Cl_ij[j, i, :] = np.interp(self.x, ells, Cl_tmp) self.Cl_ij = Cl_ij
def get_tracer(tracer_type, cosmo=None, **tracer_kwargs): if cosmo is None: cosmo = COSMO z = np.linspace(0., 1., 2000) n = dndz(z) b = np.sqrt(1. + z) if tracer_type == 'nc': ntr = 3 tr = ccl.NumberCountsTracer(cosmo, True, dndz=(z, n), bias=(z, b), mag_bias=(z, b), **tracer_kwargs) elif tracer_type == 'wl': ntr = 2 tr = ccl.WeakLensingTracer(cosmo, dndz=(z, n), ia_bias=(z, b), **tracer_kwargs) elif tracer_type == 'cl': ntr = 1 tr = ccl.CMBLensingTracer(cosmo, 1100., **tracer_kwargs) else: ntr = 0 tr = ccl.Tracer(**tracer_kwargs) return tr, ntr
def getcorrCCL(theta, data, centers): Nz, be = np.histogram(data['z'], bins=8, range=(0.05, 0.15)) z = 0.5 * (be[1:] + be[:-1]) h = 0.675 # Planck value for h (Hubble parameter) Ob = 0.044 # Planck value for Omega_b (Baryon energy density) Om = theta[1] # Planck value for Omega_m (Matter energy density) Oc = Om - Ob # Value for Omega_c (Cold dark matter energy density) ns = 0.965 # Scalar index cosmo = ccl.Cosmology(Omega_c=Oc, Omega_b=Ob, h=h, sigma8=0.8, n_s=ns, matter_power_spectrum='linear') tracer = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(z, Nz), bias=(z, np.ones_like(z))) ell = np.arange(1, 7500) # is this the same as lmax? angular_power_spectrum = ccl.angular_cl(cosmo, tracer, tracer, ell) th = centers #np.linspace(0,0.2, num = 15) ang_corr_func = ccl.correlation(cosmo, ell, angular_power_spectrum, th) return ang_corr_func
def test_tracer_nz_support(): z_max = 1.0 a = np.linspace(1 / (1 + z_max), 1.0, 100) background_def = { "a": a, "chi": ccl.comoving_radial_distance(COSMO, a), "h_over_h0": ccl.h_over_h0(COSMO, a) } calculator_cosmo = ccl.CosmologyCalculator(Omega_c=0.27, Omega_b=0.045, h=0.67, sigma8=0.8, n_s=0.96, background=background_def) z = np.linspace(0., 2., 2000) n = dndz(z) with pytest.raises(ValueError): _ = ccl.WeakLensingTracer(calculator_cosmo, (z, n)) with pytest.raises(ValueError): _ = ccl.NumberCountsTracer(calculator_cosmo, has_rsd=False, dndz=(z, n), bias=(z, np.ones_like(z))) with pytest.raises(ValueError): _ = ccl.CMBLensingTracer(calculator_cosmo, z_source=2.0)
def test_clfid_halomod(tr1, tr2): data = get_config(dtype0=tr1, dtype1=tr2, inc_hm=True) cosmo = ccl.Cosmology(**data['cov']['fiducial']['cosmo']) md = ccl.halos.MassDef200m() mf = ccl.halos.MassFuncTinker10(cosmo, mass_def=md) hb = ccl.halos.HaloBiasTinker10(cosmo, mass_def=md) cm = ccl.halos.ConcentrationDuffy08(mdef=md) hmc = ccl.halos.HMCalculator(cosmo, mf, hb, md) pNFW = ccl.halos.HaloProfileNFW(cm) profs = {} ccltr = {} normed = {} for tr, lab in [(tr1, 'Dummy__0'), (tr2, 'Dummy__1')]: if tr == 'galaxy_density': data['tracers'][lab]['hod_params'] = {'lMmin_0': 12.1, 'lM1_p': 0.1, 'bg_0': 1.2} profs[tr] = ccl.halos.HaloProfileHOD(cm, lMmin_0=12.1, lM1_p=0.1, bg_0=1.2) z, nz = np.loadtxt('xcell/tests/data/DESY1gc_dndz_bin0.txt', usecols=(1, 3), unpack=True) ccltr[tr] = ccl.NumberCountsTracer(cosmo, False, dndz=(z, nz), bias=(z, np.ones_like(z))) normed[tr] = True elif tr == 'cmb_tSZ': data['tracers'][lab]['gnfw_params'] = {'mass_bias': 0.9} profs[tr] = ccl.halos.HaloProfilePressureGNFW(mass_bias=0.9) ccltr[tr] = ccl.tSZTracer(cosmo, z_max=3.) normed[tr] = False elif tr == 'galaxy_shear': profs[tr] = pNFW z, nz = np.loadtxt('xcell/tests/data/Nz_DIR_z0.1t0.3.asc', usecols=(0, 1), unpack=True) ccltr[tr] = ccl.WeakLensingTracer(cosmo, dndz=(z, nz)) normed[tr] = True elif tr == 'cmb_convergence': profs[tr] = pNFW ccltr[tr] = ccl.CMBLensingTracer(cosmo, z_source=1100.) normed[tr] = True clf = ClFid(data, 'Dummy__0', 'Dummy__1') d = clf.get_cl_file() shutil.rmtree(tmpdir1) k_arr = np.geomspace(1E-4, 1E2, 512) a_arr = 1./(1+np.linspace(0, 3, 15)[::-1]) pk = ccl.halos.halomod_Pk2D(cosmo, hmc, profs[tr1], prof2=profs[tr2], normprof1=normed[tr1], normprof2=normed[tr2], lk_arr=np.log(k_arr), a_arr=a_arr) # Commented out until these features are pushed to the pip release of CCL # smooth_transition=(lambda a: 0.7), # supress_1h=(lambda a: 0.01)) clb = ccl.angular_cl(cosmo, ccltr[tr1], ccltr[tr2], d['ell'], p_of_k_a=pk) assert np.all(np.fabs(clb[2:]/d['cl'][0][2:]-1) < 1E-4)
def get_tracer(self, cosmo, bias, z_max=0.15, nz=1024): zarr = np.linspace(0.000001, z_max, nz) bzarr = np.ones(nz) * bias nzarr = self.get_nz(zarr, cosmo) return ccl.NumberCountsTracer(cosmo, False, dndz=(zarr, nzarr), bias=(zarr, bzarr))
def _get_tracer(self, cosmo): if self.kind == 'g': t = ccl.NumberCountsTracer(cosmo, False, (self.z, self.nz), (self.z, self.bz)) elif self.kind == 'k': t = ccl.CMBLensingTracer(cosmo, z_source=1100.) else: raise ValueError("'kind' must be 'g' or 'k'") return t
def get_ccl_tracer(self, cosmo): _, z, _, nz = np.loadtxt(self.get_path(self.config_h['dndz']), unpack=True) if self.type == 'gc': bz = np.ones_like(z) * self.config_h['bias'] tr = ccl.NumberCountsTracer(cosmo, False, (z, nz), (z, bz)) elif self.type == 'sh': tr = ccl.WeakLensingTracer(cosmo, (z, nz)) return tr
def test_cls_smoke(p_of_k_a): # make a set of tracers to test with z = np.linspace(0., 1., 200) n = np.exp(-((z - 0.5) / 0.1)**2) b = np.sqrt(1. + z) lens1 = ccl.WeakLensingTracer(COSMO, (z, n)) lens2 = ccl.WeakLensingTracer(COSMO, dndz=(z, n), ia_bias=(z, n)) nc1 = ccl.NumberCountsTracer(COSMO, False, dndz=(z, n), bias=(z, b)) nc2 = ccl.NumberCountsTracer(COSMO, True, dndz=(z, n), bias=(z, b)) nc3 = ccl.NumberCountsTracer(COSMO, True, dndz=(z, n), bias=(z, b), mag_bias=(z, b)) cmbl = ccl.CMBLensingTracer(COSMO, 1100.) tracers = [lens1, lens2, nc1, nc2, nc3, cmbl] ell_scl = 4. ell_int = 4 ell_lst = [2, 3, 4, 5] ell_arr = np.arange(2, 5) ells = [ell_int, ell_scl, ell_lst, ell_arr] for i in range(len(tracers)): for j in range(i, len(tracers)): for ell in ells: corr = ccl.angular_cl(COSMO, tracers[i], tracers[j], ell, p_of_k_a=p_of_k_a) assert np.all(np.isfinite(corr)) assert np.shape(corr) == np.shape(ell) # reversing should be fine corr_rev = ccl.angular_cl(COSMO, tracers[j], tracers[i], ell, p_of_k_a=p_of_k_a) assert np.allclose(corr, corr_rev) # Check invalid dndz with assert_raises(ValueError): ccl.NumberCountsTracer(COSMO, False, dndz=z, bias=(z, b)) with assert_raises(ValueError): ccl.NumberCountsTracer(COSMO, False, dndz=(z, n, n), bias=(z, b)) with assert_raises(ValueError): ccl.NumberCountsTracer(COSMO, False, dndz=(z, ), bias=(z, b)) with assert_raises(ValueError): ccl.NumberCountsTracer(COSMO, False, dndz=(1, 2), bias=(z, b)) with assert_raises(ValueError): ccl.WeakLensingTracer(COSMO, dndz=z) with assert_raises(ValueError): ccl.WeakLensingTracer(COSMO, dndz=(z, n, n)) with assert_raises(ValueError): ccl.WeakLensingTracer(COSMO, dndz=(z, )) with assert_raises(ValueError): ccl.WeakLensingTracer(COSMO, dndz=(1, 2))
def compute_tracer_ccl(self, tr): tracer = self.data['tracers'][tr] fiducial = self.data['cov']['fiducial'] # Get Tracers if tracer['type'] == 'gc': # Import z, pz z, pz = np.loadtxt(tracer['dndz'], usecols=tracer['dndz_cols'], unpack=True) # Calculate z bias dz = 0 z_dz = z - dz # Set to 0 points where z_dz < 0: sel = z_dz >= 0 z_dz = z_dz[sel] pz = pz[sel] # Calculate bias bias = None if fiducial['gc_bias'] is True: bias = (z, tracer['bias'] * np.ones_like(z)) # Get tracer return ccl.NumberCountsTracer(self.cosmo, has_rsd=False, dndz=(z_dz, pz), bias=bias) elif tracer['type'] == 'wl': # Import z, pz z, pz = np.loadtxt(tracer['dndz'], usecols=tracer['dndz_cols'], unpack=True) # Calculate z bias dz = 0 z_dz = z - dz # Set to 0 points where z_dz < 0: sel = z_dz >= 0 z_dz = z_dz[sel] pz = pz[sel] # # Calculate bias IA ia_bias = None if fiducial['wl_ia']: A, eta, z0 = fiducial[ 'wl_ia'] # TODO: Improve this in yml file bz = A * ( (1. + z) / (1. + z0) )**eta * 0.0139 / 0.013872474 # pyccl2 -> has already the factor inside. Only needed bz ia_bias = (z, bz) # Get tracer return ccl.WeakLensingTracer(self.cosmo, dndz=(z_dz, pz), ia_bias=ia_bias) elif tracer['type'] == 'cv': return ccl.CMBLensingTracer(self.cosmo, z_source=1100) #TODO: correct z_source else: raise ValueError( 'Type of tracer not recognized. It can be gc, wl or cv!')
def compute_theory_c_ell(self, cosmo, nz, sacc_data): # Turn the nz into CCL Tracer objects # Use the cosmology object to calculate C_ell values import pyccl as ccl import sacc CEE = sacc.standard_types.galaxy_shear_cl_ee CEd = sacc.standard_types.galaxy_shearDensity_cl_e Cdd = sacc.standard_types.galaxy_density_cl theory = {} for data_type in [CEE, CEd, Cdd]: for t1, t2 in sacc_data.get_tracer_combinations(data_type): ell = sacc_data.get_tag('ell', data_type, (t1, t2)) tracer1 = sacc_data.get_tracer(t1) tracer2 = sacc_data.get_tracer(t2) dndz1 = (tracer1.z, tracer1.nz) dndz2 = (tracer2.z, tracer2.nz) if data_type in [CEE, CEd]: cTracer1 = ccl.WeakLensingTracer(cosmo, dndz=dndz1) else: bias = (tracer1.z, np.ones_like(tracer1.z)) cTracer1 = ccl.NumberCountsTracer(cosmo, has_rsd=False, bias=bias, dndz=dndz1) warnings.warn("Not including bias in fiducial cosmology") if data_type == CEE: cTracer2 = ccl.WeakLensingTracer(cosmo, dndz=dndz1) else: bias = (tracer2.z, np.ones_like(tracer2.z)) cTracer2 = ccl.NumberCountsTracer(cosmo, has_rsd=False, bias=bias, dndz=dndz2) print(" - Calculating fiducial C_ell for ", data_type, t1, t2) theory[(data_type, t1, t2)] = ccl.angular_cl(cosmo, cTracer1, cTracer2, ell) return theory
def get_tracer_info(self, two_point_data={}): """ Creates CCL tracer objects and computes the noise for all the tracers Check usage: Can we call all the tracer at once? Parameters: ----------- two_point_data (sacc obj): Returns: -------- ccl_tracers: dict, ccl obj ccl.WeakLensingTracer or ccl.NumberCountsTracer tracer_Noise ({dict: float}): shot (shape) noise for lens (sources) """ ccl_tracers = {} tracer_Noise = {} # b = { l:bi*np.ones(len(z)) for l, bi in self.lens_bias.items()} for tracer in two_point_data.tracers: tracer_dat = two_point_data.get_tracer(tracer) z = tracer_dat.z # FIXME: Following should be read from sacc dataset.-------------- #Ngal = 26. # arc_min^2 #sigma_e = .26 #b = 1.5*np.ones(len(z)) # Galaxy bias (constant with scale and z) # AI = .5*np.ones(len(z)) # Galaxy bias (constant with scale and z) #Ngal = Ngal*3600/d2r**2 # --------------------------------------------------------------- dNdz = tracer_dat.nz dNdz /= (dNdz * np.gradient(z)).sum() dNdz *= self.Ngal[tracer] #FAO this should be called by tomographic bin if 'source' in tracer or 'src' in tracer: IA_bin = self.IA * np.ones(len(z)) # fao: refactor this ccl_tracers[tracer] = ccl.WeakLensingTracer(self.cosmo, dndz=(z, dNdz), ia_bias=(z, IA_bin)) # CCL automatically normalizes dNdz tracer_Noise[ tracer] = self.sigma_e[tracer]**2 / self.Ngal[tracer] elif 'lens' in tracer: # import pdb; pdb.set_trace() b = self.bias_lens[tracer] * np.ones(len(z)) tracer_Noise[tracer] = 1. / self.Ngal[tracer] ccl_tracers[tracer] = ccl.NumberCountsTracer(self.cosmo, has_rsd=False, dndz=(z, dNdz), bias=(z, b)) return ccl_tracers, tracer_Noise
def get_log_prob(self, theta): # Check the priors log_prior = self.get_log_prior(theta) if not np.isfinite(log_prior): return -np.inf # Update default cosmological parameters with new sampled parameters params = self.cosmology_params.copy() for param_name in self.arg_names: if param_name in params: params[param_name] = theta[self.arg_names.index(param_name)] cosmo = ccl.Cosmology(**params) # Update data parameters z_tail = theta[self.arg_names.index( 'z_tail')] if 'z_tail' in self.arg_names else self.z_tail bias = theta[self.arg_names.index( 'bias')] if 'bias' in self.arg_names else self.bias # Get redshift distribution z_arr, n_arr = get_lotss_redshift_distribution(z_tail=z_tail) bias_arr = bias * np.ones(len(z_arr)) bias_arr = bias_arr / ccl.growth_factor(cosmo, 1. / (1 + z_arr)) # Get correlations number_counts_tracer = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(z_arr, n_arr), bias=(z_arr, bias_arr)) cmb_lensing_tracer = ccl.CMBLensingTracer(cosmo, 1091) correlations = {} if 'gg' in self.correlation_symbols: correlations['gg'] = ccl.angular_cl(cosmo, number_counts_tracer, number_counts_tracer, self.l_arr) if 'gk' in self.correlation_symbols: correlations['gk'] = ccl.angular_cl(cosmo, number_counts_tracer, cmb_lensing_tracer, self.l_arr) # Bin spectra using coupling matrices in workspaces for correlation_symbol in self.correlation_symbols: correlations[correlation_symbol] = decouple_correlation( self.workspaces[correlation_symbol], correlations[correlation_symbol]) # Calculate log prob model = np.concatenate([ correlations[correlation_symbol][:self.n_ells[correlation_symbol]] for correlation_symbol in self.correlation_symbols ]) diff = self.data_vector - model log_prob = log_prior - np.dot( diff, np.dot(self.inverted_covariance, diff)) / 2.0 return log_prob
def get_tracers_ccl(cosmo, z, pz, bz): n_bins = pz.shape[0] # Tracers tracers = [] for i in range(n_bins): tracers.append( ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(z[i], pz[i]), bias=(z[i], bz[i]))) tracers.append(ccl.WeakLensingTracer(cosmo, dndz=(z[i], pz[i]))) return np.array(tracers)
def update_tracer(self, cosmo, **kwargs): if self.type == 'g': nz_new = self.nzf(self.z_avg + (self.z - self.z_avg) / kwargs['width']) nz_new /= simps(nz_new, x=self.z) self.tracer = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(self.z, nz_new), bias=(self.z, self.bz)) elif self.type == 'y': self.tracer = ccl.tSZTracer(cosmo) elif self.type == 'k': self.tracer = ccl.CMBLensingTracer(cosmo, z_source=1100.)
def setup(self): # Initialize cosmology par = self.get_cosmological_parameters() dpk = self.get_pk() a = 1./(1+dpk['z'][::-1]) self.cosmo = ccl.CosmologyCalculator(Omega_c=par['Omega_m']-par['Omega_b'], Omega_b=par['Omega_b'], h=par['h'], n_s=par['n_s'], A_s=par['A_s'], w0=par['w0'], pk_linear={'a': a, 'k': dpk['k'], 'delta_matter:delta_matter': dpk['pk_lin'][::-1][:]}, pk_nonlin={'a': a, 'k': dpk['k'], 'delta_matter:delta_matter': dpk['pk_nl'][::-1][:]}) # Initialize tracers if self.config.get('tracers_from_kernels', False): tpar = self.get_tracer_parameters() ker = self.get_tracer_kernels() a_g = 1./(1+ker['z_cl'][::-1]) self.t_g = [] for k in ker['kernels_cl']: t = ccl.Tracer() barr = np.ones_like(a_g) t.add_tracer(self.cosmo, (ker['chi_cl'], k), transfer_a=(a_g, barr)) self.t_g.append(t) self.t_s = [] for k in ker['kernels_sh']: t = ccl.Tracer() t.add_tracer(self.cosmo, kernel=(ker['chi_sh'], k), der_bessel=-1, der_angles=2) self.t_s.append(t) else: nzs = self.get_tracer_dndzs() tpar = self.get_tracer_parameters() z_g = nzs['z_cl'] z_s = nzs['z_sh'] self.t_g = [ccl.NumberCountsTracer(self.cosmo, True, (z_g, nzs['dNdz_cl'][:, ni]), bias=(z_g, np.full(len(z_g), b))) for ni, b in zip(range(0, 10), tpar['b_g'])] self.t_s = [ccl.WeakLensingTracer(self.cosmo, (z_s, nzs['dNdz_sh'][:, ni]), True) for ni in range(0, 5)]
def get_ccl_tracer(tr): if tr == 'galaxy_density': z, nz = np.loadtxt('xcell/tests/data/DESY1gc_dndz_bin0.txt', usecols=(1, 3), unpack=True) t = ccl.NumberCountsTracer(cosmo, False, dndz=(z, nz), bias=(z, np.ones_like(z)), mag_bias=(z, np.ones_like(z))) elif tr == 'galaxy_shear': z, nz = np.loadtxt('xcell/tests/data/Nz_DIR_z0.1t0.3.asc', usecols=(0, 1), unpack=True) t = ccl.WeakLensingTracer(cosmo, dndz=(z, nz)) elif tr == 'cmb_convergence': t = ccl.CMBLensingTracer(cosmo, z_source=1100.) return t
def get_tracers_ccl(cosmo, tracers_info): # Get Tracers for tr in tracers_info['maps']: if tr['type'] == 'gc': # Import z, pz fname = os.path.join(files_root, tr['dndz_file']) z, pz = np.loadtxt(fname, usecols=(1, 3), unpack=True) # Calculate z bias dz = 0 z_dz = z - dz # Set to 0 points where z_dz < 0: sel = z_dz >= 0 z_dz = z_dz[sel] pz = pz[sel] # Calculate bias bias = tr['bias'] bz = bias * np.ones(z.shape) # Get tracer tr['tracer'] = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(z_dz, pz), bias=(z, bz)) elif tr['type'] == 'wl': # Import z, pz fname = os.path.join(files_root, tr['dndz_file']) z, pz = np.loadtxt(fname, usecols=(1, 3), unpack=True) # Calculate z bias dz = 0 z_dz = z - dz # Set to 0 points where z_dz < 0: sel = z_dz >= 0 z_dz = z_dz[sel] pz = pz[sel] # # Calculate bias IA # A = # eta = # z0 = # bz = A*((1.+z)/(1.+z0))**eta*0.0139/0.013872474 # pyccl2 -> has already the factor inside. Only needed bz # Get tracer tr['tracer'] = ccl.WeakLensingTracer(cosmo, dndz=(z_dz, pz)) # ,ia_bias=(z,bz)) elif tr['type'] == 'cv': tr['tracer'] = ccl.CMBLensingTracer( cosmo, z_source=1100) #TODO: correct z_source else: raise ValueError( 'Type of tracer not recognized. It can be gc, wl or cv!')
def test_iswcl(): # Cosmology Ob = 0.05 Oc = 0.25 h = 0.7 COSMO = ccl.Cosmology(Omega_b=Ob, Omega_c=Oc, h=h, n_s=0.96, sigma8=0.8, transfer_function='bbks') # CCL calculation ls = np.arange(2, 100) zs = np.linspace(0, 0.6, 256) nz = np.exp(-0.5 * ((zs - 0.3) / 0.05)**2) bz = np.ones_like(zs) tr_n = ccl.NumberCountsTracer(COSMO, has_rsd=False, dndz=(zs, nz), bias=(zs, bz)) tr_i = ccl.ISWTracer(COSMO) cl = ccl.angular_cl(COSMO, tr_n, tr_i, ls) # Benchmark from Eq. 6 in 1710.03238 pz = nz / simps(nz, x=zs) H0 = h / ccl.physical_constants.CLIGHT_HMPC # Prefactor prefac = 3 * COSMO['T_CMB'] * (Oc + Ob) * H0**3 / (ls + 0.5)**2 # H(z)/H0 ez = ccl.h_over_h0(COSMO, 1. / (1 + zs)) # Linear growth and derivative dz = ccl.growth_factor(COSMO, 1. / (1 + zs)) gz = np.gradient(dz * (1 + zs), zs[1] - zs[0]) / dz # Comoving distance chi = ccl.comoving_radial_distance(COSMO, 1 / (1 + zs)) # P(k) pks = np.array([ ccl.nonlin_matter_power(COSMO, (ls + 0.5) / (c + 1E-6), 1. / (1 + z)) for c, z in zip(chi, zs) ]).T # Limber integral cl_int = pks[:, :] * (pz * ez * gz)[None, :] clbb = simps(cl_int, x=zs) clbb *= prefac assert np.all(np.fabs(cl / clbb - 1) < 1E-3)
def test_clustering_cl(): # We first define equivalent CCL and jax_cosmo cosmologies cosmo_ccl = ccl.Cosmology( Omega_c=0.3, Omega_b=0.05, h=0.7, sigma8=0.8, n_s=0.96, Neff=0, transfer_function="eisenstein_hu", matter_power_spectrum="halofit", ) cosmo_jax = Cosmology( Omega_c=0.3, Omega_b=0.05, h=0.7, sigma8=0.8, n_s=0.96, Omega_k=0.0, w0=-1.0, wa=0.0, ) # Define a redshift distribution nz = smail_nz(1.0, 2.0, 1.0) # And a bias model bias = constant_linear_bias(1.0) z = np.linspace(0, 5.0, 1024) tracer_ccl = ccl.NumberCountsTracer(cosmo_ccl, has_rsd=False, dndz=(z, nz(z)), bias=(z, bias(cosmo_jax, z))) tracer_jax = probes.NumberCounts([nz], bias) # Get an ell range for the cls ell = np.logspace(0.1, 4) # Compute the cls cl_ccl = ccl.angular_cl(cosmo_ccl, tracer_ccl, tracer_ccl, ell) cl_jax = angular_cl(cosmo_jax, ell, [tracer_jax]) assert_allclose(cl_ccl, cl_jax[0], rtol=0.5e-2)
def _get_cl_ccl(self, dtype): ls = np.arange(3 * self.nside) if dtype == 'galaxy_density': z, nz = self.get_nz() b = np.ones_like(z) tracer = ccl.NumberCountsTracer(self.cosmo, has_rsd=False, dndz=(z, nz), bias=(z, b)) elif dtype == 'galaxy_shear': z, nz = self.get_nz() tracer = ccl.WeakLensingTracer(self.cosmo, dndz=(z, nz)) elif dtype == 'cmb_convergence': tracer = ccl.CMBLensingTracer(self.cosmo, z_source=1100) elif dtype == 'cmb_tSZ': # Note that the tSZ power spectrum implemented here is wrong # But it's not worth for now adding all the halo model stuff. tracer = ccl.tSZTracer(self.cosmo, z_max=3.) return ccl.angular_cl(self.cosmo, tracer, tracer, ls)
def _get_theory(self, **params_values): cosmo = self.provider.get_CCL()['cosmo'] tracer_g = ccl.NumberCountsTracer( cosmo, has_rsd=False, dndz=self.dndz.T, bias=(self.dndz[:, 0], params_values['b1'] * np.ones(len(self.dndz[:, 0]))), mag_bias=(self.dndz[:, 0], params_values['s1'] * np.ones(len(self.dndz[:, 0])))) tracer_k = ccl.CMBLensingTracer(cosmo, z_source=1060) cl_gg = ccl.cls.angular_cl(cosmo, tracer_g, tracer_g, self.ell_auto) # + 1e-7 cl_kg = ccl.cls.angular_cl(cosmo, tracer_k, tracer_g, self.ell_cross) return np.concatenate([cl_gg, cl_kg])
def set_theory_correlations(self): # Get cosmology parameters with open(os.path.join(PROJECT_PATH, 'cosmologies.yml'), 'r') as cosmology_file: self.cosmology_params = yaml.full_load(cosmology_file)[ self.cosmology_name] self.cosmology_params[ 'matter_power_spectrum'] = self.cosmology_matter_power_spectrum cosmology = ccl.Cosmology(**self.cosmology_params) bias_arr = self.bias * np.ones(len(self.z_arr)) if self.scale_bias: bias_arr = bias_arr / ccl.growth_factor(cosmology, 1. / (1. + self.z_arr)) tracers_dict = { 'g': ccl.NumberCountsTracer(cosmology, has_rsd=False, dndz=(self.z_arr, self.n_arr), bias=(self.z_arr, bias_arr)), 'k': ccl.CMBLensingTracer(cosmology, 1091), 't': ISWTracer(cosmology, z_max=6., n_chi=1024), } for correlation_symbol in self.all_correlation_symbols: # Pass if theory correlation was set earlier with maps if correlation_symbol not in self.theory_correlations: tracer_symbol_a = correlation_symbol[0] tracer_symbol_b = correlation_symbol[1] correlation_symbol = tracer_symbol_a + tracer_symbol_b self.theory_correlations[correlation_symbol] = ccl.angular_cl( cosmology, tracers_dict[tracer_symbol_a], tracers_dict[tracer_symbol_b], self.l_arr) for correlation_symbol in self.theory_correlations.keys(): self.theory_correlations[correlation_symbol] += self.noise_curves[ correlation_symbol]
def _sum_stat(self, theta): lens = ccl.NumberCountsTracer(self.model, dndz=(self.z, self.gal_nz), has_rsd=True, bias=(self.z, theta * np.ones(len(self.z)))) source = ccl.WeakLensingTracer(self.model, dndz=(self.z, self.shear_nz), has_shear=True, ia_bias=None) cl_gm = ccl.angular_cl(self.model, lens, source, self.ell) xi = ccl.correlation(self.model, self.ell, cl_gm, self.theta / 60, corr_type='gl', method='Bessel') return xi
def get_tracer(tracer_type): z = np.linspace(0., 1., 2000) n = dndz(z) b = np.sqrt(1. + z) if tracer_type == 'nc': ntr = 3 tr = ccl.NumberCountsTracer(COSMO, True, dndz=(z, n), bias=(z, b), mag_bias=(z, b)) elif tracer_type == 'wl': ntr = 2 tr = ccl.WeakLensingTracer(COSMO, dndz=(z, n), ia_bias=(z, b)) elif tracer_type == 'cl': ntr = 1 tr = ccl.CMBLensingTracer(COSMO, 1100.) else: ntr = 0 tr = ccl.Tracer() return tr, ntr
def _get_tracer(self, cosmo, name, **pars): # Get CCL tracer for tracer with name `name` if 'cv' not in name: nz = self._get_nz(cosmo, name, **pars) if 'gc' in name or 'eBOSS' in name or 'DECALS' in name: # eBOSS, DECALS also gc bz = self._get_bz(cosmo, name, **pars) if '_s' in name: # Magnification bias mag_bias = self._get_mag(cosmo, name, **pars) else: mag_bias = None t = ccl.NumberCountsTracer(cosmo, dndz=nz, bias=bz, has_rsd=False, mag_bias=mag_bias) elif 'wl' in name or 'KiDS1000' in name: ia = self._get_ia_bias(cosmo, name, **pars) t = ccl.WeakLensingTracer(cosmo, nz, ia_bias=ia) elif 'cv' in name: # B.H. TODO: pass z_source as parameter to the YAML file t = ccl.CMBLensingTracer(cosmo, z_source=1100) return t
def tracer_spectro(cosmo, zmin, zmax, kind="galaxy"): """ Create a spectroscopic CCL tracer object with the right bias and selection function, for either a galaxy survey or an IM survey. Parameters: cosmo (ccl.Cosmology): CCL Cosmology object. zmin, zmax (float): Minimum and maximum redshift of the spectroscopic redshift bin. kind (str): The kind of tracer (can be 'galaxy' or 'im'). Returns: tracer (ccl.NumberCountsTracer): A CCL tracer object with the right bias/selection function. """ # Number counts/selection function in this tomographic redshift bin z = np.linspace(zmin * 0.8, zmax * 1.2, 2000) # Pad zmin, zmax slightly tomo = np.zeros(z.size) tomo[np.where(np.logical_and(z >= zmin, z < zmax))] = 1.0 # Define bias factors/selection function if kind == "galaxy": bz = bias_gal(z) else: # Clustering bias and 21cm monopole temperature, in mK bz = bias_HI(z) * Tb(z) # Number density tracer object n_spectro = ccl.NumberCountsTracer(cosmo, has_rsd=False, mag_bias=None, dndz=(z, tomo), bias=(z, bz)) return n_spectro