def __init__(self, other, nside=256): self.name=other.name self.vec = other.vec self.nside =nside self._nside = int(np.sqrt(len(self.vec)/12) ) # internal self.dirfun = Band(self.nside).dir # external direction from index self._indexfun = Band(self._nside).index #internal index from direction
def __init__(self, table_name='kde', index=860, nside=nside, **kwargs): """ table_name : index : nside : """ folder = '%s_table_%d' % (table_name, nside) assert os.path.exists(folder), 'Folder %s not found' % folder self.hmap = pickle.load(open('%s/HP12_%04d.pickle' % (folder, index))) self.index = index self.hpindices = list(make_index_table(12, nside)[index]) self.hpdict = dict((self.hpindices[i], self.hmap[i]) for i in range(len(self.hpindices))) self.center = Band(12).dir(index) self.indexfun = Band(nside).index self.ZEA_kw = kwargs.pop('ZEA_kw', dict(galactic=True, size=10, pixelsize=0.05)) self.imshow_kw = dict(interpolation='bilinear', ) self.scale = kwargs.pop('scale', lambda x: x) if type(self.scale) == types.StringTypes: if self.scale == 'sqrt': self.scale = lambda x: np.sqrt(max(x, 0)) elif self.scale == 'log': self.scale = lambda x: np.log10(max(x, 0.1)) else: raise Exception, 'unrecognized scale function, %s' % self.scale
def load(self, filename): d = load(file(filename)) self.inds = d['inds'] self.band1 = Band(d['nside1']) self.band2 = Band(d['nside2']) self.img = d['img'] self.index = d['index'] self.previous_index = -1
def __init__(self, name, skyfun, nside): """ skyfun : function of position """ self.name = name self.skyfun = skyfun self.nside = nside self.dirfun = Band(self.nside).dir self._indexfun = Band(self.nside).index
def select_seeds_in_roi(roi, fn='seeds/seeds_all.csv'): """ Read seeds from csv file, return those in the given ROI roi : int or Process instance if the latter, look up index from roi direction. direction """ if type(roi) != int: roi = Band(12).index(roi.roi_dir) seeds = pd.read_csv(fn, index_col=0) seeds['skydir'] = map(SkyDir, seeds.ra, seeds.dec) seeds.index.name = 'name' sel = np.array(map(Band(12).index, seeds.skydir)) == roi return seeds[sel]
def make_index_table(nside, subnside, usefile=True): filename = 'index_table_%02d_%03d.pickle' % (nside, subnside) if os.path.exists(filename) and usefile: return pickle.load(open(filename)) print 'generating index table for nside, subnside= %d %d' % (nside, subnside) band, subband = Band(nside), Band(subnside) npix, nsubpix = 12 * nside**2, 12 * subnside**2 t = np.array([band.index(subband.dir(i)) for i in xrange(nsubpix)]) a = np.arange(nsubpix) index_table = [a[t == i] for i in xrange(npix)] if usefile: pickle.dump(index_table, open(filename, 'w')) return index_table
def make_index_table(nside=12, subnside=512, usefile=True): """create, and/or use a table to convert between different nside pixelizations """ filename = os.path.expandvars('$FERMI/misc/index_table_%02d_%03d.pickle' % (nside, subnside) ) if os.path.exists(filename) and usefile: return pickle.load(open(filename)) print 'generating index table for nside, subnside= %d %d' % (nside, subnside) band, subband = Band(nside), Band(subnside) npix, nsubpix = 12*nside**2, 12*subnside**2 t=np.array([band.index(subband.dir(i)) for i in xrange(nsubpix)]) a = np.arange(nsubpix) index_table = [a[t==i] for i in xrange(npix)] if usefile: pickle.dump(index_table, open(filename,'w')) return index_table
def load(self, filename): d = load(file(filename)) self.inds = d['inds'] self.band1 = Band(d['nside1']) self.band2 = Band(d['nside2']) if 'ts_vals' in d.keys(): self.ts_vals = d['ts_vals'] else: dim = (self.band2.nside() / self.band1.nside())**2 self.ts_vals = np.empty([3, dim]) self.ts_vals[:] = np.nan self.index = d['index'] self.ts = self.ts_vals[0] self.previous_index = -1 self.mode = 0
def make_pictures(config_dir='.', image_folder = 'extended_images'): """ make a folder with images for all extended sources """ from uw.like2 import configuration from skymaps import Band if not os.path.exists(image_folder): os.mkdir(image_folder) cf = configuration.Configuration(config_dir, quiet=True, postpone=True) class Bandlite(object): def __init__(self, roi_dir, event_type=1, energy =133.352): self.event_type=event_type self.energy=energy self.sd=roi_dir self.psf=cf.psfman(event_type,energy) self.exposure = cf.exposureman(event_type,energy) self.radius =5 ecat = ExtendedCatalog(cf.extended) for name in ecat.names: print 'processing %s ...' % name , source = ecat.lookup(name) b12 = Band(12); roi_index = b12.index(source.skydir); roi_dir = b12.dir(roi_index) conv = convolution.ExtendedConvolver(source, Bandlite(roi_dir=roi_dir)) conv.create_grid() fig = conv.show_source() fig.savefig('%s/%s_map.png' %(image_folder,name.replace(' ','_'))) print
class FromCCube(HParray): """Manage the gtlike HEALPix counts map """ def __init__(self, filename): """ load array from a HEALPix-format FITS file """ assert os.path.exists(filename), 'File "{}" not found'.format(filename) self.hdus = pyfits.open(filename) self.data = self.hdus[1].data self.nside = self.hdus[1].header['NSIDE'] # expect to find circle in header hdr = self.hdus[0].header try: dstypes = sorted( filter(lambda n: n.startswith('DSTYP'), hdr.keys())) i = [hdr[x] for x in dstypes].index('POS(RA,DEC)') i circ = hdr['DSVAL{}'.format(i)] ra, dec, self.radius = np.array(circ[7:-1].split(','), float) except Exception, msg: print 'failed to parse file {}: expected header to have a DSVAL with POS(RA,DEC)'.format( filename), msg raise self.center = SkyDir(ra, dec) self.indexfun = Band(self.nside).index self.lookup = dict(zip(self.data.PIX, self.data.CHANNEL1))
def __init__(self, outdir, nside, roi_nside=12, **kwargs): self.subdirfun = Band(nside).dir disc_info = kwargs.pop('disc_info', ()) # allow arbitraty disk, rather than just within a ROI active boundary if disc_info: l, b, r = disc_info vec = healpy.dir2vec(l, b, lonlat=True) index_list = healpy.query_disc(nside=nside, vec=vec, radius=np.radians(r), nest=False) self.pos_list = [self.subdirfun(int(i)) for i in index_list] else: self.index_table = make_index_table(roi_nside, nside) self.skyfuns = kwargs.pop( 'skyfuns', ( ( ResidualTS, 'ts', dict(photon_index=2.3), ), (KdeMap, 'kde', dict()), ), ) self.subdirs = [ os.path.join(outdir, name + '_table_%d' % nside) for s, name, kw in self.skyfuns ] for subdir in self.subdirs: if not os.path.exists(subdir): os.makedirs(subdir)
def process_band(self, iband, verbose=False): self.select_band(iband) dflux= self.dflux.astype(float) if verbose: m = dflux.mean(axis=2); s = dflux.std(axis=2)/m ids = 'Front Back'.split() if dflux.shape[0]==2 else ['Front'] print 'mean\n', pd.DataFrame(m, index=ids, columns='gal iso'.split()) print 'rms/mean\n',pd.DataFrame(s, index=ids, columns='gal iso'.split()) x=self.fa[iband,:] if verbose: print 'Fits\n', x a64=Band(64).pixelArea() f=(dflux*a64); # iso/gal ratio r = f[:,1,:]/f[:,0,:]; if verbose: print 'ratio\n', r # adjusted fits per (flux, pixel) af = x[0] + r*(x[1]-1); if verbose: print 'adjusted fits\n',af # isolate the galactic flux f_gal = f[:,0]; if verbose: print 'galactic fit\n',f_gal # weighted sum of the adjusted fits by the galactic flux waf = (f_gal*af).sum(axis=0) / f_gal.sum(axis=0); if verbose: print 'weighted fit\n',self.waf return waf
def __init__(self, roi, roi_index=None, diffuse_fits=None, nside=64): """ """ self.roi = roi if roi_index is not None: print "setting up index {}".format(roi_index) roi.setup_roi(roi_index) # Process object external for now else: roi_index = int(roi.name[-4:]) self.ri = roi_index self.pdirs = map(Band(nside).dir, maps.make_index_table(12,nside)[roi_index]) print 'Processing ROI index {}'.format(roi_index) # load diffuse fits for this model if diffuse_fits is None: files = sorted(glob.glob('diffuse_fit/*.pickle')) assert len(files)>0, 'no files found' if len(files)<1728: msg= "found {} files, expected 1728".format(len(files)) print msg raise Exception(msg) # return as an array 1728x8x2, last being (gal,iso) self.fa = (pd.read_pickle(files[roi_index]).values[:,:2]).astype(float) print 'Loaded diffuse fits for this RoI' else: self.fa = diffuse_fits.values[:,:2].astype(float) print 'Using fits just generated'
def roi_circle(roi_index, galactic=True, radius=5.0): """ return (lon,lat,radius) tuple for given nside=12 position """ from skymaps import Band sdir = Band(12).dir(roi_index) return (sdir.l(), sdir.b(), radius) if galactic else (sdir.ra(), sdir.dec(), radius)
def plot(self, title=None, axes=None, fignum=30, ait_kw={}, log=False, **kwargs): """ make an AIT skyplot from the array title : string set the figure title. If None, use the name ait_kw : dict to set kwargs for image.AIT, perhaps pixelsize Other args passed to imshow, for example norm: to make a log plot, from matplotlib.colors import LogNorm plot(norm=LogNorm(vmin=1,vmax=10)) It returns a image.AIT object, which has a colorbar member that can be adjusted. """ cbtext = kwargs.pop('cbtext', '') if log: vmin=kwargs.pop('vmin', None) vmax=kwargs.pop('vmax', None) kwargs['norm']=LogNorm(vmin=vmin,vmax=vmax) ait_kw.update(cbtext=cbtext) band = Band(self.nside) def skyplotfun(v): skydir = SkyDir(Hep3Vector(v[0],v[1],v[2])) return self(skydir) if axes is None: plt.close(fignum) fig = plt.figure(fignum, figsize=(12,6)) axes = plt.gca() else: fig = axes.figure ait=image.AIT(PySkyFunction(skyplotfun) ,axes=axes, **ait_kw) ait.imshow( **kwargs) axes.set_title(self.name if title is None else title, loc='left') fig.set_facecolor('white') return ait
def residual_map(index): cband = roi.config.dataset.dmap[index] # need to find appropriate one nside = cband.nside() b = roi[index] # the BandLike object assert abs(cband.emin() - b.band.emin) < 1.0, '{},{}'.format( cband.emin(), b.band.emin) energy = b.band.energy event_type = b.band.event_type band_label = '{:.0f} Mev {}'.format(energy, ['Front', 'Back'][event_type]) # get pixel info: counts, model, positions wsdl = b.band.wsdl # list of WeightedSKyDir objects ids = np.array(map(cband.index, wsdl)) # list of ids with nside id12 = np.array(map(Band(12).index, wsdl), int) # nside=12 ids inside = id12 == roi_id data = b.data model = np.array(b.model_pixels, np.float32) dd = dict( band_index=index, nside=nside, energy=energy, event_type=event_type, ids=ids, model=model, data=data, inside=inside, ) chi2 = sum((data - model)**2 / model) print '{:5.0f} {:5d} {:4d} {:4.0f}/{:4d} {:+0.1%}'.format( energy, event_type, nside, chi2, len(data), (data / model - 1).mean()) return dd
def check_converge(month, tol=10, add_neighbors=True, log=None): """ check for convergence, ROI that have been updated month: int or string if int, intrepret as a month, else a folder """ from pointlike import IntVector from skymaps import Band outdir = 'month%02d' % month if type(month) == types.IntType else month #print '%s:' %outdir r = roirec(outdir) if r is None: return diff = r.loglike - r.prevlike nisnan = sum(np.isnan(diff)) if nisnan > 0: print 'warning: %d NaN values in likelihoods: ignoring them' % nisnan diff[np.isnan(diff)] = 0 dmin, dmax = diff.min(), diff.max() rmin, rmax = list(diff).index(dmin), list(diff).index(dmax) changed = set(np.arange(1728)[np.abs(diff) > tol]) print >> log, '\tpass %d: %d changed > %d, min, max: %d(#%d) %d(#%d)' % ( max(r.niter), len(changed), tol, dmin, rmin, dmax, rmax), if not add_neighbors: return list(changed) nbrs = set() b12 = Band(12) for x in changed: v = IntVector() b12.findNeighbors(int(x), v) # int is tricky for n in v: nbrs.add(n) q = list(changed.union(nbrs)) print >> log, ' (total %d)' % len(q) if log is not None: log.flush() return q
def plot(self, title='', axes=None, fignum=30, ait_kw={}, **kwargs): """ make an AIT skyplot from the array title : string set the figure title ait_kw : dict to set kwargs for image.AIT, perhaps pixelsize Other args passed to imshow, for example norm: to make a log plot, from matplotlib.colors import LogNorm plot(norm=LogNorm(vmin=1,vmax=10)) It returns a image.AIT object, which has a colorbar member that can be adjusted. """ cbtext = kwargs.pop('cbtext', '') ait_kw.update(cbtext=cbtext) band = Band(self.nside) def skyplotfun(v): skydir = SkyDir(Hep3Vector(v[0], v[1], v[2])) return self(skydir) if axes is None: plt.close(fignum) fig = plt.figure(fignum, figsize=(12, 6)) axes = plt.gca() ait = image.AIT(PySkyFunction(skyplotfun), axes=axes, **ait_kw) ait.imshow(title=title, **kwargs) return ait
def pulsar_check(self): """LAT pulsar check %(atable)s """ # compare with LAT pulsar catalog lat = self.lcatdf lat['ts'] = self.df[self.df.psr]['ts'] lat['aprob'] = self.df[self.df.psr]['aprob'] lat['ROI_index'] = [ Band(12).index(SkyDir(float(ra), float(dec))) for ra, dec in zip(lat.RAJ2000, lat.DEJ2000) ] lat['skydir'] = [ SkyDir(float(ra), float(dec)) for ra, dec in zip(lat.RAJ2000, lat.DEJ2000) ] lat['sourcedir'] = self.df.skydir[self.df.psr] lat['delta'] = [ np.degrees(s.difference(t)) if not type(t) == float else np.nan for s, t in zip(lat.skydir, lat.sourcedir) ] far = lat.delta > 0.25 dc2names = set(self.lcatdf.index) tt = set(self.df.name[self.df.psr]) print 'Catalog entries not found:', list(dc2names.difference(tt)) missing = np.array([np.isnan(x) or x < 10. for x in lat.ts]) missing |= np.array((lat.aprob == 0) & (lat.ts < 1000)) missing_names = lat.index[missing] cols = 'RAJ2000 DEJ2000 ts delta ROI_index'.split() self.latsel = latsel = pd.DataFrame(np.array( [lat[id][missing] for id in cols]), index=cols, columns=missing_names).T self.atable = '<h4>Compare with LAT pulsar catalog: {}</h4>'.format( self.version) label_info = dict( ts='TS,Test Statistic', delta='delta,distance to fit position (deg)', ROI_index='ROI Index,Index of the ROI, a HEALPix ring index') self.atable += html_table( latsel.query('ts<10'), label_info, heading='<p>LAT catalog entries with weak or no fit (TS<10)', name=self.plotfolder + '/weak', maxlines=20, float_format=(FloatFormat(2))) self.atable += html_table( latsel.query('ts>10'), label_info, heading= '<p>LAT catalog entries with nearby, but unassociated source ', name=self.plotfolder + '/far', maxlines=20, float_format=(FloatFormat(2)))
def __call__(self, band, nside, index, ps_dir): """Return an array of fractional overlap for a point source at location skydir.""" # pick an nside commensurate with PSF and that will fit evenly within base pixel scale = band.psf.parent_psf.scale_func[band.ct](band.e) / 5 sample_nside = (np.pi / 3)**0.5 / scale p0 = np.log(sample_nside / nside) / np.log(2) last_overlap = None overlaps = [] for addon in [0]: sample_nside = min(8192, nside * 2**(int(p0) + addon)) geo = (4 * np.pi / 12 / sample_nside**2) #print sample_nside,index # temporary code for testing! if True: from skymaps import Band band.b = Band(sample_nside) wsdl = WeightedSkyDirList(band.b, nside, index, True) vals = np.empty(len(wsdl)) band.psf.cpsf.wsdl_val(vals, ps_dir, wsdl) overlap = vals.sum() * geo #print overlap overlaps.append(overlap) #if last_overlap is not None: # new_est = (2*last_overlap+4*overlap)/6 # print 'Updated estimate: ',new_est last_overlap = overlap #print ('%.6f\t'*(len(overlaps)))%(tuple(overlaps)) return overlaps[-1], sample_nside
def __init__(self, table, sources=None, imshow_kw=dict(interpolation='bilinear', ), **kwargs): """table : string or iterable If a string, the name of a pickled file sources : None or a string if a string, the name of a pickled rec with name, ra, dec fields """ if type(table)==types.StringType: self.v = pickle.load(open(table)) print 'Loaded HEALpix table from file %s' %table else: self.v=table self.nside = int(np.sqrt(len(self.v)/12)) assert len(self.v)==12*self.nside**2, 'size of map not consistent with expected nside %d' % nside self.band = Band(self.nside) self.imshow_kw=imshow_kw self.scale = kwargs.pop('scale', lambda x: x) if type(self.scale) == types.StringTypes: if self.scale=='sqrt': self.scale= lambda x: np.sqrt(max(x,0)) elif self.scale=='log': self.scale=lambda x: np.log10(max(x,0.1)) else: raise Exception, 'unrecognized scale function, %s' %self.scale self.ZEA_kw = kwargs.pop('ZEA_kw', dict(galactic=True, size=10, pixelsize=0.1)) if sources is not None: self.sources = pickle.load(open(sources)) print 'loaded %d sources from %s' % (len(self.sources),sources) else:self.sources=None self.map_path = kwargs.pop('map_path',None)
def load_skyspect(fn = r'T:\data\galprop\ring_21month_P6v11.fits', # r'D:\fermi\data\galprop\gll_iem_v02.fit', nside=192, show_kw = dict(fun=np.log10, cmap='hot'), ): """ load a galactic diffuse distribution. Save the HEALpix respresentation at an energy (1 GeV default) fn : string filename for the FITS representaion of a SKySpectrum nside: int HEALpix nside to use for represenation -- note that 192 is 12*16, about 0.25 deg show_kw : dict fun: weighting function, cmap, vmin, vmax """ t = SkyImage(fn) galname = os.path.split(fn)[-1] print '%s: nx, ny, layers: %d %d %d' %(galname, t.naxis1(), t.naxis2(), t.layers()) hpdir = Band(nside).dir dmap = map(lambda i:t(hpdir(i)), xrange(12*nside**2)) tdm=DisplayMap(dmap) tdm.fill_ait(fignum=12, source_kw=dict(edgecolor='w',), show_kw=show_kw ) plt.title(galname+' (1 GeV)') sfn = galname.split('.')[0]+'.png' plt.savefig(galname.split('.')[0]+'.png', bbox_inches='tight', pad_inches=0) print 'saved figure to %s' % sfn return tdm
def __init__(self, nside=12): """ nside: integer the HEALpix nside parameter """ self.v = np.zeros(12*nside**2, float) self.index = Band(nside).index
def skyplot(crec, title='', axes=None, fignum=30, ait_kw={}, **kwargs): """ make an AIT skyplot of a HEALpix array crec : array must be sorted according to the HEALpix index title : string set the figure title ait_kw : dict to set kwargs for image.AIT, perhaps pixelsize Other args passed to imshow """ n = len(crec) nside = int(np.sqrt(n/12)) assert n==12*nside**2, 'wrong length to be healpix array' band = Band(nside) def skyplotfun(v): skydir = SkyDir(Hep3Vector(v[0],v[1],v[2])) index = band.index(skydir) return crec[index] if axes is None: plt.close(fignum) fig = plt.figure(fignum, figsize=(12,6)) ait=image.AIT(PySkyFunction(skyplotfun) ,axes=axes, **ait_kw) ait.imshow(title=title, **kwargs) return ait
def __init__(self, hptable, outdir, map_dir=None, nside=12, imshow_kw=dict( interpolation='bilinear', vmin=0, vmax=100, ), **kwargs): """ hptable : HParray object expect to have vec, nside members outdir : string folder containing the skymodel map_dir : None, string folder name to save the images """ from .. import skymodel self.v = hptable.vec self.subband = Band(hptable.nside) self.band = Band(nside) self.n = 12 * nside**2 self.imshow_kw = imshow_kw self.scale = kwargs.pop('scale', lambda x: x) if type(self.scale) == types.StringTypes: if self.scale == 'sqrt': self.scale = lambda x: np.sqrt(max(x, 0)) elif self.scale == 'log': self.scale = lambda x: np.log10(max(x, 0.1)) else: raise Exception, 'unrecognized scale function, %s' % self.scale print 'Can generate %d map figures' % (self.n) self.outdir = outdir self.ZEA_kw = kwargs.pop('ZEA_kw', dict()) if map_dir is not None: self.map_path = os.path.join(outdir, map_dir) if not os.path.exists(self.map_path): os.makedirs(self.map_path) print 'will save figures in folder %s' % self.map_path else: self.map_path = None skm = skymodel.SkyModel(outdir) self.sources = skm.point_sources + skm.extended_sources print 'loaded %d sources from skymodel %s' % (len( self.sources), outdir)
def __init__(self, name, vec): self.name = name self.vec = vec self.nside = int(np.sqrt(len(vec) / 12)) assert len( self.vec ) == 12 * self.nside**2, 'length of %s not consistent with HEALPix' % self.name self._indexfun = Band(self.nside).index
def residual_maps(roi, folder='residual_maps', nbands=4, test=False): def residual_map(index): b = roi[index] # the BandLike object cband = roi.config.dataset.dmap[ b.band.data_index] # need to find appropriate one nside = cband.nside() b = roi[index] # the BandLike object assert abs(cband.emin() - b.band.emin) < 1.0, '{},{}'.format( cband.emin(), b.band.emin) energy = b.band.energy event_type = b.band.event_type band_label = '{:.0f} Mev {}'.format(energy, ['Front', 'Back'][event_type]) # get pixel info: counts, model, positions wsdl = b.band.wsdl # list of WeightedSKyDir objects ids = np.array(map(cband.index, wsdl)) # list of ids with nside id12 = np.array(map(Band(12).index, wsdl), int) # nside=12 ids inside = id12 == roi_id data = b.data model = np.array(b.model_pixels, np.float32) galactic = np.array(b[0].pix_counts) #assume! isotropic = np.array(b[1].pix_counts) # combine counts for all local sources source_cnts = np.zeros(b.pixels, np.float32) for m in b.bandsources: if m.source.skydir is None: continue if m.counts > 0: source_cnts += m.pix_counts dd = dict(band_index=index, nside=nside, energy=energy, event_type=event_type, ids=ids, model=model, data=data, inside=inside, source=source_cnts, galactic=galactic, isotropic=isotropic) chi2 = sum((data - model)**2 / model) print '{:5.0f} {:5d} {:4d} {:4.0f}/{:4d} {:+0.1%}'.format( energy, event_type, nside, chi2, len(data), (data / model - 1).mean()) return dd roi_id = Band(12).index(roi.roi_dir) print 'energy type nside chi2/ ndf offset' maps = [residual_map(index) for index in range(nbands)] if test: return maps if not os.path.exists(folder): os.mkdir(folder) filename = '{}/ROI_{:04d}.pickle'.format(folder, roi_id) pickle.dump(maps, open(filename, 'w')) print 'Wrote file {}'.format(filename)
def inside(self): """a list of bools for pixels with data that are inside the active part of the ROI Needed to make a mask """ b12index = Band(12).index roi_id = b12index(self.roi.roi_dir) id12 = np.array(map(b12index, self.pixel_dirs), int) #ids of data pixels for nside=12 return id12 == roi_id
def __init__(self, config_dir, roi_spec=None, xml_file=None, **kwargs): """Start pointlike v2 (like2) in the specified ROI parameters ---------- config_dir : string file path to a folder containing a file config.txt see configuration.Configuration roi_spec : [None |integer | (ra,dec) tuple ] If None, require that the input_model dict has a key 'xml_file' if an integer, it must be <1728, the ROI number if a string, assume a source name and load the ROI containing it """ keyword_options.process(self, kwargs) self.config = config = configuration.Configuration( config_dir, quiet=self.quiet, postpone=self.postpone) ecat = extended.ExtendedCatalog(config.extended) if isinstance(roi_spec, str): # try: # roi_sources =from_xml.ROImodelFromXML(config, roi_spec) # roi_index = roi_sources.index # except: # print 'No ROI specification (an index) or presence of an xml file' # raise # Change to just expecting the name of a source sourcelist = glob.glob('sources_*.csv')[0] df = pd.read_csv(sourcelist, index_col=3 if roi_spec[0] == 'J' else 0) if roi_spec not in df.index: print 'Source name "{}" not found '.format(roi_spec) raise Exception roi_index = int(df.loc[roi_spec]['roiname'][-4:]) print 'Loading ROI #{}, containing source "{}"'.format( roi_index, roi_spec) elif isinstance(roi_spec, int): roi_index = roi_spec elif type(roi_spec) == tuple and len(roi_spec) == 2: roi_index = Band(12).index(SkyDir(*roi_spec)) else: raise Exception('Did not recoginze roi_spec: %s' % (roi_spec)) roi_sources = from_healpix.ROImodelFromHealpix( config, roi_index, ecat=ecat, ) config.roi_spec = configuration.ROIspec(healpix_index=roi_index) self.name = config.roi_spec.name if config.roi_spec is not None else roi_spec roi_bands = bands.BandSet(config, roi_index) roi_bands.load_data() super(ROI, self).__init__(roi_bands, roi_sources)
def average(self, radius=1.0): """ average over all pixels within radius""" iv = IntVector() hp = Healpix(self.nside, Healpix.RING, SkyDir.GALACTIC) band = Band(self.nside) newvec = [0] * len(self.vec) for i in range(len(self.vec)): hp.query_disc(band.dir(i), np.radians(radius), iv) newvec[i] = np.array([self.vec[j] for j in iv]).mean() self.vec = newvec