Exemplo n.º 1
0
def execute(block, config):
    nz_dir = config['nz_dir']
    r_array = config['r_array']
    do_save_xi = config['do_save_xi']
    use_mean_z = config['use_mean_z']
    k_hres = config['k_hres']
    Pkth_array_khres = config['Pkth_array_khres']
    xi_all = config['xi_all']
    bias_section = config['bias_section']
    constraint_b2 = config['constraint_b2']
    pt_type = config['pt_type']
    nbins = config['nbins']

    xi_mm = xi_all[1]
    Pk_mm = Pkth_array_khres[1]
    z_pk = config['z_pk']

    block.put_double_array_1d("pk_to_xi", "r", r_array)
    block.put_double_array_1d("pk_to_xi", "k", k_hres)


    for j in range(nbins):

        xi_gm_bin_f, xi_gg_bin_f, xi_mm_bin_f = np.zeros(len(r_array)), np.zeros(len(r_array)), np.zeros(len(r_array))
        xi_gm_mm_bin_f, xi_gg_mm_bin_f = np.zeros(len(r_array)), np.zeros(len(r_array))

        filename = nz_dir + 'nz_g_m_' + '_zbin_' + str(j + 1) + '_dsg_' + str(1) + '_dsm_' + str(1) + '.pk'
        nz_data = pk.load(open(filename, 'rb'))

        nz_g, nz_m, nz_z = nz_data['nz_g'], nz_data['nz_m'], nz_data['nz_z']

        zmean_g = get_zmean(nz_z, nz_g)
        zmean_m = get_zmean(nz_z, nz_m)
        zmean_gm = (zmean_g + zmean_m) / 2.

        param_array_bin1 = get_bias_param_bin_i(block, j, bias_section, constraint_b2, pt_type=pt_type)
        param_array_bin2 = get_bias_param_bin_i(block, j, bias_section, constraint_b2, pt_type=pt_type)

        xi_gg, _ = get_xiXX_terms_bins(param_array_bin1, param_array_bin2, xi_all, pt_type=pt_type)
        xi_gm, _ = get_xiXm_terms(param_array_bin1, xi_all, pt_type=pt_type)

        for k in range(len(r_array)):
            xi_gm_temp = intspline(z_pk, xi_gm[:, k])
            xi_gg_temp = intspline(z_pk, xi_gg[:, k])
            xi_mm_temp = intspline(z_pk, xi_mm[:, k])

            if use_mean_z:
                xi_gm_bin_f[k] = xi_gm_temp(zmean_gm)
                xi_gg_bin_f[k] = xi_gg_temp(zmean_g)
                xi_mm_bin_f[k] = xi_mm_temp(zmean_m)
                xi_gg_mm_bin_f[k] = xi_gg_bin_f[k] / xi_mm_bin_f[k]
                xi_gm_mm_bin_f[k] = xi_gm_bin_f[k] / xi_mm_bin_f[k]
            else:
                xi_gm_bin_f[k] = (sp.integrate.simps(xi_gm_temp(nz_z) * nz_g * nz_m, nz_z)) / (
                    sp.integrate.simps(nz_g * nz_m, nz_z))
                xi_gg_bin_f[k] = (sp.integrate.simps(xi_gg_temp(nz_z) * nz_g * nz_g, nz_z)) / (
                    sp.integrate.simps(nz_g * nz_g, nz_z))
                if j == 0:
                    xi_mm_bin_f[k] = (sp.integrate.simps(xi_mm_temp(nz_z) * nz_m * nz_m, nz_z)) // (
                        sp.integrate.simps(nz_m * nz_m, nz_z))

        if do_save_xi:
            Pk_gg, _ = get_PXX_terms_bins(param_array_bin1, param_array_bin2, Pkth_array_khres, pt_type=pt_type)
            Pk_gm, _ = get_PXm_terms(param_array_bin1, Pkth_array_khres, pt_type=pt_type)
            Pk_gm_bin_f, Pk_gg_bin_f, Pk_mm_bin_f = np.zeros(len(k_hres)), np.zeros(len(k_hres)), np.zeros(len(k_hres))
            for i in range(len(k_hres)):
                Pk_gm_temp = intspline(z_pk, Pk_gm[:, i])
                Pk_gg_temp = intspline(z_pk, Pk_gg[:, i])
                Pk_mm_temp = intspline(z_pk, Pk_mm[:, i])

                Pk_gm_bin_f[i] = Pk_gm_temp(zmean_gm)
                Pk_gg_bin_f[i] = Pk_gg_temp(zmean_g)
                Pk_mm_bin_f[i] = Pk_mm_temp(zmean_m)

            block.put_double_array_1d("pk_to_xi", "Pk_gg_bin % s" % (j + 1), Pk_gg_bin_f)
            block.put_double_array_1d("pk_to_xi", "Pk_gm_bin % s" % (j + 1), Pk_gm_bin_f)
            block.put_double_array_1d("pk_to_xi", "Pk_mm_bin % s" % (j + 1), Pk_mm_bin_f)

            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array, "xi_gg_mat_bin % s" % (j + 1), xi_gg)
            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array, "xi_gm_mat_bin % s" % (j + 1), xi_gm)

            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array, "xi_gg_mm_mat_bin % s" % (j + 1), xi_gg / xi_mm)
            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array, "xi_gm_mm_mat_bin % s" % (j + 1), xi_gm / xi_mm)

            if j == 0:
                block.put("pk_to_xi", "Pk_all", Pkth_array_khres)
                block.put("pk_to_xi", "xi_all", xi_all)
                block.put_grid("pk_to_xi", "z", z_pk, "r", r_array, "xi_mm_mat", xi_mm)

        block.put_double_array_1d("pk_to_xi", "xi_gg_bin % s" % (j + 1), xi_gg_bin_f)
        block.put_double_array_1d("pk_to_xi", "xi_gm_bin % s" % (j + 1), xi_gm_bin_f)
        block.put_double_array_1d("pk_to_xi", "xi_mm_bin % s" % (j + 1), xi_mm_bin_f)

        block.put_double_array_1d("pk_to_xi", "xi_gg_mm_bin % s" % (j + 1), xi_gg_mm_bin_f)
        block.put_double_array_1d("pk_to_xi", "xi_gm_mm_bin % s" % (j + 1), xi_gm_mm_bin_f)

        block.put_double("pk_to_xi", "zmean_gg_bin % s" % (j + 1), zmean_g)
        block.put_double("pk_to_xi", "zmean_gm_bin % s" % (j + 1), zmean_gm)

        block.put_double("pk_to_xi", "zmean_gg_mm_bin % s" % (j + 1), zmean_g)
        block.put_double("pk_to_xi", "zmean_gm_mm_bin % s" % (j + 1), zmean_gm)

    
    # import pdb; pdb.set_trace()
    # xi_mm_comb = []
    # for j in range(5):
    #   if len(xi_mm_comb) == 0:
    #     xi_mm_comb = block.get_double_array_1d("pk_to_xi", "xi_mm_bin % s" % (j + 1))
    #   else:
    #     xi_mm_comb = np.hstack((xi_mm_comb,block.get_double_array_1d("pk_to_xi", "xi_mm_bin % s" % (j + 1))))
    # np.savez('mice_xi_mm_all_bin.npz',r=r_array,xi=xi_mm_comb)
    # import pdb; pdb.set_trace()

    return 0
