def lens_rand_jk(zmin, zmax, jk): lens_file = h5py.File('LRG_lum_jk.h5') lens_z = lens_file["redshift"][:] lens_ra = lens_file["RA"][:] lens_dec = lens_file["DEC"][:] coord_lens_jk = np.vstack([lens_ra, lens_dec]).T centers = np.loadtxt("centers.txt") labels_lens = kmeans_radec.find_nearest(coord_lens_jk, centers) label_mask = labels_lens == jk lens_mask = (lens_z > zmin) & (lens_z < zmax) & (~label_mask) lens_z = lens_z[lens_mask] lens_ra = lens_ra[lens_mask] lens_dec = lens_dec[lens_mask] nlens = len(lens_z) lens_input = np.vstack( [np.arange(nlens), lens_ra, lens_dec, lens_z, np.ones((nlens))]).T random_file = h5py.File("randoms_jk.h5") random_coord = random_file["random_jk"][:] random_ra = random_coord[:, 0] random_dec = random_coord[:, 1] centers = np.loadtxt("centers.txt") labels_random = kmeans_radec.find_nearest(random_coord, centers) label_mask = labels_random == jk random_ra = random_ra[~label_mask] random_dec = random_dec[~label_mask] random_file.close() nrands = len(random_ra) shuffle = np.random.choice(nrands, 10 * nlens) random_ra = random_ra[shuffle] random_dec = random_dec[shuffle] nrands = len(random_ra) print "nrands/nlens", nrands * 1. / nlens zdist = lens_z hist, bins = np.histogram(zdist, bins=100) bin_midpoints = bins[:-1] + np.diff(bins) / 2 cdf = np.cumsum(hist) cdf = cdf / float(cdf[-1]) values = np.random.rand(nrands) value_bins = np.searchsorted(cdf, values) random_z = bin_midpoints[value_bins] rands_input = np.stack([ np.arange(nrands), random_ra, random_dec, random_z, np.ones((nrands)) ]).T return lens_input, rands_input
def incomplete_shear_preprocess(zmin, zmax): shear = fitsio.read("source_v2.fits", columns=[ "ra_gal", "dec_gal", "observed_redshift_gal", "gamma1", "gamma2" ]) ra, dec, z = shear["ra_gal"], shear["dec_gal"], shear[ "observed_redshift_gal"] g1, g2 = shear["gamma1"], shear["gamma2"] dz = 0.05 * (1 + z) #redshift uncertainties z = np.random.normal(z, dz) #perturbed redshifts mask = (z > zmax + 0.1) z = z[mask] ra, dec = ra[mask], dec[mask] g1, g2 = g1[mask], g2[mask] print("length of the catalog after applying the cut", len(ra)) coord = np.vstack([ra, dec]).T centers = np.loadtxt("flagship_jk_centers_v2.txt") NJK = centers.shape[0] print("Segmentation begins!") gal_labels_jk = kmeans_radec.find_nearest(coord, centers) print("Done with assigning jacknife labels to galaxies") gals = { "RA": ra, "DEC": dec, "gamma1": g1, "gamma2": g2, "redshift": z, "JK_LABEL": gal_labels_jk } fits = FITS('data/incomplete_shear_zmax_' + str(round(zmax, 1)) + '.fits', 'rw') fits.write(gals, names=["RA", "DEC", "gamma1", "gamma2", "redshift", "JK_LABEL"]) fits.close() for jk in range(len(centers)): gal_jk_mask = gals["JK_LABEL"] != jk gal_jk = { "RA": ra[gal_jk_mask], "DEC": dec[gal_jk_mask], "gamma1": g1[gal_jk_mask], "gamma2": g2[gal_jk_mask], "redshift": z[gal_jk_mask] } fits = FITS( 'data/incomplete_shear_zmax_' + str(round(zmax, 1)) + '_jk_' + str(jk) + '.fits', 'rw') fits.write(gal_jk, names=["RA", "DEC", "gamma1", "gamma2", "redshift"]) fits.close() return None
def segment_data(RA, DEC): coord = np.vstack([RA, DEC]).T #centers of the jacknife regions centers = np.loadtxt("new_DR4_9bandnoAwr_uniform_centers.txt") #number of jacknife regions NJK = centers.shape[0] print("Segmentation begins!") labels_jk = kmeans_radec.find_nearest(coord, centers) print("Done with assigning jacknife labels to galaxies") #final_cat = {"RA": RA, # "DEC": DEC, # "JK_LABEL": labels_jk} return labels_jk
def find_kmeans_labels(points, centers): import numpy as np import kmeans_radec as kmrd import sys # Make sure ra, dec, and centers have correct shapes assert points.shape[0] == 2 or points.shape[ 1] == 2, "Points must have RA and Dec" if points.shape[0] == 2: points.T assert centers.shape[0] == 2 or centers.shape[ 1] == 2, "Centers must have RA and Dec" if centers.shape[0] == 2: centers.T # Run find_nearest return (kmrd.find_nearest(points, centers))
def incomplete_gal_preprocess(zmin, zmax): lens = fitsio.read("lens.fits", columns=["ra_gal", "dec_gal", "observed_redshift_gal"]) ra, dec, z = lens["ra_gal"], lens["dec_gal"], lens["observed_redshift_gal"] mask = (z > zmin) & (z < zmax) ra, dec, z = ra[mask], dec[mask], z[mask] random_choice = np.random.choice(np.arange(len(ra)), int(0.45 * len(ra)), replace=False) ra, dec, z = ra[random_choice], dec[random_choice], z[random_choice] print("length of the catalog after applying the cut", len(ra)) coord = np.vstack([ra, dec]).T centers = np.loadtxt("flagship_jk_centers_v2.txt") NJK = centers.shape[0] print("Segmentation begins!") gal_labels_jk = kmeans_radec.find_nearest(coord, centers) print("Done with assigning jacknife labels to galaxies") gals = {"RA": ra, "DEC": dec, "redshift": z, "JK_LABEL": gal_labels_jk} fits = FITS('data/incomplete_gal_zmax_' + str(round(zmax, 1)) + '.fits', 'rw') fits.write(gals, names=["RA", "DEC", "redshift", "JK_LABEL"]) fits.close() for jk in range(len(centers)): gal_jk_mask = gals["JK_LABEL"] != jk gal_jk = { "RA": ra[gal_jk_mask], "DEC": dec[gal_jk_mask], "redshift": z[gal_jk_mask] } fits = FITS( 'data/incomplete_gal_zmax_' + str(round(zmax, 1)) + '_jk_' + str(jk) + '.fits', 'rw') fits.write(gal_jk, names=["RA", "DEC", "redshift"]) fits.close() return None
import seaborn as sns from kmeans_radec import KMeans, kmeans_sample random_file = h5py.File("randoms_jk.h5") random_coord = random_file["random_jk"][:] random_ra = random_coord[:, 0] random_dec = random_coord[:, 1] random_file.close() random_file_jk = h5py.File("randoms_jk.h5") coord_rand_jk = random_file_jk["random_jk"][:] random_ra_jk = coord_rand_jk[:, 0] random_dec_jk = coord_rand_jk[:, 1] random_file_jk.close() centers = np.loadtxt("centers.txt") labels_random_jk = kmeans_radec.find_nearest(coord_rand_jk, centers) lens_file_jk = h5py.File("LRG_dense_jk.h5") dense_jk_mask = h5py.File("dense_jk_mask.h5") dense_mask = dense_jk_mask["mask"][:] dense_jk_mask.close() lens_z_jk = lens_file_jk["redshift"][dense_mask] lens_ra_jk = lens_file_jk["RA"][dense_mask] lens_dec_jk = lens_file_jk["DEC"][dense_mask] lens_file_jk.close() coord_lens_jk = np.vstack([lens_ra_jk, lens_dec_jk]).T centers = np.loadtxt("centers.txt") labels_lens_jk = kmeans_radec.find_nearest(coord_lens_jk, centers) random_ra = random_coord[:, 0] random_dec = random_coord[:, 1]
def get_center_labels(x, cent): return kmrd.find_nearest(x.T, cent)