'/n/dvorkin_lab/anadr/%s_catalog/%s/%s_%s_parttype1_*.txt' % (name, numb, name, numb), ['x', 'y', 'z']) f['x'] = f['x'] / h - shiftx f['y'] = f['y'] / h - shifty f['z'] = f['z'] / h - shiftz select1 = f['x'] < lim select2 = f['x'] > -lim select3 = f['y'] < lim select4 = f['y'] > -lim select5 = f['z'] < lim select6 = f['z'] > -lim subcat = f[select1 & select2 & select3 & select4 & select5 & select6] subcat['Position'] = transform.StackColumns(subcat['x'], subcat['y'], subcat['z']) mesh = subcat.to_mesh(Nmesh=nmesh, BoxSize=bs) mesh.save('/n/dvorkin_lab/anadr/%s_%s_%s_%s_hbugfix.bigfile' % (name, numb, nmesh, bs)) N_part = len(subcat) print 'number of particles: %s' % N_part box_vol = bs**3 nbar = N_part / box_vol print nbar dicti = {'nbar': nbar, 'N_part': N_part}
def __init__(self, simname, halo_finder, redshift, comm=None): from halotools.sim_manager import CachedHaloCatalog, DownloadManager from halotools.sim_manager.supported_sims import supported_sim_dict # do seme setup self.comm = comm meta_cols = ['Lbox', 'redshift', 'particle_mass'] # try to automatically load from the Halotools cache exception = None if self.comm.rank == 0: kws = { 'simname': simname, 'halo_finder': halo_finder, 'redshift': redshift } try: cached_halos = CachedHaloCatalog(dz_tol=0.1, **kws) fname = cached_halos.fname # the filename to load meta = {k: getattr(cached_halos, k) for k in meta_cols} except Exception as e: # try to download on the root rank try: # download dl = DownloadManager() dl.download_processed_halo_table(dz_tol=0.1, **kws) # access the cached halo catalog and get fname attribute # NOTE: this does not read the data cached_halos = CachedHaloCatalog(dz_tol=0.1, **kws) fname = cached_halos.fname meta = {k: getattr(cached_halos, k) for k in meta_cols} except Exception as e: exception = e else: fname = None meta = None # re-raise a download error on all ranks if it occurred exception = self.comm.bcast(exception, root=0) if exception is not None: raise exception # broadcast the file we are loading fname = self.comm.bcast(fname, root=0) meta = self.comm.bcast(meta, root=0) # initialize an HDF catalog and add Position/Velocity cat = HDFCatalog(fname, comm=comm) cat['Position'] = transform.StackColumns(cat['halo_x'], cat['halo_y'], cat['halo_z']) cat['Velocity'] = transform.StackColumns(cat['halo_vx'], cat['halo_vy'], cat['halo_vz']) # get the cosmology from Halotools cosmo = supported_sim_dict[simname]( ).cosmology # this is astropy cosmology cosmo = Cosmology.from_astropy(cosmo) # initialize the HaloCatalog HaloCatalog.__init__(self, cat, cosmo, meta['redshift'], mdef='vir', mass='halo_mvir') # add some meta-data # NOTE: all Halotools catalogs have to these attributes self.attrs['BoxSize'] = meta['Lbox'] self.attrs['redshift'] = meta['redshift'] self.attrs['particle_mass'] = meta['particle_mass'] # save the cosmology self.cosmo = cosmo self.attrs['cosmo'] = dict(self.cosmo)
def _populate_mock(cat, model, seed=None, halocat=None, inplace=False, **params): """ Internal function to perform the mock population on a HaloCatalog, given a :mod:`halotools` model. The implementation is not massively parallel. The data is gathered to the root rank, mock population is performed, and then the data is re-scattered evenly across ranks. """ # verify input params valid = sorted(model.param_dict) missing = set(params) - set(valid) if len(missing): raise ValueError("invalid halo model parameter names: %s" % str(missing)) # update the model parameters model.param_dict.update(params) # set the seed randomly if it is None if seed is None: seed = numpy.random.randint(0, 4294967295) # use uncorrelated seed per rank rng = numpy.random.RandomState(seed=seed) seed1 = rng.randint(0, 4294967295, size=cat.comm.size)[cat.comm.rank] # the types of galaxies we are populating gal_types = getattr(model, 'gal_types', []) # re-populate the mock (without halo catalog pre-processing) kws = { 'seed': seed1, 'Num_ptcl_requirement': 0, 'halo_mass_column_key': cat.attrs['halo_mass_key'] } if hasattr(model, 'mock'): model.mock.populate(**kws) # populating model for the first time (initialization costs) else: if halocat is None: raise ValueError( "halocat cannot be None if we are populating for the first time" ) model.populate_mock(halocat=halocat, **kws) # enumerate gal types as integers # NOTE: necessary to avoid "O" type columns _enum_gal_types(model.mock.galaxy_table, gal_types) # crash if any object dtypes # NOTE: we cannot use GatherArray/ScatterArray on objects data = _test_for_objects(model.mock.galaxy_table).as_array() # re-initialize with new source if inplace: PopulatedHaloCatalog.__init__(cat, data, model, cat.cosmo, comm=cat.comm) galcat = cat else: galcat = PopulatedHaloCatalog(data, model, cat.cosmo, comm=cat.comm) # crash with no particles! if galcat.csize == 0: raise ValueError( "no particles in catalog after populating halo catalog") # add Position, Velocity galcat['Position'] = transform.StackColumns(galcat['x'], galcat['y'], galcat['z']) galcat['Velocity'] = transform.StackColumns(galcat['vx'], galcat['vy'], galcat['vz']) # add VelocityOffset z = cat.attrs['redshift'] rsd_factor = (1 + z) / (100. * cat.cosmo.efunc(z)) galcat['VelocityOffset'] = galcat['Velocity'] * rsd_factor # add meta-data galcat.attrs.update(cat.attrs) galcat.attrs.update(model.param_dict) galcat.attrs['seed'] = seed galcat.attrs['gal_types'] = {t: i for i, t in enumerate(gal_types)} # propagate total number of halos for logging Nhalos = galcat.comm.allreduce(len(galcat.model.mock.halo_table)) # and log some info _log_populated_stats(galcat, Nhalos) return galcat
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import pylab as py import sys import time import h5py from nbodykit.source.catalog import HDFCatalog, CSVCatalog from nbodykit import transform from nbodykit.lab import ProjectedFFTPower, FFTPower f = CSVCatalog('catalog_parttype1_*.txt', ['x', 'y', 'z']) print f f['Position'] = transform.StackColumns(f['x'], f['y'], f['z']) f['Weight'] = f['Weight'] * 2.8 * 1e4 f['r'] = np.linalg.norm(f['Position'], axis=1) med = np.median(f['r']) select = f['r'] == med shift = f['Position'][select] f['Position'] = f['Position'] - shift f['r'] = np.linalg.norm(f['Position'], axis=1) bs = 600 select2 = f['r'] <= 600 / 2