Example #1
0
def wp_from_files(header_file, filename, output_file, pimax, z_lens, rp_min,
                  rp_max, nbins):

    # read in cosmological parameters from header_file

    import config
    cf = config.AbacusConfigFile(header_file)
    omega_m = cf.Omega_M  # at z=0

    binmin, binmax, _, xi = np.loadtxt(filename, unpack=True)

    rp_binmin, rp_binmax, w_p = wp(binmin,
                                   binmax,
                                   xi,
                                   omega_m,
                                   float(z_lens),
                                   pimax=float(pimax),
                                   rp_min=float(rp_min),
                                   rp_max=float(rp_max),
                                   nbins=int(nbins))

    np.savetxt(output_file,
               np.c_[rp_binmin, rp_binmax,
                     np.zeros(w_p.shape[0]), w_p],
               delimiter='\t')
def DeltaSigma_from_files(header_file,
                          filename,
                          output_file,
                          pimax,
                          z_lens,
                          z_source,
                          rp_min,
                          rp_max,
                          nbins,
                          compute_DS=None):
                          
    ## read in cosmological parameters from header_file
    
    import config
    cf = config.AbacusConfigFile(header_file)
    omega_m = cf.Omega_M	# at z=0
    H_0 = 100.				# use h Msun pc^-3 units

    binmin, binmax, null, xi = np.loadtxt(filename, unpack=True)
    
    if compute_DS == False:
        DS_binmin, DS_binmax, DS = compute_gamma_t(
            binmin,
            binmax,
            xi,
            pimax=float(pimax),
            H0=H_0,
            Omega_m=omega_m,
            z_lens=z_lens,
            z_source=z_source,
            rp_min=float(rp_min),
            rp_max=float(rp_max),
            nbins=int(nbins))
    else:
        DS_binmin, DS_binmax, DS = DeltaSigma(
            binmin,
            binmax,
            xi,
            pimax=float(pimax),
            H0=H_0,
            Omega_m=omega_m,
            z_lens=z_lens,
            z_source=z_source,
            rp_min=rp_min,
            rp_max=rp_max,
            nbins=nbins)

    np.savetxt(
        output_file,
        np.c_[DS_binmin, DS_binmax,
              np.zeros(DS.shape[0]), DS],
        delimiter='\t')
def compute_mass_function(halo_file, header_file, output_file):

    cf = config.AbacusConfigFile(header_file)
    boxsize = cf.boxSize
    assert (boxsize > 0.)

    with h5.File(halo_file, mode='r') as catalog:
        halos = catalog['halos']
        binmin, binmax, mass_fun = mass_function(halos['mass'], boxsize)

    ## save mass function

    assert (~np.any(np.isnan(binmin)))
    assert (~np.any(np.isnan(binmax)))
    assert (~np.any(np.isnan(mass_fun)))

    np.savetxt(output_file, np.c_[binmin, binmax, mass_fun])
Example #4
0
def compute_HOD_parameters(ndens=None,
                           nfid=None,
                           M1_over_Mmin=None,
                           M0_over_M1=None,
                           alpha=None,
                           siglogM=None,
                           halos=None,
                           header=None,
                           logMmin_guess=13.5):
    """
    Compute the physical (er, Msun h^-1) HOD parameters from ratios of masses
    and the desired number density of galaxies.
    We convert the number density of galaxies into Mmin using the halo mass function.
    """
    logM = np.linspace(11.5, 15.0, 51)

    with h5.File(halos, mode='r') as catalog:
        bin_counts, bin_edges = np.histogram(np.log10(
            catalog['halos']['mass']),
                                             bins=logM)

    cf = config.AbacusConfigFile(header)
    vol = cf.boxSize**3
    dM = np.diff(10**(logM))
    mass_function = bin_counts / vol / dM

    logM = logM[:-1]
    """
    now solve for logMmin:
    compute_ngal(...) - ndens*nfid = 0
    """
    objective = lambda logMmin: compute_ngal(logM, dM, mass_function, logMmin,
                                             M1_over_Mmin, M0_over_M1, alpha,
                                             siglogM) - ndens * nfid

    logMmin = scipy.optimize.newton(objective, logMmin_guess, maxiter=100)
    Mmin = 10**(logMmin)
    M1 = Mmin * M1_over_Mmin
    M0 = M1 * M0_over_M1

    return logMmin, np.log10(M0), np.log10(M1)
