Esempio n. 1
0
 def solve(self, b, x0, verbose=False):
     cg = CG(self.A, b, x0=x0 * 0, M=self.M)
     while cg.err > 1e-6:
         cg.step()
         if verbose:
             print "%5d %15.7e %15.7e" % (cg.i, cg.err, cg.err_true
                                          )  #, self.dof.unzip(cg.x)[1]
         #if cg.i % 10 == 0:
         #	s, a = self.dof.unzip(cg.x)
         #	matshow(s[0]); colorbar(); show()
     return cg.x
Esempio n. 2
0
def solve_cg(eq, nmax=1000, ofmt=None, dump_interval=10):
    cg = CG(eq.A, eq.b, M=eq.M, dot=eq.dof.dot)
    while cg.i < nmax:
        with bench.mark("cg_step"):
            cg.step()
        dt = bench.stats["cg_step"]["time"].last
        L.info("CG step %5d %15.7e %6.1f %6.3f" %
               (cg.i, cg.err, dt, dt / len(eq.scans)))
        xmap, xjunk = eq.dof.unzip(cg.x)
        if ofmt and cg.i % dump_interval == 0 and myid == 0:
            enmap.write_map(ofmt % cg.i, eq.dof.unzip(cg.x)[0])
        # Output benchmarking information
        bench.stats.write(benchfile)
    return cg.x
Esempio n. 3
0
 def __call__(self, scan, tod):
     # We need to be able to restore the noise model, as this is just
     # one among several filters, and the main noise model must be computed
     # afterwards. We currently store the noise model computer as a special
     # kind of noise model that gets overwritten. That's not a good choice,
     # but since that's how we do it, we must preserve this object.
     old_noise = scan.noise
     # Set up our equation system
     signal_cut = SignalCut([scan], self.dtype, self.comm)
     signal_map = SignalMap([scan],
                            self.map,
                            self.comm,
                            sys=self.sys,
                            pmat_order=self.pmat_order)
     pmat_buddy = pmat.PmatMapMultibeam(scan,
                                        self.map,
                                        scan.buddy_offs,
                                        scan.buddy_comps,
                                        order=self.pmat_order,
                                        sys=self.sys)
     eqsys = Eqsys([scan], [signal_cut, signal_map],
                   dtype=self.dtype,
                   comm=self.comm)
     # This also updates the noise model
     eqsys.calc_b(tod.copy())
     # Set up a preconditioner
     if self.prec == "bin" or self.prec == "jacobi":
         signal_map.precon = PreconMapBinned(signal_map,
                                             signal_cut, [scan], [],
                                             noise=self.prec == "bin",
                                             hits=False)
     else:
         raise NotImplementedError
     # Ok, we can now solve the system
     cg = CG(eqsys.A, eqsys.b, M=eqsys.M, dot=eqsys.dot)
     for i in range(self.nstep):
         cg.step()
         L.debug("buddy pertod step %3d err %15.7e" % (cg.i, cg.err))
     # Apodize the buddy map
     buddy_map = eqsys.dof.unzip(cg.x)[1]
     div = signal_map.precon.div[0, 0]
     apod = np.minimum(1, div / (0.2 * np.median(div[div != 0])))**4
     buddy_map *= apod
     # Ok, the system is hopefully solved by now. Read off the buddy model
     pmat_buddy.forward(tod, buddy_map, tmul=self.tmul, mmul=self.mul)
     # Restore noise
     scan.noise = old_noise
     del signal_map.precon
     del signal_map
Esempio n. 4
0
    def solve(self, b, x0=None, verbose=False, nmin=0):
        if x0 is None: x0 = b * 0

        def wrap(fun):
            def foo(x):
                xmap = enmap.samewcs(x.reshape(b.shape), b)
                return fun(xmap).reshape(-1)

            return foo

        solver = CG(wrap(self.A),
                    b.reshape(-1),
                    x0=x0.reshape(-1),
                    M=wrap(self.M))
        #for i in range(50):
        #while solver.err > 1e-6:
        while solver.err > 1e-2 or solver.i < nmin:
            solver.step()
            if verbose:
                print "%5d %15.7e %15.7e" % (solver.i, solver.err,
                                             solver.err_true)
        return enmap.samewcs(solver.x.reshape(b.shape), b)
