예제 #1
0
def shear_fit(x, y, fig_ax, label):
    coeff = tool_box.fit_1d(x, y, 2, "scipy")
    # y = a1 + a2*x a3*x^2 = a2(x+a1/2/a2)^2 +...
    # gh = - a1/2/a2, gh_sig = \sqrt(1/2/a2)
    g_h = -coeff[1] / 2. / coeff[2]
    g_sig = 0.70710678118 / numpy.sqrt(coeff[2])
    if fig_ax:
        fig_ax.scatter(x, y, alpha=0.7, s=5)
        fig_ax.plot(x,
                    coeff[0] + coeff[1] * x + coeff[2] * x**2,
                    alpha=0.7,
                    label=label)
    return g_h, g_sig, coeff
예제 #2
0
                gal_c.drawImage(image=gal_img, scale=pixel_scale)
                gal_img_arr = gal_img.array
                gal_pow = fq.pow_spec(gal_img_arr)

                mg1, mg2, mn = fq.shear_est(gal_pow, psf_pow, F=True)[:3]
                GN[0] += mg1
                GN[1] += mg2
                GN[2] += mn

        mgs[0, k] = GN[0] / GN[2]
        mgs[1, k] = GN[1] / GN[2]
    # if rank == 0:
    #     print(g1-mgs[0])
    #     print(g2-mgs[1])

    mc1 = tool_box.fit_1d(g1, mgs[0], 1, method="scipy")
    mc2 = tool_box.fit_1d(g2, mgs[1], 1, method="scipy")

    mcs[0, i, j] = mc1[1] -1 # m1
    mcs[1, i, j] = mc1[0] # c1
    mcs[2, i, j] = mc2[1]-1 # m2
    mcs[3, i, j] = mc2[0] # c2


if rank == 0:
    if ellip > 0.8:
        numpy.savez("./result/galsim/cache_%s_ellip_mix.npz"%psf_tag, mcs)
        pic_nm = "./result/galsim/mc_%s_ellip_mix.png"%psf_tag
        pic_nm_pdf = "./result/galsim/mc_%s_ellip_mix.pdf"%psf_tag
    else:
        numpy.savez("./result/galsim/cache_%s_ellip_%.2f.npz"%(psf_tag,ellip), mcs)
예제 #3
0
idx_1 = cfht_mag > 0
idx_2 = cfht_mag < 50
mag = cfht_mag[idx_1&idx_2]

bin_num = 50
nums, bins = numpy.histogram(mag, bin_num)
print(nums, nums.shape)
print(bins, bins.shape)

fit_x = bins[:bin_num]
print(fit_x, fit_x.shape)
idx_1 = fit_x <= 24
idx_2 = fit_x >= 18.5
fit_nums = numpy.log10(nums[idx_1&idx_2])
print(fit_nums.shape)
paras = tool_box.fit_1d(fit_x[idx_1&idx_2],fit_nums, 1, "lsq")
print(paras)

fmt='%2.f%%'
fig_x = 8
fig_y = fig_x*4/6
figs = (fig_x, fig_y)
fonts = 20
xy_lb_size = 18
legend_size = fonts - 4
axis_linewidth = 1.2
plt_line_width = 2
cap_size = 5
xticks = mtick.FormatStrFormatter(fmt)

