Beispiel #1
0
def test_cov_mmarg():
    sm = 0.1
    data = get_config(dtype0='galaxy_shear')
    data['tracers']['Dummy__0']['sigma_m'] = sm

    # Homemade marginalized covariance
    # First, get decoupled power spectra
    # Theory power spectra
    clf = ClFid(data, 'Dummy__0', 'Dummy__0')
    _, cl = clf.get_ell_cl()
    shutil.rmtree(tmpdir1)
    # Binning
    clc = Cl(data, 'Dummy__0', 'Dummy__0')
    wp = clc.get_bandpower_windows()
    shutil.rmtree(tmpdir1)
    ncl, nbpw, _, nl = wp.shape
    wp = wp.reshape((ncl*nbpw, ncl*nl))
    cl = cl.reshape(ncl*nl)
    cl = np.dot(wp, cl)
    # Marginalized covariance term
    covmargb = 4*sm**2*cl[:, None]*cl[None, :]

    # Do with xCell
    covc = Cov(data, 'Dummy__0', 'Dummy__0', 'Dummy__0', 'Dummy__0')
    covmarg = covc.get_covariance_m_marg()
    shutil.rmtree(tmpdir1)
    assert np.amax(np.fabs(covmarg-covmargb))/np.mean(covmarg) < 1E-5
Beispiel #2
0
def test_get_covariance(cldata):
    # Get cl from randomnly generated map ("data")
    config = get_config(fsky=1)
    config['cov']['cls_from_data'] = cldata

    cl_class = Cl(config, 'Dummy__0', 'Dummy__0')
    ell, cl_data = cl_class.get_ell_cl()

    # Get cl from mapper (the true one)
    m1, m2 = cl_class.get_mappers()
    w = cl_class.get_workspace()
    cl_m1 = m1.get_cl()
    cl_m1_cp = w.couple_cell([cl_m1])
    cl_m1 = w.decouple_cell(cl_m1_cp)

    # # With no mask, there should not be any coupling
    # rdev = cl_m1_cp / cl_m1 - 1
    # assert np.max(np.abs(rdev) < 1e-5)

    # Compute covariance
    cov_class = Cov(config, 'Dummy__0', 'Dummy__0', 'Dummy__0', 'Dummy__0')
    cov = cov_class.get_covariance()

    diag = (2 * cl_m1[0]**2) / (2 * ell + 1) / 4
    cov_m = np.diag(diag)

    icov = np.linalg.inv(cov)
    icov_m = np.linalg.inv(cov_m)
    dCl = (cl_data - cl_m1)[0]

    chi2 = dCl.dot(icov).dot(dCl)
    chi2_m = dCl.dot(icov_m).dot(dCl)

    shutil.rmtree(tmpdir1)
    assert np.fabs(chi2/chi2_m-1) < 0.03
Beispiel #3
0
def test_custom_auto():
    # No custom auto
    data = get_config()
    clc1 = Cl(data, 'Dummy__0', 'Dummy__0')
    l1, cl1 = clc1.get_ell_cl_cp()
    shutil.rmtree(tmpdir1)

    # With custom auto
    data = get_config()
    data['tracers']['Dummy__0']['custom_auto'] = True
    data['tracers']['Dummy__0']['custom_offset'] = np.pi*1E-5
    clc2 = Cl(data, 'Dummy__0', 'Dummy__0')
    l2, cl2 = clc2.get_ell_cl_cp()
    shutil.rmtree(tmpdir1)

    assert np.allclose(cl1, cl2-np.pi*1E-5, rtol=1E-4, atol=0)

    # Covariance custom cross
    data = get_config()
    data['tracers']['Dummy__0']['custom_auto'] = True
    data['tracers']['Dummy__0']['custom_offset'] = np.pi*1E-5
    clc3 = Cl(data, 'Dummy__0', 'Dummy__0')
    l2, cl3 = clc3.get_ell_cl_cp_cov()
    shutil.rmtree(tmpdir1)

    assert np.allclose(cl1, cl3, rtol=1E-4, atol=0)
Beispiel #4
0
def test_cl_correction():
    data = get_config()
    cl_class = Cl(data, 'Dummy__2', 'Dummy__2')
    cl_file = cl_class.get_cl_file()
    correct = cl_file['correction']
    dummy = MapperDummy(data['tracers']['Dummy__2'])
    w_a = dummy.get_mask()
    w_b = dummy.get_mask()
    n_a = dummy.mask_power
    n_b = dummy.mask_power
    correct_b = np.mean(w_a*w_b)/np.mean(w_a**n_a*w_b**n_b)
    assert correct != 1
    assert correct == correct_b
Beispiel #5
0
def test_file_inconsistent_errors():
    clo = get_cl_class()
    ell, cl = clo.get_ell_cl()
    # Change bpws and try to read file
    data = get_config(0.2)
    data['bpw_edges'] = data['bpw_edges'][:-1]
    data['recompute']['cls'] = False
    data['recompute']['mcm'] = False
    os.remove(os.path.join(tmpdir1, 'data.yml'))
    clo2 = Cl(data, 'Dummy__0', 'Dummy__0')
    with pytest.raises(ValueError):
        clo2.get_ell_cl()
    shutil.rmtree(tmpdir1)
