Exemple #1
0
def test_hmf_consistency():
    """test consistency of m_cut and n_avg"""
    power_params = defaults.power_params.copy()
    power_params.camb['maxkh'] = 5000.
    power_params.camb['kmax'] = 100.
    power_params.camb['npoints'] = 1000
    C = CosmoPie(defaults.cosmology.copy(),'jdem')
    P = mps.MatterPower(C,power_params)
    C.set_power(P)

    mf_params = defaults.hmf_params.copy()
    mf_params['log10_min_mass'] = 4
    mf_params['n_grid'] = 5000
    mf = hmf.ST_hmf(C,mf_params)

    zs = np.arange(0.2,1.21,1.)
    z_fine = np.linspace(0.0,3.,4000)
    geo1 = FullSkyGeo(zs,C,z_fine)


    nz_matchers = np.zeros(4,dtype=object)
    nz_lsst_params = defaults.nz_params_lsst.copy()
    nz_matchers[0] = NZLSST(z_fine,nz_lsst_params)
    nz_wfirst_params = defaults.nz_params_wfirst_gal.copy()
    nz_wfirst_params['z_cut'] = 0.2
    nz_wfirst_params['smooth_sigma'] = 0.03
    nz_matchers[1] = NZWFirst(nz_wfirst_params)
    nz_matchers[2] = NZWFirstEff(nz_wfirst_params)
    nz_matchers[3] = NZCandel(nz_wfirst_params)

    for itr in range(0,nz_matchers.size):
        m_cuts = nz_matchers[itr].get_M_cut(mf,geo1)
        nz_got = nz_matchers[itr].get_nz(geo1)
        nz_hmf = mf.n_avg(m_cuts,z_fine)
        assert np.allclose(nz_got,nz_hmf/C.h**3)
Exemple #2
0
def util_set():
    """get set of things needed for tests"""
    omega_s = 0.02
    power_params = defaults.power_params.copy()
    C = CosmoPie(defaults.cosmology.copy(), p_space='jdem')
    P_in = mps.MatterPower(C, power_params)
    C.set_power(P_in)

    len_params = defaults.lensing_params.copy()
    len_params['z_bar'] = 1.0
    len_params['sigma'] = 0.4
    len_params['l_min'] = 30
    len_params['l_max'] = 3000
    len_params['n_l'] = 1000
    len_params['n_gal'] = 118000000 * 6.

    z_test_res1 = 0.001
    zs_test1 = np.arange(0.0005, 2., z_test_res1)

    dC_ddelta1 = ShearPower(C, zs_test1, omega_s, len_params, mode='dc_ddelta')

    sp1 = ShearPower(C, zs_test1, omega_s, len_params, mode='power')
    ls = sp1.l_starts

    z_min1 = 0.8
    z_max1 = 1.0
    #    r_min1 = C.D_comov(z_min1)
    #    r_max1 = C.D_comov(z_max1)

    z_min2 = 1.6
    z_max2 = 1.8
    #    r_min2 = C.D_comov(z_min2)
    #    r_max2 = C.D_comov(z_max2)

    QShear1_1 = QShear(dC_ddelta1, z_min1, z_max1)
    QShear1_2 = QShear(dC_ddelta1, z_min2, z_max2)

    ss_1 = Cll_q_q(sp1, QShear1_1, QShear1_2).Cll()
    return [C, dC_ddelta1, QShear1_1, QShear1_2, ss_1, ls, sp1]
Exemple #3
0
def test_cosmolike_agreement():
    """test agreement with cosmolike"""
    base_dir = './'
    input_dir = base_dir+'test_inputs/cosmolike_1/'
    cosmo_results = np.loadtxt(input_dir+'cov_results_7.dat')
    cosmo_shear = np.loadtxt(input_dir+'shear_shear_cosmo_3.dat')
    cosmo_nz = np.loadtxt(input_dir+'n_z_2.dat')

    camb_params = defaults.camb_params.copy()
    camb_params['force_sigma8'] = True
    camb_params['leave_h'] = False
    camb_params['npoints'] = 1000
    camb_params['minkh'] = 1.1e-4
    camb_params['maxkh'] = 100.
    camb_params['kmax'] = 1.
    camb_params['pivot_scalar'] = 0.05
    power_params = defaults.power_params.copy()
    power_params.camb = camb_params

    RTOL = 3.*10**-2
    ATOL = 10**-10

    cosmology_cosmolike = { 'Omegab'  :0.04868,#fixed
                            'Omegabh2':0.02204858372,#calculated
                            'Omegach2':0.12062405128,#calculated
                            'Omegamh2':0.142672635,
                            'OmegaL'  :0.685,#fixed
                            'OmegaLh2':0.310256,#calculated
                            'Omegam'  :.315, #fixed
                            'H0'      :67.3,
                            'sigma8'  :.829,#fixed
                            'h'       :0.673,#fixed
                            'Omegak'  :0.0,#fixed
                            'Omegakh2':0.0,
                            'Omegar'  :0.0,#fixed
                            'Omegarh2':0.0,
                            'ns'      :0.9603,#fixed
                            'tau'     :0.067,
                            'Yp'      :None,
                            'As'      :2.143*10**-9,
                            'LogAs'   :np.log(2.143*10**-9),
                            'w'       :-1.,
                            'de_model':'constant_w',
                            'mnu'     :0.
                          }

    C = CosmoPie(cosmology_cosmolike,p_space='basic')
    lmin_cosmo = 20
    lmax_cosmo = 5000
    n_b = 20
    area_cosmo = 1000. #deg^2
    fsky_cosmo = area_cosmo/41253. #41253 deg^2/sky
    n_gal_cosmo = 10.*(180**2/np.pi**2)*3600 #gal/rad^2
    sigma_e_cosmo = 0.27*np.sqrt(2)
    tomo_bins_cosmo = 4
