def baofit_data_corrfunc(cat, n_rp_bins, n_threads): # Distances along the :math:\pi direction are binned with unit depth. # For instance, if pimax=40, then 40 bins will be created along the pi direction try: L = cat.BoxSize except: L = cat.Lbox[0] # redshift = cat.redshift x = cat.halo_table['halo_x'] y = cat.halo_table['halo_y'] z = cat.halo_table['halo_z'] pimax = 140 n = len.cat.halo_table nr = n * 3 xr = np.random.uniform(0, L, nr) yr = np.random.uniform(0, L, nr) zr = np.random.uniform(0, L, nr) rp_bins = np.logspace(np.log10(80), np.log10(130), n_rp_bins) # perpendicular bins DD = DDrppi(1, n_threads, pimax, rp_bins, x, y, z, periodic=True, verbose=True, boxsize=L, output_rpavg=True, zbin_refine_factor=20) RR = DDrppi(1, n_threads, pimax, rp_bins, xr, yr, zr, periodic=True, verbose=True, boxsize=L, output_rpavg=True, zbin_refine_factor=20) DR = DDrppi(0, n_threads, pimax, rp_bins, x, y, z, X2=xr, Y2=yr, Z2=zr, periodic=True, verbose=True, boxsize=L, output_rpavg=True, zbin_refine_factor=20) xi = convert_3d_counts_to_cf(n, n, nr, nr, DD, DR, DR, RR) return xi
def calc_hmcf(halo_path, RR, DdRd, Dd, Rh, savepath=None, config=config_default): """Calculate the halo-matter correlation function Note: assumes the halo catalog has the format X,Y,Z,N,M,Richness. Args: halo_path (string): Path to the halo catalog RR: Corrfunc output for the RR counts DdRd: Corrfunc output for dark matter particles counted against randoms Dd: Array containing locations of dm particles Rh: Array containing locations of halo randoms save_path (string): Path to save the output config (dictionary): Configuration of the Corrfunc run. Default is config_default Returns: R (numpy.array): Radii of the correlation function hmcf (numpy.array): Halo-matter correlation function. """ boxsize = config["boxsize"] nthreads = config["nthreads"] bins = config["bins"] Xd, Yd, Zd = Dd X1, Y1, Z1 = Rh Xh, Yh, Zh, Np, M, Rich = np.genfromtxt(halo_path, unpack=True) N_h = len(Xh) N_dm = len(Xd) N_rand = len(X1) DhDd = DD(0, nthreads, bins, X1=Xh, Y1=Yh, Z1=Zh, X2=Xd, Y2=Yd, Z2=Zd) DhRh = DD(0, nthreads, bins, Xh, Yh, Zh, X2=X1, Y2=Y1, Z2=Z1) hmcf = convert_3d_counts_to_cf(N_h, N_dm, N_rand, N_rand, DhDd, DhRh, DdRd, RR) R = (bins[:-1] + bins[1:]) / 2. if savepath: out = np.array([R, hmcf]) np.savetxt(savepath, out) return [R, np.array(hmcf)]
def reference_survey_tpcf(data1, randoms1, redges, Nmu, data2=None, randoms2=None): """Reference 2D 2PCF using Corrfunc""" from Corrfunc.utils import convert_3d_counts_to_cf from Corrfunc.mocks import DDsmu_mocks if data2 is None: data2 = data1 if randoms2 is None: randoms2 = randoms1 # updack the columns to pass to Corrfunc ra_d1, dec_d1, r_d1 = data1.T ra_r1, dec_r1, r_r1 = randoms1.T ra_d2, dec_d2, r_d2 = data2.T ra_r2, dec_r2, r_r2 = randoms2.T # the Corrfunc keywords kws = {} kws['cosmology'] = 1 kws['nthreads'] = 1 kws['nmu_bins'] = Nmu kws['mu_max'] = 1.0 kws['binfile'] = redges kws['is_comoving_dist'] = True # do the pair counting D1D2 = DDsmu_mocks(0, RA1=ra_d1, DEC1=dec_d1, CZ1=r_d1, RA2=ra_d2, DEC2=dec_d2, CZ2=r_d2, **kws) D1R2 = DDsmu_mocks(0, RA1=ra_d1, DEC1=dec_d1, CZ1=r_d1, RA2=ra_r2, DEC2=dec_r2, CZ2=r_r2, **kws) D2R1 = DDsmu_mocks(0, RA1=ra_d2, DEC1=dec_d2, CZ1=r_d2, RA2=ra_r1, DEC2=dec_r1, CZ2=r_r1, **kws) R1R2 = DDsmu_mocks(0, RA1=ra_r1, DEC1=dec_r1, CZ1=r_r1, RA2=ra_r2, DEC2=dec_r2, CZ2=r_r2, **kws) # combine using Landy-Szalay ND1 = len(ra_d1); ND2 = len(ra_d2) NR1 = len(ra_r1); NR2 = len(ra_r2) CF = convert_3d_counts_to_cf(ND1, ND2, NR1, NR2, D1D2, D1R2, D2R1, R1R2).reshape((-1,Nmu)) D1D2 = D1D2.reshape((-1,Nmu)) D1R2 = D1R2.reshape((-1,Nmu)) D2R1 = D2R1.reshape((-1,Nmu)) R1R2 = R1R2.reshape((-1,Nmu)) return D1D2, D1R2, D2R1, R1R2, CF
def reference_survey_tpcf(data1, randoms1, edges, pimax, data2=None, randoms2=None): """Reference projected 2PCF using Corrfunc""" from Corrfunc.utils import convert_3d_counts_to_cf, convert_rp_pi_counts_to_wp from Corrfunc.mocks import DDrppi_mocks if data2 is None: data2 = data1 if randoms2 is None: randoms2 = randoms1 # updack the columns to pass to Corrfunc ra_d1, dec_d1, r_d1 = data1.T ra_r1, dec_r1, r_r1 = randoms1.T ra_d2, dec_d2, r_d2 = data2.T ra_r2, dec_r2, r_r2 = randoms2.T # the Corrfunc keywords kws = {} kws['cosmology'] = 1 kws['nthreads'] = 1 kws['pimax'] = pimax kws['binfile'] = edges kws['is_comoving_dist'] = True # do the pair counting D1D2 = DDrppi_mocks(0, RA1=ra_d1, DEC1=dec_d1, CZ1=r_d1, RA2=ra_d2, DEC2=dec_d2, CZ2=r_d2, **kws) D1R2 = DDrppi_mocks(0, RA1=ra_d1, DEC1=dec_d1, CZ1=r_d1, RA2=ra_r2, DEC2=dec_r2, CZ2=r_r2, **kws) D2R1 = DDrppi_mocks(0, RA1=ra_d2, DEC1=dec_d2, CZ1=r_d2, RA2=ra_r1, DEC2=dec_r1, CZ2=r_r1, **kws) R1R2 = DDrppi_mocks(0, RA1=ra_r1, DEC1=dec_r1, CZ1=r_r1, RA2=ra_r2, DEC2=dec_r2, CZ2=r_r2, **kws) # combine using Landy-Szalay ND1 = len(ra_d1); ND2 = len(ra_d2) NR1 = len(ra_r1); NR2 = len(ra_r2) CF = convert_3d_counts_to_cf(ND1, ND2, NR1, NR2, D1D2, D1R2, D2R1, R1R2).reshape((-1,pimax)) wp = convert_rp_pi_counts_to_wp(ND1, ND2, NR1, NR2, D1D2, D1R2, D2R1, R1R2, len(edges)-1, pimax) D1D2 = D1D2.reshape((-1,pimax)) D1R2 = D1R2.reshape((-1,pimax)) D2R1 = D2R1.reshape((-1,pimax)) R1R2 = R1R2.reshape((-1,pimax)) return D1D2, D1R2, D2R1, R1R2, CF, wp
def angular_correlation_function(table_1, table_2, table_r, theta_bins): n_1 = len(table_1) n_2 = len(table_2) n_r = len(table_r) n = multiprocessing.cpu_count() d1d2 = DDtheta_mocks( False, n, theta_bins, table_1['ra'], table_1['dec'], RA2=table_2['ra'], DEC2=table_2['dec'])['npairs'] d1r = DDtheta_mocks( False, n, theta_bins, table_1['ra'], table_1['dec'], RA2=table_r['ra'], DEC2=table_r['dec'])['npairs'] d2r = DDtheta_mocks( False, n, theta_bins, table_2['ra'], table_2['dec'], RA2=table_r['ra'], DEC2=table_r['dec'])['npairs'] rr = DDtheta_mocks( True, 4, theta_bins, table_r['ra'], table_r['dec'])['npairs'] return convert_3d_counts_to_cf(n_1, n_2, n_r, n_r, d1d2, d1r, d2r, rr)
def angular_corr_from_coords(ras, decs, randras, randdecs, weights=None, randweights=None, nthreads=1, nbins=10): bins = np.logspace(-2, 1, (nbins + 1)) # autocorrelation of catalog DD_counts = DDtheta_mocks(1, nthreads, bins, ras, decs, weights1=weights) # cross correlation between data and random catalog DR_counts = DDtheta_mocks(0, nthreads, bins, ras, decs, RA2=randras, DEC2=randdecs, weights1=weights, weights2=randweights) # autocorrelation of random points RR_counts = DDtheta_mocks(1, nthreads, bins, randras, randdecs, weights1=randweights) wtheta = convert_3d_counts_to_cf(len(ras), len(ras), len(randras), len(randras), DD_counts, DR_counts, DR_counts, RR_counts) return wtheta
def reference_survey_tpcf(data1, randoms1, redges, data2=None, randoms2=None): """Reference 1D 2PCF using Corrfunc""" from Corrfunc.utils import convert_3d_counts_to_cf from Corrfunc.mocks import DDsmu_mocks if data2 is None: data2 = data1 if randoms2 is None: randoms2 = randoms1 # updack the columns to pass to Corrfunc ra_d1, dec_d1, r_d1 = data1.T ra_r1, dec_r1, r_r1 = randoms1.T ra_d2, dec_d2, r_d2 = data2.T ra_r2, dec_r2, r_r2 = randoms2.T # the Corrfunc keywords kws = {} kws['cosmology'] = 1 kws['nthreads'] = 1 kws['nmu_bins'] = 1 kws['mu_max'] = 1.0 kws['binfile'] = redges kws['is_comoving_dist'] = True # do the pair counting D1D2 = DDsmu_mocks(0, RA1=ra_d1, DEC1=dec_d1, CZ1=r_d1, RA2=ra_d2, DEC2=dec_d2, CZ2=r_d2, **kws) D1R2 = DDsmu_mocks(0, RA1=ra_d1, DEC1=dec_d1, CZ1=r_d1, RA2=ra_r2, DEC2=dec_r2, CZ2=r_r2, **kws) D2R1 = DDsmu_mocks(0, RA1=ra_d2, DEC1=dec_d2, CZ1=r_d2, RA2=ra_r1, DEC2=dec_r1, CZ2=r_r1, **kws) R1R2 = DDsmu_mocks(0, RA1=ra_r1, DEC1=dec_r1, CZ1=r_r1, RA2=ra_r2, DEC2=dec_r2, CZ2=r_r2, **kws) # combine using Landy-Szalay ND1 = len(ra_d1); ND2 = len(ra_d2) NR1 = len(ra_r1); NR2 = len(ra_r2) CF = convert_3d_counts_to_cf(ND1, ND2, NR1, NR2, D1D2, D1R2, D2R1, R1R2) return D1D2, D1R2, D2R1, R1R2, CF
def run_corrfunc(data1, rand1, data2, rand2, rpbins, pimax, cosmo, nproc=1, weights_data=None, weights_rand=None, pibinwidth=1, zspace=False, proj=False): print 'Running corrfunc' #can only do autocorrelations right now ndata = len(data1.index) nrand = len(rand1.index) if zspace: print "Zspace" res = corrfunc.counts(data1['ra'].values, data1['dec'].values, data1['z'].values, rand1['ra'].values, rand1['dec'].values, rand1['z'].values, rpbins, pimax, cosmo, nproc=nproc, weights_data=weights_data, weights_rand=weights_rand, comoving=True, zspace=True, proj=proj) if proj: print "Proj" dd, dr, rr, svals, xi_proj, amps = res x = bins_logavg(rpbins) #TODO: will have to do this in corrfunc to be consistent!! #basisfuncs = [estimator_chunks.tophat_xis] #K = len(rpbins)-1 #vals = None xi_orig = convert_3d_counts_to_cf(ndata, ndata, nrand, nrand, dd, dr, dr, rr) #args = [rpbins, None] #print sdfsf #s, xis_proj = calc_wprp(a, x, basisfuncs, K, rpbins, vals, pibinwidth, zspace, *args) return svals, xi_orig, xi_proj, amps else: dd, dr, rr = res xi = convert_3d_counts_to_cf(ndata, ndata, nrand, nrand, dd, dr, dr, rr) rp_avg = 0.5 * (rpbins[1:] + rpbins[:-1]) return rp_avg, xi else: dd, dr, rr, rp_avg = corrfunc.counts(data1['ra'].values, data1['dec'].values, data1['z'].values, rand1['ra'].values, rand1['dec'].values, rand1['z'].values, rpbins, pimax, cosmo, nproc=nproc, weights_data=weights_data, weights_rand=weights_rand, comoving=True, proj=proj) est_ls, wprp = corrfunc.calc_wprp(dd, dr, rr, len(data1), len(rand1), pibinwidth=pibinwidth) ##est_ls = None return rp_avg, wprp
def run_dr7_LRGs(): print "Running corrfunc estimator on LRGs" #sample = 'Full' #DR = 7 sample = 'ns' DR = 3 nproc = 24 frac = 1 randfrac = 1 #proj_type = 'tophat' #proj_type = 'piecewise' proj_type = 'generalr' proj_label = proj_type + '_fast' #projfn = "/home/users/ksf293/Corrfunc/utils/dcosmos.dat" #projfn = None #projfn = "/home/users/ksf293/vectorizedEstimator/tables/dcosmos.dat" projfn = "/home/users/ksf293/vectorizedEstimator/tables/dcosmos_norm.dat" #proj_type = 'dcosmo' #proj_label = proj_type+'_const' projtag = '_' + proj_label if randfrac == 1: randfractag = '' else: randfractag = '_rand' + str(randfrac) saveto = "../results/dcosmo/xis_dr{}_{}LRG_frac{}{}{}.npy".format( DR, sample, frac, randfractag, projtag) K = 14 smin = 40 smax = 180 #K=2 #smin = 5 #smax = 15 sbins = np.linspace(smin, smax, K + 1) sbinsavg = np.array(0.5 * (sbins[1:] + sbins[:-1])) print "bins:", sbins if proj_type == "tophat" or proj_type == "piecewise": nprojbins = len(sbins) - 1 elif proj_type == "powerlaw": nprojbins = 5 elif proj_type == "dcosmo": nprojbins = 3 #FOR NOW elif proj_type == "generalr": nprojbins = 3 else: raise ValueError("Proj type {} not recognized".format(proj_type)) print "nprojbins:", nprojbins datafn = '../data/DR{}-{}.ascii'.format(DR, sample) randfn = '../data/random-DR{}-{}.ascii'.format(DR, sample) print "Loading data..." data = pd.read_csv(datafn, index_col=0) rand = pd.read_csv(randfn, index_col=0) #saveto = None if DR == 7: cosmo = LambdaCDM(H0=70, Om0=0.25, Ode0=0.75) elif DR == 3: cosmo = LambdaCDM(H0=70, Om0=0.30, Ode0=0.70) else: raise ValueError("DR {} not recognized".format(DR)) #check if need to return or in place utils.write_comoving_dist(data, datafn, cosmo) utils.write_comoving_dist(rand, randfn, cosmo) print 'ndata=', len(data.index) print 'nrand=', len(rand.index) #data = data.sample(frac=frac) #rand = rand.sample(frac=frac*randfrac) step = int(1 / frac) data = data[::step] rand = rand[::step] #data = data[:int(frac*len(data.index))] #rand = rand[:int(frac*len(rand.index))] nd = len(data.index) nr = len(rand.index) print 'ndata=', nd print 'nrand=', nr #weights_data = data['radial_weight']*data['fiber_coll_weight'] #weights_rand = rand['radial_weight'] weights_data = None weights_rand = None mumax = 1.0 #max of cosine ss = [] xis = [] labels = [] counts = [] aa = [] print "Running corrfunc..." start = time.time() dcm_col = 'dcm_Om0-{:.2f}'.format(cosmo.Om0) data_cz = data[dcm_col].values rand_cz = rand[dcm_col].values res = corrfuncproj.counts_smu(data['ra'].values, data['dec'].values, data_cz, rand['ra'].values, rand['dec'].values, rand_cz, sbins, mumax, cosmo, nproc=nproc, weights_data=weights_data, weights_rand=weights_rand, comoving=True, proj_type=proj_type, nprojbins=nprojbins, projfn=projfn) print("Completed counts") dd, dr, rr, qq, dd_orig, dr_orig, rr_orig = res # Note: dr twice because cross-correlations will be possible amps = compute_amps(nprojbins, nd, nd, nr, nr, dd, dr, dr, rr, qq) print 'Computed amplitudes' amps = np.array(amps) svals = np.linspace(smin, smax, 300) #print "svals:",svals #svals = np.array(0.5*(sbins[1:]+sbins[:-1])) nsvals = len(svals) sbins = np.array(sbins) nsbins = len(sbins) - 1 xi_proj = evaluate_xi(nprojbins, amps, nsvals, svals, nsbins, sbins, proj_type, projfn=projfn) #print("xi:", xi_proj) end = time.time() print 'Time for dr7 {} LRGs, nd={} nr={}: {}'.format( sample, nd, nr, end - start) xi_orig = convert_3d_counts_to_cf(nd, nd, nr, nr, dd_orig, dr_orig, dr_orig, rr_orig) #print "savg:", sbinsavg #print "xi_orig", xi_orig #print "svals", svals #print "xi_proj", xi_proj ss.append(sbinsavg) xis.append(xi_orig) labels.append("orig") counts.append([dd_orig, dr_orig, rr_orig]) aa.append(None) ss.append(svals) xis.append(xi_proj) labels.append(proj_label) counts.append([dd, dr, rr, qq]) aa.append(amps) if saveto: print "Saving to {}".format(saveto) np.save(saveto, [ss, xis, labels, counts, aa])
Xr1 = np.random.uniform(0, boxsize, N_rand) Yr1 = np.random.uniform(0, boxsize, N_rand) Zr1 = np.random.uniform(0, boxsize, N_rand) Xr2 = np.random.uniform(0, boxsize, N_rand) Yr2 = np.random.uniform(0, boxsize, N_rand) Zr2 = np.random.uniform(0, boxsize, N_rand) DhDd = DD(0, nthreads, bins, X1=Xh, Y1=Yh, Z1=Zh, X2=Xd, Y2=Yd, Z2=Zd) DhRh = DD(0, nthreads, bins, Xh, Yh, Zh, X2=Xr1, Y2=Yr1, Z2=Zr1) DdRd = DD(0, nthreads, bins, Xd, Yd, Zd, X2=Xr2, Y2=Yr2, Z2=Zr2) RhRd = DD(0, nthreads, bins, Xr1, Yr1, Zr1, X2=Xr2, Y2=Yr2, Z2=Zr2) hmcf = convert_3d_counts_to_cf(N_h, N_dm, N_rand, N_rand, DhDd, DhRh, DdRd, RhRd) np.savetxt("txt_files/hmcf_z%.2f.txt"%z,hmcf) for i in range(len(inds)): index = inds[i] z = zs[i] xi = np.loadtxt("txt_files/hmcf_z%.2f.txt"%z) print R.shape, xi.shape plt.loglog(R, xi, label="z=%.2f"%z) plt.legend(loc=0) plt.xlabel(r"$R\ [{\rm Mpc/h}]$", fontsize=24) plt.ylabel(r"$\xi_{\rm hm}$", fontsize=24) plt.subplots_adjust(bottom=0.15) plt.show() plt.clf()