def fun_mpi(rank): x = np.arange(7.0) list = np.array_split(x, 4) print("my rank is %d:" % rank) st = stats.Stats() for i in list[rank]: data = [rank, rank] st.add_to_stats('index', data) st.get_stats()
parser.add_argument("--no-mask", action='store_true',help='No mask. Use with the isotropic flag.') parser.add_argument("--debug", action='store_true',help='Debug plots.') parser.add_argument("--flat-sky-norm", action='store_true',help='Use flat-sky norm.') parser.add_argument("--flat-sky-rdn0", action='store_true',help='Do flat-sky rdn0.') args = parser.parse_args() solint,Als,Alcurl,Nl,comm,rank,my_tasks,sindex,debug_cmb,lmin,lmax,polcomb,nsims,channel,isostr = solenspipe.initialize_args(args) w2 = solint.wfactor(2) w3 = solint.wfactor(3) w4 = solint.wfactor(4) if args.write_meanfield: assert not(args.read_meanfield) s = stats.Stats(comm) if args.read_meanfield: mf_alm = hp.read_alm(f'{solenspipe.opath}/mf_{args.label}_{args.polcomb}_{isostr}_alm.fits') else: mf_alm = 0 for task in my_tasks: # Choose a seed. This has to be varied when simulating. seed = (0,0,task+sindex) # If debugging, get unfiltered maps and plot Cls if task==0 and debug_cmb: t_alm,e_alm,b_alm = solint.get_kmap(channel,seed,lmin,lmax,filtered=False)
cinv[ai,aj][agen.modlmap>ellmaxes[aj]] = 0 # Set up SZ frequency dependence def gnu(nu_ghz,tcmb=2.7255): nu = 1e9*np.asarray(nu_ghz) hplanck = 6.62607e-34 kboltzmann = 1.38065e-23 x = hplanck*nu/kboltzmann/tcmb coth = np.cosh(x/2.)/np.sinh(x/2.) return x*coth-4. yresponses = gnu(freqs) cresponses = yresponses*0 + 1. fc = maps.FourierCalc(shape[-2:],wcs) s = stats.Stats() bin_edges = np.arange(300,5000,80) binner = stats.bin2D(agen.modlmap,bin_edges) for i in range(nsims): cmb,y,observed = agen.get_maps() kmaps = [] for j in range(len(freqs)): _,kmap,_ = fc.power2d(observed[j]) km = np.nan_to_num(kmap/agen.kbeams[j]) km[agen.modlmap>ellmaxes[j]] = 0 km[agen.modlmap<ellmins[j]] = 0 kmaps.append(km.copy()) kmaps = np.stack(kmaps) sc = maps.silc(kmaps,cinv,cresponses) sy = maps.silc(kmaps,cinv,yresponses)
st.add_to_stats('frac%s_deconv' % 'ee', frac_diff) theo_idx = 'clbb' frac_diff = (clbb - theo_bin[theo_idx]) / theo_bin[theo_idx] st.add_to_stats('frac%s_deconv' % 'bb', frac_diff) theo_bin = {} for key in list(theo.keys()): if key == 'l': continue lbin_th, clbin_th = cusps_fc.bin_theory(l_th, theo[key]) theo_bin['l'] = lbin_th theo_bin[key] = clbin_th st = stats.Stats(cmblens.mpi.comm) for sim_idx in subtasks: log.info("processing %d" % sim_idx) tmap, qmap, umap = get_sim(sim_idx, mode='actsim') tmap -= np.mean(tmap) qmap -= np.mean(qmap) umap -= np.mean(umap) qmap *= -1. tmap *= taper qmap *= taper umap *= taper #emap, bmap = cusps.util.qu2eb(qmap, umap) emap, bmap = qu2eb(qmap, umap) if sim_idx == 0:
if do_apod: mask = nmt.mask_apodization_flat(mask, Lx, Ly, aposize=apod_width, apotype=apotype) # Mask fraction frac = np.sum(mask) * 1. / mask.size area = enmap.area(shape, wcs) * frac * (180. / np.pi)**2. print("Masked area sq.deg.: ", area) w2 = np.mean(mask**2.) # Naive power scaling factor N = args.Nsims s = stats.Stats() # Stats collector for i in range(N): if (i + 1) % 10 == 0: print(i + 1) # Get sim maps (npol,ny,nx) array imaps = mg.get_map(scalar=False, iau=iau) #*mask if i == 0: # Plots of mask and fields print(mask.shape) io.plot_img(mask, io.dout_dir + field + "_mask.png", high_res=True) # io.plot_img(imaps[0],io.dout_dir+field+"_I.png",high_res=True) # io.plot_img(imaps[1],io.dout_dir+field+"_Q.png",high_res=True) # io.plot_img(imaps[2],io.dout_dir+field+"_U.png",high_res=True)
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
kamp = 0.1 kappa = kamp * np.exp(-modrmap**2. / 2. / ksigma**2.) tkmask = maps.mask_kspace(shape, wcs, lmin=100, lmax=6000) kkmask = maps.mask_kspace(shape, wcs, lmin=100, lmax=7000) qest = lensing.qest(shape, wcs, theory, kmask=tkmask, kmask_K=kkmask) phi, _ = lensing.kappa_to_phi(kappa, modlmap, return_fphi=True) grad_phi = enmap.grad(phi) lens_order = 5 N = 1000 bin_edges = np.arange(0, 10., 1.0) binner = stats.bin2D(modrmap * 180. * 60 / np.pi, bin_edges) mystats = stats.Stats() fkappa = maps.filter_map(kappa, kkmask) cents, yt = binner.bin(fkappa) for i in range(N): unlensed = mg.get_map() lensed = enlensing.lens_map(unlensed, grad_phi, order=lens_order, mode="spline", border="cyclic", trans=False, deriv=False, h=1e-7) recon = qest.kappa_from_map("TT", lensed)
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
def auto(self, Lmin, Lmax, delta_L): """ Get cutout reconstructed kappa auto-power or cross-power with input cutout kappa """ # for statistics st = stats.Stats() # Initialize upper-left pixel corner iy, ix = 0, 0 for itile in range(self.ntiles): # Get bottom-right pixel corner ey = iy + self.npix ex = ix + self.npix # Slice both cmb maps cut_cmb1 = self.cmb1[iy:ey, ix:ex] cut_cmb2 = self.cmb2[iy:ey, ix:ex] # Get geometry of the cutouts, I assume cut_cmb1 and cut_cmb2 have same geometry cut_shape = cut_cmb1.shape cut_wcs = cut_cmb1.wcs cut_modlmap = enmap.modlmap(cut_shape, cut_wcs) ells = np.arange(0, cut_modlmap.max() + 1, 1) ctt = self.theory.lCl('TT', ells) # Get taper for appodization taper, w2 = maps.get_taper_deg(cut_shape, cut_wcs) # Define feed_dict for symlens feed_dict = {} feed_dict['uC_T_T'] = utils.interp(ells, ctt)(cut_modlmap) feed_dict['tC_T_T'] = utils.interp(ells, ctt)(cut_modlmap) + ( self.nlev_t * np.pi / 180. / 60.)**2. / utils.gauss_beam( cut_modlmap, self.beam_arcmin)**2 # Get cmb mask cmask = utils.mask_kspace(cut_shape, cut_wcs, lmin=self.ellmin, lmax=self.ellmax) # Get mask for reconstruction kmask = utils.mask_kspace(cut_shape, cut_wcs, lmin=Lmin, lmax=Lmax) # Stride across the map, horizontally first and # increment vertically when at the end of a row if (itile + 1) % self.num_x != 0: ix = ix + self.npix else: ix = 0 iy = iy + self.npix # Apodize cutout CMB maps cut_cmb1 = taper * cut_cmb1 cut_cmb2 = taper * cut_cmb2 # Get the Fourier maps cut_cmb1_k = enmap.fft(cut_cmb1, normalize='phys') cut_cmb2_k = enmap.fft(cut_cmb2, normalize='phys') # Reconstruct kappa fourier maps cut_reckap1, noise_2d = cutout_rec(cut_shape, cut_wcs, feed_dict, cmask, kmask, cut_cmb1_k, cut_cmb1_k) cut_reckap2, noise_2d = cutout_rec(cut_shape, cut_wcs, feed_dict, cmask, kmask, cut_cmb2_k, cut_cmb2_k) # Get auto powerspectra center_L, cut_reckap1_x_reckap1 = powspec(cut_reckap1, cut_reckap1, taper, 4, cut_modlmap, Lmin, Lmax, delta_L) center_L, cut_reckap2_x_reckap2 = powspec(cut_reckap2, cut_reckap2, taper, 4, cut_modlmap, Lmin, Lmax, delta_L) # Get bias bias = (cut_reckap2_x_reckap2 - cut_reckap1_x_reckap1) / cut_reckap1_x_reckap1 # Add to stats st.add_to_stats('reckap1 x reckap1', cut_reckap1_x_reckap1) st.add_to_stats('reckap2 x reckap2', cut_reckap2_x_reckap2) st.add_to_stats('bias', bias) # Get spectra and bias statistics st.get_stats() return center_L, st
btest = maps.gauss_beam_real(rs, fwhm_test) # pl = io.Plotter() # pl.add(rs,brs*rs) # pl.add(rs,btest*rs,ls="--") # pl.done(io.dout_dir+'rbeam.png') modrmap = enmap.modrmap(shape, wcs) kbeam = maps.interp(ls, bells)(modlmap) kbeam_test = maps.gauss_beam(modlmap, fwhm_test) bin_edges = np.arange(40, 4000, 40) taper, w2 = maps.get_taper(shape) st = stats.Stats() for i in range(nsims): print(i) imap = mgen.get_map() omap1 = maps.convolve_profile(imap.copy(), rs, brs, fwhm_test, nsigma=200.0) omap2 = maps.filter_map(imap.copy(), kbeam) omap3 = maps.convolve_gaussian(imap.copy(), fwhm=fwhm_test, nsigma=5.0) omap4 = maps.filter_map(imap.copy(), kbeam_test) if i == 0:
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] s = stats.Stats(comm) #creates an orphics stats class called s for task in my_tasks: print(task) talm = solint.get_kmap(ch,(0,0,0),lmin,lmax,filtered=True)[0] ealm = solint.get_kmap(ch,(0,0,0),lmin,lmax,filtered=True)[1] balm = solint.get_kmap(ch,(0,0,0),lmin,lmax,filtered=True)[2] rkalm=hp.almxfl(solint.get_mv_kappa(polcomb,talm,ealm,balm),Als[polcomb]) #rkalm = qe.filter_alms(solint.get_mv_kappa(polcomb,talm,ealm,balm),maps.interp(ls,al_mv)) s.add_to_stack("mf_alm",rkalm) #This is just an accumulator it adds rkalm to a an array known as mf_alm s.get_stacks() #calculate the mean of the stack if rank==0: mf_alm = s.stacks['mf_alm'] hp.write_alm(config['data_path']+"meanfield_alm_%s.fits" % polcomb,mf_alm,overwrite=True)