#    amin_cosmo = 0.2

    n_s = sigma_e_cosmo**2/(2*n_gal_cosmo)
    print("n_s",n_s)
    #format input results
    lbin_cosmo_1 = cosmo_results[:,0]
#    lbin_cosmo_2 = cosmo_results[:,1]
#    lmid_cosmo_1 = cosmo_results[:,2]
#    lmid_cosmo_2 = cosmo_results[:,3]
    zbin_cosmo_1 = cosmo_results[:,4]
    zbin_cosmo_2 = cosmo_results[:,5]
    zbin_cosmo_3 = cosmo_results[:,6]
    zbin_cosmo_4 = cosmo_results[:,7]
    c_g_cosmo_in = cosmo_results[:,8]
    c_ssc_cosmo_in = cosmo_results[:,9]

    c_g_cosmo = np.zeros((tomo_bins_cosmo,tomo_bins_cosmo,tomo_bins_cosmo,tomo_bins_cosmo),dtype=object)
    c_ssc_cosmo = np.zeros((tomo_bins_cosmo,tomo_bins_cosmo,tomo_bins_cosmo,tomo_bins_cosmo),dtype=object)
    itr = 0
    while itr<lbin_cosmo_1.size:
        zs = np.array([int(zbin_cosmo_1[itr]),int(zbin_cosmo_2[itr]),int(zbin_cosmo_3[itr]),int(zbin_cosmo_4[itr])])
        loc_mat1 = np.zeros((n_b,n_b))
        loc_mat2 = np.zeros((n_b,n_b))
        for l1 in range(0,n_b):
            for l2 in range(0,n_b):
                loc_mat1[l1,l2] = c_g_cosmo_in[itr]
                loc_mat2[l1,l2] = c_ssc_cosmo_in[itr]
                itr+=1

        c_g_cosmo[zs[0],zs[1],zs[2],zs[3]] = loc_mat1
        c_ssc_cosmo[zs[0],zs[1],zs[2],zs[3]] = loc_mat2
    n_side = int(spp.binom(tomo_bins_cosmo+1,2))
    c_g_cosmo_flat = np.zeros((n_side*n_b,n_side*n_b))
    c_ssc_cosmo_flat = np.zeros((n_side*n_b,n_side*n_b))
    itr1 = 0
    #sanity check formatting of input results from cosmolike
    for i1 in range(tomo_bins_cosmo):
        for i2 in range(i1,tomo_bins_cosmo):
            itr2 = 0
            for i3 in range(tomo_bins_cosmo):
                for i4 in range(i3,tomo_bins_cosmo):
                    assert np.all(c_g_cosmo[i1,i2,i3,i4]==c_g_cosmo[i3,i4,i1,i2].T)
                    assert np.all(c_g_cosmo[i1,i2,i3,i4]==c_g_cosmo[i1,i2,i4,i3].T)
                    assert np.all(c_g_cosmo[i1,i2,i3,i4]==c_g_cosmo[i2,i1,i3,i4].T)
                    assert np.all(c_g_cosmo[i1,i2,i3,i4]==c_g_cosmo[i2,i1,i4,i3].T)
                    assert np.all(c_ssc_cosmo[i1,i2,i3,i4]==c_ssc_cosmo[i3,i4,i1,i2].T)
                    assert np.all(c_ssc_cosmo[i1,i2,i3,i4]==c_ssc_cosmo[i1,i2,i4,i3].T)
                    assert np.all(c_ssc_cosmo[i1,i2,i3,i4]==c_ssc_cosmo[i2,i1,i3,i4].T)
                    assert np.all(c_ssc_cosmo[i1,i2,i3,i4]==c_ssc_cosmo[i2,i1,i4,i3].T)
                    c_g_cosmo_flat[itr1:itr1+n_b,itr2:itr2+n_b] = c_g_cosmo[i1,i2,i3,i4]
                    c_ssc_cosmo_flat[itr1:itr1+n_b,itr2:itr2+n_b] = c_ssc_cosmo[i1,i2,i3,i4]
                    if itr2!=itr1:
                        c_g_cosmo_flat[itr2:itr2+n_b,itr1:itr1+n_b] = c_g_cosmo_flat[itr1:itr1+n_b,itr2:itr2+n_b].T
                        c_ssc_cosmo_flat[itr2:itr2+n_b,itr1:itr1+n_b] = c_ssc_cosmo_flat[itr1:itr1+n_b,itr2:itr2+n_b].T
                    itr2+=n_b
            itr1+=n_b
    assert np.all(c_g_cosmo_flat==c_g_cosmo_flat.T)
    assert np.all(c_ssc_cosmo_flat==c_ssc_cosmo_flat.T)

    Cll_sh_sh_cosmo = np.zeros((tomo_bins_cosmo,tomo_bins_cosmo),dtype=object)
    itr = 0
    for i in range(tomo_bins_cosmo):
        for j in range(i,tomo_bins_cosmo):
            Cll_sh_sh_cosmo[i,j] = cosmo_shear[:,itr+1]
            if not i==j:
                Cll_sh_sh_cosmo[j,i] = Cll_sh_sh_cosmo[i,j]
            itr += 1

    l_starts = np.zeros(n_b)
    log_dl = (np.log(lmax_cosmo)-np.log(lmin_cosmo))/n_b
    l_mids = np.zeros(n_b)
    l_ends = np.zeros(n_b)
    dls = np.zeros(n_b)

    for i in range(l_mids.size):
        l_starts[i] = np.exp(np.log(lmin_cosmo)+i*log_dl)
        l_ends[i] = np.exp(np.log(lmin_cosmo)+(i+1)*log_dl)
        l_mids[i] = np.exp(np.log(lmin_cosmo)+(i+0.5)*log_dl)
        dls[i] = (np.exp(np.log(lmin_cosmo)+(i+1.)*log_dl)- np.exp(np.log(lmin_cosmo)+i*log_dl))


    z_fine = cosmo_nz[:,0]
    n_z = cosmo_nz[:,1]
    #prevent going to 0 badly
    z_fine[0] +=0.00001
    cum_n_z = cumtrapz(n_z,z_fine,initial=0.)
    cum_n_z = cum_n_z/cum_n_z[-1]
    z_bin_starts = np.zeros(tomo_bins_cosmo)
