示例#1
0
def test_p2d_init():
    """
    Test initialization of Pk2D objects
    """

    cosmo = ccl.Cosmology(Omega_c=0.27,
                          Omega_b=0.045,
                          h=0.67,
                          A_s=1e-10,
                          n_s=0.96)

    #If no input
    assert_raises(ValueError, ccl.Pk2D)

    #Input function has incorrect signature
    assert_raises(ValueError, ccl.Pk2D, pkfunc=pk1d)
    ccl.Pk2D(pkfunc=lpk2d, cosmo=cosmo)

    #Input arrays have incorrect sizes
    lkarr = -4. + 6 * np.arange(100) / 99.
    aarr = 0.05 + 0.95 * np.arange(100) / 99.
    pkarr = np.zeros([len(aarr), len(lkarr)])
    assert_raises(ValueError,
                  ccl.Pk2D,
                  a_arr=aarr,
                  lk_arr=lkarr,
                  pk_arr=pkarr[1:])

    #Check all goes well if we initialize things correctly
    psp = ccl.Pk2D(a_arr=aarr, lk_arr=lkarr, pk_arr=pkarr)
    assert_(not np.isnan(psp.eval(1E-2, 0.5, cosmo)))
示例#2
0
def test_pk2d_function():
    """
    Test evaluation of Pk2D objects
    """

    cosmo = ccl.Cosmology(
        Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10, n_s=0.96)

    psp = ccl.Pk2D(pkfunc=lpk2d, cosmo=cosmo)

    # Test at single point
    ktest = 1E-2
    atest = 0.5
    ptrue = pk2d(ktest, atest)
    phere = psp.eval(ktest, atest, cosmo)
    assert_almost_equal(np.fabs(phere/ptrue), 1., 6)
    dphere = psp.eval_dlogpk_dlogk(ktest, atest, cosmo)
    assert_almost_equal(dphere, -1., 6)

    ktest = 1
    atest = 0.5
    ptrue = pk2d(ktest, atest)
    phere = psp.eval(ktest, atest, cosmo)
    assert_almost_equal(np.fabs(phere/ptrue), 1., 6)
    dphere = psp.eval_dlogpk_dlogk(ktest, atest, cosmo)
    assert_almost_equal(dphere, -1., 6)

    # Test at array of points
    ktest = np.logspace(-3, 1, 10)
    ptrue = pk2d(ktest, atest)
    phere = psp.eval(ktest, atest, cosmo)
    assert_allclose(phere, ptrue, rtol=1E-6)
    dphere = psp.eval_dlogpk_dlogk(ktest, atest, cosmo)
    assert_allclose(dphere, -1.*np.ones_like(dphere), 6)

    # Test input is not logarithmic
    psp = ccl.Pk2D(pkfunc=pk2d, is_logp=False, cosmo=cosmo)
    phere = psp.eval(ktest, atest, cosmo)
    assert_allclose(phere, ptrue, rtol=1E-6)
    dphere = psp.eval_dlogpk_dlogk(ktest, atest, cosmo)
    assert_allclose(dphere, -1.*np.ones_like(dphere), 6)

    # Test input is arrays
    karr = np.logspace(-4, 2, 1000)
    aarr = np.linspace(0.01, 1., 100)
    parr = np.array([pk2d(karr, a) for a in aarr])
    psp = ccl.Pk2D(
        a_arr=aarr, lk_arr=np.log(karr), pk_arr=parr, is_logp=False)
    phere = psp.eval(ktest, atest, cosmo)
    assert_allclose(phere, ptrue, rtol=1E-6)
    dphere = psp.eval_dlogpk_dlogk(ktest, atest, cosmo)
    assert_allclose(dphere, -1.*np.ones_like(dphere), 6)
示例#3
0
def test_pk2d_cls():
    """
    Test interplay between Pk2D and the Limber integrator
    """

    cosmo = ccl.Cosmology(Omega_c=0.27,
                          Omega_b=0.045,
                          h=0.67,
                          A_s=1e-10,
                          n_s=0.96)
    z = np.linspace(0., 1., 200)
    n = np.exp(-((z - 0.5) / 0.1)**2)
    lens1 = ccl.WeakLensingTracer(cosmo, (z, n))
    ells = np.arange(2, 10)

    # Check that passing no power spectrum is fine
    cells = ccl.angular_cl(cosmo, lens1, lens1, ells)
    assert all_finite(cells)

    # Check that passing a bogus power spectrum fails as expected
    assert_raises(ValueError,
                  ccl.angular_cl,
                  cosmo,
                  lens1,
                  lens1,
                  ells,
                  p_of_k_a=1)

    # Check that passing a correct power spectrum runs as expected
    psp = ccl.Pk2D(pkfunc=lpk2d, cosmo=cosmo)
    cells = ccl.angular_cl(cosmo, lens1, lens1, ells, p_of_k_a=psp)
    assert all_finite(cells)
示例#4
0
def test_pk2d_init():
    """
    Test initialization of Pk2D objects
    """

    cosmo = ccl.Cosmology(Omega_c=0.27,
                          Omega_b=0.045,
                          h=0.67,
                          A_s=1e-10,
                          n_s=0.96)

    # If no input
    assert_raises(ValueError, ccl.Pk2D)

    # Input function has incorrect signature
    assert_raises(ValueError, ccl.Pk2D, pkfunc=pk1d)
    ccl.Pk2D(pkfunc=lpk2d, cosmo=cosmo)

    # Input function but no cosmo
    assert_raises(ValueError, ccl.Pk2D, pkfunc=lpk2d)

    # Input arrays have incorrect sizes
    lkarr = -4. + 6 * np.arange(100) / 99.
    aarr = 0.05 + 0.95 * np.arange(100) / 99.
    pkarr = np.zeros([len(aarr), len(lkarr)])
    assert_raises(ValueError,
                  ccl.Pk2D,
                  a_arr=aarr,
                  lk_arr=lkarr,
                  pk_arr=pkarr[1:])
示例#5
0
def project_Cl(cosmo, tracer, Pk_a_s, ks, a_s, want_plot=False):
    # number of redshifts
    num_zs = Pk_a_s.shape[0]

    # hubble parameter
    h = cosmo._params_init_kwargs['h']

    # change the order cause that's what CCL prefers
    i_sort = np.argsort(a_s)
    a_s = a_s[i_sort]
    Pk_a_s = Pk_a_s[i_sort, :]
    assert num_zs == len(
        a_s), "Different number of input spectra and redshifts"

    # wave numbers
    ells = np.geomspace(2, 1000, 20)
    '''
    # simple power spectrum for testing
    lpk_array = np.log(np.array([ccl.nonlin_matter_power(cosmo, ks*h, a) for a in a_s]))
    cl_tt = ccl.angular_cl(cosmo, tracer, tracer, ells)

    # generating fake data
    Cl_err = cl_tt*np.sqrt(2./(2*ells+1.))
    cov = np.diag(Cl_err**2)
    np.save("data_power/cl_gg_z%4.3f.npy"%a2z(a_s[0]),cl_tt)
    np.save("data_power/ells.npy",ells)
    np.save("data_power/cov_cl_gg_z%4.3f.npy"%a2z(a_s[0]),cov)
    '''

    # Create a Pk2D object, giving log k in Mpc^-1 and pk_arr in Mpc^3
    #lpk_array = np.log(Pk_a_s/h**3)
    #pk_tmp = ccl.Pk2D(a_arr=a_s, lk_arr=np.log(ks*h), pk_arr=lpk_array, is_logp=True)
    pk_tmp = ccl.Pk2D(a_arr=a_s,
                      lk_arr=np.log(ks * h),
                      pk_arr=Pk_a_s / h**3,
                      is_logp=False)

    # Compute power spectra with and without cutoff
    cl_tt_tmp = ccl.angular_cl(cosmo, tracer, tracer, ells, p_of_k_a=pk_tmp)

    if want_plot:
        # plot the power spectra
        plt.plot(np.log(ks * h), lpk_array[-1, :], label="ccl")
        plt.plot(np.log(ks * h), np.log(Pk_a_s[-1, :] / h**3), label="buba")
        plt.legend()
        plt.show()

        # plot the angular power spectra
        plt.plot(ells, 1E4 * cl_tt, 'r-', label='built-in tracer')
        plt.plot(ells, 1E4 * cl_tt_tmp, 'k--', label='custom tracer')
        plt.xscale('log')
        plt.xlabel('$\\ell$', fontsize=14)
        plt.ylabel('$10^4\\times C_\\ell$', fontsize=14)
        plt.legend(loc='upper right', fontsize=12, frameon=False)
        plt.savefig("figs/Cls.png")
        plt.show()

    return ells, cl_tt_tmp
示例#6
0
def test_pk2d_smoke():
    """Make sure it works once."""
    cosmo = ccl.Cosmology(
        Omega_c=0.27, Omega_b=0.045, h=0.67, A_s=1e-10, n_s=0.96)
    lkarr = -4.+6*np.arange(100)/99.
    aarr = 0.05+0.95*np.arange(100)/99.
    pkarr = np.zeros([len(aarr), len(lkarr)])
    psp = ccl.Pk2D(a_arr=aarr, lk_arr=lkarr, pk_arr=pkarr)
    assert_(not np.isnan(psp.eval(1E-2, 0.5, cosmo)))
