def pre_img(zbin_num):
    img = Image_Plot(fig_x=4, fig_y=3, xpad=0, ypad=0, axis_linewidth=2.5, plt_line_width=3, legend_size=35,
                     xy_tick_size=25)
    img.subplots(zbin_num, zbin_num)
    img.set_style()

    for i in range(zbin_num):
        for j in range(zbin_num - i, zbin_num):
            img.figure.delaxes(img.axs[i][j])

    img.axis_type(0, "major", 10)
    img.axis_type(1, "major", 10)
    img.axis_type(0, "minor", 5)
    img.axis_type(1, "minor", 5)

    return img
def show_signal(radius, signals, plot_scale=100):
    img = Image_Plot(xpad=0.25, ypad=0.15)
    img.subplots(1, 2)
    img.axs[0][0].errorbar(radius, signals[0] * plot_scale, signals[1] * plot_scale, capsize=3, marker="s", c="r",
                           fmt="--", label="$\gamma_t$")
    img.axs[0][1].errorbar(radius, signals[2] * plot_scale, signals[3] * plot_scale, capsize=3, marker='s', c="r",
                           fmt="--", label="$\gamma_x$")
    img.axs[0][0].errorbar(radius, true_signal * plot_scale, c="b", fmt="--", label="true $\gamma_t$")

    img.axs[0][0].set_yscale("log")

    for i in range(2):
        img.axs[0][i].legend(fontsize=img.legend_size)
        img.axs[0][i].set_xscale("log")

        img.set_label(0, i, 0, "$10^2\gamma$")
        img.set_label(0, i, 1, "Radius [Mpc/h]")
    ys = img.axs[0][1].set_ylim()
    xs = img.axs[0][1].set_xlim()
    img.axs[0][1].plot([xs[0], xs[1]], [0, 0], ls="--", c="k")
    xs = img.axs[0][1].set_xlim()
    img.axs[0][1].set_ylim(ys)
    img.show_img()
        data_ch = rng_ch.choice(data, int(0.1 * data_num[i]), replace=False)

        pdf_bin = FQlib.set_bin(data_ch, bin_num, 1000000)

        gh = cal_signal(sym_gh, data, pdf_bin)[0]
        results[i, j] = gh

        gh = cal_signal(asym_gh, data, pdf_bin)[0]
        results[i + len(shear), j] = gh

comm.Barrier()
print(results)

if rank == 0:

    img = Image_Plot()
    img.subplots(2, 2)
    for i in range(2):
        for j in range(2):
            tag = int(i * 2 + j)
            st, ed = min(results[tag].min(),
                         results[tag + len(shear)].min()), max(
                             results[tag + len(shear)].max(),
                             results[tag + len(shear)].max())
            hist_bin = numpy.linspace(st, ed, 50)

            img.axs[i][j].hist(results[tag], hist_bin, label="sym guess")
            img.axs[i][j].hist(results[tag + len(shear)],
                               hist_bin,
                               label="asym guess")
Exemple #4
0
    coeff_2 = com_dist_len * (com_dist_src - com_dist_len) * (1 + len_z)

mgt = mgt * coeff_1
mnu1 = mnu1 * coeff_2

#  Model
Ds_true = hk_gglensing_tool.get_delta_sigma(nfw_model, com_dist_len, len_z,
                                            com_dist_src[0], src_z[0],
                                            numpy.array([mean_radian]))

t1 = time.time()

result_sp = [len(pdf_bin_num), 5 * len(dilution_ratio)]
result_sub = numpy.zeros((result_sp[0], result_sp[1]))

chisq_img = Image_Plot(ypad=0.25, xpad=0.2)
chisq_img.subplots(len(pdf_bin_num), len(dilution_ratio))