Beispiel #6
0
def test_ignore_existing_yml():
    # Test for Cls
    cl_class = get_cl_class()
    data = cl_class.data.data
    # Now, data['cls']['Dummy-Dummy']['compute'] = 'all'. We change it to
    # 'auto' and check that is read when ignore_existing_yml=True
    data['cls']['Dummy-Dummy']['compute'] = 'auto'
    cl_class01 = Cl(data, 'Dummy__0', 'Dummy__1', ignore_existing_yml=True)
    assert cl_class01.data.data['cls']['Dummy-Dummy']['compute'] == 'auto'
    cl_class01 = Cl(data, 'Dummy__0', 'Dummy__1', ignore_existing_yml=False)
    assert cl_class01.data.data['cls']['Dummy-Dummy']['compute'] == 'all'

    # Test for Cov
    cov_class = Cov(data, 'Dummy__0', 'Dummy__1', 'Dummy__0', 'Dummy__1',
                    ignore_existing_yml=True)
    assert cov_class.data.data['cls']['Dummy-Dummy']['compute'] == 'auto'
    cov_class = Cov(data, 'Dummy__0', 'Dummy__1', 'Dummy__0', 'Dummy__1',
                    ignore_existing_yml=False)
    assert cov_class.data.data['cls']['Dummy-Dummy']['compute'] == 'all'
    shutil.rmtree(tmpdir1)
Beispiel #7
0
def test_ell_cl_autocov(use):
    s = get_ClSack(use)
    data = get_data()

    for dtype in s.s.get_data_types():
        for trs in s.s.get_tracer_combinations(dtype):
            if dtype in ['cl_00', 'cl_0e', 'cl_ee']:
                ix = 0
            elif dtype in ['cl_0b', 'cl_eb']:
                ix = 1
            elif dtype in ['cl_be']:
                ix = 2
            elif dtype in ['cl_bb']:
                ix = 3
            else:
                raise ValueError(f'data_type {dtype} must be weird!')

            if use == 'fiducial':
                cl_class = ClFid(data.data, *trs)
            else:
                cl_class = Cl(data.data, *trs)

            if use == 'nl':
                ell, cl = s.s.get_ell_cl(dtype, trs[0], trs[1])
                ell2, cl2 = cl_class.get_ell_nl()
            else:
                ell, cl, cov = s.s.get_ell_cl(dtype,
                                              trs[0],
                                              trs[1],
                                              return_cov=True)
                ell2, cl2 = cl_class.get_ell_cl()

                cov_class = Cov(data.data, *trs, *trs)
                nbpw = ell.size
                ncls = cl2.shape[0]
                cov2 = cov_class.get_covariance()
                cov2 = cov2.reshape((nbpw, ncls, nbpw, ncls))[:, ix, :, ix]
                assert np.max(np.abs((cov - cov2) / np.mean(cov))) < 1e-5

            if use == 'fiducial':
                # Matrices to bin the fiducial Cell
                ws_bpw = np.zeros((ell.size, ell2.size))
                ws_bpw[np.arange(ell.size), ell.astype(int)] = 1
                assert np.all(cl == ws_bpw.dot(cl2[ix]))
            else:
                assert np.all(cl == cl2[ix])
                assert np.all(ell == ell2)
Beispiel #8
0
def test_get_nmtbin():
    # This test checks that the bpw_edges can be read from the
    # global part of the yaml file or from the context of one
    # of the cross-correlations.
    # 1. From global
    data = get_config()
    cl1 = Cl(data, 'Dummy__0', 'Dummy__0')
    shutil.rmtree(tmpdir1)
    # 2. From cross-correlations
    data['cls']['Dummy-Dummy']['bpw_edges'] = data.pop('bpw_edges')
    cl2 = Cl(data, 'Dummy__0', 'Dummy__0')
    shutil.rmtree(tmpdir1)
    # Check they are the same
    b1 = cl1.get_NmtBin()
    b2 = cl2.get_NmtBin()
    assert np.all(b1.get_effective_ells() == b2.get_effective_ells())
Beispiel #9
0
def get_cl_class(fsky=0.2, fiducial=False):
    data = get_config(fsky)
    if fiducial:
        return ClFid(data, 'Dummy__0', 'Dummy__0')
    else:
        return Cl(data, 'Dummy__0', 'Dummy__0')
