示例#1
0
def test_fitting_m101():

    # read in the spectrum
    spectrumfile = "M101_Nucleus_irs.ipac"
    obsdata = read_spectrum(spectrumfile)

    # setup the model
    packfile = "scipack_ExGal_SpitzerIRSSLLL.ipac"
    pmodel = initialize_model(packfile, obsdata, estimate_start=True)

    # fit the spectrum
    obsfit = fit_spectrum(obsdata, pmodel, maxiter=1000)

    # fmt: off
    expvals = np.array([
        6.58721942e-12, 5.00000000e+03, 1.90661338e-08, 3.00000000e+02,
        0.00000000e+00, 2.00000000e+02, 1.26333793e-06, 1.35000000e+02,
        3.51105886e-05, 9.00000000e+01, 1.36345070e-03, 6.50000000e+01,
        0.00000000e+00, 5.00000000e+01, 0.00000000e+00, 4.00000000e+01,
        1.95762968e-01, 3.50000000e+01, 0.00000000e+00, 5.27000000e+00,
        1.79180000e-01, 0.00000000e+00, 5.70000000e+00, 1.99500000e-01,
        3.10811829e+01, 6.22000000e+00, 1.86600000e-01, 3.72585550e+00,
        6.69000000e+00, 4.68300000e-01, 9.30119820e+00, 7.42000000e+00,
        9.34920000e-01, 3.26858957e+01, 7.60000000e+00, 3.34400000e-01,
        3.20674900e+01, 7.85000000e+00, 4.16050000e-01, 8.42546582e+00,
        8.33000000e+00, 4.16500000e-01, 2.48336838e+01, 8.61000000e+00,
        3.35790000e-01, 0.00000000e+00, 1.06800000e+01, 2.13600000e-01,
        2.63819267e+01, 1.12300000e+01, 1.34760000e-01, 3.28095146e+01,
        1.13300000e+01, 3.62560000e-01, 9.18251459e+00, 1.19900000e+01,
        5.39550000e-01, 2.04774690e+01, 1.26200000e+01, 5.30040000e-01,
        9.14042121e+00, 1.26900000e+01, 1.64970000e-01, 8.17467538e+00,
        1.34800000e+01, 5.39200000e-01, 1.49795930e+00, 1.40400000e+01,
        2.24640000e-01, 7.30127345e+00, 1.41900000e+01, 3.54750000e-01,
        0.00000000e+00, 1.59000000e+01, 3.18000000e-01, 1.58347389e+01,
        1.64500000e+01, 2.30300000e-01, 2.03825236e+01, 1.70400000e+01,
        1.10760000e+00, 8.79317728e+00, 1.73750000e+01, 2.08500000e-01,
        1.39420787e+00, 1.78700000e+01, 2.85920000e-01, 1.42928360e+00,
        1.89200000e+01, 3.59480000e-01, 2.63217124e+01, 3.31000000e+01,
        1.65500000e+00, 0.00000000e+00, 5.51356156e+00, 2.08185512e-02,
        0.00000000e+00, 6.10881078e+00, 2.02547771e-02, 2.12362756e+00,
        6.91319108e+00, 2.02547771e-02, 3.26303342e+00, 8.07580000e+00,
        4.67091295e-02, 0.00000000e+00, 9.67180375e+00, 4.67091295e-02,
        0.00000000e+00, 1.22875692e+01, 4.65865366e-02, 1.24259925e+01,
        1.70048925e+01, 5.35031847e-02, 1.25723210e+01, 2.81707000e+01,
        1.58811040e-01, 1.03933808e+01, 6.98871285e+00, 2.02547771e-02,
        0.00000000e+00, 8.98922850e+00, 4.20908173e-02, 0.00000000e+00,
        1.05170387e+01, 4.67091295e-02, 3.56991869e+01, 1.28333445e+01,
        4.67091295e-02, 5.78804484e+00, 1.55285270e+01, 6.53927813e-02,
        2.93408072e+01, 1.87403569e+01, 6.53927813e-02, 0.00000000e+00,
        2.59600000e+01, 1.58811040e-01, 0.00000000e+00, 2.59390000e+01,
        1.58811040e-01, 1.72720465e+02, 3.35300000e+01, 1.58811040e-01,
        2.55063734e+02, 3.48652000e+01, 1.29936306e-01, 0.00000000e+00
    ])
    # fmt: on

    np.testing.assert_allclose(obsfit.parameters,
                               expvals,
                               rtol=1e-6,
                               atol=1e-6)