Exemplo n.º 2
0
def get_Pktharray(output_nl_grid, klin, knl, Pkzlin, Pnl_kz, usePNL_for_Pk=True, pt_type=None, Pk_terms_names=None,
                  z_array=None):
    klinlog = np.log(klin)

    Pk0lin = Pkzlin[0, :]

    ind = np.where(klin > 0.03)[0][0]

    Growth = np.sqrt(Pkzlin[:, ind] / Pkzlin[0, ind])

    nk = 4 * len(klin)  # higher res increases runtime, decreases potential ringing at low-k
    # eps = 1e-6
    # eps = 0.
    # kmin = np.log10((1. + eps) * klin[0])
    # kmax = np.log10((1. - eps) * klin[-1])

    kmin = -6.0
    kmax = 3.0

    klin_fpt = np.logspace(kmin, kmax, nk)
    k1log = np.log(klin_fpt)
    #    pinterp=interp1d(klinlog,np.log(Pk0lin),bounds_error=False, fill_value="extrapolate")
    plininterp = interp1d(klinlog, np.log(Pk0lin), fill_value='extrapolate', bounds_error=False)

    ## This interpolation should be at the input bounds. Extrapolate used to avoid failure due to numerical noise. No actual extrapolation is done. We could handle this using a margin or something else
    Plin_klin_fpt = np.exp(plininterp(k1log))

    if (knl[0] < klin_fpt[0]) or (knl[-1] > klin_fpt[-1]):
        EK1 = k_extend(klin_fpt, np.log10(knl[0]), np.log10(knl[-1]))
        klin_fpt = EK1.extrap_k()
        Plin_klin_fpt = EK1.extrap_P_low(Plin_klin_fpt)
        Plin_klin_fpt = EK1.extrap_P_high(Plin_klin_fpt)

    klin_fpt_log = np.log(klin_fpt)
    knl_log = np.log(knl)

    if pt_type in ['lin_oneloop_eul_bk','nohf_oneloop_eul_bk']:
        # if pt_type == 'lin_oneloop_eul_bk':
        usePNL_for_Pk = False

    if output_nl_grid:
        temp = intspline(klin_fpt_log, np.log(Plin_klin_fpt))
        Plin = np.exp(temp(knl_log))
        Pnl1 = Pnl_kz
        if usePNL_for_Pk:
            knl2mat = np.tile(knl ** 2, (Pnl_kz.shape[0], 1))
            k2Pnl1 = np.multiply(knl2mat, Pnl_kz)
        else:
            k2Pnl1 = np.outer(Growth ** 2, (knl ** 2) * Plin)
    else:
        Plin = Plin_klin_fpt
        Pnl1 = Pnl_kz
        if usePNL_for_Pk:
            k2Pnl1 = np.outer(Growth ** 2, (klin_fpt ** 2) * Pnl[0, :])
        else:
            k2Pnl1 = np.outer(Growth ** 2, (klin_fpt ** 2) * Plin)

    Plin_kz = np.outer(Growth ** 2, Plin)

    n_pad = len(klin_fpt)

    if pt_type in ['oneloop_eul_bk','lin_oneloop_eul_bk','nohf_oneloop_eul_bk']:

        fastpt = FASTPT.FASTPT(klin_fpt, to_do=['one_loop_dd'], low_extrap=-5, high_extrap=3, n_pad=n_pad)
        PXXNL_b1b2bsb3nl = fastpt.one_loop_dd_bias_b3nl(Plin_klin_fpt, C_window=.75)
        if output_nl_grid:
            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[0]))
            P1loop = np.outer(Growth ** 4, (temp(knl_log)))
            Pmm_linp1loop = Plin_kz + P1loop
            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[2]))
            Pd1d2 = np.outer(Growth ** 4, (temp(knl_log)))
            temp = intspline(klin_fpt_log, np.log(PXXNL_b1b2bsb3nl[3]))
            Pd2d2 = np.outer(Growth ** 4, np.exp(temp(knl_log)))
            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[4]))
            Pd1s2 = np.outer(Growth ** 4, (temp(knl_log)))
            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[5]))
            Pd2s2 = np.outer(Growth ** 4, (temp(knl_log)))
            temp = intspline(klin_fpt_log, np.log(PXXNL_b1b2bsb3nl[6]))
            Ps2s2 = np.outer(Growth ** 4, np.exp(temp(knl_log)))
            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[7]))
            sig3nl = np.outer(Growth ** 4, (temp(knl_log)))
            sig4 = np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[8] * np.ones_like(knl))
        else:
            [P1loop,Pd1d2, Pd2d2, Pd1s2, Pd2s2, Ps2s2, sig3nl, sig4] = [
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[0]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[2]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[3]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[4]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[5]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[6]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[7]),
                np.outer(Growth ** 4, PXXNL_b1b2bsb3nl[8] * np.ones_like(
                    PXXNL_b1b2bsb3nl[0]))]
            Pmm_linp1loop = Plin_kz + P1loop

        Pk_th_array = [Plin_kz, Pnl1, Pd1d2, Pd2d2, Pd1s2, Pd2s2, Ps2s2, sig3nl, k2Pnl1, Pmm_linp1loop, sig4]


    elif pt_type in ['oneloop_cleft_bk']:
        cl = cpool.CLEFT(k=klin_fpt, p=Plin_klin_fpt)
        Pk_th_array = np.zeros((len(Pk_terms_names), len(Growth), len(knl)))

        kinput_ind = np.where((knl < 4.) & (knl > 1e-3))[0]
        kinput = knl[kinput_ind]

        pk_table_f = cpool.get_nonlinear_kernels_wgroth(cl, kinput, koutput=knl, Growth=Growth, do_analysis=False,
                                                        Pnl_toplot=Pnl1, z_array=z_array)

        Pk_th_array[1:-1, :, :] = pk_table_f
        Pk_th_array[0, :, :] = Plin_kz
        Pk_th_array[1, :, :] = Pnl1
        Pk_th_array[-1, :, :] = k2Pnl1
    else:
        Pk_th_array = None
        print('give correct pt_type')

    if output_nl_grid:
        kout = knl
    else:
        kout = klin_fpt

    return Pk_th_array, kout
