def make_background_image(): """Estimate background image. See the `IterativeKernelBackgroundEstimator` tutorial and documentation how it works, or the SciNeGHe 2014 proceeding by Ellis Owen et al. """ radius = Angle(0.2, 'deg') r_in = Angle(0.3, 'deg') r_out = Angle(0.7, 'deg') significance_threshold = 5 mask_dilation_radius = Angle(0.1, 'deg') max_iterations = 3 hdu = fits.open(COUNTS_IMAGE)['COUNTS'] binsz = hdu.header['CDELT2'] images = GammaImages(counts=hdu.data, header=hdu.header) # TODO: we should have utility functions to initialise # kernels with angles so that we don't have to convert to pix here. source_kernel = binary_disk(radius=radius.deg / binsz) background_kernel = binary_ring(r_in=r_in.deg / binsz, r_out=r_out.deg / binsz) estimator = IterativeKernelBackgroundEstimator( images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=significance_threshold, mask_dilation_radius=mask_dilation_radius.deg / binsz, ) print('Running background estimation ...') estimator.run(max_iterations=max_iterations) print('Writing {}'.format(MASK_IMAGE)) estimator.mask_image_hdu.writeto(MASK_IMAGE, clobber=True) print('Writing {}'.format(BACKGROUND_IMAGE)) estimator.background_image_hdu.writeto(BACKGROUND_IMAGE, clobber=True)
def make_background_image(): """Estimate background image. See the `KernelBackgroundEstimator` tutorial and documentation how it works, or the SciNeGHe 2014 proceeding by Ellis Owen et al. """ radius = Angle(0.2, 'deg') r_in = Angle(0.3, 'deg') r_out = Angle(0.7, 'deg') significance_threshold = 5 mask_dilation_radius = Angle(0.1, 'deg') max_iterations = 3 hdu = fits.open(COUNTS_IMAGE)['COUNTS'] binsz = hdu.header['CDELT2'] images = KernelBackgroundEstimatorData(counts=hdu.data, header=hdu.header) # TODO: we should have utility functions to initialise # kernels with angles so that we don't have to convert to pix here. source_kernel = binary_disk(radius=radius.deg/binsz) background_kernel = binary_ring(r_in=r_in.deg/binsz, r_out=r_out.deg/binsz) estimator = KernelBackgroundEstimator( images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=significance_threshold, mask_dilation_radius=mask_dilation_radius.deg/binsz, ) print('Running background estimation ...') estimator.run(max_iterations=max_iterations) print('Writing {}'.format(MASK_IMAGE)) estimator.mask_image_hdu.writeto(MASK_IMAGE, clobber=True) print('Writing {}'.format(BACKGROUND_IMAGE)) estimator.background_image_hdu.writeto(BACKGROUND_IMAGE, clobber=True)
energy_exp = Quantity([10000, 500000], 'MeV') exposure_data = Quantity(exposure_hdu.data, '') exposure_spec_cube = SpectralCube(data=exposure_data, wcs=exposure_wcs, energy=energy_exp) exposure_spec_cube = exposure_spec_cube.reproject_to(flux_spec_cube) counts_data = flux_spec_cube.data * exposure_spec_cube.data counts = fits.ImageHDU(data=counts_data[0], header=flux_hdu.header) # Start with flat background estimate # Background must be provided as an ImageHDU background_data = np.ones_like(counts_data, dtype=float) background = fits.ImageHDU(data=background_data[0], header=flux_hdu.header) images = GammaImages(counts=counts, background=background) source_kernel = binary_disk(CORRELATION_RADIUS).astype(float) source_kernel /= source_kernel.sum() background_kernel = np.ones((5, 100)) background_kernel /= background_kernel.sum() # *** ITERATOR *** ibe = IterativeKernelBackgroundEstimator( images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=SIGNIFICANCE_THRESHOLD, mask_dilation_radius=MASK_DILATION_RADIUS) mask, new_background = ibe.run()
exposure_hdu = fits.open(exposure_file)[0] exposure_wcs = WCS(exposure_hdu.header) energy_exp = Quantity([10000, 500000], "MeV") exposure_data = Quantity(exposure_hdu.data, "") exposure_spec_cube = SpectralCube(data=exposure_data, wcs=exposure_wcs, energy=energy_exp) exposure_spec_cube = exposure_spec_cube.reproject_to(flux_spec_cube) counts_data = flux_spec_cube.data * exposure_spec_cube.data counts = fits.ImageHDU(data=counts_data[0], header=flux_hdu.header) # Start with flat background estimate # Background must be provided as an ImageHDU background_data = np.ones_like(counts_data, dtype=float) background = fits.ImageHDU(data=background_data[0], header=flux_hdu.header) images = GammaImages(counts=counts, background=background) source_kernel = binary_disk(CORRELATION_RADIUS).astype(float) source_kernel /= source_kernel.sum() background_kernel = np.ones((5, 100)) background_kernel /= background_kernel.sum() # *** ITERATOR *** ibe = IterativeKernelBackgroundEstimator( images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=SIGNIFICANCE_THRESHOLD, mask_dilation_radius=MASK_DILATION_RADIUS, )
from astropy.utils.data import download_file from gammapy.datasets import FermiGalacticCenter from gammapy.background import IterativeKernelBackgroundEstimator, GammaImages from gammapy.irf import EnergyDependentTablePSF from gammapy.image import make_empty_image, catalog_image, binary_disk from gammapy.image.utils import cube_to_image, solid_angle counts = fits.open('fermi_counts_galactic.fits')[0] # Start with flat background estimate # Background must be provided as an ImageHDU background_data=np.ones_like(counts.data, dtype=float) background = fits.ImageHDU(data=background_data, header=counts.header) images = GammaImages(counts=counts, background=background) source_kernel = binary_disk(3).astype(float) source_kernel /= source_kernel.sum() background_kernel = np.ones((5, 50)) background_kernel /= background_kernel.sum() # *** ITERATOR *** ibe = IterativeKernelBackgroundEstimator(images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=4, mask_dilation_radius=3, save_intermediate_results=True )
MASK_DILATION_RADIUS = 10 # Pixels psf_file = datasets.FermiGalacticCenter.psf() # Load/create example model images. filename = datasets.get_path('source_diffuse_separation/galactic_simulations/fermi_counts.fits', location='remote') # *** LOADING INPUT *** # Counts must be provided as an ImageHDU counts = fits.open(filename)[0].data header = fits.open(filename)[0].header images = GammaImages(counts=counts, header=header) source_kernel = binary_disk(CORRELATION_RADIUS) background_kernel = np.ones((10, 100)) # *** ITERATOR *** ibe = IterativeKernelBackgroundEstimator(images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=SIGNIFICANCE_THRESHOLD, mask_dilation_radius=MASK_DILATION_RADIUS ) n_iterations = 4 # *** RUN & PLOT ***
psf_file = datasets.FermiGalacticCenter.psf() # Load/create example model images. filename = datasets.get_path( 'source_diffuse_separation/galactic_simulations/fermi_counts.fits', location='remote') # *** LOADING INPUT *** # Counts must be provided as an ImageHDU counts = fits.open(filename)[0].data header = fits.open(filename)[0].header images = GammaImages(counts=counts, header=header) source_kernel = binary_disk(CORRELATION_RADIUS) background_kernel = np.ones((10, 100)) # *** ITERATOR *** ibe = IterativeKernelBackgroundEstimator( images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=SIGNIFICANCE_THRESHOLD, mask_dilation_radius=MASK_DILATION_RADIUS) n_iterations = 4 # *** RUN & PLOT ***