示例#7
0
 def get_response_func() :
   zarr=np.array([4,3,2,1,0])
   resp2d=[]
   for iz,z in enumerate(zarr) :
     kresph,_,_,_,resp1d=np.loadtxt("ssc_responses/Response_z%d.txt"%(int(z)),unpack=True)
     resp2d.append(resp1d)
   kresp=kresph*0.67
   aresp=1./(1.+zarr)
   resp2d=np.array(resp2d)
   return ccl.Pk2D(a_arr=aresp,lk_arr=np.log(kresp),pk_arr=resp2d,is_logp=False)
示例#8
0
def test_pk2d_mul_pow():
    x = np.linspace(0.1, 1, 10)
    log_y = np.linspace(-3, 1, 20)
    zarr_a = np.outer(x, np.exp(log_y))
    zarr_b = np.outer(-1*x, 4*np.exp(log_y))

    pk2d_a = ccl.Pk2D(a_arr=x, lk_arr=log_y, pk_arr=np.log(zarr_a),
                      is_logp=True)
    pk2d_b = ccl.Pk2D(a_arr=x, lk_arr=log_y, pk_arr=zarr_b,
                      is_logp=False)

    # This raises an error because multiplication is only defined for
    # float, int, and Pk2D
    with pytest.raises(TypeError):
        pk2d_a*np.array([0.1, 0.2])

    # This raises an error because exponention is only defined for
    # float and int
    with pytest.raises(TypeError):
        pk2d_a**pk2d_b

    # This raises a warning because the power spectrum is non-negative and the
    # power is non-integer
    with pytest.warns(CCLWarning):
        pk2d_b**0.5

    pk2d_g = pk2d_a * pk2d_b
    pk2d_h = 2*pk2d_a
    pk2d_i = pk2d_a**1.8

    _, _, zarr_g = pk2d_g.get_spline_arrays()
    _, _, zarr_h = pk2d_h.get_spline_arrays()
    _, _, zarr_i = pk2d_i.get_spline_arrays()

    assert np.allclose(zarr_a * zarr_b, zarr_g)
    assert np.allclose(2 * zarr_a, zarr_h)
    assert np.allclose(zarr_a**1.8, zarr_i)

    pk2d_j = (pk2d_a + 0.5*pk2d_i)**1.5
    _, _, zarr_j = pk2d_j.get_spline_arrays()
    assert np.allclose((zarr_a + 0.5*zarr_i)**1.5, zarr_j)
示例#9
0
def test_pk2d_parsing():
    a_arr = np.linspace(0.1, 1, 100)
    k_arr = np.geomspace(1E-4, 1E3, 1000)
    pk_arr = a_arr[:, None] * ((k_arr / 0.01) / (1 +
                                                 (k_arr / 0.01)**3))[None, :]

    psp = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_arr))

    cosmo = ccl.CosmologyCalculator(Omega_c=0.27,
                                    Omega_b=0.045,
                                    h=0.67,
                                    sigma8=0.8,
                                    n_s=0.96,
                                    pk_nonlin={
                                        'a': a_arr,
                                        'k': k_arr,
                                        'delta_matter:delta_matter': pk_arr,
                                        'a:b': pk_arr
                                    })
    z = np.linspace(0., 1., 200)
    n = np.exp(-((z - 0.5) / 0.1)**2)
    lens1 = ccl.WeakLensingTracer(cosmo, (z, n))
    ells = np.linspace(2, 100, 10)

    cls1 = ccl.angular_cl(cosmo, lens1, lens1, ells, p_of_k_a=None)
    cls2 = ccl.angular_cl(cosmo,
                          lens1,
                          lens1,
                          ells,
                          p_of_k_a='delta_matter:delta_matter')
    cls3 = ccl.angular_cl(cosmo, lens1, lens1, ells, p_of_k_a='a:b')
    cls4 = ccl.angular_cl(cosmo, lens1, lens1, ells, p_of_k_a=psp)
    assert all_finite(cls1)
    assert all_finite(cls2)
    assert all_finite(cls3)
    assert all_finite(cls4)
    assert np.all(np.fabs(cls2 / cls1 - 1) < 1E-10)
    assert np.all(np.fabs(cls3 / cls1 - 1) < 1E-10)
    assert np.all(np.fabs(cls4 / cls1 - 1) < 1E-10)

    # Wrong name
    with pytest.raises(KeyError):
        ccl.angular_cl(cosmo, lens1, lens1, ells, p_of_k_a='a:c')

    # Wrong type
    with pytest.raises(ValueError):
        ccl.angular_cl(cosmo, lens1, lens1, ells, p_of_k_a=3)
示例#10
0
def test_spline2d():
    x = np.linspace(0.1, 1, 10)
    log_y = np.linspace(-3, 1, 20)
    zarr_in = np.outer(x, np.exp(log_y))

    pk2d = ccl.Pk2D(a_arr=x, lk_arr=log_y, pk_arr=zarr_in, is_logp=False)

    pk2d_gsl_spline2d = pk2d.psp.fka
    xarr, yarr, zarr_out_spline = \
        ccl.pyutils._get_spline2d_arrays(pk2d_gsl_spline2d)

    cosmo = ccl.CosmologyVanillaLCDM()
    zarr_out_eval = pk2d.eval(k=np.exp(log_y), a=x[-1], cosmo=cosmo)

    assert np.allclose(x, xarr)
    assert np.allclose(log_y, yarr)
    assert np.allclose(zarr_in, zarr_out_spline)
    assert np.allclose(zarr_in[-1], zarr_out_eval)
示例#11
0
def test_pk2d_add():
    x = np.linspace(0.1, 1, 10)
    log_y = np.linspace(-3, 1, 20)
    zarr_a = np.outer(x, np.exp(log_y))
    zarr_b = np.outer(-1*x, 4*np.exp(log_y))

    empty_pk2d = ccl.Pk2D(empty=True)
    pk2d_a = ccl.Pk2D(a_arr=x, lk_arr=log_y, pk_arr=np.log(zarr_a),
                      is_logp=True)
    pk2d_b = ccl.Pk2D(a_arr=2*x, lk_arr=log_y, pk_arr=zarr_b,
                      is_logp=False)
    pk2d_b2 = ccl.Pk2D(a_arr=x, lk_arr=log_y+0.5, pk_arr=zarr_b,
                       is_logp=False)

    # This raises an error because the a ranges don't match
    with pytest.raises(ValueError):
        pk2d_a + pk2d_b
    # This raises an error because the k ranges don't match
    with pytest.raises(ValueError):
        pk2d_a + pk2d_b2
    # This raises an error because addition with an empty Pk2D should not work
    with pytest.raises(ValueError):
        pk2d_a + empty_pk2d

    pk2d_c = ccl.Pk2D(a_arr=x, lk_arr=log_y, pk_arr=zarr_b,
                      is_logp=False)

    pk2d_d = pk2d_a + pk2d_c
    pk2d_d2 = pk2d_a + 1.0
    xarr_d, yarr_d, zarr_d = pk2d_d.get_spline_arrays()
    _, _, zarr_d2 = pk2d_d2.get_spline_arrays()

    assert np.allclose(x, xarr_d)
    assert np.allclose(log_y, yarr_d)
    assert np.allclose(zarr_a + zarr_b, zarr_d)
    assert np.allclose(zarr_a + 1.0, zarr_d2)

    pk2d_e = ccl.Pk2D(a_arr=x[1:-1], lk_arr=log_y[1:-1],
                      pk_arr=zarr_b[1:-1, 1:-1],
                      is_logp=False)

    # This raises a warning because the power spectra are not defined on the
    # same support
    with pytest.warns(CCLWarning):
        pk2d_f = pk2d_e + pk2d_a

    xarr_f, yarr_f, zarr_f = pk2d_f.get_spline_arrays()

    assert np.allclose((zarr_a + zarr_b)[1:-1, 1:-1], zarr_f)