Exemplo n.º 3
0
def setup(options):
    nz_dir = options.get_string(option_section, "nz_dir")
    rmin = options.get_double(option_section, "rmin", 0.1)
    rmax = options.get_double(option_section, "rmax", 50)
    nrbin = options.get_double(option_section, "nrbin", 90)

    bins_all = ast.literal_eval(options.get_string(option_section, "bins_all", "[1, 2, 3, 4, 5]"))
    nbins = len(bins_all)

    k_hres_min = options.get_double(option_section, "k_hres_min", 0.0001)
    k_hres_max = options.get_double(option_section, "k_hres_max", 100)
    n_k_hres_bin = options.get_double(option_section, "n_k_hres_max", 50000)

    output_nl_grid = options.get_bool(option_section, "output_nl_grid", True)
    pklin_file = options.get_string(option_section, "pklin_file")
    pknl_file = options.get_string(option_section, "pknl_file")
    constraint_b2 = options.get_bool(option_section, "constraint_b2", False)

    do_regularize_pk = options.get_bool(option_section, "do_regularize", False)
    do_reg_all = options.get_bool(option_section, "do_reg_all", False)
    reg_k = options.get_double(option_section, "reg_k", 0.3)
    reg_c = options.get_double(option_section, "reg_c", 1000.)
    usePNL_for_Pk = options.get_bool(option_section, "usePNL_for_Pk", True)

    do_save_xi = options.get_bool(option_section, "do_save_xi", False)
    save_xi_def = options.get_string(option_section, "save_xi_def", '')
    use_mean_z = options.get_bool(option_section, "use_mean_z", True)
    pt_type = options.get_string(option_section, "pt_type_g", '')

    r_array = np.logspace(np.log10(rmin), np.log10(rmax), nrbin)
    k_hres = np.logspace(np.log10(k_hres_min), np.log10(k_hres_max), n_k_hres_bin)

    Pkz_lin_f = np.load(pklin_file)
    Pkz_nl_f = np.load(pknl_file)
    klin, zlin, Pkzlin = Pkz_lin_f['k'], Pkz_lin_f['z'], Pkz_lin_f['pkz']
    knl, znl, Pnl_kz = Pkz_nl_f['k'], Pkz_nl_f['z'], Pkz_nl_f['pkz']

    if pt_type in ['oneloop_eul_bk','lin_oneloop_eul_bk','nohf_oneloop_eul_bk']:
        Pk_terms_names = ['Plin', 'Pmm', 'Pd1d2', 'Pd2d2', 'Pd1s2', 'Pd2s2', 'Ps2s2', 'Pd1d3nl', 'k2Pk', 'Pmm_linp1loop', 'sig4']
        xi_gg = np.zeros((len(Pk_terms_names) - 1, len(znl), len(r_array)))
    elif pt_type in ['oneloop_cleft_bk']:
        Pk_terms_names = ['Plin', 'Pnl1', 'Pzel', 'PA', 'PW', 'Pd1', 'Pd1d1', 'Pd2', 'Pd2d2', 'Pd1d2', 'Ps2', 'Pd1s2',
                          'Pd2s2', 'Ps2s2', 'PD2', 'Pd1D2', 'k2Pk']
        xi_gg = np.zeros((len(Pk_terms_names), len(znl), len(r_array)))
    else:
        print 'No predefined pt_type given'
        Pk_terms_names = None

    Pkth_array, karray = get_Pktharray(output_nl_grid, klin, knl, Pkzlin, Pnl_kz, pt_type=pt_type,
                                       Pk_terms_names=Pk_terms_names, z_array=znl,usePNL_for_Pk=usePNL_for_Pk)

    Pkth_array_khres = np.zeros((len(Pk_terms_names), len(znl), len(k_hres)))

    for j1 in range(len(Pk_terms_names)):

        print 'processing Pk ' + str(Pk_terms_names[j1])

        P_gg_khres = np.zeros((len(znl), len(k_hres)))
        for i in range(len(znl)):
            P_gg_j1_i = Pkth_array[j1][i, :]

            Pgg_temp = intspline(karray, P_gg_j1_i)
            Pgg_term_interp = Pgg_temp(k_hres)
            P_gg_khres[i, :] = Pgg_term_interp

        if Pk_terms_names[j1] == 'Plin':
            P_lin_khres = P_gg_khres
            P_gg_khres_reg = P_gg_khres
        else:
            if do_regularize_pk:
                if do_reg_all:
                    # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                    P_gg_khres_reg = reg_Pk_mat_expmult(P_gg_khres, k_hres, reg_c)
                else:
                    if Pk_terms_names[j1] == 'k2Pk':
                        # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                        # P_gg_khres_reg = reg_Pk_gaussian(P_gg_khres, k_hres, reg_k, c_val=reg_c)
                        P_gg_khres_reg = reg_Pk_mat_expmult(P_gg_khres, k_hres, reg_c)
                    else:
                        P_gg_khres_reg = P_gg_khres
            else:
                P_gg_khres_reg = P_gg_khres

        if Pk_terms_names[j1] != 'sig4':
            for k in range(len(r_array)):
                xi_gg_f = Pk2corr_mat(r_array[k], k_hres, P_gg_khres_reg)
                xi_gg[j1, :, k] = xi_gg_f

            # import matplotlib
            # matplotlib.use('Agg')
            # import matplotlib.pyplot as plt
            # fig, ax_all = plt.subplots(1, 1, figsize=(8, 6))
            # ax = ax_all
            # ax.plot(r_array, xi_gg[j1,0,:], color='b', lw=2, marker='', linestyle='-')
            # ax.set_ylabel(r'xi', size=22)
            # ax.set_xlabel(r'r', size=22)
            # ax.set_xscale('log')
            # ax.set_yscale('log')
            # ax.tick_params(axis='both', which='major', labelsize=15)
            # ax.tick_params(axis='both', which='minor', labelsize=15)
            # ax.legend(fontsize=20)
            # plt.tight_layout()
            # fig.savefig('/global/project/projectdirs/des/shivamp/cosmosis/y3kp-bias-model/3d_stats/plots/test_bk/xi_' + Pk_terms_names[j1] + '.pdf',dpi=240)


        Pkth_array_khres[j1, :, :] = P_gg_khres_reg

    config = {'nz_dir': nz_dir, 'r_array': r_array, 'k_hres': k_hres, 'xi_all': xi_gg, 'bias_section': str(
        options.get_string(option_section, "bias_section")), 'z_pk': znl, 'constraint_b2': constraint_b2,
              'do_save_xi': do_save_xi, 'Pkth_array_khres': Pkth_array_khres, 'save_xi_def': save_xi_def,
              'Pk_terms_names': Pk_terms_names, 'use_mean_z': use_mean_z, 'pt_type': pt_type, 'nbins': nbins}

    return config
