Example #1
0
def wpcrossHogg(fbase,nbar2d,nbar3d,DRfacinfo=None,icovfname=None,wpstart=-1,wpend=-1):
  """
  nbar2d: the number density of targets ** per steradian ** !!
  nbar3d: the galaxy averaged number density within the redshift cuts you applied to the wp calculation.
  DRfacinfo is used to pass [DRfacS, DRfacI] and renormalize counts accordingly.  Needs to be tested!! 
  The exact definition is number of data divided by number of randoms over the whole footprint, for both spec and imaging samples.
  That feature is necessary for computing covariance matrices from bootstrap, where that factor
  should be fixed by total N and/or S, it does not vary from bootstrap region to region.
  """

  fDD = fbase+'.DRopt11'
  fDR = fbase+'.DRopt12'
  fRD = fbase+'.DRopt13'
  fRR = fbase+'.DRopt14'
  rpg, DDg = np.loadtxt(fDD,unpack=True)
  rpg, DRg = np.loadtxt(fDR,unpack=True)
  rpg, RDg = np.loadtxt(fRD,unpack=True)
  rpg, RRg = np.loadtxt(fRR,unpack=True)
  if DRfacinfo is None:
    normfac = ximisc.getDRnormswpcross(fbase)
    mywp = ((DDg/normfac[0,0])/(DRg/normfac[1,0]/normfac[1,1]*normfac[0,1])-(RDg/normfac[2,0])/(RRg/normfac[3,0]/normfac[3,1]*normfac[2,1]))*nbar2d/nbar3d
  else:
    #print 'Beth, really need to test this before deriving a cov matrix!'
    #sys.exit(1)
    ## we want to enforce the correct global ratio of spec randoms to data and imaging randoms to data.
    ## the ratio of imaging data to random shows up in the first term as normfac[0,1]/normfac[1,1] and second term as normfac[2,1]/normfac[3,1]
    ## the ratio of spec randoms to data shows up in how big normfac[1,0]/normfac[0,0] is compared to normfac[3,0]/normfac[2,0]
    normfac = ximisc.getDRnormswpcross(fbase)
    assert normfac[0,1]/normfac[1,1] == normfac[2,1]/normfac[3,1]
    ## make sure there's not an offset ndownDD or ndownRR factor between global and local factors.
    #print 'testing',DRfacinfo,normfac[0,0]/normfac[2,0],normfac[0,1]/normfac[1,1]
    #assert np.fabs(DRfacinfo[1] - normfac[0,1]/normfac[1,1])/DRfacinfo[1] < 0.1
    #assert np.fabs(DRfacinfo[0] - normfac[0,0]/normfac[2,0])/DRfacinfo[1] < 0.1
    DIfac = DRfacinfo[1]/(normfac[0,1]/normfac[1,1])  # multiplicative renormalization of imaging randoms to data.
    DSfac = DRfacinfo[0]/(normfac[0,0]/normfac[2,0])  # same.  Hope I did this correctly!  Let's see what hte variance looks like with and without this correction.

    mywp = ((DDg/normfac[0,0])/(DRg/normfac[1,0]/normfac[1,1]*normfac[0,1]*DIfac)-DSfac*(RDg/normfac[2,0])/(RRg/normfac[3,0]/normfac[3,1]*normfac[2,1]*DIfac))*nbar2d/nbar3d
  
  #print 'heyo',mywp
  return wp(wpfname=fDD,icovfname=icovfname,rpwplist=[rpg,mywp],wpstart=wpstart,wpend=wpend) 
