def mc_plot(result_arr, mc_arr, img_name): img = Image_Plot(fig_x=6, fig_y=4, xpad=0.25, ypad=0.2) img.subplots(1, 2) scale = 1000 img.axs[0][0].errorbar(result_arr[0], result_arr[1], result_arr[2], marker="s", fmt=" ", label="g1", capsize=img.cap_size) img.axs[0][0].errorbar(result_arr[3], result_arr[4], result_arr[5], marker="s", fmt=" ", label="g2", capsize=img.cap_size) img.axs[0][1].errorbar(result_arr[0], scale * (result_arr[1] - result_arr[0]), scale * result_arr[2], marker="s", fmt=" ", label="g1", capsize=img.cap_size) img.axs[0][1].errorbar(result_arr[3], scale * (result_arr[4] - result_arr[3]), scale * result_arr[5], marker="s", fmt=" ", label="g2", capsize=img.cap_size) mc_str = "Mean\n$m_1 = %.5f (%.5f)$\n$c_1 = %.5f (%.5f)$\n$m_2 = %.5f (%.5f)$\n$c_2 = %.5f (%.5f)$" \ % (mc_arr[0, 0], mc_arr[0, 1], mc_arr[0, 2], mc_arr[0, 3], mc_arr[1, 0], mc_arr[1, 1], mc_arr[1, 2], mc_arr[1, 3]) img.axs_text(0, 0, 0.8, 0.05, mc_str, text_fontsize=img.legend_size) img.set_label(0, 0, 0, "EST g") img.set_label(0, 1, 1, "$10^3 (\hat{g} - g_{true})$") img.axs[0][0].plot([-0.1, 0.1], [-0.1, 0.1], ls="dashed", c="grey", alpha=0.5) img.axs[0][0].set_ylim(-0.05, 0.05) img.axs[0][1].set_ylim(-0.1, 0.1) for i in range(2): img.set_label(0, i, 1, "TRUE g") img.axs[0][i].legend(fontsize=img.legend_size, loc="lower right") img.axs[0][i].set_xlim(-0.05, 0.05) img.save_img(img_name) img.close_img()
def plot_gf_pix(x, y, ichip, gf1, gf2, gf, gf1_scale, gf2_scale,gf_scale,dot_size=1,pic_path=None): chip_row, chip_col = numpy.divmod(ichip, 9) x = x + chip_col * 2112 y = y + chip_row * 4644 img = Image_Plot(xpad=0.2,ypad=0.1) img.subplots(1, 3) color_cm = 'bwr' norm = plt.Normalize(vmin=numpy.min(gf1_scale[0]), vmax=numpy.max(gf1_scale[1])) cmap = plt.get_cmap(color_cm) cl = cmap(norm(gf1)) fig = img.axs[0][0].scatter(x, y, color=cl, s=dot_size) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm._A = [] img.figure.colorbar(sm, ax=img.axs[0][0]) norm = plt.Normalize(vmin=numpy.min(gf2_scale[0]), vmax=numpy.max(gf2_scale[1])) cmap = plt.get_cmap(color_cm) cl = cmap(norm(gf2)) fig = img.axs[0][1].scatter(x, y, color=cl, s=dot_size) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm._A = [] img.figure.colorbar(sm, ax=img.axs[0][1]) norm = plt.Normalize(vmin=numpy.min(gf_scale[0]), vmax=numpy.max(gf_scale[1])) cmap = plt.get_cmap(color_cm) cl = cmap(norm(gf)) fig = img.axs[0][2].scatter(x, y, color=cl, s=dot_size) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm._A = [] img.figure.colorbar(sm, ax=img.axs[0][2]) for i in range(3): img.axis_sci_ticklabel(0,i,0) img.axis_sci_ticklabel(0,i,1) img.set_label(0,i,0,"y") img.set_label(0,i,1,"x") if pic_path: img.save_img(pic_path) # img.show_img() img.close_img()
def plot_label(x, y, cent_xy, cent_label, title, show=None, pic_path=None): ncent = cent_label.shape[0] img = Image_Plot() img.subplots(1, 1) for i in range(ncent): img.axs[0][0].scatter(cent_xy[0][i], cent_xy[1][i], color="C%d" % i, marker="*", s=80) idx = cent_label[i] == 1 img.axs[0][0].scatter(x[idx], y[idx], edgecolors="C%d" % i, color="none", label="%d, %d" % (i, idx.sum()), s=5) img.axs[0][0].set_title(title) img.axs[0][0].legend() if pic_path: img.save_img(pic_path) if show: img.show_img() img.close_img()
'%d' % ny], ['param', "RA", '%.2f' % delta_ra], ['param', "DEC", '%.2f' % delta_dec], ['param', "pixel_scale", '%.6f' % pixel_scale], ['param', "sigma", '%.4f' % sigma], ['param', "amplitude", '%.2f' % amplitude], ['param', "dx", '%.4f' % dx], ['param', "dy", '%.2f' % dy], ['param', "density/arcmin^2", '%d' % dens_num]] path_items = tool_box.config(envs_path, ['add' for i in range(len(contents))], contents, True) img = Image_Plot() img.subplots(2, 2) inverse = range(nx - 1, -1, -1) for i in range(2): for j in range(2): tag = i * 2 + j if tag < 3: fig = img.axs[i][j].imshow(g[tag][inverse], cmap="jet") img.figure.colorbar(fig, ax=img.axs[i][j]) else: num_dens = numpy.histogram2d(gal_coord[0], gal_coord[1], [ra_bin, dec_bin])[0] fig = img.axs[i][j].imshow(num_dens, cmap="jet") img.figure.colorbar(fig, ax=img.axs[i][j]) img.set_label(i, j, 0, "DEC. [arcmin]") img.set_label(i, j, 1, "R.A. [arcmin]")
h5f.close() # true, mean, measured, sigma, diff_percent, number plt_data_com = [ shear_field_grid_true, shear_field_grid_mean, shear_field_grid, shear_field_grid_mean - shear_field_grid_true, shear_field_grid - shear_field_grid_true ] plt_data_m = [shear_field_grid_err, num_in_grid] plt_data_coms.append(plt_data_com) plt_data_ms.append(plt_data_m) img = Image_Plot(fig_x=5, fig_y=5 / 4 * 3) img.subplots(len(grid_nx), 5) titles = [ "True", "Mean in grid", "Measured", "Mean - True", "Measured - True" ] for tag in range(len(grid_nx)): nx = grid_nx[tag] inverse = range(nx - 1, -1, -1) grid_min = min([plt_data_coms[tag][i].min() for i in range(3)]) grid_max = min([plt_data_coms[tag][i].max() for i in range(3)]) diff_min = min([plt_data_coms[tag][i].min() for i in range(3, 5)]) diff_max = min([plt_data_coms[tag][i].max() for i in range(3, 5)])
from Fourier_Quad import Fourier_Quad from plot_tool import Image_Plot h5f = h5py.File("test.hdf5", "r") img = h5f["/img"][()] img_pow = h5f["/img_pow"][()] img_pow_real = h5f["/img_pow_real"][()] img_pow_imag = h5f["/img_pow_imag"][()] img_pow_arg = h5f["/img_arg"][()] h5f.close() fq = Fourier_Quad(img.shape[0], 12) img_pow_fq = fq.pow_spec(img) img_pow_arg_fq = fq.pow_arg(img) img_pow_real_fq, img_pow_imag_fq = fq.pow_real_imag(img) datas = [[img, img_pow, img_pow_arg, img_pow_real, img_pow_imag], [img, img_pow_fq, img_pow_arg_fq, img_pow_real_fq, img_pow_imag_fq], [ img - img, img_pow - img_pow_fq, img_pow_arg - img_pow_arg_fq, img_pow_real - img_pow_real_fq, img_pow_imag - img_pow_imag_fq ]] img = Image_Plot() img.subplots(3, 5) for i in range(3): for j in range(5): fig = img.axs[i][j].imshow(datas[i][j]) img.figure.colorbar(fig, ax=img.axs[i][j]) img.del_ticks(i, j, [0, 1]) img.show_img()
import matplotlib matplotlib.use("Agg") from sys import path, argv path.append("/home/hklee/work/mylib") path.append("/home/hkli/work/mylib") path.append("D:/GitHub/astrophy-research/mylib") from plot_tool import Image_Plot import numpy import h5py import os parent_path = "/mnt/ddnfs/data_users/hkli/bias_check/data_from_pi/psf_scale_test" img = Image_Plot(fig_x=7, fig_y=2, ypad=0, xpad=0.2) img.subplots(4, 1) m_scale = 100 c_scale = 10000 x_tick_pos = [] for i in range(7): noise_free_path = parent_path + "/data_%d/shear_result_noise_free_epsf_ori_pdf.hdf5" % i noisy_path = parent_path + "/data_%d/shear_result_noisy_cpp_epsf_ori_pdf.hdf5" % i print(i, os.path.exists(noise_free_path)) print(os.path.exists(noisy_path)) # # continue h5f = h5py.File(noise_free_path, "r") sym_mc_nf = h5f["/sym_mc"][()] mean_mc_nf = h5f["/mean_mc"][()] h5f.close()
import os my_home = os.popen("echo $MYWORK_DIR").readlines()[0][:-1] from sys import path, argv path.append('%s/work/mylib/' % my_home) import h5py import numpy from plot_tool import Image_Plot src_path = argv[1] ra_bin = [1000, 5000, 10000, 15000, 30000] labels = [] sub_num = numpy.array([75, 34, 56, 35], dtype=numpy.intc) img = Image_Plot(fig_x=8, fig_y=6) img.subplots(2, 2) for i in range(2): for j in range(2): tag = int(i * 2 + j) h5f = h5py.File(src_path + "/group_predict_%d.hdf5" % tag, "r") group_label = h5f["/data"][()] ra_dec = h5f["/ra_dec"][()] h5f.close() for k in range(sub_num[tag]): idx = group_label == k print(k, idx.sum()) # group_label += sub_num[:tag].sum() print(tag, group_label.min(), group_label.max(), sub_num[:tag].sum()) print("\n") img.pts_scatter(i, j, ra_dec[:, 0], ra_dec[:, 1], group_label,
h5f.close() print(chisqs.shape) print(chisqs.min(), chisqs.max()) omega_cm_num = omega_cm_bin.shape[0] omega_bm_num = omega_bm_bin.shape[0] sigma8_num = sigma8_bin.shape[0] my, mx = numpy.zeros((omega_cm_num, sigma8_num)), numpy.zeros( (omega_cm_num, sigma8_num)) for i in range(omega_cm_num): my[i] = omega_cm_bin[i] for j in range(sigma8_num): mx[:, j] = sigma8_bin[j] img = Image_Plot(fig_x=4, fig_y=3, xpad=0.25, ypad=0.28) img.subplots(2, 5) for i in range(2): for j in range(5): tag = i * 2 + j idx = chisqs[tag] > -200 print(idx.sum()) x = mx[idx].flatten() y = my[idx].flatten() z = chisqs[tag][idx].flatten() img.scatter_pts(i, j, x, y, z, color_map="jet", pts_size=18) img.axs[i][j].set_title("$\Omega_b=%.3f$" % omega_bm_bin[tag]) img.set_label(i, j, 0, "$\Omega_m$") img.set_label(i, j, 1, "$\sigma_8$")
# the galactic parameters h5f = h5py.File(parent_path + "param_slope.hdf5", "w") h5f["/FIELD_g"] = shear_field h5f["/x"] = x mag_i = tool_box.mag_generator(total_num, 20, 23.5) flux_i = tool_box.mag_to_flux(mag_i) h5f["/mag"] = mag_i h5f["/flux"] = flux_i print(flux_i.min(), flux_i.max()) # Plot img = Image_Plot() img.subplots(1, 3) img.set_style() inverse = range(nx - 1, -1, -1) norm_g = plt.Normalize(vmin=numpy.min(shear_field_ch), vmax=numpy.max(shear_field_ch)) cmap_g = plt.get_cmap('jet') for i in range(3): if i == 0: fig = img.axs[0][i].imshow(shear_field[inverse], cmap="jet") img.figure.colorbar(fig, ax=img.axs[0][i]) elif i == 1: num_dens = numpy.histogram2d(gal_coord[0], gal_coord[1], [ra_bin, dec_bin])[0] fig = img.axs[0][i].imshow(num_dens, cmap="jet")
print("E-PSF") gal_imgs = numpy.zeros((size, 4*size)) # gal_imgs_cpp = numpy.zeros((size, 4*size)) psf_imgs = numpy.zeros((size, 4*size)) # psf_imgs_cpp = numpy.zeros((size, 4*size)) for i in range(4): psf = fq.cre_psf(psf_scale,ellip_theta=(psf_e, numpy.pi/4*i + theta)) gal = fq.convolve_psf(pts, psf_scale, 1, ellip_theta=(psf_e, numpy.pi/4*i + theta)) gal_imgs[:,int(i*size):int((i+1)*size)] = gal psf_imgs[:,int(i*size):int((i+1)*size)] = psf diff_psf = psf_imgs - epsf_cpp print(diff_psf.min(), diff_psf.max()) img = Image_Plot(fig_x=4,fig_y=3) img.subplots(3, 1) fig = img.axs[0][0].imshow(psf_imgs) img.figure.colorbar(fig,ax=img.axs[0][0]) fig = img.axs[1][0].imshow(epsf_cpp) img.figure.colorbar(fig,ax=img.axs[1][0]) fig = img.axs[2][0].imshow(diff_psf) img.figure.colorbar(fig,ax=img.axs[2][0]) img.show_img() diff_gal = gal_imgs - gal_epsf_cpp print(diff_gal.min(), diff_gal.max()) img = Image_Plot(fig_x=4,fig_y=3) img.subplots(3, 1) fig = img.axs[0][0].imshow(gal_imgs) img.figure.colorbar(fig,ax=img.axs[0][0])
xylim = 0 xy_lock = -1 move_steps = 200 show_num = 150 gh = numpy.linspace(-0.1, 0.1, move_steps) m, n = divmod(move_steps, numprocs) st, ed = int(m * rank), int(m * (rank + 1)) img_col = 6 img_row = 3 for k in range(st, ed): img = Image_Plot(fig_x=3, fig_y=3, xpad=0.4, ypad=0.4, xy_lb_size=12) img.subplots(img_row, img_col) for i in range(img_row): for j in range(img_col): if i < img_row - 1: tag = i * img_col + j if component_tag == 0: G1_hat = datas[tag][:show_num, 0] - gh[k] * ( datas[tag][:show_num, 2] + datas[tag][:show_num, 3]) img.axs[i][j].scatter(G1_hat, datas[tag][:show_num, 1], s=6, label="g1=%.3f" % gh[k]) else:
comm.Barrier() t2 = time.time() if rank == 0: for j in range(shear_num): h5f = h5py.File(parent_path + "shear_%d.hdf5" % j, "r") temp = h5f["/data"].value h5f.close() if j == 0: data = temp else: data = numpy.row_stack((data, temp)) img = Image_Plot() img.subplots(2, len(ch_num)) img.set_style() for i in range(len(ch_num)): idx = [n + m*gal_each_shear for m in range(shear_num) for n in range(ch_num[i])] mg1 = data[:, 0][idx] mg2 = data[:, 1][idx] mnu1 = data[:, 2][idx] + data[:, 3][idx] mnu2 = data[:, 2][idx] - data[:, 3][idx] gh1, gh1_sig = fq.fmin_g_new(g=mg1, nu=mnu1, bin_num=8, scale=100,fig_ax=img.axs[0][i])[:2] gh2, gh2_sig = fq.fmin_g_new(g=mg2, nu=mnu2, bin_num=8, scale=100,fig_ax=img.axs[1][i])[:2] result_1[i, shear_num*2] = gh1
scale = 100 xy_bin_num, radius_bin_num = 200, 60 h5f = h5py.File(data_path + "/data_%s_%d.hdf5" % (data_nm, rank), "r") print("g1:", g1, "g2:", g2) print(data_path + "/data_%s_%d.hdf5" % (data_nm, rank)) mg1 = h5f["/mg1"][()] / scale mg2 = h5f["/mg2"][()] / scale mn = h5f["/mn"][()] / scale mu = h5f["/mu"][()] / scale h5f.close() img = Image_Plot(xpad=0.15, ypad=0.2, fig_x=4, fig_y=3) img.subplots(6, 6) xy_bin = component_fit.get_bin(mg1, mg2, xy_bin_num) num, xgrid, ygrid, radius_grid = component_fit.get_2dhist(mg1, mg2, xy_bin)[:4] dpl, radius_bin, radius_mask, mean_of_annuli = component_fit.get_dipole( num, radius_grid, radius_bin_num) qpl, dpl_fit, sin_theta, cos_theta = component_fit.get_quadrupole( dpl, xgrid, ygrid, radius_bin, radius_bin_num) qpl_fit, sin_2theta, cos_2theta = component_fit.fit_quadrupole( qpl, xgrid, ygrid, radius_bin, radius_bin_num) dpl = numpy.nan_to_num(dpl)
my_g2 = tool_box.allot([i for i in range(fg2_bin_num)], cpus)[rank] for i in range(fg2_bin_num): idx_1 = fg2 >= fg2_bin[i] idx_2 = fg2 < fg2_bin[i + 1] idx_t = idx_1 & idx_2 g2, g2_sig = fq.fmin_g_new(mg2[idx_t], mnu2[idx_t], 8, fit_num=20)[:2] result[2, i] = g2 result[3, i] = g2_sig comm.Barrier() t2 = time.time() if rank == 0: img = Image_Plot() img.subplots(1, 2) img.axs[0][0].errorbar(fg1_bin[:fg1_bin_num], result[0, :fg1_bin_num], result[1, :fg1_bin_num], capsize=3, c="C1", label="g1") img.axs[0][0].scatter(fg1_bin[:fg1_bin_num], result[0, :fg1_bin_num], c="C1", marker="s") xs = img.axs[0][0].set_xlim() ys = img.axs[0][0].set_ylim() max_x = max([abs(xs[0]), abs(xs[1])])
shear_num = g1.shape[0] sub_num = 4 data_col = 4 result_g1 = numpy.zeros((5, shear_num)) result_g1[4] = g1 result_g2 = numpy.zeros((5, shear_num)) result_g2[4] = g2 parent_path = "E:/test/data_1" if mode == "all": for ir in range(1): img = Image_Plot(fig_x=5, fig_y=4, cap_size=2.5) img.subplots(2, shear_num + 2) pic_nm = parent_path + "/data_total_noise-free.png" #%ir result_path = parent_path + "/data_total_noise-free.txt" #%ir for tag in range(shear_num): # h5f = h5py.File(parent_path + "/data_rotation_%d/data_%d.hdf5"%(ir,tag),"r") h5f = h5py.File(parent_path + "/data_total/data_%d.hdf5" % tag, "r") data = h5f["/data"].value print(data.shape) h5f.close() data_nms = [ "Noise-free", "Noise_power", "Noise_residual", "Normal" ]
# my_home = os.popen("echo $MYWORK_DIR").readlines()[0][:-1] from sys import path, argv # path.append('%s/work/mylib/' % my_home) path.append("D:/GitHub/astrophy-research/mylib") from plot_tool import Image_Plot import h5py import numpy import matplotlib.pyplot as plt import tool_box data_path = "E:/works/correlation/CFHT/2021_6_15/cut_2.5_new/unsmooth/ODDS_0.4" h5f = h5py.File(data_path + "/bias_test.hdf5", "r") img = Image_Plot(fig_x=4, fig_y=3, xpad=0.14, ypad=0.2) img.subplots(2, 4) for i in range(2): for j in range(2): tag = int(2 * i + j) data = numpy.abs(h5f["/%d" % tag][()]) scale = 1 # img.axs[0][tag].errorbar(data[:,0], data[:,1]*scale, data[:,2]*scale,fmt=" ",capsize=3,marker="s", label="$10^3 c_1$") img.axs[0][tag].scatter(data[:, 0], data[:, 1] * scale / data[:, 2] * scale, marker="s", label="$|c_1|/\delta_{c_1}$") # img.axs[1][tag].errorbar(data[:,0], data[:,3]*scale, data[:,4]*scale,fmt=" ",capsize=3,marker="s", label="$10^3 c_2$") img.axs[1][tag].scatter(data[:, 0], data[:, 3] * scale / data[:, 4] * scale, marker="s",
file_tag = 1 matplotlib.style.use('default') plt.rcParams['font.family'] = 'serif' total_path = "./imgs/" # read the data file_npz = numpy.load(data_path) shears = file_npz["arr_0"] # only the 0 & 1'th column is needed ori_data, snr = file_npz["arr_1"][:, 0], file_npz["arr_1"][:, 1] shear_data = file_npz["arr_2"] # data_relative_err = file_npz["arr_4"] # plot img = Image_Plot() img.subplots(1, img_num) markers = ['o', 'v', 's', 'h', 'd', 'p', "4", "*", "X", "^", ">", "+"] colors = ["C%d" % i for i in range(10)] labels = ["P$_{k0}$", "SNR$_S$", "SNR$_A$", "MAG_AUTO", "Resolution"] fmt = '%2.f%%' for i in range(img_num): if i == 0: img.axs[0][i].set_ylabel("relative error", fontsize=img.xy_lb_size) # img.axs[1][i].set_ylabel("relative error",fontsize=img.xy_lb_size) # else: # img.axs[0][i].set_yticklabels([])
path.append("D:/Github/astrophy-research/mylib") path.append("D:/Github/astrophy-research/multi_shear_detect") from Fourier_Quad import Fourier_Quad from plot_tool import Image_Plot import matplotlib.pyplot as plt from matplotlib import cm psf_type = "Moffat" psf_flux = 1 psf_scale = 4 stamp_size = 48 seed = 56525 fq = Fourier_Quad(stamp_size, seed) img = Image_Plot() img.subplots(1, 1) fig = img.axs[0][0].imshow(fq.kx2 + fq.ky2) img.figure.colorbar(fig, ax=img.axs[0][0]) img.axs[0][0].set_title("$k^2$") img.del_ticks(0, 0, [0, 1]) img.save_img("E:/kxy.png") img.show_img() pst_num = 50 gal_fluxs = [4000, 16000, 32000, 100000] steps = [0.6, 1, 2, 4] fq = Fourier_Quad(stamp_size, seed) max_radius = 7 pts = fq.ran_pts(pst_num, max_radius, step=1)
matplotlib.use("Agg") import os my_home = os.popen("echo $MYWORK_DIR").readlines()[0][:-1] from sys import path, argv path.append('%s/work/mylib/' % my_home) from subprocess import Popen import numpy from plot_tool import Image_Plot # plot # matplotlib.style.use('default') img = Image_Plot(fig_x=6, fig_y=4, ypad=0,xpad=0.2) img.subplots(2, 1) # img.set_style_default() img.set_style() img.axis_type(0, "major") img.axis_type(1, "major") for i in range(2): img.axs[i][0].set_ylabel("Relative error", fontsize=img.xy_lb_size + 2) img.axs[0][0].set_xticklabels([]) img.axs[1][0].set_xlabel("$g_1$", fontsize=img.xy_lb_size + 2) markers = ['o', 'v', 's', 'h', 'd', 'p', "4", "*", "X", "^", ">", "+"] colors = ["C%d" % i for i in range(10)]
for tag, z in enumerate(src_z): print("Source plane 1 at z = %.2f, %.5f Mpc/h" % (z, src_com_dist[tag])) # show the shear/kappa map bin_num = 100 Rmax = 1000 print("Max to %.3f Mpc/h" % (com_dist_len * Rmax / 3600 / 180 * numpy.pi)) if rank == 0: ra_bin = numpy.linspace(-Rmax, Rmax, bin_num + 1) dec_bin = numpy.linspace(-Rmax, Rmax, bin_num + 1) ra, dec = GGLensing_tool.get_pos(ra_bin, dec_bin, bin_num) shear_data = [GGLensing_tool.cal_shear(ra, dec, nfw, z) for z in src_z] img = Image_Plot(xpad=0.25, ypad=0.25) img.subplots(4, src_plane_num) for i in range(src_plane_num): kappa, g1, g2 = shear_data[i][0], shear_data[i][1], shear_data[i][2] g = numpy.sqrt(g1**2 + g2**2) img.scatter_pts(0, i, ra, dec, kappa) img.scatter_pts(1, i, ra, dec, g) img.scatter_pts(2, i, ra, dec, g1) img.scatter_pts(3, i, ra, dec, g2) img.axs[0][i].set_title("kappa from source plane at Z=%.3f" % src_z[i]) img.axs[1][i].set_title("g from source plane at Z=%.3f" % src_z[i]) img.axs[2][i].set_title("g1 from source plane at Z=%.3f" % src_z[i]) img.axs[3][i].set_title("g2 from source plane at Z=%.3f" % src_z[i])
sin_2theta = numpy.sin(2 * position_theta) cos_4theta = numpy.cos(4 * position_theta) sin_4theta = numpy.sin(4 * position_theta) mg1r = (mg1 * cos_2theta - mg2 * sin_2theta) * crit_coeff mg2r = (mg1 * sin_2theta + mg2 * cos_2theta) * crit_coeff mur = mu * cos_4theta - mv * sin_4theta mnur1 = mn + mur mnur2 = mn - mur G1_bin = tool_box.set_bin(mg1r, mg_bin_num, 100) G1_hist_bin = gglensing_tool.set_bin(mg1r, 4000, 1.001, "log") NU1_hist_bin = gglensing_tool.set_bin(mnur1, 4000, 1.001, "log") img = Image_Plot(xpad=0.25, ypad=0.25) img.subplots(1, 2) result = gglensing_tool.find_shear_grid(mg1r, mnur1, G1_bin, G1_hist_bin, NU1_hist_bin, chisq_gap=50, dg=10, fit_num=20, ax=img.axs[0][0])[:4] gh, gh_sig, coeff, asym = result # chisqs_min = coeff[0] - coeff[1] ** 2 / 4 / coeff[2] # if ij[1] == 0: # total_result[ij[0], 0] = asym # total_result[ij[0]+dilute_case, 0] = chisqs_min
data_noisy_e = h5f["/data"][()] h5f.close() scale = 10000 diff = (data_noise_free_e - data_noisy_e) estimator = [diff[:,0],diff[:,2],diff[:,2]+diff[:,3], diff[:,1],diff[:,3],diff[:,2]-diff[:,3]] est = ["G1","N","N+U", "G2","U","N-U"] bin_num = 5000 img = Image_Plot() img.subplots(2,3) for i in range(6): m,n = divmod(i,3) img.axs[m][n].hist(estimator[i]/scale,bin_num) fit = tool_box.gaussnosie_fit(estimator[i]/scale,bin_num) img.axs[m][n].plot(fit[0],fit[1],c="C1",ls="--",linewidth=1.5) print("plot %s"%est[i]) ys = img.axs[m][n].set_ylim() img.axs[m][n].plot([0,0],[ys[0],ys[1]],ls="--",alpha=0.5,c="k",linewidth=1) print(fit[4]) text_str = "%s\n%.4f\n%.4f\n%.4f"%(est[i],fit[4][0],fit[4][1],fit[4][2]) img.axs_text(m,n,0.8,0.1,text_str)
def plot_gf(ra, dec, gf1, gf2, pic_path, gf1_bin_num = 15, gf2_bin_num = 18,gf_bin_num = 18): gf = numpy.sqrt(gf1**2 + gf2**2) label_1 = numpy.zeros_like(gf1) label_2 = numpy.zeros_like(gf2) label_3 = numpy.zeros_like(gf) gf1_bin = numpy.linspace(gf1.min(), gf1.max(), gf1_bin_num+1) gf2_bin = numpy.linspace(gf2.min(), gf2.max(), gf2_bin_num+1) gf_bin = numpy.linspace(gf.min(), gf.max(), gf_bin_num+1) for i in range(gf1_bin_num): idx1 = gf1 >= gf1_bin[i] idx2 = gf1 < gf1_bin[i+1] idx = idx1 & idx2 label_1[idx] = (gf1_bin[i]+ gf1_bin[i+1])/2 for i in range(gf2_bin_num): idx1 = gf2 >= gf2_bin[i] idx2 = gf2 < gf2_bin[i+1] idx = idx1 & idx2 label_2[idx] = (gf2_bin[i]+ gf2_bin[i+1])/2 for i in range(gf_bin_num): idx1 = gf >= gf_bin[i] idx2 = gf < gf_bin[i+1] idx = idx1 & idx2 label_3[idx] = (gf_bin[i]+ gf_bin[i+1])/2 img = Image_Plot() img.subplots(1,3) norm = plt.Normalize(vmin=numpy.min(label_1), vmax=numpy.max(label_1)) cmap = plt.get_cmap('YlOrRd',gf1_bin_num) cl = cmap(norm(label_1)) fig = img.axs[0][0].scatter(ra, dec, color=cl, s=3) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm._A = [] img.figure.colorbar(sm, ax=img.axs[0][0]) norm = plt.Normalize(vmin=numpy.min(label_2), vmax=numpy.max(label_2)) cmap = plt.get_cmap('YlOrRd',gf2_bin_num) cl = cmap(norm(label_2)) fig = img.axs[0][1].scatter(ra, dec, color=cl, s=3) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm._A = [] img.figure.colorbar(sm, ax=img.axs[0][1]) norm = plt.Normalize(vmin=numpy.min(label_3), vmax=numpy.max(label_3)) cmap = plt.get_cmap('YlOrRd',gf_bin_num) cl = cmap(norm(label_3)) fig = img.axs[0][2].scatter(ra, dec, color=cl, s=3) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm._A = [] img.figure.colorbar(sm, ax=img.axs[0][2]) img.save_img(pic_path) # img.show_img() img.close_img()
h5f.close() h5f = h5py.File(data_path + "/new_pdf_noise_free.hdf5", "r") ave_result = h5f["/average"][()].T pdf_result = h5f["/PDF"][()].T new_pdf_result1 = h5f["/new_PDF/g1"][()] new_pdf_result2 = h5f["/new_PDF/g2"][()] h5f.close() shear_num = ave_result.shape[1] img = Image_Plot(cap_size=4, xpad=0.2, ypad=0.2) img.subplots(1, 2) print(ave_result) mc1 = numpy.array(tool_box.data_fit(g1, ave_result[0], ave_result[1])) mc2 = numpy.array(tool_box.data_fit(g2, ave_result[2], ave_result[3])) mc1[0] = mc1[0] - 1 mc2[0] = mc2[0] - 1 print(mc1) print(mc2) img.axs[0][0].errorbar(0, mc1[0], mc1[1], label="ave m1", marker="s", capsize=img.cap_size)
bin_num = 8 bin_num2 = int(bin_num / 2) x = numpy.linspace(-1, 1, signal_num).reshape((signal_num, 1)) x2 = x * x x3 = x * x * x x4 = x * x * x * x signals = a1 + a2 * x + a3 * x2 + a4 * x3 + a5 * x4 parameters = [a1, a2, a3, a4, a5] print( "Signals: ", signals[:, 0], ".\n%.4f + %.4f*x + %.4f*x^2 + %.4f*x^3 + %.4f*x^4" % (a1, a2, a3, a4, a5)) ellip = numpy.zeros((signal_num, num)) img = Image_Plot() img.subplots(1, 1) fq_shear = numpy.zeros((2, signal_num)) for i in range(signal_num): # rng = numpy.random.RandomState(i+1) # ellip[i] = rng.normal(signals[i,0], 0.3, num) ellip[i] = numpy.random.normal(signals[i, 0], 0.3, num) # noise = rng.normal(0, numpy.abs(ellip[i])/5) # ellip[i] += noise t1 = time.time() gh, gh_sig = fq.fmin_g_new(ellip[i], numpy.ones_like(ellip[i]), 8)[:2] fq_shear[0, i] = gh fq_shear[1, i] = gh_sig t2 = time.time() print("signal:[%.4f at %.4f] %.4f (%.4f) [%d gal], Time: %.2f sec" % (signals[i, 0], x[i, 0], gh, gh_sig, num, t2 - t1))
sub_task = tool_box.allot(task_list, cpus)[rank] for i in sub_task: m, n = divmod(i, nx) gh = a1_range[n] + a2_range[m]*x # y(a2)-axis, x(a1)-axis chisq[m,n] = fq.get_chisq(mg1, mnu1, gh, mg_bins, bin_num2,inverse, 0) chisq_new[m,n] = fq.get_chisq_new(mg1, mnu1, gh, mg_bins, bin_num2, inverse, 0, num_exp) comm.Barrier() if rank == 0: numpy.savez(cache_nm, chisq, chisq_new) img = Image_Plot(fig_x=12, fig_y=9) img.subplots(1, 1) chi_temp = fq.get_chisq(mg1, 0, 0, mg_bins, bin_num2, inverse, 0) img.axs[0][0].hist(mg1, hist_bin, histtype='step',linewidth=img.plt_line_width, label="Before PDF, $\chi^2$=%.2f"%chi_temp) # num_null = numpy.histogram(mg, mg_bins_test)[0] # n1 = num_null[0:bin_num2_test][inverse_test] # n2 = num_null[bin_num2_test:] # chi_null = (n1-n2)/numpy.abs(n1-n2) * numpy.sum((n1-n2)**2/(n1+n2))*0.5 # img.axs[0][1].scatter(range(bin_num2_test), chi_null,marker="p", s=140, label="Before PDF, $\chi^2$=%.2f"%chi_temp) a2_test = numpy.linspace(a2-0.1, a2+0.1, 5) for i in range(5): gh = a2_test[i]*x chi_temp = fq.get_chisq(mg1, mnu1, gh, mg_bins, bin_num2, inverse, 0) img.axs[0][0].hist(mg1-gh*mnu1, hist_bin, histtype='step',linewidth=img.plt_line_width, label="$a_2$=%.2f, $\chi^2$=%.2f"%(a2_test[i],chi_temp))
print(0) idx = err_2s <= 10 if idx.sum() > 0: print(idx.sum(), err_2s[idx].min(), err_2s[idx].max()) else: print(0) # # idx = err_1s > 0.34 # print(idx.sum(), err_1s[idx].min()) # idx = err_2s > 0.34 # print(idx.sum(), err_2s[idx].min()) # # # histogram of \chi^2 img = Image_Plot() img.subplots(1, 2) img.axs[0][0].hist(err_1s, 100) img.axs[0][1].hist(err_2s, 100) img.set_label(0, 0, 1, "$\chi^2$") img.set_label(0, 1, 1, "$\chi^2$") img.set_label(0, 0, 0, "Num") img.set_label(0, 1, 0, "Num") img.axs[0][0].set_yscale("log") img.axs[0][1].set_yscale("log") img.axs[0][0].set_xscale("log") img.axs[0][1].set_xscale("log") img.save_img(total_path + "chisq_%d.png" % i) img.close_img() # img.show_img() # #
data_path + "/cuts_pi_all_sample_w_maxpkfit_sq/sym/sex2_1.5/flux2_ex3/total.hdf5", "r") mc1_mpk_pk_fit = h5f["/mc1"][()][:, ch] mc2_mpk_pk_fit = h5f["/mc2"][()][:, ch] h5f.close() h5f = h5py.File( data_path + "/cuts_pi_all_sample_w_maxpkfit_sq/sym/sex2_1.5/mag_true/total.hdf5", "r") mc1_mpk_tmag = h5f["/mc1"][()][:, ch] mc2_mpk_tmag = h5f["/mc2"][()][:, ch] h5f.close() matplotlib.rcParams["font.family"] = "serif" img = Image_Plot(fig_x=6, fig_y=4, xpad=0.2, ypad=0.2, legend_size=13) img.subplots(1, 1) img.axis_type(0, "major", tick_len=6, tick_width=1.5) img.axis_type(1, "major", tick_len=6, tick_width=1.5) img.axs[0][0].errorbar(x_coord, 100 * mc1_tf_pk_fit[0], 100 * mc1_tf_pk_fit[1], linewidth=img.plt_line_width - 0.5, capsize=img.cap_size, marker="o", fillstyle="none", c="C2", label="$m_1$, weight=$F^{-2}$") img.axs[0][0].errorbar(x_coord, 100 * mc2_tf_pk_fit[0], 100 * mc2_tf_pk_fit[1],
exit() t1 = time.time() with Pool(ncpus) as pool: sampler = emcee.EnsembleSampler( nwalkers, ndim, MCMC_program.ln_prob_g, args=[MG, NU, mg_bins, bin_num2, inverse, ra, dec, shear_tag], pool=pool) pos, prob, state = sampler.run_mcmc(p0, step) t2 = time.time() print("Time: %2.f sec" % (t2 - t1)) # Plot the walkers img = Image_Plot(fig_x=16, fig_y=4) img.subplots(ndim, 1) for i in range(nwalkers): for j in range(ndim): img.axs[j][0].plot(range(step), sampler.chain[i, :, j], color='grey', alpha=0.6) img.axs[j][0].plot([0, step], [parameters[j], parameters[j]]) img.save_img(parent_path + "pic/%s_mcmc_walkers_nw_%d_stp_%d_expo_%d.png" % (shear_cmd, nwalkers, step, expo_num)) img.close_img() # Plot the panels samples = sampler.chain[:, 150:, :].reshape((-1, ndim)) print(samples.shape) corner_fig = plt.figure(figsize=(10, 10))