Ejemplo n.º 1
0
    def mapFromFFT(self,
                   kFilter=None,
                   kFilterFromList=None,
                   showFilter=False,
                   setMeanToZero=False,
                   returnFFT=False):
        """
        @brief Performs inverse fft (map from FFT) with an optional filter.
        @param kFilter Optional; If applied, resulting map = IFFT(fft*kFilter) 
        @return (optinally filtered) 2D real array
        """
        kMap = self.kMap.copy()
        kFilter0 = np.real(kMap.copy()) * 0. + 1.
        if kFilter != None:

            kFilter0 *= kFilter

        if kFilterFromList != None:
            kFilter = kMap.copy() * 0.
            l = kFilterFromList[0]
            Fl = kFilterFromList[1]
            FlSpline = splrep(l, Fl, k=3)
            ll = np.ravel(self.modLMap)

            kk = (splev(ll, FlSpline))

            kFilter = np.reshape(kk, [self.Ny, self.Nx])
            kFilter0 *= kFilter
        if setMeanToZero:
            id = np.where(self.modLMap == 0.)
            kFilter0[id] = 0.
        #showFilter =  True
        if showFilter:
            pylab.semilogy(l, Fl, 'r', ll, kk, 'b.')
            #utils.saveAndShow()
            #sys.exit()
            pylab.matshow(fftshift(kFilter0),origin="down",extent=[np.min(self.lx),\
                                                         np.max(self.lx),\
                                                         np.min(self.ly),\
                                                         np.max(self.ly)])
            pylab.show()

        kMap[:, :] *= kFilter0[:, :]
        if returnFFT:
            ftMap = self.copy()
            ftMap.kMap = kMap.copy()
            return np.real(fftfast.ifft(kMap, axes=[-2, -1],
                                        normalize=True)), ftMap
        else:
            return np.real(fftfast.ifft(kMap, axes=[-2, -1], normalize=True))
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
 def mapFromFFT(self,kFilter=None,kFilterFromList=None,showFilter=False,setMeanToZero=False,returnFFT=False):
     """
     @brief Performs inverse fft (map from FFT) with an optional filter.
     @param kFilter Optional; If applied, resulting map = IFFT(fft*kFilter) 
     @return (optinally filtered) 2D real array
     """
     kMap = self.kMap.copy()
     kFilter0 = np.real(kMap.copy())*0.+ 1.
     if kFilter != None:
         
         kFilter0 *= kFilter
         
     if kFilterFromList != None:
         kFilter = kMap.copy()*0.
         l = kFilterFromList[0]
         Fl = kFilterFromList[1] 
         FlSpline = splrep(l,Fl,k=3)
         ll = np.ravel(self.modLMap)
         
         kk = (splev(ll,FlSpline))
         
         kFilter = np.reshape(kk,[self.Ny,self.Nx])
         kFilter0 *= kFilter
     if setMeanToZero:
         id = np.where(self.modLMap == 0.)
         kFilter0[id] = 0.
     #showFilter =  True
     if showFilter:
         pylab.semilogy(l,Fl,'r',ll,kk,'b.')
         #utils.saveAndShow()
         #sys.exit()
         pylab.matshow(fftshift(kFilter0),origin="down",extent=[np.min(self.lx),\
                                                      np.max(self.lx),\
                                                      np.min(self.ly),\
                                                      np.max(self.ly)])
         pylab.show()
     
     kMap[:,:] *= kFilter0[:,:]
     if returnFFT:
         ftMap = self.copy()
         ftMap.kMap = kMap.copy()
         return np.real(fftfast.ifft(kMap,axes=[-2,-1],normalize=True)),ftMap
     else:
         return np.real(fftfast.ifft(kMap,axes=[-2,-1],normalize=True))
