How to use the treecorr.read_config function in TreeCorr

To help you get started, we’ve selected a few TreeCorr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rmjarvis / TreeCorr / tests / test_kk.py View on Github external
print('kk.xi = ',kk.xi)
    print('true_xi = ',true_xi)
    print('ratio = ',kk.xi / true_xi)
    print('diff = ',kk.xi - true_xi)
    print('max diff = ',max(abs(kk.xi - true_xi)))
    print('max rel diff = ',max(abs((kk.xi - true_xi)/true_xi)))
    np.testing.assert_allclose(kk.xi, true_xi, rtol=0.1*tol_factor)

    # It should also work as a cross-correlation of this cat with itself
    kk.process(cat,cat)
    np.testing.assert_allclose(kk.meanlogr, np.log(kk.meanr), atol=1.e-3)
    np.testing.assert_allclose(kk.xi, true_xi, rtol=0.1*tol_factor)

    # Check that we get the same result using the corr2 function
    cat.write(os.path.join('data','kk.dat'))
    config = treecorr.read_config('configs/kk.yaml')
    config['verbose'] = 0
    config['precision'] = 8
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','kk.out'), names=True, skip_header=1)
    print('kk.xi = ',kk.xi)
    print('from corr2 output = ',corr2_output['xi'])
    print('ratio = ',corr2_output['xi']/kk.xi)
    print('diff = ',corr2_output['xi']-kk.xi)
    np.testing.assert_allclose(corr2_output['xi'], kk.xi, rtol=1.e-3)

    try:
        import fitsio
    except ImportError:
        print('Skipping FITS tests, since fitsio is not installed')
        return
github rmjarvis / TreeCorr / tests / test_rperp.py View on Github external
print('Results with bin_slop = 0.3')
    print('gg.npairs = ',gg.npairs)
    print('gg.xim = ',gg.xim)
    print('theory_gammat = ',theory_gQ)
    print('ratio = ',gg.xim / theory_gQ)
    print('diff = ',gg.xim - theory_gQ)
    print('max diff = ',max(abs(gg.xim - theory_gQ)))
    assert max(abs(gg.xim - theory_gQ)) < 4.e-5
    print('gg.xim_im = ',gg.xim_im)
    assert max(abs(gg.xim_im)) < 1.e-5

    # Check that we get the same result using the corr2 function:
    lens_cat.write(os.path.join('data','gg_oldrperp_lens.dat'))
    source_cat.write(os.path.join('data','gg_oldrperp_source.dat'))
    config = treecorr.read_config('configs/gg_oldrperp.yaml')
    config['verbose'] = 0
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','gg_oldrperp.out'),names=True,
                                    skip_header=1)
    print('gg.xim = ',gg.xim)
    print('from corr2 output = ',corr2_output['xim'])
    print('ratio = ',corr2_output['xim']/gg.xim)
    print('diff = ',corr2_output['xim']-gg.xim)
    np.testing.assert_allclose(corr2_output['xim'], gg.xim, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['xim_im'], gg.xim_im, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['xip'], gg.xip, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['xip_im'], gg.xip_im, rtol=1.e-3)
github rmjarvis / TreeCorr / tests / test_config.py View on Github external
def test_get():
    """Test getting a parameter from a config dict
    """
    config1 = treecorr.read_config('configs/kg.yaml')
    assert treecorr.config.get(config1, 'x_col', int) == 1
    assert treecorr.config.get(config1, 'x_col', str) == '1'
    assert treecorr.config.get(config1, 'x_col') == '1'
    assert treecorr.config.get(config1, 'x_col', int, 2) == 1
    assert treecorr.config.get(config1, 'ra_col', int) == None
    assert treecorr.config.get(config1, 'ra_col', int, 2) == 2

    config1['flip_g1'] = True
    assert treecorr.config.get(config1, 'flip_g1', bool) == True
    assert treecorr.config.get(config1, 'flip_g1', bool, False) == True
    assert treecorr.config.get(config1, 'flip_g2', bool, False) == False
    assert treecorr.config.get(config1, 'flip_g2', bool) == None

    assert treecorr.config.get_from_list(config1, 'k_col', 0, int) == 3
    assert treecorr.config.get_from_list(config1, 'k_col', 0, str) == '3'
    assert treecorr.config.get_from_list(config1, 'k_col', 0) == '3'