#    r_bins = np.zeros((tomo_bins_cosmo,2))
    for i in range(0,tomo_bins_cosmo):
        z_bin_starts[i] = np.min(z_fine[cum_n_z>=1./tomo_bins_cosmo*i])

    P_in = mps.MatterPower(C,power_params)
    C.set_power(P_in)
    #theta0 = np.pi/4.
    #theta1 = np.pi/2.
    #theta_in = np.pi/3.
    #phi0 = np.pi/4.
    #phi1 = np.pi/4.+np.pi/4.*0.5443397550644993
    #phi_in = np.pi/4.+0.01

    #thetas = np.array([theta0,theta1,theta1,theta0,theta0])
    #phis = np.array([phi0,phi0,phi1,phi1,phi0])
    z_coarse = np.hstack([z_bin_starts,2.])
    #geo1 = PolygonGeo(z_coarse,thetas,phis,theta_in,phi_in,C,z_fine.copy(),40,{'n_double':30})
    geo1 = CircleGeo(z_coarse,C,0.31275863997971481,100,z_fine.copy(),40,{'n_double':30})
    assert np.isclose((geo1.angular_area()*180**2/np.pi**2),1000.)
    #r_bins = geo1.rbins
    z_bins = geo1.zbins


    len_params = defaults.lensing_params.copy()
    len_params['smodel'] = 'gaussian'
    len_params['zbar'] = 1.
    len_params['n_gal'] = n_gal_cosmo
    len_params['sigma2_e'] = sigma_e_cosmo**2
    len_params['l_min'] = np.min(l_starts)
    len_params['l_max'] = np.max(l_ends)
    len_params['n_l'] = l_starts.size
    len_params['pmodel'] = 'halofit'
    #test lensing observables
    sp = ShearPower(C,z_fine,fsky_cosmo,len_params,mode='power')
    qs = np.zeros(tomo_bins_cosmo,dtype=object)
    for i in range(qs.size):
        qs[i] = QShear(sp,z_bins[i,0],z_bins[i,1])
    Cll_sh_sh = np.zeros((tomo_bins_cosmo,tomo_bins_cosmo),dtype=object)
    ratio_means = np.zeros((tomo_bins_cosmo,tomo_bins_cosmo))
    for i in range(qs.size):
        for j in range(qs.size):
            Cll_sh_sh[i,j] = Cll_q_q(sp,qs[i],qs[j]).Cll()
            ratio_means[i,j] = np.average(Cll_sh_sh[i,j]/Cll_sh_sh_cosmo[i,j])
            assert np.allclose(Cll_sh_sh[i,j],Cll_sh_sh_cosmo[i,j],rtol=RTOL,atol=ATOL)

    print("average Cll ratio: ",np.average(ratio_means))
    print("mse Cll: ",np.linalg.norm(np.linalg.norm(1.-Cll_sh_sh/Cll_sh_sh_cosmo))/(Cll_sh_sh.size*Cll_sh_sh[0,0].size))

    c_g_flat = np.zeros_like(c_g_cosmo_flat)
    c_ssc_flat = np.zeros_like(c_g_cosmo_flat)

    #first test individually by manually building covariance matrix
    itr1 = 0
    for i1 in range(tomo_bins_cosmo):
        for i2 in range(i1,tomo_bins_cosmo):
            itr2 = 0
            for i3 in range(tomo_bins_cosmo):
                for i4 in range(i3,tomo_bins_cosmo):
                    ns = np.array([0.,0.,0.,0.])
                    if i1==i3:
                        ns[0] = n_s
                    if i1==i4:
                        ns[1] = n_s
                    if i2==i4:
                        ns[2] = n_s
                    if i2==i3:
                        ns[3] = n_s
                    qs_in = np.array([qs[i1],qs[i2],qs[i3],qs[i4]])
                    c_g_flat[itr1:itr1+n_b,itr2:itr2+n_b] = np.diagflat(sp.cov_g_diag(qs_in,ns))
                    #c_ssc_flat[itr1:itr1+n_b,itr2:itr2+n_b] = c_ssc[i1,i2,i3,i4]
                    if not itr2==itr1:
                        c_g_flat[itr2:itr2+n_b,itr1:itr1+n_b] = c_g_flat[itr1:itr1+n_b,itr2:itr2+n_b].T
                    #    c_ssc_flat[itr2:itr2+n_b,itr1:itr1+n_b] = c_ssc_flat[itr1:itr1+n_b,itr2:itr2+n_b].T
                    itr2+=n_b
            itr1+=n_b
    assert np.all(c_g_flat==c_g_flat.T)
    assert np.allclose(c_g_flat,c_g_cosmo_flat,atol=ATOL,rtol=RTOL)
    assert np.abs(1.-np.average(ratio_means))<0.01
    assert np.all(c_ssc_flat==c_ssc_flat.T)

    #test with SWSurvey pipeline with 1000 sq deg spherical rectangle geo
    sw_params = {'needs_lensing':True,'cross_bins':True}
    observable_list = np.array(['len_shear_shear'])
    sw_survey = SWSurvey(geo1,'c_s',C,sw_params,observable_list=observable_list,len_params=len_params)