示例#12
0
def test_tracers_analytic(set_up, alpha, beta, gamma, is_factorizable,
                          w_transfer, mismatch, der_bessel, der_angles):
    cosmo = set_up
    zmax = 0.8
    zi = 0.4
    zf = 0.6
    nchi = 1024
    nk = 512

    # x arrays
    chii = ccl.comoving_radial_distance(cosmo, 1. / (1 + zi))
    chif = ccl.comoving_radial_distance(cosmo, 1. / (1 + zf))
    chimax = ccl.comoving_radial_distance(cosmo, 1. / (1 + zmax))
    chiarr = np.linspace(0.1, chimax, nchi)
    if mismatch:
        # Remove elements around the edges
        mask = (chiarr < 0.9 * chif) & (chiarr > 1.1 * chii)
        chiarr_transfer = chiarr[mask]
    else:
        chiarr_transfer = chiarr
    aarr_transfer = ccl.scale_factor_of_chi(cosmo, chiarr_transfer)[::-1]
    aarr = ccl.scale_factor_of_chi(cosmo, chiarr)[::-1]
    lkarr = np.log(10.**np.linspace(-6, 3, nk))

    # Kernel
    wchi = np.ones_like(chiarr)
    wchi[chiarr < chii] = 0
    wchi[chiarr > chif] = 0

    # Transfer
    t = ccl.Tracer()
    if w_transfer:
        ta = (chiarr_transfer**gamma)[::-1]
        tk = np.exp(beta * lkarr)
        if is_factorizable:
            # 1D
            t.add_tracer(cosmo,
                         kernel=(chiarr, wchi),
                         transfer_k=(lkarr, tk),
                         transfer_a=(aarr_transfer, ta),
                         der_bessel=der_bessel,
                         der_angles=der_angles)
        else:
            # 2D
            tka = ta[:, None] * tk[None, :]
            t.add_tracer(cosmo,
                         kernel=(chiarr, wchi),
                         transfer_ka=(aarr_transfer, lkarr, tka),
                         der_bessel=der_bessel,
                         der_angles=der_angles)
    else:
        t.add_tracer(cosmo,
                     kernel=(chiarr, wchi),
                     der_bessel=der_bessel,
                     der_angles=der_angles)

    # Power spectrum
    pkarr = np.ones_like(aarr)[:, None] * (np.exp(alpha * lkarr))[None, :]
    pk2d = ccl.Pk2D(a_arr=aarr, lk_arr=lkarr, pk_arr=pkarr, is_logp=False)

    # C_ells
    larr = np.linspace(2, 3000, 100)
    # 1D
    cl = ccl.angular_cl(cosmo, t, t, larr, p_of_k_a=pk2d)
    # Prediction
    clpred = get_prediction(larr, chii, chif, alpha, beta, gamma, der_bessel,
                            der_angles)

    assert np.all(np.fabs(cl / clpred - 1) < 5E-3)
示例#13
0
def compute_Cls(par,z_cent=z_s_cents,N_gal_sample=N_gal_bin,k_arr=k_ar,z_arr=z_ar,a_arr=a_ar,ell=ells,compute_inv_cov=False,plot_for_sanity=False,powerspec='halofit',simple_bias=False):
    # HOD parameters -- don't need to be called every time so can be taken out
    hod_par = {
    'sigm_0': 0.4,
    'sigm_1': 0.,
    'alpha_0': 1.0,
    'alpha_1': 0.,
    'fc_0': 1.,
    'fc_1': 0.,
    'lmmin_0': par['lmmin_0']['val'],
    'lmmin_1': par['lmmin_1']['val'],
    'm0_0': par['m0_0']['val'],
    'm0_1': par['m0_1']['val'],
    'm1_0': par['m1_0']['val'],
    'm1_1': par['m1_1']['val']}
    
    if powerspec == 'halofit':
        # Compute matter pk using halofit -- preferred
        pk_mm_arr = np.array([ccl.nonlin_matter_power(cosmo_fid, k_arr, a) for a in a_arr])
    elif powerspec == 'halomodel':
        # Alternatively use halo model
        pk_mm_arr = np.array([ccl.halomodel.halomodel_matter_power(cosmo_fid, k_arr, a) for a in a_arr])
        
    if simple_bias == True:
        # Using bias parameters
        # Pk_gg = b^2 Pmm and Pk_gm = b Pmm; however, this is taken into account when def tracers
        pk_gg_arr = pk_mm_arr.copy() 
        pk_gm_arr = pk_mm_arr.copy()
        # load bias parameter
        b_z = np.array([par['b_%02d'%k]['val'] for k in range(N_zsamples)])
    else:
        # Using the HOD model
        # load HOD and compute profile
        hodpars = hod_funcs_evol_fit.HODParams(hod_par, islogm0=True, islogm1=True)
        hodprof = hod.HODProfile(cosmo_fid, hodpars.lmminf, hodpars.sigmf,\
                                 hodpars.fcf, hodpars.m0f, hodpars.m1f, hodpars.alphaf)
        # Compute galaxy power spectrum using halofit
        pk_gg_arr = np.array([hodprof.pk(k_arr, a_arr[i]) for i in range(a_arr.shape[0])])
        # Compute galaxy-matter pk using halofit
        pk_gm_arr = np.array([hodprof.pk_gm(k_arr, a_arr[i]) for i in range(a_arr.shape[0])])
        # Here we don't vary the bias parameters
        b_z = np.ones(N_zsamples) 
        
    # Create pk2D objects for interpolation
    pk_mm = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_mm_arr), is_logp=True)
    pk_gg = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_gg_arr), is_logp=True)
    pk_gm = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_gm_arr), is_logp=True)


    # Assume a functional form of 0.95/D(a) for the bias parameters (notice how this plays in
    # the case of varying b(z)) it doesn't matter a lot in this case for we take finite diff
    #a_cent = 1./(1.+z_cent)
    #b_z *= 0.95/ccl.growth_factor(cosmo_fid,a_cent)
    
    # load the dndz parameters
    dndz_z = np.zeros((N_tomo,N_zsamples))
    for i_z in range(N_tomo):
        dndz_z[i_z,:] = np.array([par['dndz_%02d_%02d'%(i_z,k)]['val'] for k in range(N_zsamples)])
    
    # Number of total correlation elements for all of gg gs and ss variants
    tot_corr = N_ell*(N_tomo*(2*N_tomo+1))
    
    # make CL_all of size N_ell*N_tomo*(2N_tomo+1)
    CL_ALL = np.zeros(tot_corr)

    # this lists all possible combinations of all gg gs and ss's for a given number of tomo bins
    # here is how it works: say we have 3 tomo bins, then we make an array of 2x3 = 6 numbers
    # from 0 through 5; where 0 through 2 correspond to galaxy tracer (g) and 3 through 5 to
    # shear tracer (s). 
    # Our final list all_combos consists of every possible pair between
    # tracer 1 which can be any of 3 tomos and either g or s and tracer 2 which can also be
    # any of 3 tomos and either g or s. The way I do this is by first listing in temp
    # the 6 pairs making up all the tracer 1 and tracer 2 combs where they are either
    # both g or both s at the same tomographic redshift, i.e. temp is (0,0), (1,1) ... (5,5)
    # then I use the function combinations which finds all unique combinations between
    # two lists i.e. (0,1),(0,2)...(0,5),(1,2)...(1,5),(2,3)...(2,5),(3,4),(3,5),(4,5)
    # and call this list combs. Finally I combine the two in all_combos.
    # Note: combs does not have (1,0) for it contributes the same info as (0,1)
    # example pair (2,4) in this example corresponds to tracer 1 being galaxies in third tomographic
    # bin and tracer 2 being shear in second tomographic bin, in short (2,4)=(g2,s1)
    temp = np.arange(2*N_tomo)
    temp = np.vstack((temp,temp)).T
    combs = np.array(list(combinations(range(2*N_tomo),2)))
    all_combos = np.vstack((temp,combs))

    
    for c, comb in enumerate(all_combos):
        # comb is the list pair, e.g. (2,5) while c is its order no. (b/n 0 and Ntomo(2Ntomo+1)-1)
        i = comb[0]%N_tomo # redshift bin of tracer 1 (b/n 0 and N_tomo-1 regardless of g or s)
        j = comb[1]%N_tomo # redshift bin of tracer 2 (b/n 0 and N_tomo-1 regardless of g or s)
        t_i = comb[0]//N_tomo # tracer type (0 or 1): 0 means g and 1 means s
        t_j = comb[1]//N_tomo # tracer type (0 or 1): 0 means g and 1 means s
        
        # Adding NOISE to only the diagonal elements
        if (i == j):
            # number density of galaxies - use area cosmos for these are the gals that overlap
            N_gal = N_gal_sample[i]
            n_gal = N_gal/area_COSMOS # in rad^-2
            
            # Adding noise
            noise_gal = 1./n_gal
            noise_shape = sigma_e2[i]/n_gal
        else:
            noise_gal = 0.
            noise_shape = 0.
        
        # Now create corresponding Cls with pk2D objects matched to pk
        if t_i*2+t_j == 0: # this is gg (notice that this can only be 2*0+0 = 0)
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z[j,:]),mag_bias=None, \
                                               has_rsd=False)
            cl_gg = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_gg)
            cl_gg_no = cl_gg + noise_gal

            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_gg_no
        elif t_i*2+t_j == 1: # this is gs (notice that this can only be 2*0+1 = 1, never 2*1+0=2)
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z[j,:]))
            cl_gs = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_gm)
            cl_gs_no = cl_gs

            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_gs_no
            
        elif t_i*2+t_j == 3: # this is ss (notice that this can only be 2*1+1 = 3)
            tracer_z1 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z[i,:]))
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z[j,:]))
            cl_ss = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_mm)
            cl_ss_no = cl_ss + noise_shape
            
            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_ss_no

        # check whether things make sense
        if plot_for_sanity == True:
            if c == 0:
                # Plot for sanity
                plt.loglog(ells, cl_gg, label=r'$\mathrm{gg}$')
                plt.loglog(ells, cl_gg_no, label=r'$\mathrm{gg}+{\rm noise}$')
                plt.loglog(ells, cl_ss, label=r'$\mathrm{ss}$')
                plt.loglog(ells, cl_gs, label=r'$\mathrm{gs}$')
                plt.xlabel(r'$\ell$')
                plt.ylabel(r'$C_{\ell}$')
                plt.legend()
                plt.savefig('Cls.png')
                plt.close()

    # if we don't want the inverse covariance matrix
    if compute_inv_cov == False:
        print(len(CL_ALL))
        return CL_ALL

    # print out numbers to make sure there is internet connection
    print(len(CL_ALL))
    print(np.sum(CL_ALL>0))

    # initiate covariance matrix into the trade
    COV_ALL = np.zeros((len(CL_ALL),len(CL_ALL)))    
    # COMPUTE COVARIANCE MATRIX: Cov(A,B)
    for c_A, comb_A in enumerate(all_combos):
        # remove half the computations
        for c_B, comb_B in enumerate(all_combos):
            # pair A=(ti,tj), pair B=(tm,tn) at same ell, where t stands for either g or s
            i = comb_A[0]%N_tomo # redshift bin of tracer A_1 (i.e. along rows of Cov)
            j = comb_A[1]%N_tomo # redshift bin of tracer A_2 (i.e. along rows of Cov)
            m = comb_B[0]%N_tomo # redshift bin of tracer B_1 (i.e. along columns of Cov)
            n = comb_B[1]%N_tomo # redshift bin of tracer B_2 (i.e. along columns of Cov)

            # These are the pairs (their order number in all_combos) we need for Knox
            # cov(ij,mn) = im,jn + in,jm
            c_im = np.argmax(np.product(np.sort(np.array([comb_A[0],comb_B[0]]))==all_combos,axis=1))
            c_jn = np.argmax(np.product(np.sort(np.array([comb_A[1],comb_B[1]]))==all_combos,axis=1))
            c_in = np.argmax(np.product(np.sort(np.array([comb_A[0],comb_B[1]]))==all_combos,axis=1))
            c_jm = np.argmax(np.product(np.sort(np.array([comb_A[1],comb_B[0]]))==all_combos,axis=1))
            # retrieving their Cls
            C_im = CL_ALL[(c_im*N_ell):(c_im*N_ell)+N_ell]
            C_jn = CL_ALL[(c_jn*N_ell):(c_jn*N_ell)+N_ell]
            C_in = CL_ALL[(c_in*N_ell):(c_in*N_ell)+N_ell]
            C_jm = CL_ALL[(c_jm*N_ell):(c_jm*N_ell)+N_ell]
            
            # Knox formula
            Cov_ijmn = (C_im*C_jn+C_in*C_jm)/((2*ell+1.)*delta_ell*f_sky)
            
            if plot_for_sanity == True:
                if (c_A == c_B):
                    print(i,j,'=',m,n)
                    print(c_A)
                    Cl_err = np.sqrt(Cov_ijmn)

                    t_i = comb_A[0]//N_tomo
                    t_j = comb_A[1]//N_tomo
                    print(N_tomo*i+j+1)
                    
                    plt.subplot(N_tomo, N_tomo, N_tomo*i+j+1)
                    plt.title("z=%f x z=%f"%(z_bin_cents[i],z_bin_cents[j]))
                    c_ij = np.argmax(np.product(np.sort(np.array([comb_A[0],comb_A[1]]))==all_combos,axis=1))
                    C_ij = CL_ALL[(c_ij*N_ell):(c_ij*N_ell)+N_ell]
                    # maybe add legend and make sure colors are the same for each type
                    (_,caps,eb)=plt.errorbar(ell,C_ij,yerr=Cl_err,lw=2.,ls='-',capsize=5,label=str(t_i*2+t_j)) 
                    plt.legend()
                    plt.xscale('log')
                    plt.yscale('log')
                    #plt.ylim([1.e-12,1.e-5])

            # fill in the diagonals of the cov matrix and make use of its symmetry Cov(A,B)=Cov(B,A)
            COV_ALL[(N_ell*c_A):(N_ell*c_A)+\
                    N_ell,(N_ell*c_B):(N_ell*c_B)+N_ell] = np.diag(Cov_ijmn)
            COV_ALL[(N_ell*c_B):(N_ell*c_B)+\
                    N_ell,(N_ell*c_A):(N_ell*c_A)+N_ell] = np.diag(Cov_ijmn)

    # check matrix is positive definite
    if (is_pos_def(COV_ALL) != True): print("Covariance is not positive definite!"); exit(0)
    # compute and return inverse
    ICOV_ALL = la.inv(COV_ALL)
    return ICOV_ALL
