def ComputeCorr( Bodies, direct='', nbins=250 ): #This Computes the correlation function for the bodies in the array Bodies. X = np.zeros((len(Bodies), 3)) for i in range(len(Bodies)): X[i] = Bodies[i].position bins = np.logspace(-5, 2, int(nbins)) corr = two_point( X, bins ) + 1 #This is where the two point correlation is computed and stored in an array named corr. DATA = np.array( [bins[1:], corr] ).T # This array contains the correlation function data as (r,corr) points. DATA = DATA[~np.any( np.isnan(DATA), axis=1)] # Removes nan's because curve_fit doesn't handle them well. np.savetxt(direct + 'Corr.txt', DATA, fmt='%f') # a = np.loadtxt('Corr.txt') # To load the data form text return DATA
def tp_corr(out_count, rbin_N): path_dir = parent_dir + 'output/' if os.path.isfile(path_dir + 'particle_' + str(out_count) + '.npy'): out_data = np.load(path_dir + 'particle_' + str(out_count) + '.npy') out_info = np.load(path_dir + 'info_' + str(out_count) + '.npy', allow_pickle=True).item() rbin = np.logspace(0.5, 2.5, num=rbin_N) posn = out_data[:, 1:4] corr = two_point(posn, rbin) print(np.shape(corr)) print(corr) rbin = rbin[:-1] print(np.shape(rbin)) plt.figure() plt.xscale('log') plt.yscale('log') plt.plot(rbin, corr) plt.title('With expansion: 20000 particles') plt.xlabel(r'r (in code units)') plt.ylabel(r'$\xi$(r)') plt.savefig(parent_dir + 'two_point_corr_' + str(out_count) + '.png') plt.close()
def ac_astroML(self): '''from two_point_angular() in astroML/correlation.py''' from astroML.correlation import two_point, ra_dec_to_xyz, angular_dist_to_euclidean_dist # 3d project data = np.asarray(ra_dec_to_xyz(self.gal_ra, self.gal_dec), order='F').T data_R = np.asarray(ra_dec_to_xyz(self.ran_ra, self.ran_dec), order='F').T # convert spherical bins to cartesian bins bins = 10**np.linspace(np.log10(1. / 60.), np.log10(6), 16) bins_transform = angular_dist_to_euclidean_dist(bins) w = two_point(data, bins_transform, method='landy-szalay', data_R=data_R) bin_centers = 0.5 * (bins[1:] + bins[:-1]) return bin_centers, w
def find_corr(ponds,random_fraction,bins): random_points = np.random.rand(np.shape(ponds)[0],np.shape(ponds)[1]) < random_fraction ponds_coordinates = np.array(np.nonzero(ponds*random_points)).T corr = two_point(ponds_coordinates, bins) loc = corr == corr C = corr[loc] d = bins[1:][loc] C = np.hstack([1.,C,0.]) d = np.hstack([0.,d,np.inf]) Cd_interp = interp1d(C,d,kind = 'linear') l0 = Cd_interp(np.exp(-1)) return_corr = np.hstack([1.,corr,0.]) return_dist = np.hstack([0.,bins[1:],np.inf]) return return_corr,return_dist,l0
N = 500 offset = 60.0 noise = 3.0 BAO = 20.0 for i in xrange(100): N1 = np.random.randint(N/10.0, N) N2 = np.random.randint(N/10.0, N) r1 = (np.random.random(N1)-0.5)*noise* np.random.random() r2 = BAO + (np.random.random(N2)-0.5)*noise* np.random.random() x_off = 2*offset*(np.random.random() - 0.5) y_off = 2*offset*(np.random.random() - 0.5) z_off = 2*offset*(np.random.random() - 0.5) theta1 = np.random.random(N1) * np.pi phi1 = np.random.random(N1) * 2 * np.pi c1 = np.vstack( (r1*np.sin(theta1)*np.cos(phi1)+x_off, r1*np.sin(theta1)*np.sin(phi1)+y_off, r1*np.cos(theta1)+z_off )) theta2 = np.random.random(N2) * np.pi phi2 = np.random.random(N2) * 2 * np.pi c2 = np.vstack( (r2*np.sin(theta2)*np.cos(phi2)+x_off, r2*np.sin(theta2)*np.sin(phi2)+y_off, r2*np.cos(theta2)+z_off )) c = np.hstack( (c,c1,c2) ) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') np.random.shuffle(c) ax.scatter(c[0][:], c[1][:], c[2][:]) plt.show() cor = two_point(c.T, np.linspace(int(BAO)/10.0, int(BAO)*2, int(BAO)*2), method='landy-szalay') plt.plot(cor) plt.show()
def two_point_corr(xyz, xyz_rand): bins = np.linspace(bin_min, bin_max, nbins) corr = cor.two_point(xyz.T, bins, method='landy-szalay', data_R = xyz_rand.T) print corr s_ls = bins[:-1]+(bin_max-bin_min)/(2*nbins) plt.plot(s_ls, s_ls**2 * corr)
def two_point_corr(xyz): bins = np.linspace(0, 1, 51) corr = cor.two_point(xyz.T, bins, method='landy-szalay') print corr plt.plot(corr)
def new_bootstrap_two_point_angular(ra_G,dec_G, bins, method='standard',\ ra_R=None,dec_R=None, Nbootstraps=10,\ random_state=None): """Angular two-point correlation function A separate function is needed because angular distances are not euclidean, and random sampling needs to take into account the spherical volume element. Parameters ---------- ra_G, dec_G : array_like Observed galaxy ra,dec bins : array_like bins within which to compute the 2-point correlation. shape = Nbins + 1 method : string "standard" or "landy-szalay". ra_R, dec_R : array_like Random catlogue of ra,dec Nbootstraps : int number of bootstrap resamples Returns ------- corr : ndarray the estimate of the correlation function within each bin shape = Nbins dcorr : ndarray error estimate on dcorr (sample standard deviation of bootstrap resamples) bootstraps : ndarray The full sample of bootstraps used to compute corr and dcorr """ data = np.asarray(ra_dec_to_xyz(ra_G, dec_G), order='F').T n_samples, n_features = data.shape if random_state is None: rng= np.random.RandomState() if ra_R is None or dec_R is None: print('Creating random sample') data_R = data.copy() for i in range(n_features - 1): rng.shuffle(data_R[:, i]) else: data_R = np.asarray(ra_dec_to_xyz(ra_R, dec_R), order='F').T if (data_R.ndim != 2) or (data_R.shape[-1] != n_features): raise ValueError('data_R must have same n_features as data') bins = np.asarray(bins) Nbins = len(bins) - 1 if method not in ['standard', 'landy-szalay']: raise ValueError("method must be 'standard' or 'landy-szalay'") if bins.ndim != 1: raise ValueError("bins must be a 1D array") if data.ndim == 1: data = data[:, np.newaxis] elif data.ndim != 2: raise ValueError("data should be 1D or 2D") # convert spherical bins to cartesian bins bins_transform = angular_dist_to_euclidean_dist(bins) bootstraps = [] # Bootstrap sample data_R, hold data fixed #R_samples, R_features = data_R.shape #for i in range(Nbootstraps): # # Sample with replacement # #inds= rng.randint(R_samples, size=R_samples) #np.random.randint # ind = rng.randint(0, R_samples, R_samples) # new_R= data_R[ind,:] # print('WARNING: look in code, is new_R shape correct?') # bootstraps.append(two_point(data, bins_transform, method=method, # data_R=new_R, random_state=rng)) # Now, bootstrap sample data, hold data_R for i in range(Nbootstraps): print('Computing bootstrap: %d' % (i+1,)) ind = rng.randint(0, n_samples, n_samples) new_G= data[ind,:] bootstraps.append(two_point(new_G, bins_transform, method=method, data_R=data_R, random_state=rng)) bootstraps = np.asarray(bootstraps) corr = np.mean(bootstraps, 0) corr_err = np.std(bootstraps, 0, ddof=1) return corr, corr_err, bootstraps