Example #5
0
def compute(halo_file,
            env_file,
            header_file,
            output_file,
            siglogM=None,
            logMmin=None,
            logM0=None,
            logM1=None,
            alpha=None,
            q_env=None,
            del_gamma=None,
            seed=42,
            njackknife=8):
    cf = config.AbacusConfigFile(header_file)
    boxsize = cf.boxSize
    omeganow_m = cf.OmegaNow_m
    omega_m = cf.Omega_M
    redshift = cf.redshift

    ## now compute HOD
    populate_hod(halo_file, output_file, env_file, omega_m,redshift,boxsize,\
                         siglogM,logMmin,logM0,logM1,alpha,q_env,del_gamma,seed=seed)
Example #6
0
    siglogM = float(params['siglogM'])
    alpha = float(params['alpha'])
    input_ngal = float(params['ngal'])
    M0_over_M1 = float(params['m0_over_m1'])
    M1_over_Mmin = float(params['m1_over_mmin'])

    #	print("HOD parameters:",file=sys.stderr)
    #	print("\tinput_ngal = {}".format(input_ngal),file=sys.stderr)
    #	print("\tM1_over_Mmin = {}".format(M1_over_Mmin),file=sys.stderr)
    #	print("")

    ## read cosmological params, compute linear Pk with CAMB

    header_file = args.header_file

    cf = config.AbacusConfigFile(header_file)
    omega_m = cf.Omega_M  # at z=0
    redshift = cf.redshift
    sigma_8 = cf.sigma_8
    ns = cf.ns
    ombh2 = cf.ombh2
    omch2 = cf.omch2
    w0 = cf.w0  ## WARNING: growth factor is currently computed assuming w == -1.0!!!
    H0 = cf.H0

    #	## compute (linear) power spectrum
    #
    #	k, P = camb_linear_pk(ombh2=ombh2, omch2=omch2, H0=H0, ns=ns, w0=w0,
    #							sigma8=sigma_8, redshift=redshift)
    #
    #