Exemplo n.º 4
0
def get_Pk_basis_funcs(block, pt_type, 
    matter_power_lin_name=names.matter_power_lin,     
    matter_power_nl_name=names.matter_power_nl, 
    output_nl_grid=True, use_pnl_for_k2=True,
    k_growth=1.e-3, fpt_upsample=4):
    """
    Get the z=0,k-dependent basis functions required 
    to construct the galaxy-galaxy
    and galaxy-matter power spectra for a given pt_type


    Parameters
    ----------
    block: DataBlock instance
        block from which to read data
    pt_type: str
        string indicating the type of perturbation theory
        model to use - one of "linear", "oneloop_lag_bk", 
        "oneloop_eul_bk".
    matter_power_lin_name : str (default = names.matter_power_lin)
        Name of the section for the linear matter P(k)
    matter_power_nl_name : str (default = names.matter_power_nl)
        Name of the section for the non-linear matter P(k)    
    output_on_nl_grid: bool (default=True)
        Output the terms at the same k values as the non-linear
        P(k). If False, interpolate P_nl and P_lin onto the
        fast-pt k grid. It shouldn't matter which we
        do here.
    use_pnl_for_k2 : bool (default=True)
        Use the non-linear matter power for the k^2 term
    k_growth : float (default=1.e-3)
        k value at which to calcaulate growth factor
    fpt_upsample : upsample the linear P(k) by this factor
        when passing it to fast-pt.

    Returns
    -------
    k_out: float array
        array of k values at which P(k) terms are sampled
    PXXNL_out: dict
        Dictionary of pt basis functions
    """

    #Read in linear and non-linear P(k)s from block
    zlin, klin, Plin = block.get_grid(matter_power_lin_name,
        "z", "k_h", "p_k")
    znl, knl, Pnl = block.get_grid(matter_power_nl_name,
        "z", "k_h", "p_k")    

    assert np.allclose(zlin, znl) #shit is already convoluted enough
    #without different z values for the linear and non-linear P(k)s

    log_klin = np.log(klin)
    Plin_z0 = Plin[0]

    #Calculate the growth factor from linear P(k) at k closest to k_growth
    k_growth_ind = np.argmin(np.abs(klin-k_growth))
    growth = np.sqrt(Plin[:, k_growth_ind] / Plin[0, k_growth_ind])

    # NM: This whole next section is quite confusing. 

    nk = fpt_upsample * len(klin) # higher res increases runtime, decreases potential ringing at low-k
    # eps = 1e-6
    eps = 0.
    kmin = np.log10((1. + eps) * klin[0])
    kmax = np.log10((1. - eps) * klin[-1])
    klin_fpt = np.logspace(kmin, kmax, nk)
    log_klin_fpt = np.log(klin_fpt)
    plin_interp = interp1d(log_klin, np.log(Plin_z0))

    # This interpolation should be at the input bounds. 
    # Extrapolate used to avoid failure due to numerical noise. 
    # No actual extrapolation is done. 
    # We could handle this using a margin or something else
    Plin_fpt = np.exp(plin_interp(log_klin_fpt))

    if (knl[0] < klin_fpt[0]) or (knl[-1] > klin_fpt[-1]):
        EK1 = k_extend(klin_fpt, np.log10(knl[0]), np.log10(knl[-1]))
        klin_fpt = EK1.extrap_k()
        Plin_fpt = EK1.extrap_P_low(Plin_fpt)
        Plin_fpt = EK1.extrap_P_high(Plin_fpt)

    log_klin_fpt = np.log(klin_fpt)
    log_knl = np.log(knl)

    n_pad = len(klin_fpt)
    fastpt = FASTPT.FASTPT(klin_fpt, to_do=['one_loop_dd'], 
        low_extrap=-5, high_extrap=3, n_pad=n_pad)

    if pt_type in ['oneloop_lag_bk']:
        PXXNL_lpt = fastpt.one_loop_dd_bias_lpt_NL(Plin_klin_fpt, C_window=.75)
        PXXNL_lpt_z0 = {"Pb1L" : PXXNL_lpt[1], "Pb1L2" : PXXNL_lpt[2],
            "Pb1Lb2L" : PXXNL_lpt[3], "Pb2L" : PXXNL_lpt[4],
            "Pb2L2" : PXXNL_lpt[5], "sig4" : PXXNL_lpt[6]*np.ones_like(klin_fpt)}

        if output_nl_grid:
            # interpolate to nl k grid.
            for key, pk in PXXNL_lpt_z0:
                if key == "sig4":
                    PXXNL_lpt_z0[key] = PXXNL_lpt[6]*np.ones_like(knl)
                PXXNL_lpt_z0[key] = intspline(log_klin_fpt, pk)(log_knl)

        #Apply growth factor to make k,z arrays
        PXXNL_out = {}
        for key, pk in PXXNL_lpt_z0:
            PXXNL_out[key] = np.outer(growth**4, pk)

    elif pt_type in ['oneloop_eul_bk']:
        PXXNL_b1b2bsb3nl = fastpt.one_loop_dd_bias_b3nl(Plin_fpt, C_window=.75)
        PXXNL_b1b2bsb3nl_z0 = {"Pd1d2" : PXXNL_b1b2bsb3nl[2],
            "Pd2d2" : PXXNL_b1b2bsb3nl[3], "Pd1s2" : PXXNL_b1b2bsb3nl[4],
            "Pd2s2" : PXXNL_b1b2bsb3nl[5], "Ps2s2" : PXXNL_b1b2bsb3nl[6],
            "sig3nl" : PXXNL_b1b2bsb3nl[7], "sig4" : PXXNL_b1b2bsb3nl[8]}

        if output_nl_grid:   
            # interpolate to nl k grid.
            for key, pk in PXXNL_b1b2bsb3nl_z0.items():
                if key == "sig4":
                    PXXNL_b1b2bsb3nl_z0[key] = pk*np.ones_like(knl)
                else:
                    PXXNL_b1b2bsb3nl_z0[key] = intspline(log_klin_fpt, pk)(log_knl)

        #Apply growth factor to make k,z arrays
        PXXNL_out = {}
        for key, pk in PXXNL_b1b2bsb3nl_z0.items():
            PXXNL_out[key] = np.outer(growth**4, pk)

    else:
        raise ValueError("pt_type %s not valid"%pt_type)

    #We also need to add P_nl, P_lin and k^2P terms to this dictionary
    #if output_nl_grid=True, resample P_lin onto k_nl. Otherwise, 
    #resample P_nl onto the fast-pt grid.
    if output_nl_grid:

        PXXNL_out["Pnl"] = Pnl

        #start of by assuming we don't need to resample,
        #then check whether we do....
        resample_P_lin = False
        #we certainly need to resample P_lin if klin and knl
        #have different lengths 
        if len(klin) != len(knl):
            resample_P_lin = True
        #if they have the same length, still need to resample
        #if the k values are different
        elif not np.allclose(klin, knl):
            resample_P_lin = True
        plin_z0_interp = intspline(log_klin_fpt, np.log(Plin_fpt))
        Plin_z0_on_nl_grid = np.exp(plin_z0_interp(log_knl))
        PXXNL_out["Plin_from_growth"] = np.outer(growth**2, Plin_z0_on_nl_grid)
        
        #Now add k^2P(k) term
        knl2_matrix = np.tile(knl ** 2, (Pnl.shape[0], 1))
        if use_pnl_for_k2:
            PXXNL_out["k2P"] = np.multiply(knl2_matrix, Pnl)
        else:
            PXXNL_out["k2P"] = np.multiply(knl2_matrix, Plin_on_nl_grid)

        #Set k_out to knl
        k_out = knl

    else:

        #In this case work out if we need to resample P_nl
        resample_P_nl = False
        if len(knl) != len(klin_fpt):
            resample_P_nl = True
        elif not np.allclose(knl, klin_fpt):
            resample_P_lin = True
        if resample_P_nl:
            pnl_interp = RectBivariateSpline(znl, log_knl, np.log(Pnl))
            pnl_on_fpt_grid = np.exp(pnl_interp(znl, log_knl, grid=True))
        else:
            pnl_on_fpt_grid = Pnl

        PXXNL_out["Pnl"] = pnl_on_fpt_grid
        Plin_out = np.outer(growth**2, Plin_klin_fpt)
        PXXNL_out["Plin_from_growth"] = Plin_out

        #Now add k^2P(k) term
        knl2_matrix = np.tile(klin_fpt ** 2, (Pnl.shape[0], 1))
        if use_pnl_for_k2:
            PXXNL_out["k2P"] = np.multiply(knl2_matrix, pnl_on_fpt_grid)
        else:
            PXXNL_out["k2P"] = np.multiply(klin_fpt, Plin_out)

        #Set k_out to klin_fpt
        k_out = klin_fpt

    return k_out, PXXNL_out
Exemplo n.º 5
0
def get_theory_terms(block, r_data, stat_type, bins_array):
    xi_theory_rdata = []
    r_array = block.get_double_array_1d("pk_to_xi", "r"),
    if stat_type == 'gg':
        nbins = len(bins_array)
        for j in range(nbins):
            bin_j = bins_array[j]
            xi_gg = block.get_double_array_1d("pk_to_xi",
                                              "xi_gg_bin % s" % bin_j)
            xi_gg_temp = intspline(r_array, xi_gg)
            xi_gg_f = xi_gg_temp(r_data[j])

            if len(xi_theory_rdata) == 0:
                xi_theory_rdata = xi_gg_f
            else:
                xi_theory_rdata = np.hstack((xi_theory_rdata, xi_gg_f))

    elif stat_type == 'gm':
        nbins = len(bins_array)
        for j in range(nbins):
            bin_j = bins_array[j]
            xi_gm = block.get_double_array_1d("pk_to_xi",
                                              "xi_gm_bin % s" % bin_j)
            xi_gm_temp = intspline(r_array, xi_gm)
            xi_gm_f = xi_gm_temp(r_data[j])
            if len(xi_theory_rdata) == 0:
                xi_theory_rdata = xi_gm_f
            else:
                xi_theory_rdata = np.hstack((xi_theory_rdata, xi_gm_f))

    elif stat_type == 'gg_gm':
        nbins = len(bins_array)
        for j in range(nbins):
            bin_j = bins_array[j]
            xi_gg = block.get_double_array_1d("pk_to_xi",
                                              "xi_gg_bin % s" % bin_j)
            xi_gg_temp = intspline(r_array, xi_gg)
            xi_gg_f = xi_gg_temp(r_data[j])
            if len(xi_theory_rdata) == 0:
                xi_theory_rdata = xi_gg_f
            else:
                xi_theory_rdata = np.hstack((xi_theory_rdata, xi_gg_f))

        for j in range(nbins):
            bin_j = bins_array[j]
            xi_gm = block.get_double_array_1d("pk_to_xi",
                                              "xi_gm_bin % s" % bin_j)
            xi_gm_temp = intspline(r_array, xi_gm)
            xi_gm_f = xi_gm_temp(r_data[j + nbins])
            xi_theory_rdata = np.hstack((xi_theory_rdata, xi_gm_f))

    elif stat_type == 'gg_mm__gm_mm':
        nbins = len(bins_array)
        for j in range(nbins):
            bin_j = bins_array[j]
            xi_gg_mm = block.get_double_array_1d("pk_to_xi",
                                                 "xi_gg_mm_bin % s" % bin_j)
            xi_gg_mm_temp = intspline(r_array, xi_gg_mm)
            xi_gg_mm_f = xi_gg_mm_temp(r_data[j])
            if len(xi_theory_rdata) == 0:
                xi_theory_rdata = xi_gg_mm_f
            else:
                xi_theory_rdata = np.hstack((xi_theory_rdata, xi_gg_mm_f))

        for j in range(nbins):
            bin_j = bins_array[j]
            xi_gm_mm = block.get_double_array_1d("pk_to_xi",
                                                 "xi_gm_mm_bin % s" % bin_j)
            xi_gm_mm_temp = intspline(r_array, xi_gm_mm)
            xi_gm_mm_f = xi_gm_mm_temp(r_data[j + nbins])
            xi_theory_rdata = np.hstack((xi_theory_rdata, xi_gm_mm_f))

    return xi_theory_rdata