#    C_pow = sw_survey.len_pow.C_pow

    c_g_sw = sw_survey.get_non_SSC_sw_covar_arrays()[0]
    nonzero_mask = c_g_sw!=0.
    rat_c = c_g_cosmo_flat[nonzero_mask]/c_g_sw[nonzero_mask]
#    rat_m = c_g_flat[nonzero_mask]/c_g_sw[nonzero_mask]
    assert np.allclose(c_g_sw,c_g_flat)
    assert np.allclose(c_g_sw,c_g_cosmo_flat,atol=ATOL,rtol=RTOL)

    print("mean squared diff covariances:"+str(np.linalg.norm(1.-rat_c)/rat_c.size))


    print("PASS: all assertions passed")
        'npoints': 2000,
        'minkh': 1.1e-4,
        'maxkh': 1.476511342960e+02,
        'kmax': 1.476511342960e+02,
        'leave_h': False,
        'force_sigma8': False,
        'return_sigma8': False,
        'accuracy': 1,
        'pivot_scalar': 0.002
    }
    print("main: building cosmology")
    power_params = defaults.power_params.copy()
    power_params.camb = camb_params
    C = CosmoPie(defaults.cosmology.copy(), p_space='jdem')
    P_lin = mps.MatterPower(C, power_params)
    C.set_power(P_lin)
    time1 = time()

    #x_cut = 527.
    #l_max = 511
    #x_cut = 360
    #l_max = 346
    #x_cut = 150
    #l_max = 139
    #x_cut = 93
    l_max = 84
    x_cut = 5
    #l_max = 20

    do_plot = True
Exemple #5
0
        'n_params': n_params_lsst,
        'mf_params': mf_params
    }])
    basis_params = defaults.basis_params.copy()
    basis_params['n_bessel_oversample'] = 400000  #*10

    #creat a CosmoPie object to manage the cosmology details
    print("main: begin constructing CosmoPie")
    C = CosmoPie(cosmo, p_space=p_space)
    print("main: finish constructing CosmoPie")

    #get the matter power spectrum and give it to the CosmoPie
    print("main: begin constructing MatterPower")
    P = MatterPower(C, power_params)
    print("main: finish constructing MatterPower")
    C.set_power(P)

    #create the WFIRST geometry
    #zs are the bounding redshifts of the tomographic bins
    zs = np.arange(0.2, 3.01, 0.4)
    zs_lsst = np.linspace(0., 1.2, 3)
    #z_fine are the resolution redshift slices to be integrated over
    z_fine = np.linspace(0.001, np.max([zs[-1], zs_lsst[-1]]), 500)

    #l_max is the highest l that should be precomputed
    l_max = 72
    res_healpix = 6
    use_pixels = False
    print("main: begin constructing WFIRST PolygonGeo")
    if use_pixels:
        geo_wfirst = WFIRSTPixelGeo(zs, C, z_fine, l_max, res_healpix)
def test_full_sky():
    """do some tests with a full sky geo known results"""
    #get dictionaries of parameters that various functions will need
    #cosmo = defaults.cosmology_wmap.copy()
    cosmo = {   'Omegabh2':0.02223,
                'Omegach2':0.1153,
                'Omegab'  :0.04283392714316876,
                'Omegac'  :0.22216607285683124,
                'Omegamh2':0.13752999999999999,
                'OmegaL'  :0.735,
                'OmegaLh2':0.38145113207547166,
                'Omegam'  :0.265,
                'H0'      :72.04034509047493,
                'sigma8'  : 0.8269877678406697, #from the code
                'h'       :0.7204034509047493,
                'Omegak'  : 0.0,
                'Omegakh2': 0.0,
                'Omegar'  : 0.0,
                'Omegarh2': 0.0,
                'ns'      : 0.9608,
                'tau'     : 0.081,
                'Yp'      :0.299,
                'As'      : 2.464*10**-9,
                'LogAs'   :-19.821479791275138,
                'w'       :-1.0,
                'de_model':'constant_w',#dark energy model
                'mnu'     :0.}
    cosmo['de_model'] = 'constant_w'
    cosmo['wa'] = 0.
    cosmo['w0'] = -1.
    cosmo['w'] = -1.
    p_space = 'jdem'
    camb_params = defaults.camb_params.copy()
    camb_params['force_sigma8'] = False
    camb_params['maxkh'] = 100.
    camb_params['kmax'] = 30.
    camb_params['npoints'] = 10000
    camb_params['pivot_scalar'] = 0.002

    power_params = defaults.power_params.copy()
    power_params.camb = camb_params
    power_params.camb['accuracy'] = 1
    #prior_params = defaults.prior_fisher_params.copy()


    zs = np.array([0.0001,3.])
    z_fine = np.linspace(0.0001,zs[-1],10000)

    basis_params = defaults.basis_params.copy()
    basis_params['n_bessel_oversample'] = 400000*10

    C = CosmoPie(cosmo,p_space=p_space)
    l_max = 0
    x_cut = 10*np.pi

    z_max = z_fine[-1]+0.000001
    r_max = C.D_comov(z_max)
    k_cut = x_cut/r_max
    P = MatterPower(C,power_params)
    C.set_power(P)
    geo1 = FullSkyGeo(zs,C,z_fine)

    basis = SphBasisK(r_max,C,k_cut,basis_params,l_ceil=l_max,needs_m=True)

    ddbar = basis.get_ddelta_bar_ddelta_alpha(geo1,tomography=True)
    ns = np.arange(1,ddbar.size+1)
    ddbar_pred = -3./(2.*np.pi**(5./2.))*(-1)**ns/ns**2
    assert np.allclose(ddbar,ddbar_pred)
    cov_pred = np.zeros((ns.size,ns.size))
    xs = P.k*r_max
    P_in = P.get_matter_power(np.array([0.]),pmodel='linear')[:,0]
    for i1 in range(0,ns.size):
        for i2 in range(i1,ns.size):
            integrand1 = 8.*np.pi**3*(-1)**(ns[i1]+ns[i2])*ns[i1]**2*ns[i2]**2/r_max**3*np.sin(xs)**2/((ns[i1]**2*np.pi**2-xs**2)*(ns[i2]**2*np.pi**2-xs**2))
            cov_pred[i1,i2] = np.trapz(integrand1*P_in,xs)
            if i1!=i2:
                cov_pred[i2,i1] = cov_pred[i1,i2]
    cov_got = basis.get_covar_array()
    var_pred = np.dot(ddbar_pred.T,np.dot(cov_pred,ddbar_pred))
    var_got = basis.get_variance(geo1)
    assert np.all(cov_got==cov_got.T)
    assert np.allclose(basis.C_id[:,1],ns*np.pi/r_max)
    assert np.allclose(cov_pred,cov_got,atol=1.e-15,rtol=1.e-3)
    assert np.isclose(var_got[0,0],var_pred,atol=1.e-15,rtol=1.e-3)
    ddbar_got2 = basis.get_dO_I_ddelta_alpha(geo1,geo1.r_fine**2)*3./r_max**3
    assert np.allclose(ddbar,ddbar_got2,rtol=1.e-3)
