noiseP = 56.6 tellmin = 2 tellmax = 3000 gradCut = 10000 pellmin = 2 pellmax = 3000 deg = 10. px = 0.5 arc = deg*60. bin_edges = np.arange(10,3000,10) #theory = loadTheorySpectraFromCAMB(cambRoot,unlensedEqualsLensed=False,useTotal=False,lpad=9000) lmap = lm.makeEmptyCEATemplate(raSizeDeg=arc/60., decSizeDeg=arc/60.,pixScaleXarcmin=px,pixScaleYarcmin=px) print lmap.data.shape myNls = NlGenerator(lmap,theory,bin_edges,gradCut=gradCut) myNls.updateNoise(beam,noiseT,noiseP,tellmin,tellmax,pellmin,pellmax) #polCombList = ['TT','EE','ET','TE','EB','TB'] #colorList = ['red','blue','green','cyan','orange','purple'] polCombList = ['TT','EE','ET','EB','TB'] colorList = ['red','blue','green','orange','purple'] ellkk = np.arange(2,9000,1) Clkk = theory.gCl("kk",ellkk) pl = Plotter(scaleY='log',scaleX='log') pl.add(ellkk,4.*Clkk/2./np.pi)
#beamRange = np.arange(0.5,5.0,0.5) beamRange = np.arange(5.0, 10.5, 0.5) beamscale = lambda b: np.sqrt(8. * np.log(2.)) * 60. * 180. / np.pi / b kmin = 40 deg = 10. #px = 0.2 px = 0.5 dell = 10 theory = loadTheorySpectraFromCAMB(cambRoot, unlensedEqualsLensed=False, useTotal=False, lpad=9000) lmap = lm.makeEmptyCEATemplate(raSizeDeg=deg, decSizeDeg=deg, pixScaleXarcmin=px, pixScaleYarcmin=px) print((lmap.data.shape)) i = 0 for gradCut in [10000, 2000]: myNls = NlGenerator(lmap, theory, gradCut=gradCut) for polComb in ['TT', 'TE', 'EE', 'EB', 'ET', 'TB']: for beamY in beamRange: beamell = beamscale(beamY) for tellmaxY, pellmaxY in [(3000, 5000), (beamell, beamell)]: for noiseX,beamX,lab,tellminX,tellmaxX,pellminX,pellmaxX in \ [(noiseY,beamY,"sameGrad",tellminY,tellmaxY,pellminY,pellmaxY), \ (30.,7.0,"planckGrad",2,3000,2,3000)]:
kellmin = 2 kellmax = 2000 gradCut = None degx = 5. degy = 5. px = 2.0 TCMB = 2.7255e6 hugeTemplate = lm.makeEmptyCEATemplate(degx,degy,pixScaleXarcmin=px,pixScaleYarcmin=px) lxMap,lyMap,modLMap,thetaMap,lx,ly = fmaps.getFTAttributesFromLiteMap(hugeTemplate) nfreq = modLMap.max() assert nfreq>cmbellmax assert nfreq>kellmax from orphics.tools.cmb import loadTheorySpectraFromCAMB cambRoot = os.environ['HOME']+"/repos/cmb-lensing-projections/data/TheorySpectra/ell28k_highacc" theory = loadTheorySpectraFromCAMB(cambRoot,unlensedEqualsLensed=True,useTotal=False,TCMB = 2.7255e6,lpad=9000) #theory = loadTheorySpectraFromCAMB(cambRoot,unlensedEqualsLensed=False,useTotal=False,TCMB = 2.7255e6,lpad=9000) Nlfuncdict={} Nlfuncdict['TT'] = cmb.get_noise_func(beamArcmin,noiseT,TCMB=TCMB) Nlfuncdict['EE'] = cmb.get_noise_func(beamArcmin,noiseP,TCMB=TCMB) Nlfuncdict['BB'] = cmb.get_noise_func(beamArcmin,noiseP,TCMB=TCMB)
import numpy as np import orphics.analysis.flatMaps as fmaps import orphics.tools.io as io import flipper.liteMap as lm from enlib.fft import fft, ifft from numpy.fft import fftshift, ifftshift from enlib.resample import resample_fft, resample_bin arcX = 20 * 60. arcY = 10 * 60. arc = 10. * 60. px = 0.5 pxDn = 7.5 fineTemplate = lm.makeEmptyCEATemplate(arcX / 60., arcY / 60., pixScaleXarcmin=px, pixScaleYarcmin=px) lxMap, lyMap, modLMap, thetaMap, lx, ly = fmaps.getFTAttributesFromLiteMap( fineTemplate) xMap, yMap, modRMap, xx, yy = fmaps.getRealAttributes(fineTemplate) # sigArc = 3.0 # sig = sigArc*np.pi/180./60. # fineTemplate.data = np.exp(-modRMap**2./2./sig**2.) import btip.inpaintStamp as inp ell, Cl = np.loadtxt("../btip/data/cltt_lensed_Feb18.txt", unpack=True) ell, Cl = inp.total_1d_power(ell, Cl, ellmax=modLMap.max(),
def NFWMatchedFilterSN(clusterCosmology,log10Moverh,c,z,ells,Nls,kellmax,overdensity=500.,critical=True,atClusterZ=True,arcStamp=100.,pxStamp=0.05,saveId=None,verbose=False,rayleighSigmaArcmin=None,returnKappa=False,winAtLens=None): if rayleighSigmaArcmin is not None: assert rayleighSigmaArcmin>=pxStamp M = 10.**log10Moverh lmap = lm.makeEmptyCEATemplate(raSizeDeg=arcStamp/60., decSizeDeg=arcStamp/60.,pixScaleXarcmin=pxStamp,pixScaleYarcmin=pxStamp) kellmin = 2.*np.pi/arcStamp*np.pi/60./180. xMap,yMap,modRMap,xx,yy = fmaps.getRealAttributes(lmap) lxMap,lyMap,modLMap,thetaMap,lx,ly = fmaps.getFTAttributesFromLiteMap(lmap) cc = clusterCosmology cmb = False if winAtLens is None: cmb = True comS = cc.results.comoving_radial_distance(cc.cmbZ)*cc.h comL = cc.results.comoving_radial_distance(z)*cc.h winAtLens = (comS-comL)/comS kappaReal, r500 = NFWkappa(cc,M,c,z,modRMap*180.*60./np.pi,winAtLens,overdensity=overdensity,critical=critical,atClusterZ=atClusterZ) dAz = cc.results.angular_diameter_distance(z) * cc.h th500 = r500/dAz #fiveth500 = 10.*np.pi/180./60. #5.*th500 fiveth500 = 5.*th500 # print "5theta500 " , fiveth500*180.*60./np.pi , " arcminutes" # print "maximum theta " , modRMap.max()*180.*60./np.pi, " arcminutes" kInt = kappaReal.copy() kInt[modRMap>fiveth500] = 0. # print "mean kappa inside theta500 " , kInt[modRMap<fiveth500].mean() # print "area of th500 disc " , np.pi*fiveth500**2.*(180.*60./np.pi)**2. # print "estimated integral " , kInt[modRMap<fiveth500].mean()*np.pi*fiveth500**2. k500 = simps(simps(kInt, yy), xx) if verbose: print "integral of kappa inside disc ",k500 kappaReal[modRMap>fiveth500] = 0. #### !!!!!!!!! Might not be necessary! # if cmb: print z,fiveth500*180.*60./np.pi Ukappa = kappaReal/k500 # pl = Plotter() # pl.plot2d(Ukappa) # pl.done("output/kappa.png") ellmax = kellmax ellmin = kellmin Uft = fftfast.fft(Ukappa,axes=[-2,-1]) if rayleighSigmaArcmin is not None: Prayleigh = rayleigh(modRMap*180.*60./np.pi,rayleighSigmaArcmin) outDir = "/gpfs01/astro/www/msyriac/plots/" # io.quickPlot2d(Prayleigh,outDir+"rayleigh.png") rayK = fftfast.fft(ifftshift(Prayleigh),axes=[-2,-1]) rayK /= rayK[modLMap<1.e-3] Uft = Uft.copy()*rayK Upower = np.real(Uft*Uft.conjugate()) # pl = Plotter() # pl.plot2d(fftshift(Upower)) # pl.done("output/upower.png") Nls[Nls<0.]=0. s = splrep(ells,Nls,k=3) Nl2d = splev(modLMap,s) Nl2d[modLMap<ellmin]=np.inf Nl2d[modLMap>ellmax] = np.inf area = lmap.Nx*lmap.Ny*lmap.pixScaleX*lmap.pixScaleY Upower = Upower *area / (lmap.Nx*lmap.Ny)**2 filter = np.nan_to_num(Upower/Nl2d) #filter = np.nan_to_num(1./Nl2d) filter[modLMap>ellmax] = 0. filter[modLMap<ellmin] = 0. # pl = Plotter() # pl.plot2d(fftshift(filter)) # pl.done("output/filter.png") # if (cmb): print Upower.sum() # if not(cmb) and z>2.5: # bin_edges = np.arange(500,ellmax,100) # binner = bin2D(modLMap, bin_edges) # centers, nl2dells = binner.bin(Nl2d) # centers, upowerells = binner.bin(np.nan_to_num(Upower)) # centers, filterells = binner.bin(filter) # from orphics.tools.io import Plotter # pl = Plotter(scaleY='log') # pl.add(centers,upowerells,label="upower") # pl.add(centers,nl2dells,label="noise") # pl.add(centers,filterells,label="filter") # pl.add(ells,Nls,ls="--") # pl.legendOn(loc='upper right') # #pl._ax.set_ylim(0,1e-8) # pl.done("output/filterells.png") # sys.exit() varinv = filter.sum() std = np.sqrt(1./varinv) sn = k500/std if verbose: print sn if saveId is not None: np.savetxt("data/"+saveId+"_m"+str(log10Moverh)+"_z"+str(z)+".txt",np.array([log10Moverh,z,1./sn])) if returnKappa: return sn,fftfast.ifft(Uft,axes=[-2,-1],normalize=True).real*k500 return sn, k500, std
def getDLnMCMB(ells,Nls,clusterCosmology,log10Moverh,z,concentration,arcStamp,pxStamp,arc_upto,bin_width,expectedSN,Nclusters=1000,numSims=30,saveId=None,numPoints=1000,nsigma=8.,overdensity=500.,critical=True,atClusterZ=True): import flipper.liteMap as lm if saveId is not None: from orphics.tools.output import Plotter M = 10.**log10Moverh cc = clusterCosmology stepfilter_ellmax = max(ells) lmap = lm.makeEmptyCEATemplate(raSizeDeg=arcStamp/60., decSizeDeg=arcStamp/60.,pixScaleXarcmin=pxStamp,pixScaleYarcmin=pxStamp) xMap,yMap,modRMap,xx,xy = fmaps.getRealAttributes(lmap) lxMap,lyMap,modLMap,thetaMap,lx,ly = fmaps.getFTAttributesFromLiteMap(lmap) kappaMap,retR500 = NFWkappa(cc,M,concentration,z,modRMap*180.*60./np.pi,winAtLens,overdensity,critical,atClusterZ) finetheta = np.arange(0.01,arc_upto,0.01) finekappa,retR500 = NFWkappa(cc,M,concentration,z,finetheta,winAtLens,overdensity,critical,atClusterZ) kappaMap = fmaps.stepFunctionFilterLiteMap(kappaMap,modLMap,stepfilter_ellmax) generator = fmaps.GRFGen(lmap,ells,Nls) bin_edges = np.arange(0.,arc_upto,bin_width) binner = bin2D(modRMap*180.*60./np.pi, bin_edges) centers, thprof = binner.bin(kappaMap) if saveId is not None: pl = Plotter() pl.plot2d(kappaMap) pl.done("output/"+saveId+"kappa.png") expectedSNGauss = expectedSN*np.sqrt(numSims) sigma = 1./expectedSNGauss amplitudeRange = np.linspace(1.-nsigma*sigma,1.+nsigma*sigma,numPoints) lnLikes = 0. bigStamp = 0. for i in range(numSims): profiles,totstamp = getProfiles(generator,stepfilter_ellmax,kappaMap,binner,Nclusters) bigStamp += totstamp stats = getStats(profiles) if i==0 and (saveId is not None): pl = Plotter() pl.add(centers,thprof,lw=2,color='black') pl.add(finetheta,finekappa,lw=2,color='black',ls="--") pl.addErr(centers,stats['mean'],yerr=stats['errmean'],lw=2) pl._ax.set_ylim(-0.01,0.3) pl.done("output/"+saveId+"profile.png") pl = Plotter() pl.plot2d(totstamp) pl.done("output/"+saveId+"totstamp.png") Likes = getAmplitudeLikelihood(stats['mean'],stats['covmean'],amplitudeRange,thprof) lnLikes += np.log(Likes) width = amplitudeRange[1]-amplitudeRange[0] Likes = np.exp(lnLikes) Likes = Likes / (Likes.sum()*width) #normalize ampBest,ampErr = cfit(norm.pdf,amplitudeRange,Likes,p0=[1.0,0.5])[0] sn = ampBest/ampErr/np.sqrt(numSims) snAll = ampBest/ampErr if snAll<5.: print "WARNING: ", saveId, " run with mass ", M , " and redshift ", z , " has overall S/N<5. \ Consider re-running with a greater numSims, otherwise estimate of per Ncluster S/N will be noisy." if saveId is not None: Fit = np.array([np.exp(-0.5*(x-ampBest)**2./ampErr**2.) for x in amplitudeRange]) Fit = Fit / (Fit.sum()*width) #normalize pl = Plotter() pl.add(amplitudeRange,Likes,label="like") pl.add(amplitudeRange,Fit,label="fit") pl.legendOn(loc = 'lower left') pl.done("output/"+saveId+"like.png") pl = Plotter() pl.plot2d(bigStamp/numSims) pl.done("output/"+saveId+"bigstamp.png") np.savetxt("data/"+saveId+"_m"+str(log10Moverh)+"_z"+str(z)+".txt",np.array([log10Moverh,z,1./sn])) return 1./sn
import flipper.liteMap as lm from orphics.theory.cosmology import Cosmology import numpy as np import os, sys import orphics.tools.io as io import orphics.tools.stats as stats import orphics.analysis.flatMaps as fmaps out_dir = os.environ['WWW'] cc = Cosmology(pickling=True, clTTFixFile="../szar/data/cltt_lensed_Feb18.txt") lmap = lm.makeEmptyCEATemplate(raSizeDeg=20., decSizeDeg=20.) ells = np.arange(2, 6000, 1) Cell = cc.clttfunc(ells) #cc.theory.lCl('TT',ells) lmap.fillWithGaussianRandomField(ells, Cell, bufferFactor=1) io.highResPlot2d(lmap.data, out_dir + "map.png") p2d = fmaps.get_simple_power(lmap, lmap.data * 0. + 1.) lxMap, lyMap, modLMap, thetaMap, lx, ly = fmaps.getFTAttributesFromLiteMap( lmap) bin_edges = np.arange(20, 4000, 40) b = stats.bin2D(modLMap, bin_edges) cents, cdat = b.bin(p2d) pl = io.Plotter(scaleX='log', scaleY='log') pl.add(ells, Cell * ells**2.) pl.add(cents, cdat * cents**2.)