table_s['w_sys'] = 1 table_s['pix'] = healpy.ang2pix(nside, table_s['ra'], table_s['dec'], lonlat=True) all_pixs = np.unique(table_s['pix']) use_pixs = np.zeros(0, dtype=np.int) for pix in all_pixs: near_pixs = healpy.pixelfunc.get_all_neighbours(nside, pix) if np.all(np.isin(near_pixs, all_pixs)): use_pixs = np.append(use_pixs, pix) table_s = table_s[np.isin(table_s['pix'], use_pixs)] table_s = add_maximum_lens_redshift(table_s, dz_min=0.15) if 'd_com' not in table_s.colnames: table_s['d_com'] = zebu.cosmo.comoving_transverse_distance( table_s['z']).to(u.Mpc).value if 'd_com_true' not in table_s.colnames: table_s['d_com_true'] = zebu.cosmo.comoving_transverse_distance( table_s['z_true']).to(u.Mpc).value for lens_bin in range(n_lens_bins): print('\t...lens bin {}'.format(lens_bin)) table_l = zebu.read_raw_data(stage, 'lens',
else: table_s['e_1'] /= 1 + table_s['m'] table_s['e_2'] /= 1 + table_s['m'] table_s['m'] = 0 if survey == 'hsc': table_s['e_1'] /= 2 * (1 - table_s['e_rms']**2) table_s['e_2'] /= 2 * (1 - table_s['e_rms']**2) table_s['e_rms'] = np.sqrt(0.5) table_c['w_sys'] = 1.0 if args.zspec: table_c['z'] = table_c['z_true'] table_s['z'] = table_s['z_true'] for table in [table_s, table_c]: add_maximum_lens_redshift(table, dz_min=0.2, z_err_factor=0) z_lens = np.linspace(zebu.lens_z_bins[lens_bin] - 1e-6, zebu.lens_z_bins[lens_bin + 1] + 1e-6, 1000) weight = np.zeros_like(z_lens) for i in range(len(z_lens)): sigma_crit = critical_surface_density(z_lens[i], table_c['z'], zebu.cosmo, comoving=True) weight[i] = np.sum(table_c['w'] * table_c['w_sys'] * sigma_crit**-2 * (z_lens[i] < table_c['z_l_max']))
fname.format(1)))[:, 0] + 0.025 table_n['n'] = np.vstack([ np.genfromtxt(os.path.join('kids', 'raw', fname.format(i + 1)))[:, 1] for i in range(5) ]).T z_s_bins = np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.2]) precompute_kwargs = {'table_n': table_n} stacking_kwargs = {'scalar_shear_response_correction': True} else: raise ValueError("Survey must be 'des', 'hsc' or 'kids'.") # %% add_maximum_lens_redshift(table_s, dz_min=0.1) if 'table_c' in precompute_kwargs.keys(): add_maximum_lens_redshift(table_c, dz_min=0.1) if args.survey.lower() == 'kids': table_s['z_l_max'] = 1000.0 precompute_kwargs.update({ 'n_jobs': multiprocessing.cpu_count(), 'comoving': True, 'cosmology': cosmology }) # %% rp_bins = np.logspace(np.log10(0.5), np.log10(50.), 11)
table = dsigma_table(kv, 'source', survey='KiDS', version='KV450', verbose=reg == 9) for key in table.colnames: table[key].unit = None table = Table(table) table_s = vstack([table_s, table]) table_s = table_s[table_s['z'] > 0.25 - 1e-6] table_s = table_s[table_s['z'] < 1.2 + 1e-6] table_s = add_maximum_lens_redshift(table_s, dz_min=0.105) table_s['m'] = kids.multiplicative_shear_bias(table_s['z'], version='KV450') table_c = table_s[np.random.randint(len(table_s), size=100000)]['z', 'z_l_max', 'w'] table_c['w_sys'] = np.ones(len(table_c)) table_c['z_true'] = np.zeros(len(table_c)) z_bins = [0.1, 0.3, 0.5, 0.7, 0.9, 1.2] for i in range(len(z_bins) - 1): nz = Table.read(os.path.join( 'kids', 'Nz_DIR_z{}t{}.asc'.format(z_bins[i], z_bins[i + 1])), format='ascii',
def prepare_source_random(srcs, rand, calib=None, photoz_cut='medium', dz_min=0.1, cosmology=None, H0=70.0, Om0=0.3, comoving=False, n_jobs=4, r_min=0.15, r_max=11, n_bins=11, output=None, n_random=500000, verbose=True): """Prepare the lensing source, calibration, and random catalogs. Also precompute the DeltaSigma profiles for randoms if necessary, and define the cosmology model and the radial bins used for lensing profiles. """ # Define cosmology if cosmology is None: cosmology = FlatLambdaCDM(H0=H0, Om0=Om0) # Define radial bins rp_bins = np.logspace(np.log10(r_min), np.log10(r_max), n_bins) # Photo-z quality cuts if verbose: print("# Use the {:s} photo-z quality cut".format(photoz_cut)) if photoz_cut == 'basic': photoz_mask = (srcs['frankenz_model_llmin'] < 6.) elif photoz_cut == 'medium': photoz_mask = (srcs['frankenz_model_llmin'] < 6.) & (srcs['frankenz_photoz_risk_best'] < 0.25) elif photoz_cut == 'strict': photoz_mask = (srcs['frankenz_model_llmin'] < 6.) & (srcs['frankenz_photoz_risk_best'] < 0.15) else: raise Exception( "# Wrong photo-z quality cut type: [basic/medium/strict]") # Prepare the source catalog if verbose: print("\n# Prepare the lensing source catalog") srcs_use = helpers.dsigma_table(srcs[photoz_mask], 'source', survey='hsc', version='PDR2', field='field', z='frankenz_photoz_best', z_low='frankenz_photoz_err68_min', z_upp='frankenz_photoz_err68_max') # Add maximum usable redshift for lenses srcs_use = add_maximum_lens_redshift(srcs_use, dz_min=dz_min, z_err_factor=0, apply_z_low=True) # Prepare the calibration catalog if necessary if calib is not None: if verbose: print("\n# Prepare the lensing calibration catalog") # Photo-z quality cuts if photoz_cut == 'basic': photoz_mask = (calib['frankenz_model_llmin'] < 6.) elif photoz_cut == 'medium': photoz_mask = (calib['frankenz_model_llmin'] < 6.) & (calib['frankenz_photoz_risk_best'] < 0.25) elif photoz_cut == 'strict': photoz_mask = (calib['frankenz_model_llmin'] < 6.) & (calib['frankenz_photoz_risk_best'] < 0.15) else: raise Exception( "# Wrong photo-z quality cut type: [basic/medium/strict]") # Prepare the calibration catalog calib_use = helpers.dsigma_table(calib[photoz_mask], 'calibration', z='frankenz_photoz_best', z_true='z_true', z_low='frankenz_photoz_err68_min', w='w_source', w_sys='somw_cosmos_samplevaraince') # Add maximum usable redshift for lenses calib_use = add_maximum_lens_redshift(calib_use, dz_min=dz_min, z_err_factor=0, apply_z_low=True) else: calib_use = None # Prepare the random catalogs if verbose: print("\n# Prepare the random object catalog") rand_use = prepare_random_catalog(rand, size=n_random) # Pre-compute the DeltaSigma profiles for random objects if verbose: print("\n# Pre-compute the DeltaSigma profiles for random objects") rand_pre = add_precompute_results(rand_use, srcs_use, rp_bins, table_c=calib_use, cosmology=cosmology, comoving=comoving, n_jobs=n_jobs) # Remove the ones with no useful lensing information rand_pre['n_s_tot'] = np.sum(rand_pre['sum 1'], axis=1) rand_pre = rand_pre[rand_pre['n_s_tot'] > 0] if output is not None: srcs_use.write(output, path='source', format='hdf5') calib_use.write(output, path='calib', format='hdf5', append=True) rand_pre.write(output, path='random', format='hdf5', append=True) return else: return { 'cosmology': cosmology, 'rp_bins': rp_bins, 'source': srcs_use, 'calib': calib_use, 'random': rand_pre }