Example #2
0
def getbootcov(bootfile, basedir, outdirbase = 'outputdr12', covoutfname=None, NSortot=2, nboot = 5000000, \
               rpimax=80.,wpstart=1,wpend=19,\
               nell=3,rperpcut=-1.,smallRRcut=-1.,\
               dfacs=1,dfacmu=1,icovfname=None,smincut=-1.,smaxcut=1.e12,\
               binfname_xiell='xibinfiles/bin1fineMU.txt',\
               nbar2d=[-1.,-1.],nbar3d=[-1.,-1],\
               whichtask=4):
    ## resurrect these later.
    #               splitxi0=5,splitxi2=6,splitwp=7):
    """
  Get covariance matrix.
  We're going to do all tasks at once by default (4).
  whichtask = 0: xiell
  whichtask = 1: wp (compute xi(rp,rpi))
  whichtask = 2: wtheta
  whichtask = 3: Hogg spec-im cross-correlation.
  whichtask = 4: combine xiell and wp in usual way.

  Third tier of stuff goes directly to xiellfromDR
  rpimax is for wp, default is 80.
  nbar2d,nbar3d needs to be computed separately for N and S.
  """

    nsub, nsubdir, pixelfname, fbase, fbasetotN, fbasetotS = parsebootinfo(
        bootfile=basedir + bootfile)

    NSlist = [0, 1]
    NStaglist = ['N', 'S']

    for xx in [nsub, nsubdir, pixelfname, fbase, fbasetotN, fbasetotS]:
        if xx is None:
            print 'bad bootfile!'
            return None

    b = boot.bootpix()
    b.readregions(basedir + pixelfname)
    assert b.nsub == nsub

    ## this list will be filled
    DRinfolist = [-1, -1, -1, -1]

    taglist = ['-xiell', '-xigrid', '-wtheta', '-wpcross']

    ## get global DR factors for taglist.
    for ii in range(len(taglist) - 1):
        tag = taglist[ii]
        tmp = np.zeros(
            [2, 2])  # first index is N or S.  DRfac, fixRR stored for each.

        for NS, NStag, ff in zip(NSlist, NStaglist, [fbasetotN, fbasetotS]):
            try:
                #if 0==0:
                tmp[NS, 0], tmp[NS, 1] = ximisc.getDRfactors(basedir + '/' +
                                                             outdirbase + tag +
                                                             '/' + ff)
            except:
                tmp[NS, :] = -1.
        DRinfolist[ii] = tmp.copy()

    ## now get DR info for wpcross.
    ### nevermind! we reduce this to two ratios.
    ## DRinfolist[3] = np.zeros([2,4,2])
    DRinfolist[3] = np.zeros([2, 2])
    tag = taglist[3]
    for NS, NStag, ff in zip(NSlist, NStaglist, [fbasetotN, fbasetotS]):
        try:
            normfac = ximisc.getDRnormswpcross(basedir + '/' + outdirbase +
                                               tag + '/' + ff)
            DRinfolist[3][NS][0] = normfac[0, 0] / normfac[2, 0]
            DRinfolist[3][NS][1] = normfac[0, 1] / normfac[1, 1]
        except:
            DRinfolist[3][NS][:] = -1.

    tasklist = np.zeros(4, dtype='int')
    if whichtask == 4:
        tasklist = np.array([1, 1, 0, 1], dtype='int')
    else:
        tasklist[whichtask] = 1

    if tasklist[3] > 0:
        assert (nbar2d[:] > 0).all()
        assert (nbar3d[:] > 0).all()
        assert (DRinfolist[3][:, :].flatten() > 0).all()

    for ns in range(nsub):
        xx = np.where(b.pixlist['PID'] == ns)[0]
        assert len(xx) == 1
        assert xx[0] == ns
        NorSval = b.pixlist['NorS'][xx[0]]
        for tt in range(len(tasklist)):
            if tasklist[tt] == 0: continue
            tag = taglist[tt]
            ff = basedir + '/' + outdirbase + tag + '/' + nsubdir + '/' + fbase + '.%04d.Np' % (
                ns)
            if tt == 0:  #xiell
                xitmp = xiell.xiellfromDR(ff,
                                          binfile=binfname_xiell,
                                          rperpcut=rperpcut,
                                          nell=nell,
                                          smallRRcut=smallRRcut,
                                          dfacs=dfacs,
                                          dfacmu=dfacmu,
                                          smincut=smincut,
                                          smaxcut=smaxcut,
                                          DRfacinfo=DRinfolist[tt][NorSval])
                dvec = xitmp.xilong
            if tt == 1:  #wp
                wptmp = wp.wpfromDR(ff,
                                    DRfacinfo=DRinfolist[tt][NorSval],
                                    rpimax=rpimax)
                dvec = wptmp.wp

            if tt == 2:  #wtheta
                wttmp = wtheta.wthetafromDR(ff,
                                            DRfacinfo=DRinfolist[tt][NorSval])
                dvec = wttmp.wtheta

            if tt == 3:  #wpcross
                wpcrosstmp = wp.wpcrossHogg(ff,
                                            DRfacinfo=DRinfolist[tt][NorSval],
                                            nbar2d=nbar2d[NorSval],
                                            nbar3d=nbar3d[NorSval])
                dvec = wpcrosstmp.wp

        if whichtask == 4:
            dvec = xiwpvec(xitmp, wptmp, wpcrosstmp, wpstart, wpend)

        if ns == 0:  ## allocate!
            ndata = len(dvec)
            dveclist = np.zeros([nsub, ndata], dtype='float128')
        dveclist[ns, :] = dvec[:]

    ## check means with total counts.
    nindx = np.where(b.pixlist['NorS'] == 0)[0]
    sindx = np.where(b.pixlist['NorS'] == 1)[0]
    nsindx = np.where((b.pixlist['NorS'] == 0) | (b.pixlist['NorS'] == 1))[0]
    print 'N/S: ', len(nindx), len(sindx), len(nsindx)
    assert len(nsindx) == nsub
    assert (nsindx == np.arange(0, nsub, 1, dtype='int')).all()
    myindx = nsindx
    ## assume we want nsindx for this, but can restore N/S option later if I want.

    dmean = (dveclist[myindx, :]).sum(axis=0) / float(len(myindx))
    ntot = len(myindx)
    ntotflt = float(ntot)

    print 'hi beth'
    print dmean

    Cmat = np.zeros([ndata, ndata], dtype='float128')
    for b in range(nboot):
        rr = np.random.random_integers(0, ntot - 1, ntot)
        dtrial = (dveclist[rr, :]).sum(axis=0) / ntotflt
        xvec = np.matrix([dtrial - dmean])
        Cmat += (xvec.T * xvec)

    Cmat = Cmat / float(nboot - 1)
    Cmat = np.matrix(Cmat, dtype='float64')
    iCmat = Cmat.I  ##
    print 'not assuming any bootstrap unbias factor for now!'
    if covoutfname is not None:
        printcov(Cmat, covoutfname)
        printcov(iCmat, covoutfname + '.inv')
        printmean(dmean, covoutfname + '.mean')
    return Cmat, iCmat, dmean