def test_rsp1d_matrix_pha_zero_energy_bin():
    """What happens when the first bin starts at 0, with replacement.

    Unlike test_rsp1d_delta_pha_zero_energy_bin this directly
    calls Response1D to create the model.
    """

    ethresh = 1.0e-5

    rdata = create_non_delta_rmf()

    # hack the first bin to have 0 energy
    rdata.energ_lo[0] = 0.0

    # PHA and ARF have different exposure ties
    exposure_arf = 0.1
    exposure_pha = 2.4

    specresp = create_non_delta_specresp()

    with warnings.catch_warnings(record=True) as ws:
        warnings.simplefilter("always")
        adata = create_arf(rdata.energ_lo,
                           rdata.energ_hi,
                           specresp,
                           exposure=exposure_arf,
                           ethresh=ethresh)

    validate_zero_replacement(ws, 'ARF', 'user-arf', ethresh)

    nchans = rdata.e_min.size
    channels = np.arange(1, nchans + 1, dtype=np.int16)
    counts = np.ones(nchans, dtype=np.int16)
    pha = DataPHA('test-pha',
                  channel=channels,
                  counts=counts,
                  exposure=exposure_pha)
    pha.set_rmf(rdata)
    pha.set_arf(adata)

    pha.set_analysis('energy')

    mdl = MyPowLaw1D()

    rsp = Response1D(pha)
    wrapped = rsp(mdl)

    # Evaluate the statistic / model. The value was calculated using
    # commit a65fb94004664eab219cc09652172ffe1dad80a6 on a linux
    # system (Ubuntu 17.04).
    #
    f = Fit(pha, wrapped)
    ans = f.calc_stat()
    assert ans == pytest.approx(37971.8716151947)
Exemplo n.º 2
0
def test_rsp1d_matrix_pha_zero_energy_bin():
    """What happens when the first bin starts at 0, with replacement.

    Unlike test_rsp1d_delta_pha_zero_energy_bin this directly
    calls Response1D to create the model.
    """

    ethresh = 1.0e-5

    rdata = create_non_delta_rmf()

    # hack the first bin to have 0 energy
    rdata.energ_lo[0] = 0.0

    # PHA and ARF have different exposure ties
    exposure_arf = 0.1
    exposure_pha = 2.4

    specresp = create_non_delta_specresp()

    with warnings.catch_warnings(record=True) as ws:
        warnings.simplefilter("always")
        adata = create_arf(rdata.energ_lo,
                           rdata.energ_hi,
                           specresp,
                           exposure=exposure_arf,
                           ethresh=ethresh)

    validate_zero_replacement(ws, 'ARF', 'user-arf', ethresh)

    nchans = rdata.e_min.size
    channels = np.arange(1, nchans + 1, dtype=np.int16)
    counts = np.ones(nchans, dtype=np.int16)
    pha = DataPHA('test-pha', channel=channels, counts=counts,
                  exposure=exposure_pha)
    pha.set_rmf(rdata)
    pha.set_arf(adata)

    pha.set_analysis('energy')

    mdl = MyPowLaw1D()

    rsp = Response1D(pha)
    wrapped = rsp(mdl)

    # Evaluate the statistic / model. The value was calculated using
    # commit a65fb94004664eab219cc09652172ffe1dad80a6 on a linux
    # system (Ubuntu 17.04).
    #
    f = Fit(pha, wrapped)
    ans = f.calc_stat()
    assert ans == pytest.approx(37971.8716151947)
Exemplo n.º 3
0
mdl = Polynom1D()

mdl.c2.thaw()
report("mdl")

from sherpa.fit import Fit
f = Fit(d, mdl)
report("f")
report("f.data")
report("f.model")

dump("f.model.c2.val")
mdl.c2 = 1
dump("f.model.c2.val")

print("Starting statistic: {:.3f}".format(f.calc_stat()))
sinfo1 = f.calc_stat_info()
report("sinfo1")

d.ignore(0, 5)
sinfo2 = f.calc_stat_info()
d.notice()
dump("sinfo1.numpoints")
dump("sinfo2.numpoints")

res = f.fit()
if res.succeeded: print("Fit succeeded")
if not res.succeeded: print("**** ERRRR, the fit failed folks")