github rmjarvis / TreeCorr / tests / test_ng.py View on Github external
np.testing.assert_allclose(pieces_ng2.weight, full_ng.weight, rtol=1.e-7)
    np.testing.assert_allclose(pieces_ng2.xi, full_ng.xi, rtol=1.e-7)
    np.testing.assert_allclose(pieces_ng2.xi_im, full_ng.xi_im, atol=1.e-10)
    np.testing.assert_allclose(pieces_ng2.varxi, full_ng.varxi, rtol=1.e-7)

    try:
        import fitsio
    except ImportError:
        print('Skipping FITS tests, since fitsio is not installed')
        return

    # Try this with corr2
    lens_cat.write(os.path.join('data','ng_wpos_lens.fits'))
    for i, sc in enumerate(source_cats2):
        sc.write(os.path.join('data','ng_wpos_source%d.fits'%i))
    config = treecorr.read_config('configs/ng_wpos.yaml')
    config['verbose'] = 0
    treecorr.corr2(config)
    data = fitsio.read(config['ng_file_name'])
    print('data.dtype = ',data.dtype)
    np.testing.assert_allclose(data['meanr'], full_ng.meanr, rtol=1.e-7)
    np.testing.assert_allclose(data['meanlogr'], full_ng.meanlogr, rtol=1.e-7)
    np.testing.assert_allclose(data['weight'], full_ng.weight, rtol=1.e-7)
    np.testing.assert_allclose(data['gamT'], full_ng.xi, rtol=1.e-7)
    np.testing.assert_allclose(data['gamX'], full_ng.xi_im, atol=1.e-10)
    np.testing.assert_allclose(data['sigma']**2, full_ng.varxi, rtol=1.e-7)
github rmjarvis / TreeCorr / tests / test_ng.py View on Github external
r = ng.meanr
    true_gt = gamma0 * np.exp(-0.5*r**2/r0**2)

    print('ng.xi = ',ng.xi)
    print('ng.xi_im = ',ng.xi_im)
    print('true_gammat = ',true_gt)
    print('ratio = ',ng.xi / true_gt)
    print('diff = ',ng.xi - true_gt)
    print('max diff = ',max(abs(ng.xi - true_gt)))
    np.testing.assert_allclose(ng.xi, true_gt, rtol=3.e-2)
    np.testing.assert_allclose(ng.xi_im, 0, atol=1.e-4)

    # Check that we get the same result using the corr2 function:
    lens_cat.write(os.path.join('data','ng_single_lens.dat'))
    source_cat.write(os.path.join('data','ng_single_source.dat'))
    config = treecorr.read_config('configs/ng_single.yaml')
    config['verbose'] = 0
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','ng_single.out'), names=True,
                                    skip_header=1)
    print('ng.xi = ',ng.xi)
    print('from corr2 output = ',corr2_output['gamT'])
    print('ratio = ',corr2_output['gamT']/ng.xi)
    print('diff = ',corr2_output['gamT']-ng.xi)
    print('xi_im from corr2 output = ',corr2_output['gamX'])
    np.testing.assert_allclose(corr2_output['gamT'], ng.xi, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['gamX'], 0, atol=1.e-4)

    # Check that adding results with different coords or metric emits a warning.
    lens_cat2 = treecorr.Catalog(x=[0], y=[0], z=[0])
    source_cat2 = treecorr.Catalog(x=x, y=y, z=x, g1=g1, g2=g2)
    with CaptureLog() as cl:
github rmjarvis / TreeCorr / tests / test_kg.py View on Github external
r = kg.meanr
    true_kgt = kappa * gamma0 * np.exp(-0.5*r**2/r0**2)

    print('kg.xi = ',kg.xi)
    print('kg.xi_im = ',kg.xi_im)
    print('true_gammat = ',true_kgt)
    print('ratio = ',kg.xi / true_kgt)
    print('diff = ',kg.xi - true_kgt)
    print('max diff = ',max(abs(kg.xi - true_kgt)))
    np.testing.assert_allclose(kg.xi, true_kgt, rtol=1.e-2)
    np.testing.assert_allclose(kg.xi_im, 0, atol=1.e-4)

    # Check that we get the same result using the corr2 function
    lens_cat.write(os.path.join('data','kg_pairwise_lens.dat'))
    source_cat.write(os.path.join('data','kg_pairwise_source.dat'))
    config = treecorr.read_config('configs/kg_pairwise.yaml')
    config['verbose'] = 0
    with assert_warns(FutureWarning):
        treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','kg_pairwise.out'), names=True,
                                    skip_header=1)
    print('kg.xi = ',kg.xi)
    print('from corr2 output = ',corr2_output['kgamT'])
    print('ratio = ',corr2_output['kgamT']/kg.xi)
    print('diff = ',corr2_output['kgamT']-kg.xi)
    np.testing.assert_allclose(corr2_output['kgamT'], kg.xi, rtol=1.e-3)

    print('xi_im from corr2 output = ',corr2_output['kgamX'])
    np.testing.assert_allclose(corr2_output['kgamX'], 0., atol=1.e-4)
