Esempio n. 1
0
def significance_image(infile,
                       outfile,
                       theta,
                       overwrite):
    """Make correlated significance image.

    TODO: describe
    """
    from astropy.io import fits
    from gammapy.image import disk_correlate
    from gammapy.stats import significance_on_off

    log.info('Reading {0}'.format(infile))
    hdus = fits.open(infile)
    n_on = hdus['On'].data
    n_off = hdus['Off'].data
    a_on = hdus['OnExposure'].data
    a_off = hdus['OffExposure'].data

    log.info('Correlating n_on and a_on map')
    theta = theta / hdus['On'].header['CDELT2']
    n_on = disk_correlate(n_on, theta)
    a_on = disk_correlate(a_on, theta)

    log.info('Computing significance map')
    alpha = a_on / a_off
    significance = significance_on_off(n_on, n_off, alpha)

    log.info('Writing {0}'.format(outfile))
    fits.writeto(outfile, data=significance, header=hdus['On'].header, clobber=overwrite)
Esempio n. 2
0
def significance_image(infile,
                       outfile,
                       theta,
                       overwrite):
    """Make correlated significance image.

    TODO: describe
    """
    import logging
    logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')
    from astropy.io import fits
    from gammapy.image import disk_correlate
    from gammapy.stats import significance_on_off

    logging.info('Reading {0}'.format(infile))
    hdus = fits.open(infile)
    n_on = hdus['On'].data
    n_off = hdus['Off'].data
    a_on = hdus['OnExposure'].data
    a_off = hdus['OffExposure'].data

    logging.info('Correlating n_on and a_on map')
    theta = theta / hdus['On'].header['CDELT2']
    n_on = disk_correlate(n_on, theta)
    a_on = disk_correlate(a_on, theta)

    logging.info('Computing significance map')
    alpha = a_on / a_off
    significance = significance_on_off(n_on, n_off, alpha)

    logging.info('Writing {0}'.format(outfile))
    fits.writeto(outfile, data=significance, header=hdus['On'].header, clobber=overwrite)
Esempio n. 3
0
def make_significance_image():
    counts_image = fits.open("counts_image.fits")[1]
    bkg_image = fits.open("bkg_image.fits")[1]
    counts = disk_correlate(counts_image.data, 10)
    bkg = disk_correlate(bkg_image.data, 10)
    s = significance(counts, bkg)
    s_image = fits.ImageHDU(data=s, header=counts_image.header)
    s_image.writeto("significance_image.fits", clobber=True)
Esempio n. 4
0
def make_significance_image():
    counts_image = fits.open("counts_image.fits")[1]
    bkg_image = fits.open("bkg_image.fits")[1]
    counts = disk_correlate(counts_image.data, 10)
    bkg = disk_correlate(bkg_image.data, 10)
    s = significance(counts, bkg)
    s_image = fits.ImageHDU(data=s, header=counts_image.header)
    s_image.writeto("significance_image.fits", clobber=True)
Esempio n. 5
0
def residual_images(model_file, data_file, out_file, thetas, overwrite):
    """Compute source model residual images.

    The input `data_file` must contain the following HDU extensions:

    * 'On' -- Counts image
    * 'Background' -- Background image
    """
    import logging
    logging.basicConfig(level=logging.DEBUG,
                        format='%(levelname)s - %(message)s')
    import numpy as np
    from astropy.io import fits
    from gammapy.image import disk_correlate
    from gammapy.stats import significance

    logging.info('Reading {0}'.format(data_file))
    hdu_list = fits.open(data_file)
    header = hdu_list[0].header
    counts = hdu_list['On'].data.astype(np.float64)
    background = hdu_list['Background'].data.astype(np.float64)
    diffuse = hdu_list['Diffuse'].data.astype(np.float64)

    logging.info('Reading {0}'.format(model_file))
    model = fits.getdata(model_file)

    background_plus_model_diffuse = background + model + diffuse

    out_hdu_list = fits.HDUList()

    for theta in thetas:
        logging.info('Processing theta = {0} deg'.format(theta))

        theta_pix = theta / header['CDELT2']
        counts_corr = disk_correlate(counts, theta_pix)
        background_plus_model_corr = disk_correlate(
            background_plus_model_diffuse, theta_pix)

        # excess_corr = counts_corr - background_plus_model_corr
        significance_corr = significance(counts_corr,
                                         background_plus_model_corr)

        name = 'RESIDUAL_SIGNIFICANCE_{0}'.format(theta)
        logging.info('Appending HDU extension: {0}'.format(name))
        hdu = fits.ImageHDU(significance_corr, header, name)
        out_hdu_list.append(hdu)

    logging.info('Writing {0}'.format(out_file))
    out_hdu_list.writeto(out_file, clobber=overwrite)
