def check_integrals(): """Check that Sherpa normed models integrate to 1.""" from sherpa.astro import ui from sherpa.astro.ui import normgauss2d from models import normdisk2d, normshell2d ui.clean() g = normgauss2d('g') g.xpos, g.ypos, g.ampl, g.fwhm = 100, 100, 42, 5 d = normdisk2d('d') d.xpos, d.ypos, d.ampl, d.r0 = 100, 100, 42, 50 s = normshell2d('s') s.xpos, s.ypos, s.ampl, s.r0, s.width = 100, 100, 42, 30, 20 models = [g, d, s] ui.dataspace2d((200, 200)) for model in models: ui.set_model(model) # In sherpa normed model values are flux per pixel area. # So to get the total flux (represented by the `ampl` parameter) # one can simply sum over all pixels, because a pixel has area 1 pix^2. # :-) integral = ui.get_model_image().y.sum() print model.name, integral
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
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
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
def containment_fraction(self, theta, npix=1000): """Compute fraction of PSF contained inside theta.""" import sherpa.astro.ui as sau sau.dataspace2d((npix, npix)) self.set() # x_center = get_psf().kernel.pars.xpos # y_center = get_psf().kernel.pars.ypos x_center, y_center = sau.get_psf().model.center x_center, y_center = x_center + 0.5, y_center + 0.5 # shift seen on image. x, y = sau.get_data().x0, sau.get_data().x1 # @note Here we have to use the source image, before I used # get_model_image(), which returns the PSF-convolved PSF image, # which is a factor of sqrt(2) ~ 1.4 too wide!!! p = sau.get_source_image().y.flatten() p /= np.nansum(p) mask = (x - x_center) ** 2 + (y - y_center) ** 2 < theta ** 2 fraction = np.nansum(p[mask]) if 0: # debug sau.get_data().y = p sau.save_data('psf_sherpa.fits', clobber=True) sau.get_data().y = mask.astype('int') sau.save_data('mask_sherpa.fits', clobber=True) return fraction
from __future__ import print_function, division import numpy as np import sherpa.astro.ui as sau # Define width of the source and the PSF sigma_psf, sigma_source = 3, 4 # for relation of sigma and fwhm see # http://cxc.harvard.edu/sherpa/ahelp/gauss2d.html sigma_to_fwhm = np.sqrt(8 * np.log(2)) # ~ 2.35 sigma = np.sqrt(sigma_psf ** 2 + sigma_source ** 2) fwhm = sigma_to_fwhm * sigma # Seed the random number generator to make the output reproducible np.random.seed(0) sau.dataspace2d((200, 200)) sau.set_source('normgauss2d.source + const2d.background') sau.set_par('source.xpos', 100) sau.set_par('source.ypos', 100) sau.set_par('source.ampl', 1e3) sau.set_par('source.fwhm', fwhm) sau.set_par('background.c0', 1) sau.fake() sau.save_model('model.fits.gz', clobber=True) sau.save_data('counts.fits.gz', clobber=True) sau.set_source('source') sau.save_model('source.fits.gz', clobber=True) sau.set_source('background')
from __future__ import print_function, division import numpy as np import sherpa.astro.ui as sau # Define width of the source and the PSF sigma_psf, sigma_source = 3, 4 # for relation of sigma and fwhm see # http://cxc.harvard.edu/sherpa/ahelp/gauss2d.html sigma_to_fwhm = np.sqrt(8 * np.log(2)) # ~ 2.35 sigma = np.sqrt(sigma_psf**2 + sigma_source**2) fwhm = sigma_to_fwhm * sigma # Seed the random number generator to make the output reproducible np.random.seed(0) sau.dataspace2d((200, 200)) sau.set_source('normgauss2d.source + const2d.background') sau.set_par('source.xpos', 100) sau.set_par('source.ypos', 100) sau.set_par('source.ampl', 1e3) sau.set_par('source.fwhm', fwhm) sau.set_par('background.c0', 1) sau.fake() sau.save_model('model.fits.gz', clobber=True) sau.save_data('counts.fits.gz', clobber=True) sau.set_source('source') sau.save_model('source.fits.gz', clobber=True) sau.set_source('background')