示例#2
0
def test_fitting_m101():

    # read in the spectrum
    spectrumfile = "M101_Nucleus_irs.ipac"
    obsdata = read_spectrum(spectrumfile)

    # setup the model
    packfile = "scipack_ExGal_SpitzerIRSSLLL.ipac"
    pmodel = initialize_model(packfile, obsdata, estimate_start=True)

    # fit the spectrum
    obsfit = fit_spectrum(obsdata, pmodel, maxiter=200)

    # fmt: off
    expvals = np.array([
        0.00000000e+00, 5.00000000e+03, 3.47599860e-08, 3.00000000e+02,
        4.95838415e-09, 2.00000000e+02, 0.00000000e+00, 1.35000000e+02,
        7.23019059e-05, 9.00000000e+01, 1.01195174e-03, 6.50000000e+01,
        1.46671071e-03, 5.00000000e+01, 0.00000000e+00, 4.00000000e+01,
        2.26196791e-01, 3.50000000e+01, 3.20506257e+00, 5.27000000e+00,
        1.79180000e-01, 1.78233271e+00, 5.70000000e+00, 1.99500000e-01,
        3.53244685e+01, 6.22000000e+00, 1.86600000e-01, 3.00299533e+00,
        6.69000000e+00, 4.68300000e-01, 7.81921883e+00, 7.42000000e+00,
        9.34920000e-01, 3.43758753e+01, 7.60000000e+00, 3.34400000e-01,
        3.33817792e+01, 7.85000000e+00, 4.16050000e-01, 6.84681209e+00,
        8.33000000e+00, 4.16500000e-01, 2.71687219e+01, 8.61000000e+00,
        3.35790000e-01, 6.56935555e-01, 1.06800000e+01, 2.13600000e-01,
        3.16704463e+01, 1.12300000e+01, 1.34760000e-01, 3.54974660e+01,
        1.13300000e+01, 3.62560000e-01, 8.53755133e+00, 1.19900000e+01,
        5.39550000e-01, 1.93502653e+01, 1.26200000e+01, 5.30040000e-01,
        7.58851547e+00, 1.26900000e+01, 1.64970000e-01, 7.91686821e+00,
        1.34800000e+01, 5.39200000e-01, 1.44924963e+00, 1.40400000e+01,
        2.24640000e-01, 7.38257692e+00, 1.41900000e+01, 3.54750000e-01,
        0.00000000e+00, 1.59000000e+01, 3.18000000e-01, 1.52027196e+01,
        1.64500000e+01, 2.30300000e-01, 2.72997411e+01, 1.70400000e+01,
        1.10760000e+00, 8.18088143e+00, 1.73750000e+01, 2.08500000e-01,
        2.91838398e+00, 1.78700000e+01, 2.85920000e-01, 4.48597325e+00,
        1.89200000e+01, 3.59480000e-01, 2.33160787e+01, 3.31000000e+01,
        1.65500000e+00, 8.67192733e-01, 5.52624727e+00, 1.12526539e-02,
        1.23767318e+01, 6.10984747e+00, 1.12526539e-02, 5.48881885e+00,
        6.89947186e+00, 1.13265497e-02, 1.46346475e+00, 8.03873301e+00,
        4.38292984e-02, 1.70893220e+00, 9.68075948e+00, 6.36942675e-02,
        0.00000000e+00, 1.23285000e+01, 6.36942675e-02, 1.10069789e+01,
        1.70022207e+01, 3.88847916e-02, 1.30109782e+01, 2.81707000e+01,
        2.16560510e-01, 2.19719193e+01, 6.98752346e+00, 1.23509488e-02,
        0.00000000e+00, 8.98627316e+00, 4.52465026e-02, 3.44832523e+00,
        1.05068705e+01, 2.12314225e-02, 3.10265440e+01, 1.28296867e+01,
        6.36942675e-02, 0.00000000e+00, 1.55050000e+01, 8.91719745e-02,
        3.27041174e+01, 1.87321438e+01, 6.86322478e-02, 0.00000000e+00,
        2.59600000e+01, 2.16560510e-01, 0.00000000e+00, 2.59390000e+01,
        2.16560510e-01, 1.40647197e+02, 3.35300000e+01, 2.16560510e-01,
        3.07805696e+02, 3.48652000e+01, 8.96710558e-02, 5.93764689e-01
    ])
    # fmt: on

    np.testing.assert_allclose(obsfit.parameters,
                               expvals,
                               rtol=1e-6,
                               atol=1e-6)
