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 test_uniform_sphere(): np.random.seed(42) # check number of points in 3 axis-aligned cones is approximately the same ra, dec = uniform_sphere((-180, 180), (-90, 90), 10000) x, y, z = ra_dec_to_xyz(ra, dec) assert_allclose(x ** 2 + y ** 2 + z ** 2, np.ones_like(x)) in_x_cone = (y**2 + z**2 < 0.25).mean() in_y_cone = (x**2 + z**2 < 0.25).mean() in_z_cone = (x**2 + y**2 < 0.25).mean() # with prop > 0.999999 should not differ for more than 5 standard deviations assert_allclose(in_x_cone, in_y_cone, atol=5e-2) assert_allclose(in_x_cone, in_z_cone, atol=5e-2) assert_allclose(in_y_cone, in_z_cone, atol=5e-2)
ndataR = 50000 fits = fitsio.FITS(fn) fits_rand = fitsio.FITS(fn_rand) w = fits[1].where("Z > 0.43 && Z < 0.7") sel = np.arange(len(w)) np.random.shuffle(sel) data = fits[1][w][sel[:min(ndata, len(w))]] w = fits_rand[1].where("Z > 0.43 && Z < 0.7") sel = np.arange(len(w)) np.random.shuffle(sel) data_R = fits_rand[1][w][sel[:min(ndataR, len(w))]] xyz = cor.ra_dec_to_xyz(data['RA'], data['DEC'])* proper_distance(data['Z']) xyz_rand = cor.ra_dec_to_xyz(data_R['RA'], data_R['DEC'])* proper_distance(data_R['Z']) #plot_3d(xyz, xyz_rand) #plt.show() two_point_corr(xyz, xyz_rand) plt.xlabel('z') plt.ylabel(r"$xi$") plt.show() ##============================================================##
def two_point_angular_window(coords_D, coords_R, bins, D_idx=None, R_idx=None, random_state=None): """Two-point correlation function Parameters ---------- data_D: data ra, dec in deg, shape = [2, n_samples] data_R: random ra, dec in deg, shape = [2, n_samples] D_idx: idx of data in field, None if all in field R_idx: idx of random in field, None if all in field random_state : integer, np.random.RandomState, or None specify the random state to use for generating background Returns ------- corr : ndarray the estimate of the correlation function within each bin shape = Nbins corr_in: """ rng = check_random_state(random_state) coords_D, coords_R = np.asanyarray(coords_D), np.asanyarray(coords_R) if D_idx is None: D_idx = np.arange(coords_D.shape[1], dtype=int) if R_idx is None: R_idx = np.arange(coords_R.shape[1], dtype=int) if bins.ndim != 1: raise ValueError("bins must be a 1D array") data = np.asarray(ra_dec_to_xyz(coords_D[0], coords_D[1]), order='F').T data_R = np.asarray(ra_dec_to_xyz(coords_R[0], coords_R[1]), order='F').T bins = angular_dist_to_euclidean_dist(bins) Nbins = len(bins) - 1 factor = len(data_R) * 1. / len(data) factor_in = len(R_idx) * 1. / len(D_idx) BT_D = BallTree(data) BT_R = BallTree(data_R) counts_DD = np.zeros(Nbins + 1, dtype=int) counts_RR = np.zeros(Nbins + 1, dtype=int) counts_DD_in = np.zeros(Nbins + 1, dtype=int) counts_RR_in = np.zeros(Nbins + 1, dtype=int) for i in range(Nbins + 1): count_listD = BT_D.query_radius(data, bins[i]) count_listR = BT_R.query_radius(data_R, bins[i]) countD = np.sum([len(count) for count in count_listD]) countR = np.sum([len(count) for count in count_listR]) countD_in = np.sum([len(count) for count in count_listD[D_idx]]) countR_in = np.sum([len(count) for count in count_listR[R_idx]]) counts_DD[i], counts_RR[i] = countD, countR counts_DD_in[i], counts_RR_in[i] = countD_in, countR_in DD = np.diff(counts_DD) RR = np.diff(counts_RR) DD_in = np.diff(counts_DD_in) RR_in = np.diff(counts_RR_in) # check for zero in the denominator RR_zero = np.where(RR == 0)[0] RR_in_zero = np.where(RR_in == 0)[0] RR[RR_zero] = 1 RR_in[RR_in_zero] = 1 corr = factor**2 * DD / RR - 1 corr_in = factor_in**2 * DD_in / RR_in - 1 corr[RR_zero] = np.nan corr_in[RR_in_zero] = np.nan return corr, corr_in
from mpl_toolkits.mplot3d import Axes3D from astropy.cosmology import FlatLambdaCDM cosmo = FlatLambdaCDM(H0=70, Om0=0.274, Ob0=0.0457) fn = '~/Desktop/CIB/data/RIFSCz_short.v2.fits' fits = fitsio.FITS(fn) w = fits[1].where("z>0") sel = np.arange(len(w)) np.random.shuffle(sel) data = fits[1][w]#[sel[:10000]] ra = np.array(data['RA_recom']) dec = np.array(data['DEC_recom']) z = np.array(data['z']) xyz = cor.ra_dec_to_xyz(ra, dec)*cosmo.comoving_distance(z) lim = (-3000, 3000) def plot_3d(xyz): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xyz[0,:], xyz[1,:], xyz[2,:], c = 'r', marker='.', alpha=0.3) ax.set_xlim3d(*lim) ax.set_ylim3d(*lim) ax.set_zlim3d(*lim) def plot_z_histogram(z): plt.hist(z, np.linspace(0, 0.6, 100), alpha = 0.5) plot_3d(xyz)
import fitsio import numpy as np import astroML.correlation as cor import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fn = '../data/specObj-dr12.fits' fits = fitsio.FITS(fn) w = fits[1].where("FIRSTRELEASE== 'dr12' && PLUG_RA > 135 && PLUG_RA < 225 && Z > 0.43 && Z < 0.7") sel = np.arange(len(w)) np.random.shuffle(sel) data = fits[1][w][sel[:10000]] xyz = cor.ra_dec_to_xyz(data['PLUG_RA'], data['PLUG_DEC'])* data['Z'] 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 plot_3d(xyz): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xyz[0,:], xyz[1,:], xyz[2,:]) plot_3d(xyz) plt.show() two_point_corr(xyz)
fout = open(fn_out, 'w') fout_rand = open(fn_rand_out, 'w') wd = fits[1].where("Z > 0.43 && Z < 0.7 && WEIGHT_SYSTOT >0") sel = np.arange(len(wd)) #np.random.shuffle(sel) data = fits[1][wd]##[sel[:min(ndata, len(wd))]] wr = fits_rand[1].where(" Z > 0.43 && Z < 0.7") sel = np.arange(len(wr)) np.random.shuffle(sel) data_R = fits_rand[1][wr]##[sel[:min(ndataR, len(wr))]] xyz = cor.ra_dec_to_xyz(data['RA'], data['DEC'])* cosmo.comoving_distance(data['Z']).value xyz_rand = cor.ra_dec_to_xyz(data_R['RA'], data_R['DEC'])* cosmo.comoving_distance(data_R['Z']).value for i in xrange(len(wd)): fout.write("%.10f %.10f %.10f \n"%(xyz[0,i], xyz[1,i], xyz[2,i])) for i in xrange(len(wr)): fout_rand.write("%.10f %.10f %.10f \n"%(xyz_rand[0,i], xyz_rand[1,i], xyz_rand[2,i])) fout.flush() fout_rand.flush() ##============================================================## ## Results ## ##============================================================##
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