def convolveWithBeam(self, ell, B_ell, threads=1): """ Return a liteMap object holding the data convolved with the beam specified as input in Fourier space Parameters ---------- ell : array_like The 1D ell values corresponding to the beam. B_ell : array_like, The 1D beam values in Fourier space. threads : int, optional Number of threads to use in pyFFTW calculations. Default is 1. """ beamFT = self.fillFourierTransform(ell, B_ell, elTrim=numpy.amax(ell), threads=threads) mapFT = fftTools.fftFromLiteMap(self, threads=threads) mapFT.kMap[:] *= beamFT.kMap[:] if have_pyFFTW: data_conv = numpy.real(ifft2(mapFT.kMap, threads=threads)) else: data_conv = numpy.real(ifft2(mapFT.kMap)) out = self.copy() out.data[:] = data_conv[:] return out
def phiToKappa(phiMap): kappaMap = phiMap.copy() phiF = fftTools.fftFromLiteMap(phiMap) kappaF = phiF.copy() kappaF.kMap *= kappaF.modLMap**2 / 2. kappaMap.data[:] = kappaF.mapFromFFT( setMeanToZero = True) return kappaMap
def makeTemplate(m, wl, ell, maxEll, outputFile = None): """ For a given map (m) return a 2D k-space template from a 1D specification wl ell = 2pi * i / deltaX (m is not overwritten) """ ell = numpy.array(ell) wl = numpy.array(wl) fT = fftTools.fftFromLiteMap(m) print "max_lx, max_ly", fT.lx.max(), fT.ly.max() print "m_dx, m_dy", m.pixScaleX, m.pixScaleY print "m_nx, m_ny", m.Nx, m.Ny l_f = numpy.floor(fT.modLMap) l_c = numpy.ceil(fT.modLMap) fT.kMap[:,:] = 0. for i in xrange(numpy.shape(fT.kMap)[0]): for j in xrange(numpy.shape(fT.kMap)[1]): if l_f[i,j] > maxEll or l_c[i,j] > maxEll: continue w_lo = wl[l_f[i,j]] w_hi = wl[l_c[i,j]] trueL = fT.modLMap[i,j] w = (w_hi-w_lo)*(trueL - l_f[i,j]) + w_lo fT.kMap[i,j] = w m = m.copy() m.data = abs(fT.kMap) if outputFile != None: m.writeFits(outputFile, overWrite = True) return m
def fillWithGaussianRandomField(self,ell,Cell,bufferFactor = 1): ft = fftTools.fftFromLiteMap(self) Ny = self.Ny*bufferFactor Nx = self.Nx*bufferFactor bufferFactor = int(bufferFactor) realPart = numpy.zeros([Ny,Nx]) imgPart = numpy.zeros([Ny,Nx]) ly = numpy.fft.fftfreq(Ny,d = self.pixScaleY)*(2*numpy.pi) lx = numpy.fft.fftfreq(Nx,d = self.pixScaleX)*(2*numpy.pi) modLMap = numpy.zeros([Ny,Nx]) iy, ix = numpy.mgrid[0:Ny,0:Nx] modLMap[iy,ix] = numpy.sqrt(ly[iy]**2+lx[ix]**2) s = splrep(ell,Cell,k=3) ll = numpy.ravel(modLMap) kk = splev(ll,s) id = numpy.where(ll>ell.max()) kk[id] = Cell[-1] area = Nx*Ny*self.pixScaleX*self.pixScaleY p = numpy.reshape(kk,[Ny,Nx]) /area * (Nx*Ny)**2 realPart = numpy.sqrt(p)*numpy.random.randn(Ny,Nx) imgPart = numpy.sqrt(p)*numpy.random.randn(Ny,Nx) kMap = realPart+1j*imgPart data = numpy.real(numpy.fft.ifft2(kMap)) 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] return(self)
def kappaToPhi(kappaMap): phiMap = kappaMap.copy() kappaF = fftTools.fftFromLiteMap(kappaMap) phiF = kappaF.copy() phiF.kMap /= (phiF.modLMap**2 / 2.) phiF.kMap[0,0] = 0 phiMap.data[:] = phiF.mapFromFFT( setMeanToZero = True) return phiMap
def fillWithGaussianRandomField(self, ell, Cell, bufferFactor=1, threads=1): """ Generate a Gaussian random field from an input power spectrum specified as ell, Cell. Notes ----- BufferFactor = 1 means the map will have periodic boundary function, while 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 boundary conditions. Fills the data field of the map with the GRF realization. """ ft = fftTools.fftFromLiteMap(self, threads=threads) Ny = self.Ny * bufferFactor Nx = self.Nx * bufferFactor bufferFactor = int(bufferFactor) realPart = numpy.zeros([Ny, Nx]) imgPart = numpy.zeros([Ny, Nx]) ly = fftfreq(Ny, d=self.pixScaleY) * (2 * numpy.pi) lx = fftfreq(Nx, d=self.pixScaleX) * (2 * numpy.pi) # print ly modLMap = numpy.zeros([Ny, Nx]) iy, ix = numpy.mgrid[0:Ny, 0:Nx] modLMap[iy, ix] = numpy.sqrt(ly[iy] ** 2 + lx[ix] ** 2) s = splrep(ell, Cell, k=3) ll = numpy.ravel(modLMap) kk = splev(ll, s) id = numpy.where(ll > ell.max()) kk[id] = 0.0 # add a cosine ^2 falloff at the very end # id2 = numpy.where( (ll> (ell.max()-500)) & (ll<ell.max())) # lEnd = ll[id2] # kk[id2] *= numpy.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*numpy.pi/2) # pylab.loglog(ll,kk) area = Nx * Ny * self.pixScaleX * self.pixScaleY p = numpy.reshape(kk, [Ny, Nx]) / area * (Nx * Ny) ** 2 realPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) imgPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) kMap = realPart + 1j * imgPart if have_pyFFTW: data = numpy.real(ifft2(kMap, threads=threads)) else: data = numpy.real(ifft2(kMap)) 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]
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]
def filterFromList(self, lFl, setMeanToZero=False): """ @brief Given an l-space filter as a tuple [\f$\ell,F_\ell\f$] returns a filtered map @param lFl A tuple [\f$\ell,F_\ell\f$], where \f$\ell\f$ and \f$ F_\ell \f$ are 1-D arrays representing the filter. @return The filtered liteMap """ ft = fftTools.fftFromLiteMap(self) filtData = ft.mapFromFFT(kFilterFromList=lFl, setMeanToZero=setMeanToZero) filtMap = self.copy() filtMap.data[:] = filtData[:] del ft del filtData return filtMap
def filterFromList(self,lFl,setMeanToZero=False): """ @brief Given an l-space filter as a tuple [\f$\ell,F_\ell\f$] returns a filtered map @param lFl A tuple [\f$\ell,F_\ell\f$], where \f$\ell\f$ and \f$ F_\ell \f$ are 1-D arrays representing the filter. @return The filtered liteMap """ ft = fftFromLiteMap(self) filtData = ft.mapFromFFT(kFilterFromList=lFl,setMeanToZero=setMeanToZero) filtMap = self.copy() filtMap.data[:] = filtData[:] del ft del filtData return filtMap
def fillFourierTransform(self, ell, cl, ftMap=None, elTrim=30000, threads=1): """ Fill the fourier transform of the map with the 1D values specified by (ell, cl). Parameters ---------- ell : array_like The 1D array of ell values to fill in. cl : array_like The 1D array of cl values to fill in. ftMap : fftTools.fft2D, optional If given, fill this fourier transform object to save time., Default is ``None``. This will not be overwritten. elTrim : float, optional Only fill in values ell < elTrim. Default is 30000. threads : int, optional Number of threads to use in pyFFTW calculations. Default is 1. Returns ------- ft : fftTools.fft2D The fft2D object with `kMap` attribute filled as specified """ # set the max ell to infty if not specified if elTrim == None: elTrim = numpy.inf # the fourier transform if ftMap is None: ft = fftTools.fftFromLiteMap(self, threads=threads) else: ft = ftMap.copy() ft.kMap[:] = 0.0 # make an interpolation function for real and imaginary parts sreal = splrep(ell, cl.real) doImag = False if cl.dtype == complex and len(numpy.nonzero(cl.imag)[0]) > 0: doImag = True simag = splrep(ell, cl.imag) # get indices of pixels that have values of ell less than elMax idx = numpy.where((ft.lx < elTrim) & (ft.lx > -elTrim))[0] idy = numpy.where((ft.ly < elTrim) & (ft.ly > -elTrim))[0] # make a meshgrid of indices ix, iy = numpy.meshgrid(idx, idy) # flatten the indices iy_flat, ix_flat = iy.flatten(), ix.flatten() # fill in the FT with the interpolated values if not doImag: ft.kMap[iy_flat, ix_flat] = splev(ft.modLMap[iy_flat, ix_flat], sreal) else: ft.kMap[iy_flat, ix_flat] = splev(ft.modLMap[iy_flat, ix_flat], sreal) + 1j * splev( ft.modLMap[iy_flat, ix_flat], simag ) # avoid weird extrapolations id = numpy.where(ft.modLMap > elTrim) ft.kMap[id] = 0.0 return ft
def fillFourierTransform(self, ell, cl, ftMap=None, elTrim=30000, threads=1): """ Fill the fourier transform of the map with the 1D values specified by (ell, cl). Parameters ---------- ell : array_like The 1D array of ell values to fill in. cl : array_like The 1D array of cl values to fill in. ftMap : fftTools.fft2D, optional If given, fill this fourier transform object to save time., Default is ``None``. This will not be overwritten. elTrim : float, optional Only fill in values ell < elTrim. Default is 30000. threads : int, optional Number of threads to use in pyFFTW calculations. Default is 1. Returns ------- ft : fftTools.fft2D The fft2D object with `kMap` attribute filled as specified """ # set the max ell to infty if not specified if elTrim == None: elTrim = numpy.inf # the fourier transform if ftMap is None: ft = fftTools.fftFromLiteMap(self, threads=threads) else: ft = ftMap.copy() ft.kMap[:] = 0. # make an interpolation function for real and imaginary parts sreal = splrep(ell, cl.real) doImag = False if cl.dtype == complex and len(numpy.nonzero(cl.imag)[0]) > 0: doImag = True simag = splrep(ell, cl.imag) # get indices of pixels that have values of ell less than elMax idx = numpy.where((ft.lx < elTrim) & (ft.lx > -elTrim))[0] idy = numpy.where((ft.ly < elTrim) & (ft.ly > -elTrim))[0] # make a meshgrid of indices ix, iy = numpy.meshgrid(idx, idy) # flatten the indices iy_flat, ix_flat = iy.flatten(), ix.flatten() # fill in the FT with the interpolated values if not doImag: ft.kMap[iy_flat, ix_flat] = splev(ft.modLMap[iy_flat, ix_flat], sreal) else: ft.kMap[iy_flat, ix_flat] = splev( ft.modLMap[iy_flat, ix_flat], sreal) + 1j * splev(ft.modLMap[iy_flat, ix_flat], simag) # avoid weird extrapolations id = numpy.where(ft.modLMap > elTrim) ft.kMap[id] = 0. return ft
def fillWithGaussianRandomField(self, ell, Cell, bufferFactor=1, threads=1): """ Generate a Gaussian random field from an input power spectrum specified as ell, Cell. Notes ----- BufferFactor = 1 means the map will have periodic boundary function, while 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 boundary conditions. Fills the data field of the map with the GRF realization. """ ft = fftTools.fftFromLiteMap(self, threads=threads) Ny = self.Ny * bufferFactor Nx = self.Nx * bufferFactor bufferFactor = int(bufferFactor) realPart = numpy.zeros([Ny, Nx]) imgPart = numpy.zeros([Ny, Nx]) ly = fftfreq(Ny, d=self.pixScaleY) * (2 * numpy.pi) lx = fftfreq(Nx, d=self.pixScaleX) * (2 * numpy.pi) #print ly modLMap = numpy.zeros([Ny, Nx]) iy, ix = numpy.mgrid[0:Ny, 0:Nx] modLMap[iy, ix] = numpy.sqrt(ly[iy]**2 + lx[ix]**2) s = splrep(ell, Cell, k=3) ll = numpy.ravel(modLMap) kk = splev(ll, s) id = numpy.where(ll > ell.max()) kk[id] = 0. #add a cosine ^2 falloff at the very end # id2 = numpy.where( (ll> (ell.max()-500)) & (ll<ell.max())) # lEnd = ll[id2] # kk[id2] *= numpy.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*numpy.pi/2) #pylab.loglog(ll,kk) area = Nx * Ny * self.pixScaleX * self.pixScaleY p = numpy.reshape(kk, [Ny, Nx]) / area * (Nx * Ny)**2 realPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) imgPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) kMap = realPart + 1j * imgPart if have_pyFFTW: data = numpy.real(ifft2(kMap, threads=threads)) else: data = numpy.real(ifft2(kMap)) 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]
def addLiteMapsWithSpectralWeighting(liteMap1, liteMap2, kMask1Params=None, kMask2Params=None, signalMap=None): """ @brief add two maps, weighting in Fourier space Maps must be the same size. @param kMask1Params mask params for liteMap1 (see fftTools.power2D.createKspaceMask) @param kMask2Params mask params for liteMap2 (see fftTools.power2D.createKspaceMask) @param signalMap liteMap with best estimate of signal to use when estimating noise weighting @return new map """ # Get fourier weights trace.issue("liteMap", 0, "Computing Weights") # np1 = fftTools.noisePowerFromLiteMaps(liteMap1, liteMap2, applySlepianTaper = False) # np2 = fftTools.noisePowerFromLiteMaps(liteMap2, liteMap1, applySlepianTaper = False) data1 = copy.copy(liteMap1.data) data2 = copy.copy(liteMap2.data) if signalMap != None: liteMap1.data[:] = (liteMap1.data - signalMap.data)[:] liteMap2.data[:] = (liteMap2.data - signalMap.data)[:] np1 = fftTools.powerFromLiteMap(liteMap1) # , applySlepianTaper = True) np2 = fftTools.powerFromLiteMap(liteMap2) # ), applySlepianTaper = True) print "testing", liteMap1.data == data1 liteMap1.data[:] = data1[:] liteMap2.data[:] = data2[:] n1 = np1.powerMap n2 = np2.powerMap # n1[numpy.where( n1<n1.max()*.002)] = n1.max()*.001 # n2[numpy.where( n2<n2.max()*.002)] = n2.max()*.001 w1 = 1 / n1 w2 = 1 / n2 m1 = numpy.median(w1) m2 = numpy.median(w2) w1[numpy.where(abs(w1) > 4 * m1)] = 4 * m1 w2[numpy.where(abs(w2) > 4 * m2)] = 4 * m2 # w1[:] = 1. # w2[:] = 1. # pylab.hist(w1.ravel()) # pylab.savefig("hist1.png") # pylab.clf() yrange = [4, 5000] np1.powerMap = w1 # np1.plot(pngFile="w1.png", log=True, zoomUptoL=8000, yrange = yrange) np2.powerMap = w2 # np2.plot(pngFile="w2.png", log=True, zoomUptoL=8000, yrange = yrange) if kMask1Params != None: np1.createKspaceMask(**kMask1Params) w1 *= np1.kMask if kMask2Params != None: np2.createKspaceMask(**kMask2Params) w2 *= np2.kMask pylab.clf() invW = 1.0 / (w1 + w2) invW[numpy.where(numpy.isnan(invW))] = 0.0 invW[numpy.where(numpy.isinf(invW))] = 0.0 trace.issue("liteMap", 3, "NaNs in inverse weight: %s" % str(numpy.where(numpy.isnan(invW)))) trace.issue("liteMap", 3, "Infs in inverse weight: %s" % str(numpy.where(numpy.isinf(invW)))) trace.issue("liteMap", 2, "Adding Maps") f1 = fftTools.fftFromLiteMap(liteMap1, applySlepianTaper=False) f2 = fftTools.fftFromLiteMap(liteMap2, applySlepianTaper=False) kTot = (f1.kMap * w1 + f2.kMap * w2) * invW trace.issue("liteMap", 3, "NaNs in filtered transform: %s" % str(numpy.where(numpy.isnan(kTot)))) f1.kMap = kTot finalMap = liteMap1.copy() finalMap.data[:] = 0.0 finalMap.data = f1.mapFromFFT() return finalMap
def fillWithGRFFromTemplate(self, twodPower, bufferFactor=1, threads=1): """ Generate a Gaussian random field from an input power spectrum specified as a 2d powerMap Notes ----- BufferFactor = 1 means the map will have periodic boundary function, while 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 boundary conditions. Fills the data field of the map with the GRF realization. """ ft = fftTools.fftFromLiteMap(self, threads=threads) Ny = self.Ny * bufferFactor Nx = self.Nx * bufferFactor bufferFactor = int(bufferFactor) assert (bufferFactor >= 1) realPart = numpy.zeros([Ny, Nx]) imgPart = numpy.zeros([Ny, Nx]) ly = fftfreq(Ny, d=self.pixScaleY) * (2 * numpy.pi) lx = fftfreq(Nx, d=self.pixScaleX) * (2 * numpy.pi) #print ly modLMap = numpy.zeros([Ny, Nx]) iy, ix = numpy.mgrid[0:Ny, 0:Nx] modLMap[iy, ix] = numpy.sqrt(ly[iy]**2 + lx[ix]**2) # divide out area factor area = twodPower.Nx * twodPower.Ny * twodPower.pixScaleX * twodPower.pixScaleY twodPower.powerMap *= (twodPower.Nx * twodPower.Ny)**2 / area if bufferFactor > 1 or twodPower.Nx != Nx or twodPower.Ny != Ny: lx_shifted = fftshift(twodPower.lx) ly_shifted = fftshift(twodPower.ly) twodPower_shifted = fftshift(twodPower.powerMap) f_interp = interp2d(lx_shifted, ly_shifted, twodPower_shifted) # ell = numpy.ravel(twodPower.modLMap) # Cell = numpy.ravel(twodPower.powerMap) # print ell # print Cell # s = splrep(ell,Cell,k=3) # # # ll = numpy.ravel(modLMap) # kk = splev(ll,s) kk = f_interp(fftshift(lx), fftshift(ly)) kk = ifftshift(kk) # id = numpy.where(modLMap > ell.max()) # kk[id] = 0. # add a cosine ^2 falloff at the very end # id2 = numpy.where( (ll> (ell.max()-500)) & (ll<ell.max())) # lEnd = ll[id2] # kk[id2] *= numpy.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*numpy.pi/2) # pylab.loglog(ll,kk) area = Nx * Ny * self.pixScaleX * self.pixScaleY #p = numpy.reshape(kk,[Ny,Nx]) /area * (Nx*Ny)**2 p = kk #/ area * (Nx*Ny)**2 else: area = Nx * Ny * self.pixScaleX * self.pixScaleY p = twodPower.powerMap #/area*(Nx*Ny)**2 realPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) imgPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) kMap = realPart + 1j * imgPart if have_pyFFTW: data = numpy.real(ifft2(kMap, threads=threads)) else: data = numpy.real(ifft2(kMap)) 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]
def fillWithGRFFromTemplate(self,twodPower,bufferFactor = 1): """ Generates a GRF from an input power spectrum specified as a 2d powerMap 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) assert(bufferFactor>=1) realPart = numpy.zeros([Ny,Nx]) imgPart = numpy.zeros([Ny,Nx]) ly = numpy.fft.fftfreq(Ny,d = self.pixScaleY)*(2*numpy.pi) lx = numpy.fft.fftfreq(Nx,d = self.pixScaleX)*(2*numpy.pi) #print ly modLMap = numpy.zeros([Ny,Nx]) iy, ix = numpy.mgrid[0:Ny,0:Nx] modLMap[iy,ix] = numpy.sqrt(ly[iy]**2+lx[ix]**2) if bufferFactor > 1: ell = numpy.ravel(twodPower.modLMap) Cell = numpy.ravel(twodPower.powerMap) print ell print Cell s = splrep(ell,Cell,k=3) ll = numpy.ravel(modLMap) kk = splev(ll,s) id = numpy.where(ll>ell.max()) kk[id] = 0. # add a cosine ^2 falloff at the very end # id2 = numpy.where( (ll> (ell.max()-500)) & (ll<ell.max())) # lEnd = ll[id2] # kk[id2] *= numpy.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*numpy.pi/2) # pylab.loglog(ll,kk) area = Nx*Ny*self.pixScaleX*self.pixScaleY p = numpy.reshape(kk,[Ny,Nx]) /area * (Nx*Ny)**2 else: area = Nx*Ny*self.pixScaleX*self.pixScaleY p = twodPower.powerMap/area*(Nx*Ny)**2 realPart = numpy.sqrt(p)*numpy.random.randn(Ny,Nx) imgPart = numpy.sqrt(p)*numpy.random.randn(Ny,Nx) kMap = realPart+1j*imgPart data = numpy.real(numpy.fft.ifft2(kMap)) 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]
def fillWithGRFFromTemplate(self, twodPower, bufferFactor=1, threads=1): """ Generate a Gaussian random field from an input power spectrum specified as a 2d powerMap Notes ----- BufferFactor = 1 means the map will have periodic boundary function, while 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 boundary conditions. Fills the data field of the map with the GRF realization. """ ft = fftTools.fftFromLiteMap(self, threads=threads) Ny = self.Ny * bufferFactor Nx = self.Nx * bufferFactor bufferFactor = int(bufferFactor) assert bufferFactor >= 1 realPart = numpy.zeros([Ny, Nx]) imgPart = numpy.zeros([Ny, Nx]) ly = fftfreq(Ny, d=self.pixScaleY) * (2 * numpy.pi) lx = fftfreq(Nx, d=self.pixScaleX) * (2 * numpy.pi) # print ly modLMap = numpy.zeros([Ny, Nx]) iy, ix = numpy.mgrid[0:Ny, 0:Nx] modLMap[iy, ix] = numpy.sqrt(ly[iy] ** 2 + lx[ix] ** 2) # divide out area factor area = twodPower.Nx * twodPower.Ny * twodPower.pixScaleX * twodPower.pixScaleY twodPower.powerMap *= (twodPower.Nx * twodPower.Ny) ** 2 / area if bufferFactor > 1 or twodPower.Nx != Nx or twodPower.Ny != Ny: lx_shifted = fftshift(twodPower.lx) ly_shifted = fftshift(twodPower.ly) twodPower_shifted = fftshift(twodPower.powerMap) f_interp = interp2d(lx_shifted, ly_shifted, twodPower_shifted) # ell = numpy.ravel(twodPower.modLMap) # Cell = numpy.ravel(twodPower.powerMap) # print ell # print Cell # s = splrep(ell,Cell,k=3) # # # ll = numpy.ravel(modLMap) # kk = splev(ll,s) kk = f_interp(fftshift(lx), fftshift(ly)) kk = ifftshift(kk) # id = numpy.where(modLMap > ell.max()) # kk[id] = 0. # add a cosine ^2 falloff at the very end # id2 = numpy.where( (ll> (ell.max()-500)) & (ll<ell.max())) # lEnd = ll[id2] # kk[id2] *= numpy.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*numpy.pi/2) # pylab.loglog(ll,kk) area = Nx * Ny * self.pixScaleX * self.pixScaleY # p = numpy.reshape(kk,[Ny,Nx]) /area * (Nx*Ny)**2 p = kk # / area * (Nx*Ny)**2 else: area = Nx * Ny * self.pixScaleX * self.pixScaleY p = twodPower.powerMap # /area*(Nx*Ny)**2 realPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) imgPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) kMap = realPart + 1j * imgPart if have_pyFFTW: data = numpy.real(ifft2(kMap, threads=threads)) else: data = numpy.real(ifft2(kMap)) 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]
def addLiteMapsWithSpectralWeighting( liteMap1, liteMap2, kMask1Params = None, kMask2Params = None, signalMap = None ): """ @brief add two maps, weighting in Fourier space Maps must be the same size. @param kMask1Params mask params for liteMap1 (see fftTools.power2D.createKspaceMask) @param kMask2Params mask params for liteMap2 (see fftTools.power2D.createKspaceMask) @param signalMap liteMap with best estimate of signal to use when estimating noise weighting @return new map """ #Get fourier weights flTrace.issue("liteMap", 0, "Computing Weights") #np1 = fftTools.noisePowerFromLiteMaps(liteMap1, liteMap2, applySlepianTaper = False) #np2 = fftTools.noisePowerFromLiteMaps(liteMap2, liteMap1, applySlepianTaper = False) data1 = copy.copy(liteMap1.data) data2 = copy.copy(liteMap2.data) if signalMap != None: liteMap1.data[:] = (liteMap1.data - signalMap.data)[:] liteMap2.data[:] = (liteMap2.data - signalMap.data)[:] np1 = fftTools.powerFromLiteMap(liteMap1)#, applySlepianTaper = True) np2 = fftTools.powerFromLiteMap(liteMap2)#), applySlepianTaper = True) print "testing", liteMap1.data == data1 liteMap1.data[:] = data1[:] liteMap2.data[:] = data2[:] n1 = np1.powerMap n2 = np2.powerMap # n1[np.where( n1<n1.max()*.002)] = n1.max()*.001 # n2[np.where( n2<n2.max()*.002)] = n2.max()*.001 w1 = 1/n1 w2 = 1/n2 m1 = np.median(w1) m2 = np.median(w2) w1[np.where(abs(w1)>4*m1)]=4*m1 w2[np.where(abs(w2)>4*m2)]=4*m2 #w1[:] = 1. #w2[:] = 1. #pylab.hist(w1.ravel()) #pylab.savefig("hist1.png") #pylab.clf() yrange = [4,5000] np1.powerMap = w1 #np1.plot(pngFile="w1.png", log=True, zoomUptoL=8000, yrange = yrange) np2.powerMap = w2 #np2.plot(pngFile="w2.png", log=True, zoomUptoL=8000, yrange = yrange) if kMask1Params != None: np1.createKspaceMask(**kMask1Params) w1 *= np1.kMask if kMask2Params != None: np2.createKspaceMask(**kMask2Params) w2 *= np2.kMask pylab.clf() invW = 1.0/(w1+w2) invW[np.where(np.isnan(invW))] = 0. invW[np.where(np.isinf(invW))] = 0. flTrace.issue("liteMap", 3, "NaNs in inverse weight: %s" % str(np.where(np.isnan(invW)))) flTrace.issue("liteMap", 3, "Infs in inverse weight: %s" % str(np.where(np.isinf(invW)))) flTrace.issue("liteMap", 2, "Adding Maps") f1 = fftTools.fftFromLiteMap( liteMap1, applySlepianTaper = False ) f2 = fftTools.fftFromLiteMap( liteMap2, applySlepianTaper = False ) kTot = (f1.kMap*w1 + f2.kMap*w2)*invW flTrace.issue("liteMap", 3, "NaNs in filtered transform: %s" % str(np.where(np.isnan(kTot)))) f1.kMap = kTot finalMap = liteMap1.copy() finalMap.data[:] = 0. finalMap.data = f1.mapFromFFT() return finalMap
def fillWithGRFFromTemplate(self, twodPower, bufferFactor=1): """ Generates a GRF from an input power spectrum specified as a 2d powerMap 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) assert (bufferFactor >= 1) realPart = numpy.zeros([Ny, Nx]) imgPart = numpy.zeros([Ny, Nx]) ly = numpy.fft.fftfreq(Ny, d=self.pixScaleY) * (2 * numpy.pi) lx = numpy.fft.fftfreq(Nx, d=self.pixScaleX) * (2 * numpy.pi) #print ly modLMap = numpy.zeros([Ny, Nx]) iy, ix = numpy.mgrid[0:Ny, 0:Nx] modLMap[iy, ix] = numpy.sqrt(ly[iy]**2 + lx[ix]**2) if bufferFactor > 1: ell = numpy.ravel(twodPower.modLMap) Cell = numpy.ravel(twodPower.powerMap) print ell print Cell s = splrep(ell, Cell, k=3) ll = numpy.ravel(modLMap) kk = splev(ll, s) id = numpy.where(ll > ell.max()) kk[id] = 0. # add a cosine ^2 falloff at the very end # id2 = numpy.where( (ll> (ell.max()-500)) & (ll<ell.max())) # lEnd = ll[id2] # kk[id2] *= numpy.cos((lEnd-lEnd.min())/(lEnd.max() -lEnd.min())*numpy.pi/2) # pylab.loglog(ll,kk) area = Nx * Ny * self.pixScaleX * self.pixScaleY p = numpy.reshape(kk, [Ny, Nx]) / area * (Nx * Ny)**2 else: area = Nx * Ny * self.pixScaleX * self.pixScaleY p = twodPower.powerMap / area * (Nx * Ny)**2 realPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) imgPart = numpy.sqrt(p) * numpy.random.randn(Ny, Nx) kMap = realPart + 1j * imgPart data = numpy.real(numpy.fft.ifft2(kMap)) 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]