def test_half_sky():
    """do some tests with a half sky geo known results"""
    #get dictionaries of parameters that various functions will need
    cosmo = {   'Omegabh2':0.0222,
                'Omegach2':0.1153,
                'Omegab'  :0.04283392714316876,
                'Omegac'  :0.22216607285683124,
                'Omegamh2':0.13752999999999999,
                'OmegaL'  :0.735,
                'OmegaLh2':0.38145113207547166,
                'Omegam'  :0.265,
                'H0'      :72.04034509047493,
                'sigma8'  : 0.8269877678406697, #from the code
                'h'       :0.7204034509047493,
                'Omegak'  : 0.0,
                'Omegakh2': 0.0,
                'Omegar'  : 0.0,
                'Omegarh2': 0.0,
                'ns'      : 0.9608,
                'tau'     : 0.081,
                'Yp'      :0.299,
                'As'      : 2.464*10**-9,
                'LogAs'   :-19.821479791275138,
                'w'       :-1.0,
                'de_model':'constant_w',#dark energy model
                'mnu'     :0.}
    cosmo['de_model'] = 'constant_w'
    cosmo['wa'] = 0.
    cosmo['w0'] = -1.
    cosmo['w'] = -1.
    p_space = 'jdem'
    camb_params = defaults.camb_params.copy()
    camb_params['force_sigma8'] = False
    camb_params['maxkh'] = 100.
    camb_params['kmax'] = 30.
    camb_params['npoints'] = 10000
    camb_params['pivot_scalar'] = 0.002

    power_params = defaults.power_params.copy()
    power_params.camb = camb_params
    power_params.camb['accuracy'] = 1
    #prior_params = defaults.prior_fisher_params.copy()


    zs = np.array([0.0001,3.])
    z_fine = np.linspace(0.0001,zs[-1],10000)

    basis_params = defaults.basis_params.copy()
    basis_params['n_bessel_oversample'] = 400000*10
    #test with half sky using results calculated in mathematica
    basis_params = defaults.basis_params.copy()
    basis_params['n_bessel_oversample'] = 400000*10

    C = CosmoPie(cosmo,p_space=p_space)
    l_max = 4
    x_cut = 10

    z_max = z_fine[-1]+0.000001
    r_max = C.D_comov(z_max)
    k_cut = x_cut/r_max

    geo1 = HalfSkyGeo(zs,C,z_fine)

    P = MatterPower(C,power_params)
    P.P_lin = 1./(P.k*r_max)
    C.set_power(P)

    basis = SphBasisK(r_max,C,k_cut,basis_params,l_ceil=l_max,needs_m=False)
    basis2 = SphBasisK(r_max,C,k_cut,basis_params,l_ceil=l_max,needs_m=True)

    ddbar = basis.get_ddelta_bar_ddelta_alpha(geo1,tomography=True)
    ddbar2 = basis2.get_ddelta_bar_ddelta_alpha(geo1,tomography=True)
    ddbar_got2 = basis.get_dO_I_ddelta_alpha(geo1,geo1.r_fine**2)*3./r_max**3

    #ns = np.arange(1,ddbar.size+1)
    cov_got = basis.get_covar_array()
    cov_got2 = basis2.get_covar_array()
    var_got = basis.get_variance(geo1)
    var_got2 = basis2.get_variance(geo1)
    cov_pred0 = np.array([[3.220393570704632e-11,-9.51309537893619e-12,9.955267964241254e-12],[-9.51309537893619e-12,5.937410951604845e-11,-1.2342996089442917e-11],[9.955267964241254e-12,-1.2342996089442917e-11,8.524795995567036e-11]])
    cov_pred1 = np.array([[4.306831299006748e-11,-7.977015797357624e-12],[-7.977015797357624e-12,6.907871421795387e-11]])
    cov_pred2 = np.array([[5.467066924435653e-11,-7.056047462103054e-12],[-7.056047462103054e-12,7.984696153964485e-11]])
    cov_pred3 = np.array([[6.70079506270691e-11]])
    cov_pred4 = np.array([[7.985214180284307e-11]])
    cov_pred = np.zeros((ddbar.size,ddbar.size))
    cov_pred[0:3,0:3] = cov_pred0
    cov_pred[3:5,3:5] = cov_pred1
    cov_pred[5:7,5:7] = cov_pred2
    cov_pred[7,7] = cov_pred3
    cov_pred[8,8] = cov_pred4
    var_pred = np.dot(ddbar,np.dot(cov_pred,ddbar.T))
    var_pred2 = np.dot(ddbar2,np.dot(cov_got2,ddbar2.T))
    assert np.all(cov_got==cov_got.T)
    assert np.all(cov_got2==cov_got2.T)
    assert np.isclose(var_got[0,0],var_pred,atol=1.e-15,rtol=1.e-3)
    assert np.isclose(var_got2[0,0],var_pred2,atol=1.e-15,rtol=1.e-3)
    assert np.isclose(var_got2[0,0],var_pred,atol=1.e-15,rtol=1.e-3)
    assert np.allclose(cov_pred,cov_got,atol=1.e-15,rtol=1.e-3)
    assert np.allclose(ddbar,ddbar_got2,rtol=1.e-3,atol=1.e-15)
    for itr in range(0,basis.C_id.shape[0]):
        if basis.C_id[itr,0]>0 and np.mod(basis.C_id[itr,0],2)==0.:
            assert ddbar[0,itr] == 0.
    for itr in range(0,basis2.C_id.shape[0]):
        if basis2.C_id[itr,0]>0 and np.mod(basis2.C_id[itr,0],2)==0.:
            assert ddbar2[0,itr] == 0.
        elif basis2.C_id[itr,2]!=0:
            assert ddbar2[0,itr] == 0.