Exemplo n.º 6
0
def setup(options):
    nz_dir = options.get_string(option_section, "nz_dir")
    rmin = options.get_double(option_section, "rmin", 0.1)
    rmax = options.get_double(option_section, "rmax", 50)
    nrbin = options.get_double(option_section, "nrbin", 90)

    bins_all = ast.literal_eval(
        options.get_string(option_section, "bins_all", "[1, 2, 3, 4, 5]"))
    nbins = len(bins_all)

    k_hres_min = options.get_double(option_section, "k_hres_min", 0.0001)
    k_hres_max = options.get_double(option_section, "k_hres_max", 100)
    n_k_hres_bin = options.get_double(option_section, "n_k_hres_max", 50000)

    output_nl_grid = options.get_bool(option_section, "output_nl_grid", True)
    pklin_file = options.get_string(option_section, "pklin_file")
    pknl_file = options.get_string(option_section, "pknl_file")
    constraint_b2 = options.get_bool(option_section, "constraint_b2", False)

    do_regularize_pk = options.get_bool(option_section, "do_regularize", False)
    do_reg_all = options.get_bool(option_section, "do_reg_all", False)
    reg_k = options.get_double(option_section, "reg_k", 0.3)
    reg_c = options.get_double(option_section, "reg_c", 1000.)

    do_save_xi = options.get_bool(option_section, "do_save_xi", False)
    save_xi_def = options.get_string(option_section, "save_xi_def", '')
    use_mean_z = options.get_bool(option_section, "use_mean_z", True)
    pt_type = options.get_string(option_section, "pt_type_g", '')

    r_array = np.logspace(np.log10(rmin), np.log10(rmax), nrbin)
    k_hres = np.logspace(np.log10(k_hres_min), np.log10(k_hres_max),
                         n_k_hres_bin)

    Pkz_lin_f = np.load(pklin_file)
    Pkz_nl_f = np.load(pknl_file)
    klin, zlin, Pkzlin = Pkz_lin_f['k'], Pkz_lin_f['z'], Pkz_lin_f['pkz']
    knl, znl, Pnl_kz = Pkz_nl_f['k'], Pkz_nl_f['z'], Pkz_nl_f['pkz']

    if pt_type in ['oneloop_eul_bk']:
        Pk_terms_names = [
            'Plin', 'Pmm', 'Pd1d2', 'Pd2d2', 'Pd1s2', 'Pd2s2', 'Ps2s2',
            'Pd1d3nl', 'k2Pk', 'sig4'
        ]
        xi_gg = np.zeros((len(Pk_terms_names) - 1, len(znl), len(r_array)))
    elif pt_type in ['oneloop_cleft_bk']:
        Pk_terms_names = [
            'Plin', 'Pnl1', 'Pzel', 'PA', 'PW', 'Pd1', 'Pd1d1', 'Pd2', 'Pd2d2',
            'Pd1d2', 'Ps2', 'Pd1s2', 'Pd2s2', 'Ps2s2', 'PD2', 'Pd1D2', 'k2Pk'
        ]
        xi_gg = np.zeros((len(Pk_terms_names), len(znl), len(r_array)))
    else:
        print 'No predefined pt_type given'
        Pk_terms_names = None

    # pdb.set_trace()

    Pkth_array, karray = get_Pktharray(output_nl_grid,
                                       klin,
                                       knl,
                                       Pkzlin,
                                       Pnl_kz,
                                       pt_type=pt_type,
                                       Pk_terms_names=Pk_terms_names,
                                       z_array=znl)

    Pkth_array_khres = np.zeros((len(Pk_terms_names), len(znl), len(k_hres)))

    for j1 in range(len(Pk_terms_names)):

        print 'processing Pk ' + str(Pk_terms_names[j1])

        P_gg_khres = np.zeros((len(znl), len(k_hres)))
        for i in range(len(znl)):
            P_gg_j1_i = Pkth_array[j1][i, :]

            Pgg_temp = intspline(karray, P_gg_j1_i)
            Pgg_term_interp = Pgg_temp(k_hres)
            P_gg_khres[i, :] = Pgg_term_interp

        if Pk_terms_names[j1] == 'Plin':
            P_gg_khres_reg = P_gg_khres
        else:
            if do_regularize_pk:
                if do_reg_all:
                    # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                    P_gg_khres_reg = reg_Pk_mat_expmult(
                        P_gg_khres, k_hres, reg_c)
                else:
                    if Pk_terms_names[j1] == 'k2Pk':
                        # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                        # P_gg_khres_reg = reg_Pk_gaussian(P_gg_khres, k_hres, reg_k, c_val=reg_c)
                        P_gg_khres_reg = reg_Pk_mat_expmult(
                            P_gg_khres, k_hres, reg_c)
                    else:
                        P_gg_khres_reg = P_gg_khres
            else:
                P_gg_khres_reg = P_gg_khres

        if Pk_terms_names[j1] != 'sig4':
            for k in range(len(r_array)):
                xi_gg_f = Pk2corr_mat(r_array[k], k_hres, P_gg_khres_reg)
                xi_gg[j1, :, k] = xi_gg_f

        Pkth_array_khres[j1, :, :] = P_gg_khres_reg

    config = {
        'nz_dir': nz_dir,
        'r_array': r_array,
        'k_hres': k_hres,
        'xi_all': xi_gg,
        'bias_section': str(options.get_string(option_section,
                                               "bias_section")),
        'z_pk': znl,
        'constraint_b2': constraint_b2,
        'do_save_xi': do_save_xi,
        'Pkth_array_khres': Pkth_array_khres,
        'save_xi_def': save_xi_def,
        'Pk_terms_names': Pk_terms_names,
        'use_mean_z': use_mean_z,
        'pt_type': pt_type,
        'nbins': nbins
    }

    return config
Pkth_array, karray, xi_all, r_array = ptx.get_Pktharray(output_nl_grid, klin, knl, Pkzlin, Pnl_kz,pt_type=pt_type,Pk_terms_names = Pk_terms_names, z_array=znl, output_xi=True, use_fftlog=False)

Pkth_array_khres = np.zeros((len(Pk_terms_names), len(znl), len(k_hres)))

do_regularize_pk = True
do_reg_all = False
for j1 in range(len(Pk_terms_names)):

    print 'processing Pk ' + str(Pk_terms_names[j1])

    P_gg_khres = np.zeros((len(znl), len(k_hres)))
    for i in range(len(znl)):
        P_gg_j1_i = Pkth_array[j1][i, :]

        Pgg_temp = intspline(karray, P_gg_j1_i)
        Pgg_term_interp = Pgg_temp(k_hres)
        P_gg_khres[i, :] = Pgg_term_interp

    if Pk_terms_names[j1] == 'Plin':
        P_gg_khres_reg = P_gg_khres
    else:
        if do_regularize_pk:
            if do_reg_all:
                # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                P_gg_khres_reg = ptx.reg_Pk_mat_expmult(P_gg_khres, k_hres, reg_c)
            else:
                if Pk_terms_names[j1] == 'k2Pk':
                    # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                    # P_gg_khres_reg = reg_Pk_gaussian(P_gg_khres, k_hres, reg_k, c_val=reg_c)
                    P_gg_khres_reg = ptx.reg_Pk_mat_expmult(P_gg_khres, k_hres, reg_c)
