def process(kouts, name="default", ellmax=None, y=False, y_ellmin=400): ellmax = lmax if ellmax is None else ellmax ksilc = enmap.zeros((Ny, Nx), wcs, dtype=np.complex128).reshape(-1) ksilc[modlmap.reshape(-1) < lmax] = np.nan_to_num(kouts.copy()) ksilc = enmap.enmap(ksilc.reshape((Ny, Nx)), wcs) ksilc[modlmap > ellmax] = 0 if y: ksilc[modlmap < y_ellmin] = 0 msilc = np.nan_to_num( fft.ifft(ksilc, axes=[-2, -1], normalize=True).real * bmask) enmap.write_map(proot + "outmap_%s.fits" % name, enmap.enmap(msilc, wcs)) p2d = fc.f2power(ksilc, ksilc) bin_edges = np.arange(100, 3000, 40) binner = stats.bin2D(modlmap, bin_edges) cents, p1d = binner.bin(p2d) try: # io.plot_img(np.log10(np.fft.fftshift(p2d)),proot+"ksilc_%s.png" % name,aspect='auto') # io.plot_img(msilc,proot+"msilc_%s.png" % name) # io.plot_img(msilc,proot+"lmsilc_%s.png" % name,lim=300) tmask = maps.mask_kspace(shape, wcs, lmin=300, lmax=5000 if not (y) else 1500) fmap = maps.filter_map(msilc, tmask) * bmask io.plot_img(fmap, proot + "hmsilc_%s.png" % name, high_res=True) except: pass return cents, p1d
def main(): z_edges = np.arange(0., 1.0, 0.05) Mexp_edges = np.arange(14.0, 15.0, 0.05) emu = NmzEmulator(Mexp_edges, z_edges) mzs = emu.get_catalog(poisson=True) pdf2d, _, _ = np.histogram2d(mzs[:, 0], mzs[:, 1], bins=(Mexp_edges, z_edges)) print(emu.Nmz.sum(), pdf2d.sum(), lnlike(pdf2d, emu.Nmz)) io.plot_img(pdf2d, "pdf2d.png", flip=False) io.plot_img(emu.Nmz, "N2d.png", flip=False) data = pdf2d true_as = emu.cc.cosmo['As'] cparams = cosmo.defaultCosmology lnlikes = [] Ases = np.linspace(2.19e-9, 2.21e-9, 30) for As in Ases: cparams['As'] = As temu = NmzEmulator(Mexp_edges, z_edges, cosmo_params=cparams) lnlikes.append(lnlike(data, temu.Nmz)) lnlikes = np.array(lnlikes) pl = io.Plotter(xlabel="As", ylabel="lnlike") #pl.add(Ases,np.exp(lnlikes)) pl.add(Ases, np.exp(lnlikes - lnlikes.max())) pl.vline(x=true_as, ls="--") pl.done("lnlike.png")
def inpaint_map_const_cov(imap,mask,union_sources_version=None,noise_pix = 20,hole_radius = 3.,plots=False): """ Inpaints a map under the assumption of constant 2D Fourier covariance. This uses the average PS of the full map for the noise model at each source location and thus does not handle inhomogenity. Pros: no products needed other than map-maker outputs of map Cons: imap -- (npol,Ny,Nx) ivar -- (Ny,Nx) """ ras,decs = sints.get_act_mr3f_union_sources(version=union_sources_version) kmap = enmap.fft(mask*imap,normalize='phys') gtags = [] gdicts = {} pcoords = [] for i,(ra,dec) in enumerate(zip(ras,decs)): sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True) if sel is None: continue civar = ivar[sel] if np.any(civar<=0): continue modrmap = civar.modrmap() modlmap = civar.modlmap() res = maps.resolution(civar.shape,civar.wcs) cimap = imap[sel] print(ra,dec) if plots: for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/cimap_%d_%s" % (p,str(i).zfill(2))) mimap = cimap.copy() mimap[...,modrmap<np.deg2rad(hole_radius/60.)] = np.nan for p in range(3): io.plot_img(mimap[p],os.environ['WORK']+"/masked_cimap_%d_%s" % (p,str(i).zfill(2))) scov = pixcov.scov_from_theory(modlmap,cmb_theory_fn,fn_beam,iau=False) ncov = pixcov.ncov_from_ivar(civar) pcov = scov + ncov gdicts[i] = pixcov.make_geometry(hole_radius=np.deg2rad(hole_radius/60.),n=noise_pix,deproject=True,iau=False,pcov=pcov,res=res) pcoords.append(np.array((dec,ra))) gtags.append(i) if len(gtags)>0: pcoords = np.stack(pcoords).swapaxes(0,1) result = pixcov.inpaint(imap,pcoords,deproject=True,iau=False,geometry_tags=gtags,geometry_dicts=gdicts,verbose=True) if plots: for i,(ra,dec) in enumerate(zip(ras,decs)): sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True) if sel is None: continue civar = ivar[sel] if np.any(civar<=0): continue modrmap = civar.modrmap() modlmap = civar.modlmap() res = maps.resolution(civar.shape,civar.wcs) cimap = result[sel] print("Inpainted ", ra,dec) if plots: for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/inpainted_cimap_%d_%s" % (p,str(i).zfill(2))) return result
def test_mcon(): zs = np.linspace(0., 1., 30) ks = np.geomspace(1e-4, 1, 10) hcos = hmvec.HaloModel(zs, ks, params={'sigma2_numks': 100}, skip_nfw=True) ms = np.geomspace(1e13, 1e15, 1000) cs = hmvec.duffy_concentration(ms[None, :], zs[:, None]) rho1s = hcos.rho_matter_z(zs) rho2s = hcos.rho_critical_z(zs) with bench.show("vectorized"): mcritzs0 = hmvec.mdelta_from_mdelta(ms, cs, 200. * rho1s, 200. * rho2s, vectorized=True) with bench.show("unvectorized"): mcritzs1 = hmvec.mdelta_from_mdelta(ms, cs, 200. * rho1s, 200. * rho2s, vectorized=False) from orphics import io io.plot_img(np.log10(ms[None] + cs * 0.), flip=False) io.plot_img(np.log10(mcritzs0), flip=False) io.plot_img((mcritzs0 - mcritzs1) / mcritzs0, flip=False)
def check_equality(imap1, imap2): assert np.all(imap1.shape == imap2.shape) assert wcsutils.equal(imap1.wcs, imap2.wcs) try: assert np.all(np.isclose(imap1, imap2)) except: from orphics import io io.plot_img(imap1, "i1.png", lim=[-1.5, 2]) io.plot_img(imap2, "i2.png", lim=[-1.5, 2]) io.plot_img((imap1 - imap2) / imap1, "ip.png", lim=[-0.1, 0.1]) assert 1 == 0
""" Loads a catalog Maps it Smooths it Thresholds it Projects it onto ACT This gives a mask of 1s and 0s from which a random catalog can be made """ paths = cutils.paths #cat_type = "wise_panstarrs" #cat_type = "madcows_photz" cat_type = args.sys[1] meanfield = False # cat_type = "sdss_redmapper" # meanfield = True shape,wcs = enmap.fullsky_geometry(res=1 * utils.degree) ras,decs,_ = cutils.catalog_interface(cat_type,is_meanfield=meanfield) cmapper = catalogs.CatMapper(ras,decs,shape=shape,wcs=wcs) cmap = maps.binary_mask(enmap.smooth_gauss(cmapper.counts,2 * utils.degree),1e-3) io.hplot(cmap,'counts') shape,wcs = enmap.read_map_geometry(paths.coadd_data + f"act_planck_s08_s18_cmb_f150_daynight_srcfree_map.fits") omap = enmap.project(cmap,shape,wcs,order=0) io.plot_img(omap,'pcounts') enmap.write_map(f'{paths.scratch}{cat_type}_mask.fits',omap)
# ycmap_dc[selchunk] = np.nan_to_num(hilc.multi_constrained_map(kcoadds[...,selchunk],"tsz",["dust","cmb"])) del ilcgen, cov snoise = enmap.enmap(snoise.reshape((Ny, Nx)), wcs) cnoise = enmap.enmap(cnoise.reshape((Ny, Nx)), wcs) ksmap = enmap.enmap(smap.reshape((Ny, Nx)), wcs) kcmap = enmap.enmap(cmap.reshape((Ny, Nx)), wcs) yksmap = enmap.enmap(ysmap.reshape((Ny, Nx)), wcs) ykcmap = enmap.enmap(ycmap.reshape((Ny, Nx)), wcs) ykcmap_d = enmap.enmap(ycmap_d.reshape((Ny, Nx)), wcs) # ykcmap_dc = enmap.enmap(ycmap_dc.reshape((Ny,Nx)),wcs) enmap.write_map("snoise.fits", snoise) enmap.write_map("cnoise.fits", cnoise) enmap.write_map("ksmap.fits", enmap.enmap(c.fc.ifft(ksmap).real, wcs)) enmap.write_map("kcmap.fits", enmap.enmap(c.fc.ifft(kcmap).real, wcs)) io.plot_img(maps.ftrans(snoise), "snoise2d.png", aspect='auto') io.plot_img(maps.ftrans(cnoise), "cnoise2d.png", aspect='auto') bin_edges = np.arange(80, 8000, 80) binner = stats.bin2D(modlmap, bin_edges) cents, s1d = binner.bin(snoise) cents, c1d = binner.bin(cnoise) cents, as1d = binner.bin(c.fc.f2power(ksmap, ksmap)) cents, ac1d = binner.bin(c.fc.f2power(kcmap, kcmap)) pl = io.Plotter(yscale='log', scalefn=lambda x: x**2. / np.pi) pl.add(cents, as1d, ls="-") pl.add(cents, ac1d, ls="-") pl.add(cents, s1d, ls="--") pl.add(cents, c1d, ls="--") pl.done("snoise_data.png")
fwhm = 5. kbeam = maps.gauss_beam(modlmap,fwhm) imap = mgen.get_map() bmap2 = maps.convolve_gaussian(imap.copy(),fwhm=fwhm,nsigma=5.0) print(bmap2.shape) bmap = maps.filter_map(imap.copy(),kbeam) taper,w2 = maps.get_taper(shape) io.plot_img(imap*taper,io.dout_dir+"bmap0.png",high_res=True) io.plot_img(bmap*taper,io.dout_dir+"bmap1.png",high_res=True) io.plot_img(bmap2*taper,io.dout_dir+"bmap2.png",high_res=True) bin_edges = np.arange(200,3000,100) binner = stats.bin2D(modlmap,bin_edges) fc = maps.FourierCalc(shape,wcs) p2d,_,_ = fc.power2d(imap*taper) cents,p1di = binner.bin(p2d/w2) p2d,_,_ = fc.power2d(bmap*taper) cents,p1db = binner.bin(p2d/w2) p2d,_,_ = fc.power2d(bmap2*taper) cents,p1db2 = binner.bin(p2d/w2) pl = io.Plotter(yscale='log') pl.add(ells,ps[0,0]*ells**2.)
lpad=9000, get_dimensionless=False) shape, wcs = maps.rect_geometry(width_deg=10., px_res_arcmin=0.5) lmax = 4000 ells = np.arange(0, lmax, 1) cltt = theory.uCl('TT', ells) pstt = cltt.reshape((1, 1, ells.size)) cgen = maps.MapGen(shape, wcs, pstt) imap = cgen.get_map() clpp = np.nan_to_num(theory.gCl('kk', ells) * 4. / ells**4.) psphi = clpp.reshape((1, 1, ells.size)) phigen = maps.MapGen(shape, wcs, psphi) phi = phigen.get_map() grad_phi = enmap.grad(phi) omap = lensing.lens_map(imap, grad_phi, order=3, mode="spline", border="cyclic", trans=False, deriv=False, h=1e-7) io.plot_img(imap, "unlensed.png", high_res=True) io.plot_img(omap, "lensed.png", high_res=True)
# fit2d = covtools.rednoise(modlmap,wnoise,lknee=lknee_fit,alpha=alpha_fit) bin_edges = np.arange(100, 8000, 40) binner = stats.bin2D(modlmap, bin_edges) ndown, nfitted, nparams = covtools.noise_average(npower, dfact=(16, 16), lmin=100, lmax=8000, wnoise_annulus=500, bin_annulus=20, lknee_guess=3000, alpha_guess=-4, method="fft", radial_fit=True) ndown2 = covtools.signal_average(npower, bin_width=40) io.plot_img(tutils.tpower(ndown), "ndown.png", aspect='auto') #,lim=[-6,3]) cents, n1d = binner.bin(ndown) cents, n1d0 = binner.bin(npower) cents, d1d = binner.bin(np.nan_to_num((npower - ndown) / ndown)) cents, d1d2 = binner.bin(np.nan_to_num((npower - ndown2) / ndown2)) # cents,f1d = binner.bin(fit2d) pl = io.Plotter(xlabel='l', ylabel='C', yscale='log') pl.add(cents, n1d0) pl.add(cents, n1d, ls="--", lw=3) # pl.add(cents,f1d,ls="--") pl.done("n1d.png") pl = io.Plotter(xlabel='l', ylabel='D') pl.add(cents, d1d) pl.add(cents, d1d2, ls="--") pl.hline()
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) # io.plot_img(imaps[0],io.dout_dir+field+"_I_lowres.png") # io.plot_img(imaps[1],io.dout_dir+field+"_Q_lowres.png") # io.plot_img(imaps[2],io.dout_dir+field+"_U_lowres.png") # Quick power sanity check p2d, _, _ = fc.power2d(imaps[0], imaps[0]) # io.plot_img(np.fft.fftshift(np.log10(p2d)),io.dout_dir+"p2d.png") p1d = binit(p2d) / w2 pl = io.Plotter(yscale='log') pl.add(ellrange, clkk, lw=2, color="k") pl.add(cents, p1d, marker="o") pl.done(io.dout_dir + "cls.png")
from __future__ import print_function from orphics import maps,io,cosmology from enlib import enmap,fft import numpy as np import os,sys from flipper import liteMap as lm from flipper import fftTools as ft shape,wcs = maps.rect_geometry(width_deg=30./60.,px_res_arcmin=0.5) modrmap = enmap.modrmap(shape,wcs) modlmap = enmap.modlmap(shape,wcs) sigma = 2.*np.pi/180./60. ptsrc = np.exp(-modrmap**2./2./sigma**2.) io.plot_img(ptsrc) lmap = enmap.to_flipper(ptsrc) kmap = ft.fftFromLiteMap(lmap) modlmap2 = kmap.modLMap imap = fft.ifft(kmap.kMap,axes=[-2,-1],normalize=True).real diff = (modlmap-modlmap2)*100./modlmap io.plot_img(diff) print(diff)
pl.done(os.environ['WORK']+"/tiling/planck_healpix_split_lfi.png") #pl.done(os.environ['WORK']+"/tiling/planck_healpix_split.png") sys.exit() array = '143' region = 'deep56' mask = sints.get_act_mr3_crosslinked_mask(region) dm = sints.PlanckHybrid(region=mask) imap0 = dm.get_split(array,0,srcfree=True,ncomp=1) imap1 = dm.get_split(array,1,srcfree=True,ncomp=1) diff = imap0-imap1 io.plot_img(diff,os.environ['WORK']+"/tiling/planck_diff.png") shape,wcs = imap0.shape[-2:],imap0.wcs modlmap = enmap.modlmap(shape,wcs) w2 = np.mean(mask**2.) k1 = enmap.fft(imap0*mask,normalize='phys') k2 = enmap.fft(imap1*mask,normalize='phys') ells = np.arange(2,8000,1) lbeam = dm.get_beam(ells,array) pl = io.Plotter(xlabel='l',ylabel='B') pl.add(ells,lbeam) pl.add(ells,maps.gauss_beam(ells,7.0)) pl.done(os.environ['WORK']+"/tiling/planck_beam.png")
cutout = stamp[int(oshape[0] / 2. - bshape[0] / 2.):int(oshape[0] / 2. + bshape[0] / 2.), int(oshape[0] / 2. - bshape[0] / 2.):int(oshape[0] / 2. + bshape[0] / 2.)] totlnlikes = [] for k, kamp in enumerate(bkamps): lnlike = maps.get_lnlike(cinvs[k], cutout) + logdets[k] totlnlike = lnlike #+ lnprior[k] totlnlikes.append(totlnlike) nlnlikes = -0.5 * np.array(totlnlikes) mstats.add_to_stats("totlikes", nlnlikes) # QE if task == 0: io.plot_img(stamp, pout_dir + "cmb_noisy.png") recon = qest.kappa_from_map("TT", stamp) cents, recon1d = binner.bin(recon) mstats.add_to_stats("recon1d", recon1d) mstats.add_to_stack("recon", recon) mstats.get_stats() mstats.get_stacks() if rank == 0: # Bayesian blnlikes = mstats.vectors["totlikes"].sum(axis=0) blnlikes -= blnlikes.max()
if i == j: print(qid1, gcov[i, j]) if qid1 == 'p03': gcov[i, j] = bcov[i, j] = 0 if gcov[i, j] == 0.: gcov[i, j] = 700000 if bcov[i, j] == 0.: bcov[i, j] = 700000 print(np.diagonal(bcov)) print(bcov) np.savetxt(f"bcov_{seed}.txt", bcov, delimiter=',') np.savetxt(f"gcov_{seed}.txt", gcov, delimiter=',') gcorr = stats.cov2corr(gcov) bcorr = stats.cov2corr(bcov) print(gcorr.min(), gcorr.max()) print(bcorr.min(), bcorr.max()) io.plot_img(gcorr, f"det_gcov_{seed}.png", flip=False, lim=[0.5, 1]) io.plot_img(bcorr, f"det_bcov_{seed}.png", flip=False, lim=[0.5, 1]) print(seed) gi = np.linalg.inv(gcov) bi = np.linalg.inv(bcov) bi2 = utils.eigpow(bcov, -1) print(np.linalg.eigh(gcov)[0]) print(np.linalg.eigh(bcov)[0]) pl.done("detscatter.png") pl = io.Plotter(xyscale='linlin', xlabel='a', ylabel='r') c = 0 for i in range(narrays): for j in range(i, narrays):
ivars = dm.get_splits_ivar(season=season,patch=patch,arrays=dm.array_freqs[array]) n2d_xflat = noise.get_n2d_data(splits,ivars,mask,coadd_estimator=True, flattened=False, plot_fname=None, dtype=dm.dtype) modlmap = splits.modlmap() bin_edges = np.arange(20,8000,20) binner = stats.bin2D(modlmap,bin_edges) n2d = n2d_xflat[3,3] cents,n1d = binner.bin(n2d) pl = io.Plotter(xyscale='linlog',scalefn = lambda x: x**2,xlabel='l',ylabel='D*2pi') pl.add(cents,n1d) pl.done('n1d.png') nsplits = 4 delta_ell = 400 n2d_xflat_smoothed,_,_ = covtools.noise_block_average(n2d,nsplits,delta_ell,lmin=300,lmax=8000,wnoise_annulus=500,bin_annulus=20, lknee_guess=3000,alpha_guess=-4,nparams=None,log=True,radial_fit=False) print(n2d_xflat.shape,n2d_xflat_smoothed.shape) N = 1200 Ny,Nx = n2d_xflat_smoothed.shape[-2:] M = maps.crop_center(np.fft.fftshift(modlmap),N,int(N*Nx/Ny)) d = maps.crop_center(np.fft.fftshift(n2d_xflat_smoothed),N,int(N*Nx/Ny)) # io.hplot(np.log10(d),'fig_hnoise',colorbar=True) # io.plot_img(np.log10(np.fft.fftshift(n2d_xflat_smoothed)),'fig_tlognoise.png',aspect='auto') io.plot_img(maps.crop_center(np.log10(np.fft.fftshift(n2d_xflat_smoothed)),N,int(N*Nx/Ny)),"fig_noise.pdf" ,aspect='auto',xlabel='$\\ell_x$',ylabel='$\\ell_y$',arc_width=2*M[0,0],lim=[-4.39,-3.46],label="$\\rm{log}_{10}(N ~\\mu{\\rm K}^2\\cdot {\\rm sr})$")
deg = 2.0 smoothed = enmap.smooth_gauss(imap, np.deg2rad(deg)) fname = f'{opath}/car_mask_lmax_{lmax}_smoothed_{deg:.1f}_deg.fits' enmap.write_map(fname, smoothed) # fname = f'{opath}/car_mask_lmax_{lmax}_smoothed_{deg:.1f}_deg_south.fits' # enmap.write_map(fname,smoothed*nmask) # fname = f'{opath}/car_mask_lmax_{lmax}_smoothed_{deg:.1f}_deg_north.fits' # enmap.write_map(fname,smoothed*smask) # io.plot_img(smoothed,'sm_low_carmask.png') # io.plot_img(smoothed*nmask,'sm_low_south_carmask.png') # io.plot_img(smoothed*smask,'sm_low_north_carmask.png') deg = 2.0 r = np.deg2rad(deg) apodized = 0.5 * (1 - np.cos(imap.distance_transform(rmax=r) * (np.pi / r))) io.plot_img(imap, 'low_carmask.png') #io.hplot(imap,'carmask') io.plot_img(apodized, 'low_ap_carmask') #io.hplot(apodized,'ap_carmask') afname = f'{opath}/car_mask_lmax_{lmax}_apodized_{deg:.1f}_deg.fits' enmap.write_map(afname, apodized)
import os, sys from tilec import utils as tutils, covtools import numpy as np from orphics import io, stats, cosmology, maps from pixell import enmap shape, wcs = maps.rect_geometry(width_deg=50., height_deg=30, px_res_arcmin=0.5) modlmap = enmap.modlmap(shape, wcs) ells = np.arange(0, 8000, 1) theory = cosmology.default_theory() cltt2d = enmap.enmap(theory.lCl('TT', modlmap), wcs) cltt2d[modlmap < 50] = 0 cltt = theory.lCl('TT', ells) ndown = covtools.signal_average(cltt2d, bin_width=40) ny = int(shape[0] * 5. / 100.) nx = int(shape[1] * 5. / 100.) diff = maps.crop_center(np.fft.fftshift((ndown - cltt2d) / cltt2d), ny, nx) io.plot_img(diff, "diff2d.png", aspect='auto', lim=[-0.1, 0.1])
ta.initialize_output("out") for ext, ins in ta.tiles(): emap = ext(imap) emap = filter_map(emap) ta.update_output("out", emap, ins) outmap = ta.get_final_output("out") # print(comm.rank) # io.plot_img(outmap,"rank_%d" % comm.rank) if comm.rank == 0: fcmap = filter_map(imap) io.hplot(enmap.downgrade(imap, 8)) io.hplot(enmap.downgrade(outmap, 8)) io.hplot(enmap.downgrade(fcmap, 8)) io.plot_img(enmap.downgrade(outmap - fcmap, 8), lim=1) # brmap = enmap.zeros(observed.shape[-2:],observed.wcs) # bwrmap = enmap.zeros(observed.shape[-2:],observed.wcs) # shape,wcs = observed.shape,observed.wcs # epboxes = get_pixboxes(shape,wcs,width_deg,pad_deg) # pboxes = get_pixboxes(shape,wcs,width_deg,pad_deg-2.*rtap_deg) # for i in range(pboxes.shape[0]): # for j in range(pboxes.shape[1]): # omap = observed.copy() # #print(npix(pboxes[i,j])) # emap = enmap.extract_pixbox(omap,epboxes[i,j],wrap=shape[-2:]) # print("Min ell: ", maps.minimum_ell(emap.shape,emap.wcs)) # taper,w2 = maps.get_taper_deg(emap.shape,emap.wcs,taper_width_degrees = rtap_deg,pad_width_degrees = 0.)
# bin_edges = np.arange(100,5000,100) # binner = stats.bin2D(enmap.modlmap(bshape,bwcs),bin_edges) # cents, bp1d = binner.bin(fc.power2d(bmap*taper)[0]/w2) # pl = io.Plotter(yscale='log') # pl.add(ellrange,totcls) # pl.add(cents,bp1d) # pl.done("cls.png") bbox = bmap.box() pad_rad = 4. * np.pi / 180. bbox0 = bbox.copy() center = (bbox[1, 1] + bbox[0, 1]) / 2. bbox0[1, 1] = center + pad_rad bbox1 = bbox.copy() bbox1[0, 1] = center - pad_rad print(bbox * 180. / np.pi) print(bbox0 * 180. / np.pi) print(bbox1 * 180. / np.pi) bleft = bmap.submap(bbox0) bright = bmap.submap(bbox1) print(bleft.shape) print(bright.shape) io.plot_img(bmap) io.plot_img(bleft) io.plot_img(bright)
if rank==0: print ("At most ", max(num_each) , " tasks...") my_tasks = each_tasks[rank] mstats = stats.Stats(comm) np.random.seed(rank) for i,task in enumerate(my_tasks): if (i+1)%10==0 and rank==0: print(i+1) # Sim unlensed = mg.get_map() noise_map = ng.get_map() lensed = enlensing.displace_map(unlensed, alpha_pix, order=lens_order) tot_beamed = maps.filter_map(lensed,kbeam) #+ fg_true stamp = tot_beamed + noise_map if task==0: io.plot_img(unlensed,pout_dir + "0_unlensed.png") io.plot_img(lensed,pout_dir + "1_lensed.png") io.plot_img(fg,pout_dir + "2_fg.png") io.plot_img(stamp,pout_dir + "3_tot.png") # Bayesian totlnlikes = [] for k,kamp in enumerate(bkamps): lnlike = maps.get_lnlike(cinvs[k],stamp) + logdets[k] totlnlike = lnlike #+ lnprior[k] totlnlikes.append(totlnlike) nlnlikes = -0.5*np.array(totlnlikes) mstats.add_to_stats("totlikes",nlnlikes)
if not (is_planck(qid)) and ("s16" not in qid) and np.any( ta.crop_main(eivars) <= 0): print( "Skipping %s as it seems to have some zeros in the tile center..." % qid) continue aids.append(qid) apod = ta.apod * apodize_zero(np.sum(eivars, axis=0), ivar_apod_pix) esplits = get_splits(qid, extracter) if args.ivars: if i in dtiles: #io.hplot(esplits * apod,os.environ['WORK']+"/tiling/esplits_%s_%d" % (qid,i)) #io.hplot(eivars * apod,os.environ['WORK']+"/tiling/eivars_%s_%d" % (qid,i)) io.plot_img( esplits * apod, os.environ['WORK'] + "/tiling/esplits_%s_%d" % (qid, i)) div = eivars.copy() div[div <= 0] = np.nan io.plot_img( div * apod, os.environ['WORK'] + "/tiling/eivars_%s_%d" % (qid, i)) kdiff, kcoadd = kspace.process_splits(esplits, eivars, apod, skip_splits=False, do_fft_splits=False) kdiffs.append(kdiff.copy()) # ksplits.append(ksplit.copy()) lmin, lmax, hybrid, radial, friend, cfreq, fgroup = get_specs(qid)
ndown, nfitted, nparams = covtools.noise_average(npower[0, 0], dfact=(16, 16), lmin=300, lmax=8000, wnoise_annulus=500, bin_annulus=20, lknee_guess=3000, alpha_guess=-4, method="fft", radial_fit=True) #io.plot_img(np.fft.fftshift(np.log10(ndown)),"ndown.png",aspect='auto',lim=[-6,3]) #io.hplot(np.fft.fftshift(np.log10(ndown)),"hndown") io.hplot(np.fft.fftshift((ndown)), "hndown") io.plot_img(np.fft.fftshift(ndown / nfitted), "nunred.png", aspect='auto') nmod = ndown / nfitted enmap.write_map("anisotropy_template.fits", enmap.samewcs(nmod, npower)) shape, wcs = maps.rect_geometry(width_deg=50., height_deg=30, px_res_arcmin=0.5) rms = 10.0 lknee = 3000 alpha = -3 n2d = covtools.get_anisotropic_noise(shape, wcs, rms, lknee, alpha) modlmap = enmap.modlmap(shape, wcs) bin_edges = np.arange(100, 8000, 100) binner = stats.bin2D(modlmap, bin_edges) cents, n1d = binner.bin(n2d)
hlensed = enlensing.displace_map(hunlensed, ralpha_pix, order=lens_order, mode=mode) enmap.write_map("lensed_0.001arc.fits", hlensed) lensed0 = enmap.enmap(resample.resample_fft(hlensed, cshape), cwcs) sys.exit() # ckappa = lens_func(cmodrmap) # phi,_ = lensing.kappa_to_phi(ckappa,cmodlmap,return_fphi=True) # grad_phi = enmap.grad(phi) # pos = enmap.posmap(cshape,cwcs) + grad_phi # alpha_pix = enmap.sky2pix(cshape,cwcs,pos, safe=False) # lensed2 = enlensing.displace_map(unlensed, alpha_pix, order=lens_order,mode=mode) print(lensed.shape) print(lensed0.shape) enmap.write_map("lensed_lowres.fits", lensed) enmap.write_map("lensed_highres.fits", lensed0) enmap.write_map("unlensed_lowres.fits", unlensed) io.plot_img(rkappa, "lres_test_00.png") io.plot_img(lensed0, "lres_test_0.png") io.plot_img(lensed, "lres_test_1.png") io.plot_img(lensed0 - unlensed, "lres_test_2.png", lim=[-7., 7.]) io.plot_img(lensed - unlensed, "lres_test_3.png", lim=[-7., 7.]) io.plot_img(lensed0 - lensed, "lres_test_4.png")
if args.debug_noise: p2d = fc.f2power(lnt,lnt) cents,p1d = binner.bin(p2d/w2) st.add_to_stats("nTT",p1d.copy()) p2d = fc.f2power(lne,lne) cents,p1d = binner.bin(p2d/w2) st.add_to_stats("nEE",p1d.copy()) p2d = fc.f2power(lnb,lnb) cents,p1d = binner.bin(p2d/w2) st.add_to_stats("nBB",p1d.copy()) if args.debug and task==0: io.plot_img(cpatch[0],io.dout_dir+"cmbI.png",high_res=False) if pol: io.plot_img(cpatch[1],io.dout_dir+"cmbQ.png",high_res=False) io.plot_img(cpatch[2],io.dout_dir+"cmbU.png",high_res=False) io.plot_img(cpatch[1],io.dout_dir+"cmbQh.png",high_res=True) io.plot_img(cpatch[2],io.dout_dir+"cmbUh.png",high_res=True) io.plot_img(nmaps[0],io.dout_dir+"nI.png",high_res=False) if pol: io.plot_img(nmaps[1],io.dout_dir+"nQ.png",high_res=False) io.plot_img(nmaps[2],io.dout_dir+"nU.png",high_res=False) if args.save_meanfield is None: io.plot_img(kpatch,io.dout_dir+"kappa.png",high_res=False) if args.save_meanfield is None: p2d,kinp,kinp = fc.power2d(kpatch) cents,p1dii = binner.bin(p2d/w2) st.add_to_stats("input",p1dii.copy())
# do lensing reconstruction in Fourier space rkmap = qe.reconstruct(shape, wcs, feed_dict, estimator='hdv', XY="TT", xmask=xmask, ymask=ymask, kmask=kmask, physical_units=True) assert np.all(np.isfinite(rkmap)) # transform to real space kappa = enmap.ifft(rkmap, normalize='phys').real s.add_to_stack("lstamp", kappa) s.get_stacks() if rank == 0: kmap = s.stacks['kstamp'] io.plot_img(kmap, f'wsky_outs/kappa.png') io.plot_img(kmap[100:140, 100:140], f'wsky_outs/kappa_zoom.png') lmap = s.stacks['lstamp'] io.plot_img(lmap, f'wsky_outs/rec_kappa.png') io.plot_img(lmap[100:140, 100:140], f'wsky_outs/rec_kappa_zoom.png') elapsed = t.time() - start print("\r ::: entire run took %.1f seconds" % elapsed)
# QE tellmin = modlmap[modlmap>2].min(); tellmax = 8000; kellmin = tellmin ; kellmax = 8096 tmask = maps.mask_kspace(shape,wcs,lmin=tellmin,lmax=tellmax) kmask = maps.mask_kspace(shape,wcs,lmin=kellmin,lmax=kellmax) qest = lensing.qest(shape,wcs,theory,noise2d=kbeam*0.+(noise_uK_rad)**2.,beam2d=kbeam,kmask=tmask,kmask_K=kmask,pol=False,grad_cut=2000,unlensed_equals_lensed=False) for i,task in enumerate(my_tasks): if (i+1)%10==0 and rank==0: print(i+1) unlensed = mg.get_map() noise_map = ng.get_map() lensed = maps.filter_map(enlensing.displace_map(unlensed, alpha_pix, order=lens_order),kbeam) stamp = lensed + noise_map if task==0: io.plot_img(stamp,pout_dir+"cmb_noisy.png") recon = qest.kappa_from_map("TT",stamp) cents, recon1d = binner.bin(recon) mstats.add_to_stats("recon1d",recon1d) mstats.add_to_stack("recon",recon) mstats.get_stats() mstats.get_stacks() if rank==0: stack = mstats.stacks['recon'] recon1d = mstats.stats['recon1d']['mean']
Npix = int(arcmin_width/res)*1. if Npix%2==0: Npix += 1 cshape,cwcs = enmap.geometry(pos=(0.,0.),res=res/(180./np.pi*60.),shape=(Npix,Npix)) cmodrmap = enmap.modrmap(cshape,cwcs) sigmas = [] for ra,dec in zip(ras,decs): iy,ix = enmap.sky2pix(shape,wcs,(dec*np.pi/180.,ra*np.pi/180.)) sigma = np.random.normal(3.0,1.0)*np.pi/180./60. paste_in = np.exp(-cmodrmap**2./2./sigma**2.) bigmap[int(iy-Npix/2):int(iy+Npix/2),int(ix-Npix/2):int(ix+Npix/2)] += paste_in sigmas.append(sigma) io.plot_img(bigmap,"cat.png",high_res=True) print("done") st = maps.Stacker(bigmap,arcmin_width=30.) stack = 0. for ra,dec in zip(ras,decs): stack += st.cutout(ra*np.pi/180.,dec*np.pi/180.) io.plot_img(stack) st = maps.InterpStack(arc_width=30.,px=0.5) stack = 0. for ra,dec in zip(ras,decs): stack += st.cutout(bigmap,ra,dec)
border="cyclic", trans=False, deriv=False, h=1e-7) recon = qest.kappa_from_map("TT", lensed) cents, recon1d = binner.bin(recon) mystats.add_to_stack("recon", recon) mystats.add_to_stats("recon1d", recon1d) mystats.add_to_stats("recon1d_diffper", (recon1d - yt) / yt) if (i + 1) % 100 == 0: print(i + 1) mystats.get_stacks() mystats.get_stats() recon_stack = mystats.stacks['recon'] io.plot_img(kappa, "toy_1_kappa.png") io.plot_img(unlensed, "toy_2_unlensed.png") io.plot_img(lensed, "toy_3_lensed.png") io.plot_img(lensed - unlensed, "toy_4_diff.png") io.plot_img(recon, "toy_5_recon.png") io.plot_img(recon_stack, "toy_6_recon_stack.png") io.plot_img((recon_stack - fkappa) * 100. / fkappa, "toy_7_recon_diff.png", lim=[-10., 10.]) y = mystats.stats['recon1d']['mean'] yerr = mystats.stats['recon1d']['errmean'] pl = io.Plotter() pl.add(cents, yt, ls="--") pl.add_err(cents, y, yerr=yerr, ls="-")
rmap = maps.filter_map(cmap * mask, mfilter) io.hplot(rmap, os.environ['WORK'] + "/new_mr3f/coadd_srcfree_filtered_%s" % fname, grid=True) rmap = maps.filter_map(splits * mask, mfilter) io.hplot(rmap, os.environ['WORK'] + "/new_mr3f/splits_srcfree_filtered_%s" % fname, grid=True) sys.exit() out_dir = "/scratch/r/rbond/msyriac/data/depot/actsims/inpainted/" plot_img = lambda x, y, **kwargs: io.plot_img( x, os.environ['WORK'] + "/new_mr3f/" + y, cmap='gray', **kwargs) noise_pix = 60 cmb_theory_fn = lambda s, l: cosmology.default_theory().lCl(s, l) hole_radius = 9. res = np.deg2rad(0.5 / 60.) def plot_cutout(nsplits, cutout, pcutout, ivars, tag="", skip_plots=False): pols = ['I', 'Q', 'U'] retvars = ivars.copy() for s in range(nsplits): if np.std(cutout[s]) < 1e-3: print("Skipping split %d as it seems empty" % s) continue iname = "%s%s_%s_%s_split_%d_%d" % (tag, season, patch, array, s, sid) if not (skip_plots): plot_img(ivars[s, 0], "ivars_%s.png" % iname)