def do_auto_correlation(model_name, phase, r, mode='smu-post-recon-std'): sim_name = '{}_{:02}-{}'.format(sim_name_prefix, cosmology, phase) filedir = os.path.join(save_dir, sim_name, 'z{}-r{}'.format(redshift, r)) if 'smu' in mode: DDnpy = np.load(os.path.join(filedir, '{}-auto-paircount-DD-{}.npy' .format(model_name, mode))) DD, _ = rebin_smu_counts(DDnpy, 'DD') np.savetxt(os.path.join(filedir, '{}-auto-paircount-DD-{}-rebinned.txt' .format(model_name, mode)), DD, fmt=txtfmt) # # create r vector from weighted average of DD pair counts # savg_vec = s_bins_centre # savg_vec = np.sum(savg * DD, axis=1) / np.sum(DD, axis=1) DR_ar, _, RR_ar = analytic_random(1, 1, 1, 1, s_bins, mu_bins, L**3) assert DD.shape == DR_ar.shape == RR_ar.shape np.savetxt(os.path.join(filedir, '{}-auto-paircount-DR-{}-ar.txt' .format(model_name, mode)), DR_ar, fmt=txtfmt) np.savetxt(os.path.join(filedir, '{}-auto-paircount-RR-{}-ar.txt' .format(model_name, mode)), RR_ar, fmt=txtfmt) xi_s_mu = ls_estimator(DD, DR_ar, DR_ar, RR_ar, RR_ar) xi_0 = tpcf_multipole(xi_s_mu, mu_bins, order=0) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=2) xi_0_txt = np.vstack([s_bins_centre, xi_0]).T xi_2_txt = np.vstack([s_bins_centre, xi_2]).T for output, tag in zip([xi_s_mu, xi_0_txt, xi_2_txt], ['xi', 'xi_0', 'xi_2']): np.savetxt(os.path.join(filedir, '{}-auto-{}-{}-ar.txt' .format(model_name, tag, mode)), output, fmt=txtfmt)
def multipoles(self, s: np.array, mu: np.array): if self.list_analytical_moments: self.s_mu = real2redshift.simps_integrate( s, mu, self.sim_measurement_best_fit.tpcf.mean, self.jointpdf_los) else: self.s_mu = real2redshift.simps_integrate( s, mu, self.sim_measurement.tpcf.mean, self.jointpdf_los) monopole = tpcf_multipole(self.s_mu, mu, order=0) quadrupole = tpcf_multipole(self.s_mu, mu, order=2) hexadecapole = tpcf_multipole(self.s_mu, mu, order=4) #self.wedges = tpcf_wedges(self.s_mu, mu) return monopole, quadrupole, hexadecapole
def compute_xi2(x, y, z, L, s_min, s_max, n_sbins, fn_save, nmubins=15, nthreads=1): print("Computing xi_2") # Set up bins (log) sbins = np.logspace(np.log10(smin), np.log10(smax), nsbins + 1) # note the + 1 to nbins s_avg = 10**(0.5 * (np.log10(sbins)[1:] + np.log10(sbins)[:-1])) # Use halotools to compute the quadrupole, as in this example: # https://halotools.readthedocs.io/en/latest/api/halotools.mock_observables.tpcf_multipole.html sample = np.vstack((x, y, z)).T mu_bins = np.linspace(0, 1, nmubins) xi_s_mu = s_mu_tpcf(sample, sbins, mu_bins, period=L) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=1) # Order 1 is quadrupole print("Saving") os.makedirs(os.path.dirname(savename), exist_ok=True) results = np.array([s_avg, xi_2]) np.savetxt(savename, results.T, delimiter=',', fmt=['%f', '%e'])
def multipoles(mu, s_mu): ''' Computes monopole, quadrupole and hexadecapole of the redshift space correlation function Args: s : array of s distances in Mpc/h mu : array of mu angular bins (between 0 and 1) Returns: monopole: monopole evaluated at the given s bins quadrupole: quadrupole evaluated at the given s bins hexadecapole: hexadecapole evaluated at the given s bins ''' monopole = tpcf_multipole(s_mu, mu, order=0) quadrupole = tpcf_multipole(s_mu, mu, order=2) hexadecapole = tpcf_multipole(s_mu, mu, order=4) return monopole, quadrupole, hexadecapole
def xi_rsd(x, y, z, L, smin, smax, nsbins, savename, nthreads=1, order=1): print("Computing xi_2") #LOG sbins = np.logspace(np.log10(smin), np.log10(smax), nsbins + 1) # note the + 1 to nbins s_avg = 10**(0.5 * (np.log10(sbins)[1:] + np.log10(sbins)[:-1])) #LINEAR #sbins = np.linspace(smin, smax, sbins + 1) # note the + 1 to nbins #s_avg = 0.5*(sbins[1:] + sbins[:-1]) sample = np.vstack((x, y, z)).T nmubins = 15 mu_bins = np.linspace(0, 1, nmubins) xi_s_mu = s_mu_tpcf(sample, sbins, mu_bins, period=L) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=order) print("Saving") os.makedirs(os.path.dirname(savename), exist_ok=True) print(xi_2) results = np.array([s_avg, xi_2]) np.savetxt(savename, results.T, delimiter=',', fmt=['%f', '%e'])
import numpy as np from halotools.mock_observables import tpcf, s_mu_tpcf, tpcf_multipole import matplotlib.pyplot as plt h = 0.6726 b = 2.23 pos = np.fromfile('sample_input_gal_mx3_float32_0-1100_mpc.dat', dtype=np.float32).reshape(-1, 4)[:, 1:4]*h s_bins, mu_bins = np.arange(5, 155, 5), np.arange(0, 1.05, 0.05) r = (s_bins[:-1] + s_bins[1:])/2 xi = tpcf(pos, s_bins, period=1100) xi_s_mu = s_mu_tpcf(pos, s_bins, mu_bins, period=1100) xi_0 = tpcf_multipole(xi_s_mu, mu_bins, order=0) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=2) fig, ax = plt.subplots() ax.plot(r, xi*np.square(r), 'o:', label='xi') ax.plot(r, xi_0*np.square(r), '+:', label='xi_0') ax.plot(r, xi_2*np.square(r), 'x-.', label='xi_2') fig.legend() fig.savefig('correlation.pdf')
dd_all = np.zeros((len(s_bins) - 1) * (len(mu_bins) - 1)) for xyz in ['xyz', 'yzx', 'zxy']: pos = return_xyz_formatted_array( x=mock[xyz[0]], y=mock[xyz[1]], z=mock[xyz[2]], velocity=mock['v'+xyz[2]], velocity_distortion_dimension='z', period=boxsize, redshift=redshift, cosmology=cosmology)[select] pos = pos.astype(float) dd_all += DDsmu(1, n_threads, s_bins, 1, len(mu_bins) - 1, pos[:, 0], pos[:, 1], pos[:, 2], periodic=True, boxsize=boxsize)[ 'npairs'] / 3.0 xi = xi_from_dd(dd_all, len(pos), boxsize, s_bins, mu_bins) n_jk = 10 for order in [0, 2, 4]: table['xi{}'.format(order)] = tpcf_multipole(xi, mu_bins, order=order) table['xi{}_jk'.format(order)] = np.zeros((len(table), n_jk**3)) for i_x, i_y, i_z in tqdm.tqdm(itertools.product( range(n_jk), range(n_jk), range(n_jk)), total=n_jk**3): select = ~((pos[:, 0] < boxsize / n_jk * i_x) | (pos[:, 0] >= boxsize / n_jk * (i_x + 1)) | (pos[:, 1] < boxsize / n_jk * i_y) | (pos[:, 1] >= boxsize / n_jk * (i_y + 1)) | (pos[:, 2] < boxsize / n_jk * i_z) | (pos[:, 2] >= boxsize / n_jk * (i_z + 1))) dd_auto = DDsmu(1, 4, s_bins, 1, len(mu_bins) - 1, pos[:, 0][select], pos[:, 1][select], pos[:, 2][select], periodic=True, boxsize=boxsize)['npairs'] dd_cross = DDsmu(0, 4, s_bins, 1, len(mu_bins) - 1, pos[:, 0][~select], pos[:, 1][~select],
def do_subcross_correlation(model, n_sub=3): # n defines number of subvolums ''' cross-correlation between 1/n_sub^3 of a box to the whole box n_sub^3 * 16 results are used for emperically estimating covariance matrix ''' # setting halocat properties to be compatibble with halotools sim_name = model.mock.header['SimName'] Lbox = model.mock.Lbox redshift = model.mock.redshift model_name = model.model_name gt = model.mock.galaxy_table ND1 = len(gt) s_bins = np.arange(0, 150 + step_s_bins, step_s_bins) s_bins_centre = (s_bins[:-1] + s_bins[1:]) / 2 mu_bins = np.arange(0, 1 + step_mu_bins, step_mu_bins) filedir = os.path.join(save_dir, sim_name, 'z{}-r{}'.format(redshift, model.r)) for i, j, k in itertools.product(range(n_sub), repeat=3): linind = i * n_sub**2 + j * n_sub + k # linearised index # read in pair counts file which has fine bins D1D2 = np.load( os.path.join( filedir, '{}-cross_{}-paircount-D1D2.npy'.format(model_name, linind))) # print('Re-binning {} of {} cross-correlation into ({}, {})...' # .format(linind+1, n_sub**3, s_bins.size-1, mu_bins.size-1)) # set up bins using specifications, re-bin the counts npairs_D1D2 = rebin(D1D2) # save re-binned pair ounts np.savetxt(os.path.join( filedir, '{}-cross_{}-paircount-D1D2-rebinned.txt'.format( model_name, linind)), npairs_D1D2, fmt=txtfmt) # read R1D2 counts and re-bin if use_analytic_randoms: npairs_R1D2 = np.loadtxt( os.path.join( filedir, '{}-cross_{}-paircount-R1D2.txt'.format( model_name, linind))) if not use_analytic_randoms or debug_mode: R1D2 = np.load( os.path.join( filedir, '{}-cross_{}-paircount-R1D2.npy'.format( model_name, linind))) npairs_R1D2 = rebin(R1D2) # save re-binned pair ounts np.savetxt(os.path.join( filedir, '{}-cross_{}-paircount-R1D2-rebinned.txt'.format( model_name, linind)), npairs_R1D2, fmt=txtfmt) # calculate cross-correlation from counts using dp estimator nD1 = ND1 / Lbox.prod() nR1 = nD1 xi_s_mu = dp_estimator(nD1, nR1, npairs_D1D2, npairs_R1D2) # print('Calculating cross-correlation for subvolume {} of {}...' # .format(linind+1, np.power(n_sub, 3))) xi_0 = tpcf_multipole(xi_s_mu, mu_bins, order=0) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=2) xi_0_txt = np.vstack([s_bins_centre, xi_0]).transpose() xi_2_txt = np.vstack([s_bins_centre, xi_2]).transpose() # save to text np.savetxt(os.path.join( filedir, '{}-cross_{}-xi_s_mu.txt'.format(model_name, linind)), xi_s_mu, fmt=txtfmt) np.savetxt(os.path.join( filedir, '{}-cross_{}-xi_0.txt'.format(model_name, linind)), xi_0_txt, fmt=txtfmt) np.savetxt(os.path.join( filedir, '{}-cross_{}-xi_2.txt'.format(model_name, linind)), xi_2_txt, fmt=txtfmt)
def do_auto_correlation(model): ''' read in counts, and generate xi_s_mu using bins specified using the PH (natural) estimator for periodic sim boxes instead of LS ''' # setting halocat properties to be compatibble with halotools sim_name = model.mock.header['SimName'] redshift = model.mock.redshift model_name = model.model_name s_bins = np.arange(0, 150 + step_s_bins, step_s_bins) s_bins_centre = (s_bins[:-1] + s_bins[1:]) / 2 mu_bins = np.arange(0, 1 + step_mu_bins, step_mu_bins) filedir = os.path.join(save_dir, sim_name, 'z{}-r{}'.format(redshift, model.r)) # read in pair counts file which has fine bins filepath1 = os.path.join(filedir, '{}-auto-paircount-DD.npy'.format(model_name)) DD = np.load(filepath1) ND = len(model.mock.galaxy_table) NR = ND # re-count pairs in new bins, re-bin the counts # print('Re-binning auto DD counts into ({}, {})...' # .format(s_bins.size-1, mu_bins.size-1)) npairs_DD = rebin(DD) # save re-binned pair ounts np.savetxt(os.path.join( filedir, '{}-auto-paircount-DD-rebinned.txt'.format(model_name)), npairs_DD, fmt=txtfmt) if use_analytic_randoms: filepath2 = os.path.join(filedir, '{}-auto-paircount-RR.txt'.format(model_name)) npairs_RR = np.loadtxt(filepath2) if not use_analytic_randoms or debug_mode: # rebin RR.npy counts RR = np.load( os.path.join(filedir, '{}-auto-paircount-RR.npy'.format(model_name))) npairs_RR = rebin(RR) # save re-binned pair ounts np.savetxt(os.path.join( filedir, '{}-auto-paircount-RR-rebinned.txt'.format(model_name)), npairs_RR, fmt=txtfmt) xi_s_mu = ph_estimator(ND, NR, npairs_DD, npairs_RR) # print('Calculating first two orders of auto-correlation multipoles...') xi_0 = tpcf_multipole(xi_s_mu, mu_bins, order=0) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=2) xi_0_txt = np.vstack([s_bins_centre, xi_0]).transpose() xi_2_txt = np.vstack([s_bins_centre, xi_2]).transpose() # save to text # print('Saving auto-correlation texts to: {}'.format(filedir)) np.savetxt(os.path.join(filedir, '{}-auto-xi_s_mu.txt'.format(model_name)), xi_s_mu, fmt=txtfmt) np.savetxt(os.path.join(filedir, '{}-auto-xi_0.txt'.format(model_name)), xi_0_txt, fmt=txtfmt) np.savetxt(os.path.join(filedir, '{}-auto-xi_2.txt'.format(model_name)), xi_2_txt, fmt=txtfmt) return xi_s_mu, xi_0_txt, xi_2_txt
def save_tpcfs(r, s, mu, pos, vel, los_direction, cosmology, boxsize, saveto, n_wedges=3): print('Computing real space correlation function...') tpcf_real = tpcf_tools.compute_real_tpcf(r, pos, vel, boxsize) print('Done!') print('Computing redshift space correlation function...') tpcf_s_mu = tpcf_tools.compute_tpcf_s_mu(s, mu, pos, vel, los_direction, cosmology, boxsize) print('Done!') print('Computing multipoles...') monopole = tpcf_multipole(tpcf_s_mu, mu, order=0) quadrupole = tpcf_multipole(tpcf_s_mu, mu, order=2) hexadecapole = tpcf_multipole(tpcf_s_mu, mu, order=4) print('Done!') # wedges for mu n_mu_bins_per_wedge = 50 mu_wedges = np.concatenate([ np.linspace(i * (1. / n_wedges), (i + 1) * 1. / n_wedges - 0.001, n_mu_bins_per_wedge) for i in range(n_wedges) ], ) mu_wedges[-1] = 1. print('Computing wedges...') tpcf_s_mu_wedges = tpcf_tools.compute_tpcf_s_mu(s, mu_wedges, pos, vel, los_direction, cosmology, boxsize) wedges = tpcf_tools.tpcf_wedges(tpcf_s_mu_wedges, mu_wedges, n_wedges=n_wedges) print('Done!') tpcf_dict = { 'real': { 'tpcf': tpcf_real, 'r': r, }, 'redsfhit': { 's': s, 'mu': mu, 'tpcf_s_mu': tpcf_s_mu, 'monopole': monopole, 'quadrupole': quadrupole, 'hexadecapole': hexadecapole, 'wedges': wedges } } with open(saveto + '.pickle', 'wb') as fp: pickle.dump(tpcf_dict, fp, protocol=pickle.HIGHEST_PROTOCOL)
def do_subcross_correlation(linind, phase, r, model_name, mode='smu-post-recon-std', use_shifted_randoms=True): sim_name = '{}_{:02}-{}'.format(sim_name_prefix, cosmology, phase) filedir = os.path.join(save_dir, sim_name, 'z{}-r{}'.format(redshift, r)) print('r = {}, x-correlation subvolume {}...'.format(r, linind)) DDnpy = np.load(os.path.join(filedir, '{}-cross_{}-paircount-DD-{}.npy' .format(model_name, linind, mode))) DD, _ = rebin_smu_counts(DDnpy, 'DD') # re-bin and re-weight np.savetxt(os.path.join(filedir, '{}-cross_{}-paircount-DD-{}-rebinned.txt' .format(model_name, linind, mode)), DD, fmt=txtfmt) if use_shifted_randoms: DRnpy = np.load(os.path.join(filedir, '{}-cross_{}-paircount-DR-{}-sr.npy' .format(model_name, linind, mode))) RDnpy = np.load(os.path.join(filedir, '{}-cross_{}-paircount-RD-{}-sr.npy' .format(model_name, linind, mode))) DR, _ = rebin_smu_counts(DRnpy, 'DR') RD, _ = rebin_smu_counts(RDnpy, 'RD') for txt, tag in zip([DR, RD], ['DR', 'RD']): np.savetxt(os.path.join( filedir, '{}-cross_{}-paircount-{}-{}-sr_rebinned.txt' .format(model_name, linind, tag, mode)), txt, fmt=txtfmt) RR_list = [] for n in range(random_multiplier): RRnpy = np.load(os.path.join( filedir, '{}-cross_{}_{}-paircount-RR-{}-sr.npy' .format(model_name, linind, n, mode))) RR, _ = rebin_smu_counts(RRnpy, 'RR') RR_list.append(RR) RR = np.mean(RR_list, axis=0) # shifted, could be co-adding zeros np.savetxt(os.path.join( filedir, '{}-cross_{}-paircount-RR-{}-sr_rebinned.txt' .format(model_name, linind, mode)), RR, fmt=txtfmt) # use analytic RR as denominator in LS estimator _, _, RR_ar = analytic_random(1, 1, 1, 1, s_bins, mu_bins, L**3) np.savetxt(os.path.join( filedir, '{}-cross_{}-paircount-RR-{}-ar.txt' .format(model_name, linind, mode)), RR_ar, fmt=txtfmt) assert DD.shape == DR.shape == RD.shape == RR.shape == RR_ar.shape xi_s_mu = ls_estimator(DD, DR, RD, RR, RR_ar) xi_0 = tpcf_multipole(xi_s_mu, mu_bins, order=0) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=2) xi_0_txt = np.vstack([s_bins_centre, xi_0]).T xi_2_txt = np.vstack([s_bins_centre, xi_2]).T for output, tag in zip([xi_s_mu, xi_0_txt, xi_2_txt], ['xi', 'xi_0', 'xi_2']): np.savetxt(os.path.join( filedir, '{}-cross_{}-{}-{}-sr.txt' .format(model_name, linind, tag, mode)), output, fmt=txtfmt) elif 'post' not in mode: # pre-recon covariance, use analytic randoms DR, RD, RR = analytic_random(1, 1, 1, 1, s_bins, mu_bins, L**3) for output, tag in zip([DR, RD, RR], ['DR', 'RD', 'RR']): np.savetxt(os.path.join( filedir, '{}-cross_{}-paircount-{}-{}-ar.txt' .format(model_name, linind, tag, mode)), output, fmt=txtfmt) # calculate cross-correlation from counts using ls estimator xi_s_mu = ls_estimator(DD, DR, RD, RR, RR) xi_0 = tpcf_multipole(xi_s_mu, mu_bins, order=0) xi_2 = tpcf_multipole(xi_s_mu, mu_bins, order=2) xi_0_txt = np.vstack([s_bins_centre, xi_0]).T xi_2_txt = np.vstack([s_bins_centre, xi_2]).T for output, tag in zip([xi_s_mu, xi_0_txt, xi_2_txt], ['xi', 'xi_0', 'xi_2']): np.savetxt(os.path.join( filedir, '{}-cross_{}-{}-{}-ar.txt' .format(model_name, linind, tag, mode)), output, fmt=txtfmt)