Ejemplo n.º 1
0
def test_pha_case_6(ds_setup, ds_datadir):

    datadir = ds_datadir
    ls = '@' + '/'.join((datadir, 'pha.lis'))
    rmf1 = '/'.join((datadir, "acisf04938_000N002_r0043_rmf3.fits"))
    rmf2 = '/'.join((datadir, "acisf07867_000N001_r0002_rmf3.fits"))
    arf1 = '/'.join((datadir, "acisf04938_000N002_r0043_arf3.fits"))
    arf2 = '/'.join((datadir, "acisf07867_000N001_r0002_arf3.fits"))
    datastack.load_pha(ls)

    datastack.load_bkg_rmf([], rmf1)
    datastack.load_bkg_rmf([], rmf2)

    datastack.load_bkg_arf([], arf1)
    datastack.load_bkg_arf([], arf2)

    # Define background models
    bkg_arfs = datastack.get_bkg_arf([])
    bkg_scales = datastack.get_bkg_scale([])
    bkg_models = [
        ui.const1d.c1 * acis_bkg_model('acis7s'),
        ui.const1d.c2 * acis_bkg_model('acis7s')
    ]
    bkg_rsps = datastack.get_response([], bkg_id=1)
    for i in range(2):
        id_ = i + 1
        # Make the ARF spectral response flat.  This is required for using
        # the acis_bkg_model.
        bkg_arfs[i].specresp = bkg_arfs[i].specresp * 0 + 1.
        datastack.set_bkg_full_model(id_, bkg_rsps[i](bkg_models[i]))

    # Fit background
    datastack.notice(0.5, 8.)
    datastack.set_method("neldermead")
    datastack.set_stat("cash")

    datastack.thaw(c1.c0)
    datastack.thaw(c2.c0)
    datastack.fit_bkg()
    datastack.freeze(c1.c0)
    datastack.freeze(c2.c0)

    # Define source models
    rsps = datastack.get_response([])
    src_model = ui.powlaw1d.pow1
    src_models = [src_model, src_model * ui.const1d.ratio_12]
    for i in range(2):
        id_ = i + 1
        datastack.set_full_model(id_,
                                 (rsps[i](src_models[i]) +
                                  bkg_scales[i] * bkg_rsps[i](bkg_models[i])))

    datastack.fit()
Ejemplo n.º 2
0
    def test_case_6(self):
        datadir = '/'.join((self._this_dir, 'data'))
        ls = '@'+'/'.join((datadir, 'pha.lis'))
        rmf1 = '/'.join((datadir, "acisf04938_000N002_r0043_rmf3.fits"))
        rmf2 = '/'.join((datadir, "acisf07867_000N001_r0002_rmf3.fits"))
        arf1 = '/'.join((datadir, "acisf04938_000N002_r0043_arf3.fits"))
        arf2 = '/'.join((datadir, "acisf07867_000N001_r0002_arf3.fits"))
        datastack.load_pha(ls)

        datastack.load_bkg_rmf([], rmf1)
        datastack.load_bkg_rmf([], rmf2)

        datastack.load_bkg_arf([], arf1)
        datastack.load_bkg_arf([], arf2)

        # Define background models
        bkg_arfs = datastack.get_bkg_arf([])
        bkg_scales = datastack.get_bkg_scale([])
        bkg_models = [ui.const1d.c1 * acis_bkg_model('acis7s'),
                      ui.const1d.c2 * acis_bkg_model('acis7s')]
        bkg_rsps = datastack.get_response([], bkg_id=1)
        for i in range(2):
            id_ = i + 1
            # Make the ARF spectral response flat.  This is required for using
            # the acis_bkg_model.
            bkg_arfs[i].specresp = bkg_arfs[i].specresp * 0 + 1.
            datastack.set_bkg_full_model(id_, bkg_rsps[i](bkg_models[i]))

        # Fit background
        datastack.notice(0.5, 8.)
        datastack.set_method("neldermead")
        datastack.set_stat("cash")

        datastack.thaw(c1.c0)
        datastack.thaw(c2.c0)
        datastack.fit_bkg()
        datastack.freeze(c1.c0)
        datastack.freeze(c2.c0)

        # Define source models
        rsps = datastack.get_response([])
        src_model = ui.powlaw1d.pow1
        src_models = [src_model,
                      src_model * ui.const1d.ratio_12]
        for i in range(2):
            id_ = i + 1
            datastack.set_full_model(id_, (rsps[i](src_models[i]) +
                                           bkg_scales[i] *
                                           bkg_rsps[i](bkg_models[i])))

        datastack.fit()
