Esempio n. 1
0
def setup_imgdata_model():
    """Use a model for the PSF"""

    # Fake an image
    #
    x1, x0 = np.mgrid[0:8, 0:10]
    ymod = 10 + 100 / ((x0 - 5.5)**2 + (x1 - 3.5)**2)

    x0 = x0.flatten()
    x1 = x1.flatten()
    y = ymod.flatten()

    y = y.astype(int)  # convert to integers

    ui.load_arrays(1, x0, x1, y, (8, 10), ui.DataIMG)

    pmodel = ui.create_model_component('gauss2d', 'pmodel')
    pmodel.xpos = 0
    pmodel.ypos = 0
    pmodel.fwhm = 3

    ui.load_psf('psf', pmodel)
    psf.size = [10, 10]  # not sure if this is useful but leave in
    psf.center = [5, 4]
    ui.set_psf(psf)

    setup_imgdata_source()
Esempio n. 2
0
def test_psf_model2d(model, clean_astro_ui):
    ui.dataspace2d([216, 261])
    ui.load_psf('psf2d', model + '.mdl')
    ui.set_psf('psf2d')
    mdl = ui.get_model_component('mdl')
    assert (numpy.array(mdl.get_center()) ==
            numpy.array([108, 130])).all()
Esempio n. 3
0
 def set(self):
     """Set the PSF for Sherpa."""
     import sherpa.astro.ui as sau
     # from gammapy.image.models.utils import read_json
     read_json(self.pars, sau.set_model)
     sau.load_psf('psf', sau.get_model())
     self.center_psf()
     sau.set_psf('psf')
Esempio n. 4
0
 def set(self):
     """Set the PSF for Sherpa."""
     import sherpa.astro.ui as sau
     # from morphology.utils import read_json
     read_json(self.pars, sau.set_model)
     sau.load_psf('psf', sau.get_model())
     sau.set_psf('psf')
     self.center_psf()
Esempio n. 5
0
 def set(self):
     """Set the PSF for Sherpa."""
     import sherpa.astro.ui as sau
     from ..scripts.image_fit import read_json
     read_json(self.pars, sau.set_model)
     sau.load_psf('psf', sau.get_model())
     self.center_psf()
     sau.set_psf('psf')
Esempio n. 6
0
 def test_psf_model2d(self):
     ui.dataspace2d([216, 261])
     for model in self.models2d:
         try:
             ui.load_psf("psf2d", model + ".mdl")
             ui.set_psf("psf2d")
             mdl = ui.get_model_component("mdl")
             self.assert_((numpy.array(mdl.get_center()) == numpy.array([108, 130])).all())
         except:
             print model
             raise
Esempio n. 7
0
 def test_psf_model1d(self):
     ui.dataspace1d(1, 10)
     for model in self.models1d:
         try:
             ui.load_psf("psf1d", model + ".mdl")
             ui.set_psf("psf1d")
             mdl = ui.get_model_component("mdl")
             self.assert_((numpy.array(mdl.get_center()) == numpy.array([4])).all())
         except:
             print model
             raise
Esempio n. 8
0
 def test_psf_model1d(self):
     ui.dataspace1d(1, 10)
     for model in self.models1d:
         try:
             ui.load_psf('psf1d', model + '.mdl')
             ui.set_psf('psf1d')
             mdl = ui.get_model_component('mdl')
             self.assertTrue(
                 (numpy.array(mdl.get_center()) == numpy.array([4])).all())
         except:
             print model
             raise
Esempio n. 9
0
 def test_psf_model2d(self):
     ui.dataspace2d([216, 261])
     for model in self.models2d:
         try:
             ui.load_psf('psf2d', model + '.mdl')
             ui.set_psf('psf2d')
             mdl = ui.get_model_component('mdl')
             self.assertTrue((numpy.array(mdl.get_center()) ==
                              numpy.array([108, 130])).all())
         except:
             print model
             raise
Esempio n. 10
0
 def test_psf_model1d(self):
     ui.dataspace1d(1, 10)
     for model in self.models1d:
         try:
             ui.load_psf('psf1d', model + '.mdl')
             ui.set_psf('psf1d')
             mdl = ui.get_model_component('mdl')
             self.assertTrue((numpy.array(mdl.get_center()) ==
                              numpy.array([4])).all())
         except:
             print model
             raise
Esempio n. 11
0
def setup(imgfile, emapfile):
    shp.load_data(imgfile)

    shp.load_table_model('emap', emapfile)
    shp.freeze(emap.ampl)

    # we're using an on axis psf provided in 
    # /packages/CALDB/data/nustar/fpm/bcf/psf/
    shp.load_psf('psf', 'nuA2dpsf20100101v003_onaxis.fits')
    shp.set_psf(psf)
    psf.center = (500.0, 500.0)
    psf.size = (200, 200)