report("res.format()")
report("res")
Exemplo n.º 4
0
from sherpa.stats import LeastSq
from sherpa.optmethods import NelderMead
from sherpa.fit import Fit
f = Fit(d, mdl, stat=LeastSq(), method=NelderMead())
report("print(f)")

res = f.fit()
dump("res.succeeded")

report("res.format()")
report("res")

report("mdl")

stat2 = f.calc_stat()
print("Statistic = {:.4f}".format(stat2))

from sherpa.plot import FitPlot, ResidPlot, SplitPlot
fplot = FitPlot()
mplot.prepare(d, mdl)
fplot.prepare(dplot, mplot)
splot = SplitPlot()
splot.addplot(fplot)
rplot = ResidPlot()
print("### should get a WARNING about missing errors")
rplot.prepare(d, mdl, stat=LeastSq())

rplot.plot_prefs['yerrorbars'] = False
splot.addplot(rplot)
Exemplo n.º 5
0
def prepare_spectra(group, nH, add_gal, redshift):
    pha = read_pha("core_spectrum.pi")
    pha.set_analysis("energy")
    pha.notice(0.5, 7.0)
    tabs = ~pha.mask
    pha.group_counts(group, tabStops=tabs)
    x = pha.get_x()
    x = pha.apply_filter(x, pha._middle)
    y = pha.get_y(filter=True)
    pha.set_analysis("energy")

    model = xsphabs.abs1 * powlaw1d.srcp1
    print("Fitting the spectrum")

    zFlag = False
    if (nH is not None) and (nH > 0.0):
        if add_gal == 1:
            model = xsphabs.gal * xszphabs.abs1 * powlaw1d.srcp
            gal.nH = nH
            freeze(gal.nH)
            zFlag = True

        else:
            model = xsphabs.abs1 * powlaw1d.srcp1
            abs1.nH = nH
            freeze(abs1.nH)
    else:
        model = xszphabs.abs1 * powlaw1d.srcp1
        zFlag = True

    if zFlag is True and add_gal == 1:
        # print('REDSHIFT',redshift)
        abs1.redshift = redshift
        freeze(abs1.redshift)

    full_model = RSPModelPHA(pha.get_arf(), pha.get_rmf(), pha, pha.exposure * model)

    print(full_model)

    fit = Fit(pha, full_model, method=MonCar(), stat=WStat())
    res = fit.fit()

    print(res.format())
    print(fit.est_errors())

    # calculate the p-value for wstat
    mplot2 = ModelPlot()
    mplot2.prepare(pha, full_model)

    miu = mplot2.y * pha.exposure * 0.0146
    obs = y * pha.exposure * 0.0146

    c, ce, cv = gof_cstat(miu, obs)

    print(f"C0={c},C_e={ce},C_v={cv}")

    zval = (fit.calc_stat() - ce) / np.sqrt(cv)

    if zval > 0:
        pval = special.erfc(zval / np.sqrt(2))
    else:
        pval = special.erf(abs(zval) / np.sqrt(2))

    print(f"p-value for wstat = {pval}")

    set_data(pha)
    set_model(model)
    save_chart_spectrum("core_flux_chart.dat", elow=0.5, ehigh=7.0)
    # save_chart_spectrum("core_flux_chart.rdb",format='text/tsv', elow=0.5, ehigh=7.0)
    save_spectrum_rdb("core_flux_chart.dat")