Esempio n. 5
0
	signal_cut   = mapmaking.SignalCut(scans, dtype, comm_sub)
	signal_phase = mapmaking.SignalPhase(scans, pids=[0]*len(scans), patterns=pboxes[pid:pid+1],
			array_shape=(args.nrow,args.ncol), res=daz, dtype=dtype, comm=comm_sub, cuts=signal_cut, ofmt="phase")
	signal_cut.precon   = mapmaking.PreconCut(signal_cut, scans)
	signal_phase.precon = mapmaking.PreconPhaseBinned(signal_phase, signal_cut, scans, weights)
	filters = []
	if args.dedark:
		filters.append(mapmaking.FilterDedark())
	if args.demode:
		filters.append(mapmaking.FilterPhaseBlockwise(daz=4*utils.arcmin, niter=10))
	if args.decommon:
		filters.append(mapmaking.FilterCommonBlockwise())

	eq = mapmaking.Eqsys(scans, [signal_cut, signal_phase], weights=weights,
			filters=filters, dtype=dtype, comm=comm_sub)

	# Write precon
	signal_phase.precon.write(proot)
	# Solve for the given number of steps
	eq.calc_b()
	cg = CG(eq.A, eq.b, M=eq.M, dot=eq.dof.dot)
	while cg.i < args.nstep:
		with bench.mark("cg_step"):
			cg.step()
		dt = bench.stats["cg_step"]["time"].last
		if comm_sub.rank == 0:
			L.debug("CG step %5d %15.7e %6.1f %6.3f" % (cg.i, cg.err, dt, dt/max(1,len(eq.scans))))
	eq.write(proot, "map%04d" % cg.i, cg.x)

L.debug("Done")
Esempio n. 6
0
    inds = range(comm.rank, nwork, comm.size)
    mywork = []
    for ind in inds:
        L.debug("Read %s" % ifiles[ind])
        mywork.append(read_workspace(ifiles[ind]))

    # We will solve this system by conjugate gradients:
    #  Pw' Fw' hdiv_norm' N" hdiv_norm Fw Pw map = Pw' Fw' hdiv_norm' N" rhs
    # What should N" be? rhs = Pt' Nt" (Pt map + n)
    # var(rhs) = var(Pt' N" Pt) = ?

    # By the time we have made workspaces, the pixelization is fixed.
    # Our only freedom is to select which subregion of that pixel space
    # to actually include in our maps. We will take in a template which
    # must be in compatible pixelization to indicate this region.
    L.info("Initializing solver")
    solver = FastmapSolver(mywork, template, comm)
    L.info("Computing right-hand side")
    b = solver.calc_b()
    L.info("Solving")
    cg = CG(solver.A, solver.dof.zip(b))
    for i in range(1000):
        t1 = time.time()
        cg.step()
        t2 = time.time()
        if cg.i % 10 == 0 and comm.rank == 0:
            m = solver.dof.unzip(cg.x)
            enmap.write_map(args.odir + "/step%04d.fits" % cg.i, m)
        if comm.rank == 0:
            print "%5d %15.7e %7.2f" % (cg.i, cg.err, t2 - t1)
Esempio n. 7
0
    filters = []
    if args.dedark:
        filters.append(mapmaking.FilterDedark())
    if args.demode:
        filters.append(
            mapmaking.FilterPhaseBlockwise(daz=4 * utils.arcmin, niter=10))
    if args.decommon:
        filters.append(mapmaking.FilterCommonBlockwise())

    eq = mapmaking.Eqsys(scans, [signal_cut, signal_phase],
                         weights=weights,
                         filters=filters,
                         dtype=dtype,
                         comm=comm_sub)

    # Write precon
    signal_phase.precon.write(proot)
    # Solve for the given number of steps
    eq.calc_b()
    cg = CG(eq.A, eq.b, M=eq.M, dot=eq.dof.dot)
    while cg.i < args.nstep:
        with bench.mark("cg_step"):
            cg.step()
        dt = bench.stats["cg_step"]["time"].last
        if comm_sub.rank == 0:
            L.debug("CG step %5d %15.7e %6.1f %6.3f" %
                    (cg.i, cg.err, dt, dt / max(1, len(eq.scans))))
    eq.write(proot, "map%04d" % cg.i, cg.x)

