def test_xi(set_up, corr_method, t1, t2, bm, er, kind, pref): cosmo, trcs, bms, ers, fls = set_up method, errfac = corr_method # Debugging - define the same cosmology but in GR cl = ccl.angular_cl(cosmo, trcs[t1], trcs[t2], fls['ells']) ell = np.arange(fls['lmax']) cli = interp1d(fls['ells'], cl, kind='cubic')(ell) # Our benchmarks have theta in arcmin # but CCL requires it in degrees: theta_deg = bms['theta'] / 60. # We cut the largest theta value for xi+ because of issues with the # benchmarks. if kind == 'GG+': xi = ccl.correlation(cosmo, ell, cli, theta_deg[0:14], type=kind, method=method) else: xi = ccl.correlation(cosmo, ell, cli, theta_deg, type=kind, method=method) xi *= pref assert np.all(np.fabs(xi - bms[bm]) < ers[er] * errfac)
def get_tomo_xi_ccl(sigma8, Omega_cm0, Omega_bm0, h, zpts, zhist, theta_deg, ell): ns = 0.965 ell_num = len(ell) # tomo panel num tomo_bin_num, zpts_num = zhist.shape tomo_panel_num = int((tomo_bin_num * tomo_bin_num + tomo_bin_num) / 2) cosmo = pyccl.Cosmology(Omega_c=Omega_cm0, Omega_b=Omega_bm0, h=h, n_s=ns, sigma8=sigma8, transfer_function="boltzmann_camb") ccl_lens_trs = [] ccl_PL = numpy.zeros((tomo_panel_num, ell_num)) ccl_xip = numpy.zeros_like(theta_deg) ccl_xim = numpy.zeros_like(theta_deg) for i in range(tomo_bin_num): lens_tr = pyccl.WeakLensingTracer(cosmo, dndz=(zpts, zhist[i])) ccl_lens_trs.append(lens_tr) tag = 0 for i in range(tomo_bin_num): for j in range(i, tomo_bin_num): ccl_PL[tag] = pyccl.angular_cl(cosmo, ccl_lens_trs[i], ccl_lens_trs[j], ell) ccl_xip[tag] = pyccl.correlation(cosmo, ell, ccl_PL[tag], theta_deg[tag], type='GG+', method='FFTLog') ccl_xim[tag] = pyccl.correlation(cosmo, ell, ccl_PL[tag], theta_deg[tag], type='GG-', method='FFTLog') tag += 1 return ccl_xip, ccl_xim, ccl_PL
def test_correlation_zero_ends(): # This should give an error instead of crashing ell = np.arange(2, 1001) C_ell = np.zeros(ell.size) C_ell[500] = 1.0 theta = np.logspace(0, 2, 20) with pytest.raises(ccl.CCLError): ccl.correlation(COSMO, ell, C_ell, theta)
def get_tomo_xi_ccl_2(sigma8, Omega_cm0, Omega_bm0, h, zpts, zhist, theta_deg, theta_num_per_bin, ell, used_zbins, xi_pm="xi_p"): """ for the MCMC :param sigma8: :param Omega_cm0: :param Omega_bm0: :param h: :param zpts: the center of the Z bins :param zhist: Z histogram, (n,m), n is the tomographic bin num, includes the histograms of all bins, the used_zbins will determine which bins are used :param theta_deg: where the signals are measured, (n,) :param ell: \ell of C(\ell), the angular power spectrum :param uesd_zbins: numpy array,(n,), labels, 1 for used, 0 for not used :return: """ # tomo panel num tomo_bin_num, zbin_num = used_zbins.shape[0],used_zbins.sum() cosmo = pyccl.Cosmology(Omega_c=Omega_cm0, Omega_b=Omega_bm0, h=h, n_s=0.965, sigma8=sigma8, transfer_function="boltzmann_camb") ccl_lens_trs = [] for i in range(tomo_bin_num): if used_zbins[i] == 1: lens_tr = pyccl.WeakLensingTracer(cosmo, dndz=(zpts, zhist[i])) ccl_lens_trs.append(lens_tr) if xi_pm == "all": pts_num = int(2*theta_deg.shape[0]) ccl_xipm = numpy.zeros((pts_num,)) tag = 0 for i in range(zbin_num): for j in range(i, zbin_num): st, ed = int(tag * theta_num_per_bin), int((tag + 1) * theta_num_per_bin) ccl_cls = pyccl.angular_cl(cosmo, ccl_lens_trs[i], ccl_lens_trs[j], ell) ccl_xipm[st:ed] = pyccl.correlation(cosmo, ell, ccl_cls, theta_deg[st:ed], type='GG+', method='FFTLog') ccl_xipm[pts_num+st:pts_num+ed] = pyccl.correlation(cosmo, ell, ccl_cls, theta_deg[st:ed], type='GG-', method='FFTLog') tag += 1 return ccl_xipm if xi_pm == "xi_p": corr_type = "GG+" elif xi_pm == "xi_m": corr_type = "GG-" else: print("xi_pm must be one of [\"xi_p\", \"xi_m\",\"all\"]") return None ccl_xi = numpy.zeros_like(theta_deg) tag = 0 for i in range(zbin_num): for j in range(i, zbin_num): st, ed = int(tag*theta_num_per_bin), int((tag+1)*theta_num_per_bin) ccl_cls = pyccl.angular_cl(cosmo, ccl_lens_trs[i], ccl_lens_trs[j], ell) ccl_xi[st:ed] = pyccl.correlation(cosmo, ell, ccl_cls, theta_deg[st:ed], type=corr_type, method='FFTLog') tag += 1 return ccl_xi
def setup_data(): """Set up data for test.""" pi = np.pi # Set up ccl cosmology object cosmo = ccl.Cosmology(Omega_c=0.25, Omega_b=0.05, n_s=0.96, sigma8=0.8, h=0.7) # Set up a simple n(z) z = np.linspace(0., 1., 50) n = np.exp(-((z - 0.5) / 0.1)**2) lens_tracer = ccl.ClTracerLensing(cosmo, False, n=n, z=z) # Set up ell and get Cl's ell = np.arange(2, 4000, dtype=np.float64) Cl = ccl.angular_cl(cosmo, lens_tracer, lens_tracer, ell) # Set up theta theta_min = 0.6 / 60 # degree theta_max = 600.0 / 60 # degree n_theta = 100 theta = np.logspace(np.log10(theta_min), np.log10(theta_max), n_theta, endpoint=True) # Get CCL xi's xi_plus = ccl.correlation(cosmo, ell, Cl, theta, corr_type="L+", method="fftlog") xi_minus = ccl.correlation(cosmo, ell, Cl, theta, corr_type="L-", method="fftlog") # Get FFTLog xi's xi_plus_fftlog = fftlog.Cl2xi(Cl, ell, theta, bessel_order=0) xi_minus_fftlog = fftlog.Cl2xi(Cl, ell, theta, bessel_order=4) return (xi_plus_fftlog, xi_minus_fftlog), (xi_plus, xi_minus), (theta, theta)
def get_theory(var): #Get cosmological parameters var_tot = get_cosmo(var) #Cosmology cosmo = ccl.Cosmology(h=var_tot[0], Omega_c=var_tot[1] / var_tot[0]**2., Omega_b=var_tot[2] / var_tot[0]**2., A_s=(10.**(-10.)) * np.exp(var_tot[3]), n_s=var_tot[4]) #Tracers lens = np.array([ ccl.ClTracerLensing(cosmo, False, z=z.astype(np.float64), n=pz[x].astype(np.float64)) for x in range(n_bins) ]) #Cl's ell = np.arange(n_ells) cls = np.zeros((n_bins, n_bins, n_ells)) for count1 in range(n_bins): for count2 in range(n_bins): cls[count1, count2] = ccl.angular_cl(cosmo, lens[count1], lens[count2], ell) cls = np.transpose(cls, axes=[2, 0, 1]) #Correlation function xi_th = np.zeros((2, n_bins, n_bins, n_theta)) for count1 in range(n_bins): for count2 in range(n_bins): for count3 in range(n_theta): xi_th[0, count1, count2, count3] = ccl.correlation(cosmo, ell, cls[:, count1, count2], theta[count3], corr_type='L+', method='FFTLog') xi_th[1, count1, count2, count3] = ccl.correlation(cosmo, ell, cls[:, count1, count2], theta[count3], corr_type='L-', method='FFTLog') xi_th = np.transpose(xi_th, axes=[0, 3, 1, 2]) #Reshape and eventually KL transform if is_kl: xi_th = kl_transform(xi_th, datat='corr') xi_th = reshape(xi_th, datat='corr') return xi_th
def test_xi(set_up, corr_method, t1, t2, bm, er, kind, pref): cosmo, trcs, bms, ers, fls = set_up method, errfac = corr_method global T0_CLS t0 = time.time() cl = ccl.angular_cl(cosmo, trcs[t1], trcs[t2], fls['ells']) T0_CLS += (time.time() - t0) ell = np.arange(fls['lmax']) cli = interp1d(fls['ells'], cl, kind='cubic')(ell) global T0 t0 = time.time() xi = ccl.correlation(cosmo, ell, cli, bms['theta'], corr_type=kind, method=method) T0 += (time.time() - t0) xi *= pref assert np.all(np.fabs(xi - bms[bm]) < ers[er] * errfac) print("time:", T0) print("time cls:", T0_CLS)
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 check_corr(cosmo): # Number density input z = np.linspace(0., 1., 200) n = np.ones(z.shape) # ClTracer test objects lens1 = ccl.ClTracerLensing(cosmo, False, n=n, z=z) lens2 = ccl.ClTracerLensing(cosmo, True, n=(z, n), bias_ia=(z, n), f_red=(z, n)) ells = np.arange(3000) cls = ccl.angular_cl(cosmo, lens1, lens2, ells) t = np.logspace(-2, np.log10(5.), 20) #degrees corrfunc = ccl.correlation(cosmo, ells, cls, t, corr_type='L+', method='FFTLog') assert_(all_finite(corrfunc))
def comp_xipm(self, theta): self.wl_bin_shear = [] self.Cl = {} self.xip = {} self.xim = {} for i in range(4): filtre = (self.redshifts[i] > 0) self.wl_bin_shear.append( ccl.WeakLensingTracer(self.cosmology, dndz=(self.redshifts[i][filtre], self.nz[i][filtre]), has_shear=True, ia_bias=(self.redshift, self.AI))) for i in range(4): for j in range(4): if i >= j: # calcul de l auto-correlation pour le bin donnee key = "%i_%i" % ((i, j)) self.Cl.update({ key: ccl.angular_cl(self.cosmology, self.wl_bin_shear[i], self.wl_bin_shear[j], self.ell) }) m_ij = (1. + self.mbias[i]) * (1. + self.mbias[j]) self.xip.update({ key: ccl.correlation(self.cosmology, self.ell, self.Cl[key], theta, corr_type='L+', method='fftlog') * m_ij }) self.xim.update({ key: ccl.correlation(self.cosmology, self.ell, self.Cl[key], theta, corr_type='L-', method='fftlog') * m_ij }) self.theta = theta
def test_two_point_some(kind, tmpdir): cosmo = ccl.Cosmology(Omega_c=0.27, Omega_b=0.045, Omega_k=0.0, w0=-1.0, wa=0.0, sigma8=0.8, n_s=0.96, h=0.67) sources = {} for i, mn in enumerate([0.25, 0.75]): sources['src%d' % i] = DummySource() z = np.linspace(0, 2, 50) nz = np.exp(-0.5 * (z - mn)**2 / 0.25 / 0.25) if ('g' in kind and i == 0) or kind == 'gg': sources['src%d' % i].tracer_ = ccl.ClTracerNumberCounts( cosmo, has_rsd=False, has_magnification=False, z=z, n=nz, bias=np.ones_like(z) * 2.0) else: sources['src%d' % i].tracer_ = ccl.ClTracerLensing( cosmo, has_intrinsic_alignment=False, z=z, n=nz) sources['src%d' % i].scale_ = i / 2.0 + 1.0 # compute the statistic tracers = [v.tracer_ for k, v in sources.items()] scale = np.prod([v.scale_ for k, v in sources.items()]) data = os.path.join(tmpdir, 'stat.csv') if kind == 'cl': ell = np.logspace(1, 3, 10) cell = ccl.angular_cl(cosmo, *tracers, ell) * scale pd.DataFrame({'l': ell, 'cl': cell}).to_csv(data, index=False) else: theta = np.logspace(1, 2, 100) ell = _ell_for_xi() cell = ccl.angular_cl(cosmo, *tracers, ell) xi = ccl.correlation(cosmo, ell, cell, theta / 60.0, corr_type=kind) * scale pd.DataFrame({'t': theta, 'xi': xi}).to_csv(data, index=False) stat = TwoPointStatistic(data=data, kind=kind, sources=['src0', 'src1']) stat.compute(cosmo, {}, sources, systematics=None) if kind == 'cl': assert np.allclose(stat.measured_statistic_, cell) else: assert np.allclose(stat.measured_statistic_, xi) assert np.allclose(stat.measured_statistic_, stat.predicted_statistic_)
def test_xi(set_up, corr_method, t1, t2, bm, er, kind, pref): cosmo, trcs, bms, ers, fls = set_up method, errfac = corr_method cl = ccl.angular_cl(cosmo, trcs[t1], trcs[t2], fls['ells']) ell = np.arange(fls['lmax']) cli = interp1d(fls['ells'], cl, kind='cubic')(ell) xi = ccl.correlation(cosmo, ell, cli, bms['theta'], corr_type=kind, method=method) xi *= pref assert np.all(np.fabs(xi - bms[bm]) < ers[er] * errfac)
def test_correlation_zero(): ell = np.arange(2, 100000) C_ell = np.zeros(ell.size) theta = np.logspace(0, 2, 10000) t0 = default_timer() corr = ccl.correlation(COSMO, ell, C_ell, theta) t1 = default_timer() # if the short-cut has worked this should take # less than 1 second at the absolute outside assert t1 - t0 < 1.0 assert (corr == np.zeros(theta.size)).all()
def test_correlation_raises(): with pytest.raises(ValueError): ccl.correlation(COSMO, [1], [1e-3], [1], method='blah') with pytest.raises(ValueError): ccl.correlation(COSMO, [1], [1e-3], [1], type='blah') with pytest.raises(ValueError): ccl.correlation(COSMO, [1], [1e-3], [1], corr_type='blah')
def check_corr(cosmo): # Number density input z = np.linspace(0., 1., 200) n = np.ones(z.shape) # ClTracer test objects lens1 = ccl.ClTracerLensing(cosmo, False, n=n, z=z) lens2 = ccl.ClTracerLensing(cosmo, True, n=(z,n), bias_ia=(z,n), f_red=(z,n)) ells = np.arange(3000) cls = ccl.angular_cl(cosmo, lens1, lens2, ells) t_arr = np.logspace(-2., np.log10(5.), 20) # degrees t_lst = [t for t in t_arr] t_scl = 2. t_int = 2 # Make sure correlation functions work for valid inputs corr1 = ccl.correlation(cosmo, ells, cls, t_arr, corr_type='L+', method='FFTLog') corr2 = ccl.correlation(cosmo, ells, cls, t_lst, corr_type='L+', method='FFTLog') corr3 = ccl.correlation(cosmo, ells, cls, t_scl, corr_type='L+', method='FFTLog') corr4 = ccl.correlation(cosmo, ells, cls, t_int, corr_type='L+', method='FFTLog') assert_( all_finite(corr1)) assert_( all_finite(corr2)) assert_( all_finite(corr3)) assert_( all_finite(corr4)) # Check that exceptions are raised for invalid input assert_raises(KeyError, ccl.correlation, cosmo, ells, cls, t_arr, corr_type='xx', method='FFTLog') assert_raises(KeyError, ccl.correlation, cosmo, ells, cls, t_arr, corr_type='L+', method='xx')
def test_correlation_smoke(method): z = np.linspace(0., 1., 200) n = np.ones(z.shape) lens = ccl.WeakLensingTracer(COSMO, dndz=(z, n)) ell = np.logspace(1, 3, 5) cl = ccl.angular_cl(COSMO, lens, lens, ell) t_arr = np.logspace(-2., np.log10(5.), 5) t_lst = [t for t in t_arr] t_scl = 2. t_int = 2 for tval in [t_arr, t_lst, t_scl, t_int]: corr = ccl.correlation(COSMO, ell, cl, tval, type='NN', method=method) assert np.all(np.isfinite(corr)) assert np.shape(corr) == np.shape(tval)
def test_correlation_newtypes(typs): from pyccl.pyutils import assert_warns z = np.linspace(0., 1., 200) n = np.ones(z.shape) lens = ccl.WeakLensingTracer(COSMO, dndz=(z, n)) ell = np.logspace(1, 3, 5) cl = ccl.angular_cl(COSMO, lens, lens, ell) theta = np.logspace(-2., np.log10(5.), 5) corr_old = assert_warns(ccl.CCLWarning, ccl.correlation, COSMO, ell, cl, theta, corr_type=typs[0]) corr_new = ccl.correlation(COSMO, ell, cl, theta, type=typs[1]) assert np.all(corr_new == corr_old)
def test_xi(set_up, corr_method, t1, t2, bm, er, kind, pref): cosmo, trcs, bms, ers, fls = set_up method, errfac = corr_method # Debugging - define the same cosmology but in GR cl = ccl.angular_cl(cosmo, trcs[t1], trcs[t2], fls['ells']) ell = np.arange(fls['lmax']) cli = interp1d(fls['ells'], cl, kind='cubic')(ell) # Our benchmarks have theta in arcmin # but CCL requires it in degrees: theta_deg = bms['theta'] / 60. xi = ccl.correlation(cosmo, ell, cli, theta_deg, corr_type=kind, method=method) xi *= pref print(xi) assert np.all(np.fabs(xi - bms[bm]) < ers[er] * errfac)
def compute(self, cosmo, params, sources, systematics=None): """Compute a two-point statistic from sources. Parameters ---------- cosmo : pyccl.Cosmology A pyccl.Cosmology object. params : dict A dictionary mapping parameter names to their current values. sources : dict A dictionary mapping sources to their objects. The sources must already have been rendered by calling `render` on them. systematics : dict, optional A dictionary mapping systematic names to their objects. The default of `None` corresponds to no systematics. """ self.ell_or_theta_ = self._ell_or_theta.copy() self.measured_statistic_ = self._stat.copy() tracers = [sources[k].tracer_ for k in self.sources] self.scale_ = np.prod([sources[k].scale_ for k in self.sources]) if self.kind == 'cl': self.predicted_statistic_ = ccl.angular_cl( cosmo, *tracers, self.ell_or_theta_) * self.scale_ else: ells = _ell_for_xi(ell_min=self.ell_min, ell_mid=self.ell_mid, ell_max=self.ell_max, n_log=self.n_log) cells = ccl.angular_cl(cosmo, *tracers, ells) self.predicted_statistic_ = ccl.correlation( cosmo, ells, cells, self.ell_or_theta_ / 60, corr_type=self.kind) * self.scale_ systematics = systematics or {} for systematic in self.systematics: systematics[systematic].apply(cosmo, params, self)
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 make_theory_plot_data(data, cosmo, obs, label, smooth=True): import pyccl theory = {'name': label} nbin_source = obs['nbin_source'] nbin_lens = obs['nbin_lens'] ell = np.unique(np.logspace(np.log10(2), 5, 400).astype(int)) tracers = {} for i in range(nbin_source): name = f'source_{i}' Ti = data.get_tracer(name) if smooth: nz = smooth_nz(Ti.nz) else: nz = Ti.nz tracers[name] = pyccl.WeakLensingTracer(cosmo, (Ti.z, nz)) for i in range(nbin_lens): name = f'lens_{i}' Ti = data.get_tracer(name) if smooth: nz = smooth_nz(Ti.nz) else: nz = Ti.nz tracers[name] = pyccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(Ti.z, nz), bias=(Ti.z, np.ones_like(Ti.z))) for i in range(nbin_source): for j in range(i + 1): theta, _ = obs[(XIP, i, j)] cl = pyccl.angular_cl(cosmo, tracers[f'source_{i}'], tracers[f'source_{j}'], ell) print(f"Computing theory xip/m ({i},{j})") theory[(XIP, i, j)] = theta, pyccl.correlation(cosmo, ell, cl, theta / 60, 'L+') theory[(XIM, i, j)] = theta, pyccl.correlation(cosmo, ell, cl, theta / 60, 'L-') for i in range(nbin_lens): theta, _ = obs[(W, i, i)] print(f"Computing theory w ({i},{i})") cl = pyccl.angular_cl(cosmo, tracers[f'source_{i}'], tracers[f'source_{j}'], ell) theory[W, i, i] = theta, pyccl.correlation(cosmo, ell, cl, theta / 60, 'GG') for i in range(nbin_source): for j in range(nbin_lens): theta, _ = obs[(GAMMA, i, j)] print(f"Computing theory gamma_t ({i},{j})") cl = pyccl.angular_cl(cosmo, tracers[f'source_{i}'], tracers[f'lens_{j}'], ell) theory[GAMMA, i, j] = theta, pyccl.correlation(cosmo, ell, cl, theta / 60, 'GL') return theory
cltracer2=tracers[j], ell=ell) ell_for_xi = np.concatenate( (np.arange(2, 100, dtype=float), np.logspace(2, np.log10(ell[-1]), 300))) cl_for_xi = ccl.angular_cl(cosmo=ccl_cosmo, cltracer1=tracers[i], cltracer2=tracers[j], ell=ell_for_xi) cl_for_xi /= ((ell_for_xi - 1) * ell_for_xi * (ell_for_xi + 1) * (ell_for_xi + 2) / (ell_for_xi + 1 / 2)**4) xip = ccl.correlation(cosmo=ccl_cosmo, ell=ell_for_xi, C_ell=cl_for_xi, theta=theta, corr_type="L+") xim = ccl.correlation(cosmo=ccl_cosmo, ell=ell_for_xi, C_ell=cl_for_xi, theta=theta, corr_type="L-") ccl_xi[i][j] = xip, xim print("Plotting Cls") ell_plot_lim = 10, 5000 fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True,
def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir): ''' Loop over magnitude cuts and make plots ''' catalog_data = self.load_catalog_data( catalog_instance=catalog_instance, requested_columns=self.requested_columns, test_samples=self.test_samples) if not catalog_data: return TestResult(skipped=True, summary='Missing requested quantities') # Initialize catalog's cosmology cosmo = ccl.Cosmology( Omega_c=catalog_instance.cosmology.Om0 - catalog_instance.cosmology.Ob0, Omega_b=catalog_instance.cosmology.Ob0, h=catalog_instance.cosmology.h, sigma8=0.8, # For now let's assume a value for 0.8 n_s=0.96 #We assume this value for the scalar index ) rand_cat, rr = self.generate_processed_randoms(catalog_data) correlation_data = dict() nz_data = dict() correlation_theory = dict() best_fit_bias = [] z_mean = [] for sample_name, sample_conditions in self.test_samples.items(): tmp_catalog_data = self.create_test_sample(catalog_data, sample_conditions) with open(os.path.join(output_dir, 'galaxy_count.dat'), 'a') as f: f.write('{} {}\n'.format(sample_name, len(tmp_catalog_data['ra']))) if not len(tmp_catalog_data['ra']): continue z_mean.append(np.mean(tmp_catalog_data['redshift'])) output_treecorr_filepath = os.path.join( output_dir, self.output_filename_template.format(sample_name)) xi_rad, xi, xi_sig = self.run_treecorr( catalog_data=tmp_catalog_data, treecorr_rand_cat=rand_cat, rr=rr, output_file_name=output_treecorr_filepath) correlation_data[sample_name] = (xi_rad, xi, xi_sig) nz, be = np.histogram(tmp_catalog_data['redshift'], range=(0, 2), bins=100) zcent = 0.5 * (be[1:] + be[:-1]) nz_data[sample_name] = (zcent, nz * 1.0) # Generate CCL tracer object to compute Cls -> w(theta) tracer = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(zcent, nz), bias=(zcent, np.ones_like(zcent))) ells = np.arange(0, self.ell_max) # Reduce ell_max to speed-up cls = ccl.angular_cl(cosmo, tracer, tracer, ells) w_th = ccl.correlation(cosmo, ells, cls, xi_rad) angles = (xi_rad > self.fit_range[sample_name]['min_theta']) & \ (xi_rad < self.fit_range[sample_name]['max_theta']) # Select the fitting range result = op.minimize(neglnlike, [1.0], args=(w_th[angles], xi[angles], xi_sig[angles]), bounds=[(0.1, 10)]) best_bias = result['x'] correlation_theory[sample_name] = best_bias**2 * w_th best_fit_bias.append(best_bias) z_mean = np.array(z_mean) best_fit_bias = np.array(best_fit_bias) self.plot_bias_results(corr_data=correlation_data, catalog_name=catalog_name, corr_theory=correlation_theory, bias=best_fit_bias, z=z_mean, output_dir=output_dir) passed = np.all((best_fit_bias[1:] - best_fit_bias[:-1]) > 0) score = np.count_nonzero( (best_fit_bias[:-1] - best_fit_bias[1:]) > 0) * 1.0 / ( len(best_fit_bias) - 1.0) return TestResult( score=score, passed=passed, summary="Resulting linear bias obtained from the 2pcf")
ell_max = np.max([10000, 3 * args.nside]) ells = np.arange(1, ell_max) for i in range(0, args.nzbins): dndz, be = np.histogram(data['redshift'][redshift_cuts[i]], bins=100, range=(0, 3)) #plt.plot(0.5*(be[1:]+be[:-1]), dndz*1.0) tracer = ccl.NumberCountsTracer(cosmo, True, dndz=(0.5 * be[:1] + 0.5 * be[:-1], dndz * 1.0), bias=(0.5 * be[:1] + 0.5 * be[:-1], np.ones(100))) cls_th.append(ccl.angular_cl(cosmo, tracer, tracer, ells)) w_th.append(ccl.correlation(cosmo, ells, cls_th[i], theta)) if args.debug and i == 0: plt.figure() plt.loglog(theta, w_th[i]) plt.xlabel(r'$\theta$ [deg]') plt.ylabel(r'$w(\theta)$') plt.show() w_out = {} print('Computing ACF') for i in range(0, args.nzbins): dd = tc.NNCorrelation(min_sep=0.01, max_sep=10, nbins=50, metric='Arc', sep_units='deg',
def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir): ''' Loop over magnitude cuts and make plots ''' catalog_data = self.load_catalog_data(catalog_instance=catalog_instance, requested_columns=self.requested_columns, test_samples=self.test_samples) if not catalog_data: return TestResult(skipped=True, summary='Missing requested quantities') if self.truncate_cat_name: catalog_name = catalog_name.partition("_")[0] # Initialize catalog's cosmology cosmo = ccl.Cosmology(Omega_c=catalog_instance.cosmology.Om0-catalog_instance.cosmology.Ob0, Omega_b=catalog_instance.cosmology.Ob0, h=catalog_instance.cosmology.h, sigma8=0.8, # For now let's assume a value for 0.8 n_s=0.96 #We assume this value for the scalar index ) rand_cat, rr = self.generate_processed_randoms(catalog_data) correlation_data = dict() nz_data = dict() correlation_theory = dict() best_fit_bias = [] z_mean = [] best_fit_err = [] for sample_name, sample_conditions in self.test_samples.items(): tmp_catalog_data = self.create_test_sample( catalog_data, sample_conditions) with open(os.path.join(output_dir, 'galaxy_count.dat'), 'a') as f: f.write('{} {}\n'.format(sample_name, len(tmp_catalog_data['ra']))) if not len(tmp_catalog_data['ra']): continue z_mean.append(np.mean(tmp_catalog_data['redshift'])) output_treecorr_filepath = os.path.join(output_dir, self.output_filename_template.format(sample_name)) xi_rad, xi, xi_sig = self.run_treecorr( catalog_data=tmp_catalog_data, treecorr_rand_cat=rand_cat, rr=rr, output_file_name=output_treecorr_filepath) correlation_data[sample_name] = (xi_rad, xi, xi_sig) nz, be = np.histogram(tmp_catalog_data['redshift'], range=(0, 2), bins=100) zcent = 0.5*(be[1:]+be[:-1]) nz_data[sample_name] = (zcent, nz*1.0) # Generate CCL tracer object to compute Cls -> w(theta) tracer = ccl.NumberCountsTracer(cosmo, has_rsd=False, dndz=(zcent, nz), bias=(zcent, np.ones_like(zcent))) ells = np.arange(0, self.ell_max) # Reduce ell_max to speed-up cls = ccl.angular_cl(cosmo, tracer, tracer, ells) w_th = ccl.correlation(cosmo, ells, cls, xi_rad) angles = (xi_rad > self.fit_range[sample_name]['min_theta']) & \ (xi_rad < self.fit_range[sample_name]['max_theta']) # Select the fitting range result = op.minimize(neglnlike, [1.0], args=(w_th[angles], xi[angles], xi_sig[angles]), bounds=[(0.1, 10)]) best_bias = result['x'] #extract covariance matrix #use curve_fit to get error on fit which has documented normalization for covariance matrix cfit = op.curve_fit(wtheta, w_th[angles], xi[angles], p0=1.0, sigma=xi_sig[angles], bounds=(0.1, 10)) #best_bias_obj = result.hess_inv*np.identity(1)[0] #unknown relative normalization best_bias_err = np.sqrt(cfit[1][0][0]) correlation_theory[sample_name] = best_bias**2*w_th best_fit_bias.append(best_bias[0]) best_fit_err.append(best_bias_err) print(sample_name, best_fit_bias, w_th[angles], xi[angles], xi_sig[angles]) z_mean = np.array(z_mean) best_fit_bias = np.array(best_fit_bias) best_fit_err = np.array(best_fit_err) chi_2 = [] # compute chi*2 between best_fit bias and validation data for v in self.validation_data.values(): colz = v['colnames'][0] colb = 'bias' validation_data = np.interp(z_mean, v[colz], v[colb]) if 'b_lo'in v['colnames'] and 'b_hi' in v['colnames']: val_err_lo = np.abs(np.interp(z_mean, v[colz], v['b_lo']) - validation_data) val_err_hi = np.abs(np.interp(z_mean, v[colz], v['b_hi']) - validation_data) print(val_err_lo, val_err_hi) val_err = (val_err_lo + val_err_hi)/2 # mean of upper and lower errors error_sq = best_fit_err**2 + val_err**2 # sum in quadrature print(val_err, best_fit_err, error_sq) else: error_sq = best_fit_err**2 chi__2 = np.sum((best_fit_bias - validation_data)**2/error_sq/len(best_fit_bias)) print('\nchi**2(linear bias - bias data)={:.3g}'.format(chi__2)) chi_2.append(chi__2) # get mag_cut for plot mag_label = '' if 'mag' in self.requested_columns.keys(): filt = self.requested_columns['mag'][0].split('_')[1] mag_vals = self.test_samples[list(self.test_samples.keys())[0]]['mag'] # assume all cuts the same #mag_label = '{:.2g} < {}'.format(mag_vals['min'], filt) if 'min' in mag_vals.keys() else filt mag_label = filt + ' < {:.3g}'.format(mag_vals['max']) self.plot_bias_results(corr_data=correlation_data, catalog_name=catalog_name, corr_theory=correlation_theory, bias=best_fit_bias, z=z_mean, mag_label=mag_label, output_dir=output_dir, err=best_fit_err, chisq=chi_2) passed = np.all((best_fit_bias[1:]-best_fit_bias[:-1]) > 0) score = np.count_nonzero((best_fit_bias[:-1]-best_fit_bias[1:])>0)*1.0/(len(best_fit_bias)-1.0) return TestResult(score=score, passed=passed, summary="Resulting linear bias obtained from the 2pcf")
cosmo = ccl.Cosmology(Omega_c=vrs.Omega_c, Omega_b=vrs.Omega_b, h=vrs.h, sigma8=vrs.sigma8, n_s=vrs.n_s) #Tracers lens = np.array([ccl.ClTracerLensing(cosmo, False, z=z.astype(np.float64), n=pz[x].astype(np.float64)) for x in range(n_bins)]) #Cl's ell = np.arange(n_ells) cls = np.zeros((n_bins, n_bins, n_ells)) for count1 in range(n_bins): for count2 in range(n_bins): cls[count1,count2] = ccl.angular_cl(cosmo, lens[count1], lens[count2], ell) cls = np.transpose(cls,axes=[2,0,1]) #Correlation function xi_th = np.zeros((2, n_bins, n_bins, n_theta)) for count1 in range(n_bins): for count2 in range(n_bins): for count3 in range(n_theta): xi_th[0,count1,count2,count3] = ccl.correlation(cosmo, ell, cls[:,count1,count2], theta[count3], corr_type='L+', method='FFTLog') xi_th[1,count1,count2,count3] = ccl.correlation(cosmo, ell, cls[:,count1,count2], theta[count3], corr_type='L-', method='FFTLog') xi_th = np.transpose(xi_th,axes=[0,3,1,2]) check_symm(xi_th, (0, 1, 3, 2)) print 'Calculated theory correlation function' sys.stdout.flush() #Mask mask_p = np.array([x not in vrs.NEGLECT_THETA_PLUS for x in range(n_theta)]) mask_m = np.array([x not in vrs.NEGLECT_THETA_MINUS for x in range(n_theta)]) mask = np.hstack((mask_p,mask_m)) n_data = len(mask[mask])*n_bins*(n_bins+1)/2 #Compute the chi^2 #Reshape and Flatten
def test_no_sys_pipeline(plot=True): param_dict = {"cosmological_parameters" : {"omch2" : 0.1, "ombh2" : 0.022, "h0" : 0.7, "n_s" : 0.96, "ln_1e10_A_s" : 3.0, "omega_k" : 0.0, "w" : -1.0, "mnu" : 0.06}, "halo_model_parameters" : {"A" : 2.0,}} pipeline_ini = cosmosis.runtime.config.Inifile(PIPELINE_FILE) values_ini = cosmosis.runtime.config.Inifile(None) values_ini.read_dict(param_dict) pipeline = cosmosis.runtime.pipeline.LikelihoodPipeline(pipeline_ini, values=values_ini) data = pipeline.run_parameters([]) ccl_cosmo = ccl.Cosmology(Omega_c=data["cosmological_parameters", "omega_c"], Omega_b=data["cosmological_parameters", "omega_b"], Omega_k=data["cosmological_parameters", "omega_k"], h=data["cosmological_parameters", "h0"], n_s=data["cosmological_parameters", "n_s"], A_s=data["cosmological_parameters", "a_s"], Neff=data["cosmological_parameters", "n_eff"], m_nu=data["cosmological_parameters", "mnu"], w0=data["cosmological_parameters", "w"], ) print("Loading n(z) and creating CCL tracers.") nofz_files = pipeline_ini.get("load_nz", "filepath").split(" ") tracers = [] for nofz_file in nofz_files: z, nofz = np.loadtxt(nofz_file, unpack=True) z += (z[1]-z[0])/2 tracers.append(ccl.WeakLensingTracer(cosmo=ccl_cosmo, dndz=(z, nofz))) n_tomo_bin = len(tracers) print("Comparing P(k) at z=0") h = data["cosmological_parameters", "h0"] k_lin = data["matter_power_lin", "k_h"] k_nl = data["matter_power_nl", "k_h"] pk_lin_ccl = ccl.linear_matter_power(ccl_cosmo, k_lin*h, 1.0)*h**3 pk_nl_ccl = ccl.nonlin_matter_power(ccl_cosmo, k_nl*h, 1.0)*h**3 frac_diff_pk_lin = data["matter_power_lin", "p_k"][0]/pk_lin_ccl - 1 frac_diff_pk_nl = data["matter_power_nl", "p_k"][0]/pk_nl_ccl - 1 print(f"Maximum fractional difference in linear P(k) at z=0 for k<5 h/Mpc: {max(np.abs(frac_diff_pk_lin[k_lin < 5.0]))}") print(f"Maximum fractional difference in non-linear P(k) at z=0 for k<5 h/Mpc: {max(np.abs(frac_diff_pk_nl[k_nl < 5.0]))}") ell = data["shear_cl", "ell"] theta = data["shear_xi_plus", "theta"]*180/pi print("Running CCL for Cls and xis.") ccl_cl = {} ccl_xi = {} for i in range(n_tomo_bin): ccl_cl[i] = {} ccl_xi[i] = {} for j in range(i+1): print(f"Bin {i+1}-{j+1}") ccl_cl[i][j] = ccl.angular_cl(cosmo=ccl_cosmo, cltracer1=tracers[i], cltracer2=tracers[j], ell=ell) ell_for_xi = np.concatenate((np.arange(2,100, dtype=float), np.logspace(2,np.log10(ell[-1]), 300))) cl_for_xi = ccl.angular_cl(cosmo=ccl_cosmo, cltracer1=tracers[i], cltracer2=tracers[j], ell=ell_for_xi) cl_for_xi /= ((ell_for_xi-1)*ell_for_xi*(ell_for_xi+1)*(ell_for_xi+2)/(ell_for_xi+1/2)**4) xip = ccl.correlation(cosmo=ccl_cosmo, ell=ell_for_xi, C_ell=cl_for_xi, theta=theta, corr_type="L+") xim = ccl.correlation(cosmo=ccl_cosmo, ell=ell_for_xi, C_ell=cl_for_xi, theta=theta, corr_type="L-") ccl_xi[i][j] = xip, xim ell_range = 10, 10000 theta_range = 0.05, 10.0 ell_mask = (ell_range[0] < ell) & (ell < ell_range[1]) theta_mask = (theta_range[0] < theta) & (theta < theta_range[1]) for i in range(n_tomo_bin): for j in range(i+1): frac_diff_cl = data["shear_cl", f"bin_{i+1}_{j+1}"]/ccl_cl[i][j] - 1 print(f"Maximum fractional difference in Cl (ell>10) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_cl[ell_mask]))}") frac_diff_xi_plus = data["shear_xi_plus", f"bin_{i+1}_{j+1}"]/ccl_xi[i][j][0] - 1 print(f"Maximum fractional difference in xi_p (theta<10 deg) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_xi_plus[theta_mask]))}") frac_diff_xi_minus = data["shear_xi_minus", f"bin_{i+1}_{j+1}"]/ccl_xi[i][j][1] - 1 print(f"Maximum fractional difference in xi_m (theta>0.05 deg) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_xi_plus[theta_mask]))}") if plot: import matplotlib.pyplot as plt print("Plotting P(k)") fig, ax = plt.subplots(2, 1, sharex=True, sharey=False, figsize=(5, 5)) fig.subplots_adjust(hspace=0, wspace=0) ax[0].loglog(k_lin, pk_lin_ccl, ls="--", label=f"CCL linear P(k)") ax[0].loglog(k_lin, data["matter_power_lin", "p_k"][0], ls="--", label=f"CosmoSIS linear P(k)") ax[0].loglog(k_nl, pk_nl_ccl, label=f"CCL non-linear P(k)") ax[0].loglog(k_nl, data["matter_power_nl", "p_k"][0], label=f"CosmoSIS non-linear P(k)") ax[1].semilogx(k_lin, data["matter_power_lin", "p_k"][0]/pk_lin_ccl-1, ls="--") ax[1].semilogx(k_nl, data["matter_power_nl", "p_k"][0]/pk_nl_ccl-1) ax[0].legend(frameon=False) ax[0].set_ylabel("$P(k)$ [$h^{-3}$ Mpc$^{3}$]") ax[1].set_ylim(-0.05, 0.05) ax[1].set_xlabel("$k$ [$h$ Mpc$^{-1}$]") ax[1].set_ylabel("$\Delta P(k)/P(k)$ [$h$ Mpc$^{-1}$]") fig.suptitle("KCAP vs CCL, P(k)") # fig.savefig("KV450_pofk_kcap_vs_ccl.pdf") print("Plotting Cls") ell_plot_lim = ell_range fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2*n_tomo_bin, 1.5*n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) u = ell**2 for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: ax[i][j].loglog(ell, u*ccl_cl[i][j], label=f"CCL bin {i+1}-{j+1}") ax[i][j].loglog(ell, u*data["shear_cl", f"bin_{i+1}_{j+1}"], label=f"CosmoSIS") ax[i][j].legend(fontsize="small", frameon=False) ax[i][j].set_xlim(*ell_plot_lim) for p in ax[-1]: p.set_xlabel(r"$\ell$") for p in ax: p[0].set_ylabel(r"$\ell^2\ C_\ell$") fig.suptitle("KCAP vs CCL, Cls") #fig.savefig("KV450_Cl_kcap_vs_ccl.pdf") print("Plotting Cl fractional differences.") fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2*n_tomo_bin, 1.5*n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: ax[i][j].semilogx(ell, data["shear_cl", f"bin_{i+1}_{j+1}"]/ccl_cl[i][j]-1, label=f"bin {i+1}-{j+1}") ax[i][j].legend(fontsize="small", frameon=False) ax[i][j].set_xlim(*ell_plot_lim) ax[i][j].set_ylim(-0.1, 0.1) for p in ax[-1]: p.set_xlabel(r"$\ell$") for p in ax: p[0].set_ylabel(r"$|\Delta C_\ell|/C_\ell$") fig.suptitle("KCAP vs CCL, Cls") #fig.savefig("KV450_Cl_kcap_vs_ccl_frac_diff.pdf") print("Plotting xi fractional differences.") theta_plot_lim = theta_range fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2*n_tomo_bin, 1.5*n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: ax[i][j].semilogx(theta, data["shear_xi_plus", f"bin_{i+1}_{j+1}"]/ccl_xi[i][j][0]-1, label=f"xip bin {i+1}-{j+1}") ax[i][j].semilogx(theta, data["shear_xi_minus", f"bin_{i+1}_{j+1}"]/ccl_xi[i][j][1]-1, label=f"xim bin {i+1}-{j+1}") ax[i][j].legend(fontsize="small", frameon=False) ax[i][j].set_xlim(*theta_plot_lim) ax[i][j].set_ylim(-0.1, 0.1) for p in ax[-1]: p.set_xlabel(r"$\theta$ [deg]") for p in ax: p[0].set_ylabel(r"$|\Delta \xi|/\xi$") fig.suptitle("KCAP vs CCL, xis") #fig.savefig("KV450_xi_kcap_vs_ccl_frac_diff.pdf") plt.show()
def execute(block, config): sample_a, sigma_a, sample_b, sigma_b, pimax, nu, input_name, output_name, ell_max, nell, transform, do_lensing, do_magnification, use_precomputed_cls, cl_dict, Sz_interpolator, Xlens = config H0 = block['cosmological_parameters', 'h0']*100 h0 = block['cosmological_parameters', 'h0'] omega_m = block['cosmological_parameters', 'omega_m'] omega_b = block['cosmological_parameters', 'omega_b'] omega_de = block['cosmological_parameters', 'omega_lambda'] sigma_8 = 0.8234379064365687 # this is hard coded for now... ns = block['cosmological_parameters', 'n_s'] cosmology = Cosmology(Omega_c=omega_m-omega_b, Omega_b=omega_b, h=h0, sigma8=sigma_8, n_s=ns, matter_power_spectrum='halofit', transfer_function='boltzmann_camb') # choose a set of bins for line-of-sight separation npi = 20 nzm = 50 Pi = np.hstack((np.linspace(-500,0,npi), np.linspace(0,500,npi)[1:] ))# h^-1 Mpc npi = len(Pi) Zm = np.linspace(0.05,2.15,nzm) z_distance = block['distances', 'z'] chi_distance = block['distances', 'd_m'] a_distance = 1./(1+z_distance) chi_of_z_spline = interp1d(z_distance, chi_distance) ell = np.logspace(-1,np.log10(8000000),100) cl_vec = np.zeros((nzm, npi, len(ell))) print('initialising arrays...') x1 = np.linspace(0,3,100) X = chi_of_z_spline(x1) az = 1./(1+x1) if not use_precomputed_cls: P_flat = load_power_all(block, input_name, chi_of_z_spline, ell, X, do_lensing, do_magnification) # first bit: Limber integrals print('Starting loop') #import pdb ; pdb.set_trace() for i, zm in enumerate(Zm): for j,p in enumerate(Pi): # coordinate transform Hz = 100 * np.sqrt(omega_m*(1+zm)**3 + omega_de) # no h because Pi is in units h^-1 Mpc z1 = zm - (0.5/clight * Hz * p) z2 = zm + (0.5/clight * Hz * p) if (z1<0) or (z2<0) : continue if use_precomputed_cls: Cell = extract_cls(cl_dict, block, input_name, do_lensing, do_magnification, z1, z2, sample_a, sample_b) #get_cls_from_file(block, input_name, do_lensing, do_magnification, z1, z2, sample_a, sample_b) else: # evaluate the per-galaxy PDFs at z1 and z2 x1,pz1 = choose_pdf(z1, sigma=sigma_a, interpolator=Sz_interpolator) #gaussian(z1, sigma=sigma_a) pz1 /=np.trapz(pz1,X) #pz1.max() x2,pz2 = choose_pdf(z2, sigma=sigma_b, interpolator=Sz_interpolator) #gaussian(z2, sigma=sigma_b) pz2 /= np.trapz(pz2,X) Cell = coeff(block, sample_a, sample_b, input_name) * do_limber_integral(ell, P_flat[input_name], pz1, pz2, X) Cell += get_lensing_terms(block, input_name, do_lensing, do_magnification, ell, P_flat, pz1, pz2, X, chi_of_z_spline(z1), chi_of_z_spline(z2), z1, z2, az, sample_a, sample_b) #import pdb ; pdb.set_trace() cl_vec[i,j,:] = Cell # import pdb ; pdb.set_trace() #print(i,j) #import pdb ; pdb.set_trace() # Next do the Hankel transform xi_vec = np.zeros_like(cl_vec)-9999. rp_vec = np.logspace(np.log10(0.01), np.log10(500), xi_vec.shape[2]) theta = 2*np.pi/np.flipud(ell) * (180/np.pi) print('Hankel transform...') for i, zm in enumerate(Zm): x0 = chi_of_z_spline(zm) # do the coordinate transform to convert theta to rp at given redshift theta_radians = rp_vec/x0 theta_degrees = theta_radians * 180./np.pi for j,p in enumerate(Pi): # select a Cell, at fixed Pi, zm, and Hankel transform it j_flipped = len(Pi)-1-j if not (xi_vec[i,j,:][0]==-9999.): continue C = cl_vec[i,j,:] #import pdb ; pdb.set_trace() if (nu==0): # import pdb ; pdb.set_trace() #xi = - (np.pi/np.sqrt(1.04)*np.sqrt(np.pi/2))/1.77 * correlation(cosmology, ell, C, theta_degrees, type='NG', method='FFTLog') if (abs(C)<1e-10).all(): xi = np.zeros(len(ell)) else: xi = -Xlens * (np.pi/np.sqrt(1.04) * np.sqrt(np.pi/2))/1.77 * correlation(cosmology, ell, C, theta_degrees, type='NG', method='FFTLog') #rp, xi = transform.projected_correlation(ell, C, j_nu=2, taper=True) #xi = 10**interp1d(np.log10(rp), np.log10(-xi))(np.log10(rp_vec)) elif (nu==1): if (abs(C)<1e-40).all(): xi = np.zeros(len(ell)) else: xi = (np.pi/2) * np.sqrt(1.02)* np.sqrt(2.2) * correlation(cosmology, ell, C, theta_degrees, type='NN', method='FFTLog') elif (nu==2): if (abs(C)<1e-40).all(): xi = np.zeros(len(ell)) else: xi_0 = correlation(cosmology, ell, C, theta_degrees, type='GG+', method='FFTLog') xi_4 = correlation(cosmology, ell, C, theta_degrees, type='GG-', method='FFTLog') #xi = (1./2/1.08/np.sqrt(np.pi/2))*(xi_0 + xi_4) #/np.sqrt(2) #xi = (1./1.08/np.sqrt(2.*np.pi))*(xi_0 + xi_4) #; import pdb ; pdb.set_trace() xi = (xi_0 + xi_4)/np.pi/np.sqrt(2) #1.0279*np.sqrt(2)*(xi_0 + xi_4)/np.pi #xi = (xi_0 + xi_4)/2/np.pi #(np.pi/2/1.08)*(xi_0 + xi_4) xi_vec[i,j,:] = xi xi_vec[i,j_flipped,:] = xi # by symmetry #if (p==0) & (i==10): #if sum(xi)!=0.: # import pdb ; pdb.set_trace() #rp_vec*=h0 print('saving') # import pdb ; pdb.set_trace() xi_vec[np.isnan(xi_vec)]=0. xi_vec[np.isinf(xi_vec)]=0. # integrate over line of sight separation mask = ((Pi<pimax) & (Pi>-pimax)) print('pi_max=%3.3f'%pimax) xi_pi_rp = np.trapz(xi_vec[:,mask,:], Pi[mask], axis=1) # and then over redshift za, W = get_redshift_kernel(block, 0, 0, x1, X, sample_a, sample_b) #W[np.isnan(W)] = 0 #import pdb ; pdb.set_trace() Wofz = interp1d(W,za) K = np.array([Wofz(Zm)]*len(rp_vec)).T #K = np.array([W]*len(rp_vec)).T w_rp = np.trapz(xi_pi_rp*K, Zm, axis=0)/np.trapz(K, Zm, axis=0) #bg = block['bias_parameters','b_%s'%sample_a] #w_rp*=bg #import pdb ; pdb.set_trace() block.put_double_array_1d(output_name, 'w_rp_1_1_%s_%s'%(sample_a,sample_b), w_rp) block[output_name, 'r_p'] = rp_vec return 0
def test_no_sys_pipeline(plot=True): param_dict = { "cosmological_parameters": { "omch2": 0.1, "ombh2": 0.022, "h0": 0.7, "n_s": 0.96, "ln_1e10_A_s": 3.0, "omega_k": 0.0, "w": -1.0, "mnu": 0.06 }, "halo_model_parameters": { "A": 2.0, } } pipeline_ini = cosmosis.runtime.config.Inifile(PIPELINE_FILE) values_ini = cosmosis.runtime.config.Inifile(None) values_ini.read_dict(param_dict) pipeline = cosmosis.runtime.pipeline.LikelihoodPipeline(pipeline_ini, values=values_ini) data = pipeline.run_parameters([]) ccl_cosmo = ccl.Cosmology( Omega_c=data["cosmological_parameters", "omega_c"], Omega_b=data["cosmological_parameters", "omega_b"], Omega_k=data["cosmological_parameters", "omega_k"], h=data["cosmological_parameters", "h0"], n_s=data["cosmological_parameters", "n_s"], A_s=data["cosmological_parameters", "a_s"], Neff=data["cosmological_parameters", "n_eff"], m_nu=data["cosmological_parameters", "mnu"], w0=data["cosmological_parameters", "w"], ) print("Loading n(z) and creating CCL tracers.") nofz_files = pipeline_ini.get("load_nz", "filepath").split(" ") tracers = [] for nofz_file in nofz_files: z, nofz = np.loadtxt(nofz_file, unpack=True) z += (z[1] - z[0]) / 2 tracers.append(ccl.WeakLensingTracer(cosmo=ccl_cosmo, dndz=(z, nofz))) n_tomo_bin = len(tracers) print("Comparing P(k) at z=0") h = data["cosmological_parameters", "h0"] k_lin = data["matter_power_lin", "k_h"] k_nl = data["matter_power_nl", "k_h"] pk_lin_ccl = ccl.linear_matter_power(ccl_cosmo, k_lin * h, 1.0) * h**3 pk_nl_ccl = ccl.nonlin_matter_power(ccl_cosmo, k_nl * h, 1.0) * h**3 frac_diff_pk_lin = data["matter_power_lin", "p_k"][0] / pk_lin_ccl - 1 frac_diff_pk_nl = data["matter_power_nl", "p_k"][0] / pk_nl_ccl - 1 print( f"Maximum fractional difference in linear P(k) at z=0 for k<5 h/Mpc: {max(np.abs(frac_diff_pk_lin[k_lin < 5.0]))}" ) print( f"Maximum fractional difference in non-linear P(k) at z=0 for k<5 h/Mpc: {max(np.abs(frac_diff_pk_nl[k_nl < 5.0]))}" ) ell = data["shear_cl", "ell"] print(ell.min(), ell.max(), len(ell)) print("Setting up MontePython now.") import subprocess statistics_mp = {} for idx, likelihood in enumerate(["K1K_COSEBIs", "K1K_BandPowers"]): set_up_MontePython(likelihood) # we need to set some paths... path_to_param_file = os.path.join( "montepython/", "{:}/INPUT/{:}_Benchmark.param".format(likelihood, likelihood[4:])) path_to_mp_output = os.path.join(PATH_TO_MP_OUTPUT, likelihood) # TODO: unfortunately, MP saves out theory vectors and Cls to folder # from which it reads in its data and NOT to the MCMC output folder... path_to_mp_input = os.path.join(PATH_TO_MP_DATA[likelihood]) # the call to MontePython: cmd = "python {:} run -p {:} -o {:} --conf {:} -N 1".format( os.path.join(PATH_TO_MONTEPYTHON, "montepython/MontePython.py"), path_to_param_file, path_to_mp_output, os.path.join(PATH_TO_MONTEPYTHON, "default.conf")) subprocess.call(cmd, shell=True) # we only need the Cls from one MP likelihood: if idx == 0: print("Loading and re-ordering Cls from MP.") fname = os.path.join(path_to_mp_input, 'Cls_tot.txt') data_mp = np.loadtxt(fname) mp_cl_raw = data_mp[:, 1:] # bring raw Cls from MP into same sorting order: mp_cl_my_sorting = {} idx_unique = 0 for i in range(n_tomo_bin): mp_cl_my_sorting[i] = {} for j in range(i, n_tomo_bin): #print(f"Bin {i+1}-{j+1}") mp_cl_my_sorting[i][j] = mp_cl_raw[:, idx_unique] idx_unique += 1 mp_cl = {} for i in range(n_tomo_bin): mp_cl[i] = {} for j in range(i + 1): print(f"Bin {i+1}-{j+1} = Bin {j+1}-{i+1}") try: mp_cl[i][j] = mp_cl_my_sorting[j][i] except: mp_cl[i][j] = mp_cl_my_sorting[i][j] fname = os.path.join(path_to_mp_input, "{:}_theory.ascii".format(likelihood[4:])) print("Loaded theory vector from MontePython' {:} from: \n {:} \n". format(likelihood, fname)) statistics_mp[likelihood] = np.loadtxt(fname) print(statistics_mp) print(statistics_mp["K1K_COSEBIs"]) theta = data["shear_xi_plus", "theta"] * 180 / pi print("Running CCL for Cls and xis.") ccl_cl = {} ccl_xi = {} for i in range(n_tomo_bin): ccl_cl[i] = {} ccl_xi[i] = {} for j in range(i + 1): print(f"Bin {i+1}-{j+1}") ccl_cl[i][j] = ccl.angular_cl(cosmo=ccl_cosmo, cltracer1=tracers[i], cltracer2=tracers[j], ell=ell) ell_for_xi = np.concatenate( (np.arange(2, 100, dtype=float), np.logspace(2, np.log10(ell[-1]), 300))) cl_for_xi = ccl.angular_cl(cosmo=ccl_cosmo, cltracer1=tracers[i], cltracer2=tracers[j], ell=ell_for_xi) cl_for_xi /= ((ell_for_xi - 1) * ell_for_xi * (ell_for_xi + 1) * (ell_for_xi + 2) / (ell_for_xi + 1 / 2)**4) xip = ccl.correlation(cosmo=ccl_cosmo, ell=ell_for_xi, C_ell=cl_for_xi, theta=theta, corr_type="L+") xim = ccl.correlation(cosmo=ccl_cosmo, ell=ell_for_xi, C_ell=cl_for_xi, theta=theta, corr_type="L-") ccl_xi[i][j] = xip, xim ell_range = 10, 10000 theta_range = 0.05, 10.0 ell_mask = (ell_range[0] < ell) & (ell < ell_range[1]) theta_mask = (theta_range[0] < theta) & (theta < theta_range[1]) for i in range(n_tomo_bin): for j in range(i + 1): frac_diff_cl_KCAP_CCL = data["shear_cl", f"bin_{i+1}_{j+1}"] / ccl_cl[i][j] - 1 frac_diff_cl_KCAP_MP = data["shear_cl", f"bin_{i+1}_{j+1}"] / mp_cl[i][j] - 1 print( f"Maximum fractional difference between KCAP and CCL in Cl (ell>10) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_cl_KCAP_CCL[ell_mask]))}" ) print( f"Maximum fractional difference between KCAP and MP in Cl (ell>10) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_cl_KCAP_MP[ell_mask]))}" ) frac_diff_xi_plus = data["shear_xi_plus", f"bin_{i+1}_{j+1}"] / ccl_xi[i][j][0] - 1 print( f"Maximum fractional difference in xi_p (theta<10 deg) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_xi_plus[theta_mask]))}" ) frac_diff_xi_minus = data["shear_xi_minus", f"bin_{i+1}_{j+1}"] / ccl_xi[i][j][1] - 1 print( f"Maximum fractional difference in xi_m (theta>0.05 deg) for bin {i+1}-{j+1}: {max(np.abs(frac_diff_xi_minus[theta_mask]))}" ) if plot: import matplotlib.pyplot as plt print("Plotting P(k)") fig, ax = plt.subplots(2, 1, sharex=True, sharey=False, figsize=(5, 5)) fig.subplots_adjust(hspace=0, wspace=0) ax[0].loglog(k_lin, pk_lin_ccl, ls="--", label=f"CCL linear P(k)") ax[0].loglog(k_lin, data["matter_power_lin", "p_k"][0], ls="--", label=f"CosmoSIS linear P(k)") ax[0].loglog(k_nl, pk_nl_ccl, label=f"CCL non-linear P(k)") ax[0].loglog(k_nl, data["matter_power_nl", "p_k"][0], label=f"CosmoSIS non-linear P(k)") ax[1].semilogx(k_lin, data["matter_power_lin", "p_k"][0] / pk_lin_ccl - 1, ls="--") ax[1].semilogx(k_nl, data["matter_power_nl", "p_k"][0] / pk_nl_ccl - 1) ax[0].legend(frameon=False) ax[0].set_ylabel("$P(k)$ [$h^{-3}$ Mpc$^{3}$]") ax[1].set_ylim(-0.05, 0.05) ax[1].set_xlabel("$k$ [$h$ Mpc$^{-1}$]") ax[1].set_ylabel("$\Delta P(k)/P(k)$ [$h$ Mpc$^{-1}$]") fig.suptitle("KCAP vs. CCL, P(k)") fig.savefig("KV450_pofk_kcap_vs_ccl.pdf") ell_plot_lim = ell_range print("Plotting Cls") fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2 * n_tomo_bin, 1.5 * n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) u = ell**2 for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: ax[i][j].loglog(ell, u * ccl_cl[i][j], label=f"CCL bin {i+1}-{j+1}") ax[i][j].loglog(ell, u * data["shear_cl", f"bin_{i+1}_{j+1}"], label=f"CosmoSIS") ax[i][j].loglog(ell, u * mp_cl[i][j], label=f"MP") ax[i][j].legend(fontsize="small", frameon=False) ax[i][j].set_xlim(*ell_plot_lim) for p in ax[-1]: p.set_xlabel(r"$\ell$") for p in ax: p[0].set_ylabel(r"$\ell^2\ C_\ell$") fig.suptitle("KCAP vs. CCL vs. MP, Cls") fig.savefig("KV450_Cl_kcap_vs_ccl_vs_mp.pdf") print("Plotting Cl fractional differences.") fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2 * n_tomo_bin, 1.5 * n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: label = f"bin {i+1}-{j+1}" ax[i][j].text(0.75, 0.80, label, horizontalalignment='center', transform=ax[i][j].transAxes) ax[i][j].semilogx( ell, data["shear_cl", f"bin_{i+1}_{j+1}"] / ccl_cl[i][j] - 1, label=f"KCAP vs. CCL") ax[i][j].semilogx( ell, data["shear_cl", f"bin_{i+1}_{j+1}"] / mp_cl[i][j] - 1, label="KCAP vs. MP") #ax[i][j].legend(fontsize="small", frameon=False) ax[i][j].axhline(0., ls='-', color='gray') for val in [0.01, 0.05]: ax[i][j].axhline(val, ls=':', color='gray') ax[i][j].axhline(-val, ls=':', color='gray') ax[i][j].set_xlim(*ell_plot_lim) ax[i][j].set_ylim(-0.1, 0.1) ax[1][1].legend(fontsize="small", frameon=False, bbox_to_anchor=(.95, 1.05)) for p in ax[-1]: p.set_xlabel(r"$\ell$") for p in ax: p[0].set_ylabel(r"$|\Delta C_\ell|/C_\ell$") fig.suptitle("KCAP vs. CCL vs. MP, Cls") fig.savefig("KV450_Cl_kcap_vs_ccl_vs_mp_frac_diff.pdf") print("Plotting xi fractional differences.") theta_plot_lim = theta_range fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2 * n_tomo_bin, 1.5 * n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: ax[i][j].semilogx( theta, data["shear_xi_plus", f"bin_{i+1}_{j+1}"] / ccl_xi[i][j][0] - 1, label=f"xip bin {i+1}-{j+1}") ax[i][j].semilogx( theta, data["shear_xi_minus", f"bin_{i+1}_{j+1}"] / ccl_xi[i][j][1] - 1, label=f"xim bin {i+1}-{j+1}") ax[i][j].legend(fontsize="small", frameon=False) ax[i][j].set_xlim(*theta_plot_lim) ax[i][j].set_ylim(-0.1, 0.1) for p in ax[-1]: p.set_xlabel(r"$\theta$ [deg]") for p in ax: p[0].set_ylabel(r"$|\Delta \xi|/\xi$") fig.suptitle("KCAP vs. CCL, xis") fig.savefig("KV450_xi_kcap_vs_ccl_frac_diff.pdf") print("Plotting COSEBIs fractional differences.") # TODO: this range should be taken from CosmoSIS keywords!!! n_modes = 5 n_tomo_corrs = n_tomo_bin * (n_tomo_bin + 1) // 2 modes = np.arange(1, n_modes + 1) cosebis_mp = statistics_mp["K1K_COSEBIs"].reshape( n_tomo_corrs, n_modes) fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2 * n_tomo_bin, 1.5 * n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) idx_corr = 0 for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: # TODO: add also output from CosmoSIS! #ax[i][j].semilogx(modes, data["cosebis", f"bin_{i+1}_{j+1}"] / cosebis_mp[idx_corr, :] -1, label=f"COSEBIs bin {i+1}-{j+1}") ax[i][j].plot( modes, cosebis_mp[idx_corr, :] / cosebis_mp[idx_corr, :] - 1, label=f"COSEBIs bin {i+1}-{j+1}") ax[i][j].legend(fontsize="small", frameon=False) idx_corr += 1 ax[i][j].set_xlim([0., 6.]) ax[i][j].set_ylim([-0.1, 0.1]) for p in ax[-1]: p.set_xlabel(r"$n$") for p in ax: p[0].set_ylabel(r"$|\Delta E_n|/E_n$") fig.suptitle("KCAP vs. MP, COSEBIs") fig.savefig("KV450_cosebis_kcap_vs_mp_frac_diff.pdf") print("Plotting BandPowers fractional differences.") # TODO: this range should be taken from CosmoSIS keywords!!! n_ell_bins = 8 n_tomo_corrs = n_tomo_bin * (n_tomo_bin + 1) // 2 plot_ell = np.logspace(np.log10(100.), np.log10(1500.), n_ell_bins) bandpowers_mp = statistics_mp["K1K_BandPowers"].reshape( n_tomo_corrs, n_ell_bins) fig, ax = plt.subplots(n_tomo_bin, n_tomo_bin, sharex=True, sharey=True, figsize=(2 * n_tomo_bin, 1.5 * n_tomo_bin)) fig.subplots_adjust(hspace=0, wspace=0) idx_corr = 0 for i in range(n_tomo_bin): for j in range(n_tomo_bin): if j > i: ax[i][j].axis("off") else: # TODO: add also output from CosmoSIS! #ax[i][j].semilogx(modes, data["bandpower", f"bin_{i+1}_{j+1}"] / bandpowers_mp[idx_corr, :] -1, label=f"BandPowers bin {i+1}-{j+1}") ax[i][j].semilogx(plot_ell, bandpowers_mp[idx_corr, :] / bandpowers_mp[idx_corr, :] - 1, label=f"BandPowers bin {i+1}-{j+1}") ax[i][j].legend(fontsize="small", frameon=False) idx_corr += 1 ax[i][j].set_xlim([100., 1500.]) ax[i][j].set_ylim([-0.1, 0.1]) for p in ax[-1]: p.set_xlabel(r"$\ell$") for p in ax: p[0].set_ylabel(r"$|\Delta PeeE|/PeeE $") fig.suptitle("KCAP vs. MP, BandPowers") fig.savefig("MOCK_bandpowers_kcap_vs_mp_frac_diff.pdf") plt.show()
def comp_xipm(self, theta): self.wl_bin_shear = [] nell = len(self.ell) self.theta = theta self.Cl = np.zeros(len(self.ell), dtype={ 'names': ('11', '12', '13', '14', '22', '23', '24', '33', '34', '44'), 'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8') }) self.xip = np.zeros(len(theta), dtype={ 'names': ('11', '12', '13', '14', '22', '23', '24', '33', '34', '44'), 'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8') }) self.xim = np.zeros(len(theta), dtype={ 'names': ('11', '12', '13', '14', '22', '23', '24', '33', '34', '44'), 'formats': ('f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8', 'f8') }) for i in range(4): filtre = (self.redshifts_bias[i] > 0) # je ne sais plus si c'est important que le redshift de l ai soit le meme que # celui de la distribution des galaxies #AI = self.intrinsic_al(self.redshifts[i][filtre], A0=self.A0, eta=self.eta, z0=0.62) AI = self.intrinsic_al(self.redshifts[i], A0=self.A0, eta=self.eta, z0=0.62) with warnings.catch_warnings(): warnings.simplefilter("ignore") self.wl_bin_shear.append( ccl.WeakLensingTracer(self.cosmology, dndz=(self.redshifts_bias[i][filtre], self.nz[i][filtre]), has_shear=True, ia_bias=(self.redshifts[i], AI))) #ia_bias=(self.redshifts[i][filtre], AI))) for i in range(4): for j in range(4): if j >= i: key = "%i%i" % ((i + 1, j + 1)) cl = ccl.angular_cl(self.cosmology, self.wl_bin_shear[i], self.wl_bin_shear[j], self.ell) m_ij = self.multiplicatif_bias(delta_m=self.delta_m) cl *= m_ij self.Cl[key] = cl self.xip[key] = ccl.correlation(self.cosmology, self.ell, self.Cl[key], theta, type='GG+', method='fftlog') self.xim[key] = ccl.correlation(self.cosmology, self.ell, self.Cl[key], theta, type='GG-', method='fftlog')