def simplexiell(self): """ Computes multipoles in the simplest way -- Direct summation over the bins times Legendre polynomials. No interpolation or correction for the fact that xi_{2,4,...} may not be 0 for xi=0. """ myxiell = np.zeros([3, self.ns], dtype=float) mysvec = np.zeros([3, self.ns], dtype=float) for i in range(self.ns): i1 = i * self.nmu i2 = (i + 1) * self.nmu mymu = self.mu[i1:i2] ## sanity check that you picked out a correct and full range of mu. assert (mymu[1:] - mymu[:-1] > 0.).all() dmu = (mymu[1:] - mymu[:-1]).mean() assert np.fabs(mymu[0] - dmu * 0.5) < 2.0e-4 assert np.fabs(mymu[-1] + dmu * 0.5 - 1.) < 2.0e-4 for ell in [0, 2, 4]: myxiell[ell / 2, i] = (self.xi[i1:i2] * ximisc.legendre( ell, mymu)).sum() * (2. * ell + 1.) / float(self.nmu) mysvec[ell / 2, i] = self.s1d[i] ## tmp! #print '%e %e\n' % (mysvec[ell/2,i], myxiell[ell/2,i]) ## create a xiell object. return xiell.xiell(ellmax=4, sxilist=[mysvec, myxiell])
def simplexiell(self): """ Computes multipoles in the simplest way -- Direct summation over the bins times Legendre polynomials. No interpolation or correction for the fact that xi_{2,4,...} may not be 0 for xi=0. """ myxiell = np.zeros([3,self.ns],dtype=float) mysvec = np.zeros([3,self.ns],dtype=float) for i in range(self.ns): i1 = i*self.nmu i2 = (i+1)*self.nmu mymu = self.mu[i1:i2] ## sanity check that you picked out a correct and full range of mu. assert (mymu[1:] - mymu[:-1] > 0.).all() dmu = (mymu[1:] - mymu[:-1]).mean() assert np.fabs(mymu[0]-dmu*0.5) < 2.0e-4 assert np.fabs(mymu[-1]+dmu*0.5-1.) < 2.0e-4 for ell in [0,2,4]: myxiell[ell/2,i] = (self.xi[i1:i2]*ximisc.legendre(ell,mymu)).sum()*(2.*ell+1.)/float(self.nmu) mysvec[ell/2,i] = self.s1d[i] ## tmp! #print '%e %e\n' % (mysvec[ell/2,i], myxiell[ell/2,i]) ## create a xiell object. return xiell.xiell(ellmax=4,sxilist=[mysvec,myxiell])
def xiellfromDR(fbase,nell=3,binfile=None,rperpcut=-1., dfacs=1,dfacmu=1,icovfname=None,smincut=-1.,smaxcut=1.e12,\ DRfacinfo=None,smallRRcut=-1.,periodicopt=0,printmaskopt=0,xiopt=0): """ This function should supercede older codes with names like rebinxismugeneric*py input the base for the DR files, as well as optional bin files and small scale cutoff for masking (rperpcut). If you don't want to use a binfile to specify the downsampling, use dfacs and dfacmu inputs. smincut/smaxcut will only be used if binfile is None. DRfacinfo is used to pass [DRfac, fixRR] (dont get it from file headers in this case) 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. smallRRcut allows you to replace randoms(mu) with the average over mu where the total number of randoms in the bin is smaller than smallRRcut, just to keep Poisson noise down. If periodicopt == 1, assume the input is file containing [rg, mug, Vg, Ng] as currently output by correlationfxnMASTERv4. xiopt introduced on May 13 2014 to input xi(s,mu) and simply rebin (for comparison with Hong). """ if periodicopt == 1: # periodic box. rg, mug, Vg, Ng = np.loadtxt(fbase,unpack=True) Vzerochk = Vg.min()*0.1 fDR = fbase ## just for the return xiell purposes. elif xiopt == 1: rg, mug, xig = np.loadtxt(fbase,unpack=True,usecols=[0,1,2]) ## Hong format. fDR = fbase if binfile is None: ikeep = np.where((rg >= smincut) & (rg <= smaxcut))[0] rg = rg[ikeep] mug = mug[ikeep] xig = xig[ikeep] else: # DD,RR counts. fDD = fbase+'.DRopt1' fDR = fbase+'.DRopt2' fRR = fbase+'.DRopt3' rg, mug, DDg = np.loadtxt(fDD,skiprows=3,unpack=True) rg, mug, DRg = np.loadtxt(fDR,skiprows=3,unpack=True) rg, mug, RRg = np.loadtxt(fRR,skiprows=3,unpack=True) if binfile is None: ikeep = np.where((rg >= smincut) & (rg <= smaxcut))[0] rg = rg[ikeep] mug = mug[ikeep] DDg = DDg[ikeep] DRg = DRg[ikeep] RRg = RRg[ikeep] if DRfacinfo is None: DRfac, fixRR = ximisc.getDRfactors(fbase) else: DRfac = DRfacinfo[0] fixRR = DRfacinfo[1] nmu = len(np.where(rg == rg[0])[0]) dmu = 1./float(nmu) nr = len(np.where(mug == mug[0])[0]) if nmu*nr != len(rg): return None ## is s (called r here) log or linear binning? s1d = rg.reshape([nr,nmu])[:,0] mu1d = rg.reshape([nr,nmu])[0,:] ## compute linear spacings. dmutmp = (mu1d[1:]-mu1d[:-1]).mean() ds = (s1d[1:]-s1d[:-1]).mean() dlogs = (np.log(s1d[1:]/s1d[:-1])).mean() if (np.fabs(mu1d[1:]-mu1d[:-1] - dmutmp) > 0.0001*dmutmp).any(): ## not linear binning! ## this shouldn't be the case for a s,mu grid. assert 0==1 assert np.fabs(dmutmp - dmu) < 1.0e-4 if (np.fabs(s1d[1:]-s1d[:-1] - ds) < 0.0001*ds).all(): logsopt = 0 rglowedge = rg.copy() rglowedge = rglowedge-0.5*ds ## added specifically for xiopt rghighedge = rg.copy() rghighedge = rghighedge+0.5*ds if (np.fabs(np.log(s1d[1:])-np.log(s1d[:-1]) - dlogs) < 0.0001*dlogs).all(): logsopt = 1 rglowedge = rg.copy() rglowedge = rglowedge*np.exp(-0.5*dlogs) ## added specifically for xiopt rghighedge = rg.copy() rghighedge = rghighedge*np.exp(0.5*dlogs) assert logsopt == 0 or logsopt == 1 if xiopt == 1: Vg = rghighedge**3 - rglowedge**3 ## just need something proportional to volume. Vzerochk = Vg.min()*0.1 ## cut at edge of bin. xx = np.where(rglowedge*(1-(mug+0.5*dmu)**2)**0.5 < rperpcut)[0] if(rperpcut < 0.): assert len(xx) == 0 if len(xx) > 0: if periodicopt == 1: Vg[xx] = 0. Ng[xx] = 0. elif xiopt == 1: Vg[xx] = 0. xig[xx] = 0. else: DDg[xx] = 0. DRg[xx] = 0. RRg[xx] = 0. mymask = np.zeros(len(rg),dtype='int') mymask[xx] = 1 ## tmp! print a mask file. if(printmaskopt == 1): print 'yoyoyo opening masktmp.dat' ofpmask = open('masktmp.dat','w') for i in range(len(mymask)): ofpmask.write('%d\n' % (mymask[i])) ofpmask.close() if(printmaskopt == 2): #### nevermind, let's print below the boundaries of each s,mu bin. # print 'yoyoyo opening masktmp2.dat' # ofpmask = open('masktmp2.dat','w') mumaxlist = np.zeros(len(s1d)) + 1. for qi in range(len(s1d)): rgval = s1d[qi] qq = np.where((rg == rgval) & (mymask == 1))[0] ww = np.where((rg == rgval))[0] assert len(ww) > 0 if(len(qq) == 0): pass # ofpmask.write('%e %e %e\n' % (rgval,rglowedge[ww[0]],1.0)) else: # ofpmask.write('%e %e %e\n' % (rgval,rglowedge[ww[0]],mug[qq].max())) mumaxlist[qi] = mug[qq].min()-0.5*dmu ### testing. # print s1d[qi], mumaxlist[qi] #ofpmask.write('%d\n' % (mymask[i])) # ofpmask.close() if binfile is not None: ## specifies how many rbins to join together for first bin, next bin, etc. rjoin, mujoin = np.loadtxt(binfile,unpack=True,dtype='int') nrcut = len(rjoin) ### use later. #bintag = binfile.split('.')[0] else: # dfacs = dfacsin # dfacmu = dfacmuin nrcut = len(s1d)/dfacs dmudown = dfacmu*dmu if printmaskopt == 2: ofpmask = open('masktmp2.dat','w') ## check mask if I'm going to cut more. ristart = 0 badlist = np.zeros(nrcut,dtype='int') for i in range(nrcut): if binfile is not None: dfacs = rjoin[i] if (mymask[ristart*nmu:(ristart+dfacs)*nmu] == 1).all(): badlist[i] = 1 ristart = ristart + dfacs nrcutkeep = nrcut-len(np.where(badlist == 1)[0]) xi = np.zeros([nell,nrcutkeep],dtype=float) svec = np.zeros([nell,nrcutkeep],dtype=float) rcen = np.zeros(nrcut,dtype=float) ristart = 0 xiindx = 0 for i in range(nrcut): if binfile is not None: dfacs = rjoin[i] dfacmu = mujoin[i] dmudown = dfacmu*dmu riend = ristart + dfacs - 1 if logsopt == 0: rcen[i] = 0.5*(s1d[ristart] + s1d[riend]) if logsopt == 1: rcen[i] = np.exp(0.5*np.log(s1d[ristart]*s1d[riend])) for ishort in range(dfacs): i1 = (ristart+ishort)*nmu i2 = (ristart+ishort+1)*nmu if periodicopt == 1: if(ishort == 0): mymu = ximisc.downsample1d(mug[i1:i2],dfacmu) myVg = ximisc.downsample1dsum(Vg[i1:i2],dfacmu) myNg = ximisc.downsample1dsum(Ng[i1:i2],dfacmu) else: myVg = myVg + ximisc.downsample1dsum(Vg[i1:i2],dfacmu) myNg = myNg + ximisc.downsample1dsum(Ng[i1:i2],dfacmu) elif xiopt == 1: if(ishort == 0): mymu = ximisc.downsample1d(mug[i1:i2],dfacmu) myxig = ximisc.downsample1dvweight(xig[i1:i2],Vg[i1:i2],dfacmu) #perform volume weighted sum. myVg = ximisc.downsample1dsum(Vg[i1:i2],dfacmu) else: myxig = myxig + ximisc.downsample1dvweight(xig[i1:i2],Vg[i1:i2],dfacmu) # perform volume weighted sum, divide out at the end. myVg = myVg + ximisc.downsample1dsum(Vg[i1:i2],dfacmu) else: if(ishort == 0): mymu = ximisc.downsample1d(mug[i1:i2],dfacmu) mydd = ximisc.downsample1dsum(DDg[i1:i2],dfacmu) mydr = ximisc.downsample1dsum(DRg[i1:i2],dfacmu) myrr = ximisc.downsample1dsum(RRg[i1:i2],dfacmu) else: mydd = mydd + ximisc.downsample1dsum(DDg[i1:i2],dfacmu) mydr = mydr + ximisc.downsample1dsum(DRg[i1:i2],dfacmu) myrr = myrr + ximisc.downsample1dsum(RRg[i1:i2],dfacmu) if periodicopt == 1: yy = np.where(myVg < Vzerochk)[0] xitmp = myNg/myVg-1. xitmp[yy] = 0. elif xiopt == 1: yy = np.where(myVg < Vzerochk)[0] xitmp = myxig/myVg xitmp[yy] = 0. ## for now, nothing to 0 out (?) else: ##DR stuff zz = np.where(myrr < smallRRcut)[0] if(len(zz) > 0): print 'using smallRRcut! here are details',i,rcen[i],smallRRcut myrr = myrr.mean() yy = np.where(myrr < 0.01)[0] xitmp = (mydd-mydr*DRfac)/myrr/DRfac**2/fixRR**2+1. xitmp[yy] = 0. ## fix 0'd out regions if(len(yy) > 0 and 0==1): print 'ack why zerod out regions?' print len(yy) print i, dfacs, dfacmu print myrr print mymu for ell in np.arange(0,nell*2,2): if(badlist[i] == 0): xi[ell/2,xiindx] = (xitmp*ximisc.legendre(ell,mymu)).sum()*dmudown*(2.*ell+1.) svec[ell/2,xiindx] = rcen[i] if printmaskopt == 2: ofpmask.write('%e %e %e %e %e ' % (rcen[i], s1d[ristart]*np.exp(-0.5*dlogs), s1d[riend]*np.exp(0.5*dlogs), mumaxlist[ristart],mumaxlist[riend])) for ell in np.arange(0,nell*2,2): if badlist[i] == 0: ofpmask.write('%e ' % (xi[ell/2,xiindx])) else: ofpmask.write('%e ' % (0.0)) ofpmask.write('\n') ristart = ristart + dfacs if(badlist[i] == 0): xiindx += 1 return xiell(xiellfname=fDR,sxilist=[svec, xi],icovfname=icovfname)
def xiellfromDR(fbase,nell=3,binfile=None,rperpcut=-1., dfacs=1,dfacmu=1,icovfname=None,smincut=-1.,smaxcut=1.e12,\ DRfacinfo=None,smallRRcut=-1.,periodicopt=0,printmaskopt=0,xiopt=0): """ This function should supercede older codes with names like rebinxismugeneric*py input the base for the DR files, as well as optional bin files and small scale cutoff for masking (rperpcut). If you don't want to use a binfile to specify the downsampling, use dfacs and dfacmu inputs. smincut/smaxcut will only be used if binfile is None. DRfacinfo is used to pass [DRfac, fixRR] (dont get it from file headers in this case) 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. smallRRcut allows you to replace randoms(mu) with the average over mu where the total number of randoms in the bin is smaller than smallRRcut, just to keep Poisson noise down. If periodicopt == 1, assume the input is file containing [rg, mug, Vg, Ng] as currently output by correlationfxnMASTERv4. xiopt introduced on May 13 2014 to input xi(s,mu) and simply rebin (for comparison with Hong). """ if periodicopt == 1: # periodic box. rg, mug, Vg, Ng = np.loadtxt(fbase, unpack=True) Vzerochk = Vg.min() * 0.1 fDR = fbase ## just for the return xiell purposes. elif xiopt == 1: rg, mug, xig = np.loadtxt(fbase, unpack=True, usecols=[0, 1, 2]) ## Hong format. fDR = fbase if binfile is None: ikeep = np.where((rg >= smincut) & (rg <= smaxcut))[0] rg = rg[ikeep] mug = mug[ikeep] xig = xig[ikeep] else: # DD,RR counts. fDD = fbase + '.DRopt1' fDR = fbase + '.DRopt2' fRR = fbase + '.DRopt3' rg, mug, DDg = np.loadtxt(fDD, skiprows=3, unpack=True) rg, mug, DRg = np.loadtxt(fDR, skiprows=3, unpack=True) rg, mug, RRg = np.loadtxt(fRR, skiprows=3, unpack=True) if binfile is None: ikeep = np.where((rg >= smincut) & (rg <= smaxcut))[0] rg = rg[ikeep] mug = mug[ikeep] DDg = DDg[ikeep] DRg = DRg[ikeep] RRg = RRg[ikeep] if DRfacinfo is None: DRfac, fixRR = ximisc.getDRfactors(fbase) else: DRfac = DRfacinfo[0] fixRR = DRfacinfo[1] nmu = len(np.where(rg == rg[0])[0]) dmu = 1. / float(nmu) nr = len(np.where(mug == mug[0])[0]) if nmu * nr != len(rg): return None ## is s (called r here) log or linear binning? s1d = rg.reshape([nr, nmu])[:, 0] mu1d = rg.reshape([nr, nmu])[0, :] ## compute linear spacings. dmutmp = (mu1d[1:] - mu1d[:-1]).mean() ds = (s1d[1:] - s1d[:-1]).mean() dlogs = (np.log(s1d[1:] / s1d[:-1])).mean() if (np.fabs(mu1d[1:] - mu1d[:-1] - dmutmp) > 0.0001 * dmutmp).any(): ## not linear binning! ## this shouldn't be the case for a s,mu grid. assert 0 == 1 assert np.fabs(dmutmp - dmu) < 1.0e-4 if (np.fabs(s1d[1:] - s1d[:-1] - ds) < 0.0001 * ds).all(): logsopt = 0 rglowedge = rg.copy() rglowedge = rglowedge - 0.5 * ds ## added specifically for xiopt rghighedge = rg.copy() rghighedge = rghighedge + 0.5 * ds if (np.fabs(np.log(s1d[1:]) - np.log(s1d[:-1]) - dlogs) < 0.0001 * dlogs).all(): logsopt = 1 rglowedge = rg.copy() rglowedge = rglowedge * np.exp(-0.5 * dlogs) ## added specifically for xiopt rghighedge = rg.copy() rghighedge = rghighedge * np.exp(0.5 * dlogs) assert logsopt == 0 or logsopt == 1 if xiopt == 1: Vg = rghighedge**3 - rglowedge**3 ## just need something proportional to volume. Vzerochk = Vg.min() * 0.1 ## cut at edge of bin. xx = np.where(rglowedge * (1 - (mug + 0.5 * dmu)**2)**0.5 < rperpcut)[0] if (rperpcut < 0.): assert len(xx) == 0 if len(xx) > 0: if periodicopt == 1: Vg[xx] = 0. Ng[xx] = 0. elif xiopt == 1: Vg[xx] = 0. xig[xx] = 0. else: DDg[xx] = 0. DRg[xx] = 0. RRg[xx] = 0. mymask = np.zeros(len(rg), dtype='int') mymask[xx] = 1 ## tmp! print a mask file. if (printmaskopt == 1): print 'yoyoyo opening masktmp.dat' ofpmask = open('masktmp.dat', 'w') for i in range(len(mymask)): ofpmask.write('%d\n' % (mymask[i])) ofpmask.close() if (printmaskopt == 2): #### nevermind, let's print below the boundaries of each s,mu bin. # print 'yoyoyo opening masktmp2.dat' # ofpmask = open('masktmp2.dat','w') mumaxlist = np.zeros(len(s1d)) + 1. for qi in range(len(s1d)): rgval = s1d[qi] qq = np.where((rg == rgval) & (mymask == 1))[0] ww = np.where((rg == rgval))[0] assert len(ww) > 0 if (len(qq) == 0): pass # ofpmask.write('%e %e %e\n' % (rgval,rglowedge[ww[0]],1.0)) else: # ofpmask.write('%e %e %e\n' % (rgval,rglowedge[ww[0]],mug[qq].max())) mumaxlist[qi] = mug[qq].min() - 0.5 * dmu ### testing. # print s1d[qi], mumaxlist[qi] #ofpmask.write('%d\n' % (mymask[i])) # ofpmask.close() if binfile is not None: ## specifies how many rbins to join together for first bin, next bin, etc. rjoin, mujoin = np.loadtxt(binfile, unpack=True, dtype='int') nrcut = len(rjoin) ### use later. #bintag = binfile.split('.')[0] else: # dfacs = dfacsin # dfacmu = dfacmuin nrcut = len(s1d) / dfacs dmudown = dfacmu * dmu if printmaskopt == 2: ofpmask = open('masktmp2.dat', 'w') ## check mask if I'm going to cut more. ristart = 0 badlist = np.zeros(nrcut, dtype='int') for i in range(nrcut): if binfile is not None: dfacs = rjoin[i] if (mymask[ristart * nmu:(ristart + dfacs) * nmu] == 1).all(): badlist[i] = 1 ristart = ristart + dfacs nrcutkeep = nrcut - len(np.where(badlist == 1)[0]) xi = np.zeros([nell, nrcutkeep], dtype=float) svec = np.zeros([nell, nrcutkeep], dtype=float) rcen = np.zeros(nrcut, dtype=float) ristart = 0 xiindx = 0 for i in range(nrcut): if binfile is not None: dfacs = rjoin[i] dfacmu = mujoin[i] dmudown = dfacmu * dmu riend = ristart + dfacs - 1 if logsopt == 0: rcen[i] = 0.5 * (s1d[ristart] + s1d[riend]) if logsopt == 1: rcen[i] = np.exp(0.5 * np.log(s1d[ristart] * s1d[riend])) for ishort in range(dfacs): i1 = (ristart + ishort) * nmu i2 = (ristart + ishort + 1) * nmu if periodicopt == 1: if (ishort == 0): mymu = ximisc.downsample1d(mug[i1:i2], dfacmu) myVg = ximisc.downsample1dsum(Vg[i1:i2], dfacmu) myNg = ximisc.downsample1dsum(Ng[i1:i2], dfacmu) else: myVg = myVg + ximisc.downsample1dsum(Vg[i1:i2], dfacmu) myNg = myNg + ximisc.downsample1dsum(Ng[i1:i2], dfacmu) elif xiopt == 1: if (ishort == 0): mymu = ximisc.downsample1d(mug[i1:i2], dfacmu) myxig = ximisc.downsample1dvweight( xig[i1:i2], Vg[i1:i2], dfacmu) #perform volume weighted sum. myVg = ximisc.downsample1dsum(Vg[i1:i2], dfacmu) else: myxig = myxig + ximisc.downsample1dvweight( xig[i1:i2], Vg[i1:i2], dfacmu ) # perform volume weighted sum, divide out at the end. myVg = myVg + ximisc.downsample1dsum(Vg[i1:i2], dfacmu) else: if (ishort == 0): mymu = ximisc.downsample1d(mug[i1:i2], dfacmu) mydd = ximisc.downsample1dsum(DDg[i1:i2], dfacmu) mydr = ximisc.downsample1dsum(DRg[i1:i2], dfacmu) myrr = ximisc.downsample1dsum(RRg[i1:i2], dfacmu) else: mydd = mydd + ximisc.downsample1dsum(DDg[i1:i2], dfacmu) mydr = mydr + ximisc.downsample1dsum(DRg[i1:i2], dfacmu) myrr = myrr + ximisc.downsample1dsum(RRg[i1:i2], dfacmu) if periodicopt == 1: yy = np.where(myVg < Vzerochk)[0] xitmp = myNg / myVg - 1. xitmp[yy] = 0. elif xiopt == 1: yy = np.where(myVg < Vzerochk)[0] xitmp = myxig / myVg xitmp[yy] = 0. ## for now, nothing to 0 out (?) else: ##DR stuff zz = np.where(myrr < smallRRcut)[0] if (len(zz) > 0): print 'using smallRRcut! here are details', i, rcen[ i], smallRRcut myrr = myrr.mean() yy = np.where(myrr < 0.01)[0] xitmp = (mydd - mydr * DRfac) / myrr / DRfac**2 / fixRR**2 + 1. xitmp[yy] = 0. ## fix 0'd out regions if (len(yy) > 0 and 0 == 1): print 'ack why zerod out regions?' print len(yy) print i, dfacs, dfacmu print myrr print mymu for ell in np.arange(0, nell * 2, 2): if (badlist[i] == 0): xi[ell / 2, xiindx] = (xitmp * ximisc.legendre( ell, mymu)).sum() * dmudown * (2. * ell + 1.) svec[ell / 2, xiindx] = rcen[i] if printmaskopt == 2: ofpmask.write( '%e %e %e %e %e ' % (rcen[i], s1d[ristart] * np.exp(-0.5 * dlogs), s1d[riend] * np.exp(0.5 * dlogs), mumaxlist[ristart], mumaxlist[riend])) for ell in np.arange(0, nell * 2, 2): if badlist[i] == 0: ofpmask.write('%e ' % (xi[ell / 2, xiindx])) else: ofpmask.write('%e ' % (0.0)) ofpmask.write('\n') ristart = ristart + dfacs if (badlist[i] == 0): xiindx += 1 return xiell(xiellfname=fDR, sxilist=[svec, xi], icovfname=icovfname)