Beispiel #10
0
def test_symmetric():
    data = get_config()
    # Request only 'auto' to test if read_symmetric works in the case you have
    # 'auto but you need the cross for the covariance
    data['cls']['Dummy-Dummy']['compute'] = 'auto'
    cl_class01 = Cl(data, 'Dummy__0', 'Dummy__1')
    os.remove(os.path.join(tmpdir1, 'data.yml'))
    cl_class10 = Cl(data, 'Dummy__1', 'Dummy__0')

    fname = os.path.join(cl_class10.outdir, 'cl_Dummy__1_Dummy__0.npz')
    assert not os.path.isfile(fname)
    fname = os.path.join(cl_class10.outdir, 'w__mask_dummy1_mask_dummy0.fits')
    assert not os.path.isfile(fname)
    assert np.all(np.array(cl_class01.get_masks()) ==
                  np.array(cl_class10.get_masks()[::-1]))
    assert np.all(cl_class01.get_ell_cl()[1] == cl_class10.get_ell_cl()[1])
    assert np.all(cl_class01.get_ell_nl()[1] == cl_class10.get_ell_nl()[1])
    assert np.all(cl_class01.get_ell_nl_cp()[1] ==
                  cl_class10.get_ell_nl_cp()[1])
    shutil.rmtree(tmpdir1)
Beispiel #11
0
def test_cov_ng_1h():
    # From CCL directly
    data = get_config(fsky=0.2)
    clc = Cl(data, 'Dummy__0', 'Dummy__0')
    ells = clc.b.get_effective_ells()
    shutil.rmtree(tmpdir1)
    cosmo = ccl.Cosmology(**data['cov']['fiducial']['cosmo'])
    md = ccl.halos.MassDef200m()
    mf = ccl.halos.MassFuncTinker10(cosmo, mass_def=md)
    hb = ccl.halos.HaloBiasTinker10(cosmo, mass_def=md)
    cm = ccl.halos.ConcentrationDuffy08(mdef=md)
    hmc = ccl.halos.HMCalculator(cosmo, mf, hb, md)
    pr = ccl.halos.HaloProfileHOD(cm, lMmin_0=12.1,
                                  lM1_p=0.1, bg_0=1.2)
    prof2pt = ccl.halos.Profile2ptHOD()
    z, nz = np.loadtxt('xcell/tests/data/DESY1gc_dndz_bin0.txt',
                       usecols=(1, 3), unpack=True)
    tr = ccl.NumberCountsTracer(cosmo, False, dndz=(z, nz),
                                bias=(z, np.ones_like(z)))
    k_arr = np.geomspace(1E-4, 1E2, 256)
    a_arr = 1./(1+np.linspace(0, 3, 15)[::-1])
    tkk = ccl.halos.halomod_Tk3D_1h(cosmo, hmc,
                                    prof1=pr, prof2=pr, prof12_2pt=prof2pt,
                                    prof3=pr, prof4=pr, prof34_2pt=prof2pt,
                                    normprof1=True, normprof2=True,
                                    normprof3=True, normprof4=True,
                                    a_arr=a_arr, lk_arr=np.log(k_arr))
    covNG0 = ccl.angular_cl_cov_cNG(cosmo,
                                    cltracer1=tr, cltracer2=tr,
                                    ell=ells, tkka=tkk, fsky=1.,
                                    cltracer3=tr, cltracer4=tr, ell2=ells)

    # Gaussian only
    data = get_config(fsky=0.2, inc_hm=True)
    data['tracers']['Dummy__0']['hod_params'] = {'lMmin_0': 12.1,
                                                 'lM1_p': 0.1,
                                                 'bg_0': 1.2}
    covcG = Cov(data, 'Dummy__0', 'Dummy__0', 'Dummy__0', 'Dummy__0')
    covG = covcG.get_covariance()
    shutil.rmtree(tmpdir1)

    # Gaussian + non-Gaussian
    data = get_config(fsky=0.2, inc_hm=True)
    data['cov']['non_Gaussian'] = True
    data['cov']['NG_terms'] = ['1h']
    data['tracers']['Dummy__0']['hod_params'] = {'lMmin_0': 12.1,
                                                 'lM1_p': 0.1,
                                                 'bg_0': 1.2}
    covc1 = Cov(data, 'Dummy__0', 'Dummy__0', 'Dummy__0', 'Dummy__0')
    mapper = MapperDummy(data['tracers']['Dummy__0'])
    fsky = np.mean((mapper.get_mask() > 0))
    covNG1 = covc1.get_covariance_ng_halomodel(0, 0, 0, 0, fsky)
    cov1 = covc1.get_covariance()
    shutil.rmtree(tmpdir1)

    # fsky on input
    data = get_config(fsky=0.2, inc_hm=True)
    data['cov']['non_Gaussian'] = True
    data['cov']['NG_terms'] = ['1h']
    data['cov']['fsky_NG'] = 0.1
    data['tracers']['Dummy__0']['hod_params'] = {'lMmin_0': 12.1,
                                                 'lM1_p': 0.1,
                                                 'bg_0': 1.2}
    covc2 = Cov(data, 'Dummy__0', 'Dummy__0', 'Dummy__0', 'Dummy__0')
    covNG2 = covc2.get_covariance()-covG
    shutil.rmtree(tmpdir1)

    # Tests
    # Compare result of NG method with G+NG-G
    assert np.allclose(covNG1, cov1-covG, atol=0)
    # Compare with CCL prediction
    # (interpolation errors are ~1E-4)
    assert np.allclose(covNG0, covNG1*fsky, atol=0, rtol=1E-3)
    # fsky scaling
    assert np.allclose(covNG2, covNG1*fsky/0.1, atol=0)