Exemplo n.º 6
0
    def prepare_spectra(nH: float,
                        group: int = 1,
                        add_gal: bool = False,
                        redshift: Optional[float] = None,
                        **kwargs) -> float:
        """
        Fit the spectra using an absorbed powerlaw model using the Wstat statistic. The function also returns a p-value for the gof.
        :param nH: The galactic absorption column density in units of 10^22 /cm3
        :param group: The number of counts per energy bin
        :param add_gal: Setting this to True would add an intrinsic abrosption column density along side the galactic one
        :param redshift: The redshift to use in the fit. Only takes effect if add_gal is set to True
        ...
        :return: Returns the p-value of the gof. The null hypothesis states that the model and the observation differ while alternate says that the model explains the data
        """

        pha = read_pha("core_spectrum.pi")
        pha.set_analysis("energy")
        pha.notice(0.5, 7.0)
        tabs = ~pha.mask
        pha.group_counts(group, tabStops=tabs)
        x = pha.get_x()
        x = pha.apply_filter(x, pha._middle)
        y = pha.get_y(filter=True)
        pha.set_analysis("energy")

        model = xsphabs.abs1 * powlaw1d.srcp1
        print("Fitting the spectrum")

        zFlag = False
        if (nH is not None) and (nH > 0.0):
            if add_gal == 1:
                model = xsphabs.gal * xszphabs.abs1 * powlaw1d.srcp
                gal.nH = nH
                freeze(gal.nH)
                zFlag = True

            else:
                model = xsphabs.abs1 * powlaw1d.srcp1
                abs1.nH = nH
                freeze(abs1.nH)
        else:
            model = xszphabs.abs1 * powlaw1d.srcp1
            zFlag = True

        if zFlag is True and add_gal == 1:
            # print('REDSHIFT',redshift)
            abs1.redshift = redshift
            freeze(abs1.redshift)

        full_model = RSPModelPHA(pha.get_arf(), pha.get_rmf(), pha,
                                 pha.exposure * model)

        print(full_model)

        fit = Fit(pha, full_model, method=MonCar(), stat=WStat())
        res = fit.fit()

        print(res.format())
        print(fit.est_errors())

        # calculate the p-value for wstat
        mplot2 = ModelPlot()
        mplot2.prepare(pha, full_model)

        miu = mplot2.y * pha.exposure * 0.0146
        obs = y * pha.exposure * 0.0146

        c, ce, cv = SpecUtils.estimate_gof_cstat(miu, obs)

        #print(f"C0={c},C_e={ce},C_v={cv}")

        zval = (fit.calc_stat() - ce) / np.sqrt(cv)

        if zval > 0:
            pval = special.erfc(zval / np.sqrt(2))
        else:
            pval = special.erf(abs(zval) / np.sqrt(2))

        print(f"p-value for wstat = {pval}")

        set_data(pha)
        set_model(model)
        save_chart_spectrum("core_flux_chart.dat", elow=0.5, ehigh=7.0)
        # save_chart_spectrum("core_flux_chart.rdb",format='text/tsv', elow=0.5, ehigh=7.0)
        SAOTraceUtils.save_spectrum_rdb("core_flux_chart.dat")

        return pval
Exemplo n.º 7
0
from sherpa.stats import LeastSq
from sherpa.optmethods import NelderMead
from sherpa.fit import Fit
f = Fit(d, mdl, stat=LeastSq(), method=NelderMead())
report("print(f)")

res = f.fit()
dump("res.succeeded")

report("res.format()")
report("res")

report("mdl")

stat2 = f.calc_stat()
print("Statistic = {:.4f}".format(stat2))


from sherpa.plot import FitPlot, ResidPlot, SplitPlot
fplot = FitPlot()
mplot.prepare(d, mdl)
fplot.prepare(dplot, mplot)
splot = SplitPlot()
splot.addplot(fplot)
rplot = ResidPlot()
print("### should get a WARNING about missing errors")
rplot.prepare(d, mdl, stat=LeastSq())

rplot.plot_prefs['yerrorbars'] = False
splot.addplot(rplot)
Exemplo n.º 8
0
mplot = ModelPlot()
mplot.prepare(d, mdl)
plt.subplot(2, 1, 1)
mplot.plot(clearwindow=False)
plt.subplot(2, 1, 2)
dplot.plot(clearwindow=False)
plt.title('')

savefig("model_data_before_fit.png")

from sherpa.stats import Chi2
from sherpa.fit import Fit
f = Fit(d, mdl, stat=Chi2())
report("f")

print("Starting statistic: {}".format(f.calc_stat()))

fitres = f.fit()
report("fitres.format()")

print("Reduced chi square = {:.2f}".format(fitres.rstat))

mplot.prepare(d, mdl)
dplot.plot()
mplot.overplot()
savefig("model_data_fit1.png")

from sherpa.optmethods import NelderMead
f.method = NelderMead()
fitres2 = f.fit()
report("mdl")