L.debug("Done")
Esempio n. 8
0
    L.info("Writing preconditioners")
    mapmaking.write_precons(signals, root)

    L.info("Writing RHS")
    eqsys.write(root, "rhs", eqsys.b)

    L.info("Computing approximate map")
    x = eqsys.M(eqsys.b)
    eqsys.write(root, "bin", x)

    utils.mkdir(root + "cgstate")
    cgpath = root + "cgstate/cgstate%02d.hdf" % comm.rank

    if nmax > 0:
        L.info("Solving")
        cg = CG(eqsys.A, eqsys.b, M=eqsys.M, dot=eqsys.dot)
        dump_steps = [int(w) for w in args.dump.split(",")]
        # Start from saved cg info if available
        if resume > 0 and os.path.isfile(cgpath):
            cg.load(cgpath)
        assert cg.i == comm.bcast(cg.i), "Inconsistent CG step in mapmaker!"
        while cg.i < nmax:
            with bench.mark("cg_step"):
                cg.step()
            # Save cg state
            if resume != 0 and cg.i % np.abs(resume) == 0:
                cg.save(cgpath)
            dt = bench.stats["cg_step"]["time"].last
            if cg.i in dump_steps or cg.i % dump_steps[-1] == 0:
                x = eqsys.postprocess(cg.x)
                eqsys.write(root, "map%04d" % cg.i, x)
Esempio n. 9
0
    entry = filedb.data[id]
    osys = "hor:%.6f_%.6f:cel/0_0:hor" % tuple(srcs[:2, si])
    try:
        scans = [actscan.ACTScan(entry)]
        if scans[0].nsamp == 0 or scans[0].ndet == 0:
            raise errors.DataMissing("no data in scan")
    except errors.DataMissing as e:
        print "Skipping %s: %s" % (id, str(e))
        continue
    # Signals
    signal_cut = mapmaking.SignalCut(scans, dtype=dtype, comm=tcomm)
    signal_map = mapmaking.SignalMap(scans, area, comm=tcomm, sys=osys)
    # Weights
    weights = [mapmaking.FilterWindow(config.get("tod_window"))]
    # And equation system
    eqsys = mapmaking.Eqsys(scans, [signal_cut, signal_map],
                            weights=weights,
                            dtype=dtype,
                            comm=tcomm)
    eqsys.calc_b()
    # Precons
    signal_cut.precon = mapmaking.PreconCut(signal_cut, scans)
    signal_map.precon = mapmaking.PreconMapBinned(signal_map, signal_cut,
                                                  scans, weights)

    cg = CG(eqsys.A, eqsys.b, M=eqsys.M, dot=eqsys.dot)
    while cg.i < nmax:
        cg.step()
    eqsys.write(root, "map", cg.x)
    signal_map.precon.write(root)
Esempio n. 10
0
				enmap.write_map(oname, map)
			del map

	L.info("Writing RHS")
	eqsys.write(root, "rhs", eqsys.b)

	L.info("Computing approximate map")
	x = eqsys.M(eqsys.b)
	eqsys.write(root, "bin", x)

	utils.mkdir(root + "cgstate")
	cgpath = root + "cgstate/cgstate%02d.hdf" % comm.rank

	if nmax > 0:
		L.info("Solving")
		cg = CG(eqsys.A, eqsys.b, M=eqsys.M, dot=eqsys.dot)
		dump_steps = [int(w) for w in args.dump.split(",")]
		# Start from saved cg info if available
		if resume > 0 and os.path.isfile(cgpath):
			cg.load(cgpath)
		assert cg.i == comm.bcast(cg.i), "Inconsistent CG step in mapmaker!"
		def dump(cg):
			if args.prepost: eqsys.write(root, "map%04d_prepost" % cg.i, cg.x)
			x = eqsys.postprocess(cg.x)
			eqsys.write(root, "map%04d" % cg.i, x)
			if args.tod_debug:
				eqsys.A(cg.x, debug_file = root + "tod_debug%04d.hdf" % cg.i)
		errlim = 1e-30
		while cg.i < nmax and cg.err > errlim:
			with bench.mark("cg_step"):
				cg.step()