def kappaToAlpha(self, kappaMap, test=False): fKappa = fft(kappaMap, axes=[-2, -1]) fAlpha = self.ftkernels * fKappa pixScaleY, pixScaleX = kappaMap.pixshape() Ny, Nx = kappaMap.shape #retAlpha = (np.fft.ifftshift(enmap.ifft(fAlpha,normalize=False).real)+kappaMap*0.)*pixScaleY*pixScaleX/Nx/Ny retAlpha = -(np.fft.ifftshift( ifft(fAlpha, axes=[-2, -1], normalize=False).real[::-1]) + kappaMap * 0.) * pixScaleY * pixScaleX / Nx / Ny if test: newKap = -np.nan_to_num(0.5 * enmap.div(retAlpha)) thetaMap = kappaMap.posmap() thetaModMap = 60. * 180. * (np.sum(thetaMap**2, 0)**0.5) / np.pi print "newkappaint ", np.nanmean(newKap[thetaModMap < 10.]) pl = Plotter() pl.plot2d(kappaMap) pl.done("output/oldKap.png") pl = Plotter() pl.plot2d(newKap) pl.done("output/newKap.png") ratio = np.nan_to_num(newKap / kappaMap) print thetaMap.shape print ratio[thetaModMap < 5].mean() pl = Plotter() pl.plot2d(ratio[200:-200, 200:-200]) pl.done("output/testratio.png") return retAlpha
def fftFromLiteMap(liteMap,applySlepianTaper = False,nresForSlepian=3.0): """ @brief Creates an fft2D object out of a liteMap @param liteMap The map whose fft is being taken @param applySlepianTaper If True applies the lowest order taper (to minimize edge-leakage) @param nresForSlepian If above is True, specifies the resolution of the taeper to use. """ ft = fft2D() ft.Nx = liteMap.Nx ft.Ny = liteMap.Ny flTrace.issue("flipper.fftTools",1, "Taking FFT of map with (Ny,Nx)= (%f,%f)"%(ft.Ny,ft.Nx)) ft.pixScaleX = liteMap.pixScaleX ft.pixScaleY = liteMap.pixScaleY lx = 2*np.pi * fftfreq( ft.Nx, d = ft.pixScaleX ) ly = 2*np.pi * fftfreq( ft.Ny, d = ft.pixScaleY ) ix = np.mod(np.arange(ft.Nx*ft.Ny),ft.Nx) # This was dividing ints, so change below needed to maintain same behaviour in python3 iy = np.array(np.arange(ft.Nx*ft.Ny)/ft.Nx, dtype = int) modLMap = np.zeros([ft.Ny,ft.Nx]) modLMap[iy,ix] = np.sqrt(lx[ix]**2 + ly[iy]**2) ft.modLMap = modLMap ft.lx = lx ft.ly = ly ft.ix = ix ft.iy = iy ft.thetaMap = np.zeros([ft.Ny,ft.Nx]) ft.thetaMap[iy[:],ix[:]] = np.arctan2(ly[iy[:]],lx[ix[:]]) ft.thetaMap *=180./np.pi map = liteMap.data.copy() #map = map0.copy() #map[:,:] =map0[::-1,:] taper = map.copy()*0.0 + 1.0 if (applySlepianTaper) : try: path_to_taper = os.path.join(__FLIPPER_DIR__, "tapers", 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) f = open(path_to_taper) taper = pickle.load(f) f.close() except: taper = slepianTaper00(ft.Nx,ft.Ny,nresForSlepian) path_to_taper = os.path.join(__FLIPPER_DIR__, "tapers", 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) f = open(path_to_taper, mode="w") pickle.dump(taper,f) f.close() ft.kMap = fftfast.fft(map*taper,axes=[-2,-1]) del map, modLMap, lx, ly return ft
def fftFromLiteMap(liteMap,applySlepianTaper = False,nresForSlepian=3.0): """ @brief Creates an fft2D object out of a liteMap @param liteMap The map whose fft is being taken @param applySlepianTaper If True applies the lowest order taper (to minimize edge-leakage) @param nresForSlepian If above is True, specifies the resolution of the taeper to use. """ ft = fft2D() ft.Nx = liteMap.Nx ft.Ny = liteMap.Ny flTrace.issue("flipper.fftTools",1, "Taking FFT of map with (Ny,Nx)= (%f,%f)"%(ft.Ny,ft.Nx)) ft.pixScaleX = liteMap.pixScaleX ft.pixScaleY = liteMap.pixScaleY lx = 2*np.pi * fftfreq( ft.Nx, d = ft.pixScaleX ) ly = 2*np.pi * fftfreq( ft.Ny, d = ft.pixScaleY ) ix = np.mod(np.arange(ft.Nx*ft.Ny),ft.Nx) iy = np.arange(ft.Nx*ft.Ny)/ft.Nx modLMap = np.zeros([ft.Ny,ft.Nx]) modLMap[iy,ix] = np.sqrt(lx[ix]**2 + ly[iy]**2) ft.modLMap = modLMap ft.lx = lx ft.ly = ly ft.ix = ix ft.iy = iy ft.thetaMap = np.zeros([ft.Ny,ft.Nx]) ft.thetaMap[iy[:],ix[:]] = np.arctan2(ly[iy[:]],lx[ix[:]]) ft.thetaMap *=180./np.pi map = liteMap.data.copy() #map = map0.copy() #map[:,:] =map0[::-1,:] taper = map.copy()*0.0 + 1.0 if (applySlepianTaper) : try: path_to_taper = os.path.join(__FLIPPER_DIR__, "tapers", 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) f = open(path_to_taper) taper = pickle.load(f) f.close() except: taper = slepianTaper00(ft.Nx,ft.Ny,nresForSlepian) path_to_taper = os.path.join(__FLIPPER_DIR__, "tapers", 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) f = open(path_to_taper, mode="w") pickle.dump(taper,f) f.close() ft.kMap = fftfast.fft(map*taper,axes=[-2,-1]) del map, modLMap, lx, ly return ft
def __init__(self, imap): thetaMap = imap.posmap() thetaModSqMap = np.sum(thetaMap**2, 0) kernels = thetaMap / thetaModSqMap / np.pi self.ftkernels = fft(kernels, axes=[-2, -1])
def upgradePixelPitch( m, N = 1 ): """ @brief go to finer pixels with fourier interpolation @param m a liteMap @param N go to 2^N times smaller pixels @return the map with smaller pixels """ Ny = m.Ny*2**N Nx = m.Nx*2**N npix = Ny*Nx ft = fftfast.fft(m.data,axes=[-2,-1]) ftShifted = np.fft.fftshift(ft) newFtShifted = np.zeros((Ny, Nx), dtype=np.complex128) # From the np.fft.fftshift help: # """ # Shift zero-frequency component to center of spectrum. # # This function swaps half-spaces for all axes listed (defaults to all). # If len(x) is even then the Nyquist component is y[0]. # """ # # So in the case that we have an odd dimension in our map, we want to put # the extra zero at the beginning if m.Nx % 2 != 0: offsetX = (Nx-m.Nx)/2 + 1 else: offsetX = (Nx-m.Nx)/2 if m.Ny % 2 != 0: offsetY = (Ny-m.Ny)/2 + 1 else: offsetY = (Ny-m.Ny)/2 newFtShifted[offsetY:offsetY+m.Ny,offsetX:offsetX+m.Nx] = ftShifted del ftShifted ftNew = np.fft.ifftshift(newFtShifted) del newFtShifted # Finally, deconvolve by the pixel window mPix = np.copy(np.real(ftNew)) mPix[:] = 0.0 mPix[mPix.shape[0]/2-(2**(N-1)):mPix.shape[0]/2+(2**(N-1)),mPix.shape[1]/2-(2**(N-1)):mPix.shape[1]/2+(2**(N-1))] = 1./(2.**N)**2 ftPix = fftfast.fft(mPix,axes=[-2,-1]) del mPix inds = np.where(ftNew != 0) ftNew[inds] /= np.abs(ftPix[inds]) newData = fftfast.ifft(ftNew,axes=[-2,-1],normalize=True)*(2**N)**2 del ftNew del ftPix x0_new,y0_new = m.pixToSky(0,0) m = m.copy() # don't overwrite original m.wcs.header['NAXIS1'] = 2**N*m.wcs.header['NAXIS1'] m.wcs.header['NAXIS2'] = 2**N*m.wcs.header['NAXIS2'] m.wcs.header['CDELT1'] = m.wcs.header['CDELT1']/2.**N m.wcs.header['CDELT2'] = m.wcs.header['CDELT2']/2.**N m.wcs.updateFromHeader() p_x, p_y = m.skyToPix(x0_new, y0_new) m.wcs.header['CRPIX1'] = m.wcs.header['CRPIX1'] - p_x m.wcs.header['CRPIX2'] = m.wcs.header['CRPIX2'] - p_y m.wcs.updateFromHeader() mNew = liteMapFromDataAndWCS(np.real(newData), m.wcs) mNew.data[:] = newData[:] return mNew
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
import sys from mpi4py import MPI Nyorig = int(sys.argv[1]) Nxorig = int(sys.argv[2]) N = int(sys.argv[3]) # MPI set up comm = MPI.COMM_WORLD rank = comm.Get_rank() numcores = comm.Get_size() nthread_fft = multiprocessing.cpu_count() print "Number of threads: ", nthread_fft, " on rank ", rank Ny = Nyorig Nx = Nxorig A = np.random.normal(0., 1., (Ny, Nx)) for i in range(1, nthread_fft + 1)[::-1]: B = fftfast.fft(A, axes=[-2, -1], flags=['FFTW_MEASURE'], nthread=i) st = time.time() for j in range(N): B = fftfast.fft(A, axes=[-2, -1], nthread=i) elapsed = time.time() - st print "Enlib: ", '{:.2f}'.format(elapsed * 1000. / N), " milliseconds for ", i, " threads."