Example #1
0
def stack_on_map(lite_map,
                 width_stamp_arcminute,
                 pix_scale,
                 ra_range,
                 dec_range,
                 catalog=None,
                 n_random_points=None):
    width_stamp_degrees = width_stamp_arcminute / 60.
    Np = np.int(width_stamp_arcminute / pix_scale + 0.5)
    pad = np.int(Np / 2 + 0.5)
    print("Expected width in pixels = ", Np)

    lmap = lite_map
    stack = 0
    N = 0

    if catalog is not None:
        looprange = range(0, len(catalog))
        assert n_random_points is None
        random = False
    else:
        assert n_random_points is not None
        assert len(ra_range) == 2
        assert len(dec_range) == 2
        looprange = range(0, n_random_points)
        random = True

    for i in looprange:
        if random:
            ra = np.random.uniform(*ra_range)
            dec = np.random.uniform(*dec_range)
        else:
            ra = catalog[i][1]
            dec = catalog[i][2]
        ix, iy = lmap.skyToPix(ra, dec)
        if ix >= pad and ix < lmap.Nx - pad and iy >= pad and iy < lmap.Ny - pad:
            print(i)
            #print(ra,dec)
            smap = lmap.selectSubMap(ra - width_stamp_degrees / 2.,
                                     ra + width_stamp_degrees / 2.,
                                     dec - width_stamp_degrees / 2.,
                                     dec + width_stamp_degrees / 2.)
            #print (smap.data.shape)
            #cutout = zoom(smap.data.copy(),zoom=(float(Np)/smap.data.shape[0],float(Np)/smap.data.shape[1]))
            cutout = resize(smap.data.copy(), output_shape=(Np, Np))
            #print (cutout.shape)
            stack = stack + cutout
            xMap, yMap, modRMap, xx, yy = fmaps.getRealAttributes(smap)
            N = N + 1.
        else:
            print("skip")
    stack = stack / N
    #print(stack.shape())
    #print(smap.data.shape)
    print(stack)
    print(N)
    io.quickPlot2d(stack, out_dir + "stackrandom.png")

    dt = pix_scale
    arcmax = 20.
    thetaRange = np.arange(0., arcmax, dt)
    breal = bin2D(modRMap * 180. * 60. / np.pi, thetaRange)
    cents, recons = breal.bin(stack)
    pl = Plotter(labelX='Distance from Center (arcminutes)',
                 labelY='Temperature Fluctuation ($\mu K$)',
                 ftsize=10)
    pl.add(cents, recons)
    pl._ax.axhline(y=0., ls="--", alpha=0.5)
    pl.done(out_dir + "randomprofiles.png")
    return stack, cents, recons
Example #2
0
    fieldCoords = (raLeft, decLeft, raRight, decRight)
    smap = lm.makeEmptyCEATemplateAdvanced(*fieldCoords,
                                           pixScaleXarcmin=pixScale,
                                           pixScaleYarcmin=pixScale)
    smap.loadDataFromHealpixMap(hpPlanck, hpCoords="GALACTIC")

    stamp = smap.data.copy() / tfact
    stamp = zoom(stamp,
                 zoom=(float(Np) / stamp.shape[0], float(Np) / stamp.shape[1]))
    #print ra, dec, stamp.shape

    if i == 1:

        lxMap, lyMap, modLMap, angMap, lx, ly = fmaps.getFTAttributesFromLiteMap(
            smap)
        xMap, yMap, modRMap, xx, yy = fmaps.getRealAttributes(smap)
        polCombList = ["TT"]

        Ny, Nx = stamp.shape
        win = fmaps.initializeCosineWindowData(Ny, Nx, lenApod=40, pad=5)
        #win = 1.
        w2 = np.mean(win**2.)

        fwhm = 5.0
        tht_fwhm = np.deg2rad(fwhm / 60.)
        l = np.arange(0., 10000.)
        beamells = np.exp(-(tht_fwhm**2.) * (l**2.) / (8. * np.log(2.)))
        beamTemplate = fmaps.makeTemplate(l, beamells, modLMap)
        #print beamTemplate.shape

        noiseT = 42.0
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(),
                             beamArcmin=1.4,
                             noiseMukArcmin=12.0,
                             TCMB=2.7255e6)