Ejemplo n.º 4
0
    def fillWithGaussianRandomField(self,ell,Cell,bufferFactor = 1):
        """
        Generates a GRF from an input power spectrum specified as ell, Cell 
        BufferFactor =1 means the map will be periodic boundary function
        BufferFactor > 1 means the map will be genrated on  a patch bufferFactor times 
        larger in each dimension and then cut out so as to have non-periodic bcs.
        
        Fills the data field of the map with the GRF realization
        """
        
        ft = fftFromLiteMap(self)
        Ny = self.Ny*bufferFactor
        Nx = self.Nx*bufferFactor
        
        bufferFactor = int(bufferFactor)
        
        
        realPart = np.zeros([Ny,Nx])
        imgPart  = np.zeros([Ny,Nx])
        
        ly = np.fft.fftfreq(Ny,d = self.pixScaleY)*(2*np.pi)
        lx = np.fft.fftfreq(Nx,d = self.pixScaleX)*(2*np.pi)
        #print ly
        modLMap = np.zeros([Ny,Nx])
        iy, ix = np.mgrid[0:Ny,0:Nx]
        modLMap[iy,ix] = np.sqrt(ly[iy]**2+lx[ix]**2)
        
        s = splrep(ell,Cell,k=3)
        
        ll = np.ravel(modLMap)
        kk = splev(ll,s)
        id = np.where(ll>ell.max())
        kk[id] = 0.
        #add a cosine ^2 falloff at the very end
        #id2 = np.where( (ll> (ell.max()-500)) & (ll<ell.max()))
        #lEnd = ll[id2]
        #kk[id2] *= np.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*np.pi/2)
        
        #pylab.loglog(ll,kk)
        

        area = Nx*Ny*self.pixScaleX*self.pixScaleY
        p = np.reshape(kk,[Ny,Nx]) /area * (Nx*Ny)**2
        assert np.all(p>=0)
        
        realPart = np.sqrt(p)*np.random.randn(Ny,Nx)
        imgPart = np.sqrt(p)*np.random.randn(Ny,Nx)
        
        
        kMap = realPart+1j*imgPart
        
        data = np.real(fftfast.ifft(kMap,axes=[-2,-1],normalize=True))
        
        b = bufferFactor
        self.data = data[(b-1)/2*self.Ny:(b+1)/2*self.Ny,(b-1)/2*self.Nx:(b+1)/2*self.Nx]
Ejemplo n.º 5
0
    def fillWithGaussianRandomField(self,ell,Cell,bufferFactor = 1):
        """
        Generates a GRF from an input power spectrum specified as ell, Cell 
        BufferFactor =1 means the map will be periodic boundary function
        BufferFactor > 1 means the map will be genrated on  a patch bufferFactor times 
        larger in each dimension and then cut out so as to have non-periodic bcs.
        
        Fills the data field of the map with the GRF realization
        """
        
        ft = fftFromLiteMap(self)
        Ny = self.Ny*bufferFactor
        Nx = self.Nx*bufferFactor
        
        bufferFactor = int(bufferFactor)
        
        
        realPart = np.zeros([Ny,Nx])
        imgPart  = np.zeros([Ny,Nx])
        
        ly = np.fft.fftfreq(Ny,d = self.pixScaleY)*(2*np.pi)
        lx = np.fft.fftfreq(Nx,d = self.pixScaleX)*(2*np.pi)
        #print ly
        modLMap = np.zeros([Ny,Nx])
        iy, ix = np.mgrid[0:Ny,0:Nx]
        modLMap[iy,ix] = np.sqrt(ly[iy]**2+lx[ix]**2)
        
        s = splrep(ell,Cell,k=3)
        
        ll = np.ravel(modLMap)
        kk = splev(ll,s)
        id = np.where(ll>ell.max())
        kk[id] = 0.
        #add a cosine ^2 falloff at the very end
        #id2 = np.where( (ll> (ell.max()-500)) & (ll<ell.max()))
        #lEnd = ll[id2]
        #kk[id2] *= np.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*np.pi/2)
        
        #pylab.loglog(ll,kk)
        

        area = Nx*Ny*self.pixScaleX*self.pixScaleY
        p = np.reshape(kk,[Ny,Nx]) /area * (Nx*Ny)**2
        assert np.all(p>=0)
        
        realPart = np.sqrt(p)*np.random.randn(Ny,Nx)
        imgPart = np.sqrt(p)*np.random.randn(Ny,Nx)
        
        
        kMap = realPart+1j*imgPart
        
        data = np.real(fftfast.ifft(kMap,axes=[-2,-1],normalize=True))
        
        b = bufferFactor
        self.data = data[(b-1)/2*self.Ny:(b+1)/2*self.Ny,(b-1)/2*self.Nx:(b+1)/2*self.Nx]
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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