示例#14
0
def hm_ang_power_spectrum(cosmo,
                          hmc,
                          l,
                          profiles,
                          include_1h=True,
                          include_2h=True,
                          hm_correction=None,
                          kpts=128,
                          zpts=16,
                          **kwargs):
    """Angular power spectrum using CCL.

    Args:
        cosmo (~pyccl.core.Cosmology): a Cosmology object
        hmc (`~pyccl.halos.halo_model.HMCalculator): halo model calculator
        l (`numpy.array`): effective multipoles to sample
        profiles (tuple of `model.data.ProfTracer`): profile and tracer pair
        include_1h (`bool`): whether to include the 1-halo term
        include_2h (`bool`): whether to include the 2-halo term
        kpts (`int`): number of wavenumber integration points
        zpts (`int`): number of redshift integration points
        **kwagrs: Parametrisation of the profiles and cosmology.

    Returns:
        `numpy.array`: Angular power spectrum of input profiles.
    """
    p1, p2 = profiles
    p1.update_parameters(cosmo, **kwargs)
    p2.update_parameters(cosmo, **kwargs)

    # Set up covariance
    p2pt = get_2pt(p1, p2, **kwargs)

    k_arr = np.logspace(-3, 2, kpts)

    if p1.type == "g":
        zmin, zmax = p1.zrange
    elif p2.type == "g":
        zmin, zmax = p2.zrange
    else:
        zmax = 6.0

    a_arr = np.linspace(1 / (1 + zmax), 1., zpts)

    smooth, supress = None, None
    if hm_correction == "Mead":
        aHM = kwargs.get("a_HM_" + p1.type + p2.type)
        if aHM is not None:
            smooth = lambda a: aHM

        sHM = kwargs.get("s_HM_" + p1.type + p2.type)
        if sHM is not None:
            supress = lambda a: sHM

    pk_arr = ccl.halos.halomod_power_spectrum(cosmo,
                                              hmc,
                                              k_arr,
                                              a_arr,
                                              prof=p1.profile,
                                              prof_2pt=p2pt,
                                              prof2=p2.profile,
                                              normprof=(p1.type != "y"),
                                              normprof2=(p2.type != "y"),
                                              get_1h=include_1h,
                                              get_2h=include_2h,
                                              smooth_transition=smooth,
                                              supress_1h=supress)

    if hm_correction == "HALOFIT":
        pk_arr *= hm_correction(k_arr, a_arr)

    pk = ccl.Pk2D(a_arr=a_arr,
                  lk_arr=np.log(k_arr),
                  pk_arr=pk_arr,
                  cosmo=cosmo,
                  is_logp=False)

    cl = ccl.angular_cl(cosmo, p1.tracer, p2.tracer, ell=l, p_of_k_a=pk)
    return cl