Exemplo n.º 8
0
        '_MICE_cosmogg_mm__gm_mm_nocov_crosszbinsgmgg_False_crosszbinsall_False_gmgg_False_covdiag_False_njk_180.npz'
    )
    xi_gm_file = np.load(
        '/global/u1/s/spandey/cosmosis_exp/cosmosis/y3kp-bias-model/3d_stats/bestfits/measurements/xi_gm_total_bin_'
        + str(binval) +
        '_MICE_cosmogg_mm__gm_mm_nocov_crosszbinsgmgg_False_crosszbinsall_False_gmgg_False_covdiag_False_njk_180.npz'
    )
    xi_mm_file = np.load(
        '/global/u1/s/spandey/cosmosis_exp/cosmosis/y3kp-bias-model/3d_stats/bestfits/measurements/xi_mm_total_bin_'
        + str(binval) +
        '_MICE_cosmogg_mm__gm_mm_nocov_crosszbinsgmgg_False_crosszbinsall_False_gmgg_False_covdiag_False_njk_180.npz'
    )
    r_array_theory = xi_gg_file['r']
    xi_gg_th, xi_gm_th, xi_mm_th = xi_gg_file['xi'], xi_gm_file[
        'xi'], xi_mm_file['xi']
    xi_gg_temp = intspline(r_array_theory, xi_gg_th)
    xi_gg = xi_gg_temp(r_gg_data)
    xi_gg_mat_diag = np.diag(xi_gg)

    xi_mm_temp = intspline(r_array_theory, xi_mm_th)
    xi_mm = xi_mm_temp(r_gg_data)
    xi_mm_mat_diag = np.diag(xi_mm)

    if stat_type == 'gg_mm':
        inv_xi_mm1 = np.tile((1. / xi_mm).reshape(nr, 1), (1, nr))
        inv_xi_mm2 = np.tile((1. / xi_mm).reshape(1, nr), (nr, 1))
        factor_mult = inv_xi_mm1 * inv_xi_mm2

    if stat_type == 'gg':
        factor_mult = 1.
Exemplo n.º 9
0
def execute(block, config):
    nz_dir = config['nz_dir']
    r_array = config['r_array']
    do_save_xi = config['do_save_xi']
    use_mean_z = config['use_mean_z']
    k_hres = config['k_hres']

    bias_section = config['bias_section']
    constraint_b2 = config['constraint_b2']
    pt_type = config['pt_type']

    output_nl_grid = config['output_nl_grid']
    do_regularize_pk = config['do_regularize_pk']
    do_reg_all = config['do_reg_all']
    reg_k = config['reg_k']
    reg_c = config['reg_c']

    block.put_double_array_1d("pk_to_xi", "r", r_array)
    block.put_double_array_1d("pk_to_xi", "k", k_hres)

    name_lin = names.matter_power_lin
    name_nl = names.matter_power_nl

    klin, zlin, Pkzlin = block[name_lin, "k_h"], block[name_lin,
                                                       "z"], block[name_lin,
                                                                   "P_k"]
    knl, znl, Pnl_kz = block[name_nl, "k_h"], block[name_nl,
                                                    "z"], block[name_nl, "P_k"]
    z_pk = znl

    if pt_type in ['oneloop_eul_bk']:
        Pk_terms_names = [
            'Plin', 'Pmm', 'Pd1d2', 'Pd2d2', 'Pd1s2', 'Pd2s2', 'Ps2s2',
            'Pd1d3nl', 'k2Pk', 'sig4'
        ]
        xi_gg = np.zeros((len(Pk_terms_names) - 1, len(znl), len(r_array)))
    elif pt_type in ['oneloop_cleft_bk']:
        Pk_terms_names = [
            'Plin', 'Pnl1', 'Pzel', 'PA', 'PW', 'Pd1', 'Pd1d1', 'Pd2', 'Pd2d2',
            'Pd1d2', 'Ps2', 'Pd1s2', 'Pd2s2', 'Ps2s2', 'PD2', 'Pd1D2', 'k2Pk'
        ]
        xi_gg = np.zeros((len(Pk_terms_names), len(znl), len(r_array)))
    else:
        print 'No predefined pt_type given'
        Pk_terms_names = None

    Pkth_array, karray = get_Pktharray(output_nl_grid,
                                       klin,
                                       knl,
                                       Pkzlin,
                                       Pnl_kz,
                                       pt_type=pt_type,
                                       Pk_terms_names=Pk_terms_names)

    Pkth_array_khres = np.zeros((len(Pk_terms_names), len(znl), len(k_hres)))

    for j1 in range(len(Pk_terms_names)):

        print 'processing Pk ' + str(Pk_terms_names[j1])

        P_gg_khres = np.zeros((len(znl), len(k_hres)))
        for i in range(len(znl)):
            P_gg_j1_i = Pkth_array[j1][i, :]

            Pgg_temp = intspline(karray, P_gg_j1_i)
            Pgg_term_interp = Pgg_temp(k_hres)
            P_gg_khres[i, :] = Pgg_term_interp

        if Pk_terms_names[j1] == 'Plin':
            P_lin_khres = P_gg_khres
            P_gg_khres_reg = P_gg_khres
        else:
            if do_regularize_pk:
                if do_reg_all:
                    P_gg_khres_reg = reg_Pk(P_gg_khres,
                                            P_lin_khres,
                                            k_hres,
                                            reg_k,
                                            c_val=reg_c)
                else:
                    if Pk_terms_names[j1] == 'k2Pk':
                        # P_gg_khres_reg = reg_Pk(P_gg_khres, P_lin_khres, k_hres, reg_k, c_val=reg_c)
                        P_gg_khres_reg = reg_Pk_gaussian(P_gg_khres,
                                                         k_hres,
                                                         reg_k,
                                                         c_val=reg_c)
                    else:
                        P_gg_khres_reg = P_gg_khres
            else:
                P_gg_khres_reg = P_gg_khres

        if Pk_terms_names[j1] != 'sig4':
            for k in range(len(r_array)):
                xi_gg_f = Pk2corr_mat(r_array[k], k_hres, P_gg_khres_reg)
                xi_gg[j1, :, k] = xi_gg_f

        Pkth_array_khres[j1, :, :] = P_gg_khres_reg

    xi_all = xi_gg
    xi_mm = xi_all[1]
    Pk_mm = Pkth_array_khres[1]

    for j in range(5):

        xi_gm_bin_f, xi_gg_bin_f, xi_mm_bin_f = np.zeros(
            len(r_array)), np.zeros(len(r_array)), np.zeros(len(r_array))
        xi_gm_mm_bin_f, xi_gg_mm_bin_f = np.zeros(len(r_array)), np.zeros(
            len(r_array))

        filename = nz_dir + 'nz_g_m_' + '_zbin_' + str(j + 1) + '_dsg_' + str(
            1) + '_dsm_' + str(1) + '.pk'
        nz_data = pk.load(open(filename, 'rb'))

        nz_g, nz_m, nz_z = nz_data['nz_g'], nz_data['nz_m'], nz_data['nz_z']

        zmean_g = get_zmean(nz_z, nz_g)
        zmean_m = get_zmean(nz_z, nz_m)
        zmean_gm = (zmean_g + zmean_m) / 2.

        param_array_bin1 = get_bias_param_bin_i(block,
                                                j,
                                                bias_section,
                                                constraint_b2,
                                                pt_type=pt_type)
        param_array_bin2 = get_bias_param_bin_i(block,
                                                j,
                                                bias_section,
                                                constraint_b2,
                                                pt_type=pt_type)

        xi_gg, _ = get_xiXX_terms_bins(param_array_bin1,
                                       param_array_bin2,
                                       xi_all,
                                       pt_type=pt_type)
        xi_gm, _ = get_xiXm_terms(param_array_bin1, xi_all, pt_type=pt_type)

        for k in range(len(r_array)):
            xi_gm_temp = intspline(z_pk, xi_gm[:, k])
            xi_gg_temp = intspline(z_pk, xi_gg[:, k])
            xi_mm_temp = intspline(z_pk, xi_mm[:, k])

            if use_mean_z:
                xi_gm_bin_f[k] = xi_gm_temp(zmean_gm)
                xi_gg_bin_f[k] = xi_gg_temp(zmean_g)
                xi_mm_bin_f[k] = xi_mm_temp(zmean_m)
                xi_gg_mm_bin_f[k] = xi_gg_bin_f[k] / xi_mm_bin_f[k]
                xi_gm_mm_bin_f[k] = xi_gm_bin_f[k] / xi_mm_bin_f[k]
            else:
                xi_gm_bin_f[k] = (sp.integrate.simps(
                    xi_gm_temp(nz_z) * nz_g * nz_m,
                    nz_z)) / (sp.integrate.simps(nz_g * nz_m, nz_z))
                xi_gg_bin_f[k] = (sp.integrate.simps(
                    xi_gg_temp(nz_z) * nz_g * nz_g,
                    nz_z)) / (sp.integrate.simps(nz_g * nz_g, nz_z))
                if j == 0:
                    xi_mm_bin_f[k] = (sp.integrate.simps(
                        xi_mm_temp(nz_z) * nz_m * nz_m,
                        nz_z)) // (sp.integrate.simps(nz_m * nz_m, nz_z))

        if do_save_xi:
            Pk_gg, _ = get_PXX_terms_bins(param_array_bin1,
                                          param_array_bin2,
                                          Pkth_array_khres,
                                          pt_type=pt_type)
            Pk_gm, _ = get_PXm_terms(param_array_bin1,
                                     Pkth_array_khres,
                                     pt_type=pt_type)
            Pk_gm_bin_f, Pk_gg_bin_f, Pk_mm_bin_f = np.zeros(
                len(k_hres)), np.zeros(len(k_hres)), np.zeros(len(k_hres))
            for i in range(len(k_hres)):
                Pk_gm_temp = intspline(z_pk, Pk_gm[:, i])
                Pk_gg_temp = intspline(z_pk, Pk_gg[:, i])
                Pk_mm_temp = intspline(z_pk, Pk_mm[:, i])

                Pk_gm_bin_f[i] = Pk_gm_temp(zmean_gm)
                Pk_gg_bin_f[i] = Pk_gg_temp(zmean_g)
                Pk_mm_bin_f[i] = Pk_mm_temp(zmean_m)

            block.put_double_array_1d("pk_to_xi", "Pk_gg_bin % s" % (j + 1),
                                      Pk_gg_bin_f)
            block.put_double_array_1d("pk_to_xi", "Pk_gm_bin % s" % (j + 1),
                                      Pk_gm_bin_f)
            block.put_double_array_1d("pk_to_xi", "Pk_mm_bin % s" % (j + 1),
                                      Pk_mm_bin_f)

            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array,
                           "xi_gg_mat_bin % s" % (j + 1), xi_gg)
            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array,
                           "xi_gm_mat_bin % s" % (j + 1), xi_gm)

            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array,
                           "xi_gg_mm_mat_bin % s" % (j + 1), xi_gg / xi_mm)
            block.put_grid("pk_to_xi", "z", z_pk, "r", r_array,
                           "xi_gm_mm_mat_bin % s" % (j + 1), xi_gm / xi_mm)

            if j == 0:
                block.put("pk_to_xi", "Pk_all", Pkth_array_khres)
                block.put("pk_to_xi", "xi_all", xi_all)
                block.put_grid("pk_to_xi", "z", z_pk, "r", r_array,
                               "xi_mm_mat", xi_mm)

        block.put_double_array_1d("pk_to_xi", "xi_gg_bin % s" % (j + 1),
                                  xi_gg_bin_f)
        block.put_double_array_1d("pk_to_xi", "xi_gm_bin % s" % (j + 1),
                                  xi_gm_bin_f)
        block.put_double_array_1d("pk_to_xi", "xi_mm_bin % s" % (j + 1),
                                  xi_mm_bin_f)

        block.put_double_array_1d("pk_to_xi", "xi_gg_mm_bin % s" % (j + 1),
                                  xi_gg_mm_bin_f)
        block.put_double_array_1d("pk_to_xi", "xi_gm_mm_bin % s" % (j + 1),
                                  xi_gm_mm_bin_f)

        block.put_double("pk_to_xi", "zmean_gg_bin % s" % (j + 1), zmean_g)
        block.put_double("pk_to_xi", "zmean_gm_bin % s" % (j + 1), zmean_gm)

        block.put_double("pk_to_xi", "zmean_gg_mm_bin % s" % (j + 1), zmean_g)
        block.put_double("pk_to_xi", "zmean_gm_mm_bin % s" % (j + 1), zmean_gm)

    return 0