示例#3
0
def main():

    # setup and parse the command line
    parser = initialize_parser()
    args = parser.parse_args()

    # read in the spectrum
    obsdata = read_spectrum(args.spectrumfile)

    # setup the model
    pmodel = initialize_model(args.packfile, obsdata,
                              not args.no_starting_estimate)

    # fit the spectrum
    obsfit = fit_spectrum(obsdata, pmodel, maxiter=args.fit_maxiter)

    # save fit results to file
    outputname = args.spectrumfile.split(".")[0]
    pmodel.save(obsfit, outputname, args.saveoutput)

    # plot result
    fontsize = 18
    font = {"size": fontsize}
    mpl.rc("font", **font)
    mpl.rc("lines", linewidth=2)
    mpl.rc("axes", linewidth=2)
    mpl.rc("xtick.major", size=5, width=1)
    mpl.rc("ytick.major", size=5, width=1)
    mpl.rc("xtick.minor", size=3, width=1)
    mpl.rc("ytick.minor", size=3, width=1)

    fig, axs = plt.subplots(
        ncols=1,
        nrows=2,
        figsize=(15, 10),
        gridspec_kw={"height_ratios": [3, 1]},
        sharex=True,
    )

    pmodel.plot(
        axs,
        obsdata["x"],
        obsdata["y"],
        obsdata["unc"],
        obsfit,
        scalefac_resid=args.scalefac_resid,
    )

    # use the whitespace better
    fig.subplots_adjust(hspace=0)

    # show
    if args.showplot:
        plt.show()
    # save (always)
    fig.savefig("{}.{}".format(outputname, args.savefig))
示例#4
0
def test_classic_pack():
    # first use the static code to generate the feature dictonaries
    # instrument pack
    spitzer_sl_ll_pack = InstPackSpitzerIRSSLLL()
    # science pack
    sci_pack = SciPackExGal(spitzer_sl_ll_pack)
    oparam_info = (sci_pack.bb_info, sci_pack.dust_features,
                   sci_pack.h2_features, sci_pack.ion_features,
                   sci_pack.att_info)

    # now read in the equivalent info from a file

    # read in the spectrum
    spectrumfile = "M101_Nucleus_irs.ipac"
    obsdata = read_spectrum(spectrumfile)

    # setup the model
    packfile = "scipack_ExGal_SpitzerIRSSLLL.ipac"
    pmodel = initialize_model(packfile, obsdata)
    nparam_info = pmodel.param_info

    # check the different dictonaries are equivalent
    for k in range(len(oparam_info)):
        for ckey in oparam_info[k].keys():
            if isinstance(oparam_info[k][ckey][0], tuple):
                # loop over each tuple and test
                # complicated as tuple and has None values
                assert len(oparam_info[k][ckey]) == len(nparam_info[k][ckey])
                for i in range(len(oparam_info[k][ckey])):
                    o1, o2 = oparam_info[k][ckey][i]
                    n1, n2 = nparam_info[k][ckey][i]
                    if (o1 is not None) and (n1 is not None):
                        np.testing.assert_allclose(o1, n1)
                    else:
                        assert o1 == n1
                    if (o2 is not None) and (n2 is not None):
                        np.testing.assert_allclose(o2, n2)
                    else:
                        assert o2 == n2
            elif isinstance(oparam_info[k][ckey][0], float):
                np.testing.assert_allclose(oparam_info[k][ckey],
                                           nparam_info[k][ckey])
            else:
                np.testing.assert_equal(oparam_info[k][ckey],
                                        nparam_info[k][ckey])
示例#5
0
def main():

    # commandline parser
    parser = initialize_parser()
    args = parser.parse_args()

    # read in the spectrum
    obsdata = read_spectrum(args.spectrumfile)

    # setup the model
    pmodel = initialize_model(args.fitfilename, obsdata, estimate_start=False)

    # plot result
    fontsize = 18
    font = {"size": fontsize}
    mpl.rc("font", **font)
    mpl.rc("lines", linewidth=2)
    mpl.rc("axes", linewidth=2)
    mpl.rc("xtick.major", size=5, width=1)
    mpl.rc("ytick.major", size=5, width=1)
    mpl.rc("xtick.minor", size=3, width=1)
    mpl.rc("ytick.minor", size=3, width=1)

    fig, axs = plt.subplots(ncols=1, nrows=2, figsize=(15, 10),
                            gridspec_kw={'height_ratios': [3, 1]},
                            sharex=True)

    pmodel.plot(axs, obsdata["x"], obsdata["y"], obsdata["unc"], pmodel.model, scalefac_resid=args.scalefac_resid)

    # use the whitespace better
    fig.subplots_adjust(hspace=0)

    # show or save
    outputname = args.spectrumfile.split(".")[0]
    if args.savefig:
        fig.savefig("{}.{}".format(outputname, args.savefig))
    else:
        plt.show()