示例#15
0
    def _get_p_of_k_a(self, emu_spec, cosmo, b_trs, clm):
        # eBOSS, DECALS also gc

        # Initialize power spectrum Pk_a(as, ks)
        Pk_a = np.zeros_like(emu_spec[:, 0, :])

        # If both tracers are galaxies, Pk^{tr1,tr2} = f_i^bin1 * f_j^bin2 * Pk_ij
        if ('gc' in clm['bin_1'] or 'eBOSS' in clm['bin_1']
                or 'DECALS' in clm['bin_1']) and ('gc' in clm['bin_2']
                                                  or 'eBOSS' in clm['bin_2']
                                                  or 'DECALS' in clm['bin_2']):
            bias_eft1 = b_trs[clm['bin_1']]
            bias_eft2 = b_trs[clm['bin_2']]

            for key1 in bias_eft1.keys():
                bias1 = bias_eft1[key1]

                for key2 in bias_eft2.keys():
                    bias2 = bias_eft2[key2]

                    if key1 + '_' + key2 in self.emu_dict.keys():
                        comb = self.emu_dict[key1 + '_' + key2]
                    else:
                        comb = self.emu_dict[key2 + '_' + key1]
                    Pk_a += bias1 * bias2 * emu_spec[:, comb, :]

        # If first tracer is galaxies, Pk^{tr1,tr2} = f_i^bin1 * 1. * Pk_0i
        elif ('gc' in clm['bin_1'] or 'eBOSS' in clm['bin_1']
              or 'DECALS' in clm['bin_1']) and ('wl' in clm['bin_2']
                                                or 'KiDS1000' in clm['bin_2']):
            bias_eft1 = b_trs[clm['bin_1']]

            for key1 in bias_eft1.keys():
                bias1 = bias_eft1[key1]
                comb = self.emu_dict['b0' + '_' + key1]
                Pk_a += bias1 * emu_spec[:, comb, :]

        # If second tracer is galaxies, Pk^{tr1,tr2} = f_j^bin2 * 1. * Pk_0j
        elif ('wl' in clm['bin_1']
              or 'KiDS1000' in clm['bin_1']) and ('gc' in clm['bin_2']
                                                  or 'eBOSS' in clm['bin_2']
                                                  or 'DECALS' in clm['bin_2']):
            bias_eft2 = b_trs[clm['bin_2']]

            for key2 in bias_eft2.keys():
                bias2 = bias_eft2[key2]
                comb = self.emu_dict['b0' + '_' + key2]
                Pk_a += bias2 * emu_spec[:, comb, :]

        # Convert ks from [Mpc/h]^-1 to [Mpc]^-1
        lk_arr = np.log(self.k * cosmo['H0'] / 100.)

        # Same for the power spectrum: convert to Mpc^3
        Pk_a /= (cosmo['H0'] / 100.)**3.

        # Compute the 2D power spectrum
        p_of_k_a = ccl.Pk2D(a_arr=self.a,
                            lk_arr=lk_arr,
                            pk_arr=Pk_a,
                            is_logp=False)

        return p_of_k_a
示例#16
0
文件: Cls.py 项目: damonge/NzGCWL
def compute_Cls(cosmo_fid,
                dndz_z,
                b_z,
                z_cent,
                N_gal_sample,
                ell,
                a_arr,
                k_arr,
                sigma_e2,
                area_overlap,
                powerspec='halofit',
                simple_bias=True):
    N_ell = len(ell)
    N_zsamples_theo = len(z_cent)
    N_tomo = len(N_gal_sample)
    # choose a scheme for evaluating the matter power spectrum -- halofit is recommended
    if powerspec == 'halofit':
        # Compute matter pk using halofit
        pk_mm_arr = np.array(
            [ccl.nonlin_matter_power(cosmo_fid, k_arr, a) for a in a_arr])
    elif powerspec == 'halomodel':
        # Alternatively use halo model
        pk_mm_arr = np.array([
            ccl.halomodel.halomodel_matter_power(cosmo_fid, k_arr, a)
            for a in a_arr
        ])

    # choose a scheme for evaluating the galaxy power spectrum between simple bias and HOD model
    if simple_bias == True:
        # Set the power spectra equal to the matter PS -- this changes later in the code
        pk_gg_arr = pk_mm_arr.copy()
        pk_gm_arr = pk_mm_arr.copy()
    else:
        # HOD parameter values
        hod_par = {'sigm_0': 0.4,'sigm_1': 0.,'alpha_0': 1.0,'alpha_1': 0.,'fc_0': 1.,'fc_1': 0.,\
                  'lmmin_0': 3.71,'lmmin_1': 9.99,'m0_0': 1.28,'m0_1': 10.34,'m1_0': 7.08,'m1_1': 9.34}
        # Evolve HOD parameters and create an HOD profile
        hodpars = hod_funcs_evol_fit.HODParams(hod_par,
                                               islogm0=True,
                                               islogm1=True)
        hodprof = hod.HODProfile(cosmo_fid, hodpars.lmminf, hodpars.sigmf,\
                                 hodpars.fcf, hodpars.m0f, hodpars.m1f, hodpars.alphaf)
        # Compute galaxy pk using halofit
        pk_gg_arr = np.array(
            [hodprof.pk(k_arr, a_arr[i]) for i in range(a_arr.shape[0])])
        # Compute galaxy-matter pk using halofit
        pk_gm_arr = np.array(
            [hodprof.pk_gm(k_arr, a_arr[i]) for i in range(a_arr.shape[0])])
        # Wipe b_z since HOD is adopted
        b_z[:, :] = 1.

    # Create pk2D objects for interpolation
    pk_mm = ccl.Pk2D(a_arr=a_arr,
                     lk_arr=np.log(k_arr),
                     pk_arr=np.log(pk_mm_arr),
                     is_logp=True)
    pk_gg = ccl.Pk2D(a_arr=a_arr,
                     lk_arr=np.log(k_arr),
                     pk_arr=np.log(pk_gg_arr),
                     is_logp=True)
    pk_gm = ccl.Pk2D(a_arr=a_arr,
                     lk_arr=np.log(k_arr),
                     pk_arr=np.log(pk_gm_arr),
                     is_logp=True)

    # number of indep correlation functions per type gg gs ss
    N_tot_corr = N_ell * (N_tomo * (2 * N_tomo + 1))

    # make C_ell of size N_ell*N_tomo*(2N_tomo+1)
    C_ell = np.zeros(N_tot_corr)

    # List all redshift bin combinations
    temp = np.arange(2 * N_tomo)
    temp = np.vstack((temp, temp)).T
    combs = np.array(list(combinations(range(2 * N_tomo), 2)))
    all_combos = np.vstack((temp, combs))

    for c, comb in enumerate(all_combos):
        i = comb[0] % N_tomo  # first redshift bin
        j = comb[1] % N_tomo  # second redshift bin
        t_i = comb[0] // N_tomo  # tracer type 0 means g and 1 means s
        t_j = comb[1] // N_tomo  # tracer type 0 means g and 1 means s

        # Noise
        if (i == j):
            # number density of galaxies
            N_gal = N_gal_sample[i]
            n_gal = N_gal / area_overlap  # in rad^-2

            # Adding noise
            noise_gal = 1. / n_gal
            noise_shape = sigma_e2[i] / n_gal
        else:
            noise_gal = 0.
            noise_shape = 0.

        # Now create corresponding Cls with pk2D objects matched to pk
        # this is gg
        if t_i * 2 + t_j == 0:
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z[i,:]), \
                                               dndz=(z_cent, dndz_z[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z[j,:]), \
                                               dndz=(z_cent, dndz_z[j,:]),mag_bias=None, \
                                               has_rsd=False)

            cl_gg = ccl.angular_cl(cosmo_fid,
                                   tracer_z1,
                                   tracer_z2,
                                   ell,
                                   p_of_k_a=pk_gg)
            cl_gg_no = cl_gg + noise_gal

            C_ell[(N_ell * c):(N_ell * c) + N_ell] = cl_gg_no

        # this is gs
        elif t_i * 2 + t_j == 1:
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z[i,:]), \
                                               dndz=(z_cent, dndz_z[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid,
                                              dndz=(z_cent, dndz_z[j, :]))
            cl_gs = ccl.angular_cl(cosmo_fid,
                                   tracer_z1,
                                   tracer_z2,
                                   ell,
                                   p_of_k_a=pk_gm)
            cl_gs_no = cl_gs

            C_ell[(N_ell * c):(N_ell * c) + N_ell] = cl_gs_no

        # this is ss
        elif t_i * 2 + t_j == 3:
            tracer_z1 = ccl.WeakLensingTracer(cosmo_fid,
                                              dndz=(z_cent, dndz_z[i, :]))
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid,
                                              dndz=(z_cent, dndz_z[j, :]))
            cl_ss = ccl.angular_cl(cosmo_fid,
                                   tracer_z1,
                                   tracer_z2,
                                   ell,
                                   p_of_k_a=pk_mm)
            cl_ss_no = cl_ss + noise_shape

            C_ell[(N_ell * c):(N_ell * c) + N_ell] = cl_ss_no

    print(len(C_ell))
    return C_ell
示例#17
0
import numpy as np
import pytest
import pyccl as ccl


COSMO = ccl.Cosmology(
    Omega_c=0.27, Omega_b=0.045, h=0.67, sigma8=0.8, n_s=0.96,
    transfer_function='bbks', matter_power_spectrum='linear')
PKA = ccl.Pk2D(lambda k, a: np.log(a/k), cosmo=COSMO)
HBFS = [ccl.halos.HaloBiasSheth99,
        ccl.halos.HaloBiasSheth01,
        ccl.halos.HaloBiasTinker10,
        ccl.halos.HaloBiasBhattacharya11]
MS = [1E13, [1E12, 1E15], np.array([1E12, 1E15])]
MFOF = ccl.halos.MassDef('fof', 'matter')
MVIR = ccl.halos.MassDef('vir', 'critical')
MDFS = [MVIR, MVIR, MFOF, MVIR]


@pytest.mark.parametrize('bM_class', HBFS)
def test_bM_subclasses_smoke(bM_class):
    bM = bM_class(COSMO)
    for m in MS:
        b = bM.get_halo_bias(COSMO, m, 0.9)
        assert np.all(np.isfinite(b))
        assert np.shape(b) == np.shape(m)


