Esempio n. 1
0
        mplot.overplot()

        #set error methods, ChiSq() or LeastSq()
        #Chi square is a way to compare which profile best describes data, ie: is it more gaussian or lorentzian
        #Least Square says how good the data fits the particular model instance
        #opt - optimizers improve the fit. Monte Carlo is what I used, it is slow but it is most robust. Many options on sherpas site
        ustat = LeastSq()
        opt = MonCar()  #LevMar() #NelderMead() #

        #apply actual Fit
        f = Fit(d, mdl, stat=ustat, method=opt)
        res = f.fit()
        fplot = FitPlot()
        mplot.prepare(d, mdl)
        fplot.prepare(dplot, mplot)
        fplot.plot()

        #param_errors = f.est_errors()

        #plotting routine
        plt.plot(d.x, d.y, "c.", label="Data")
        plt.plot(d.x, mdl(d.x), linewidth=2, label="Gaussian")
        plt.legend(loc=2)
        plt.title(fits_image_filename)
        plt.xlabel("Wavelength nm")
        plt.ylabel("Normalized Intensity")
        os.chdir(r"/home/dtyler/Desktop/DocumentsDT/outputs")
        os.mkdir("images_" + fits_image_filename)
        os.chdir(r"/home/dtyler/Desktop/DocumentsDT/outputs/images_" +
                 fits_image_filename)
        plt.savefig(fits_image_filename[:-4] + "_model" + str(icorr + 1) +
Esempio n. 2
0
def fit(star_name, data, model, silent=False, breakdown=False):
    """A function that will fit a given multi-part model to a given spectrum.



    :param star_name: Name of the target star
    :type star_name: str
    :param data: Spectrum data in the form (wave, flux)
    :type data: tuple
    :param model: An unfit spectrum model
    :type model: object
    :param silent:  If true, no plots will generate, defaults to False
    :type silent: bool

    :return: model that is fit to the data
    :rtype: object


    """

    wave, flux = data

    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    d = Data1D(star_name, wave, flux)

    # ==========================================
    # Initial guesses

    # Dataset 1
    dplot = DataPlot()
    dplot.prepare(d)
    if silent is False:
        dplot.plot()

    mplot = ModelPlot()
    mplot.prepare(d, model)
    if silent is False:
        dplot.plot()
        mplot.overplot()
        plt.show()

    # =========================================
    # Fitting happens here - don't break please
    start = time.time()

    stat = LeastSq()

    opt = LevMar()

    opt.verbose = 0
    opt.ftol = 1e-15
    opt.xtol = 1e-15
    opt.gtol = 1e-15
    opt.epsfcn = 1e-15

    if silent is False:
        print(opt)

    vfit = Fit(d, model, stat=stat, method=opt)

    if silent is False:
        print(vfit)

    vres = vfit.fit()

    if silent is False:
        print()
        print()
        print(vres.format())

    # =========================================
    # Plotting after fit

    # Dataset 1
    if silent is False:
        fplot = FitPlot()
        mplot.prepare(d, model)
        fplot.prepare(dplot, mplot)
        fplot.plot()

        # residual
        plt.title(star_name)
        plt.plot(wave, flux - model(wave))

        # plt.xaxis(fontsize = )
        plt.xlabel("Wavelength (AA)", fontsize=12)
        plt.ylabel("Flux", fontsize=12)
        plt.tick_params(axis="both", labelsize=12)

    if silent is False:
        duration = time.time() - start
        print()
        print("Time taken: " + str(duration))
        print()

    plt.show()

    if breakdown is True:
        params = []

        cont = model[0]

        if silent is False:
            plt.scatter(wave, flux, marker=".", c="black")
            plt.plot(wave, model(wave), c="C1")

        for line in model:
            if line.name[0] != "(":
                if line.name == "Cont_flux":
                    if silent is False:
                        print(line)
                        plt.plot(wave, line(wave), linestyle="--")
                else:
                    params.append(line)
                    if silent is False:
                        print()
                        print(line)
                        plt.plot(wave, line(wave) * cont(wave), linestyle="--")

        plt.show()

        return model, params

    return model
Esempio n. 3
0
mdl.pars[2].val = 22
mdl.pars[3].val = 10
report("mdl")

from sherpa.plot import ModelPlot
mplot = ModelPlot()
mplot.prepare(d, mdl)
mplot.plot()
dplot.overplot()

savefig('modelplot_histogram_overplot.png')

from sherpa.plot import FitPlot
fplot = FitPlot()
fplot.prepare(dplot, mplot)
fplot.plot()

savefig('fitplot_histogram.png')

# settings

report("dplot.plot_prefs")

dplot.plot_prefs['ylog'] = True
dplot.plot(marker='s', linestyle='dashed')

savefig('settings_dataplot_combined.png')

dplot.plot()
savefig('settings_dataplot_ylog.png')
Esempio n. 4
0
def multifit(star_name, data_list, model_list, silent=False):
    """A function that will fit 2 models to 2 spectra simultaneously.
        This was created to fit the NaI doublets at ~3300 and ~5890 Angstroms.

    :param star_name: Name of the target star
    :type star_name: str
    :param data_list: List of spectrum data in the form [(wave, flux), (wave, flux),...]
    :type data_list: tuple
    :param model_list:  A list of unfit spectrum models
    :type model_list: list
    :param silent:  If true, no plots will generate, defaults to False
    :type silent: bool

    :return: models that are fit to the data
    :rtype: list

    """

    wave1, flux1 = data_list[0]
    wave2, flux2 = data_list[1]

    model1 = model_list[0]
    model2 = model_list[1]

    name_1 = star_name + " 1"
    name_2 = star_name + " 2"

    d1 = Data1D(name_1, wave1, flux1)
    d2 = Data1D(name_2, wave2, flux2)

    dall = DataSimulFit("combined", (d1, d2))
    mall = SimulFitModel("combined", (model1, model2))

    # # ==========================================
    # # Initial guesses

    # Dataset 1
    dplot1 = DataPlot()
    dplot1.prepare(d1)
    if silent is False:
        dplot1.plot()

    mplot1 = ModelPlot()
    mplot1.prepare(d1, model1)
    if silent is False:
        dplot1.plot()
        mplot1.overplot()
        plt.show()

        # Dataset 2
    dplot2 = DataPlot()
    dplot2.prepare(d2)
    if silent is False:
        dplot2.plot()

    mplot2 = ModelPlot()
    mplot2.prepare(d2, model2)
    if silent is False:
        dplot2.plot()
        mplot2.overplot()
        plt.show()

    # # =========================================
    # # Fitting happens here - don't break please
    stat = LeastSq()

    opt = LevMar()
    opt.verbose = 0
    opt.ftol = 1e-15
    opt.xtol = 1e-15
    opt.gtol = 1e-15
    opt.epsfcn = 1e-15
    print(opt)

    vfit = Fit(dall, mall, stat=stat, method=opt)
    print(vfit)
    vres = vfit.fit()

    print()
    print()
    print("Did the fit succeed? [bool]")
    print(vres.succeeded)
    print()
    print()
    print(vres.format())

    # # =========================================
    # # Plotting after fit
    if silent is False:
        # Dataset 1
        fplot1 = FitPlot()
        mplot1.prepare(d1, model1)
        fplot1.prepare(dplot1, mplot1)
        fplot1.plot()

        # residual
        title = "Data 1"
        plt.title(title)
        plt.plot(wave1, flux1 - model1(wave1))
        plt.show()

        # Dataset 2
        fplot2 = FitPlot()
        mplot2.prepare(d2, model2)
        fplot2.prepare(dplot2, mplot2)
        fplot2.plot()

        # residual
        title = "Data 2"
        plt.title(title)
        plt.plot(wave2, flux2 - model2(wave2))
        plt.show()

        # both datasets - no residuals
        splot = SplitPlot()
        splot.addplot(fplot1)
        splot.addplot(fplot2)

        plt.tight_layout()
        plt.show()

    return model_list
Esempio n. 5
0
from sherpa.plot import DelchiPlot
residplot = DelchiPlot()
residplot.prepare(d, mdl, f.stat)
residplot.plot()
savefig("model_data_delchi.png")

d.notice()
dump("d.get_filter(format='%d')")

from sherpa.plot import FitPlot
fitplot = FitPlot()
dplot.prepare(d)
mplot.prepare(d, mdl)
fitplot.prepare(dplot, mplot)
fitplot.plot()
savefig("model_data_fit_all.png")

# do we get an error? Actually, it looks to not be the divide-by-zero
# being the problem but list/list instead:
#
"""
residplot.prepare(d, mdl, f.stat)

/home/djburke/miniconda2/envs/sherpa410-py35/lib/python3.5/site-packages/sherpa-4.10.0-py3.5-linux-x86_64.egg/sherpa/plot/__init__.py:1128: RuntimeWarning: divide by zero encountered in true_divide
  return (ylist[0] - ylist[1]) / staterr
Traceback (most recent call last):
  File "example.py", line 125, in <module>
    residplot.prepare(d, mdl, f.stat)
  File "/home/djburke/miniconda2/envs/sherpa410-py35/lib/python3.5/site-packages/sherpa-4.10.0-py3.5-linux-x86_64.egg/sherpa/plot/__init__.py", line 1140, in prepare
    self.yerr = staterr / staterr