Ejemplo n.º 3
0
def _nH_uplimit(fixgamma, sigma_level=0.955):
    # Fix nH to 1e19 cm-2 and find the best fit
    shp.set_par("abs1.nH", val=0.001, frozen=True)
    properFit()
    dsmod.freeze("po1.PhoIndex")

    fitresult = properFit()
    chi2_min = fitresult.statval

    # Estimate the probability distribution of NH using chi2
    NHtest = np.logspace(-3, 3, num=120)
    PNHtest = np.full(len(NHtest), np.nan)
    cumNHtest = np.full(len(NHtest), np.nan)

    for i, nh in enumerate(NHtest):
        shp.set_par("abs1.nH", val=nh)
        fitresult = properFit()

        Dchi2 = fitresult.statval - chi2_min
        PNHtest[i] = np.exp(-Dchi2 / 2)
        cumNHtest[i] = np.trapz(PNHtest[:i + 1], NHtest[:i + 1])

    # Normalize
    Pnorm = 1 / np.trapz(PNHtest, NHtest)
    C = Pnorm * cumNHtest
    nH = 10**np.interp(np.log10(sigma_level),
                       np.log10(C),
                       np.log10(NHtest),
                       left=0,
                       right=1)

    #    fig = plt.figure()
    #    ax = fig.add_subplot(111)
    #    ax.loglog(NHtest, P, linewidth=0, marker='o')
    #    ax.loglog(NHtest, C, linewidth=0, marker='o')
    #    ax.axvline(nH)
    #    plt.show()

    # Restore original fit (NH=0)
    if not fixgamma:
        dsmod.thaw("po1.PhoIndex")

    shp.set_par("abs1.nH", val=0)
    properFit()

    return nH
Ejemplo n.º 4
0
    def test_case_4(self):
        x1 = np.arange(50) + 100
        y1 = 2 * (3 * x1**2 + x1)
        x2 = np.arange(50)
        y2 = 2 * (x2**2 + 3 * x2)
        x3 = np.arange(50) + 200
        y3 = 2 * (x3**2 + 3 * x3)

        ds = self.ds

        datastack.load_arrays(ds, [[x1, y1], [x2, y2], [x3, y3]])

        datastack.set_source(ds, 'const1d.const * polynom1d.poly__ID')

        poly1 = ui._session._get_model_component('poly1')
        poly2 = ui._session._get_model_component('poly2')
        poly3 = ui._session._get_model_component('poly3')
        const = ui._session._get_model_component('const')

        datastack.freeze(ds, 'poly')
        datastack.thaw(ds, 'poly.c0')
        datastack.thaw(ds, 'poly.c1')
        datastack.thaw(ds, 'poly.c2')
        datastack.thaw(ds, 'const')

        assert poly1.c0.frozen is False
        assert poly1.c1.frozen is False
        assert poly1.c2.frozen is False
        assert poly1.c3.frozen is True

        assert poly2.c0.frozen is False
        assert poly2.c1.frozen is False
        assert poly2.c2.frozen is False
        assert poly2.c3.frozen is True

        assert poly3.c0.frozen is False
        assert poly3.c1.frozen is False
        assert poly3.c2.frozen is False
        assert poly3.c3.frozen is True

        datastack.set_par(ds, 'poly.c1', 0.45)

        assert poly1.c1.val == 0.45
        assert poly2.c1.val == 0.45
        assert poly3.c1.val == 0.45

        datastack.set_par(ds[1], 'poly.c1', 0.1)

        assert poly1.c1.val == 0.1
        assert poly2.c1.val == 0.45
        assert poly3.c1.val == 0.45

        datastack.set_par(ds, 'const.c0', 2)

        assert const.c0.val == 2

        datastack.set_par(ds, 'const.integrate', False)
        datastack.freeze(ds, 'const.c0')

        vals = datastack.get_par(ds, 'poly.c1.val')
        assert ([0.1, 0.45, 0.45] == vals).all()

        # QUS: pars is not checked, so is this just
        # checking that get_par doesn't fail?
        pars = datastack.get_par(ds, 'const.c0')

        datastack.fit(ds)

        assert round(poly1.c1.val) == 1
        assert round(poly1.c2.val) == 3
        assert round(poly2.c1.val) == 3
        assert round(poly2.c2.val) == 1
        assert round(poly3.c1.val) == 3
        assert round(poly3.c2.val) == 1

        ds.clear_stack()

        x1 = np.arange(50) + 100
        y1 = 7 * (3 * x1**2 + x1)
        x2 = np.arange(50)
        y2 = 2 * (x2**2 + 3 * x2)
        x3 = np.arange(50) + 200
        y3 = 2 * (x3**2 + 3 * x3)

        datastack.load_arrays(ds, [[x1, y1], [x2, y2], [x3, y3]])

        datastack.set_template_id("foo")

        datastack.set_source(ds, 'const1d.constfoo * polynom1d.polyfoo')

        const1 = ui._session._get_model_component('const1')
        const2 = ui._session._get_model_component('const2')
        const3 = ui._session._get_model_component('const3')

        datastack.link(ds[2, 3], 'const.c0')

        datastack.set_par(ds[2], 'const.c0', 3)
        datastack.set_par(ds[1], 'const.c0', 7)

        datastack.freeze(ds[1], 'const.c0')

        assert const2.c0.frozen is False

        datastack.fit(ds)

        assert const2.c0.val == const3.c0.val
        assert const3.c0._link is const2.c0

        datastack.unlink(ds, "const.c0")

        assert const3.c0._link is not const2.c0