fig = plt.figure(figsize=figs)
예제 #4
0
def find_shear_grid(G,
                    NU,
                    G_PDF_bin,
                    G_hist_bin,
                    NU_hist_bin,
                    chisq_gap=100,
                    dg=0.01,
                    fit_num=10,
                    ax=False):
    data_num = G.shape[0]

    bin_num = G_PDF_bin.shape[0] - 1
    bin_num2 = int(bin_num * 0.5)
    inverse = range(int(bin_num / 2 - 1), -1, -1)

    num_g = G_hist_bin.shape[0] - 1
    num_nu = NU_hist_bin.shape[0] - 1
    grid_x, grid_y = numpy.zeros(
        (num_nu, num_g), dtype=numpy.float64), numpy.zeros((num_nu, num_g),
                                                           dtype=numpy.float64)
    hist_num2d = numpy.zeros((num_nu, num_g), dtype=numpy.intc)

    hist2d_fast(G, NU, data_num, G_hist_bin, NU_hist_bin, num_g, num_nu,
                hist_num2d, grid_x, grid_y)

    iters = 0
    change = 1
    left, right = -0.1, 0.11
    while change == 1:
        change = 0
        mc = (left + right) / 2.
        mcl = left
        mcr = right
        fmc = get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, mc,
                             bin_num, bin_num2, inverse)
        fmcl = get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, mcl,
                              bin_num, bin_num2, inverse)
        fmcr = get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, mcr,
                              bin_num, bin_num2, inverse)
        #         print("%d. %.4f %.2f  %.4f %.2f  %.4f %.2f"%(iters, left, fmcl, mc,fmc,right,fmcr))
        temp = fmc + chisq_gap

        if fmcl > temp:
            #             left = (mc + mcl) / 2.
            left = mcl + (mc - mcl) / 3
            change = 1
        if fmcr > temp:
            #             right = (mc + mcr) / 2.
            right = mcr - (mcr - mc) / 3
            change = 1

        iters += 1
        if right - left < dg:
            break
        if iters > 40:
            break

    ghs = numpy.linspace(left, right, fit_num)
    xi2 = numpy.array([
        get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, gh, bin_num,
                       bin_num2, inverse) for gh in ghs
    ])
    #     print(iters, ghs)
    if ax:
        ax.plot(ghs, xi2)

    coeff = tool_box.fit_1d(ghs, xi2, 2, "scipy")
    gh = -coeff[1] / 2. / coeff[2]
    gh_sig = 0.70710678118 / numpy.sqrt(coeff[2])

    return gh, gh_sig, grid_x, grid_y, hist_num2d
num = int(argv[1])
order = int(argv[2])
percent = float(argv[3])

x = numpy.linspace(-5, 5, num)
fx = 0
coeff = numpy.random.uniform(-2, 2, order + 1)
for i in range(order + 1):
    fx += coeff[i] * (x**i)
plt.plot(x, fx, label='origin')

print("Input coeff: ", coeff)
for i in range(num):
    fx[i] = fx[i] + numpy.random.normal(0, numpy.abs(percent * fx[i]), 1)[0]

coeff, cov, fxy = tool_box.fit_1d(x, fx, order, "lsq")
coeff_s = tool_box.fit_1d(x, fx, order, "scipy")
# print(cov)
# print(fxy)
print("Lsq: ", coeff)
print("Scipy: ", coeff_s)
pic_title = "coeff: "
for i in range(order + 1):
    pic_title += "%.4f, " % coeff[i]

fx_fit = 0
for i in range(order + 1):
    fx_fit += coeff[i] * (x**i)
plt.scatter(x, fx, label="noise")
plt.plot(x, fx_fit, label="fit")
plt.title(pic_title)
예제 #6
0
                                              limited=40)

        img.axs[0][0].plot(g1_hat,
                           g1_chisq,
                           marker="o",
                           c="C%d" % data_tag,
                           label=labels[data_tag])
        img.axs[1][0].plot(g2_hat,
                           g2_chisq,
                           marker="o",
                           c="C%d" % data_tag,
                           label=labels[data_tag])

        if data_tag != 2:
            # estimate shear signal
            coeff = tool_box.fit_1d(g1_hat, g1_chisq, 2, "scipy")
            g1_estimate = -coeff[1] / 2. / coeff[2]
            g1_estimate_sig = 0.70710678118 / numpy.sqrt(coeff[2])
            # estimate shear signal
            coeff = tool_box.fit_1d(g2_hat, g2_chisq, 2, "scipy")
            g2_estimate = -coeff[1] / 2. / coeff[2]
            g2_estimate_sig = 0.70710678118 / numpy.sqrt(coeff[2])

            # plot chi squared
            img.axs[0][1].plot(g1_hat,
                               g1_chisq,
                               marker="o",
                               c="C%d" % data_tag,
                               label=labels[data_tag])
            ys = img.axs[0][1].set_ylim()
            # 1 sigma range
