def __init__(self, config): super().__init__(config) self.cur_wp = np.zeros(11) if self['sim'] == "bolplanck": halocat = CachedHaloCatalog(simname='bolplanck') elif self['sim'] == "old": halocat = CachedHaloCatalog( fname='/home/lom31/Halo/hlist_1.00231.list.halotools_v0p1.hdf5', update_cached_fname=True) halocat.redshift = 0. elif self['sim'] == "smdpl": halocat = CachedHaloCatalog( fname= '/home/lom31/.astropy/cache/halotools/halo_catalogs/SMDPL/rockstar/2019-07-03-18-38-02-9731.dat.my_cosmosim_halos.hdf5', update_cached_fname=True) #halocat = CachedHaloCatalog(fname='/home/lom31/.astropy/cache/halotools/halo_catalogs/smdpl/rockstar/2019-07-03-18-38-02-9731.dat.my_cosmosim_halos.hdf5',update_cached_fname = True) halocat.redshift = 0. elif self['sim'] == "mdr1": halocat = CachedHaloCatalog( fname= '/home/lom31/.astropy/cache/halotools/halo_catalogs/multidark/rockstar/hlist_0.68215.list.halotools_v0p4.hdf5', update_cached_fname=True) if self['param'] == 'mvir': cens_occ_model = Zheng07Cens(threshold=-19) cens_prof_model = TrivialPhaseSpace() sats_occ_model = Zheng07Sats(modulate_with_cenocc=True, threshold=-19) sats_prof_model = NFWPhaseSpace() elif self['param'] == 'vmax': cens_occ_model = Zheng07Cens(prim_haloprop_key='halo_vmax', threshold=-19) cens_prof_model = TrivialPhaseSpace() sats_occ_model = Zheng07Sats(prim_haloprop_key='halo_vmax', threshold=-19, modulate_with_cenocc=True) sats_prof_model = NFWPhaseSpace() global model_instance model_instance = HodModelFactory(centrals_occupation=cens_occ_model, centrals_profile=cens_prof_model, satellites_occupation=sats_occ_model, satellites_profile=sats_prof_model) try: model_instance.mock.populate() except: model_instance.populate_mock(halocat)
def __makemodel__(self): """ Return the Zheng 07 HOD model. This model evaluates Eqs. 2 and 5 of Zheng et al. 2007 """ from halotools.empirical_models import HodModelFactory from halotools.empirical_models import Zheng07Sats, Zheng07Cens from halotools.empirical_models import NFWPhaseSpace, TrivialPhaseSpace model = {} # use concentration from halo table if 'halo_nfw_conc' in self._halos.halo_table.colnames: conc_mass_model = 'direct_from_halo_catalog' # use empirical prescription for c(M) else: conc_mass_model = 'dutton_maccio14' # occupation functions cenocc = Zheng07Cens(prim_haloprop_key=self.mass) satocc = Zheng07Sats(prim_haloprop_key=self.mass, modulate_with_cenocc=True, cenocc_model=cenocc) satocc._suppress_repeated_param_warning = True # add to model model['centrals_occupation'] = cenocc model['satellites_occupation'] = satocc # profile functions kws = {'cosmology':self.cosmo.to_astropy(), 'redshift':self.attrs['redshift'], 'mdef':self.attrs['mdef']} model['centrals_profile'] = TrivialPhaseSpace(**kws) model['satellites_profile'] = NFWPhaseSpace(conc_mass_model=conc_mass_model, **kws) return HodModelFactory(**model)
def decorated_hod_model(): cen_occ_model = AssembiasZheng07Cens(prim_haloprop_key='halo_mvir', sec_haloprop_key='halo_nfw_conc') cen_prof_model = TrivialPhaseSpace() sat_occ_model = AssembiasZheng07Sats(prim_haloprop_key='halo_mvir', sec_haloprop_key='halo_nfw_conc') sat_prof_model = NFWPhaseSpace() return HodModelFactory(centrals_occupation=cen_occ_model, centrals_profile=cen_prof_model, satellites_occupation=sat_occ_model, satellites_profile=sat_prof_model)
from noisyopt import minimizeCompass, minimizeSPSA, bisect, AveragedFunction warnings.filterwarnings("ignore") wp_ng_vals = zehavi_data_file_20.get_wp() bin_edges = zehavi_data_file_20.get_bins() cov_matrix = zehavi_data_file_20.get_cov() err = np.array([cov_matrix[i, i] for i in range(len(cov_matrix))]) bin_cen = (bin_edges[1:] + bin_edges[:-1]) / 2. #cens_occ_model = Zheng07Cens(prim_haloprop_key = 'halo_vmax') cens_occ_model = Zheng07Cens() cens_prof_model = TrivialPhaseSpace() #sats_occ_model = Zheng07Sats(prim_haloprop_key = 'halo_vmax', modulate_with_cenocc=True) sats_occ_model = Zheng07Sats(modulate_with_cenocc=True) sats_prof_model = NFWPhaseSpace() halocat = CachedHaloCatalog( fname= '/home/lom31/.astropy/cache/halotools/halo_catalogs/SMDPL/rockstar/2019-07-03-18-38-02-9731.dat.my_cosmosim_halos.hdf5', update_cached_fname=True) halocat.redshift = 0. pi_max = 60. Lbox = 400. model_instance = HodModelFactory(centrals_occupation=cens_occ_model, centrals_profile=cens_prof_model, satellites_occupation=sats_occ_model, satellites_profile=sats_prof_model) try: model_instance.mock.populate()
def __init__(self, **kwargs): """ Initialize a ABHodFitModel. """ # first, set up appropriate priors on parameters if ('priors' in kwargs.keys()): self.set_prior(kwargs['priors']) else: self.set_prior(default_priors) # set up keys for the parameter names for plotting self.param_names = [ 'alpha', 'logM1', 'siglogM', 'logM0', 'logMmin', 'Acens', 'Asats' ] self.latex_param_names = [ r'$\alpha$', r'$\log(M_1)$', r'$\sigma_{\log M}$', r'$\log(M_0)$', r'$\log(M_{\rm min})$', r'$\mathcal{A}_{\rm cens}$', r'$\mathcal{A}_{\rm sats}' ] # set up size parameters for any MCMC self.set_nwalkers(ndim=default_ndim, nwalkers=default_nwalkers) # if data is specified, load it into memory if 'rpcut' in kwargs.keys(): self.rpcut = kwargs['rpcut'] else: self.rpcut = default_rpcut if ('datafile' in kwargs.keys()): self.read_datafile(datafile=kwargs['datafile']) else: self.read_datafile(datafile=default_wp_datafile) if ('covarfile' in kwargs.keys()): self.read_covarfile(covarfile=kwargs['covarfile']) else: self.read_covarfile(covarfile=default_wp_covarfile) # if binfile is specified, load it into memory # these are Manodeep-style bins if ('binfile' in kwargs.keys()): self.binfile = kwargs['binfile'] else: self.binfile = default_binfile # set up a default HOD Model if ('cen_occ_model' in kwargs.keys()): cen_occ_model = kwargs['cen_occ_model'] else: cen_occ_model = AssembiasZheng07Cens( prim_haloprop_key='halo_mvir', sec_haloprop_key='halo_nfw_conc') if ('cen_prof_model' in kwargs.keys()): cen_prof_model = kwargs['cen_prof_model'] else: cen_prof_model = TrivialPhaseSpace() if ('sat_occ_model' in kwargs.keys()): sat_occ_model = kwargs['sat_occ_model'] else: sat_occ_model = AssembiasZheng07Sats( prim_haloprop_key='halo_mvir', sec_haloprop_key='halo_nfw_conc') if ('sat_prof_model' in kwargs.keys()): sat_prof_model = kwargs['sat_prof_model'] else: sat_prof_model = NFWPhaseSpace() # Default HOD Model is Zheng07 with Heaviside Assembly Bias self.hod_model = HodModelFactory(centrals_occupation=cen_occ_model, centrals_profile=cen_prof_model, satellites_occupation=sat_occ_model, satellites_profile=sat_prof_model) # set pi_max for wp(rp) calculations self.pi_max = default_pi_max if ('simname' in kwargs.keys()): simname = kwargs['simname'] else: simname = default_simname if ('halo_finder' in kwargs.keys()): halo_finder = kwargs['halo_finder'] else: halo_finder = default_halofinder if ('redshift' in kwargs.keys()): redshift = kwargs['redshift'] else: redshift = default_simredshift if ('version_name' in kwargs.keys()): version_name = kwargs['version_name'] else: version_name = default_version_name # set default simulation halocatalog to work with self.halocatalog = CachedHaloCatalog(simname=simname, halo_finder=halo_finder, redshift=redshift, version_name=version_name) return None
def tabulate(cls, halocat, tpcf, *tpcf_args, mode='auto', Num_ptcl_requirement=sim_defaults.Num_ptcl_requirement, prim_haloprop_key=model_defaults.prim_haloprop_key, prim_haloprop_bins=100, sec_haloprop_key=model_defaults.sec_haloprop_key, sec_haloprop_percentile_bins=None, sats_per_prim_haloprop=3e-12, downsample=1.0, verbose=False, redshift_space_distortions=True, cens_prof_model=None, sats_prof_model=None, project_xyz=False, cosmology_obs=None, num_threads=1, **tpcf_kwargs): """ Tabulates correlation functions for halos such that galaxy correlation functions can be calculated rapidly. Parameters ---------- halocat : object Either an instance of `halotools.sim_manager.CachedHaloCatalog` or `halotools.sim_manager.UserSuppliedHaloCatalog`. This halo catalog is used to tabubulate correlation functions. tpcf : function The halotools correlation function for which values are tabulated. Positional arguments should be passed after this function. Additional keyword arguments for the correlation function are also passed through this function. *tpcf_args : tuple, optional Positional arguments passed to the ``tpcf`` function. mode : string, optional String describing whether an auto- ('auto') or a cross-correlation ('cross') function is going to be tabulated. Num_ptcl_requirement : int, optional Requirement on the number of dark matter particles in the halo catalog. The column defined by the ``prim_haloprop_key`` string will have a cut placed on it: all halos with halocat.halo_table[prim_haloprop_key] < Num_ptcl_requirement*halocat.particle_mass will be thrown out immediately after reading the original halo catalog in memory. Default value is set in `~halotools.sim_defaults.Num_ptcl_requirement`. prim_haloprop_key : string, optional String giving the column name of the primary halo property governing the occupation statistics of gal_type galaxies. Default value is specified in the model_defaults module. prim_haloprop_bins : int or list, optional Integer determining how many (logarithmic) bins in primary halo property will be used. If a list or numpy array is provided, these will be used as bins directly. sec_haloprop_key : string, optional String giving the column name of the secondary halo property governing the assembly bias. Must be a key in the table passed to the methods of `HeavisideAssembiasComponent`. Default value is specified in the `~halotools.empirical_models.model_defaults` module. sec_haloprop_percentile_bins : int, float or None, optional If an integer, it determines how many evenly spaced bins in the secondary halo property percentiles are going to be used. If a float between 0 and 1, it determines the split. If None is provided, no binning is applied. sats_per_prim_haloprop : float, optional Float determing how many satellites sample each halo. For each halo, the number is drawn from a Poisson distribution with an expectation value of ``sats_per_prim_haloprop`` times the primary halo property. downsample : float or function, optional Fraction between 0 and 1 used to downsample the total sample used to tabulate correlation functions. Values below unity can be used to reduce the computation time. It should not result in biases but the resulting correlation functions will be less accurate. If float, the same value is applied to all halos. If function, it should return the fraction is a function of the primary halo property. verbose : boolean, optional Boolean determing whether the progress should be displayed. redshift_space_distortions : boolean, optional Boolean determining whether redshift space distortions should be applied to halos/galaxies. cens_prof_model : object, optional Instance of `halotools.empirical_models.MonteCarloGalProf` that determines the phase space coordinates of centrals. If none is provided, `halotools.empirical_models.TrivialPhaseSpace` will be used. sats_prof_model : object, optional Instance of `halotools.empirical_models.MonteCarloGalProf` that determines the phase space coordinates of satellites. If none is provided, `halotools.empirical_models.NFWPhaseSpace` will be used. project_xyz : bool, optional If True, the coordinates will be projected along all three spatial axes. By default, only the projection onto the z-axis is used. cosmology_obs : object, optional Instance of an astropy `~astropy.cosmology`. This can be used to correct coordinates in the simulation for the Alcock-Paczynski (AP) effect, i.e. a mismatch between the cosmology of the model (simulation) and the cosmology used to interpret observations. Note that the cosmology of the simulation is part of the halocat object. If None, no correction for the AP effect is applied. Also, a correction for the AP effect is only applied for auto-correlation functions. num_threads : int, optional How many threads to use for the tabulation. **tpcf_kwargs : dict, optional Keyword arguments passed to the ``tpcf`` function. Returns ------- halotab : TabCorr Object containing all necessary information to calculate correlation functions for arbitrary galaxy models. """ if 'period' in tpcf_kwargs: print('Warning: TabCorr will pass the keyword argument "period" ' + 'to {} based on the Lbox argument of'.format(tpcf.__name__) + ' the halo catalog. The value you provided will be ignored.') del tpcf_kwargs['period'] halotab = cls() if cosmology_obs is not None and mode == 'auto': rp_stretch = ( (cosmology_obs.comoving_distance(halocat.redshift) * cosmology_obs.H0) / (halocat.cosmology.comoving_distance(halocat.redshift) * halocat.cosmology.H0)) pi_stretch = (halocat.cosmology.efunc(halocat.redshift) / cosmology_obs.efunc(halocat.redshift)) lbox_stretch = np.array([rp_stretch, rp_stretch, pi_stretch]) else: lbox_stretch = np.ones(3) # First, we tabulate the halo number densities. halos = halocat.halo_table halos = halos[halos['halo_pid'] == -1] halos = halos[halos[prim_haloprop_key] >= (Num_ptcl_requirement + 0.5) * halocat.particle_mass] if isinstance(prim_haloprop_bins, int): prim_haloprop_bins = np.linspace( np.log10(np.amin(halos[prim_haloprop_key])) - 1e-3, np.log10(np.amax(halos[prim_haloprop_key])) + 1e-3, prim_haloprop_bins + 1) elif isinstance(prim_haloprop_bins, (list, np.ndarray)): pass else: raise ValueError('prim_haloprop_bins must be an int, list or ' + 'numpy array.') if sec_haloprop_percentile_bins is None: sec_haloprop_percentile_bins = np.array([-1e-3, 1 + 1e-3]) elif isinstance(sec_haloprop_percentile_bins, float): if not (0 < sec_haloprop_percentile_bins and sec_haloprop_percentile_bins < 1): raise ValueError('sec_haloprop_percentile_bins must be ' + 'between 0 and 1.') sec_haloprop_percentile_bins = np.array( [-1e-3, sec_haloprop_percentile_bins, 1 + 1e-3]) elif isinstance(sec_haloprop_percentile_bins, int): sec_haloprop_percentile_bins = np.linspace( -1e-3, 1 + 1e-3, sec_haloprop_percentile_bins + 1) else: raise ValueError('sec_haloprop_percentile_bins must be an int, ' + 'float, list or numpy array.') halos[sec_haloprop_key + '_percentile'] = (compute_conditional_percentiles( table=halos, prim_haloprop_key=prim_haloprop_key, sec_haloprop_key=sec_haloprop_key)) halotab.gal_type = Table() n_h, prim_haloprop_bins, sec_haloprop_percentile_bins = ( np.histogram2d( np.log10(halos[prim_haloprop_key]), halos[sec_haloprop_key + '_percentile'], bins=[prim_haloprop_bins, sec_haloprop_percentile_bins])) halotab.gal_type['n_h'] = n_h.ravel(order='F') grid = np.meshgrid(prim_haloprop_bins, sec_haloprop_percentile_bins) halotab.gal_type['log_prim_haloprop_min'] = grid[0][:-1, :-1].ravel() halotab.gal_type['log_prim_haloprop_max'] = grid[0][:-1, 1:].ravel() halotab.gal_type['sec_haloprop_percentile_min'] = ( grid[1][:-1, :-1].ravel()) halotab.gal_type['sec_haloprop_percentile_max'] = ( grid[1][1:, :-1].ravel()) halotab.gal_type = vstack([halotab.gal_type, halotab.gal_type]) halotab.gal_type['gal_type'] = np.concatenate( (np.repeat('centrals'.encode('utf8'), len(halotab.gal_type) // 2), np.repeat('satellites'.encode('utf8'), len(halotab.gal_type) // 2))) halotab.gal_type['prim_haloprop'] = 10**( 0.5 * (halotab.gal_type['log_prim_haloprop_min'] + halotab.gal_type['log_prim_haloprop_max'])) halotab.gal_type['sec_haloprop_percentile'] = ( 0.5 * (halotab.gal_type['sec_haloprop_percentile_min'] + halotab.gal_type['sec_haloprop_percentile_max'])) # Now, we tabulate the correlation functions. cens_occ_model = Zheng07Cens(prim_haloprop_key=prim_haloprop_key) if cens_prof_model is None: cens_prof_model = TrivialPhaseSpace(redshift=halocat.redshift) sats_occ_model = Zheng07Sats(prim_haloprop_key=prim_haloprop_key) if sats_prof_model is None: sats_prof_model = NFWPhaseSpace(redshift=halocat.redshift) model = HodModelFactory(centrals_occupation=cens_occ_model, centrals_profile=cens_prof_model, satellites_occupation=sats_occ_model, satellites_profile=sats_prof_model) model.param_dict['logMmin'] = 0 model.param_dict['sigma_logM'] = 0.1 model.param_dict['alpha'] = 1.0 model.param_dict['logM0'] = 0 model.param_dict['logM1'] = -np.log10(sats_per_prim_haloprop) model.populate_mock(halocat, Num_ptcl_requirement=Num_ptcl_requirement) gals = model.mock.galaxy_table idx_gals, idx_halos = crossmatch(gals['halo_id'], halos['halo_id']) assert np.all(gals['halo_id'][idx_gals] == halos['halo_id'][idx_halos]) gals[sec_haloprop_key + '_percentile'] = np.zeros(len(gals)) gals[sec_haloprop_key + '_percentile'][idx_gals] = (halos[sec_haloprop_key + '_percentile'][idx_halos]) if verbose: print("Number of tracer particles: {0}".format(len(gals))) for xyz in ['xyz', 'yzx', 'zxy']: if verbose and project_xyz: print("Projecting onto {0}-axis...".format(xyz[2])) pos_all = (return_xyz_formatted_array( x=gals[xyz[0]], y=gals[xyz[1]], z=gals[xyz[2]], velocity=gals['v' + xyz[2]] if redshift_space_distortions else 0, velocity_distortion_dimension='z', period=halocat.Lbox, redshift=halocat.redshift, cosmology=halocat.cosmology) * lbox_stretch) period = halocat.Lbox * lbox_stretch # Get a list of the positions of each sub-population. i_prim = np.digitize(np.log10(gals[prim_haloprop_key]), bins=prim_haloprop_bins, right=False) - 1 mask = (i_prim < 0) | (i_prim >= len(prim_haloprop_bins)) i_sec = np.digitize(gals[sec_haloprop_key + '_percentile'], bins=sec_haloprop_percentile_bins, right=False) - 1 i_type = np.where(gals['gal_type'] == 'centrals', 0, 1) # Throw out those that don't fall into any bin. pos_all = pos_all[~mask] i = (i_prim + i_sec * (len(prim_haloprop_bins) - 1) + i_type * ((len(prim_haloprop_bins) - 1) * (len(sec_haloprop_percentile_bins) - 1))) pos_all = pos_all[np.argsort(i)] counts = np.bincount(i, minlength=len(halotab.gal_type)) assert len(counts) == len(halotab.gal_type) pos_bin = [] for i in range(len(halotab.gal_type)): pos = pos_all[np.sum(counts[:i]):np.sum(counts[:i + 1]), :] if halotab.gal_type['gal_type'][i] == 'centrals': # Make sure the number of halos are consistent. try: assert len(pos) == int(halotab.gal_type['n_h'][i]) except AssertionError: raise RuntimeError('There was an internal error in ' + 'TabCorr. If possible, please ' + 'report this bug in the TabCorr ' + 'GitHub repository.') else: if len(pos) == 0 and halotab.gal_type['n_h'][i] != 0: raise RuntimeError( 'There was at least one bin without satellite ' + 'tracers. Increase sats_per_prim_haloprop.') if len(pos) > 0: if isinstance(downsample, float): use = np.random.random(len(pos)) < downsample else: use = (np.random.random(len(pos)) < downsample( halotab.gal_type['prim_haloprop'][i])) # If the down-sampling reduced the number of tracers to at # or below one, force at least 2 tracers to not bias the # clustering estimates. if np.sum(use) <= 1 and len(pos) > 1: use = np.zeros(len(pos), dtype=bool) use[np.random.choice(len(pos), size=2)] = True pos = pos[use] pos_bin.append(pos) if mode == 'auto': combinations = itertools.combinations_with_replacement( range(len(halotab.gal_type)), 2) else: combinations = range(len(halotab.gal_type)) if xyz == 'xyz': tpcf_matrix, tpcf_shape = compute_tpcf_matrix( mode, pos_bin, tpcf, period, tpcf_args, tpcf_kwargs, combinations, num_threads=num_threads, verbose=verbose) if not project_xyz or mode == 'cross': break elif xyz != 'xyz': tpcf_matrix += compute_tpcf_matrix(mode, pos_bin, tpcf, period, tpcf_args, tpcf_kwargs, combinations, num_threads=num_threads, verbose=verbose)[0] if project_xyz and mode == 'auto': tpcf_matrix /= 3.0 if mode == 'auto': tpcf_matrix_flat = [] for i in range(tpcf_matrix.shape[0]): tpcf_matrix_flat.append( symmetric_matrix_to_array(tpcf_matrix[i])) tpcf_matrix = np.array(tpcf_matrix_flat) # Remove entries that don't have any halos. use = halotab.gal_type['n_h'] != 0 halotab.gal_type = halotab.gal_type[use] if mode == 'auto': use = symmetric_matrix_to_array(np.outer(use, use)) tpcf_matrix = tpcf_matrix[:, use] halotab.gal_type['n_h'] /= np.prod(halocat.Lbox * lbox_stretch) halotab.attrs = {} halotab.attrs['tpcf'] = tpcf.__name__ halotab.attrs['mode'] = mode halotab.attrs['simname'] = halocat.simname halotab.attrs['redshift'] = halocat.redshift halotab.attrs['Num_ptcl_requirement'] = Num_ptcl_requirement halotab.attrs['prim_haloprop_key'] = prim_haloprop_key halotab.attrs['sec_haloprop_key'] = sec_haloprop_key halotab.tpcf_args = tpcf_args halotab.tpcf_kwargs = tpcf_kwargs halotab.tpcf_shape = tpcf_shape halotab.tpcf_matrix = tpcf_matrix halotab.init = True return halotab
def tabulate(cls, halocat, tpcf, *tpcf_args, mode='auto', Num_ptcl_requirement=sim_defaults.Num_ptcl_requirement, cosmology=sim_defaults.default_cosmology, prim_haloprop_key=model_defaults.prim_haloprop_key, prim_haloprop_bins=100, sec_haloprop_key=model_defaults.sec_haloprop_key, sec_haloprop_percentile_bins=None, sats_per_prim_haloprop=3e-12, downsample=1.0, verbose=False, redshift_space_distortions=True, cens_prof_model=None, sats_prof_model=None, project_xyz=False, cosmology_ref=None, comm=None, **tpcf_kwargs): """ Tabulates correlation functions for halos such that galaxy correlation functions can be calculated rapidly. Parameters ---------- halocat : object Either an instance of `halotools.sim_manager.CachedHaloCatalog` or `halotools.sim_manager.UserSuppliedHaloCatalog`. This halo catalog is used to tabubulate correlation functions. tpcf : function The halotools correlation function for which values are tabulated. Positional arguments should be passed after this function. Additional keyword arguments for the correlation function are also passed through this function. *tpcf_args : tuple, optional Positional arguments passed to the ``tpcf`` function. mode : string, optional String describing whether an auto- ('auto') or a cross-correlation ('cross') function is going to be tabulated. Num_ptcl_requirement : int, optional Requirement on the number of dark matter particles in the halo catalog. The column defined by the ``prim_haloprop_key`` string will have a cut placed on it: all halos with halocat.halo_table[prim_haloprop_key] < Num_ptcl_requirement*halocat.particle_mass will be thrown out immediately after reading the original halo catalog in memory. Default value is set in `~halotools.sim_defaults.Num_ptcl_requirement`. cosmology : object, optional Instance of an astropy `~astropy.cosmology`. Default cosmology is set in `~halotools.sim_manager.sim_defaults`. This might be used to calculate phase-space distributions and redshift space distortions. prim_haloprop_key : string, optional String giving the column name of the primary halo property governing the occupation statistics of gal_type galaxies. Default value is specified in the model_defaults module. prim_haloprop_bins : int or list, optional Integer determining how many (logarithmic) bins in primary halo property will be used. If a list or numpy array is provided, these will be used as bins directly. sec_haloprop_key : string, optional String giving the column name of the secondary halo property governing the assembly bias. Must be a key in the table passed to the methods of `HeavisideAssembiasComponent`. Default value is specified in the `~halotools.empirical_models.model_defaults` module. sec_haloprop_percentile_bins : int, float, list or None, optional If an integer, it determines how many evenly spaced bins in the secondary halo property percentiles are going to be used. If a float between 0 and 1, it determines the split. Finally, if a list or numpy array, it directly describes the bins that are going to be used. If None is provided, no binning is applied. sats_per_prim_haloprop : float, optional Float determing how many satellites sample each halo. For each halo, the number is drawn from a Poisson distribution with an expectation value of ``sats_per_prim_haloprop`` times the primary halo property. downsample : float, optional Fraction between 0 and 1 used to downsample the total sample used to tabulate correlation functions. Values below unity can be used to reduce the computation time. It should not result in biases but the resulting correlation functions will be less accurate. verbose : boolean, optional Boolean determing whether the progress should be displayed. redshift_space_distortions : boolean, optional Boolean determining whether redshift space distortions should be applied to halos/galaxies. cens_prof_model : object, optional Instance of `halotools.empirical_models.MonteCarloGalProf` that determines the phase space coordinates of centrals. If none is provided, `halotools.empirical_models.TrivialPhaseSpace` will be used. sats_prof_model : object, optional Instance of `halotools.empirical_models.MonteCarloGalProf` that determines the phase space coordinates of satellites. If none is provided, `halotools.empirical_models.NFWPhaseSpace` will be used. project_xyz : bool, optional If True, the coordinates will be projected along all three spatial axes. By default, only the projection onto the z-axis is used. comm : MPI communicator If not None, then will distribute calculation via MPI **tpcf_kwargs : dict, optional Keyword arguments passed to the ``tpcf`` function. Returns ------- halotab : TabCorr Object containing all necessary information to calculate correlation functions for arbitrary galaxy models. """ if sec_haloprop_percentile_bins is None: sec_haloprop_percentile_bins = np.array([0, 1]) elif isinstance(sec_haloprop_percentile_bins, float): sec_haloprop_percentile_bins = np.array( [0, sec_haloprop_percentile_bins, 1]) if 'period' in tpcf_kwargs: print('Warning: TabCorr will pass the keyword argument "period" ' + 'to {} based on the Lbox argument of'.format(tpcf.__name__) + ' the halo catalog. The value you provided will be ignored.') del tpcf_kwargs['period'] halotab = cls() if cosmology_ref is not None and mode == 'auto': rp_stretch = ( (cosmology_ref.comoving_distance(halocat.redshift) * cosmology_ref.H0) / (cosmology.comoving_distance(halocat.redshift) * cosmology.H0)) pi_stretch = (cosmology.efunc(halocat.redshift) / cosmology_ref.efunc(halocat.redshift)) lbox_stretch = np.array([rp_stretch, rp_stretch, pi_stretch]) else: lbox_stretch = np.ones(3) # First, we tabulate the halo number densities. halos = halocat.halo_table halos = halos[halos['halo_pid'] == -1] halos = halos[halos[prim_haloprop_key] >= (Num_ptcl_requirement - 0.5) * halocat.particle_mass] if isinstance(prim_haloprop_bins, int): prim_haloprop_bins = np.linspace( np.log10(np.amin(halos[prim_haloprop_key])) - 1e-3, np.log10(np.amax(halos[prim_haloprop_key])) + 1e-3, prim_haloprop_bins + 1) elif not isinstance(prim_haloprop_bins, (list, np.ndarray)): raise ValueError('prim_haloprop_bins must be an int, list or ' + 'numpy array.') halos[sec_haloprop_key + '_percentile'] = (compute_conditional_percentiles( table=halos, prim_haloprop_key=prim_haloprop_key, sec_haloprop_key=sec_haloprop_key)) halotab.gal_type = Table() n_h, prim_haloprop_bins, sec_haloprop_percentile_bins = ( np.histogram2d( np.log10(halos[prim_haloprop_key]), halos[sec_haloprop_key + '_percentile'], bins=[prim_haloprop_bins, sec_haloprop_percentile_bins])) halotab.gal_type['n_h'] = n_h.ravel(order='F') / np.prod( halocat.Lbox * lbox_stretch) grid = np.meshgrid(prim_haloprop_bins, sec_haloprop_percentile_bins) halotab.gal_type['log_prim_haloprop_min'] = grid[0][:-1, :-1].ravel() halotab.gal_type['log_prim_haloprop_max'] = grid[0][:-1, 1:].ravel() halotab.gal_type['sec_haloprop_percentile_min'] = ( grid[1][:-1, :-1].ravel()) halotab.gal_type['sec_haloprop_percentile_max'] = ( grid[1][1:, :-1].ravel()) halotab.gal_type = vstack([halotab.gal_type, halotab.gal_type]) halotab.gal_type['gal_type'] = np.concatenate( (np.repeat('centrals'.encode('utf8'), len(halotab.gal_type) // 2), np.repeat('satellites'.encode('utf8'), len(halotab.gal_type) // 2))) halotab.gal_type['prim_haloprop'] = 10**( 0.5 * (halotab.gal_type['log_prim_haloprop_min'] + halotab.gal_type['log_prim_haloprop_max'])) halotab.gal_type['sec_haloprop_percentile'] = ( 0.5 * (halotab.gal_type['sec_haloprop_percentile_min'] + halotab.gal_type['sec_haloprop_percentile_max'])) # Now, we tabulate the correlation functions. cens_occ_model = Zheng07Cens(prim_haloprop_key=prim_haloprop_key) if cens_prof_model is None: cens_prof_model = TrivialPhaseSpace(redshift=halocat.redshift) sats_occ_model = Zheng07Sats(prim_haloprop_key=prim_haloprop_key) if sats_prof_model is None: sats_prof_model = NFWPhaseSpace(redshift=halocat.redshift) model = HodModelFactory(centrals_occupation=cens_occ_model, centrals_profile=cens_prof_model, satellites_occupation=sats_occ_model, satellites_profile=sats_prof_model) model.param_dict['logMmin'] = 0 model.param_dict['sigma_logM'] = 0.1 model.param_dict['alpha'] = 1.0 model.param_dict['logM0'] = 0 model.param_dict['logM1'] = -np.log10(sats_per_prim_haloprop) model.populate_mock(halocat, Num_ptcl_requirement=Num_ptcl_requirement) gals = model.mock.galaxy_table gals = gals[np.random.random(len(gals)) < downsample] idx_gals, idx_halos = crossmatch(gals['halo_id'], halos['halo_id']) assert np.all(gals['halo_id'][idx_gals] == halos['halo_id'][idx_halos]) gals[sec_haloprop_key + '_percentile'] = np.zeros(len(gals)) gals[sec_haloprop_key + '_percentile'][idx_gals] = (halos[sec_haloprop_key + '_percentile'][idx_halos]) if verbose: print("Number of tracer particles: {0}".format(len(gals))) for xyz in ['xyz', 'yzx', 'zxy']: pos_all = return_xyz_formatted_array( x=gals[xyz[0]], y=gals[xyz[1]], z=gals[xyz[2]], velocity=gals['v' + xyz[2]] if redshift_space_distortions else 0, velocity_distortion_dimension='z', period=halocat.Lbox, redshift=halocat.redshift, cosmology=cosmology) * lbox_stretch pos = [] n_gals = [] for i in range(len(halotab.gal_type)): mask = ((10**(halotab.gal_type['log_prim_haloprop_min'][i]) < gals[prim_haloprop_key]) & (10**(halotab.gal_type['log_prim_haloprop_max'][i]) >= gals[prim_haloprop_key]) & (halotab.gal_type['sec_haloprop_percentile_min'][i] < gals[sec_haloprop_key + '_percentile']) & (halotab.gal_type['sec_haloprop_percentile_max'][i] >= gals[sec_haloprop_key + '_percentile']) & (halotab.gal_type['gal_type'][i] == gals['gal_type'])) pos.append(pos_all[mask]) n_gals.append(np.sum(mask)) n_gals = np.array(n_gals) n_done = 0 if verbose: print("Projecting onto {0}-axis...".format(xyz[2])) gal_type_index = np.arange(len(halotab.gal_type)) if (comm is not None) & (has_mpi): size = comm.size rank = comm.rank gal_type_index = gal_type_index[rank::size] print('{}: len(gal_type_index)={}'.format( rank, len(gal_type_index))) elif (comm is not None) & (not has_mpi): raise (ImportError( "You passed something to the comm argument, but I couldn't import mpi4py" )) for i in gal_type_index: if mode == 'auto': for k in np.arange(i, len(halotab.gal_type)): if len(pos[i]) * len(pos[k]) > 0: if verbose: if comm: if comm.rank == 0: n_done += (n_gals[i] * n_gals[k] * (2 if k != i else 1)) print_progress(n_done / np.sum(n_gals)**2) else: n_done += (n_gals[i] * n_gals[k] * (2 if k != i else 1)) print_progress(n_done / np.sum(n_gals)**2) if i == k: xi = tpcf(pos[i], *tpcf_args, sample2=pos[k] if k != i else None, do_auto=True, do_cross=False, period=halocat.Lbox * lbox_stretch, **tpcf_kwargs) else: xi = tpcf(pos[i], *tpcf_args, sample2=pos[k] if k != i else None, do_auto=False, do_cross=True, period=halocat.Lbox * lbox_stretch, **tpcf_kwargs) if 'tpcf_matrix' not in locals(): tpcf_matrix = np.zeros( (len(xi.ravel()), len(halotab.gal_type), len(halotab.gal_type))) tpcf_shape = xi.shape tpcf_matrix[:, i, k] += xi.ravel() tpcf_matrix[:, k, i] = tpcf_matrix[:, i, k] elif mode == 'cross': if len(pos[i]) > 0: if verbose: n_done += n_gals[i] print_progress(n_done / np.sum(n_gals)) xi = tpcf(pos[i], *tpcf_args, **tpcf_kwargs, period=halocat.Lbox * lbox_stretch) if tpcf.__name__ == 'delta_sigma': xi = xi[1] if 'tpcf_matrix' not in locals(): tpcf_matrix = np.zeros( (len(xi.ravel()), len(halotab.gal_type))) tpcf_shape = xi.shape tpcf_matrix[:, i] = xi.ravel() if not project_xyz or mode == 'cross': break if comm: tpcf_matrix = comm.allreduce(tpcf_matrix, op=MPI.SUM) if project_xyz and mode == 'auto': tpcf_matrix /= 3.0 if mode == 'auto': tpcf_matrix_flat = [] for i in range(tpcf_matrix.shape[0]): tpcf_matrix_flat.append( symmetric_matrix_to_array(tpcf_matrix[i])) tpcf_matrix = np.array(tpcf_matrix_flat) halotab.attrs = {} halotab.attrs['tpcf'] = tpcf.__name__ halotab.attrs['mode'] = mode halotab.attrs['simname'] = halocat.simname halotab.attrs['redshift'] = halocat.redshift halotab.attrs['Num_ptcl_requirement'] = Num_ptcl_requirement halotab.attrs['prim_haloprop_key'] = prim_haloprop_key halotab.attrs['sec_haloprop_key'] = sec_haloprop_key halotab.tpcf_args = tpcf_args halotab.tpcf_kwargs = tpcf_kwargs halotab.tpcf_shape = tpcf_shape halotab.tpcf_matrix = tpcf_matrix halotab.init = True return halotab
def assign_positions(self, **kwargs): """ assign satellite positions based on subhalo radial positions and random angular positions. """ if 'table' in kwargs.keys(): table = kwargs['table'] halo_x = table['halo_x'] halo_y = table['halo_y'] halo_z = table['halo_z'] halo_hostid = table['halo_hostid'] halo_id = table['halo_id'] b_to_a = table['halo_b_to_a'] c_to_a = table['halo_c_to_a'] halo_axisA_x = table['halo_axisA_x'] halo_axisA_y = table['halo_axisA_y'] halo_axisA_z = table['halo_axisA_z'] halo_axisC_x = table['halo_axisC_x'] halo_axisC_y = table['halo_axisC_y'] halo_axisC_z = table['halo_axisC_z'] concentration = table['halo_nfw_conc'] rvir = table['halo_rvir'] try: Lbox = kwargs['Lbox'] except KeyError: Lbox = self._Lbox else: halo_x = kwargs['halo_x'] halo_y = kwargs['halo_y'] halo_z = kwargs['halo_z'] halo_hostid = kwargs['halo_hostid'] halo_id = kwargs['halo_id'] b_to_a = kwargs['halo_b_to_a'] c_to_a = kwargs['halo_c_to_a'] halo_axisA_x = kwargs['halo_axisA_x'] halo_axisA_y = kwargs['halo_axisA_y'] halo_axisA_z = kwargs['halo_axisA_z'] halo_axisC_x = kwargs['halo_axisC_x'] halo_axisC_y = kwargs['halo_axisC_y'] halo_axisC_z = kwargs['halo_axisC_z'] concentration = kwargs['halo_nfw_conc'] rvir = tabel['halo_rvir'] try: Lbox = kwargs['Lbox'] except KeyError: Lbox = self._Lbox Npts = len(halo_x) # get host halo properties inds1, inds2 = crossmatch(halo_hostid, halo_id) # some sub-haloes point to a host that does not exist no_host = ~np.in1d(halo_hostid, halo_id) if np.sum(no_host) > 0: msg = ("There are {0} sub-haloes with no host halo.".format( np.sum(no_host))) warn(msg) host_halo_concentration = np.zeros(Npts) host_halo_concentration[inds1] = concentration[inds2] host_halo_rvir = np.zeros(Npts) host_halo_rvir[inds1] = rvir[inds2] host_b_to_a = np.zeros(Npts) host_b_to_a[inds1] = b_to_a[inds2] host_c_to_a = np.zeros(Npts) host_c_to_a[inds1] = c_to_a[inds2] major_axis = np.vstack((halo_axisA_x, halo_axisA_y, halo_axisA_z)).T minor_axis = np.vstack((halo_axisC_x, halo_axisC_y, halo_axisC_z)).T inter_axis = np.cross(major_axis, minor_axis) host_major_axis = np.zeros((Npts, 3)) host_inter_axis = np.zeros((Npts, 3)) host_minor_axis = np.zeros((Npts, 3)) host_major_axis[inds1] = major_axis[inds2] host_inter_axis[inds1] = inter_axis[inds2] host_minor_axis[inds1] = minor_axis[inds2] # host x,y,z-position halo_x[inds1] = halo_x[inds2] halo_y[inds1] = halo_y[inds2] halo_z[inds1] = halo_z[inds2] # host halo centric positions phi = np.random.uniform(0, 2 * np.pi, Npts) uran = np.random.rand(Npts) * 2 - 1 cos_t = uran sin_t = np.sqrt((1. - cos_t * cos_t)) b_to_a, c_to_a = self.anisotropy_bias_response(host_b_to_a, host_c_to_a) c_to_b = c_to_a / b_to_a # temporarily use x-axis as the major axis x = 1.0 / c_to_a * sin_t * np.cos(phi) y = 1.0 / c_to_b * sin_t * np.sin(phi) z = cos_t x_correlated_axes = np.vstack((x, y, z)).T x_axes = np.tile((1, 0, 0), Npts).reshape((Npts, 3)) matrices = rotation_matrices_from_basis(host_major_axis, host_inter_axis, host_minor_axis) # rotate x-axis into the major axis #angles = angles_between_list_of_vectors(x_axes, major_axes) #rotation_axes = vectors_normal_to_planes(x_axes, major_axes) #matrices = rotation_matrices_from_angles(angles, rotation_axes) correlated_axes = rotate_vector_collection(matrices, x_correlated_axes) x, y, z = correlated_axes[:, 0], correlated_axes[:, 1], correlated_axes[:, 2] nfw = NFWPhaseSpace(conc_mass_model='direct_from_halo_catalog', ) dimensionless_radial_distance = nfw._mc_dimensionless_radial_distance( host_halo_concentration) x *= dimensionless_radial_distance y *= dimensionless_radial_distance z *= dimensionless_radial_distance x *= host_halo_rvir y *= host_halo_rvir z *= host_halo_rvir a = 1 b = b_to_a * a c = c_to_a * a T = (c**2 - b**2) / (c**2 - a**2) q = b / a s = c / a x *= np.sqrt(q * s) y *= np.sqrt(q * s) z *= np.sqrt(q * s) # host-halo centric radial distance r = np.sqrt(x * x + y * y + z * z) # move back into original cordinate system xx = halo_x + x yy = halo_y + y zz = halo_z + z xx[no_host] = halo_x[no_host] yy[no_host] = halo_y[no_host] zz[no_host] = halo_z[no_host] # account for PBCs xx, yy, zz = wrap_coordinates(xx, yy, zz, Lbox) if 'table' in kwargs.keys(): # assign satellite galaxy positions try: mask = (table['gal_type'] == 'satellites') except KeyError: mask = np.array([True] * len(table)) msg = ( "`gal_type` not indicated in `table`.", "The orientation is being assigned for all galaxies in the `table`." ) print(msg) table['x'] = halo_x * 1.0 table['y'] = halo_y * 1.0 table['z'] = halo_z * 1.0 table['x'][mask] = xx[mask] table['y'][mask] = yy[mask] table['z'][mask] = zz[mask] table['r'] = 0.0 table['r'][mask] = r[mask] table['halo_x'][mask] = halo_x[mask] table['halo_y'][mask] = halo_y[mask] table['halo_z'][mask] = halo_z[mask] return table else: x = xx y = yy z = zz return np.vstack((x, y, z)).T
def to_halotools(cosmo, redshift, mdef, concentration_key=None, **kwargs): """ Return the Zheng 07 HOD model. See :func:`halotools.empirical_models.zheng07_model_dictionary`. Parameters ---------- cosmo : the nbodykit or astropy Cosmology object to use in the model redshift : float the desired redshift of the model mdef : str, optional string specifying mass definition, used for computing default halo radii and concentration; should be 'vir' or 'XXXc' or 'XXXm' where 'XXX' is an int specifying the overdensity concentration_key : str the name of the column that will specify concentration; if not provided, the analytic formula from `Dutton and Maccio 2014 <https://arxiv.org/abs/1402.7073>`_ is used. **kwargs : additional keywords passed to the model components; see the Halotools documentation for further details Returns ------- :class:`~halotools.empirical_models.HodModelFactory` the halotools object implementing the HOD model """ from halotools.empirical_models import Zheng07Sats, Zheng07Cens, NFWPhaseSpace, TrivialPhaseSpace from halotools.empirical_models import HodModelFactory kwargs.setdefault('modulate_with_cenocc', True) # need astropy Cosmology if isinstance(cosmo, Cosmology): cosmo = cosmo.to_astropy() # determine concentration key if concentration_key is None: conc_mass_model = 'dutton_maccio14' else: conc_mass_model = 'direct_from_halo_catalog' # determine mass column mass_key = 'halo_m' + mdef # occupation functions cenocc = Zheng07Cens(prim_haloprop_key=mass_key, **kwargs) satocc = Zheng07Sats(prim_haloprop_key=mass_key, cenocc_model=cenocc, **kwargs) satocc._suppress_repeated_param_warning = True # profile functions kwargs.update({'cosmology': cosmo, 'redshift': redshift, 'mdef': mdef}) censprof = TrivialPhaseSpace(**kwargs) satsprof = NFWPhaseSpace(conc_mass_model=conc_mass_model, **kwargs) # make the model model = {} model['centrals_occupation'] = cenocc model['centrals_profile'] = censprof model['satellites_occupation'] = satocc model['satellites_profile'] = satsprof return HodModelFactory(**model)