def _Data_setup(self, bins): # check emin and bpd for consistency with CALDB c1 = N.abs(bins - 100).min() > 1 c2 = (self.binsperdec % 4) > 0 if c1 or c2: print """ ###################WARNING!!!########################## It is STRONGLY recommended that you use a binning comm- ensurate with CALDB binning. This can be achieved by making sure 100 MeV appears in the bin edges (an easy way is to set emin=100) and that binsperdec is a multi- ple of 4. You can choose a subset of these bins in the likelihood fitting if you want to narrow the energy bounds. ####################################################### """ pointlike.Data.set_class_level(self.event_class) pointlike.Data.set_zenith_angle_cut(self.zenithcut) pointlike.Data.set_theta_cut(self.thetacut) if 'mc_energy' in self.__dict__: pointlike.Data.set_use_mc_energy(self.mc_energy) pointlike.Data.set_Gti_mask(self.gti) if not self.quiet: print '.....set Data theta cut at %.1f deg' % (self.thetacut) if not self._binner_set: from pointlike import DoubleVector, IntVector f_nside = IntVector(NsideMapper.nside(bins, 0)) b_nside = IntVector(NsideMapper.nside(bins, 1)) self.binner = skymaps.PhotonBinner(DoubleVector(bins), f_nside, b_nside) pointlike.Data.setPhotonBinner(self.binner) self._binner_set = True
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 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 get_seeds(self, mode=0, ts_thresh=9): """Do a crude test to try to find good seeds.""" ts = self.ts_vals[mode] mask = np.asarray([True] * len(ts)) b2 = self.band2 inds = self.inds iv = IntVector() seeds = deque() seeds_ts = deque() n = len(inds) for i in xrange(n): my_ts = ts[mask] amax = np.argmax(my_ts) mts = my_ts[amax] if mts < ts_thresh: break idx = int(inds[mask][amax]) b2.findNeighbors(idx, iv) neigh_ts = deque() for neigh in [x for x in iv]: arg = np.searchsorted(inds, neigh) if (arg < n) and (inds[arg] == neigh): mask[arg] = False neigh_ts.append(ts[arg]) arg = np.searchsorted(inds, idx) mask[arg] = False if mts > max(neigh_ts): seeds.append(idx) seeds_ts.append(mts) return [b2.dir(x) for x in seeds], np.asarray(seeds_ts)
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
def smooth(self, a=0.6): """ simple-minded smooth using nearest neighbors """ newvec = [0]*len(self.vec) band = Band(self.nside) iv = IntVector() # needed for findNeighbors: first 4 share sides b=0.25*(1-a) for i in range(len(self.vec)): band.findNeighbors(i,iv) newvec[i] = a*self.vec[i] + b*sum([self.vec[j] for j in iv[:4]]) self.vec= newvec
def __init__(self, nside, inds, vals): self.band = Band(nside) #s = np.argsort(inds) #self.inds = inds[s] #self.vals = vals[s] self.vals = np.empty(12 * nside**2) self.vals[:] = np.nan self.vals[inds] = vals from pointlike import IntVector self.iv = IntVector()
def __call__(self, skydir): if self.sigma==0: return self.vec[self._indexfun(skydir)] iv = IntVector() self.hp.query_disc(skydir, 3*self.sigma, iv) sds = map(self.dirfun, iv) deltas = np.array(map(skydir.difference, sds)) values = np.array(self.vec[list(iv)]) notnan = ~np.isnan(values) weights = np.array(map( lambda x: np.exp(-0.5*(x/self.sigma)**2), deltas[notnan])) ret =np.dot(values[notnan],weights) / sum(weights) if np.isnan(ret) or np.isinf(ret): raise Exception('Bad value at %s' %skydir) return ret
def ajacent_ts(i, rts): iv = IntVector() n = band.findNeighbors(int(i), iv) ats = [rts[i] for i in iv[:4]] return sum(ats), max(ats)
def neighbors(i, j): iv = IntVector() n = band.findNeighbors(int(i), iv) return j in iv
def neighbor_pixels(index, nside=12): """return set of pixel indeces (RING indexing) """ nb= IntVector() Band(nside).findNeighbors(index,nb); return np.array( nb, int)
def neighbors(i): iv = IntVector() n = B12.findNeighbors(int(i),iv) return list(iv)