for i in range(len(dilution_ratio)):

    st, ed = int(i * 5), int((i + 1) * 5)
    temp = numpy.random.choice(mgt[:total_num[i]], 100000, False)

    print(
        "Totla num: %d. src num: %d(%.2f). "
        "Dilution: %d(%.2f)." %
        (total_num[i], src_num, src_num / total_num[i], total_num[i] - src_num,
         (total_num[i] - src_num) / total_num[i]))
    for j in range(len(pdf_bin_num)):
        # result_t = hk_FQlib.find_shear_cpp(mgt_nf[idx], mnu1_nf[idx], bin_num=pdf_bin_num[j], left=-200, right=200,
        #                                    chi_gap=chi_gap,max_iters=60,fig_ax=chisq_img.axs[0][j])[:2]
        h5f["/z_hist"] = total_zhist

        h5f["/ra_bin"] = ra_hist_bin
        h5f["/dec_bin"] = dec_hist_bin
        h5f["/z_bin"] = z_hist_bin
        h5f.close()

        idx = total_pos_hist < 1
        total_pos_hist[idx] = numpy.nan
        idx = total_z_pos_hist < 1
        total_z_pos_hist[idx] = numpy.nan

        for i in range(band_num):
            st, ed = int(i * dec_bin_num), int((i + 1) * dec_bin_num)

            img = Image_Plot(xpad=0.25, ypad=0.25)
            img.subplots(1, 3)

            img.axs[0][0].imshow(numpy.flip(total_pos_hist[st:ed], axis=0))
            img.axs[0][1].imshow(numpy.flip(total_z_pos_hist[st:ed], axis=0))

            for j in range(2):
                pos_dec = [
                    0,
                    int(dec_bin_num / 4),
                    int(dec_bin_num / 2),
                    int(dec_bin_num * 3 / 4),
                    int(dec_bin_num - 1)
                ]
                img.set_ticklabel_str(0, j, 0, pos_dec,
                                      ["90", "45", "0", "-45", "-90"])
    else:
        data = numpy.row_stack((data, temp))

redshift_bin = numpy.array([0.2, 0.39, 0.58, 0.72, 0.86, 1.02, 1.3],
                           dtype=numpy.float32)
# redshift_bin = numpy.array([0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4],dtype=numpy.float32)
bin_num = len(redshift_bin) - 1

nz_bin_num = 200
zehist, zebin, zebin_cent = cf_tool.get_nz(data[:, z_col], redshift_bin,
                                           data[:, ze_col], nz_bin_num, (0, 3),
                                           False)

tag = 0

img = Image_Plot(xpad=0.2, ypad=0.2)
img.subplots(1, 1)

for i in range(bin_num):
    alpha = 1
    img.axs[0][0].plot(zebin_cent,
                       zehist[i],
                       alpha=alpha,
                       label="[%.2f~%.2f]" %
                       (redshift_bin[i], redshift_bin[i + 1]))
img.axs[0][0].legend()
img.save_img("%s/zhist.png" % dst_path)

h5f = h5py.File("%s/zhist.hdf5" % dst_path, "w")
h5f["/zhist"] = zehist
h5f["/zbin"] = zebin
        sub_num = idx.sum()
        if sub_num > 0:
            count += sub_num

            pos_angle = ic_skypos.position_angle(back_skypos[idx]).radian
            sub_data = back_data[idx]
            mgt = sub_data[:, 0] * numpy.cos(pos_angle * 2) - sub_data[:, 1] * numpy.sin(pos_angle * 2)
            mnut = sub_data[:, 2] + sub_data[:, 3] * numpy.cos(4 * pos_angle) - sub_data[:, 4] * numpy.sin(4 * pos_angle)
            Gts.extend(mgt.tolist())
            NUts.extend(mnut.tolist())
    print(file_tag, count)
mgt = numpy.array(Gts)
mnut = numpy.array(NUts)

print(mgt.shape)

gh, gh_sig = fq.find_shear(mgt, mnut, bin_num)[:2]
print(ir, gh, gh_sig)

mg_bins_spec = fq.set_bin(mgt, bin_num, 1.1)

chi_sq_spec = numpy.array([fq.get_chisq(mgt, mnut, g_hat, mg_bins_spec, bin_num2, inverse, 0) for g_hat in pdf_guess])
chi_sq = numpy.array([fq.get_chisq(mgt, mnut, g_hat, mg_bins, bin_num2, inverse, 0) for g_hat in pdf_guess])