Esempio n. 12
0
 def test_psf_model2d(self):
     ui.dataspace2d([216,261])
     for model in self.models2d:
         try:
             ui.load_psf('psf2d', model+'.mdl')
             ui.set_psf('psf2d')
             mdl = ui.get_model_component('mdl')
             self.assert_( (numpy.array(mdl.get_center()) ==
                            numpy.array([108,130])).all() )
         except:
             print model
             raise
Esempio n. 13
0
def setup_imgdata_file(tmp_path, recwarn):
    """Use a model for the PSF"""

    # Fake an image
    #
    x1, x0 = np.mgrid[0:8, 0:10]
    ymod = 10 + 100 / ((x0 - 5.5)**2 + (x1 - 3.5)**2)

    x0 = x0.flatten()
    x1 = x1.flatten()
    y = ymod.flatten()

    y = y.astype(int)  # convert to integers

    ui.load_arrays(1, x0, x1, y, (8, 10), ui.DataIMG)

    # Sherpa can confuse astropy if you send in pathlib objects so
    # convert to the raw filesystem path.
    #
    fname = tmp_path / "psf.fits"
    infile = str(fname)
    fake_psf_image(infile)

    ui.load_psf('psf', infile)
    psf.size = [10, 8]
    psf.center = [5, 4]

    # Just check where the warning comes from (pytest.warns didn't
    # seem to remove the warning so it still triggered the general
    # "check no warning" fixture, so use recwarn like this).
    #
    assert len(recwarn) == 0
    ui.set_psf(psf)
    assert len(recwarn) == 1

    setup_imgdata_source()

    assert len(recwarn) == 1
    w = recwarn.pop()
    assert issubclass(w.category, UserWarning)

    wmsg = 'PSF Image does not have a pixel size. Sherpa ' + \
        'will assume the pixel size is the same as the data'
    assert str(w.message) == wmsg
Esempio n. 14
0
def test_psf_model1d(model, center, clean_astro_ui):
    ui.dataspace1d(1, 10)
    ui.load_psf('psf1d', model + '.mdl')
    ui.set_psf('psf1d')
    mdl = ui.get_model_component('mdl')
    assert mdl.get_center() == (center, )
Esempio n. 15
0
    def simulate_null_images(img_file: str,
                             psf_file: str,
                             n_null_sims: int,
                             no_core: bool = False,
                             mcmciter: int = 5000,
                             **kwargs) -> None:
        """
        Simulates a specified number of baseline images for a given input observation and a psf file

        :param img_file: Path to the input image file
        :param psf_file: Path to the psf image file
        :param n_null_sims: Number of baseline replicates to be simulated
        :param no_core: Setting this to True will only generate baseline replicates with a flat background while the default value includes a point source at the location of the core
        :param mcmciter: The number of MCMC samples to draw for simulating the baselines
        """
        print("Creating the null file")
        clean()
        set_stat("cstat")
        set_method("simplex")
        load_image(img_file)
        load_psf("mypsf", psf_file)
        set_psf(mypsf)

        if no_core:
            set_model(const2d.c0)
            set_par(c0.c0, min=0)
        else:
            set_model(gauss2d.q1 + const2d.c0)
            set_par(c0.c0, min=0)
            # set_par(q1.fwhm,max=0.5)
            guess(q1)
        fit()
        results = get_fit_results()
        save("core_source_fit.save", clobber=True)
        save_source("null_q1_c1.fits", clobber=True)
        covar()

        if no_core:
            for i in range(n_null_sims):
                fake()
                save_image("sim_null_{}.fits".format(i), clobber=True)
            clean()
            return

        normgauss1d.g1
        g1.pos = q1.fwhm
        g1.fwhm = get_covar_results().parmaxes[0]

        # check if there is a valid upper bound.
        print(get_covar_results())
        if (get_covar_results().parmaxes[0] is None
                or get_covar_results().parmins[1] is None
                or get_covar_results().parmins[0] is None):
            for i in range(n_null_sims):
                fake()
                save_image("sim_null_{}.fits".format(i), clobber=True)
            clean()
            return
        # if not go for the regular
        set_prior(q1.fwhm, g1)
        set_sampler_opt("defaultprior", False)
        set_sampler_opt("priorshape", [True, False, False, False, False])
        set_sampler_opt("originalscale", [True, True, True, True, True])
        if mcmciter < n_null_sims * 100:
            mcmciter = n_null_sims * 100

        # the following code throws an error sometimes #bug
        try:
            stats, accept, params = get_draws(1, niter=mcmciter)
        except:
            params = [np.repeat(q1.fwhm.val, mcmciter)]

        # print('Simulating the null files')
        for i in range(n_null_sims):
            set_par(q1.fwhm, params[0][(i + 1) * 100 - 1])
            fake()
            save_image("sim_null_{}.fits".format(i), clobber=True)
        save_all(outfile="lira_input_baseline_sim.log", clobber=True)
        clean()