Esempio n. 1
0
    def generatePower(self):

        #empty place-holder for final power spectrum
        p2d = fftTools.powerFromLiteMap(self.map)
        p2d.powerMap[:] = 0.

        #Best guess power
        tempMap = self.map.copy()
        tempMap2 = self.map2.copy()
        tempMap.data[:, :] *= self.mask.data[:, :]
        tempMap2.data[:, :] *= self.mask.data[:, :]
        p2dBest = fftTools.powerFromLiteMap(tempMap,
                                            tempMap2,
                                            applySlepianTaper=True)  #00 taper

        del tempMap, tempMap2

        #place-holder for total weights
        totWeight = numpy.zeros([self.map.Ny, self.map.Nx])

        num_iter = self.Niter
        if self.Niter == 0:
            num_iter = 1

        for k in xrange(num_iter):
            trace.issue('mtm', 2, 'Iteration ...%02d' % k)
            weights = self._getWeights(p2dBest)
            p2d.powerMap[:] = 0.
            totWeight[:] = 0.
            for i in xrange(self.Ntap):
                for j in xrange(self.Ntap):

                    tempMap = self.map.copy()
                    tempMap2 = self.map2.copy()
                    tempMap.data[:, :] *= self.tapers[:, :, i,
                                                      j] * self.mask.data[:, :]
                    tempMap2.data[:, :] *= self.tapers[:, :, i,
                                                       j] * self.mask.data[:, :]
                    p2dRunning = fftTools.powerFromLiteMap(tempMap, tempMap2)
                    p2d.powerMap[:, :] += self.eigs[
                        i, j] * weights[:, :, i,
                                        j]**2 * p2dRunning.powerMap[:, :]
                    totWeight[:, :] += self.eigs[i, j] * weights[:, :, i, j]**2
                    del tempMap
                    del tempMap2
                    del p2dRunning

            p2d.powerMap[:] /= totWeight[:]
            p2dBest.powerMap[:] = p2d.powerMap[:]  #new best guess

        return p2d
Esempio n. 2
0
    def __call__(self, data, pixScaleX=string_scaleX, pixScaleY=string_scaleY,
                 bin_file='/home/verag/flipper-master/params/BIN_100_LOG'):
        """
           pixScaleX, pixScaleY are in rad/pixel.

        """

        m = liteMap.liteMap()
        m.data = data
        m.Nx, m.Ny = data.shape
        m.pixScaleX = pixScaleX
        m.pixScaleY = pixScaleY
        map_fov_degX = np.rad2deg(pixScaleX) * m.Nx
        map_fov_degY = np.rad2deg(pixScaleY) * m.Ny
        m.area = map_fov_degX * map_fov_degY
        m.x0 = 0
        m.y0 = 0
        m.x1 = map_fov_degX
        m.y1 = map_fov_degY
        
        p2d = fftTools.powerFromLiteMap(m)
        ll, ll, lbin, cl, ll, ll = p2d.binInAnnuli(bin_file)        

        #note: cl is now res[1,:], lbin is res[0,:]
        res = np.array([lbin,cl])
        return res 
Esempio n. 3
0
    def generatePower(self):

        # empty place-holder for final power spectrum
        p2d = fftTools.powerFromLiteMap(self.map)
        p2d.powerMap[:] = 0.0

        # Best guess power
        tempMap = self.map.copy()
        tempMap2 = self.map2.copy()
        tempMap.data[:, :] *= self.mask.data[:, :]
        tempMap2.data[:, :] *= self.mask.data[:, :]
        p2dBest = fftTools.powerFromLiteMap(tempMap, tempMap2, applySlepianTaper=True)  # 00 taper

        del tempMap, tempMap2

        # place-holder for total weights
        totWeight = numpy.zeros([self.map.Ny, self.map.Nx])

        num_iter = self.Niter
        if self.Niter == 0:
            num_iter = 1

        for k in xrange(num_iter):
            trace.issue("mtm", 2, "Iteration ...%02d" % k)
            weights = self._getWeights(p2dBest)
            p2d.powerMap[:] = 0.0
            totWeight[:] = 0.0
            for i in xrange(self.Ntap):
                for j in xrange(self.Ntap):

                    tempMap = self.map.copy()
                    tempMap2 = self.map2.copy()
                    tempMap.data[:, :] *= self.tapers[:, :, i, j] * self.mask.data[:, :]
                    tempMap2.data[:, :] *= self.tapers[:, :, i, j] * self.mask.data[:, :]
                    p2dRunning = fftTools.powerFromLiteMap(tempMap, tempMap2)
                    p2d.powerMap[:, :] += self.eigs[i, j] * weights[:, :, i, j] ** 2 * p2dRunning.powerMap[:, :]
                    totWeight[:, :] += self.eigs[i, j] * weights[:, :, i, j] ** 2
                    del tempMap
                    del tempMap2
                    del p2dRunning

            p2d.powerMap[:] /= totWeight[:]
            p2dBest.powerMap[:] = p2d.powerMap[:]  # new best guess

        return p2d
Esempio n. 4
0
def getBinnedPower(templateMap,binFile,taperMap):
    p2d = ft.powerFromLiteMap(templateMap,applySlepianTaper=False)


    # pl = Plotter()
    # pl.plot2d(np.log(fftshift(p2d.powerMap)))
    # pl.done("power.png")

    lower, upper, center, bin_means, bin_stds, bincount = bin2d(p2d, binfile = binFile)
    w2 = np.mean(taperMap.data**2)
    return lower, upper, center, bin_means/w2
Esempio n. 5
0
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
        pl.done("inkappa.png")



    px = np.abs(lmap1.x1-lmap1.x0)/lmap1.Nx*np.pi/180.\
         *np.cos(np.pi/180.*0.5*(lmap1.y0+lmap1.y1))*180./np.pi*60.
    print px
    lmap1.pixScaleX = px / 180. * np.pi / 60.

    lmap1.data = lmap1.data * window
    w4 = np.mean(window**4.)

    print "crossing with input"
    import fftTools as ft
    import orphics.tools.stats as stats
    p2d = ft.powerFromLiteMap(templateMap, lmap1, applySlepianTaper=False)
    bin_edges = np.arange(100, 2000, 50)
    centers, means = stats.binInAnnuli(p2d.powerMap, p2d.modLMap, bin_edges)

    avg += means

    pl = Plotter()
    pl.add(centers, centers * avg / k / w4)
    pl.add(ellkk, ellkk * Clkk, lw=2)
    pl._ax.set_xlim(100, 2000)
    pl.done("crosspower.png")

    p2d = ft.powerFromLiteMap(lmap1, applySlepianTaper=False)
    centers, means = stats.binInAnnuli(p2d.powerMap, p2d.modLMap, bin_edges)

    avg2 += means
Esempio n. 7
0
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