def make_gaussian_spatial_map(skydir, sigma, outfile, cdelt=None, npix=None): if cdelt is None: cdelt = sigma / 10. if npix is None: npix = int(np.ceil((6.0 * (sigma + cdelt)) / cdelt)) w = wcs_utils.create_wcs(skydir, cdelt=cdelt, crpix=npix / 2. + 0.5) hdu_image = fits.PrimaryHDU(np.zeros((npix, npix)), header=w.to_header()) hdu_image.data[:, :] = utils.make_gaussian_kernel(sigma, npix=npix, cdelt=cdelt) with fits.HDUList([hdu_image]) as hdulist: hdulist.writeto(outfile, overwrite=True)
def _make_residual_map_wcs(self, prefix, **kwargs): src_dict = copy.deepcopy(kwargs.setdefault('model', {})) exclude = kwargs.setdefault('exclude', None) loge_bounds = kwargs.setdefault('loge_bounds', None) use_weights = kwargs.setdefault('use_weights', False) if loge_bounds: if len(loge_bounds) != 2: raise Exception('Wrong size of loge_bounds array.') loge_bounds[0] = (loge_bounds[0] if loge_bounds[0] is not None else self.log_energies[0]) loge_bounds[1] = (loge_bounds[1] if loge_bounds[1] is not None else self.log_energies[-1]) else: loge_bounds = [self.log_energies[0], self.log_energies[-1]] # Put the test source at the pixel closest to the ROI center xpix, ypix = (np.round( (self.npix[0] - 1.0) / 2.), np.round((self.npix[1] - 1.0) / 2.)) cpix = np.array([xpix, ypix]) geom = self.geom.to_image() skywcs = self.geom.wcs skydir = wcs_utils.pix_to_skydir(cpix[0], cpix[1], skywcs) if src_dict is None: src_dict = {} src_dict['ra'] = skydir.ra.deg src_dict['dec'] = skydir.dec.deg src_dict.setdefault('SpatialModel', 'PointSource') src_dict.setdefault('SpatialWidth', 0.3) src_dict.setdefault('Index', 2.0) kernel = None if src_dict['SpatialModel'] == 'Gaussian': kernel = utils.make_gaussian_kernel(src_dict['SpatialWidth'], cdelt=self.binsz, npix=101) kernel /= np.sum(kernel) cpix = [50, 50] self.add_source('residmap_testsource', src_dict, free=True, init_source=False, save_source_maps=False) src = self.roi.get_source_by_name('residmap_testsource') modelname = utils.create_model_name(src) mmst = np.zeros(self.npix[::-1]) cmst = np.zeros(self.npix[::-1]) emst = np.zeros(self.npix[::-1]) sm = get_source_kernel(self, 'residmap_testsource', kernel) ts = np.zeros(self.npix[::-1]) sigma = np.zeros(self.npix[::-1]) excess = np.zeros(self.npix[::-1]) self.delete_source('residmap_testsource') for i, c in enumerate(self.components): imin = utils.val_to_edge(c.log_energies, loge_bounds[0])[0] imax = utils.val_to_edge(c.log_energies, loge_bounds[1])[0] mc = c.model_counts_map(exclude=exclude).data.astype('float') cc = c.counts_map().data.astype('float') ec = np.ones(mc.shape) if use_weights: wmap = c.weight_map().data mask = np.where(wmap > 0, 1., 0.) else: wmap = None mask = None ccs = convolve_map(cc, sm[i], cpix, imin=imin, imax=imax, wmap=wmap) mcs = convolve_map(mc, sm[i], cpix, imin=imin, imax=imax, wmap=wmap) ecs = convolve_map(ec, sm[i], cpix, imin=imin, imax=imax, wmap=wmap) cms = np.sum(ccs, axis=0) mms = np.sum(mcs, axis=0) ems = np.sum(ecs, axis=0) cmst += cms mmst += mms emst += ems # cts = 2.0 * (poisson_lnl(cms, cms) - poisson_lnl(cms, mms)) excess += cms - mms ts = 2.0 * (poisson_lnl(cmst, cmst) - poisson_lnl(cmst, mmst)) sigma = np.sqrt(ts) sigma[excess < 0] *= -1 emst /= np.max(emst) sigma_map = WcsNDMap(geom, sigma) model_map = WcsNDMap(geom, mmst / emst) data_map = WcsNDMap(geom, cmst / emst) excess_map = WcsNDMap(geom, excess / emst) o = { 'name': utils.join_strings([prefix, modelname]), 'projtype': 'WCS', 'file': None, 'sigma': sigma_map, 'model': model_map, 'data': data_map, 'excess': excess_map, 'mask': mask, 'config': kwargs } return o
def _make_residual_map_wcs(self, prefix, **kwargs): src_dict = copy.deepcopy(kwargs.setdefault('model', {})) exclude = kwargs.setdefault('exclude', None) loge_bounds = kwargs.setdefault('loge_bounds', None) use_weights = kwargs.setdefault('use_weights', False) if loge_bounds: if len(loge_bounds) != 2: raise Exception('Wrong size of loge_bounds array.') loge_bounds[0] = (loge_bounds[0] if loge_bounds[0] is not None else self.log_energies[0]) loge_bounds[1] = (loge_bounds[1] if loge_bounds[1] is not None else self.log_energies[-1]) else: loge_bounds = [self.log_energies[0], self.log_energies[-1]] # Put the test source at the pixel closest to the ROI center xpix, ypix = (np.round((self.npix - 1.0) / 2.), np.round((self.npix - 1.0) / 2.)) cpix = np.array([xpix, ypix]) geom = self.geom.to_image() skywcs = self.geom.wcs skydir = wcs_utils.pix_to_skydir(cpix[0], cpix[1], skywcs) if src_dict is None: src_dict = {} src_dict['ra'] = skydir.ra.deg src_dict['dec'] = skydir.dec.deg src_dict.setdefault('SpatialModel', 'PointSource') src_dict.setdefault('SpatialWidth', 0.3) src_dict.setdefault('Index', 2.0) kernel = None if src_dict['SpatialModel'] == 'Gaussian': kernel = utils.make_gaussian_kernel(src_dict['SpatialWidth'], cdelt=self.components[0].binsz, npix=101) kernel /= np.sum(kernel) cpix = [50, 50] self.add_source('residmap_testsource', src_dict, free=True, init_source=False, save_source_maps=False) src = self.roi.get_source_by_name('residmap_testsource') modelname = utils.create_model_name(src) npix = self.components[0].npix mmst = np.zeros((npix, npix)) cmst = np.zeros((npix, npix)) emst = np.zeros((npix, npix)) sm = get_source_kernel(self, 'residmap_testsource', kernel) ts = np.zeros((npix, npix)) sigma = np.zeros((npix, npix)) excess = np.zeros((npix, npix)) self.delete_source('residmap_testsource') for i, c in enumerate(self.components): imin = utils.val_to_edge(c.log_energies, loge_bounds[0])[0] imax = utils.val_to_edge(c.log_energies, loge_bounds[1])[0] mc = c.model_counts_map(exclude=exclude).data.astype('float') cc = c.counts_map().data.astype('float') ec = np.ones(mc.shape) if use_weights: wmap = c.weight_map().data mask = np.where(wmap > 0, 1., 0.) else: wmap = None mask = None ccs = convolve_map( cc, sm[i], cpix, imin=imin, imax=imax, wmap=wmap) mcs = convolve_map( mc, sm[i], cpix, imin=imin, imax=imax, wmap=wmap) ecs = convolve_map( ec, sm[i], cpix, imin=imin, imax=imax, wmap=wmap) cms = np.sum(ccs, axis=0) mms = np.sum(mcs, axis=0) ems = np.sum(ecs, axis=0) cmst += cms mmst += mms emst += ems # cts = 2.0 * (poisson_lnl(cms, cms) - poisson_lnl(cms, mms)) excess += cms - mms ts = 2.0 * (poisson_lnl(cmst, cmst) - poisson_lnl(cmst, mmst)) sigma = np.sqrt(ts) sigma[excess < 0] *= -1 emst /= np.max(emst) sigma_map = WcsNDMap(geom, sigma) model_map = WcsNDMap(geom, mmst / emst) data_map = WcsNDMap(geom, cmst / emst) excess_map = WcsNDMap(geom, excess / emst) o = {'name': utils.join_strings([prefix, modelname]), 'projtype': 'WCS', 'file': None, 'sigma': sigma_map, 'model': model_map, 'data': data_map, 'excess': excess_map, 'mask': mask, 'config': kwargs} return o
def _make_residual_map(self, prefix, config, **kwargs): write_fits = kwargs.get('write_fits', True) write_npy = kwargs.get('write_npy', True) src_dict = copy.deepcopy(config.setdefault('model', {})) exclude = config.setdefault('exclude', None) loge_bounds = config.setdefault('loge_bounds', None) if loge_bounds is not None: if len(loge_bounds) == 0: loge_bounds = [None, None] elif len(loge_bounds) == 1: loge_bounds += [None] loge_bounds[0] = (loge_bounds[0] if loge_bounds[0] is not None else self.energies[0]) loge_bounds[1] = (loge_bounds[1] if loge_bounds[1] is not None else self.energies[-1]) else: loge_bounds = [self.energies[0], self.energies[-1]] # Put the test source at the pixel closest to the ROI center xpix, ypix = (np.round( (self.npix - 1.0) / 2.), np.round((self.npix - 1.0) / 2.)) cpix = np.array([xpix, ypix]) skywcs = self._skywcs skydir = wcs_utils.pix_to_skydir(cpix[0], cpix[1], skywcs) if src_dict is None: src_dict = {} src_dict['ra'] = skydir.ra.deg src_dict['dec'] = skydir.dec.deg src_dict.setdefault('SpatialModel', 'PointSource') src_dict.setdefault('SpatialWidth', 0.3) src_dict.setdefault('Index', 2.0) kernel = None if src_dict['SpatialModel'] == 'Gaussian': kernel = utils.make_gaussian_kernel(src_dict['SpatialWidth'], cdelt=self.components[0].binsz, npix=101) kernel /= np.sum(kernel) cpix = [50, 50] self.add_source('residmap_testsource', src_dict, free=True, init_source=False, save_source_maps=False) src = self.roi.get_source_by_name('residmap_testsource') modelname = utils.create_model_name(src) npix = self.components[0].npix mmst = np.zeros((npix, npix)) cmst = np.zeros((npix, npix)) emst = np.zeros((npix, npix)) sm = get_source_kernel(self, 'residmap_testsource', kernel) ts = np.zeros((npix, npix)) sigma = np.zeros((npix, npix)) excess = np.zeros((npix, npix)) self.delete_source('residmap_testsource') for i, c in enumerate(self.components): imin = utils.val_to_edge(c.energies, loge_bounds[0])[0] imax = utils.val_to_edge(c.energies, loge_bounds[1])[0] mc = c.model_counts_map(exclude=exclude).counts.astype('float') cc = c.counts_map().counts.astype('float') ec = np.ones(mc.shape) ccs = convolve_map(cc, sm[i], cpix, imin=imin, imax=imax) mcs = convolve_map(mc, sm[i], cpix, imin=imin, imax=imax) ecs = convolve_map(ec, sm[i], cpix, imin=imin, imax=imax) cms = np.sum(ccs, axis=0) mms = np.sum(mcs, axis=0) ems = np.sum(ecs, axis=0) cmst += cms mmst += mms emst += ems # cts = 2.0 * (poisson_lnl(cms, cms) - poisson_lnl(cms, mms)) excess += cms - mms ts = 2.0 * (poisson_lnl(cmst, cmst) - poisson_lnl(cmst, mmst)) sigma = np.sqrt(ts) sigma[excess < 0] *= -1 emst /= np.max(emst) sigma_map = Map(sigma, skywcs) model_map = Map(mmst / emst, skywcs) data_map = Map(cmst / emst, skywcs) excess_map = Map(excess / emst, skywcs) o = { 'name': utils.join_strings([prefix, modelname]), 'file': None, 'sigma': sigma_map, 'model': model_map, 'data': data_map, 'excess': excess_map, 'config': config } fits_file = utils.format_filename(self.config['fileio']['workdir'], 'residmap.fits', prefix=[prefix, modelname]) if write_fits: fits_utils.write_maps( sigma_map, { 'DATA_MAP': data_map, 'MODEL_MAP': model_map, 'EXCESS_MAP': excess_map }, fits_file) o['file'] = os.path.basename(fits_file) if write_npy: np.save(os.path.splitext(fits_file)[0] + '.npy', o) return o
def make_residual_map(self,src_dict,prefix,**kwargs): exclude = kwargs.get('exclude',None) # Put the test source at the pixel closest to the ROI center xpix, ypix = (np.round((self._gta.npix-1.0)/2.), np.round((self._gta.npix-1.0)/2.)) cpix = np.array([xpix,ypix]) skywcs = self._gta._skywcs skydir = utils.pix_to_skydir(cpix[0],cpix[1],skywcs) src_dict['ra'] = skydir.ra.deg src_dict['dec'] = skydir.dec.deg src_dict.setdefault('SpatialModel','PointSource') src_dict.setdefault('SpatialWidth',0.3) src_dict.setdefault('Index',2.0) kernel = None if src_dict['SpatialModel'] == 'Gaussian': kernel = utils.make_gaussian_kernel(src_dict['SpatialWidth'], cdelt=self._gta.components[0].binsz, npix=101) kernel /= np.sum(kernel) cpix = [50,50] self._gta.add_source('testsource',src_dict,free=True,init_source=False) src = self._gta.roi.get_source_by_name('testsource',True) modelname = create_model_name(src) enumbins = self._gta.enumbins npix = self._gta.components[0].npix mmst = np.zeros((npix,npix)) cmst = np.zeros((npix,npix)) emst = np.zeros((npix,npix)) sm = self.get_source_mask('testsource',kernel) ts = np.zeros((npix,npix)) sigma = np.zeros((npix,npix)) excess = np.zeros((npix,npix)) self._gta.delete_source('testsource') for i, c in enumerate(self._gta.components): mc = c.model_counts_map(exclude=exclude).counts.astype('float') cc = c.counts_map().counts.astype('float') ec = np.ones(mc.shape) ccs = smooth(cc,sm[i],cpix) mcs = smooth(mc,sm[i],cpix) ecs = smooth(ec,sm[i],cpix) cms = np.sum(ccs,axis=0) mms = np.sum(mcs,axis=0) ems = np.sum(ecs,axis=0) cmst += cms mmst += mms emst += ems cts = 2.0*(poisson_lnl(cms,cms) - poisson_lnl(cms,mms)) excess += cms - mms ts = 2.0*(poisson_lnl(cmst,cmst) - poisson_lnl(cmst,mmst)) sigma = np.sqrt(ts) sigma[excess<0] *= -1 sigma_map_file = os.path.join(self.config['fileio']['workdir'], '%s_residmap_%s_sigma.fits'%(prefix,modelname)) data_map_file = os.path.join(self.config['fileio']['workdir'], '%s_residmap_%s_data.fits'%(prefix,modelname)) model_map_file = os.path.join(self.config['fileio']['workdir'], '%s_residmap_%s_model.fits'%(prefix,modelname)) excess_map_file = os.path.join(self.config['fileio']['workdir'], '%s_residmap_%s_excess.fits'%(prefix,modelname)) emst /= np.max(emst) utils.write_fits_image(sigma,skywcs,sigma_map_file) utils.write_fits_image(cmst/emst,skywcs,data_map_file) utils.write_fits_image(mmst/emst,skywcs,model_map_file) utils.write_fits_image(excess/emst,skywcs,excess_map_file) files = { 'sigma' : os.path.basename(sigma_map_file), 'model' : os.path.basename(model_map_file), 'data' : os.path.basename(data_map_file), 'excess' : os.path.basename(excess_map_file) } o = { 'name' : '%s_%s'%(prefix,modelname), 'files' : files, 'wcs' : skywcs, 'sigma' : Map(sigma,skywcs), 'model' : Map(mmst/emst,skywcs), 'data' : Map(cmst/emst,skywcs), 'excess' : Map(excess/emst,skywcs) } return o