Exemple #8
0
def test_rotational_invariance():
    """test invariance of results under global rotations"""
    camb_params = { 'npoints':2000,
                    'minkh':1.1e-4,
                    'maxkh':1.476511342960e+02,
                    'kmax':1.476511342960e+02,
                    'leave_h':False,
                    'force_sigma8':False,
                    'return_sigma8':False,
                    'accuracy':1,
                    'pivot_scalar':0.002
                  }
    print("main: building cosmology")
    power_params = defaults.power_params.copy()
    power_params.camb = camb_params
    power_params.camb['accuracy'] = 1
    power_params.camb['maxkh'] = 100.
    power_params.camb['kmax'] = 30.
    power_params.camb['npoints'] = 1000
    C = CosmoPie(defaults.cosmology.copy(),p_space='jdem')
    P_lin = mps.MatterPower(C,power_params)
    C.set_power(P_lin)

    l_max = 23
    x_cut = 30.

    print("main: building geometries")
    polygon_params = defaults.polygon_params.copy()
    polygon_params['n_double'] = 80
    z_coarse = np.array([0.2,1.,2.,3.])
    zs_lsst = np.linspace(0.,1.2,3)
    #z_max = np.max(z_coarse)
    #z_fine = np.arange(0.0001,z_max,0.0001)
    z_fine = np.linspace(0.001,3.,500)
    z_max = z_fine[-1]+0.001

    print("main: building basis")
    basis_params = defaults.basis_params.copy()
    basis_params['n_bessel_oversample'] = 400000
    basis_params['x_grid_size'] = 100000

    r_max = C.D_comov(z_max)
    k_cut = x_cut/r_max

    basis = SphBasisK(r_max,C,k_cut,basis_params,l_ceil=l_max)

    geo1 = WFIRSTGeo(z_coarse,C,z_fine,l_max,polygon_params)#HalfSkyGeo(z_coarse,C,z_fine)
    geo2 = LSSTGeo(zs_lsst,C,z_fine,l_max,polygon_params)#HalfSkyGeo(z_coarse,C,z_fine)
    geo1_rot1 = AlmRotGeo(geo1,C,z_coarse,z_fine,np.array([0.,np.pi/2.,np.pi]),polygon_params['n_double'])
    geo1_rot2 = AlmRotGeo(geo1_rot1,C,z_coarse,z_fine,np.array([0.,np.pi/2.,np.pi]),polygon_params['n_double'])
    geo1_rot3 = AlmRotGeo(geo1_rot2,C,z_coarse,z_fine,np.array([0.005,1.2496,1.72]),polygon_params['n_double'])
    geo2_rot1 = AlmRotGeo(geo2,C,zs_lsst,z_fine,np.array([0.,np.pi/2.,np.pi]),polygon_params['n_double'])
    geo2_rot2 = AlmRotGeo(geo2_rot1,C,zs_lsst,z_fine,np.array([0.,np.pi/2.,np.pi]),polygon_params['n_double'])
    geo2_rot3 = AlmRotGeo(geo2_rot2,C,zs_lsst,z_fine,np.array([0.005,1.2496,1.72]),polygon_params['n_double'])
    assert np.allclose(geo1.get_alm_array(l_max),geo1_rot2.get_alm_array(l_max))
    var_geo1 = basis.get_variance(geo1,k_cut_in=k_cut)
    var_geo1_rot1 = basis.get_variance(geo1_rot1,k_cut_in=k_cut)
    var_geo1_rot2 = basis.get_variance(geo1_rot2,k_cut_in=k_cut)
    var_geo1_rot3 = basis.get_variance(geo1_rot3,k_cut_in=k_cut)
    assert np.allclose(var_geo1,var_geo1_rot1,atol=1.e-20,rtol=1.e-8)
    assert np.allclose(var_geo1,var_geo1_rot2,atol=1.e-20,rtol=1.e-8)
    assert np.allclose(var_geo1,var_geo1_rot3,atol=1.e-20,rtol=1.e-8)
    var_geo2 = basis.get_variance(geo2,k_cut_in=k_cut)
    var_geo2_rot1 = basis.get_variance(geo2_rot1,k_cut_in=k_cut)
    var_geo2_rot2 = basis.get_variance(geo2_rot2,k_cut_in=k_cut)
    var_geo2_rot3 = basis.get_variance(geo2_rot3,k_cut_in=k_cut)
    assert np.allclose(var_geo2,var_geo2_rot1,atol=1.e-20,rtol=1.e-8)
    assert np.allclose(var_geo2,var_geo2_rot2,atol=1.e-20,rtol=1.e-8)
    assert np.allclose(var_geo2,var_geo2_rot3,atol=1.e-20,rtol=1.e-8)

    cosmo_par_list = np.array(['ns','Omegamh2','Omegabh2','OmegaLh2','LogAs','w'])
    cosmo_par_eps = np.array([0.002,0.00025,0.0001,0.00025,0.1,0.01])

    nz_params_wfirst_lens = defaults.nz_params_wfirst_lens.copy()
    nz_params_wfirst_lens['i_cut'] = 26.3
    nz_params_wfirst_lens['data_source'] = './data/CANDELS-GOODSS2.dat'
    nz_wfirst_lens = NZWFirstEff(nz_params_wfirst_lens)
    sw_params = defaults.sw_survey_params.copy()
    lw_params = defaults.lw_survey_params.copy()
    sw_observable_list = defaults.sw_observable_list.copy()
    lw_observable_list = defaults.lw_observable_list.copy()
    len_params = defaults.lensing_params.copy()
    mf_params = defaults.hmf_params.copy()
    mf_params['n_grid'] = 2000
    mf_params['log10_min_mass'] = 10.
    n_params_lsst = defaults.nz_params_lsst_use.copy()
    n_params_lsst['i_cut'] = 24.1 #gold standard subset of LSST 1 year (10 year 25.3)

    dn_params = defaults.dn_params.copy()
    dn_params['nz_select'] = 'LSST'
    dn_params['sigma0'] = 0.1
    lw_param_list = np.array([{'dn_params':dn_params,'n_params':n_params_lsst,'mf_params':mf_params}])
    prior_params = defaults.prior_fisher_params.copy()

    sw_survey_geo1 = SWSurvey(geo1,'geo1',C,sw_params,cosmo_par_list,cosmo_par_eps,sw_observable_list,len_params,nz_wfirst_lens)
    sw_survey_geo1_rot1 = SWSurvey(geo1_rot1,'geo1_rot1',C,sw_params,cosmo_par_list,cosmo_par_eps,sw_observable_list,len_params,nz_wfirst_lens)
    sw_survey_geo1_rot2 = SWSurvey(geo1_rot2,'geo1_rot2',C,sw_params,cosmo_par_list,cosmo_par_eps,sw_observable_list,len_params,nz_wfirst_lens)
    sw_survey_geo1_rot3 = SWSurvey(geo1_rot3,'geo1_rot3',C,sw_params,cosmo_par_list,cosmo_par_eps,sw_observable_list,len_params,nz_wfirst_lens)

    survey_lw1 = LWSurvey(np.array([geo1,geo2]),'lw1',basis,C,lw_params,observable_list=lw_observable_list,param_list=lw_param_list)
    survey_lw1_rot1 = LWSurvey(np.array([geo1_rot1,geo2_rot1]),'lw1',basis,C,lw_params,observable_list=lw_observable_list,param_list=lw_param_list)
    survey_lw1_rot2 = LWSurvey(np.array([geo1_rot2,geo2_rot2]),'lw1',basis,C,lw_params,observable_list=lw_observable_list,param_list=lw_param_list)
    survey_lw1_rot3 = LWSurvey(np.array([geo1_rot3,geo2_rot3]),'lw1',basis,C,lw_params,observable_list=lw_observable_list,param_list=lw_param_list)
    SS_geo1 = SuperSurvey(np.array([sw_survey_geo1]),np.array([survey_lw1]),basis,C,prior_params,get_a=True,do_unmitigated=True,do_mitigated=True,include_sw=True)
    SS_geo1_rot1 = SuperSurvey(np.array([sw_survey_geo1_rot1]),np.array([survey_lw1_rot1]),basis,C,prior_params,get_a=True,do_unmitigated=True,do_mitigated=True,include_sw=True)
    SS_geo1_rot2 = SuperSurvey(np.array([sw_survey_geo1_rot2]),np.array([survey_lw1_rot2]),basis,C,prior_params,get_a=True,do_unmitigated=True,do_mitigated=True,include_sw=True)
    SS_geo1_rot3 = SuperSurvey(np.array([sw_survey_geo1_rot3]),np.array([survey_lw1_rot3]),basis,C,prior_params,get_a=True,do_unmitigated=True,do_mitigated=True,include_sw=True)

    for i in xrange(0,3):
        for j in xrange(1,3):
            assert np.allclose(SS_geo1.f_set_nopriors[i][j].get_covar(),SS_geo1_rot1.f_set_nopriors[i][j].get_covar(),atol=1.e-30,rtol=1.e-8)
            assert np.allclose(SS_geo1.f_set_nopriors[i][j].get_covar(),SS_geo1_rot2.f_set_nopriors[i][j].get_covar(),atol=1.e-30,rtol=1.e-8)
            assert np.allclose(SS_geo1.f_set_nopriors[i][j].get_covar(),SS_geo1_rot3.f_set_nopriors[i][j].get_covar(),atol=1.e-30,rtol=1.e-8)
    a_geo1 = SS_geo1.multi_f.get_a_lw()
    a_geo1_rot1 = SS_geo1_rot1.multi_f.get_a_lw()
    a_geo1_rot2 = SS_geo1_rot2.multi_f.get_a_lw()
    a_geo1_rot3 = SS_geo1_rot3.multi_f.get_a_lw()
    assert np.allclose(a_geo1[0],a_geo1_rot1[0],atol=1.e-20,rtol=1.e-8)
    assert np.allclose(a_geo1[1],a_geo1_rot1[1],atol=1.e-20,rtol=1.e-8)
    assert np.allclose(a_geo1[0],a_geo1_rot2[0],atol=1.e-20,rtol=1.e-8)
    assert np.allclose(a_geo1[1],a_geo1_rot2[1],atol=1.e-20,rtol=1.e-8)
    assert np.allclose(a_geo1[0],a_geo1_rot3[0],atol=1.e-20,rtol=1.e-8)
    assert np.allclose(a_geo1[1],a_geo1_rot3[1],atol=1.e-20,rtol=1.e-8)
    assert np.allclose(a_geo1[0],var_geo1,atol=1.e-30,rtol=1.e-13)
    assert np.allclose(a_geo1_rot1[0],var_geo1_rot1,atol=1.e-30,rtol=1.e-11)
    assert np.allclose(a_geo1_rot2[0],var_geo1_rot2,atol=1.e-30,rtol=1.e-11)
    assert np.allclose(a_geo1_rot3[0],var_geo1_rot3,atol=1.e-30,rtol=1.e-11)

    for i in xrange(0,2):
        for j in xrange(0,2):
            eig_geo1 = SS_geo1.eig_set[i][j][0]
            eig_geo1_rot1 = SS_geo1_rot1.eig_set[i][j][0]
            eig_geo1_rot2 = SS_geo1_rot2.eig_set[i][j][0]
            eig_geo1_rot3 = SS_geo1_rot3.eig_set[i][j][0]
            assert np.allclose(eig_geo1,eig_geo1_rot1)
            assert np.allclose(eig_geo1,eig_geo1_rot2)
            assert np.allclose(eig_geo1,eig_geo1_rot3)
