aqids = args.arrays.split(',') narrays = len(aqids) qpairs = [] for qid1 in range(narrays): for qid2 in range(qid1,narrays): qpairs.append((aqids[qid1],aqids[qid2])) """ We will MPI parallelize over pairs of arrays. This wastes FFTs, but is more memory efficient. (Each job only holds 2 arrays in memory). """ njobs = len(qpairs) comm,rank,my_tasks = mpi.distribute(njobs) mask = sints.get_act_mr3_crosslinked_mask(args.region, version=args.mask_version, kind='binary_apod') shape,wcs = mask.shape,mask.wcs modlmap = mask.modlmap() aspecs = tutils.ASpecs().get_specs region = args.region fbeam = lambda qname,x: tutils.get_kbeam(qname,x,sanitize=not(args.unsanitized_beam),planck_pixwin=True) nbin_edges = np.arange(20,8000,100) nbinner = stats.bin2D(modlmap,nbin_edges) ncents = nbinner.centers
modlmap = enmap.modlmap(shape,wcs) ngen = {} ngen['act_mr3'] = actnoise.NoiseGen(args.sim_version,model="act_mr3",extract_region=mask,ncache=0,verbose=True) ngen['planck_hybrid'] = actnoise.NoiseGen(args.sim_version,model="planck_hybrid",extract_region=mask,ncache=0,verbose=True) arrays = args.arrays.split(',') narrays = len(arrays) nsims = args.nsims jsim = pipeline.JointSim(arrays,args.fg_res_version+"_"+args.region, bandpassed=bandpasses,no_act_color_correction=args.no_act_color_correction, ccor_exp=args.ccor_exp) comm,rank,my_tasks = mpi.distribute(nsims) for task in my_tasks: sim_index = task + args.start_index print("Rank %d starting task %d at %s..." % (rank,task,str(datetime.now()))) ind_str = str(set_id).zfill(2)+"_"+str(sim_index).zfill(4) sim_version = "%s_%s" % (args.version,ind_str) scratch = tutils.get_scratch_path(sim_version,args.region) try: os.makedirs(scratch) except: pass """ MAKE SIMS
def do(ymap, cmap, dmap, mask, ras, decs, wt): combined = list(zip(ras, decs)) random.shuffle(combined) ras[:], decs[:] = zip(*combined) Nrand = 400 njobs = len(ras) comm, rank, my_tasks = mpi.distribute(njobs) print("Rank %d starting" % rank) s = stats.Stats(comm) i = 0 for task in my_tasks: ra = ras[task] dec = decs[task] mcut, ycut, ccut, dcut, weight = get_cuts(mask, ymap, cmap, dmap, wt, ra, dec, arcmin, pix) if mcut is None: continue if i == 0: modrmap = np.rad2deg(ycut.modrmap()) * 60. bin_edges = np.arange(0., 15., 1.0) binner = stats.bin2D(modrmap, bin_edges) rras, rdecs = catalogs.random_catalog(ymap.shape, ymap.wcs, Nrand, edge_avoid_deg=4.) nrej = 0 for rra, rdec in zip(rras, rdecs): rmcut, rycut, rccut, rdcut, rweight = get_cuts( mask, ymap, cmap, dmap, wt, rra, rdec, arcmin, pix) if rmcut is None: nrej = nrej + 1 continue cents, ry1d = binner.bin(rycut) cents, rc1d = binner.bin(rccut) cents, rd1d = binner.bin(rdcut) s.add_to_stats("rc1d", rc1d * 1e6) s.add_to_stats("ry1d", ry1d * 1e6) s.add_to_stats("rd1d", rd1d * 1e6) if rank == 0: print(Nrand - nrej, " accepted") cents, y1d = binner.bin(ycut) cents, c1d = binner.bin(ccut) cents, d1d = binner.bin(dcut) s.add_to_stats("c1d", c1d * 1e6) s.add_to_stats("y1d", y1d * 1e6) s.add_to_stats("d1d", d1d * 1e6) s.add_to_stack("cstack", ccut * 1e6 * weight) s.add_to_stack("dstack", dcut * 1e6 * weight) s.add_to_stack("ystack", ycut * 1e6 * weight) s.add_to_stats("sum", (weight, )) i = i + 1 if i % 10 == 0 and rank == 0: print(i) print("Rank %d done " % rank) s.get_stats() s.get_stacks() if rank == 0: N = s.vectors['sum'].sum() ystack = s.stacks['ystack'] * N cstack = s.stacks['cstack'] * N dstack = s.stacks['dstack'] * N y1ds = s.vectors['y1d'] c1ds = s.vectors['c1d'] d1ds = s.vectors['d1d'] ry1d = s.stats['ry1d']['mean'] rc1d = s.stats['rc1d']['mean'] rd1d = s.stats['rd1d']['mean'] _, nwcs = enmap.geometry(pos=(0, 0), shape=ystack.shape, res=np.deg2rad(0.5 / 60.)) return rank, enmap.enmap( ystack, nwcs), enmap.enmap(cstack, nwcs), enmap.enmap( dstack, nwcs), N, cents, y1ds, c1ds, d1ds, ry1d, rc1d, rd1d else: return rank, None, None, None, None, None, None, None, None, None, None, None
self.mlmax) self.cache[key] = alms return alms def get_kmap(self, X, seed): s_i, s_set, _ = solenspipe.convert_seeds(seed) key = (s_i, s_set) if key in self.cache: alms = self.cache[key] else: alms = self.load_alms(s_i, s_set) return alms nsims = 40 comm, _, _ = mpi.distribute(nsims, verbose=True) power = lambda x, y: hp.alm2cl(x, y) mcache = MapCacher(mlmax) get_kmap = mcache.get_kmap cl_n1 = bias.mcn1(0, polcomb, polcomb, qfunc, get_kmap, comm, power, nsims, verbose=True) ls = np.arange(len(cl_n1)) cents, cn1 = binner.bin(ls, cl_n1)
def mcrdn0(icov, get_kmap, power, nsims, qfunc1, qfunc2=None, Xdat=None, use_mpi=True, verbose=True, skip_rd=False): """ Using Monte Carlo sims, this function calculates the anisotropic Gaussian N0 bias between two quadratic estimators. It returns both the data realization-dependent version (RDN0) and the pure-simulation version (MCN0). e.g. >> mcrdn0(0,qfunc,get_kmap,comm,power) Parameters ---------- icov: int The index of the realization passed to get_kmap if performing a covariance calculation - otherwise, set to zero. get_kmap: function Function for getting the filtered a_lms of data and simulation maps. See notes at top of module. power: function Returns C(l) from two maps x,y, as power(x,y). nsims: int Number of sims qfunc1: function Function for reconstructing lensing from maps x and y, called as e.g. qfunc(x, y). See e.g. SoLensPipe.qfunc. The x and y arguments accept a [T_alm,E_alm,B_alm] tuple. The function should return an (N,...) array where N is typically two components for the gradient and curl. qfunc2: function, optional Same as above, for the third and fourth legs of the 4-point RDN0. comm: object, optional MPI communicator verbose: bool, optional Whether to show progress skip_rd: bool, optional Whether to skip the RDN0 terms. The first returned component is then None. Returns ------- rdn0: (N*(N+1)/2,...) array Estimate of the RDN0 bias. If N=2 for gradient and curl, the three components correspond to the gradient RDN0, the curl RDN0 and the gradient x curl RDN0. None is returned if skip_rd is True. mcn0: (N*(N+1)/2,...) array Estimate of the MCN0 bias. If N=2 for gradient and curl, the three components correspond to the gradient RDN0, the curl RDN0 and the gradient x curl RDN0. """ qa = qfunc1 qb = qfunc2 mcn0evals = [] if not (skip_rd): assert Xdat is not None # Data rdn0evals = [] if use_mpi: comm, rank, my_tasks = mpi.distribute(nsims) else: comm, rank, my_tasks = FakeCommunicator(), 0, range(nsims) for i in my_tasks: if rank == 0 and verbose: print("MCRDN0: Rank %d doing task %d" % (rank, i)) Xs = get_kmap((icov, 0, i)) if not (skip_rd): qaXXs = qa(Xdat, Xs) qbXXs = qb(Xdat, Xs) if qb is not None else qaXXs qaXsX = qa(Xs, Xdat) qbXsX = qb(Xs, Xdat) if qb is not None else qaXsX rdn0_only_term = power(qaXXs,qbXXs) + power(qaXsX,qbXXs) \ + power(qaXsX,qbXsX) + power(qaXXs,qbXsX) Xsp = get_kmap((icov, 1, i)) qaXsXsp = qa(Xs, Xsp) qbXsXsp = qb(Xs, Xsp) if qb is not None else qaXsXsp qbXspXs = qb(Xsp, Xs) if qb is not None else qa(Xsp, Xs) mcn0_term = (power(qaXsXsp, qbXsXsp) + power(qaXsXsp, qbXspXs)) mcn0evals.append(mcn0_term.copy()) if not (skip_rd): rdn0evals.append(rdn0_only_term - mcn0_term) if not (skip_rd): avgrdn0 = utils.allgatherv(rdn0evals, comm) else: avgrdn0 = None avgmcn0 = utils.allgatherv(mcn0evals, comm) return avgrdn0, avgmcn0
def mcn1(icov, get_kmap, power, nsims, qfunc1, qfunc2=None, comm=None, verbose=True): """ MCN1 for alpha=XY cross beta=AB qfunc(x,y) returns QE reconstruction minus mean-field in fourier space Parameters ---------- icov: int The index of the realization passed to get_kmap if performing a covariance calculation - otherwise, set to zero. get_kmap: function Function for getting the filtered a_lms of data and simulation maps. See notes at top of module. power: function Returns C(l) from two maps x,y, as power(x,y). nsims: int Number of sims qfunc1: function Function for reconstructing lensing from maps x and y, called as e.g. qfunc(x, y). See e.g. SoLensPipe.qfunc. The x and y arguments accept a [T_alm,E_alm,B_alm] tuple. The function should return an (N,...) array where N is typically two components for the gradient and curl. qfunc2: function, optional Same as above, for the third and fourth legs of the 4-point MCN1. comm: object, optional MPI communicator verbose: bool, optional Whether to show progress Returns ------- mcn1: (N*(N+1)/2,...) array Estimate of the MCN1 bias. If N=2 for gradient and curl, the three components correspond to the gradient MCN1, the curl MCN1 and the gradient x curl MCN1. Estimate of the MCN1 bias """ qa = qfunc1 qb = qfunc2 comm, rank, my_tasks = mpi.distribute(nsims) n1evals = [] for i in my_tasks: if rank == 0 and verbose: print("MCN1: Rank %d doing task %d" % (comm.rank, i)) Xs = get_kmap((icov, 0, i)) # S Ysp = get_kmap((icov, 1, i)) # S' Xsk = get_kmap((icov, 2, i)) # Sphi Yskp = get_kmap((icov, 3, i)) # Sphi' qa_Xsk_Yskp = qa(Xsk, Yskp) qb_Xsk_Yskp = qb(Xsk, Yskp) if qb is not None else qa_Xsk_Yskp qb_Yskp_Xsk = qb(Yskp, Xsk) if qb is not None else qa(Yskp, Xsk) qa_Xs_Ysp = qa(Xs, Ysp) qb_Xs_Ysp = qb(Xs, Ysp) if qb is not None else qa_Xs_Ysp qb_Ysp_Xs = qb(Ysp, Xs) if qb is not None else qa(Ysp, Xs) qb_Yskp_Xsk = qb(Yskp, Xsk) if qb is not None else qa(Yskp, Xsk) term = power(qa_Xsk_Yskp,qb_Xsk_Yskp) + power(qa_Xsk_Yskp,qb_Yskp_Xsk) \ - power(qa_Xs_Ysp,qb_Xs_Ysp) - power(qa_Xs_Ysp,qb_Ysp_Xs) n1evals.append(term.copy()) n1s = utils.allgatherv(n1evals, comm) return n1s
def do(ymap, cmap, mask, ras, decs, wt): combined = list(zip(ras, decs)) random.shuffle(combined) ras[:], decs[:] = zip(*combined) njobs = len(ras) comm, rank, my_tasks = mpi.distribute(njobs) print("Rank %d starting" % rank) s = stats.Stats(comm) i = 0 for task in my_tasks: ra = ras[task] dec = decs[task] # mcut = reproject.cutout(mask, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) mcut = reproject.postage_stamp(mask, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) if mcut is None: continue if np.any(mcut) <= 0: continue # ycut = reproject.cutout(ymap, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) # ccut = reproject.cutout(cmap, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) # wcut = reproject.cutout(wt, ra=np.deg2rad(ra), dec=np.deg2rad(dec),npix=int(arcmin/pix)) ycut = reproject.postage_stamp(ymap, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) ccut = reproject.postage_stamp(cmap, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) wcut = reproject.postage_stamp(wt, np.deg2rad(ra), np.deg2rad(dec), arcmin, pix) weight = wcut.mean() # if i==0: # modrmap = np.rad2deg(ycut.modrmap())*60. # bin_edges = np.arange(0.,15.,1.0) # binner = stats.bin2D(modrmap,bin_edges) # cents,y1d = binner.bin(ycut) # cents,c1d = binner.bin(ccut) # s.add_to_stats("c1d",c1d*1e6) # s.add_to_stats("y1d",y1d*1e6) s.add_to_stack("cstack", ccut * 1e6 * weight) s.add_to_stack("ystack", ycut * 1e6 * weight) s.add_to_stats("sum", (weight, )) i = i + 1 if i % 10 == 0 and rank == 0: print(i) print("Rank %d done " % rank) s.get_stats() s.get_stacks() if rank == 0: N = s.vectors['sum'].sum() ystack = s.stacks['ystack'] * N cstack = s.stacks['cstack'] * N _, nwcs = enmap.geometry(pos=(0, 0), shape=ystack.shape, res=np.deg2rad(0.5 / 60.)) return rank, enmap.enmap(ystack, nwcs), enmap.enmap(cstack, nwcs), N else: return rank, None, None, None
#qids = 'boss_03,boss_04'.split(',') #qids = 'd56_01,d56_02,d56_03,d56_04,d56_05,d56_06'.split(',') #qids = 'd56_02,d56_03,d56_04'.split(',') #qids = 'd56_04,p01,p02,p03,p04,p05,p06,p07,p08'.split(',') mask = sints.get_act_mr3_crosslinked_mask(region, kind='binary_apod') modlmap = mask.modlmap() jsim = pipeline.JointSim(qids,fg_res_version+"_"+region,bandpassed=True) jsim0 = pipeline.JointSim(qids,None,bandpassed=True) comm,rank,my_tasks = mpi.distribute(len(qids)) bin_edges = np.arange(20,8000,20) binner = stats.bin2D(modlmap,bin_edges) jsim.update_signal_index(0,set_idx=0) jsim0.update_signal_index(0,set_idx=0) for task in my_tasks: qid = qids[task] with bench.show("signal"): signal = jsim.compute_map(mask.shape,mask.wcs,qid, include_cmb=True,include_tsz=True, include_fgres=True,sht_beam=True) # !!!
lmin = args.lmin lmax = args.lmax polcomb = args.polcomb config = io.config_from_yaml("../input/config.yml") """ nside=2048 smooth_deg=4. ch=SOChannel('LA',145) polcomb='TT' lmin=100 lmax=3000 config = io.config_from_yaml("../input/config.yml") """ comm,rank,my_tasks = mpi.distribute(args.nsims,verbose=True) #tasks are the number of simulations mask = initialize_mask(nside,smooth_deg) solint = SOLensInterface(mask) thloc = "../data/" + config['theory_root'] theory = cosmology.loadTheorySpectraFromCAMB(thloc,get_dimensionless=False) # norm dict Als = {} with bench.show("norm"): ls,Als['TT'],Als['EE'],Als['EB'],Als['TE'],Als['TB'],al_mv_pol,al_mv,Al_te_hdv = initialize_norm(solint,ch,lmin,lmax) Als['mv'] = al_mv Als['mvpol'] = al_mv_pol al_mv = Als[polcomb]