Exemplo n.º 10
0
def get_Pktharray(output_nl_grid,
                  klin,
                  knl,
                  Pkzlin,
                  Pnl_kz,
                  usePNL_for_Pk=True,
                  pt_type=None,
                  Pk_terms_names=None,
                  z_array=None,
                  output_xi=False,
                  use_fftlog=False):
    klinlog = np.log(klin)

    Pk0lin = Pkzlin[0, :]

    ind = np.where(klin > 0.03)[0][0]

    Growth = np.sqrt(Pkzlin[:, ind] / Pkzlin[0, ind])

    nk = 4 * len(
        klin
    )  # higher res increases runtime, decreases potential ringing at low-k
    # eps = 1e-6
    # eps = 0.
    # kmin = np.log10((1. + eps) * klin[0])
    # kmax = np.log10((1. - eps) * klin[-1])

    kmin = -6.0
    kmax = 3.0

    klin_fpt = np.logspace(kmin, kmax, nk)
    k1log = np.log(klin_fpt)
    #    pinterp=interp1d(klinlog,np.log(Pk0lin),bounds_error=False, fill_value="extrapolate")
    plininterp = interp1d(klinlog,
                          np.log(Pk0lin),
                          fill_value='extrapolate',
                          bounds_error=False)

    ## This interpolation should be at the input bounds. Extrapolate used to avoid failure due to numerical noise. No actual extrapolation is done. We could handle this using a margin or something else
    Plin_klin_fpt = np.exp(plininterp(k1log))

    if (knl[0] < klin_fpt[0]) or (knl[-1] > klin_fpt[-1]):
        EK1 = k_extend(klin_fpt, np.log10(knl[0]), np.log10(knl[-1]))
        klin_fpt = EK1.extrap_k()
        Plin_klin_fpt = EK1.extrap_P_low(Plin_klin_fpt)
        Plin_klin_fpt = EK1.extrap_P_high(Plin_klin_fpt)

    klin_fpt_log = np.log(klin_fpt)
    knl_log = np.log(knl)

    # print output_nl_grid
    if output_nl_grid:
        temp = intspline(klin_fpt_log, np.log(Plin_klin_fpt))
        Plin = np.exp(temp(knl_log))
        Pnl1 = Pnl_kz
        if usePNL_for_Pk:
            knl2mat = np.tile(knl**2, (Pnl_kz.shape[0], 1))
            k2Pnl1 = np.multiply(knl2mat, Pnl_kz)
        else:
            k2Pnl1 = np.outer(Growth**2, (knl**2) * Plin)

        if output_xi:
            renorm = np.sqrt(np.pi / 2.)

            if use_fftlog:
                myfftlog = fftlog(knl, (knl**3) * Plin,
                                  nu=1.5,
                                  c_window_width=0.2)
                r_lin, xi_lin_z0 = myfftlog.fftlog(0)
            else:
                r_lin, xitemp = sph(knl, nu=0, q=1.5)(Plin * renorm,
                                                      extrap=True)
                xi_lin_z0 = (1 / (2 * np.pi**2)) * xitemp
            xi_lin = np.outer(Growth**2, xi_lin_z0)
            xi_nl = np.zeros(xi_lin.shape)
            xi_k2Pk = np.zeros(xi_lin.shape)
            for j in range(len(z_array)):
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * Pnl_kz[j, :],
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_basis = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0, q=1.5)(Pnl_kz[j, :] * renorm,
                                                      extrap=True)
                    xi_basis = (1 / (2 * np.pi**2)) * xitemp

                xi_nl[j, :] = xi_basis

                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * k2Pnl1[j, :],
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_basis = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0, q=1.5)(k2Pnl1[j, :] * renorm,
                                                      extrap=True)
                    xi_basis = (1 / (2 * np.pi**2)) * xitemp
                xi_k2Pk[j, :] = xi_basis
    else:
        Plin = Plin_klin_fpt
        Pnl1 = Pnl_kz
        if usePNL_for_Pk:
            k2Pnl1 = np.outer(Growth**2, (klin_fpt**2) * Pnl1[0, :])
        else:
            k2Pnl1 = np.outer(Growth**2, (klin_fpt**2) * Plin)

    Plin_kz = np.outer(Growth**2, Plin)

    n_pad = len(klin_fpt)

    if pt_type in ['oneloop_eul_bk']:

        fastpt = FASTPT.FASTPT(klin_fpt,
                               to_do=['one_loop_dd'],
                               low_extrap=-5,
                               high_extrap=3,
                               n_pad=n_pad)
        PXXNL_b1b2bsb3nl = fastpt.one_loop_dd_bias_b3nl(Plin_klin_fpt,
                                                        C_window=.75)

        if output_nl_grid:

            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[2]))
            Pd1d2 = np.outer(Growth**4, (temp(knl_log)))
            if output_xi:
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * temp(knl_log),
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_d1d2_z0 = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0, q=1.5)(temp(knl_log) * renorm,
                                                      extrap=True)
                    xi_d1d2_z0 = (1 / (2 * np.pi**2)) * xitemp
                xi_d1d2 = np.outer(Growth**4, xi_d1d2_z0)

            temp = intspline(klin_fpt_log, np.log(PXXNL_b1b2bsb3nl[3]))
            Pd2d2 = np.outer(Growth**4, np.exp(temp(knl_log)))
            if output_xi:
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * np.exp(temp(knl_log)),
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_d2d2_z0 = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0,
                                    q=1.5)(np.exp(temp(knl_log)) * renorm,
                                           extrap=True)
                    xi_d2d2_z0 = (1 / (2 * np.pi**2)) * xitemp
                xi_d2d2 = np.outer(Growth**4, xi_d2d2_z0)

            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[4]))
            Pd1s2 = np.outer(Growth**4, (temp(knl_log)))
            if output_xi:
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * temp(knl_log),
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_d1s2_z0 = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0, q=1.5)(temp(knl_log) * renorm,
                                                      extrap=True)
                    xi_d1s2_z0 = (1 / (2 * np.pi**2)) * xitemp
                xi_d1s2 = np.outer(Growth**4, xi_d1s2_z0)

            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[5]))
            Pd2s2 = np.outer(Growth**4, (temp(knl_log)))
            if output_xi:
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * temp(knl_log),
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_d2s2_z0 = myfftlog.fftlog(0)
                else:
                    rval, xitemp = sph(knl, nu=0,
                                       q=1.5)(temp(knl_log) * renorm,
                                              extrap=False)
                    xi_d2s2_z0 = (1 / (2 * np.pi**2)) * xitemp
                    # pdb.set_trace()
                xi_d2s2 = np.outer(Growth**4, xi_d2s2_z0)

            temp = intspline(klin_fpt_log, np.log(PXXNL_b1b2bsb3nl[6]))
            Ps2s2 = np.outer(Growth**4, np.exp(temp(knl_log)))
            if output_xi:
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * np.exp(temp(knl_log)),
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_s2s2_z0 = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0,
                                    q=1.5)(np.exp(temp(knl_log)) * renorm,
                                           extrap=True)
                    xi_s2s2_z0 = (1 / (2 * np.pi**2)) * xitemp
                xi_s2s2 = np.outer(Growth**4, xi_s2s2_z0)

            temp = intspline(klin_fpt_log, (PXXNL_b1b2bsb3nl[7]))
            sig3nl = np.outer(Growth**4, (temp(knl_log)))
            if output_xi:
                if use_fftlog:
                    myfftlog = fftlog(knl, (knl**3) * temp(knl_log),
                                      nu=1.5,
                                      c_window_width=0.2)
                    _, xi_sig3nl_z0 = myfftlog.fftlog(0)
                else:
                    _, xitemp = sph(knl, nu=0, q=1.5)(temp(knl_log) * renorm,
                                                      extrap=True)
                    xi_sig3nl_z0 = (1 / (2 * np.pi**2)) * xitemp
                xi_sig3nl = np.outer(Growth**4, xi_sig3nl_z0)

            sig4 = np.outer(Growth**4, PXXNL_b1b2bsb3nl[8] * np.ones_like(knl))
        else:
            [Pd1d2, Pd2d2, Pd1s2, Pd2s2, Ps2s2, sig3nl, sig4] = [
                np.outer(Growth**4, PXXNL_b1b2bsb3nl[2]),
                np.outer(Growth**4, PXXNL_b1b2bsb3nl[3]),
                np.outer(Growth**4, PXXNL_b1b2bsb3nl[4]),
                np.outer(Growth**4, PXXNL_b1b2bsb3nl[5]),
                np.outer(Growth**4, PXXNL_b1b2bsb3nl[6]),
                np.outer(Growth**4, PXXNL_b1b2bsb3nl[7]),
                np.outer(
                    Growth**4,
                    PXXNL_b1b2bsb3nl[8] * np.ones_like(PXXNL_b1b2bsb3nl[0]))
            ]

        pdb.set_trace()
        Pk_th_array = [
            Plin_kz, Pnl1, Pd1d2, Pd2d2, Pd1s2, Pd2s2, Ps2s2, sig3nl, k2Pnl1,
            sig4
        ]
        if output_xi:
            xi_th_array = [
                xi_lin, xi_nl, xi_d1d2, xi_d2d2, xi_d1s2, xi_d2s2, xi_s2s2,
                xi_sig3nl, xi_k2Pk
            ]

    elif pt_type in ['oneloop_cleft_bk']:
        cl = cpool.CLEFT(k=klin_fpt, p=Plin_klin_fpt)
        Pk_th_array = np.zeros((len(Pk_terms_names), len(Growth), len(knl)))

        kinput_ind = np.where((knl < 4.) & (knl > 1e-3))[0]
        kinput = knl[kinput_ind]

        # pdb.set_trace()
        pk_table_f = cpool.get_nonlinear_kernels_wgroth(cl,
                                                        kinput,
                                                        koutput=knl,
                                                        Growth=Growth,
                                                        do_analysis=False,
                                                        Pnl_toplot=Pnl1,
                                                        z_array=z_array)

        Pk_th_array[1:-1, :, :] = pk_table_f
        Pk_th_array[0, :, :] = Plin_kz
        Pk_th_array[1, :, :] = Pnl1
        Pk_th_array[-1, :, :] = k2Pnl1
    else:
        Pk_th_array = None
        print('give correct pt_type')

    if output_nl_grid:
        kout = knl
    else:
        kout = klin_fpt

    if output_xi:
        return Pk_th_array, kout, xi_th_array, r_lin
    else:
        return Pk_th_array, kout