Ejemplo n.º 5
0
    def test_case_4(self):
        x1 = np.arange(50)+100
        y1 = 2*(3*x1**2 + x1)
        x2 = np.arange(50)
        y2 = 2*(x2**2 + 3*x2)
        x3 = np.arange(50)+200
        y3 = 2*(x3**2+3*x3)

        ds = self.ds

        datastack.load_arrays(ds, [[x1, y1], [x2, y2], [x3, y3]])

        datastack.set_source(ds, 'const1d.const * polynom1d.poly__ID')

        poly1 = ui._session._get_model_component('poly1')
        poly2 = ui._session._get_model_component('poly2')
        poly3 = ui._session._get_model_component('poly3')
        const = ui._session._get_model_component('const')

        datastack.freeze(ds, 'poly')
        datastack.thaw(ds, 'poly.c0')
        datastack.thaw(ds, 'poly.c1')
        datastack.thaw(ds, 'poly.c2')
        datastack.thaw(ds, 'const')

        assert poly1.c0.frozen is False
        assert poly1.c1.frozen is False
        assert poly1.c2.frozen is False
        assert poly1.c3.frozen is True

        assert poly2.c0.frozen is False
        assert poly2.c1.frozen is False
        assert poly2.c2.frozen is False
        assert poly2.c3.frozen is True

        assert poly3.c0.frozen is False
        assert poly3.c1.frozen is False
        assert poly3.c2.frozen is False
        assert poly3.c3.frozen is True

        datastack.set_par(ds, 'poly.c1', 0.45)

        assert poly1.c1.val == 0.45
        assert poly2.c1.val == 0.45
        assert poly3.c1.val == 0.45

        datastack.set_par(ds[1], 'poly.c1', 0.1)

        assert poly1.c1.val == 0.1
        assert poly2.c1.val == 0.45
        assert poly3.c1.val == 0.45

        datastack.set_par(ds, 'const.c0', 2)

        assert const.c0.val == 2

        datastack.set_par(ds, 'const.integrate', False)
        datastack.freeze(ds, 'const.c0')

        vals = datastack.get_par(ds, 'poly.c1.val')
        assert ([0.1, 0.45, 0.45] == vals).all()

        # QUS: pars is not checked, so is this just
        # checking that get_par doesn't fail?
        pars = datastack.get_par(ds, 'const.c0')

        datastack.fit(ds)

        assert round(poly1.c1.val) == 1
        assert round(poly1.c2.val) == 3
        assert round(poly2.c1.val) == 3
        assert round(poly2.c2.val) == 1
        assert round(poly3.c1.val) == 3
        assert round(poly3.c2.val) == 1

        ds.clear_stack()

        x1 = np.arange(50)+100
        y1 = 7*(3*x1**2 + x1)
        x2 = np.arange(50)
        y2 = 2*(x2**2 + 3*x2)
        x3 = np.arange(50)+200
        y3 = 2*(x3**2+3*x3)

        datastack.load_arrays(ds, [[x1, y1], [x2, y2], [x3, y3]])

        datastack.set_template_id("foo")

        datastack.set_source(ds, 'const1d.constfoo * polynom1d.polyfoo')

        const1 = ui._session._get_model_component('const1')
        const2 = ui._session._get_model_component('const2')
        const3 = ui._session._get_model_component('const3')

        datastack.link(ds[2,3], 'const.c0')

        datastack.set_par(ds[2], 'const.c0', 3)
        datastack.set_par(ds[1], 'const.c0', 7)

        datastack.freeze(ds[1], 'const.c0')

        assert const2.c0.frozen is False

        datastack.fit(ds)

        assert const2.c0.val == const3.c0.val
        assert const3.c0._link is const2.c0

        datastack.unlink(ds, "const.c0")

        assert const3.c0._link is not const2.c0