Beispiel #1
0
    def test_cache_copy(self):
        # fake up a PHA data set
        chans = numpy.arange(1, 11, dtype=numpy.int8)
        counts = numpy.ones(chans.size)

        # bin width is not 0.1 but something slightly different
        ebins = numpy.linspace(0.1, 1.2, num=chans.size + 1)
        elo = ebins[:-1]
        ehi = ebins[1:]

        dset = ui.DataPHA('test', chans, counts)

        # make sure ARF isn't 1 to make sure it's being applied
        arf = ui.create_arf(elo, ehi, specresp=0.7 * numpy.ones(chans.size))

        rmf = ui.create_rmf(elo, ehi, e_min=elo, e_max=ehi)

        ui.set_data(1, dset)
        ui.set_arf(1, arf)
        ui.set_rmf(1, rmf)

        ui.set_source(ui.const1d.mdl)

        # again not 1
        mdl.c0 = 8


        # Copy the values from the plot structures, since get_xxx_plot
        # returns the same object so m1.y == m2.y will not note a difference.
        #

        d1y = ui.get_data_plot().y.copy()
        m1y = ui.get_model_plot().y.copy()
        s1y = ui.get_source_plot().y.copy()

        d2y = ui.get_data_plot().y.copy()
        m2y = ui.get_model_plot().y.copy()
        s2y = ui.get_source_plot().y.copy()
        rtol = 1.0e-4
        atol = 1.0e-4
        numpy.testing.assert_allclose(d1y, d2y, rtol, atol)
        numpy.testing.assert_allclose(m1y, m2y, rtol, atol)
        numpy.testing.assert_allclose(s1y, s2y, rtol, atol)
Beispiel #2
0
def _get_plot_data(ids, emin, emax):
    all_model = []
    all_emodel = []
    all_data = []
    all_dataxerr = []
    all_datayerr = []
    all_edata = []
    all_ratio = []
    all_ratioerr = []

    # Get data and model for each spectrum
    for sid in ids:
        d = shp.get_data_plot(sid)
        m = shp.get_model_plot(sid)
        e = (m.xhi + m.xlo) / 2
        bins = np.concatenate((d.x - d.xerr / 2, [d.x[-1] + d.xerr[-1]]))

        model = m.y
        model_de = model * (m.xhi - m.xlo)
        model_binned, foo1, foo2 = binned_statistic(
            e, model_de, bins=bins, statistic="sum"
        )
        model_binned = model_binned / d.xerr

        # delchi = resid/d.yerr
        ratio = d.y / model_binned

        mask_data = np.logical_and(d.x + d.xerr / 2 >= emin, d.x - d.xerr / 2 <= emax)
        mask_model = np.logical_and(e >= emin, e <= emax)

        all_model.append(model[mask_model])
        all_emodel.append(e[mask_model])
        all_data.append(d.y[mask_data])
        all_dataxerr.append(d.xerr[mask_data])
        all_datayerr.append(d.yerr[mask_data])
        all_edata.append(d.x[mask_data])
        all_ratio.append(ratio[mask_data])
        all_ratioerr.append(d.yerr[mask_data] / model_binned[mask_data])

    return (
        all_model,
        all_emodel,
        all_data,
        all_dataxerr,
        all_datayerr,
        all_edata,
        all_ratio,
        all_ratioerr,
    )
Beispiel #3
0
def qq_export(id=None, bkg=False, outfile='qq.txt', elow=None, ehigh=None):
    """
	Export Q-Q plot into a file for plotting.

	:param id: spectrum id to use (see get_bkg_plot/get_data_plot)
	:param bkg: whether to use get_bkg_plot or get_data_plot
	:param outfile: filename to write results into
	:param elow: low energy limit
	:param ehigh: low energy limit

	Example::

		qq.qq_export('bg', outfile='my_bg_qq', elow=0.2, ehigh=10)

	"""
    # data
    d = ui.get_bkg_plot(id=id) if bkg else ui.get_data_plot(id=id)
    e = d.x
    mask = logical_and(e >= elow, e <= ehigh)
    data = d.y[mask].cumsum()
    d = ui.get_bkg_model_plot(id=id) if bkg else ui.get_model_plot(id=id)
    e = d.xlo
    mask = logical_and(e >= elow, e <= ehigh)
    e = e[mask]
    model = d.y[mask].cumsum()
    last_stat = ui.get_stat()
    ui.set_stat(ksstat)
    ks = ui.calc_stat()
    ui.set_stat(cvmstat)
    cvm = ui.calc_stat()
    ui.set_stat(adstat)
    ad = ui.calc_stat()
    ui.set_stat(last_stat)
    ad = ui.calc_stat()

    ui.set_stat('chi2gehrels')
    chi2 = ui.calc_stat()
    ui.set_stat('cstat')
    cstat = ui.calc_stat()
    ui.set_stat(last_stat)
    stats = dict(ks=ks, cvm=cvm, ad=ad, cstat=cstat, chi2=chi2)

    numpy.savetxt(outfile, numpy.transpose([e, data, model]))
    json.dump(stats, open(outfile + '.json', 'w'), indent=4)