Exemple #9
0
def test_rint_match_mp():
    """
    Test numeric integral compared to mp arbitrary precision
    Both the numeric and mp results should agree
    scipy's hypergeometric functions would fail this test because of a catastrophic loss of precision,
    mp works but to avoid mp dependency we just use numerical method
    """
    print("test_rint_match_mp: begin testing numeric r integral agreement with exact (arbitrary precision) mp solution")
    ZERO_TOLERANCE = 10e-13
    MAX_TOLERANCE = 10e-11
    AVG_TOLERANCE = 10e-13
    ls = np.arange(0,70)
    k_cut = 0.015
    r_max = 4000

    r1 = 100
    r2 = 3000

    r1_m = mp.mpf(r1)
    r2_m = mp.mpf(r2)

    mp.dps = 200
    diff_tot = 0.
    diff_max = 0.
    zero_diff = 0.
    diff_tot2 = 0.
    diff_max2 = 0.
    zero_diff2 = 0.
    n_count = 0
    z_count = 0
    bad_count = 0
    C = CosmoPie(defaults.cosmology.copy(),p_space='jdem')
    P_lin = mps.MatterPower(C,defaults.power_params.copy())
    C.set_power(P_lin)
    basis = sph.SphBasisK(r_max,C,k_cut,defaults.basis_params.copy())
    rints = basis.gen_R_cache(np.array([[r1,r2]]))
    for ll in ls:
        ks = jn_zeros_cut(ll,k_cut*r_max)/r_max
        ll_m = mp.mpf(ll)
        for i in range(0,ks.size):
            kk_m = mp.mpf(ks[i])

            hyp1 = mp.hyp1f2(1.5+ll_m/2.,2.5+ll_m/2.,1.5+ll_m,-1./4*kk_m**2*r2_m**2)*r2_m**(3+ll_m)
            hyp2 = mp.hyp1f2(1.5+ll_m/2.,2.5+ll_m/2.,1.5+ll_m,-1./4.*kk_m**2*r1_m**2)*r1_m**(3+ll_m)
            r_int_exact = float(1./((3.+ll_m)*mp.gamma(1.5+ll_m))/2.**(ll_m+1.)*kk_m**ll_m*mp.sqrt(mp.pi)*(hyp1-hyp2))
            r_int_compute = sph.R_int([r1,r2],ks[i],ll)
            #r_int_compute2 = (basis.rints[ll](r2*ks[i])-basis.rints[ll](r1*ks[i]))/ks[i]**3
            r_int_compute2 = rints[(ks[i],ll)]*(r2**3-r1**3)/3.
            if np.abs(r_int_exact)>0.:
                diff = np.abs(r_int_compute-r_int_exact)/r_int_exact
                diff2 = np.abs(r_int_compute2-r_int_exact)/r_int_exact
                n_count+=1
                diff_tot+=diff
                diff_tot2+=diff2
                if diff>MAX_TOLERANCE:
                    #print("test_rint_match_mp: error outside tolerance at l,k,numeric,mp: ",ll,ks[i],r_int_compute,r_int_exact)
                    bad_count+=1
                diff_max = max(diff_max,diff)
                diff_max2 = max(diff_max2,diff2)
            else:
                zero_diff+=r_int_compute
                zero_diff2+=r_int_compute2
                z_count+=1
    print("test_rint_match_mp: zero diff, n zero",zero_diff,z_count)
    print("test_rint_match_mp: max diff, avg diff,n",diff_max,diff_tot/n_count,n_count)
    print("test_rint_match_mp: max diff tolerance, avg diff tolerance",MAX_TOLERANCE,AVG_TOLERANCE)
    print("test_rint_match_mp: n values outside tolerance",bad_count)
    if z_count>0:
        assert ZERO_TOLERANCE>zero_diff/z_count
        assert ZERO_TOLERANCE>zero_diff2/z_count
    assert MAX_TOLERANCE>diff_max
    assert MAX_TOLERANCE>diff_max2
    assert AVG_TOLERANCE>diff_tot/n_count
    assert AVG_TOLERANCE>diff_tot2/n_count
    print("test_rint_match_mp: finished testing numeric r integral agreement with exact (arbitrary precision) mp solution")