@pytest.mark.parametrize('bM_pair', zip(HBFS, MDFS))
def test_bM_mdef_raises(bM_pair):
    bM_class, mdef = bM_pair
示例#18
0
def test_pk2d_get_spline_arrays():
    empty_pk2d = ccl.Pk2D(empty=True)

    # Pk2D needs splines defined to get splines out
    with pytest.raises(ValueError):
        empty_pk2d.get_spline_arrays()
示例#19
0
def compute_mCs(par,i_zs,j_zs,z_cent=z_s_cents,k_arr=k_ar,z_arr=z_ar,a_arr=a_ar,ell=ells):    
    # Compute matter pk using halofit
    pk_mm_arr = np.array([ccl.nonlin_matter_power(cosmo_fid, k_arr, a) for a in a_arr])
    pk_gg_arr = pk_mm_arr.copy() # because we later include bias
    pk_gm_arr = pk_mm_arr.copy() # because we later include bias

    # Create pk2D objects for interpolation
    pk_mm = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_mm_arr), is_logp=True)
    pk_gg = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_gg_arr), is_logp=True)
    pk_gm = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_gm_arr), is_logp=True)

    # bias parameters should be set to 1
    b_z = np.ones(N_zsamples)
    
    # load the dndz parameters for curly C matrix element i_zs,j_zs
    dndz_z_single_i = np.zeros((N_tomo_single,N_zsamples))
    dndz_z_single_j = np.zeros((N_tomo_single,N_zsamples))
    # D_i_many is defined below
    for i_z in range(N_tomo_single):
        if interp == 'nearest':
            dndz_z_single_i[i_z,:] = D_i_many_near[i_zs,:]
            dndz_z_single_j[i_z,:] = D_i_many_near[j_zs,:]
        elif interp == 'linear':
            dndz_z_single_i[i_z,:] = D_i_many[i_zs,:]
            dndz_z_single_j[i_z,:] = D_i_many[j_zs,:]

    # per type gg gs ss
    tot_corr = N_ell*(N_tomo_single*(2*N_tomo_single+1))
    
    # make Cl_all of size N_ell*N_tomo_single*(2N_tomo_single+1)=30
    CL_ALL = np.zeros(tot_corr)
    temp = np.arange(2*N_tomo_single)
    temp = np.vstack((temp,temp)).T
    combs = np.array(list(combinations(range(2*N_tomo_single),2)))
    all_combos = np.vstack((temp,combs))

    for c, comb in enumerate(all_combos):
        # this is artifact from the other code -- here N_tomo_single is always 1
        i = comb[0]%N_tomo_single # first redshift bin (ALWAYS 0 HERE)
        j = comb[1]%N_tomo_single # second redshift bin (ALWAYS 0 HERE)
        t_i = comb[0]//N_tomo_single # tracer type 0 means g and 1 means s
        t_j = comb[1]//N_tomo_single # tracer type 0 means g and 1 means s

        
        # Now create corresponding Cls with pk2D objects matched to pk
        if t_i*2+t_j == 0: # this is gg
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z_single_i[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z_single_j[j,:]),mag_bias=None, \
                                               has_rsd=False)

            cl_gg = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_gg)
            cl_gg_no = cl_gg + 0.
            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_gg_no

        
        elif t_i*2+t_j == 1: # this is gs
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z_single_i[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z_single_j[j,:]))
            cl_gs = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_gm)
            cl_gs_no = cl_gs
            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_gs_no

        elif t_i*2+t_j == 3: # this is ss
            
            tracer_z1 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z_single_i[i,:]))
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z_single_j[j,:]))
            cl_ss = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_mm)
            cl_ss_no = cl_ss + 0.
            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_ss_no
                
    return CL_ALL
