Exemplo n.º 1
0
 def from_tree_transform(cls, node, ctx):
     return functional_models.Sersic2D(amplitude=node['amplitude'],
                                       r_eff=node['r_eff'],
                                       n=node['n'],
                                       x_0=node['x_0'],
                                       y_0=node['y_0'],
                                       ellip=node['ellip'],
                                       theta=node['theta'])
Exemplo n.º 2
0
def prob3b():
    #image is already cropped, so no need for a true cutout (just use the whole data payload)
    data = fits.getdata("../res/ngc5055.fits")
    pa = 100.0  #position angle
    max_count_core = 23755
    #ellipticity = (425.8-202.6)/425.8  #roughly 61 deg

    ellipticity = (425.44 - 279.89) / 425.44  #roughly 49 deg

    h_init = int(
        dist(450., 450., 295., 410.)
    )  #using contours on ds9 image, approx location of center counts to 1/e
    #this is the center (450,450) to one edge(295,410)
    best_h = 259.63  #308.92 #309. #half-light radius ... r_1/2 for r_e = r_1/2 / ln(2) ~~ 445.68 pixels

    x, y = np.meshgrid(np.arange(data.shape[1]), np.arange(
        data.shape[0]))  #in ds9, y is 1st index, x is second

    #minimize the residual to fit h?

    #
    # d = data[288:594,168:640]
    # max_count_core = np.max(d)
    # best_h = h_init
    # best_chi2 = 9e99
    # for h in range(h_init+90,h_init+120):
    #     model = models.Sersic2D(amplitude=1, r_eff=h,n=1.0,
    #                             x_0=len(x)/2.,y_0=len(y)/2.,
    #                             ellip=ellipticity,theta=(pa-90.)*np.pi/180.)
    #     img = model(x, y)[288:594,168:640]
    #     img *= max_count_core/np.max(img)
    #
    #     chi2, scale = chi_sqr2D(d, img, None)
    #     print (chi2,h)
    #
    #     if chi2 < best_chi2:
    #         best_chi2 = chi2
    #         best_h = h
    #
    # print("Best",best_chi2,best_h)

    #amplitude = surface brightness at r_eff not at center, but still okay, since scaling up afterward so the core
    # counts match

    #refine from 309
    # h_init = 309.
    # h_init = 260.
    # d = data[288:594,168:640]
    # max_count_core = np.max(d)
    # best_h = h_init
    # best_chi2 = 9e99
    # for deci in range(-100,101):
    #     h = float(h_init)+float(deci)/100.
    #     model = models.Sersic2D(amplitude=1, r_eff=h,n=1.0,
    #                             x_0=len(x)/2.,y_0=len(y)/2.,
    #                             ellip=ellipticity,theta=(pa-90.)*np.pi/180.)
    #     img = model(x, y)[288:594,168:640]
    #     img *= max_count_core/np.max(img)
    #
    #     chi2, scale = chi_sqr2D(d, img, None)
    #     print (chi2,h)
    #
    #     if chi2 < best_chi2:
    #         best_chi2 = chi2
    #         best_h = h
    #
    # print("Best",best_chi2,best_h)

    #best_h = 259.63

    # # fit scale
    #d = data[288:594,168:640]
    # max_count_core = np.max(d)
    # best_h = h_init
    # best_chi2 = 9e99
    # best_scale = 4459
    # for sc in range(best_scale-100,best_scale+100):
    #     model = models.Sersic2D(amplitude=sc, r_eff=best_h,n=1.0,
    #                             x_0=len(x)/2.,y_0=len(y)/2.,
    #                             ellip=ellipticity,theta=(pa-90.)*np.pi/180.)
    #     img = model(x, y)[288:594,168:640]
    #
    #     chi2, scale = chi_sqr2D(d, img, None)
    #     print ("Scale", chi2,sc)
    #
    #     if chi2 < best_chi2:
    #         best_chi2 = chi2
    #         best_scale = sc
    #
    # print("Best Scale",best_chi2,best_scale)

    #try scaling
    # best_h = h_init
    # best_chi2 = 9e99
    # best_scale = 4459
    # model = models.Sersic2D(amplitude=1, r_eff=best_h, n=1.0,
    #                              x_0=len(x)/2.,y_0=len(y)/2.,
    #                              ellip=ellipticity,theta=(pa-90.)*np.pi/180.)
    # for sc in range(best_scale-100,best_scale+100):
    #     img = model(x, y)  # .swapaxes(0,1)
    #     img *= sc
    #     chi2, scale = chi_sqr2D(d, img, None)
    #     print ("Scale", chi2,sc)

    model = models.Sersic2D(amplitude=1,
                            r_eff=best_h,
                            n=1.0,
                            x_0=len(x) / 2.,
                            y_0=len(y) / 2.,
                            ellip=ellipticity,
                            theta=(pa - 90.) * np.pi / 180.)
    img = model(x, y)  # .swapaxes(0,1)
    img *= max_count_core / np.max(img)

    plt.figure(figsize=(13, 4))  # 2 cols, 1row

    plt.subplot(121)  #image
    plt.title("NGC 5055")
    plt.imshow(data, origin="lower", cmap="gray")
    cbar = plt.colorbar()
    cbar.set_label('Counts')
    plt.xlabel("pix")
    plt.ylabel("pix")

    plt.subplot(122)  #model
    plt.title(r"$\Sigma (r)$ model")
    plt.imshow(img, origin="lower", cmap="gray")
    plt.xlabel("pix")
    plt.ylabel("pix")

    cbar = plt.colorbar()
    cbar.set_label('Counts')

    plt.savefig(op.join(OUTDIR, "hw2_prob3b.png"))
    plt.show()
    return data, model, img