Пример #1
0
def kbe():
    source_kernel = np.ones((1, 3))
    background_kernel = np.ones((5, 3))
    return KernelBackgroundEstimator(
        kernel_src=source_kernel,
        kernel_bkg=background_kernel,
        significance_threshold=4,
        mask_dilation_radius="1 deg",
        keep_record=True,
    )
Пример #2
0
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()

Пример #4
0
#
# TODO

# In[ ]:

# 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
#
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()