示例#20
0
    def getCls(self, tr_i_name, tr_j_name, l_arr):
        """ typ - is a two character string gg, gs,ss, sy, sk etc...
            i,j are indices for g and s"""

        if self.params['corr_halo_mod']:
            logger.info('Correcting halo model Pk with HALOFIT ratio.')
            if not hasattr(self, 'rk_hm'):
                logger.info('Computing halo model correction.')
                HMCorr = HaloModCorrection(self.cosmo,
                                           self.hmc,
                                           self.pM,
                                           k_range=[1e-4, 1e2],
                                           nlk=256,
                                           z_range=[0., 3.],
                                           nz=50)
                self.rk_hm = HMCorr.rk_interp(GSKYTheory.k_arr,
                                              GSKYTheory.a_arr)

        if 'wl' in tr_i_name and 'wl' in tr_j_name or 'wl' in tr_i_name and 'kappa' in tr_j_name or \
                'kappa' in tr_i_name and 'wl' in tr_j_name or 'kappa' in tr_i_name and 'kappa' in tr_j_name:
            if not hasattr(self, 'pk_MMf'):
                if not self.params['corr_halo_mod']:
                    Pk = ccl.halos.halomod_Pk2D(self.cosmo,
                                                self.hmc,
                                                self.pM,
                                                normprof1=True,
                                                lk_arr=np.log(
                                                    GSKYTheory.k_arr),
                                                a_arr=GSKYTheory.a_arr)
                else:
                    Pk_arr = ccl.halos.halomod_power_spectrum(self.cosmo,
                                                              self.hmc,
                                                              GSKYTheory.k_arr,
                                                              GSKYTheory.a_arr,
                                                              self.pM,
                                                              normprof1=True)
                    Pk_arr *= self.rk_hm
                    Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                  lk_arr=np.log(GSKYTheory.k_arr),
                                  pk_arr=Pk_arr,
                                  cosmo=self.cosmo,
                                  is_logp=False)
                self.pk_MMf = Pk

            else:
                Pk = self.pk_MMf
        elif 'wl' in tr_i_name and 'y' in tr_j_name or 'y' in tr_i_name and 'wl' in tr_j_name or \
                'kappa' in tr_i_name and 'y' in tr_j_name or 'y' in tr_i_name and 'kappa' in tr_j_name:
            if not hasattr(self, 'pk_yMf'):
                if not self.params['corr_halo_mod']:
                    Pk = ccl.halos.halomod_Pk2D(self.cosmo,
                                                self.hmc,
                                                self.py,
                                                prof2=self.pM,
                                                normprof1=False,
                                                normprof2=True,
                                                lk_arr=np.log(
                                                    GSKYTheory.k_arr),
                                                a_arr=GSKYTheory.a_arr)
                else:
                    Pk_arr = ccl.halos.halomod_power_spectrum(self.cosmo,
                                                              self.hmc,
                                                              GSKYTheory.k_arr,
                                                              GSKYTheory.a_arr,
                                                              self.py,
                                                              prof2=self.pM,
                                                              normprof1=False,
                                                              normprof2=True)
                    Pk_arr *= self.rk_hm
                    Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                  lk_arr=np.log(GSKYTheory.k_arr),
                                  pk_arr=Pk_arr,
                                  cosmo=self.cosmo,
                                  is_logp=False)
                self.pk_yMf = Pk

            else:
                Pk = self.pk_yMf
        elif 'g' in tr_i_name and 'wl' in tr_j_name or 'wl' in tr_i_name and 'g' in tr_j_name or \
                'kappa' in tr_i_name and 'g' in tr_j_name or 'g' in tr_i_name and 'kappa' in tr_j_name:
            if self.params['HODmod'] == 'zevol':
                if not hasattr(self, 'pk_gMf'):
                    if not self.params['corr_halo_mod']:
                        Pk = ccl.halos.halomod_Pk2D(self.cosmo,
                                                    self.hmc,
                                                    self.pg,
                                                    prof2=self.pM,
                                                    normprof1=True,
                                                    normprof2=True,
                                                    lk_arr=np.log(
                                                        GSKYTheory.k_arr),
                                                    a_arr=GSKYTheory.a_arr)
                    else:
                        Pk_arr = ccl.halos.halomod_power_spectrum(
                            self.cosmo,
                            self.hmc,
                            GSKYTheory.k_arr,
                            GSKYTheory.a_arr,
                            self.pg,
                            prof2=self.pM,
                            normprof1=True,
                            normprof2=True)
                        Pk_arr *= self.rk_hm
                        Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                      lk_arr=np.log(GSKYTheory.k_arr),
                                      pk_arr=Pk_arr,
                                      cosmo=self.cosmo,
                                      is_logp=False)
                    self.pk_gMf = Pk

                else:
                    Pk = self.pk_gMf
            else:
                if 'g' in tr_i_name:
                    tr_g_name = tr_i_name
                else:
                    tr_g_name = tr_j_name
                if not self.params['corr_halo_mod']:
                    Pk = ccl.halos.halomod_Pk2D(
                        self.cosmo,
                        self.hmc,
                        self.ccl_tracers[tr_g_name]['prof'],
                        prof2=self.pM,
                        normprof1=True,
                        normprof2=True,
                        lk_arr=np.log(GSKYTheory.k_arr),
                        a_arr=GSKYTheory.a_arr)
                else:
                    Pk_arr = ccl.halos.halomod_power_spectrum(
                        self.cosmo,
                        self.hmc,
                        GSKYTheory.k_arr,
                        GSKYTheory.a_arr,
                        self.ccl_tracers[tr_g_name]['prof'],
                        prof2=self.pM,
                        normprof1=True,
                        normprof2=True)
                    Pk_arr *= self.rk_hm
                    Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                  lk_arr=np.log(GSKYTheory.k_arr),
                                  pk_arr=Pk_arr,
                                  cosmo=self.cosmo,
                                  is_logp=False)
        elif 'g' in tr_i_name and 'y' in tr_j_name or 'y' in tr_i_name and 'g' in tr_j_name:
            if self.params['HODmod'] == 'zevol':
                if not hasattr(self, 'pk_ygf'):
                    if not self.params['corr_halo_mod']:
                        Pk = ccl.halos.halomod_Pk2D(self.cosmo,
                                                    self.hmc,
                                                    self.pg,
                                                    prof2=self.py,
                                                    normprof1=True,
                                                    normprof2=False,
                                                    lk_arr=np.log(
                                                        GSKYTheory.k_arr),
                                                    a_arr=GSKYTheory.a_arr)
                    else:
                        Pk_arr = ccl.halos.halomod_power_spectrum(
                            self.cosmo,
                            self.hmc,
                            GSKYTheory.k_arr,
                            GSKYTheory.a_arr,
                            self.pg,
                            prof2=self.py,
                            normprof1=True,
                            normprof2=False)
                        Pk_arr *= self.rk_hm
                        Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                      lk_arr=np.log(GSKYTheory.k_arr),
                                      pk_arr=Pk_arr,
                                      cosmo=self.cosmo,
                                      is_logp=False)
                    self.pk_ygf = Pk

                else:
                    Pk = self.pk_ygf
            else:
                if 'g' in tr_i_name:
                    tr_g_name = tr_i_name
                else:
                    tr_g_name = tr_j_name
                Pk = ccl.halos.halomod_Pk2D(
                    self.cosmo,
                    self.hmc,
                    self.ccl_tracers[tr_g_name]['prof'],
                    prof2=self.py,
                    normprof1=True,
                    normprof2=False,
                    lk_arr=np.log(GSKYTheory.k_arr),
                    a_arr=GSKYTheory.a_arr)
        elif 'g' in tr_i_name and 'g' in tr_j_name:
            if self.params['HODmod'] == 'zevol':
                if not hasattr(self, 'pk_ggf'):
                    if not self.params['corr_halo_mod']:
                        Pk = ccl.halos.halomod_Pk2D(self.cosmo,
                                                    self.hmc,
                                                    self.pg,
                                                    prof2=self.pg,
                                                    prof_2pt=self.HOD2pt,
                                                    normprof1=True,
                                                    normprof2=True,
                                                    lk_arr=np.log(
                                                        GSKYTheory.k_arr),
                                                    a_arr=GSKYTheory.a_arr)
                    else:
                        Pk_arr = ccl.halos.halomod_power_spectrum(
                            self.cosmo,
                            self.hmc,
                            GSKYTheory.k_arr,
                            GSKYTheory.a_arr,
                            self.pg,
                            prof_2pt=self.HOD2pt,
                            prof2=self.pg,
                            normprof1=True,
                            normprof2=True)
                        Pk_arr *= self.rk_hm
                        Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                      lk_arr=np.log(GSKYTheory.k_arr),
                                      pk_arr=Pk_arr,
                                      cosmo=self.cosmo,
                                      is_logp=False)

                    self.pk_ggf = Pk
                else:
                    Pk = self.pk_ggf
            else:
                if not self.params['corr_halo_mod']:
                    Pk = ccl.halos.halomod_Pk2D(
                        self.cosmo,
                        self.hmc,
                        self.ccl_tracers[tr_i_name]['prof'],
                        prof2=self.ccl_tracers[tr_j_name]['prof'],
                        prof_2pt=self.HOD2pt,
                        normprof1=True,
                        normprof2=True,
                        lk_arr=np.log(GSKYTheory.k_arr),
                        a_arr=GSKYTheory.a_arr)
                else:
                    Pk_arr = ccl.halos.halomod_power_spectrum(
                        self.cosmo,
                        self.hmc,
                        GSKYTheory.k_arr,
                        GSKYTheory.a_arr,
                        self.ccl_tracers[tr_i_name]['prof'],
                        prof_2pt=self.HOD2pt,
                        prof2=self.ccl_tracers[tr_j_name]['prof'],
                        normprof1=True,
                        normprof2=True)
                    Pk_arr *= self.rk_hm
                    Pk = ccl.Pk2D(a_arr=GSKYTheory.a_arr,
                                  lk_arr=np.log(GSKYTheory.k_arr),
                                  pk_arr=Pk_arr,
                                  cosmo=self.cosmo,
                                  is_logp=False)
        else:  ## eg yy
            logger.warning(
                'Tracer combination {}, {} not implemented. Returning zero.'.
                format(tr_i_name, tr_j_name))
            return np.zeros_like(l_arr)

        cls = ccl.angular_cl(self.cosmo,
                             self.ccl_tracers[tr_i_name]['ccl_tracer'],
                             self.ccl_tracers[tr_j_name]['ccl_tracer'],
                             l_arr,
                             p_of_k_a=Pk)

        if 'wl' in tr_i_name and 'm' in self.ccl_tracers[tr_i_name].keys():
            cls *= (1. + self.ccl_tracers[tr_i_name]['m'])
        if 'wl' in tr_j_name and 'm' in self.ccl_tracers[tr_j_name].keys():
            cls *= (1. + self.ccl_tracers[tr_j_name]['m'])

        return cls
示例#21
0

COSMO = ccl.Cosmology(
    Omega_c=0.27, Omega_b=0.045, h=0.67, sigma8=0.8, n_s=0.96,
    transfer_function='bbks', matter_power_spectrum='linear')
M200 = ccl.halos.MassDef200m()
HMF = ccl.halos.MassFuncTinker10(COSMO, mass_def=M200)
HBF = ccl.halos.HaloBiasTinker10(COSMO, mass_def=M200)
P1 = ccl.halos.HaloProfileNFW(ccl.halos.ConcentrationDuffy08(M200),
                              fourier_analytic=True)
P2 = P1
PKC = ccl.halos.Profile2pt()
KK = np.geomspace(1E-3, 10, 32)
MM = np.geomspace(1E11, 1E15, 16)
AA = 1.0
PK2D = ccl.Pk2D(cosmo=COSMO, pkfunc=lambda k, a: a / k)


def test_prof2pt_smoke():
    uk_NFW = P1.fourier(COSMO, KK, MM, AA,
                        mass_def=M200)
    uk_EIN = P2.fourier(COSMO, KK, MM, AA,
                        mass_def=M200)
    # Variance
    cv_NN = PKC.fourier_2pt(P1, COSMO, KK, MM, AA,
                            mass_def=M200)
    assert np.all(np.fabs((cv_NN - uk_NFW**2)) < 1E-10)

    # 2-point
    cv_NE = PKC.fourier_2pt(P1, COSMO, KK, MM, AA,
                            prof2=P2, mass_def=M200)
示例#22
0
                                             ks * h,
                                             a=1. / (1 + z_templates[z_str]))

        if want_rat:
            Pk *= ccl.nonlin_matter_power(
                cosmo_ccl, ks * h, a=1. / (1 + z_templates[z_str])) * h**3
        # convert to Mpc^3 rather than [Mpc/h]^3
        Pk_a[i, :] = Pk / h**3.
    Pk_a_ij[combo] = Pk_a
# convert to Mpc^-1 rather than h/Mpc
Pk_a_ij['lk_arr'] = np.log(ks * h)

# tuks
pk_a *= 1.41
pk_tmp = ccl.Pk2D(a_arr=a_arr,
                  lk_arr=np.log(ks * h),
                  pk_arr=pk_a,
                  is_logp=False)
ells = np.geomspace(2, 1000, 20)
z_arr = np.linspace(0, 4, 100)[::-1]
nz_arr = np.exp(-((z_arr - 0.5) / 0.05)**2 / 2)
bz_arr = np.ones(len(z_arr)) * 1.41
g_tracer = ccl.NumberCountsTracer(cosmo_ccl,
                                  has_rsd=False,
                                  dndz=(z_arr[::-1], nz_arr[::-1]),
                                  bias=(z_arr[::-1], bz_arr[::-1]))
g_1_tracer = ccl.NumberCountsTracer(cosmo_ccl,
                                    has_rsd=False,
                                    dndz=(z_arr[::-1], nz_arr[::-1]),
                                    bias=(z_arr[::-1], np.ones(len(z_arr))))
