def add_theory(self, cc=None, theory=None, lmax=None, orphics_is_dimensionless=True): if cc is not None: self.cc = cc self.theory = cc.theory self.lmax = cc.lmax # assert theory is None # assert lmax is None if theory is None: theory = self.theory if lmax is None: lmax = self.lmax else: assert theory is not None assert lmax is not None self.theory = theory self.lmax = lmax #psl = cmb.enmap_power_from_orphics_theory(theory,lmax,lensed=True,dimensionless=self.dimensionless,TCMB=self.TCMB,orphics_dimensionless=orphics_is_dimensionless) psu = cmb.enmap_power_from_orphics_theory( theory, lmax, lensed=False, dimensionless=self.dimensionless, TCMB=self.TCMB, orphics_dimensionless=orphics_is_dimensionless) self.fine_ells = np.arange(0, lmax, 1) pclkk = theory.gCl("kk", self.fine_ells) self.clkk = pclkk.copy() pclkk = pclkk.reshape((1, 1, pclkk.size)) #self.pclkk.resize((1,self.pclkk.size)) self.ugenerator = maps.MapGen(self.shape, self.wcs, psu) self.kgenerator = maps.MapGen(self.shape[-2:], self.wcs, pclkk)
def init_geometry(ishape,iwcs): modlmap = enmap.modlmap(ishape,iwcs) bin_edges = np.arange(args.kellmin,args.kellmax,args.dell) binner = stats.bin2D(modlmap,bin_edges) if args.beam<1e-5: kbeam = None else: kbeam = maps.gauss_beam(modlmap,args.beam) lmax = modlmap.max() ells = np.arange(2,lmax,1) wnoise_TT = ells*0.+(args.noise*(np.pi/180./60.))**2. wnoise_PP = 2.*wnoise_TT nT = modlmap*0.+(args.noise*(np.pi/180./60.))**2. nP = 2.*nT ncomp = 3 if pol else 1 ps = np.zeros((ncomp,ncomp,ells.size)) ps[0,0] = wnoise_TT if pol: ps[1,1] = wnoise_PP ps[2,2] = wnoise_PP oshape = (3,)+ishape if pol else ishape if not(args.flat) and args.noise_pad>1.e-5: # Pad noise sim geometry pad_width_deg = args.noise_pad pad_width = pad_width_deg * np.pi/180. res = maps.resolution(oshape[-2:],iwcs) pad_pixels = int(pad_width/res) template = enmap.zeros(oshape,iwcs) btemplate = enmap.pad(template,pad_pixels) bshape,bwcs = btemplate.shape,btemplate.wcs del template del btemplate ngen = maps.MapGen(bshape,bwcs,ps) else: ngen = maps.MapGen(oshape,iwcs,ps) tmask = maps.mask_kspace(ishape,iwcs,lmin=args.tellmin,lmax=args.tellmax) pmask = maps.mask_kspace(ishape,iwcs,lmin=args.pellmin,lmax=args.pellmax) kmask = maps.mask_kspace(ishape,iwcs,lmin=args.kellmin,lmax=args.kellmax) qest = lensing.qest(ishape,iwcs,theory,noise2d=nT,beam2d=kbeam,kmask=tmask,noise2d_P=nP,kmask_P=pmask,kmask_K=kmask,pol=pol,grad_cut=None,unlensed_equals_lensed=True) taper,w2 = maps.get_taper_deg(ishape,iwcs,taper_width_degrees = args.taper_width,pad_width_degrees = args.pad_width) fc = maps.FourierCalc(oshape,iwcs,iau=args.iau) purifier = maps.Purify(ishape,iwcs,taper) if args.purify else None return qest,ngen,kbeam,binner,taper,fc,purifier
def init_flat(ishape,iwcs): modlmap = enmap.modlmap(ishape,iwcs) lmax = modlmap.max() ells = np.arange(2,lmax,1) ncomp = 3 if pol else 1 ps = np.zeros((ncomp,ncomp,ells.size)) ps[0,0] = theory.uCl('TT',ells) if pol: ps[1,1] = theory.uCl('EE',ells) ps[1,0] = theory.uCl('TE',ells) ps[0,1] = theory.uCl('TE',ells) oshape = (3,)+ishape if pol else ishape mgen = maps.MapGen(oshape,iwcs,ps) psk = theory.gCl('kk',ells).reshape((1,1,ells.size)) kgen = maps.MapGen(ishape,iwcs,psk) return mgen,kgen
def __init__(self, shape, wcs, components, constDict, ksz_file='input/ksz_BBPS.txt', ksz_p_file='input/ksz_p_BBPS.txt', tsz_cib_file='input/sz_x_cib_template.dat', ksz_battaglia_test_csv=None, tsz_battaglia_template_csv=None): from enlib import enmap from orphics import maps modlmap = enmap.modlmap(shape, wcs) fgNoises.__init__(self, constDict, ksz_file, ksz_p_file, tsz_cib_file, ksz_battaglia_test_csv, tsz_battaglia_template_csv, components, lmax=modlmap.max()) self.mgens = {} for component in components: noise = self.noises[component](self.ells) ps = noise.reshape((1, 1, self.ells.size)) self.mgens[component] = maps.MapGen(shape, wcs, ps)
def __init__(self, shape, wcs, beams, rmss, lknees, alphas, aniss, inhoms, nsplits, plancks, response_dict, ps_dict, ellmin=100): """ TODO: inhomogenity noise cross covariance """ self.fc = maps.FourierCalc(shape, wcs) self.narrays = len(beams) self.modlmap = enmap.modlmap(shape, wcs) self.beams = beams self.inhoms = inhoms self.nsplits = nsplits self.plancks = plancks.astype(np.bool) self.ngens = [] antemplate = covtools.get_anisotropic_noise_template(shape, wcs) for rms, lknee, alpha, anis in zip(rmss, lknees, alphas, aniss): if anis: template = antemplate.copy() else: template = 1 p2d = covtools.rednoise(enmap.modlmap(shape, wcs), rms, lknee, alpha) * template p2d[self.modlmap < ellmin] = 0 self.ngens.append(maps.MapGen(shape, wcs, p2d[None, None, ...])) self.fgens = {} assert "cmb" in ps_dict.keys() self.components = ps_dict.keys() for key in ps_dict.keys(): self.fgens[key] = maps.MapGen(shape, wcs, ps_dict[key][None, None, ...]) self.shape = shape self.wcs = wcs self.rdict = response_dict self.ellmin = ellmin
def add_noise_2d(self, nT, nP=None): self.nT = nT if nP is None: nP = 2. * nT self.nP = nP ps_noise = np.zeros( (3, 3, self.modlmap.shape[0], self.modlmap.shape[1])) ps_noise[0, 0] = nT ps_noise[1, 1] = nP ps_noise[2, 2] = nP noisecov = ps_noise self.is_2d_noise = True self.ngenerator = maps.MapGen(self.shape, self.wcs, noisecov)
def add_white_noise_with_atm(self, noise_uK_arcmin_T, noise_uK_arcmin_P=None, lknee_T=0., alpha_T=0., lknee_P=0., alpha_P=0.): map_dimensionless = self.dimensionless TCMB = self.TCMB self.nT = cmb.white_noise_with_atm_func(self.modlmap, noise_uK_arcmin_T, lknee_T, alpha_T, map_dimensionless, TCMB) if noise_uK_arcmin_P is None and np.isclose( lknee_T, lknee_P) and np.isclose(alpha_T, alpha_P): self.nP = 2. * self.nT.copy() else: if noise_uK_arcmin_P is None: noise_uK_arcmin_P = np.sqrt(2.) * noise_uK_arcmin_T self.nP = cmb.white_noise_with_atm_func(self.modlmap, noise_uK_arcmin_P, lknee_P, alpha_P, map_dimensionless, TCMB) TCMBt = TCMB if map_dimensionless else 1. ps_noise = np.zeros((3, 3, self.pix_ells.size)) ps_noise[0, 0] = self.pix_ells * 0. + (noise_uK_arcmin_T * np.pi / 180. / 60. / TCMBt)**2. ps_noise[1, 1] = self.pix_ells * 0. + (noise_uK_arcmin_P * np.pi / 180. / 60. / TCMBt)**2. ps_noise[2, 2] = self.pix_ells * 0. + (noise_uK_arcmin_P * np.pi / 180. / 60. / TCMBt)**2. noisecov = ps_noise self.is_2d_noise = False self.ngenerator = maps.MapGen(self.shape, self.wcs, noisecov)
def __init__(self,shape,wcs,theory,freqs,beams,noises,lknees,alphas,ellmins,ellmaxes): fgn = fg.fgNoises(cosmology.defaultConstants,ksz_file='/home/msyriac/repos/szar/input/ksz_BBPS.txt', ksz_p_file='/home/msyriac/repos/szar/input/ksz_p_BBPS.txt', tsz_cib_file='/home/msyriac/repos/szar/input/sz_x_cib_template.txt', ksz_battaglia_test_csv=None, tsz_battaglia_template_csv="/home/msyriac/repos/szar/input/sz_template_battaglia.csv", rs_template="/home/msyriac/repos/szar/input/fiducial_scalCls_lensed_5_5.txt", rsx_template="/home/msyriac/repos/szar/input/fiducial_scalCls_lensed_1_5.txt", components=['tsz','cibp','cibc','radps'],lmax=20000) self.modlmap = enmap.modlmap(shape,wcs) modlmap = self.modlmap self.fgn = fgn lmax = self.modlmap.max() ells = np.arange(0,lmax,1) ps_cmb = theory.lCl('TT',modlmap).reshape((1,1,shape[-2],shape[-1])) self.ps_cmb = ps_cmb ps_y = fgn.tsz_template(ells).reshape((1,1,ells.size))*self.fgn.c['A_tsz']*2.*np.pi*np.nan_to_num(1./ells/(ells+1.)) ps_cibp = (fgn.c['A_cibp'] * ((ells/fgn.c['ell0sec'])) ** 2.0 *2.*np.pi*np.nan_to_num(1./ells/(ells+1.))).reshape((1,1,ells.size)) ps_cibc = (fgn.c['A_cibc'] * ((ells/fgn.c['ell0sec'])) ** (2.-fgn.c['n_cib']) * 2.*np.pi*np.nan_to_num(1./ells/(ells+1.))).reshape((1,1,ells.size)) ps_radps = (fgn.c['A_ps'] * ((ells/fgn.c['ell0sec'])) ** 2 * 2.*np.pi*np.nan_to_num(1./ells/(ells+1.))).reshape((1,1,ells.size)) self.cgen = maps.MapGen(shape[-2:],wcs,ps_cmb) self.tgen = maps.MapGen(shape[-2:],wcs,ps_y) self.cibpgen = maps.MapGen(shape[-2:],wcs,ps_cibp) self.cibcgen = maps.MapGen(shape[-2:],wcs,ps_cibc) self.radpsgen = maps.MapGen(shape[-2:],wcs,ps_radps) self.shape = shape ; self.wcs = wcs self.freqs = freqs self.kbeams = [] self.ngens = [] self.n2ds = [] for ai,nu in enumerate(self.freqs): self.kbeams.append(maps.gauss_beam(fwhm=beams[ai],ell=self.modlmap)) n2d = cosmology.noise_func(self.modlmap,0,noises[ai],lknee=lknees[ai],alpha=alphas[ai],dimensionless=False,TCMB=2.7255e6) n2d[modlmap<ellmins[ai]] = 0 n2d[modlmap>ellmaxes[ai]] = 0 n2dmod = n2d.copy() n2dmod[modlmap>ellmaxes[ai]] = 1e90 n2dmod[modlmap<ellmins[ai]] = 1e90 self.n2ds.append(n2dmod.copy()) ps_noise = n2d.reshape((1,1,shape[-2],shape[-1])) self.ngens.append(maps.MapGen(shape[-2:],wcs,ps_noise)) self.ellmins = ellmins self.ellmaxes = ellmaxes
theory_file_root = "../alhazen/data/Aug6_highAcc_CDM" theory = cosmology.loadTheorySpectraFromCAMB(theory_file_root, unlensedEqualsLensed=False, useTotal=False, TCMB=2.7255e6, 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,
omap = np.sin(ymap/np.pi*100) + np.cos(xmap/np.pi*100) mfact = 10 afact = 20 rms = (omap - omap.min())*mfact + afact # io.hplot(rms,colorbar=True) pmap = enmap.pixsizemap(shape,wcs) ivar = maps.ivar(shape,wcs,rms,ipsizemap=pmap) # io.hplot(ivar,colorbar=True) my_tasks = range(nsims) theory = cosmology.default_theory() cov = theory.lCl('TT',modlmap) mgen = maps.MapGen((1,)+shape,wcs,cov=cov[None,None]) fwhm = 1.5 wnoise = 40. kbeam = maps.gauss_beam(modlmap,fwhm) feed_dict = {} lmin = 200 lmax = 3000 Lmin = 40 Lmax = 3000 xmask = maps.mask_kspace(shape,wcs,lmin=lmin,lmax=lmax) ymask = xmask kmask = maps.mask_kspace(shape,wcs,lmin=Lmin,lmax=Lmax) feed_dict['uC_T_T'] = cov feed_dict['tC_T_T'] = cov + (wnoise*np.pi/180/60)**2 / kbeam**2
""" nsplits = 4 theory = cosmology.default_theory(lpad=40000) shape,wcs = maps.rect_geometry(width_deg=10.,px_res_arcmin=0.5) modlmap = enmap.modlmap(shape,wcs) rms = 1. lknee = 2500 alpha = -4 ps_noise = covtools.noise_average(covtools.get_anisotropic_noise(shape,wcs,rms,lknee,alpha,template_file=None,tmin=0,tmax=100),(16,16),radial_fit=True,bin_annulus=80)[0] ps_noise[modlmap<20] = 0 ps_signal = enmap.enmap(theory.lCl('TT',modlmap),wcs) mgen = maps.MapGen(shape,wcs,ps_signal[None,None]) ngen = maps.MapGen(shape,wcs,ps_noise[None,None]*nsplits) cmb = mgen.get_map(seed=(1,0)) observed = [] for i in range(nsplits): nreal = ngen.get_map(seed=(2,i)) observed.append(cmb+nreal) kmaps = [] fc = maps.FourierCalc(shape,wcs) for i in range(nsplits): kmaps.append(fc.power2d(observed[i])[1]) io.hplot(maps.ftrans(ps_noise),"noise_orig",grid=False)
# Fourier magnitude map modlmap = enmap.modlmap(shape, wcs) # Unlensed CMB theory theory = cosmology.default_theory() cltt2d = theory.uCl('TT', modlmap) clte2d = theory.uCl('TE', modlmap) clee2d = theory.uCl('EE', modlmap) power = np.zeros((3, 3, shape[0], shape[1])) power[0, 0] = cltt2d power[1, 1] = clee2d power[1, 2] = clte2d power[2, 1] = clte2d # Unlensed CMB generator mgen = maps.MapGen((3, ) + shape, wcs, power) for j, task in enumerate(my_tasks): print(f'Rank {rank} performing task {task} as index {j}') cmb = mgen.get_map( seed=cutils.get_seed('lensed', task, False) ) # unlensed map ; you can save it if you want, before lensing it cmb = lens_map(cmb) # do the lensing dcmb = cmb.resample( (3, dNpix, dNpix) ) # downsample the lensed CMB map (if saving unlensed, do the same to it) # For some reason, I was saving the Fourier transforms... probably since it is easier to apply a beam later kmap = enmap.map2harm(dcmb, iau=False) enmap.write_map(f'{savedir}lensed_kmap_real_{task:06d}.fits', kmap.real) enmap.write_map(f'{savedir}lensed_kmap_imag_{task:06d}.fits', kmap.imag)
# Theory theory_file_root = "../alhazen/data/Aug6_highAcc_CDM" cc = counts.ClusterCosmology(skipCls=True) theory = cosmology.loadTheorySpectraFromCAMB(theory_file_root,unlensedEqualsLensed=False, useTotal=False,TCMB = 2.7255e6,lpad=9000,get_dimensionless=False) # Simulate lmax = int(bmodlmap.max()+1) ells = np.arange(0,lmax,1) ps = theory.uCl('TT',ells).reshape((1,1,lmax)) ps_noise = np.array([(noise_uK_rad)**2.]*ells.size).reshape((1,1,ells.size)) mg = maps.MapGen(bshape,bwcs,ps) ng = maps.MapGen(bshape,bwcs,ps_noise) kamp_true = args.Amp kappa = lensing.nfw_kappa(kamp_true*1e15,bmodrmap,cc,overdensity=200.,critical=True,atClusterZ=True) phi,_ = lensing.kappa_to_phi(kappa,bmodlmap,return_fphi=True) grad_phi = enmap.grad(phi) posmap = enmap.posmap(bshape,bwcs) pos = posmap + grad_phi alpha_pix = enmap.sky2pix(bshape,bwcs,pos, safe=False) lens_order = 5 ### FG PROFS
fmodlmap = enmap.modlmap(fshape, fwcs) cmodlmap = enmap.modlmap(cshape, cwcs) print(fshape, cshape) mass = 2.e14 fkappa = lens_func(fmodrmap) phi, _ = lensing.kappa_to_phi(fkappa, fmodlmap, return_fphi=True) grad_phi = enmap.grad(phi) pos = enmap.posmap(fshape, fwcs) + grad_phi alpha_pix = enmap.sky2pix(fshape, fwcs, pos, safe=False) lmax = cmodlmap.max() ells = np.arange(0, lmax, 1) cls = theory.uCl('TT', ells) ps = cls.reshape((1, 1, ells.size)) mg = maps.MapGen(cshape, cwcs, ps) unlensed = mg.get_map() hunlensed = enmap.enmap(resample.resample_fft(unlensed.copy(), fshape), fwcs) hlensed = enlensing.displace_map(hunlensed, alpha_pix, order=lens_order, mode=mode) lensed = enmap.enmap(resample.resample_fft(hlensed, cshape), cwcs) rkappa = lens_func(rmodrmap) phi, _ = lensing.kappa_to_phi(rkappa, rmodlmap, return_fphi=True) grad_phi = enmap.grad(phi) pos = enmap.posmap(rshape, rwcs) + grad_phi ralpha_pix = enmap.sky2pix(rshape, rwcs, pos, safe=False)
lpad=9000, get_dimensionless=False) ls, nls = np.loadtxt("nlkk.dat", usecols=[0, 1], unpack=True) clkk = theory.gCl('kk', ls) ellrange = np.arange(0, 6000, 1) totcls = interp1d(ls, clkk + nls, bounds_error=False, fill_value="extrapolate")(ellrange) ps = totcls.reshape((1, 1, ellrange.size)) bshape, bwcs = maps.rect_geometry(width_deg=80., px_res_arcmin=2.0, height_deg=15.) tap_per = 1. / 40. * 100. pad_per = 1. / 40. * 100. mg = maps.MapGen(bshape, bwcs, ps) fc = maps.FourierCalc(bshape, bwcs) taper, w2 = maps.get_taper(bshape, taper_percent=tap_per, pad_percent=pad_per, weight=None) bmap = mg.get_map() #io.plot_img(bmap*taper,"map.png",high_res=True) # 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)
# Binning bin_edges = np.arange(0.,20.0,args.pix*2) binner = stats.bin2D(modrmap*60.*180./np.pi,bin_edges) # Noise model noise_uK_rad = args.noise*np.pi/180./60. normfact = np.sqrt(np.prod(enmap.pixsize(shape,wcs))) kbeam = maps.gauss_beam(args.beam,modlmap) # Simulate lmax = int(modlmap.max()+1) ells = np.arange(0,lmax,1) ps = theory.uCl('TT',ells).reshape((1,1,lmax)) ~ps_noise = np.array([(noise_uK_rad)**2.]*ells.size).reshape((1,1,ells.size)) mg = maps.MapGen(shape,wcs,ps) ng = maps.MapGen(shape,wcs,ps_noise) kamp_true = args.Amp kappa = lensing.nfw_kappa(kamp_true*1e15,modrmap,cc,overdensity=200.,critical=True,atClusterZ=True) phi,_ = lensing.kappa_to_phi(kappa,modlmap,return_fphi=True) grad_phi = enmap.grad(phi) posmap = enmap.posmap(shape,wcs) pos = posmap + grad_phi alpha_pix = enmap.sky2pix(shape,wcs,pos, safe=False) lens_order = 5 if rank==0: print("Starting sims...") # Stats Nsims = args.Nclusters Njobs = Nsims
from enlib import enmap import numpy as np import os,sys from scipy import signal cc = cosmology.Cosmology(lmax=6000,pickling=True) deg = 15. shape,wcs = maps.rect_geometry(width_deg=deg,px_res_arcmin=1.0) modlmap = enmap.modlmap(shape,wcs) modrmap = enmap.modrmap(shape,wcs) lmax = modlmap.max() ells = np.arange(0,lmax,1) ps = cc.theory.lCl('TT',ells).reshape((1,1,ells.size)) mgen = maps.MapGen(shape,wcs,ps) 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)
from pixell import enmap import numpy as np import os, sys from tilec import covtools, ilc from scipy.optimize import curve_fit deg = 20. px = 2.0 theory = cosmology.default_theory() shape, wcs = maps.rect_geometry(width_deg=deg, px_res_arcmin=px) modlmap = enmap.modlmap(shape, wcs) ells = np.arange(modlmap.max()) cltt = theory.lCl('TT', ells) mgen = maps.MapGen(shape, wcs, cltt[None, None]) noise = [10, 20] ngen1 = maps.MapGen(shape, wcs, (ells * 0 + (noise[0] * np.pi / 180. / 60.)**2.)[None, None]) ngen2 = maps.MapGen(shape, wcs, (ells * 0 + (noise[1] * np.pi / 180. / 60.)**2.)[None, None]) cov = enmap.enmap(np.zeros((shape[0], shape[1], 2, 2)), wcs) for i in range(2): for j in range(2): cov[..., i, j] = maps.interp( ells, cltt)(modlmap) + int(i == j) * (noise[i] * np.pi / 180. / 60.)**2.