github rmjarvis / TreeCorr / tests / test_nk.py View on Github external
print('max diff = ',max(abs(xi - true_k)))
    # It turns out this doesn't come out much better.  I think the imprecision is mostly just due
    # to the smallish number of lenses, not to edge effects
    np.testing.assert_allclose(nk.xi, true_k, rtol=0.05, atol=1.e-3)

    try:
        import fitsio
    except ImportError:
        print('Skipping FITS tests, since fitsio is not installed')
        return

    # Check that we get the same result using the corr2 function
    lens_cat.write(os.path.join('data','nk_lens.fits'))
    source_cat.write(os.path.join('data','nk_source.fits'))
    rand_cat.write(os.path.join('data','nk_rand.fits'))
    config = treecorr.read_config('configs/nk.yaml')
    config['verbose'] = 0
    config['precision'] = 8
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','nk.out'), names=True, skip_header=1)
    print('nk.xi = ',nk.xi)
    print('xi = ',xi)
    print('from corr2 output = ',corr2_output['kappa'])
    print('ratio = ',corr2_output['kappa']/xi)
    print('diff = ',corr2_output['kappa']-xi)
    np.testing.assert_allclose(corr2_output['kappa'], xi, rtol=1.e-3)

    # In the corr2 context, you can turn off the compensated bit, even if there are randoms
    # (e.g. maybe you only want randoms for some nn calculation, but not nk.)
    config['nk_statistic'] = 'simple'
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','nk.out'), names=True, skip_header=1)
github rmjarvis / TreeCorr / tests / test_rperp.py View on Github external
print('Results with bin_slop = 0.1')
    print('gg.npairs = ',gg.npairs)
    print('gg.xim = ',gg.xim)
    print('theory_gammat = ',theory_gQ)
    print('ratio = ',gg.xim / theory_gQ)
    print('diff = ',gg.xim - theory_gQ)
    print('max diff = ',max(abs(gg.xim - theory_gQ)))
    assert max(abs(gg.xim - theory_gQ)) < 1.e-4
    print('gg.xim_im = ',gg.xim_im)
    assert max(abs(gg.xim_im)) < 1.e-4

    # Check that we get the same result using the corr2 function
    lens_cat.write(os.path.join('data','gg_rperp_local_lens.dat'))
    source_cat.write(os.path.join('data','gg_rperp_local_source.dat'))
    config = treecorr.read_config('configs/gg_rperp_local.yaml')
    config['verbose'] = 0
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','gg_rperp_local.out'),names=True,
                                    skip_header=1)
    print('gg.xim = ',gg.xim)
    print('from corr2 output = ',corr2_output['xim'])
    print('ratio = ',corr2_output['xim']/gg.xim)
    print('diff = ',corr2_output['xim']-gg.xim)
    np.testing.assert_allclose(corr2_output['xim'], gg.xim, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['xim_im'], gg.xim_im, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['xip'], gg.xip, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['xip_im'], gg.xip_im, rtol=1.e-3)

    with assert_raises(ValueError):
        treecorr.GGCorrelation(bin_size=bin_size, min_sep=min_sep, max_sep=max_sep, verbose=1,
                               metric='FisherRperp', bin_slop=0.1, min_rpar=50, max_rpar=-50)
github rmjarvis / TreeCorr / tests / test_rperp.py View on Github external
print('Results with bin_slop = 0.5')
    print('ng.npairs = ',ng1.npairs)
    print('ng.xi = ',ng1.xi)
    print('theory_gammat = ',theory_gt)
    print('ratio = ',ng1.xi / theory_gt)
    print('diff = ',ng1.xi - theory_gt)
    print('max diff = ',max(abs(ng1.xi - theory_gt)))
    print('ng.xi_im = ',ng1.xi_im)
    np.testing.assert_allclose(ng1.xi, theory_gt, rtol=0.5)
    np.testing.assert_allclose(ng1.xi, theory_gt, atol=1.e-3)
    np.testing.assert_allclose(ng1.xi_im, 0, atol=1.e-3)

    # Check that we get the same result using the corr2 function:
    lens_cat.write(os.path.join('data','ng_rlens_bkg_lens.dat'))
    source_cat.write(os.path.join('data','ng_rlens_bkg_source.dat'))
    config = treecorr.read_config('configs/ng_rlens_bkg.yaml')
    config['verbose'] = 0
    treecorr.corr2(config)
    corr2_output = np.genfromtxt(os.path.join('output','ng_rlens_bkg.out'), names=True,
                                    skip_header=1)
    print('ng.xi = ',ng1.xi)
    print('from corr2 output = ',corr2_output['gamT'])
    print('ratio = ',corr2_output['gamT']/ng1.xi)
    print('diff = ',corr2_output['gamT']-ng1.xi)
    np.testing.assert_allclose(corr2_output['gamT'], ng1.xi, rtol=1.e-3)
    np.testing.assert_allclose(corr2_output['gamX'], ng1.xi_im, atol=1.e-3)

    # Repeat with Arc metric
    ng2 = treecorr.NGCorrelation(bin_size=bin_size, min_sep=min_sep_arc, max_sep=max_sep_arc,
                                 metric='Arc', brute=True, min_rpar=0, sep_units='arcmin')
    ng2.process(lens_cat, source_cat)