def pdf_func(data_arr, bins_arr,): N_pix, edg_f = binned(data_arr, data_arr, statistic = 'count', bins = bins_arr)[:2] pdf_pix = (N_pix / np.sum(N_pix) ) / (edg_f[1] - edg_f[0]) pdf_err = ( np.sqrt(N_pix) / np.sum(N_pix) ) / (edg_f[1] - edg_f[0]) f_cen = 0.5 * ( edg_f[1:] + edg_f[:-1]) id_zero = N_pix < 1. pdf_arr = pdf_pix[ id_zero == False] err_arr = pdf_err[ id_zero == False] pdf_x = f_cen[ id_zero == False] return pdf_arr, err_arr, pdf_x
samp_flux = samp_flux[id_nn == False] / pixel**2 bin_wide_samp = 1e-2 bin_edgs_samp = np.arange( samp_flux.min(), samp_flux.max(), bin_wide_samp, ) sam_tmp_mean_f.append(np.mean(samp_flux)) sam_tmp_medi_f.append(np.median(samp_flux)) sam_tmp_err.append(np.std(samp_flux)) # pdf of sample img flux N_pix, edg_f = binned(samp_flux, samp_flux, statistic='count', bins=bin_edgs_samp)[:2] pdf_pix = (N_pix / np.sum(N_pix)) / (edg_f[1] - edg_f[0]) pdf_err = (np.sqrt(N_pix) / np.sum(N_pix)) / (edg_f[1] - edg_f[0]) f_cen = 0.5 * (edg_f[1:] + edg_f[:-1]) mean_f = np.mean(samp_flux) medi_f = np.median(samp_flux) try: idu = N_pix != 0. popt, pcov = optimize.curve_fit( gau_func, f_cen[idu],
bin_pix_cont = bin_pix_cont[id_nan == False] #bin_pix_cont = bin_pix_cont.astype( np.int32 ) lis_f = [] for dd in range( len(bin_flux) ): rep_arr = np.ones( bin_pix_cont[dd], dtype = np.float32) * bin_flux[dd] lis_f.append( rep_arr) F_arr = np.hstack(lis_f) / pixel**2 mean_f = np.mean( F_arr ) medi_f = np.median( F_arr ) bin_wide = 5e-4 bin_edgs = np.arange( F_arr.min(), F_arr.max(), bin_wide) N_pix, edg_f = binned(F_arr, F_arr, statistic = 'count', bins = bin_edgs)[:2] pdf_pix = (N_pix / np.sum(N_pix) ) / (edg_f[1] - edg_f[0]) pdf_err = ( np.sqrt(N_pix) / np.sum(N_pix) ) / (edg_f[1] - edg_f[0]) f_cen = 0.5 * ( edg_f[1:] + edg_f[:-1]) abs_min_f = np.abs( F_arr.min() ) try: idu = N_pix != 0. ''' popt, pcov = optimize.curve_fit(gau_func, f_cen[idu], pdf_pix[idu], p0 = [mean_f, np.std(F_arr)], sigma = pdf_err[idu],) e_mu, e_chi = popt[0], popt[1] fit_line = gau_func(f_cen, e_mu, e_chi) ''' eta_0, lamda_0 = 3, -0.5
lc0 = np.array([np.min(ll) for ll in tot_flux]) lc1 = np.array([np.max(ll) for ll in tot_flux]) bins_2 = np.linspace(lc0.min(), lc1.max(), 60) chi_test = [] nu_on_chi = [] for kk in range(len(k_r)): ## parameter fit tt_data = mean_percen[kk] bin_di = bins_0 mu = np.mean(tt_data) sqr_var = np.std(tt_data) pix_n, edgs = binned(tt_data, tt_data, statistic='count', bins=bin_di)[:2] pdf_pix = (pix_n / np.sum(pix_n)) / (edgs[1] - edgs[0]) pdf_err = (np.sqrt(pix_n) / np.sum(pix_n)) / (edgs[1] - edgs[0]) x_cen = 0.5 * (edgs[1:] + edgs[:-1]) idu = pix_n != 0. use_obs = pix_n[idu] use_err = np.sqrt(use_obs) use_x = x_cen[idu] popt, pcov = curve_fit( gau_func, use_x, pdf_pix[idu], p0=[mu, sqr_var], sigma=pdf_err[idu], )
img_1 = data_1[0].data idnn = np.isnan(img_1) sub_flux_1 = img_1[idnn == False] pix_flux_1sigm.append(sub_flux_0) pix_flux_2sigm.append(sub_flux_1) ## stack test stack_1sigm.append(img_0) stack_2sigm.append(img_1) eta_0 = len(sub_flux_0) / (1489 * 2048) eta_1 = len(sub_flux_1) / (1489 * 2048) pix_remain_1sigm.append(eta_0) pix_remain_2sigm.append(eta_1) pock_n, edgs = binned(sub_flux_0, sub_flux_0, statistic='count', bins=60)[:2] id_zero = pock_n == 0. pock_n = pock_n[id_zero == False] sub_pdf0 = (pock_n / np.sum(pock_n)) / (edgs[1] - edgs[0]) sub_x0 = 0.5 * (edgs[1:] + edgs[:-1]) sub_x0 = sub_x0[id_zero == False] popt, pcov = curve_fit(gau_func, sub_x0, sub_pdf0, p0=[np.mean(sub_flux_0), np.std(sub_flux_0)]) e_mu0, e_sigm0 = popt[0], popt[1] fit_l0 = gau_func( sub_x0, e_mu0, e_sigm0,
def hist_analysis_func( cat_file, img_file, mask_img_file, band, star_cat, gal_cat, out_imgs, pix_scale, N_step, ): """ cat_file : cluster catalog (.csv file, including path information) img_file : imgs have no mask mask_img_file : imgs have been masked band : band (ie. 'g', 'r', 'i',), str type star_cat : star catalog. (.txt or .csv file, including path information) gal_cat : source catalog in image region (builded by Source Extractor, .cat file, including path information) out_imgs : ouput of figs,(.png or .pdf files, including path information) pix_scale : pixel size of CCD, in unit of 'arcsec' N_step : grid size for img division. """ ### cat and select dat = pds.read_csv(cat_file) ra, dec, z = np.array(dat.ra), np.array(dat.dec), np.array(dat.z) clus_x, clus_y = np.array(dat.bcg_x), np.array(dat.bcg_y) Da = Test_model.angular_diameter_distance(z).value N_samp = len(z) pixel = pix_scale * 1. Angu_l = (1 / Da) * rad2asec R_pix = Angu_l / pixel cen_sigm, cen_mu = [], [] img_mu, img_sigm = [], [] ### overview on img structure for kk in range(N_samp): ra_g, dec_g, z_g = ra[kk], dec[kk], z[kk] # original img data = fits.open(img_file % (band, ra_g, dec_g, z_g), ) img = data[0].data head = data[0].header wcs_lis = awc.WCS(head) xn, yn = clus_x[kk], clus_y[kk] # mask imgs res_data = fits.open(mask_img_file % (band, ra_g, dec_g, z_g), ) remain_img = res_data[0].data # grid img (for selecting flare, saturated region...) block_m, block_pix, block_Var, block_S0, x_edgs, y_edgs = cc_grid_img( remain_img, N_step, N_step) idzo = block_pix < 1. pix_eta = block_pix / block_S0 idnn = np.isnan(pix_eta) pix_eta[idnn] = 0. idnul = pix_eta < 5e-2 block_m[idnul] = 0. img_mu.append(np.nanmean(block_m[idnul == False])) img_sigm.append(np.nanstd(block_m[idnul == False])) idnn = np.isnan(remain_img) bin_flux = remain_img[idnn == False] bin_di = np.linspace(bin_flux.min(), bin_flux.max(), 51) / pixel**2 pix_n, edgs = binned(bin_flux / pixel**2, bin_flux / pixel**2, statistic='count', bins=bin_di)[:2] pdf_pix = (pix_n / np.sum(pix_n)) / (edgs[1] - edgs[0]) pdf_err = (np.sqrt(pix_n) / np.sum(pix_n)) / (edgs[1] - edgs[0]) x_cen = 0.5 * (edgs[1:] + edgs[:-1]) idu = pix_n != 0. use_obs = pix_n[idu] use_err = np.sqrt(use_obs) use_x = x_cen[idu] popt, pcov = curve_fit( gau_func, use_x, pdf_pix[idu], p0=[np.mean(bin_flux / pixel**2), np.std(bin_flux / pixel**2)], sigma=pdf_err[idu], ) e_mu, e_chi = popt[0], popt[1] fit_line = gau_func(x_cen, e_mu, e_chi) ### applied the mask region star_file = star_cat % (z_g, ra_g, dec_g) galax_file = gal_cat % (ra_g, dec_g, z_g) tot_Numb, tot_cx, tot_cy, tot_a, tot_b, tot_theta = get_cat( star_file, galax_file, pixel, wcs_lis, ) sc_x, sc_y = tot_cx / (img.shape[1] / block_m.shape[1]), tot_cy / ( img.shape[0] / block_m.shape[0]) sc_a, sc_b = tot_a * (block_m.shape[1] / img.shape[1]), tot_b * ( block_m.shape[0] / img.shape[0]) sc_x, sc_y = sc_x - 0.5, sc_y - 0.5 Rpp = R_pix[kk] fig = plt.figure(figsize=(13.12, 9.84)) ax0 = fig.add_axes([0.05, 0.55, 0.40, 0.45]) ax1 = fig.add_axes([0.55, 0.55, 0.40, 0.45]) ax2 = fig.add_axes([0.05, 0.05, 0.40, 0.45]) ax3 = fig.add_axes([0.55, 0.05, 0.40, 0.40]) ax0.set_title('img ra%.3f dec%.3f z%.3f' % (ra_g, dec_g, z_g), ) tf = ax0.imshow(img / pixel**2, cmap='Greys', origin='lower', vmin=1e-4, vmax=1e0, norm=mpl.colors.LogNorm()) clust = Circle( xy=(xn, yn), radius=Rpp, fill=False, ec='b', ls='-', linewidth=1, alpha=0.50, ) ax0.add_patch(clust) cb = plt.colorbar( tf, ax=ax0, fraction=0.035, pad=0.01, label='SB [nanomaggies / $arcsec^2$]', ) ax1.set_title('after masking') tg = ax1.imshow( remain_img / pixel**2, origin='lower', cmap='seismic', vmin=-4e-1, vmax=4e-1, ) cb = plt.colorbar( tg, ax=ax1, fraction=0.035, pad=0.01, label='SB [nanomaggies / $arcsec^2$]', ) cb.formatter.set_powerlimits((0, 0)) ### image patch_mean case ax2.set_title('2D hist of sub-patch mean value') th = ax2.imshow( block_m / pixel**2, origin='lower', cmap='seismic', vmin=-4e-2, vmax=4e-2, ) cb = plt.colorbar( th, ax=ax2, fraction=0.034, pad=0.01, label='SB [nanomaggies / $arcsec^2$]', ) cb.formatter.set_powerlimits((0, 0)) for mm in range(tot_Numb): ellips = Ellipse( xy=(sc_x[mm], sc_y[mm]), width=sc_a[mm], height=sc_b[mm], angle=tot_theta[mm], fill=True, fc='w', ec='w', ls='-', linewidth=0.75, ) ax2.add_patch(ellips) for mm in range(block_m.shape[1]): for nn in range(block_m.shape[0]): ax2.text(mm, nn, s='%.3f' % pix_eta[nn, mm], ha='center', va='center', color='g', fontsize=8, alpha=0.5) ax2.set_xlabel('effective pixel ratio shown in green text') ax3.set_title('pixel SB PDF [after masking]') ax3.hist( bin_flux / pixel**2, bins=bin_di, density=True, color='b', alpha=0.5, ) ax3.plot( x_cen, fit_line, color='r', alpha=0.5, label='Gaussian \n $\\mu=%.4f$ \n $\\sigma=%.4f$' % (e_mu, e_chi), ) ax3.axvline( x=0, ls='-', color='k', alpha=0.5, ) ax3.axvline(x=e_mu - e_chi, ls='--', color='k', alpha=0.5, label='1 $\\sigma$') ax3.axvline( x=e_mu + e_chi, ls='--', color='k', alpha=0.5, ) ax3.legend( loc=1, frameon=False, ) ax3.set_xlabel('pixel SB [nanomaggies / $arcsec^2$]') ax3.set_ylabel('PDF') plt.savefig(out_imgs % (band, ra_g, dec_g, z_g), dpi=300) plt.close() raise return
def binned_img_flux(): N_bin = 30 bin_side = np.linspace(-1e-1, 1e-1, N_bin + 1) for kk in range(rank, rank + 1): with h5py.File(load + 'random_cat/rand_%s_band_catalog.h5' % (band[kk]), 'r') as f: tmp_array = np.array(f['a']) ra, dec, z, rich = np.array(tmp_array[0]), np.array(tmp_array[1]), np.array(tmp_array[2]), np.array(tmp_array[3]) zN = len(z) bin_flux_mean = np.zeros((zN, N_bin), dtype = np.float) bin_flux_median = np.zeros((zN, N_bin), dtype = np.float) bin_num = np.zeros((zN, N_bin), dtype = np.float) for jj in range(zN): ra_g, dec_g, z_g = ra[jj], dec[jj], z[jj] data_A = fits.open(load + 'random_cat/edge_cut_img/rand_pont_Edg_cut-%s-ra%.3f-dec%.3f-redshift%.3f.fits' % (band[kk], ra_g, dec_g, z_g) ) img_A = data_A[0].data id_nan = np.isnan(img_A) flux_array = img_A[id_nan == False] value_mean = binned(flux_array, flux_array, statistic='mean', bins = bin_side)[0] value_median = binned(flux_array, flux_array, statistic='median', bins = bin_side)[0] value_number = binned(flux_array, flux_array, statistic='count', bins = bin_side)[0] bin_flux_mean[jj,:] = value_mean bin_flux_median[jj,:] = value_median bin_num[jj,:] = value_number / np.nansum(value_number) flux_mean = np.nanmean(bin_flux_mean, axis = 0) / pixel**2 flux_median = np.nanmean(bin_flux_median, axis = 0) / pixel**2 num_mean = np.nanmean(bin_num, axis = 0) dmp_array = np.array([flux_mean, num_mean]) with h5py.File(load + 'random_cat/stack/%s_band_random_field_pix_SB_mean_pdf.h5' % band[kk], 'w') as f: f['a'] = np.array(dmp_array) with h5py.File(load + 'random_cat/stack/%s_band_random_field_pix_SB_mean_pdf.h5' % band[kk], ) as f: for ll in range(len(dmp_array)): f['a'][ll,:] = dmp_array[ll,:] dmp_array = np.array([flux_median, num_mean]) with h5py.File(load + 'random_cat/stack/%s_band_random_field_pix_SB_median_pdf.h5' % band[kk], 'w') as f: f['a'] = np.array(dmp_array) with h5py.File(load + 'random_cat/stack/%s_band_random_field_pix_SB_median_pdf.h5' % band[kk], ) as f: for ll in range(len(dmp_array)): f['a'][ll,:] = dmp_array[ll,:] with h5py.File(load + 'random_cat/stack/%s_band_random_field_pix_SB_mean_pdf.h5' % band[kk], 'r') as f: dmp_array = np.array(f['a']) flux_mean, num_mean = dmp_array[0], dmp_array[1] dx = flux_mean[1:] - flux_mean[:-1] dx = np.r_[dx[0], dx] M_flux_mean = np.sum(flux_mean * num_mean * dx) with h5py.File(load + 'random_cat/stack/%s_band_random_field_pix_SB_median_pdf.h5' % band[kk], 'r') as f: dmp_array = np.array(f['a']) flux_median, num_mean = dmp_array[0], dmp_array[1] dx = flux_median[1:] - flux_median[:-1] dx = np.r_[dx[0], dx] M_flux_median = np.sum(flux_median * num_mean * dx) fig = plt.figure() ax = plt.subplot(111) ax.set_title('%s band random filed pixel SB pdf' % band[kk]) ax.plot(flux_mean, num_mean, c = 'r', ls = '-', label = 'Mean pixel SB') ax.axvline(x = M_flux_mean, color = 'r', linestyle = '-', alpha = 0.5) ax.plot(flux_median, num_mean, c = 'g', ls = '--', label = 'Median pixel SB') ax.axvline(x = M_flux_median, color = 'g', linestyle = '--', alpha = 0.5) ax.set_xlabel('$ pixel \; SB \; [nanomaggies / arcsec^2]$') ax.set_ylabel('pdf') ax.legend(loc = 1, frameon = False) ax.tick_params(axis = 'both', which = 'both', direction = 'in') subax = fig.add_axes([0.2, 0.35, 0.25, 0.4]) subax.plot(flux_mean, num_mean, c = 'r', ls = '-', label = 'Mean pixel SB') subax.axvline(x = M_flux_mean, color = 'r', linestyle = '-', alpha = 0.5) subax.set_xlim(-5e-4, 5e-4) xtick = subax.get_xticks() re_xtick = xtick * 1e4 subax.set_xticks(xtick) subax.set_xticklabels(['%.1f' % ll for ll in re_xtick]) subax.set_xlabel('1e4 * pixel SB') subax.tick_params(axis = 'both', which = 'both', direction = 'in') plt.savefig(load + 'random_cat/stack/%s_band_pix_SB_pdf.png' % band[kk], dpi = 300) plt.close() return