Esempio n. 6
0
def residual_images(model_file,
                    data_file,
                    out_file,
                    thetas,
                    overwrite):
    """Compute source model residual images.

    The input `data_file` must contain the following HDU extensions:

    * 'On' -- Counts image
    * 'Background' -- Background image
    """
    import numpy as np
    from astropy.io import fits
    from gammapy.image import disk_correlate
    from gammapy.stats import significance

    log.info('Reading {0}'.format(data_file))
    hdu_list = fits.open(data_file)
    header = hdu_list[0].header
    counts = hdu_list['On'].data.astype(np.float64)
    background = hdu_list['Background'].data.astype(np.float64)
    diffuse = hdu_list['Diffuse'].data.astype(np.float64)

    log.info('Reading {0}'.format(model_file))
    model = fits.getdata(model_file)

    background_plus_model_diffuse = background + model + diffuse

    out_hdu_list = fits.HDUList()

    for theta in thetas:
        log.info('Processing theta = {0} deg'.format(theta))

        theta_pix = theta / header['CDELT2']
        counts_corr = disk_correlate(counts, theta_pix)
        background_plus_model_corr = disk_correlate(background_plus_model_diffuse, theta_pix)

        # excess_corr = counts_corr - background_plus_model_corr
        significance_corr = significance(counts_corr, background_plus_model_corr)

        name = 'RESIDUAL_SIGNIFICANCE_{0}'.format(theta)
        log.info('Appending HDU extension: {0}'.format(name))
        hdu = fits.ImageHDU(significance_corr, header, name)
        out_hdu_list.append(hdu)

    log.info('Writing {0}'.format(out_file))
    out_hdu_list.writeto(out_file, clobber=overwrite)
Esempio n. 7
0
"""Plot significance image with HESS and MILAGRO colormap.
"""
import numpy as np
import matplotlib.pyplot as plt
from gammapy.datasets import load_poisson_stats_image
from gammapy.image import disk_correlate
from gammapy.stats import significance
from gammapy.image import colormap_hess, colormap_milagro
from astropy.visualization.mpl_normalize import ImageNormalize
from astropy.visualization import LinearStretch

# Compute an example significance image
counts = load_poisson_stats_image()
counts = disk_correlate(counts, radius=5, mode='reflect')
background = np.median(counts) * np.ones_like(counts)
image = significance(counts, background)

# Plot with the HESS and Milagro colormap
vmin, vmax, vtransition = -5, 15, 5
plt.figure(figsize=(8, 4))

normalize = ImageNormalize(vmin=vmin, vmax=vmax, stretch=LinearStretch())
transition = normalize(vtransition)

plt.subplot(121)
cmap = colormap_hess(transition=transition)
plt.imshow(image, cmap=cmap, norm=normalize)
plt.axis('off')
plt.colorbar()
plt.title('HESS-style colormap')
Esempio n. 8
0
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Plot significicance image with HESS- and MILAGRO-colormap.
"""
import numpy as np
import matplotlib.pyplot as plt
from gammapy.data import poisson_stats_image
from gammapy.image import disk_correlate
from gammapy.stats import significance
from gammapy.image import colormap_hess, colormap_milagro

# Compute an example significance image
counts = disk_correlate(poisson_stats_image(), radius=5, mode='reflect')
background = np.median(counts) * np.ones_like(counts)
image = significance(counts, background)

# Plot with the HESS and Milagro colormap
vmin, vmax, vtransition = -5, 15, 5
plt.figure(figsize=(8, 4))

plt.subplot(121)
cmap = colormap_hess(vtransition=vtransition, vmin=vmin, vmax=vmax)
plt.imshow(image, cmap=cmap, vmin=vmin, vmax=vmax)
plt.axis('off')
plt.colorbar()
plt.title('HESS-style colormap')

plt.subplot(122)
cmap = colormap_milagro(vtransition=vtransition, vmin=vmin, vmax=vmax)
plt.imshow(image, cmap=cmap, vmin=vmin, vmax=vmax)
plt.axis('off')
plt.colorbar()