print("From CPP")
print("%.5f (%.5f), %.5f (%.5f)" % (mc1[0] - 1, mc1[1], mc1[2], mc1[3]))
# print("%.5f (%.5f), %.5f (%.5f)"%(mc1_n[0],mc1_n[1],mc1_n[2],mc1_n[3]))
print("%.5f (%.5f), %.5f (%.5f)" % (mc2[0] - 1, mc2[1], mc2[2], mc2[3]))
# print("%.5f (%.5f), %.5f (%.5f)"%(mc2_n[0],mc2_n[1],mc2_n[2],mc2_n[3]))

check_result = numpy.zeros_like(mg_l)
new_result = numpy.zeros_like(mg_l)

for i in range(shear_num):
    fit_x1 = chisq_g1[i, 20:]
    fit_y1 = chisq_g1[i, :20]
    fit_x2 = chisq_g2[i, 20:]
    fit_y2 = chisq_g2[i, :20]

    coeff = tool_box.fit_1d(fit_x1, fit_y1, 2, "scipy")
    fit_curve_g1 = coeff[0] + coeff[1] * fit_x1 + coeff[2] * fit_x1**2
    print(-coeff[1] / 2. / coeff[2])
    fit_slope_g1 = fit_y1 - fit_curve_g1
    coeff = tool_box.fit_1d(fit_x1, fit_y1 - fit_slope_g1, 2, "scipy")
    print(-coeff[1] / 2. / coeff[2])
    fit_curve_g1 = coeff[0] + coeff[1] * fit_x1 + coeff[2] * fit_x1**2
    coeff_new, slope_coeff = new_fit(fit_x1, fit_y1)
    print(coeff_new)  #,slope_coeff)
    print(coeff)

    new_fit_curve_g1 = coeff_new[
        0] + coeff_new[1] * fit_x1 + coeff_new[2] * fit_x1**2

    check_result[0, i] = -coeff[1] / 2. / coeff[2]
    check_result[1, i] = 0.70710678118 / numpy.sqrt(coeff[2])
예제 #8
0
for i in range(2):
    # shear
    chi_left = chi_data[i][:, :half_bin][:, inverse]
    chi_right = chi_data[i][:, half_bin:]
    dn = (chi_right - chi_left)**2
    dm = chi_right + chi_left
    chi = numpy.sum(dn / dm, axis=1)
    idx = chi == numpy.nan
    chi[idx] = -10
    chisq[:, i] = chi

    idx = chi >= 0
    chi_min = chi[idx].min()
    idx = chi <= chi_min + 40

    coeff = tool_box.fit_1d(gh[idx], chisq[:, i][idx], 2, "scipy")
    chisq_fit.append((chisq[:, i][idx], gh[idx], coeff))
    g_h = -coeff[1] / 2. / coeff[2]
    g_sig = 0.70710678118 / numpy.sqrt(coeff[2])
    final_signal[rank, i * 2] = g_h
    final_signal[rank, i * 2 + 1] = g_sig

    # shear*critical_surface_density
    chi_left = chi_crit_data[i][:, :half_bin][:, inverse]
    chi_right = chi_crit_data[i][:, half_bin:]
    dn = (chi_right - chi_left)**2
    dm = chi_right + chi_left
    chi = numpy.sum(dn / dm, axis=1)
    idx = chi == numpy.nan
    chi[idx] = -10
    chisq_crit[:, i] = chi