Example #7
0
def read_data(sims_dir,
              redshift_dir,
              param_files,
              filename_ext=None,
              load_scalar=False):
    """read parameters from *.template_param files, read in *.galaxy_bias files,
	read simulation /header files for cosmological params.
	return (matrix X of parameters, vector Y of observations) for each radial bin."""

    sims_path = Path(sims_dir)

    # each param_file should be of the form '*.template_param'
    def obs_filename(param_filename):
        return str(param_filename) + filename_ext

    if load_scalar == True:
        obs_vector = load_scalar_file(obs_filename(param_files[0]))
    else:
        binmin, binmax, obs_err, obs_vector = load_correlation_file(
            obs_filename(param_files[0]))

    if load_scalar == True:
        nbins = 1
    else:
        nbins = len(binmin)

    nobs = len(param_files)
    nparams = 17

    X = np.zeros((nobs, nparams))
    Y = np.zeros((nbins, nobs))
    Yerr = np.zeros((nbins, nobs))
    SimID = np.empty((nobs, ), dtype=object)
    good_sample = np.empty(nobs, dtype=np.bool)
    good_sample[:] = True

    param_names_vector = np.asarray([
        'ngal', 'siglogM', 'M0_over_M1', 'M1_over_Mmin', 'alpha', 'q_env',
        'del_gamma', 'f_cen', 'A_conc', 'R_rescale', 'redshift', 'sigma_8',
        'H0', 'ombh2', 'omch2', 'w0', 'ns', 'sim_id'
    ])

    for j, param_file in enumerate(param_files):

        myconfigparser = configparser.ConfigParser()
        myconfigparser.read(str(param_file))
        hod_params = myconfigparser['params']

        ## read in simulation file

        simdir = hod_params['dir'].strip('"')
        subdir = sims_path / hod_params['dir'].strip('"') / redshift_dir
        header_file = str(subdir) + '/header'
        cf = config.AbacusConfigFile(header_file)
        redshift = cf.redshift
        sigma_8 = cf.sigma_8
        H0 = cf.H0
        ombh2 = cf.ombh2
        omch2 = cf.omch2
        w0 = cf.w0
        ns = cf.ns

        bias_file = obs_filename(param_file)
        if not Path(bias_file).exists():
            continue  # ignore missing files

        if load_scalar == True:
            obs_vector = load_scalar_file(bias_file)
            obs_err = (obs_vector * 1e-2)  # arbitrary
        else:
            binmin, binmax, obs_err, obs_vector = load_correlation_file(
                bias_file)

        hod_vector = np.array([
            float(hod_params['ngal']),
            float(hod_params['siglogM']),
            float(hod_params['M0_over_M1']),
            float(hod_params['M1_over_Mmin']),
            float(hod_params['alpha']),
            float(hod_params['q_env']),
            float(hod_params['del_gamma']),
            float(hod_params['f_cen']),
            float(hod_params['A_conc']),
            float(hod_params['R_rescale'])
        ])

        cosmo_vector = np.array([redshift, sigma_8, H0, ombh2, omch2, w0, ns])
        this_param_vector = np.concatenate([hod_vector, cosmo_vector])

        ## reject extreme samples (*and* delete these rows from the arrays)
        #		if (alpha > 1.8):  # most of the difficulty is predicting high alpha models
        #		if (M1_over_Mmin < 8.0 and alpha < 0.7):
        #			good_sample[j] = False

        assert (np.all(obs_err > 0.))

        X[j, :] = this_param_vector
        Y[:, j] = obs_vector
        Yerr[:, j] = obs_err
        SimID[j] = simdir

    ## delete rows of X, Y, Yerr that have np.NaN entries

    mask = good_sample
    X = X[mask, :]
    Y = Y[:, mask]
    Yerr = Yerr[:, mask]
    SimID = SimID[mask]
    good_param_files = np.asarray(param_files)[mask]

    ## delete columns of X that have zero variance (for numerical stability)

    idx = []
    for i in range(X.shape[1]):
        x = X[:, i]
        var = np.var(x)
        eps = 1e-15
        if var > eps:
            print(
                f"variance for parameter {i} [{param_names_vector[i]}] = {var}; range = [{np.min(x)}, {np.max(x)}]"
            )
            idx.append(i)

    ## remove zero-variance columns

    X = X[:, idx]
    param_names = param_names_vector[idx]

    print("nparams: {}".format(X.shape[1]))
    print("nobs: {}".format(X.shape[0]))

    if load_scalar == True:
        binmin = 0.
        binmax = 0.

    return X, Y, Yerr, SimID, binmin, binmax, param_names, good_param_files
Example #8
0
""" Compute linear correlation function from input power spectrum """
if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('input_file')
    parser.add_argument('min_r_bin', type=float)
    parser.add_argument('max_r_bin', type=float)
    parser.add_argument('nbins', type=int)
    parser.add_argument('header_file')
    parser.add_argument('output_file')
    args = parser.parse_args()

    # read in cosmological parameters from header_file
    import config
    cf = config.AbacusConfigFile(args.header_file)
    omega_m = cf.Omega_M  # at z=0
    redshift = cf.redshift
    target_sigma_8 = cf.sigma_8
    H_0 = cf.H0
    omch2 = cf.omch2
    ombh2 = cf.ombh2
    w0 = cf.w0
    ns = cf.ns

    # read in power spectrum
    data = np.loadtxt(args.input_file)
    k_camb = data[:, 0]
    P_camb = data[:, 1]
    log_k_camb = np.log10(k_camb)