R2=R2, pmesh=True, abund=abund, doexp=doexp, mexp=mexp, cc=cc, stellar=stellar)[1] datap = pm.paint(hdictf['position'][:num], hdictf['mass'][:num]) datapR = ft.smooth(datap, Rsm, 'fingauss') predicts.append(predictR[...].copy()) datas.append(datapR[...].copy()) for i, sg in enumerate(sgs): hmass, hpos = dg.scatter_catalog(hdictf['mass'], hdictf['position'], sg, seed=i) datasg = pm.paint(hpos[:num], hmass[:num]) datasgR = ft.smooth(datasg, Rsm, 'fingauss') datasgs[i].append(datasgR[...].copy()) #####Setup #bins = np.linspace(-3, 3, 300) func = dg.normal colors = ['r', 'b', 'g', 'y', 'm', 'orange', 'brown', 'k'] if doexp: mms = [10**8, 10**8.5, 10**9, 10**9.2, 10**9.8, 10**10][::-1] elif stellar: mms = [1e6, 1e7, 1e8, 1e9, 1e10][::-1]
def gridhalos(pm, dpath=None, pos=None, mass=None, R1=None, R2=None, rank=None, abund=True, sigma=None, seed=123, pmesh=True, z=0, doexp=False, mexp=None, cc=None, stellar=False, verbose=True): '''Read halo file or pos/mas arrays and grid and smooth them after scattering or matching abundance or both The path should have folder FOF in it. ''' #Matter bs, nc = pm.BoxSize[0], pm.Nmesh[0] #Halos if dpath is not None: from nbodykit.lab import CurrentMPIComm CurrentMPIComm.set( pm.comm ) #WORKAROUND because BigFileCatalog ignores comm o/w. Should not need after nbodykit v0.3.4 fofcat = BigFileCatalog(dpath + 'FOF', header='Header', comm=pm.comm) halopos = fofcat['PeakPosition'].compute()[1:] halomass = (fofcat['Mass'].compute() * 10**10)[1:] try: hvel = fofcat['CMVelocity'].compute()[1:] except Exception as e: print(e) print('Cannot read velocity') hvel = None if verbose: print('BigFileCatalog read') elif pos is not None: halopos = pos if mass is not None: halomass = mass else: print( 'No halo masses given, so mass=1. Scatter and abundance not valid' ) halomass = np.ones(halopos.shape[0]) sigma = None abund = False else: print('Need either path of Bigfile, or catalogs') return None if abund: if verbose: print('Match abundance') halomass = mf.icdf_sampling(hmass=halomass, bs=bs, z=z) if stellar: halomass = mf.stellar_mass(halomass) if sigma is not None: print('Scatter catalog with sigma = %0.2f' % sigma) halomass, halopos = dg.scatter_catalog(halomass, halopos, sigma, seed) if doexp: if verbose: print('Doing doexp with mexp = %0.2f, cc=%0.2f' % (mexp, cc)) halomass = mf.fmexp(halomass, mexp, cc) #print('In gridhalos') #print(rank, halomass.size) #print(dpath) if rank is not None: if rank > halomass.size: print('\nCatalog not big enough to be able to match abundance') print('\nDesired rank = %d, catalog size = %d' % (rank, halomass.size)) return None else: halopos = halopos[:rank + 1] halomass = halomass[:rank + 1] hvel = hvel[:rank + 1] halos = {'position': halopos, 'mass': halomass, 'velocity': hvel} if pmesh: halomesh = pm.paint(halopos, mass=halomass) hposmesh = pm.paint(halopos) meshdict = {'hposmesh': hposmesh.value, 'halomesh': halomesh.value} dkh = halomesh.r2c() if R1 is not None: hmR1mesh = ft.smooth(dkh, R1, 'fingauss') meshdict['hmR1mesh'] = hmR1mesh.value if R2 is not None: hmR2mesh = ft.smooth(dkh, R2, 'fingauss') meshdict['hmR2mesh'] = hmR2mesh.value else: meshdict = None return meshdict, halos
predictR.value, M0=M0, binfit=bins, c='k', axin=ax, func=func, mbin=mbinsm, retfit=True, lsf='--')[0] fits = [] sgs = [0.2] for i, sg in enumerate(sgs): hmass, hpos = dg.scatter_catalog(hdictf['mass'], hdictf['position'], sigma=sg) datasg = pm.paint(hpos[:num], hmass[:num]) datasgR = ft.smooth(datasg, 3, 'fingauss') fitt = dg.plot_noise(datasgR.value, predictR.value, M0=M0, binfit=bins, c='k', axin=ax, func=func, mbin=mbinsm, retfit=True)[0] fits.append(fitt) for i in range(3):
predictR.value, M0=M0, binfit=bins, c='k', axin=ax, func=func, mbin=mbinsm, retfit=True, lsf='--')[0] fits = [] sgs = [0.1, 0.15, 0.20] for i, sg in enumerate(sgs): hmass, hpos, hvel = dg.scatter_catalog(hdictf['mass'], hdictf['position'], sg, hvel=hdictf['velocity']) if rsd: datasg = pm.paint( hpos[:num] + (np.array([0, 0, 1]).reshape(-1, 1) * hvel[:num][:, 2]).T, mass=hmass[:num]) else: datasg = pm.paint(hpos[:num], mass=hmass[:num]) datasgR = ft.smooth(datasg, Rsm, 'fingauss') fitt = dg.plot_noise(datasgR.value, predictR.value, M0=M0, binfit=bins, c=colors[i], axin=ax,
# bins = np.zeros((mbinsm.size, 1000 )) # for i in range(0, bins.shape[0]): # bins[i] = np.linspace(-3*mbinsm[i]/M0, 2*mbinsm[i]/M0, bins.shape[1]) # print('mbinsm -- ', mbinsm) #### fig, ax = plt.subplots(3, 3, figsize = (14, 12)) fit0 = dg.plot_noise(datapR.value, predictR.value, M0=M0, binfit=bins, c='k', axin=ax, func=func, mbin=mbinsm, retfit=True, lsf='--')[0] fits = [] sgs = [0.1, 0.15, 0.20] for i, sg in enumerate(sgs): if truemass: hmass, hpos, hmasstrue = dg.scatter_catalog(hdictf['mass'], hdictf['position'], sg, truemass=True) datasg = pm.paint(hpos[:num], mass = hmasstrue[:num]) datasgR = ft.smooth(datasg, Rsm, 'fingauss') else: hmass, hpos = dg.scatter_catalog(hdictf['mass'], hdictf['position'], sg) datasg = pm.paint(hpos[:num], mass = hmass[:num]) datasgR = ft.smooth(datasg, Rsm, 'fingauss') fitt = dg.plot_noise(datasgR.value, predictR.value, M0=M0, binfit=bins, c=colors[i], axin=ax, func=func, mbin=mbinsm, retfit=True)[0] fits.append(fitt) ff = mtpath + 'noisehist_M%02d_na.png'%(10*np.log10(M0)) if truemass: ff = ff[:-4] + 'trueM.png' if Rsm !=3 : ff = ff[:-4] + '_R%d.png'%Rsm fig.savefig(ff) tosave = []