def __init__(self, boresight=None, offsets=None, comps=None, tod=None, sys=None, cut=None, site=None, mjd0=0, noise=None, dets=None, id=""): # Boresight will always be unwound, i.e. it will have no 2pi jumps in it. # Time is measured in seconds since start of scan, with mjd0 indicating the MJD of the scan start. self.boresight = np.asfarray(boresight) # [nsamp,coords] self.offsets = np.asfarray(offsets) # [ndet,coords] self.comps = np.asfarray(comps) # [ndet,comps] self.cut = rangelist.Multirange(cut) # Multirange[ndet,ranges] self.noise = noise self.id = id # Identifier of this scan, for printing purposes # These are needed in order to interpret the coordinates self.sys = str(sys) # str self.site = site self.mjd0 = mjd0 # time basis self.dets = np.arange(len(self.comps)) if dets is None else dets self.dgrid = (1, np.max(self.dets) + 1) self.hwp = None # Not part of the general interface self._tod = np.asfarray(tod) # [ndet,nsamp]
def find_spikes(tod, nsigma=10, width=15, padding=7, noise=None): res = [] ftod = tod.reshape(-1, tod.shape[-1]) if noise is None: noise = estimate_white_noise(ftod)**0.5 for di, d in enumerate(ftod): smooth = scipy.signal.medfilt(d, width) bad = np.abs(d - smooth) > noise[di] * nsigma bad = rangelist.Rangelist(bad) bad = bad.widen(padding) res.append(rangelist.Rangelist(bad)) res = rangelist.Multirange(res) res.data.reshape(tod.shape[:-1]) return res
def find_jumps(tod, bsize=100, nsigma=10, margin=50, step=50, margin_step=1000): ndet = tod.shape[0] cuts = [] for det, det_tod in enumerate(tod): n = len(det_tod) # Compute difference tod dtod = det_tod[1:] - det_tod[:-1] nsamp = dtod.size # Find typical standard deviation nblock = int(nsamp / bsize) sigma = utils.medmean( np.var(dtod[:nblock * bsize].reshape(nblock, bsize), -1))**0.5 # Look for samples that deviate too much from 0 bad = np.abs(dtod) > sigma * nsigma bad = np.concatenate([bad[:1], bad]) # Look for steps, areas where the mean level changes dramatically on each # side of the jump. First find the center of each bad region steps = bad * 0 labels, nlabel = scipy.ndimage.label(bad) centers = np.array( scipy.ndimage.center_of_mass(bad, labels, np.arange(nlabel + 1))).astype(int)[:, 0] # Find mean to the left and right of each bad region for i, pos in enumerate(centers): m1 = np.mean(det_tod[max(0, pos - step * 3 / 2):max(1, pos - step / 2)]) m2 = np.mean(det_tod[min(n - 2, pos + step / 2):min(n - 1, pos + step * 3 / 2)]) if np.abs(m2 - m1) > sigma * nsigma: steps[pos] = 1 #print centers.shape, np.sum(steps) # Grow each cut by a margin bad = scipy.ndimage.distance_transform_edt(1 - bad) <= margin steps = scipy.ndimage.distance_transform_edt(1 - steps) <= margin_step cuts.append(rangelist.Rangelist(bad | steps)) return rangelist.Multirange(cuts)
def __init__(self, fname): self.fname = fname with h5py.File(fname, "r") as hfile: for k in ["boresight", "offsets", "comps", "sys", "mjd0", "dets"]: setattr(self, k, hfile[k].value) n = self.boresight.shape[0] neach = hfile["cut/neach"].value flat = hfile["cut/flat"].value self.cut = rangelist.Multirange((n, neach, flat), copy=False) self.cut_noiseest = self.cut.copy() self.noise = nmat.read_nmat(hfile, "noise") self.site = bunch.Bunch( {k: hfile["site/" + k].value for k in hfile["site"]}) self.subdets = np.arange(self.ndet) self.hwp = np.zeros(n) self.hwp_phase = np.zeros([n, 2]) self.sampslices = [] self.id = os.path.basename(fname) self.entry = bunch.Bunch(id=self.id)
def to_rangelist(self): ranges = self.to_ranges() return rangelist.Multirange( [rangelist.Rangelist(r, n=self.nsamp) for r in ranges])
"hor", args.objname, mjd, site=scan.site) visible = np.any(object_pos[1] >= margin) if not visible: cut = rangelist.zeros((d.ndet, d.nsamp)) else: pmap = pmat.PmatMap(scan, mask, sys="hor:%s" % args.objname) # Build a tod to project onto. tod = np.zeros((d.ndet, d.nsamp), dtype=dtype) # And project pmap.forward(tod, mask) # Any nonzero samples should be cut tod = np.rint(tod) cut = rangelist.Multirange([rangelist.Rangelist(t) for t in tod]) print "%s %6.4f %d" % (id, float(cut.sum()) / cut.size, visible) mystats.append([ind, float(cut.sum()) / cut.size, visible]) # Write cuts to output directory if args.persample: files.write_cut("%s/%s.cuts" % (args.odir, id), d.dets, cut, nrow=d.array_info.nrow, ncol=d.array_info.ncol) mystats = np.array(mystats) stats = utils.allgatherv(mystats, comm) if comm.rank == 0: with open(args.odir + "/stats.txt", "w") as f: for stat in stats: f.write("%s %6.4f %d\n" %
def nocut(ndet, nsamp): return rangelist.Multirange([ rangelist.Rangelist(np.zeros([0, 2], dtype=int), n=nsamp) for i in range(ndet) ])