def kernels(self, image): """Ring kernels according to the specified method. Parameters ---------- image : `~gammapy.image.SkyImage` Sky image specifying the WCS information. Returns ------- kernels : list List of `~astropy.convolution.Ring2DKernel` """ p = self.parameters scale = image.wcs_pixel_scale()[0] r_in = p['r_in'].to('deg') / scale r_out_max = p['r_out_max'].to('deg') / scale width = p['width'].to('deg') / scale stepsize = p['stepsize'].to('deg') / scale kernels = [] if p['method'] == 'fixed_width': r_ins = np.arange(r_in.value, (r_out_max - width).value, stepsize.value) widths = [width.value] elif p['method'] == 'fixed_r_in': widths = np.arange(width.value, (r_out_max - r_in).value, stepsize.value) r_ins = [r_in.value] for r_in, width in product(r_ins, widths): kernel = Ring2DKernel(r_in, width) kernel.normalize('peak') kernels.append(kernel) return kernels
def kernels(self, image): """Ring kernels according to the specified method. Parameters ---------- image : `~gammapy.maps.WcsNDMap` Map specifying the WCS information. Returns ------- kernels : list List of `~astropy.convolution.Ring2DKernel` """ scale = image.geom.pixel_scales[0] r_in = (self.r_in / scale).to_value("") r_out_max = (self.r_out_max / scale).to_value("") width = (self.width / scale).to_value("") stepsize = (self.stepsize / scale).to_value("") if self.method == "fixed_width": r_ins = np.arange(r_in, (r_out_max - width), stepsize) widths = [width] elif self.method == "fixed_r_in": widths = np.arange(width, (r_out_max - r_in), stepsize) r_ins = [r_in] else: raise ValueError(f"Invalid method: {self.method!r}") kernels = [] for r_in, width in itertools.product(r_ins, widths): kernel = Ring2DKernel(r_in, width) kernel.normalize("peak") kernels.append(kernel) return kernels
def background_skyimage_2fhl(counts): log.info('Computing background map.') images = GammaImages(counts.data, header=counts.wcs.to_header()) source_kernel = Tophat2DKernel(5) source_kernel.normalize('peak') background_kernel = Ring2DKernel(20, 20) background_kernel.normalize('peak') ikbe = IKBE( images=images, source_kernel=source_kernel.array, background_kernel=background_kernel.array, significance_threshold=5, mask_dilation_radius=3, ) mask_data, background_data = ikbe.run() mask = SkyMap.empty_like(counts) mask.data = mask_data background = SkyMap.empty_like(counts) background.data = background_data return mask, background
def kernel(self, image): """Ring kernel. Parameters ---------- image : `~gammapy.maps.WcsNDMap` Input Map Returns ------- ring : `~astropy.convolution.Ring2DKernel` Ring kernel. """ scale = image.geom.pixel_scales[0].to("deg") r_in = self.r_in.to("deg") / scale width = self.width.to("deg") / scale ring = Ring2DKernel(r_in.value, width.value) ring.normalize("peak") return ring
def kernel(self, image): """Ring kernel. Parameters ---------- image : `~gammapy.maps.WcsNDMap` Input Map Returns ------- ring : `~astropy.convolution.Ring2DKernel` Ring kernel. """ p = self.parameters scale = image.geom.pixel_scales[0].to('deg') r_in = p['r_in'].to('deg') / scale width = p['width'].to('deg') / scale ring = Ring2DKernel(r_in.value, width.value) ring.normalize('peak') return ring
def kernels(self, image): """Ring kernels according to the specified method. Parameters ---------- image : `~gammapy.maps.WcsNDMap` Map specifying the WCS information. Returns ------- kernels : list List of `~astropy.convolution.Ring2DKernel` """ p = self.parameters scale = image.geom.pixel_scales[0].to('deg') r_in = p['r_in'].to('deg') / scale r_out_max = p['r_out_max'].to('deg') / scale width = p['width'].to('deg') / scale stepsize = p['stepsize'].to('deg') / scale if p['method'] == 'fixed_width': r_ins = np.arange(r_in.value, (r_out_max - width).value, stepsize.value) widths = [width.value] elif p['method'] == 'fixed_r_in': widths = np.arange(width.value, (r_out_max - r_in).value, stepsize.value) r_ins = [r_in.value] else: raise ValueError('Invalid method: {}'.format(p['method'])) kernels = [] for r_in, width in product(r_ins, widths): kernel = Ring2DKernel(r_in, width) kernel.normalize('peak') kernels.append(kernel) return kernels
def kernel(self, image): """Ring kernel. Parameters ---------- image : `gammapy.image.SkyImage` Image Returns ------- ring : `~astropy.convolution.Ring2DKernel` Ring kernel. """ p = self.parameters scale = image.wcs_pixel_scale()[0] r_in = p['r_in'].to('deg') / scale width = p['width'].to('deg') / scale ring = Ring2DKernel(r_in.value, width.value) ring.normalize('peak') return ring
import numpy as np import pywt import cv2 from astropy.stats import sigma_clip from astropy.convolution import convolve, AiryDisk2DKernel,Box2DKernel,Gaussian2DKernel,MexicanHat2DKernel from astropy.convolution import Ring2DKernel,Tophat2DKernel,TrapezoidDisk2DKernel import myroutines as myr from util import standard kernels = [AiryDisk2DKernel(3),Box2DKernel(5),Gaussian2DKernel(2),Gaussian2DKernel(4),MexicanHat2DKernel(2) ,MexicanHat2DKernel(4),Tophat2DKernel(2),TrapezoidDisk2DKernel(2),Ring2DKernel(7,2)] kernel_names = ['AiryDisk3','Box5','Gaussian2','Gaussian4',\ 'MexicanHat2','MexicanHat4','Tophat2','TrapezoidDisk2','Ring'] #wts = ['db38','sym20','coif17','bior2.8','bior3.9',\ #'bior4.4','bior5.5','bior6.8','dmey','rbio1.5',\ #'rbio2.8','rbio6.8'] wts = ['db38','sym20','coif17','dmey'] def wavelet(data, wlf, threshold): """ wavelet: this function . Arguments: data (numpy array): input data. wlf: wavelet fucntion. threshold: threshold of high pass filter. -------- Returns:
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()
# Make a counts image for a single observation events = datastore.obs(obs_id=obsid[0]).events counts_image = SkyImage.empty_like(ref_image) counts_image.fill_events(events) norm = simple_norm(counts_image.data, stretch='sqrt', min_cut=0, max_cut=0.3) counts_image.smooth(radius=0.1 * u.deg).plot(norm=norm, add_cbar=True) #kernel background??? source_kernel = Tophat2DKernel(radius=5) source_kernel.normalize(mode='peak') source_kernel = source_kernel.array background_kernel = Ring2DKernel(radius_in=20, width=10) background_kernel.normalize(mode='peak') background_kernel = background_kernel.array plt.imshow(source_kernel, interpolation='nearest', cmap='gray') plt.colorbar() plt.grid('off') plt.imshow(background_kernel, interpolation='nearest', cmap='gray') plt.colorbar() plt.grid('off') obs_ids=obsid[0:3] counts_image2 = SkyImage.empty_like(ref_image) for obs_id in obs_ids: events = datastore.obs(obs_id=obs_id).events counts_image2.fill_events(events) # To use the `KernelBackgroundEstimator` you first have to set
q2=counts_image2#.cutout(position=SkyCoord(0, 0, unit='deg', frame='galactic'),size=(10*u.deg, 10*u.deg)).plot(norm=norm, add_cbar=True) if(show_it==False): plt.show(q2) print("2") ################QUI C'è IL CONTO SUL BKG source_kernel = Tophat2DKernel(radius=5)#5 source_kernel.normalize(mode='peak') source_kernel = source_kernel.array background_kernel = Ring2DKernel(radius_in=5, width=20)#20-10 background_kernel.normalize(mode='peak') background_kernel = background_kernel.array a = plt.imshow(source_kernel, interpolation='nearest', cmap='gray') plt.colorbar() plt.grid('off') if(show_it==False): plt.show(a) b = plt.imshow(background_kernel, interpolation='nearest', cmap='gray') plt.colorbar() plt.grid('off') if(show_it==False): plt.show(b)
from astropy.convolution import Ring2DKernel kernel = Ring2DKernel(1, 1) print(kernel.array)