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)
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)
from gammapy.detect import KernelBackgroundEstimator # Parameters CORRELATION_RADIUS = 10 # Pixels SIGNIFICANCE_THRESHOLD = 5 # Sigma MASK_DILATION_RADIUS = 0.5 * u.deg # Load example images. filename = ('$GAMMAPY_EXTRA/datasets/source_diffuse_separation/' 'galactic_simulations/fermi_counts.fits') counts = SkyImage.read(filename) center = SkyCoord(0, 0, frame='galactic', unit='deg') images = SkyImageList() images['counts'] = counts.cutout(center, (10 * u.deg, 80 * u.deg)) kernel_src = Tophat2DKernel(CORRELATION_RADIUS).array kernel_bkg = np.ones((10, 150)) kbe = KernelBackgroundEstimator( kernel_src=kernel_src, kernel_bkg=kernel_bkg, significance_threshold=SIGNIFICANCE_THRESHOLD, mask_dilation_radius=MASK_DILATION_RADIUS, ) result = kbe.run(images) kbe.images_stack_show() plt.show()
# Start with flat background estimate background = np.ones_like(counts, dtype=float) images = KernelBackgroundEstimatorData(counts=counts, background=background) # CORRELATION_RADIUS source_kernel = np.ones((5, 5)) background_kernel = np.ones((100, 10)) kbe = KernelBackgroundEstimator(images=images, source_kernel=source_kernel, background_kernel=background_kernel, significance_threshold=SIGNIFICANCE_THRESHOLD, mask_dilation_radius=MASK_DILATION_RADIUS) kbe.run(n_iterations=3) # In[ ]: kbe.run_iteration() # In[ ]: kbe._data[1].print_info() # ## Plot results # # TODO # In[ ]:
import numpy as np from gammapy.maps import Map from astropy.convolution import Ring2DKernel, Tophat2DKernel from gammapy.detect import KernelBackgroundEstimator counts = Map.create(npix=100, binsz=1) counts.data += 42 counts.data[50][50] = 1000 source_kernel = Tophat2DKernel(3) bkg_kernel = Ring2DKernel(radius_in=4, width=2) kbe = KernelBackgroundEstimator(kernel_src=source_kernel.array, kernel_bkg=bkg_kernel.array) result = kbe.run({'counts': counts}) result['exclusion'].plot()
# To use the `KernelBackgroundEstimator` you first have to set # up a source and background kernel and put the counts image input # into a container `SkyImageList` class. images = SkyImageList() images['counts'] = counts_image2 print("3") kbe = KBE( kernel_src=source_kernel, kernel_bkg=background_kernel, significance_threshold=5, mask_dilation_radius=0.06 * u.deg, ) # This takes about 10 seconds on my machine result2 = kbe.run(images) print("results2") print(result2.names) ####altro modo stima bkg obs_list = data_store.obs_list(obs_ids) target_position = SkyCoord(0, 0, unit='deg', frame='galactic') on_radius = 0.3 * u.deg on_region = CircleSkyRegion(center=target_position, radius=on_radius) exclusion_mask = ref_image.region_mask(on_region) exclusion_mask.data = 1 - exclusion_mask.data bkg_estimator = RingBackgroundEstimator( r_in=0.5 * u.deg, width=0.2 * u.deg, )