Beispiel #4
0
def qq_export(id=None, bkg=False, outfile='qq.txt', elow=None, ehigh=None):
	"""
	Export Q-Q plot into a file for plotting.

	:param id: spectrum id to use (see get_bkg_plot/get_data_plot)
	:param bkg: whether to use get_bkg_plot or get_data_plot
	:param outfile: filename to write results into
	:param elow: low energy limit
	:param ehigh: low energy limit

	Example::

		qq.qq_export('bg', outfile='my_bg_qq', elow=0.2, ehigh=10)
	"""
	# data
	d = ui.get_bkg_plot(id=id) if bkg else ui.get_data_plot(id=id)
	e = d.x
	mask = logical_and(e >= elow, e <= ehigh)
	data = d.y[mask].cumsum()
	d = ui.get_bkg_model_plot(id=id) if bkg else ui.get_model_plot(id=id)
	e = d.xlo
	mask = logical_and(e >= elow, e <= ehigh)
	e = e[mask]
	model = d.y[mask].cumsum()
	last_stat = ui.get_stat()
	ui.set_stat(ksstat)
	ks = ui.calc_stat()
	ui.set_stat(cvmstat)
	cvm = ui.calc_stat()
	ui.set_stat(adstat)
	ad = ui.calc_stat()
	ui.set_stat(last_stat)
	ad = ui.calc_stat()
	
	ui.set_stat('chi2gehrels')
	chi2 = ui.calc_stat()
	ui.set_stat('cstat')
	cstat = ui.calc_stat()
	ui.set_stat(last_stat)
	stats = dict(ks=ks, cvm=cvm, ad=ad, cstat=cstat, chi2=chi2)
	
	numpy.savetxt(outfile, numpy.transpose([e, data, model]))
	json.dump(stats, open(outfile + '.json', 'w'), indent=4)
def check_bad_grouping(exp_xlo, exp_xhi, exp_counts, lo1, hi1, lo2, hi2):
    """Common tests from test_grouped_pha_all_badXXX

    Sending in two ranges is a bit excessive but easiest
    thing to implement
    """

    cts = ui.get_counts()
    assert cts == pytest.approx([exp_counts])

    dplot = ui.get_data_plot()
    assert dplot.xlo == pytest.approx([exp_xlo])
    assert dplot.xhi == pytest.approx([exp_xhi])
    assert dplot.y == pytest.approx([exp_counts])

    # ignore all the data
    ui.ignore(lo1, hi1)

    # can still plot
    cts = ui.get_counts()
    assert cts == pytest.approx([exp_counts])

    cts = ui.get_counts(filter=True)
    assert len(cts) == 0

    dplot = ui.get_data_plot()
    assert len(dplot.xlo) == 0
    assert len(dplot.xhi) == 0
    assert len(dplot.y) == 0

    # ignore does not fail
    #
    ui.ignore(lo2, hi2)

    # we can restore the data
    ui.notice(None, None)

    cts = ui.get_counts()
    assert cts == pytest.approx([exp_counts])

    dplot = ui.get_data_plot()
    assert dplot.xlo == pytest.approx([exp_xlo])
    assert dplot.xhi == pytest.approx([exp_xhi])
    assert dplot.y == pytest.approx([exp_counts])

    # now ignore the bad channels (ie everything)
    #
    ui.ignore_bad()

    cts = ui.get_counts()
    assert len(cts) == 0

    dplot = ui.get_data_plot()
    assert len(dplot.xlo) == 0
    assert len(dplot.xhi) == 0
    assert len(dplot.y) == 0

    # there's nothing to notice (this line is an example of #790)
    ui.notice(lo1, hi1)

    cts = ui.get_counts()
    assert len(cts) == 0

    dplot = ui.get_data_plot()
    assert len(dplot.xlo) == 0
    assert len(dplot.xhi) == 0
    assert len(dplot.y) == 0