예제 #9
0
    img = Image_Plot(fig_x=14, fig_y=5, xpad=0.15, ypad=0.15)
    img.subplots(1, 1)

    for i in range(10, 7, -1):
        # for i in range(chi_guess_num-1,-1,-1):
        tag_1 = int(2 * i)
        tag_2 = tag_1 + 1

        npz = numpy.load(data_path + "/chisq_%d_%d_%s.npz" %
                         (tag_1, tag_2, data_type[j]))
        chi_guess_bin = npz["arr_0"]
        chisq_arr = npz["arr_1"][0]
        idx = chisq_arr <= 150
        ls = img.line_styles[i]

        coeff = tool_box.fit_1d(chi_guess_bin[idx], chisq_arr[idx], 2, "scipy")
        xi_err = numpy.sqrt(1 / 2. / coeff[2])
        xi = -coeff[1] / 2. / coeff[2]
        chi_measures[j][0, i] = xi
        chi_measures[j][1, i] = xi_err
        print(tag_1, tag_2, i, chi_guess[i], xi, xi_err)

        img.axs[0][0].plot(chi_guess_bin[idx],
                           chisq_arr[idx],
                           linewidth=2.2,
                           label="$\\xi^t=%.2e, \\xi^m=%.3e(%.2e)$" %
                           (chi_guess[i], xi, xi_err),
                           ls=ls[0],
                           c=ls[1])
        ys = img.axs[0][0].set_ylim()
        img.axs[0][0].plot([chi_guess[i], chi_guess[i]], [ys[0], ys[1]],
예제 #10
0
def find_shear_grid(G,
                    NU,
                    G_PDF_bin,
                    G_hist_bin,
                    NU_hist_bin,
                    chisq_gap=100,
                    dg=0.01,
                    fit_num=10):
    data_num = G.shape[0]

    bin_num = G_PDF_bin.shape[0] - 1
    bin_num2 = int(bin_num * 0.5)
    inverse = range(int(bin_num / 2 - 1), -1, -1)

    num_g = G_hist_bin.shape[0] - 1
    num_nu = NU_hist_bin.shape[0] - 1
    grid_x, grid_y = numpy.zeros(
        (num_nu, num_g), dtype=numpy.float64), numpy.zeros((num_nu, num_g),
                                                           dtype=numpy.float64)
    hist_num2d = numpy.zeros((num_nu, num_g), dtype=numpy.intc)

    hist2d_fast(G, NU, data_num, G_hist_bin, NU_hist_bin, num_g, num_nu,
                hist_num2d, grid_x, grid_y)

    iters = 0
    change = 1
    left, right = -0.1, 0.099
    while change == 1:
        change = 0
        mc = (left + right) / 2.
        mcl = left
        mcr = right
        fmc = get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, mc,
                             bin_num, bin_num2, inverse)
        fmcl = get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, mcl,
                              bin_num, bin_num2, inverse)
        fmcr = get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, mcr,
                              bin_num, bin_num2, inverse)

        temp = fmc + chisq_gap

        if fmcl > temp:
            left = (mc + mcl) / 2.
            change = 1
        if fmcr > temp:
            right = (mc + mcr) / 2.
            change = 1
        #         print(iters, left,right)
        iters += 1
        if right - left < dg:
            break
        if iters > 12:
            break

    ghs = numpy.linspace(left, right, fit_num)
    xi2 = numpy.array([
        get_chisq_grid(hist_num2d, grid_x, grid_y, G_PDF_bin, gh, bin_num,
                       bin_num2, inverse) for gh in ghs
    ])
    #     print(iters, ghs)

    #     img = Image_Plot()
    #     img.subplots(1, 1)
    #     img.axs[0][0].plot(ghs, xi2)
    #     img.show_img()

    coeff = tool_box.fit_1d(ghs, xi2, 2, "scipy")
    gh = -coeff[1] / 2. / coeff[2]
    gh_sig = 0.70710678118 / numpy.sqrt(coeff[2])

    return gh, gh_sig, grid_x, grid_y, hist_num2d