img = Image_Plot()
img.subplots(1, 1)
img.axs[0][0].plot(pdf_guess, chi_sq_spec)
img.axs[0][0].plot(pdf_guess, chi_sq)
img.save_img("./raidus_%d.png"%ir)
# img.show_img()
# img.close_img()
t1 = time.time()

guess_num_p = 200
signal_guess = numpy.zeros((guess_num_p + guess_num_p, ))
signal_guess[:guess_num_p] = -hk_tool_box.set_bin_log(0.001, 200, guess_num_p)
signal_guess[guess_num_p:] = hk_tool_box.set_bin_log(0.001, 200, guess_num_p)
signal_guess = numpy.sort(signal_guess)

for tt in range(1):
    for i in my_radius_bin_tag:
        idx1 = separation_radius >= radius_bin[i]
        idx2 = separation_radius < radius_bin[i + 1]
        idx = idx1 & idx2

        chisq_img = Image_Plot()
        chisq_img.subplots(4, 3)

        for j in range(len(pdf_bin_num)):
            # result_t = hk_FQlib.find_shear_cpp(mgt_nf[idx], mnu1_nf[idx], bin_num=pdf_bin_num[j], left=-200, right=200,
            #                                    chi_gap=chi_gap,max_iters=60,fig_ax=chisq_img.axs[0][j])[:2]
            # result_x = hk_FQlib.find_shear_cpp(mgx_nf[idx], mnu2_nf[idx], bin_num=pdf_bin_num[j], left=-200, right=200,
            #                                    chi_gap=chi_gap,max_iters=60,fig_ax=chisq_img.axs[1][j])[:2]

            temp = numpy.random.choice(mgt_nf, 100000, False)
            pdf_bin = hk_FQlib.set_bin_(temp, pdf_bin_num[j], scale=100000)

            result_t = hk_FQlib.find_shear_cpp_guess(
                mgt_nf[idx],
                mnu1_nf[idx],
                pdf_bin,
        # !!!! remember the data type, MPI.DOUBLE, MPI.FLOAT, ...
        # or it will raise an error, Keyerror
        comm.Send([ds_result, MPI.DOUBLE], dest=0, tag=rank)
    else:

        # receive the data from other CPUs
        # !!!! the start points is 1 in range() not 0
        for procs in range(1, numprocs):
            # prepare a buffer for the data, the shape must be the same
            # with that of what the other CPUs send, you have collected them in 'data_sps'
            recvs = numpy.empty(ds_shape, dtype=numpy.double)
            # receive it using the buffer,
            comm.Recv(recvs, source=procs, tag=procs)
            ds_result += recvs

        img = Image_Plot(xpad=0.3, ypad=0.25)
        img.subplots(1, 2)
        img.axs[0][0].plot(ds_result[1], ds_result[2], label="Pure src signal")
        img.axs[0][0].plot(ds_result[3], ds_result[4], label="True signal at the mixed radius")

        img.axs[0][0].errorbar(ds_result[3], ds_result[5], ds_result[6], capsize=3, label="ori")
        img.axs[0][0].errorbar(ds_result[3], ds_result[7], ds_result[8], capsize=3, label="weight")

        img.axs[0][1].errorbar(ds_result[3], ds_result[5] - ds_result[4], ds_result[6], capsize=3, label="ori")
        img.axs[0][1].errorbar(ds_result[3], ds_result[7] - ds_result[4], ds_result[8], capsize=3, label="weight")

        img.axs[0][1].plot(ds_result[3], ds_result[4]*ds_result[0], label="Should Be")

        img.axs[0][0].set_yscale("log")
        for i in range(2):
            img.axs[0][i].set_xscale("log")
Exemple #10
0
bias_approx = get_bias_approx(sigma_z, back_dz, pz_m_norm, zpts_mid, len_z, CF)
bias = get_bias(sigma_z, back_dz, pz_m_norm, zpts_mid, len_z, CF)

src_num = src_et.shape[0]

sigma_crit = CF.get_sigma_crit(src_z)
src_ds = src_et * sigma_crit

sigma_crit_m = CF.get_sigma_crit(src_z_m)
src_ds_m = src_et * sigma_crit_m

sep_arcsec = src_radian[idx_z].mean() / numpy.pi * 180 * 3600
sep_arcsec_m = src_radian[idx_zm].mean() / numpy.pi * 180 * 3600

ds_true = CF.get_shear(sep_arcsec, 0, src_z[idx_z].mean())[2]
img = Image_Plot()
img.subplots(1, 1)

#     ds1, ds1_err = hk_FQlib.find_shear_cpp(src_ds_m[idx_zm], numpy.ones_like(src_ds_m)[idx_zm],
#                                                                    numpy.ones_like(src_ds_m)[idx_zm], 10,
#                                                                    scale =1, left=-100, right=300,chi_gap=20., max_iters=50)[:2]
#     print("True: %.4f. Measured: %.4f(%.4f). Ratio: %.4f. Expected Bias: %.4f %.4f"%(ds_true, ds1, ds1_err, ds1/ds_true, bias, bias_approx))
ds2, ds2_err = hk_FQlib.find_shear_cpp(src_et[idx_zm], numpy.ones_like(src_et[idx_zm]) / sigma_crit_m[idx_zm],
                                       numpy.ones_like(src_et[idx_zm]), 8,
                                       fit_scale=1., left=-200, right=500, chi_gap=20., max_iters=50,
                                       fig_ax=img.axs[0][0])[:2]
print("True: %.4f. Measured: %.4f(%.4f). Ratio: %.4f(%.4f). Expected Bias: %.4f %.4f" % (
ds_true, ds2, ds2_err, ds2 / ds_true, ds2_err / ds_true, bias, bias_approx))
img.show_img()
h5f = h5py.File(param_path + "/ratio_sig_%s" % files[ii], "w")
h5f["/%d/result" % radius_tag] = numpy.array([ds_true, ds2, ds2_err, ds2 / ds_true, ds2_err / ds_true, bias, bias_approx])
Exemple #11
0
    h5f["/xi_pm"] = xi_pm
    h5f["/cov_xi_pm"] = cov_pm
    h5f["/inv_cov_xi_pm"] = inv_cov_pm

    h5f["/used_data_pts"] = used_data_pts
    h5f["/used_zbin"] = used_zbins
    h5f.close()

    print(result_path)

for i in range(len(cov)):

    img = Image_Plot(fig_x=4,
                     fig_y=3,
                     xpad=0.15,
                     ypad=0.15,
                     axis_linewidth=2,
                     plt_line_width=3,
                     legend_size=25,
                     xy_tick_size=25)
    img.subplots(3, 3)
    img.set_style()

    for j in range(3):
        for k in range(3):
            if k == 2:
                arr = cov[i][j][0]
                cov_scale = numpy.zeros_like(arr)
                y, x = arr.shape
                print(y, x)
                for p in range(y):
                    for q in range(x):
Exemple #12
0
theta_err = theta_jkf[:jkf_num].std(axis=0) * numpy.sqrt(jkf_num - 1)

pts_num = delta_sigmat.shape[0]
results = numpy.zeros((pts_num, 7))
results[:, 0] = radius
results[:, 1] = radius_err
results[:, 2] = delta_sigmat
results[:, 3] = delta_sigmat_err
results[:, 4] = delta_sigmax
results[:, 5] = delta_sigmax_err
results[:, 6] = dilution_ratio

header = "Radius\tRadius_err\tDelta_Sigma\tDelta_Sigma_err\tDelta_Sigma_x\tDelta_Sigma_x_err\tDilution_ratio"
numpy.savetxt(data_path + "/%s.txt" % file_name, X=results, header=header)

img = Image_Plot(xpad=0.3, ypad=0.15)
img.subplots(1, 2)
img.axs[0][0].errorbar(radius,
                       delta_sigmat,
                       delta_sigmat_err,
                       fmt=" ",
                       marker="s",
                       capsize=3)
img.axs[0][0].errorbar(zhang_radius,
                       zhang_esd,
                       zhang_esd_err,
                       fmt=" ",
                       marker="s",
                       capsize=3)
# img.axs[0][1].errorbar(radius, delta_sigmat*radius, delta_sigmat_err*radius, fmt=" ", marker="s", capsize=3)
# img.axs[0][2].errorbar(radius, delta_sigmax, delta_sigmax_err, fmt=" ", marker="s", capsize=3)