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 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