twoDPower = inp.spec1dTo2d(fineTemplate, ell, Cl)  #,k=1) # !!!!!
fineTemplate.data = twoDPower
Example #4
0
def stack_on_map(lite_map,
                 width_stamp_arcminute,
                 pix_scale,
                 ra_range,
                 dec_range,
                 catalog=None,
                 n_random_points=None):
    width_stamp_degrees = width_stamp_arcminute / 60.
    Np = np.int(width_stamp_arcminute / pix_scale + 0.5)
    pad = np.int(Np / 2 + 0.5)
    print("Expected width in pixels = ", Np)
    lmap = lite_map
    stack = 0
    N = 0

    if catalog is not None:
        looprange = range(0, len(catalog))
        assert n_random_points is None
        random = False
    else:
        assert n_random_points is not None
        assert len(ra_range) == 2
        assert len(dec_range) == 2
        looprange = range(0, n_random_points)
        random = True
    print(looprange)
    for i in looprange:
        banana = True
        mass = catalog[i][10]
        if random:
            ra = np.random.uniform(*ra_range)
            dec = np.random.uniform(*dec_range)
        if random == False:
            ra = catalog[i][1]  #1 for ACT catalog 2 for SDSS
            dec = catalog[i][2]  #2 for ACT catalog 3 for SDSS
        for j in range(0, 2130):
            distance = np.sqrt((ra - RAps[j])**2 + (dec - DECps[j])**2)
            crit = 0.25
            if distance < crit:
                banana = False
                print('too close')
        ix, iy = lmap.skyToPix(ra, dec)
        if ix >= pad and ix < lmap.Nx - pad and iy >= pad and iy < lmap.Ny - pad and banana == True and mass > 8:
            print(i)
            #print(ra,dec)
            smap = lmap.selectSubMap(ra - width_stamp_degrees / 2.,
                                     ra + width_stamp_degrees / 2.,
                                     dec - width_stamp_degrees / 2.,
                                     dec + width_stamp_degrees / 2.)
            #print (smap.data.shape)
            #cutout = zoom(smap.data.copy(),zoom=(float(Np)/smap.data.shape[0],float(Np)/smap.data.shape[1])
            cutout = resize(smap.data.copy(),
                            output_shape=(Np, Np)) - randomstack
            xMap, yMap, modRMap, xx, yy = fmaps.getRealAttributes(smap)
            dt = pix_scale
            arcmax = 20.
            thetaRange = np.arange(0., arcmax, dt)
            breali = bin2D(modRMap * 180. * 60. / np.pi, thetaRange)
            a = breali.bin(cutout)[1]
            profiles.append(a)
            io.quickPlot2d(cutout, str(i) + "cutout.png")
            #print (cutout.shape)
            stack = stack + cutout
            N = N + 1
        else:
            print("skip")
    stack = stack / N  #-randomstack
    #print(stack.shape())
    #print(smap.data.shape)
    # print(stack)
    print(N)
    statistics = stats.getStats(profiles)
    mean = statistics['mean']
    error = statistics['errmean']
    corrcoef = statistics['corr']
    covmat = statistics['covmat']
    print(mean / error)
    np.save('statistics', statistics)
    #np.save('newrandomstamp',stack)
    # io.quickPlot2d(stack,out_dir+"newACTstack.png")
    dt = pix_scale
    arcmax = 20.
    thetaRange = np.arange(0., arcmax, dt)
    breal = bin2D(modRMap * 180. * 60. / np.pi, thetaRange)
    cents, recons = breal.bin(stack)
    pl = Plotter(labelX='Distance from Center (arcminutes)',
                 labelY='Temperature Fluctuation ($\mu K$)',
                 ftsize=10)
    pl.add(cents, mean)
    pl.addErr(cents, mean, yerr=error)
    pl._ax.axhline(y=0., ls="--", alpha=0.5)
    pl.done(out_dir + "error.png")
    print(covmat)
    io.quickPlot2d(covmat, 'covmat.png')
    return (stack, cents, recons)
Example #5
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
Example #6
0
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
Example #7
0
def stack_on_map(lite_map,
                 width_stamp_arcminute,
                 pix_scale,
                 ra_range,
                 dec_range,
                 catalog=None,
                 n_random_points=None):
    from skimage.transform import resize
    import orphics.tools.stats as stats

    width_stamp_degrees = width_stamp_arcminute / 60.
    Np = np.int(width_stamp_arcminute / pix_scale + 0.5)
    pad = np.int(Np / 2 + 0.5)
    print("Expected width in pixels = ", Np)

    lmap = lite_map
    stack = 0
    N = 0

    if catalog is not None:
        looprange = goodobjects
        print(looprange)
        assert n_random_points is None
        random = False
    else:
        assert n_random_points is not None
        assert len(ra_range) == 2
        assert len(dec_range) == 2
        looprange = range(0, n_random_points)
        random = True

    for i in looprange:
        if random:
            ra = np.random.uniform(*ra_range)
            dec = np.random.uniform(*dec_range)
        if random == False:
            ra = catalog[i][1]  #1 for ACT catalog 2 for SDSS and redmapper
            dec = catalog[i][2]  #2 for ACT catalog 3 for SDSS and redmapper
        ix, iy = lmap.skyToPix(ra, dec)
        if ix >= pad and ix < lmap.Nx - pad and iy >= pad and iy < lmap.Ny - pad:
            print(i)
            smap = lmap.selectSubMap(ra - width_stamp_degrees / 2.,
                                     ra + width_stamp_degrees / 2.,
                                     dec - width_stamp_degrees / 2.,
                                     dec + width_stamp_degrees / 2.)
            #cutout = zoom(smap.data.copy(),zoom=(float(Np)/smap.data.shape[0],float(Np)/smap.data.shape[1]))
            cutout = resize(smap.data.copy(), output_shape=(Np, Np))
            cutouts.append(cutout - randomstack)
            stack = stack + cutout
            xMap, yMap, modRMap, xx, yy = fmaps.getRealAttributes(smap)
            N = N + 1.
            ixs.append(ix)
            iys.append(iy)
            modRMaps.append(modRMap)
        else:
            print("skip")
    stack = stack / N - randomstack
    print(N)
    if catalog is not None:
        io.quickPlot2d(stack, out_dir + "stack.png")
    else:
        np.save('randomstamp', stack)

    dt = pix_scale
    arcmax = 20.
    thetaRange = np.arange(0., arcmax, dt)
    breal = stats.bin2D(modRMap * 180. * 60. / np.pi, thetaRange)
    cents, recons = breal.bin(stack)
    pl = Plotter(labelX='Distance from Center (arcminutes)',
                 labelY='Temperature Fluctuation ($\mu K$)',
                 ftsize=10)
    pl.add(cents, recons)
    pl._ax.axhline(y=0., ls="--", alpha=0.5)
    pl.done(out_dir + "profiles.png")
    return stack, cents, recons