wl_tracer = ccl.WeakLensingTracer(cosmo_ccl, dndz=(z_arr[::-1], nz_arr[::-1]))
cl_gg = ccl.angular_cl(cosmo_ccl, g_tracer, wl_tracer, ells)
示例#23
0
def compute_Cls(par,hod_par=hod_params,z_cent=z_s_cents,N_gal_sample=N_gal_bin,k_arr=k_ar,z_arr=z_ar,a_arr=a_ar,ell=ells,
                compute_cov=False, compute_inv_cov=False,plot_for_sanity=False,powerspec='halofit',simple_bias=True):
    
    if powerspec == 'halofit':
        # Compute matter pk using halofit
        pk_mm_arr = np.array([ccl.nonlin_matter_power(cosmo_fid, k_arr, a) for a in a_arr])
    elif powerspec == 'halomodel':
        # Alternatively use halo model
        pk_mm_arr = np.array([ccl.halomodel.halomodel_matter_power(cosmo_fid, k_arr, a) for a in a_arr])
        
    if simple_bias == True:
        pk_gg_arr = pk_mm_arr.copy() # because we later include bias
        pk_gm_arr = pk_mm_arr.copy() # because we later include bias
    else:
        # Compute HOD
        hodpars = hod_funcs_evol_fit.HODParams(hod_par, islogm0=True, islogm1=True)
        
        hodprof = hod.HODProfile(cosmo_fid, hodpars.lmminf, hodpars.sigmf,\
                                 hodpars.fcf, hodpars.m0f, hodpars.m1f, hodpars.alphaf)
        # Compute galaxy pk using halofit
        pk_gg_arr = np.array([hodprof.pk(k_arr, a_arr[i]) for i in range(a_arr.shape[0])])
        # Compute galaxy-matter pk using halofit
        pk_gm_arr = np.array([hodprof.pk_gm(k_arr, a_arr[i]) for i in range(a_arr.shape[0])])
        
    # Create pk2D objects for interpolation
    pk_mm = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_mm_arr), is_logp=True)
    pk_gg = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_gg_arr), is_logp=True)
    pk_gm = ccl.Pk2D(a_arr=a_arr, lk_arr=np.log(k_arr), pk_arr=np.log(pk_gm_arr), is_logp=True)

    # load the bias parameters
    # k # indicates which of N_tomo sampling bins
    b_z = np.array([par['b_%02d'%k]['val'] for k in range(N_zsamples)])
    
    # load the dndz parameters
    dndz_z = np.zeros((N_tomo,N_zsamples))
    for i_z in range(N_tomo):
        dndz_z[i_z,:] = np.array([par['dndz_%02d_%02d'%(i_z,k)]['val'] for k in range(N_zsamples)])
    

    # per type gg gs ss
    tot_corr = N_ell*(N_tomo*(2*N_tomo+1))
    
    # make Cl_all of size N_ell*N_tomo*(2N_tomo+1)
    CL_ALL = np.zeros(tot_corr)
    temp = np.arange(2*N_tomo)
    temp = np.vstack((temp,temp)).T
    combs = np.array(list(combinations(range(2*N_tomo),2)))
    all_combos = np.vstack((temp,combs))
    
    for c, comb in enumerate(all_combos):
        i = comb[0]%N_tomo # first redshift bin
        j = comb[1]%N_tomo # second redshift bin
        t_i = comb[0]//N_tomo # tracer type 0 means g and 1 means s
        t_j = comb[1]//N_tomo # tracer type 0 means g and 1 means s
        
        # NOISE
        if (i == j):
            # number density of galaxies
            N_gal = N_gal_sample[i]            
            n_gal = N_gal/area_COSMOS # in rad^-2
            
            
            # Adding noise
            noise_gal = 1./n_gal
            noise_shape = sigma_e2[i]/n_gal
        else:
            noise_gal = 0.
            noise_shape = 0.
        
        # Now create corresponding Cls with pk2D objects matched to pk
        if t_i*2+t_j == 0: # this is gg
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z[j,:]),mag_bias=None, \
                                               has_rsd=False)

            cl_gg = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_gg)
            cl_gg_no = cl_gg + noise_gal

            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_gg_no
        elif t_i*2+t_j == 1: # this is gs
            
            tracer_z1 = ccl.NumberCountsTracer(cosmo_fid, bias=(z_cent, b_z), \
                                               dndz=(z_cent, dndz_z[i,:]),mag_bias=None, \
                                               has_rsd=False)
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z[j,:]))
            cl_gs = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_gm)
            cl_gs_no = cl_gs
            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_gs_no
            
        elif t_i*2+t_j == 3: # this is ss
            
            tracer_z1 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z[i,:]))
            tracer_z2 = ccl.WeakLensingTracer(cosmo_fid, dndz=(z_cent, dndz_z[j,:]))
            cl_ss = ccl.angular_cl(cosmo_fid, tracer_z1, tracer_z2, ell, p_of_k_a=pk_mm)
            cl_ss_no = cl_ss + noise_shape

            CL_ALL[(N_ell*c):(N_ell*c)+N_ell] = cl_ss_no

        if plot_for_sanity == True:
            if c == 0:
                # Plot for sanity
                plt.loglog(ells, cl_gg, label=r'$\mathrm{gg}$')
                plt.loglog(ells, cl_gg_no, label=r'$\mathrm{gg}+{\rm noise}$')
                plt.loglog(ells, cl_ss, label=r'$\mathrm{ss}$')
                plt.loglog(ells, cl_gs, label=r'$\mathrm{gs}$')
                plt.xlabel(r'$\ell$')
                plt.ylabel(r'$C_{\ell}$')
                plt.legend()
                plt.savefig('Cls.png')
                plt.close()

    if compute_inv_cov == False and compute_cov == False:
        print(len(CL_ALL))
        return CL_ALL

    print(len(CL_ALL))
    print(np.sum(CL_ALL>0))
    
    COV_ALL = np.zeros((len(CL_ALL),len(CL_ALL)))    
    # COMPUTE COVARIANCE MATRIX 
    for c_A, comb_A in enumerate(all_combos):
        for c_B, comb_B in enumerate(all_combos):
            i = comb_A[0]%N_tomo # first redshift bin
            j = comb_A[1]%N_tomo # second redshift bin
            m = comb_B[0]%N_tomo # first redshift bin
            n = comb_B[1]%N_tomo # second redshift bin                        
            
            c_im = np.argmax(np.product(np.sort(np.array([comb_A[0],comb_B[0]]))==all_combos,axis=1))
            c_jn = np.argmax(np.product(np.sort(np.array([comb_A[1],comb_B[1]]))==all_combos,axis=1))
            c_in = np.argmax(np.product(np.sort(np.array([comb_A[0],comb_B[1]]))==all_combos,axis=1))
            c_jm = np.argmax(np.product(np.sort(np.array([comb_A[1],comb_B[0]]))==all_combos,axis=1))

            # PAIRS A,B ARE (ti,tj),(tm,tn) at same ell
            #cov(ij,mn) = im,jn + in,jm                    
            C_im = CL_ALL[(c_im*N_ell):(c_im*N_ell)+N_ell]
            C_jn = CL_ALL[(c_jn*N_ell):(c_jn*N_ell)+N_ell]
            C_in = CL_ALL[(c_in*N_ell):(c_in*N_ell)+N_ell]
            C_jm = CL_ALL[(c_jm*N_ell):(c_jm*N_ell)+N_ell]

            
           # Knox formula
            Cov_ijmn = (C_im*C_jn+C_in*C_jm)/((2*ell+1.)*delta_ell*f_sky)
            
            if plot_for_sanity == True:
                if (c_A == c_B):
                    print(i,j,'=',m,n)
                    print(c_A)
                    Cl_err = np.sqrt(Cov_ijmn)

                    t_i = comb_A[0]//N_tomo
                    t_j = comb_A[1]//N_tomo
                    print(N_tomo*i+j+1)
                    
                    plt.subplot(N_tomo, N_tomo, N_tomo*i+j+1)
                    plt.title("z=%f x z=%f"%(z_bin_cents[i],z_bin_cents[j]))
                    c_ij = np.argmax(np.product(np.sort(np.array([comb_A[0],comb_A[1]]))==all_combos,axis=1))
                    C_ij = CL_ALL[(c_ij*N_ell):(c_ij*N_ell)+N_ell]
                    # maybe add legend
                    (_,caps,eb)=plt.errorbar(ell,C_ij,yerr=Cl_err,lw=2.,ls='-',capsize=5,label=str(t_i*2+t_j)) 
                    plt.legend()
                    plt.xscale('log')
                    plt.yscale('log')



            COV_ALL[(N_ell*c_A):(N_ell*c_A)+\
                    N_ell,(N_ell*c_B):(N_ell*c_B)+N_ell] = np.diag(Cov_ijmn)
            COV_ALL[(N_ell*c_B):(N_ell*c_B)+\
                    N_ell,(N_ell*c_A):(N_ell*c_A)+N_ell] = np.diag(Cov_ijmn)


    if compute_cov:
        return COV_ALL

    evals,evecs = la.eig(COV_ALL)
    
    if (is_pos_def(COV_ALL) != True): print("Covariance is not positive definite!"); exit(0)
    ICOV_ALL = la.inv(COV_ALL)
    return ICOV_ALL