def load(self): try: self.hdulist = hdus = fits.open(self.fullfilename) if hdus[2].columns[0].name=='CHANNEL': # binned format: assume next 2 columns are min, max and use geometric mean emin,emax = [hdus[2].data.field(i) for i in (1,2)] self.energies = np.sqrt(emin*emax) else: self.energies = hdus[2].data.field(0) self.vector_mode = len(hdus[1].columns)==1 if self.vector_mode: # one vector column, expect 2d array with shape (12*nside**2, len(energies)) self.spectra = hdus[1].data.field(0) self.nside = int(np.sqrt(self.spectra.shape[0]/12.)) assert self.spectra.shape[1]==len(self.energies), 'shape inconsistent with number of energies' else: # one column per energy: expect len(energies) columns hdu1 = hdus[1] assert len(hdu1.columns)==len(self.energies) , 'wrong number of columns' self.data = hdu1.data self.nside = int(np.sqrt(self.data.field(0).flatten().shape[0]/12.)) self.loaded=True self.indexfun = skymaps.Band(self.nside).index self.dirfun = skymaps.Band(self.nside).dir except Exception, msg: print 'bad file or unexpected FITS format, file %s: %s' % (self.fullfilename, msg) raise
def load_sources(self, roi_index, rings=1, tsmin=[0, 25, 100]): """ load sources from the roi and its neighbors in the pickle file found in modeldir roi_index : integer HEALPix index of the ROI rings : integer number of rings of concentric pixels to search for (fixed) sources to add Special value: if -1, do not add any sources at all tsmin : array minimun TS to accept sources in """ self.pickle_file = os.path.join(self.config.modeldir, 'pickle.zip') if not os.path.exists(self.pickle_file): raise Exception('Expected file "pickle.zip" not found in %s' % config.configdir) if hasattr(roi_index, '__len__'): roi_index = skymaps.Band(12).dir(skymaps.SkyDir(*roi_index)) self.index = roi_index self.roi_dir = skymaps.Band(12).dir(roi_index) self.name = 'HP12_%04d' % roi_index self._z = zipfile.ZipFile(os.path.expandvars(self.pickle_file)) global_only = rings == -1 self.load_sources_from_healpix((roi_index, 0), global_only=global_only) if global_only: return for neighbor_index in neighbors(roi_index, rings=rings): self.load_sources_from_healpix(neighbor_index, neighbors=True)
def fill_grid(self): # fill the grid for evaluating counts integral over ROI, individual pixel predictions roi_index = skymaps.Band(12).index(self.roicenter) dmodel = self.dmodel self.corr = 1.0 # TODO: modify this to use the kw "key" if exists haskey = hasattr(dmodel, 'kw') and dmodel.kw is not None if haskey and dmodel.kw.get('key', None)=='iso': et = ['front','back'][self.band.event_type] self.corr = DiffuseCorrection(None, diffuse.normalization['iso'][et])(roi_index, self.energy) #print roi_index, et, self.energy, self.corr elif haskey and 'correction' in dmodel.kw: # Set self.corr according to "correction" corr_file = dmodel.kw.get('correction', None) energy=round(self.band.energy) if corr_file is None: self.corr=1.0 else: roi_index = skymaps.Band(12).index(self.roicenter) self.corr = DiffuseCorrection(corr_file)(roi_index, self.energy) # dn = self.roi.sources.diffuse_normalization # if False: #dn is not None and 'iso' in dn: # # in processsself.corr = dn['iso'][('front','back')[self.]] # assert False, 'need to add if I want' # # load the correction value from the correction file # else: # self.corr = DiffuseCorrection(corr_file)(roi_index, energy) # #print 'ISO: {} {} MeV: apply correction {} '.format(corr_file, energy, self.corr) grid = self.grid grid.cvals = grid.fill(self.band.exposure) * dmodel(self.band.skydir) * self.corr
def initialize(self, force=False): if self.setup and not force: return self.setup = True #set up the spatial model self.dmodel = self.source.dmodel[self.band.event_type] self.dmodel.load() self.energy = self.band.energy self.dmodel.setEnergy(self.band.energy) roi_index = skymaps.Band(12).index(self.roicenter) self._keyword_check(roi_index) if getattr(self, 'preconvolved', False): #print 'Using preconvolved' c = self.roicenter cv = healpy.dir2vec(c.l(), c.b(), lonlat=True) hplist = healpy.query_disc(self.dmodel.nside, cv, self.band.radius_in_rad) smband = skymaps.Band(self.dmodel.nside) assert smband.index( self.roicenter ) in hplist, 'Problem HEALPix indexing, ROI center not consistent?' if self.dmodel.hdulist[1].header.get('TUNIT1', None) == 'photons': #must rescale from photons/pixel to density: photons/Mev/Sr scale_factor = (self.band.emax - self.band.emin) * smband.pixelArea() else: scale_factor = 1 dirs = map(self.dmodel.dirfun, hplist) self.evalpoints = lambda dirs: np.array(map( self.dmodel, dirs)) * self.corr / scale_factor self.ap_average = self.evalpoints(dirs).mean() # **** scale by years **** self.ap_average *= self.years / 10. else: self.create_grid() # will raise exception if no overlap grid = self.grid inside = grid.dists < self.band.radius_in_rad self.ap_average = grid.cvals[inside].mean() self.evalpoints = lambda dirs: grid(dirs, grid.cvals) self.delta_e = self.band.emax - self.band.emin self.factor = self.ap_average * self.band.solid_angle * self.delta_e if self.band.has_pixels: self.pixel_values = self.evalpoints( self.band.wsdl) * self.band.pixel_area * self.delta_e self.evaluate()
def neighbors(index, rings=1): """ return the cluster of pixel indeces around the pixel Parameters ---------- index : int specified pixel rings : int, optional number of rings around the pixel Returns a list of (index, ring_number) tuples """ b12 = skymaps.Band(12) v = IntVector() outer_ring = set([index]) cluster = set([index]) ret = [] for ring_number in range(rings): found = set([]) for i in outer_ring: b12.findNeighbors(int(i), v) found = found.union(set(v)) outer_ring = found.difference(cluster) ret = ret + [(x, ring_number + 1) for x in outer_ring] cluster = cluster.union(found) return ret
def __init__(self, healpix_index=None, pos=None, radius=5, radius_factor=0.5): """ parameters --------- healpix_index : [None | int ] pos : [None | tuple of float] radius : float radius_factor : float Factor to apply to define the ROI size for extraction of data to fit. This is needed to allow enough size to perform the convolution using a square grid """ if healpix_index is not None: self.index = healpix_index self.pos = skymaps.Band(12).dir(self.index) self.radius=radius self.name = 'HP12_%04d' % self.index elif pos is not None: self.index=None self.pos=skymaps.SkyDir(*pos[:2]) self.radius = pos[2]*radius_factor if len(pos)>2 else radius self.name = 'ROI(%.3f,%.3f, %.1f)' % ( self.pos.ra(),self.pos.dec(), self.radius) else: # no explicit index or position self.name = None pass
def extended(self, source_name='W28', roi=None, expect=(0, ), psf_check=True, model=None, quiet=True): source = ecat.lookup(source_name) if model is not None: source.model = model self.assertIsNotNone(source, 'Source %s not in catalog' % source_name) b12 = skymaps.Band(12) roi_index = roi if roi is not None else b12.index(source.skydir) roi_dir = b12.dir(roi_index) difference = np.degrees(roi_dir.difference(source.skydir)) band1 = bands.EnergyBand(self.config, roi_dir) if not quiet: print 'Using ROI #%d, distance=%.2f deg' % (roi_index, difference) print 'Testing source "%s at %s" with band parameters' % ( source, source.skydir) for item in band1.__dict__.items(): print '\t%-10s %s' % item self.resp = conv = source.response(band1) if not quiet: print 'overlap: %.3f, exposure_ratio: %.3f' % ( conv.overlap, conv.exposure_ratio) print 'PSF overlap: %.3f' % conv.psf_overlap if psf_check: self.assertAlmostEqual(conv.overlap, conv.psf_overlap, delta=1e-2) self.assertAlmostEqual(expect[0], conv.overlap, delta=1e-2) if len(expect) > 1: self.assertAlmostEqual(expect[1], conv.counts, delta=10)
def __init__(self, config, roi_index, load=False, radius=5): """create a list of energy bands config : configuration.Configuration object roi_index : int | None specify direction, either as nside=12 index, or from config.roi_spec load : bool if False, do not load data into the pixels """ self.config = config if roi_index is None or roi_index < 0: self.roi_dir = config.roi_spec.pos self.radius = config.roi_spec.radius roi_index = None else: ### HEALPix case self.roi_index = roi_index self.roi_dir = skymaps.Band(12).dir( roi_index) # could be defined otherwise self.radius = radius for emin, emax in zip(energybins[:-1], energybins[1:]): for et in config.dataset.event_types: if emin < event_type_min_energy[et]: continue self.append( EnergyBand(config, self.roi_dir, event_type=et, radius=self.radius, emin=emin, emax=emax)) self.has_data = False if load: self.load_data()
def create_grid(self): """ set up the grid from the cached files """ roi_index = skymaps.Band(12).index(self.roicenter) dmodel = self.dmodel try: self.filename = dmodel.files[roi_index] self.cached_diffuse = pickle.load(dmodel.opener(self.filename)) except Exception, msg: raise ResponseException( 'Diffuse cache file # %d not found:%s' %(roi_index,msg))
def initialize(self, force=False): if self.setup or force: return self.setup = True #set up the spatial model self.dmodel = self.source.dmodel[self.band.event_type] self.dmodel.load() self.energy = self.band.energy self.dmodel.setEnergy(self.band.energy) roi_index = skymaps.Band(12).index(self.roicenter) self._keyword_check(roi_index) if getattr(self, 'preconvolved', False): #print 'Using preconvolved' c = self.roicenter cv = healpy.dir2vec(c.l(), c.b(), lonlat=True) hplist = healpy.query_disc(self.dmodel.nside, cv, self.band.radius_in_rad) assert skymaps.Band(self.dmodel.nside).index( self.roicenter) in hplist dirs = map(self.dmodel.dirfun, hplist) self.evalpoints = lambda dirs: np.array(map(self.dmodel, dirs) ) * self.corr self.ap_average = self.evalpoints(dirs).mean() else: self.create_grid() # will raise exception if no overlap grid = self.grid inside = grid.dists < self.band.radius_in_rad self.ap_average = grid.cvals[inside].mean() self.evalpoints = lambda dirs: grid(dirs, grid.cvals) self.delta_e = self.band.emax - self.band.emin self.factor = self.ap_average * self.band.solid_angle * self.delta_e if self.band.has_pixels: self.pixel_values = self.evalpoints( self.band.wsdl) * self.band.pixel_area * self.delta_e self.evaluate()
def initialize(self, force=False): if self.setup or force: return self.setup = True #set up the spatial model self.dmodel = self.source.dmodel[self.band.event_type] self.dmodel.load() self.energy = self.band.energy self.dmodel.setEnergy(self.band.energy) roi_index = skymaps.Band(12).index(self.roicenter) self._keyword_check(roi_index) # self.create_grid() # grid = self.grid # inside = grid.dists< self.band.radius_in_rad # self.ap_average = grid.cvals[inside].mean() self.delta_e = self.band.emax - self.band.emin self.factor = self.ap_average * self.band.solid_angle * self.delta_e if self.band.has_pixels: assert False # self.pixel_values = grid(self.band.wsdl, grid.cvals) * self.band.pixel_area * self.delta_e self.evaluate()
def get_bands_for_ROI(self, index): return self.get_bands(skymaps.Band(12).dir(index), minROI=5)
$Header: /nfs/slac/g/glast/ground/cvs/pointlike/python/uw/like2/analyze/seedcheck.py,v 1.14 2018/01/27 15:39:29 burnett Exp $ """ import os, glob import numpy as np import pylab as plt import pandas as pd import skymaps from uw.like2.pub import healpix_map as hpm import healpy from astropy.io import fits from .. import associate from . import sourceinfo, _html from .analysis_base import FloatFormat, html_table b12index = skymaps.Band(12).index class SeedCheck(sourceinfo.SourceInfo): """Seed Analysis <p>This is a second stage after a run that produced a residual TS map. The first pipeline run, "sourcefinding", generates nside=512 HEALPix tables of the residual plots for the following spectral templates: <ul type="circle"> <li>flat</li> <li>hard</li> <li>soft</li> <li>peaked</li> <li>psr</li> </ul>. <p>