def __init__(self, **kwargs): Zheng07Sats.__init__(self, **kwargs) PreservingNgalHeavisideAssembias.__init__( self, lower_assembias_bound=self._lower_occupation_bound, upper_assembias_bound=self._upper_occupation_bound, method_name_to_decorate='mean_occupation', **kwargs)
def __init__(self, **kwargs): Zheng07Sats.__init__(self, threshold = -21) HeavisideAssembias.__init__(self, method_name_to_decorate = 'mean_occupation', lower_assembias_bound = 0., upper_assembias_bound = np.inf, **kwargs)
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)
import warnings 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:
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 main(): # get simulation information if len(sys.argv)>1: sim_name = sys.argv[1] snapnum = int(sys.argv[2]) shape_type = sys.argv[3] sample_name = sys.argv[4] else: sim_name = 'TNG300-1' # full physics high-res run snapnum = 99 # z=0 shape_type = 'reduced' # non-reduced, reduced, iterative sample_name = 'sample_3' # load a test halo catalog from halotools.sim_manager import CachedHaloCatalog halocat = CachedHaloCatalog(simname='bolplanck', halo_finder='rockstar', redshift=0.0, dz_tol=0.1, version_name='halotools_v0p4') from halotools.empirical_models import HodModelFactory # define the central occupatoion model from halotools.empirical_models import TrivialPhaseSpace, Zheng07Cens cens_occ_model = Zheng07Cens() cens_prof_model = TrivialPhaseSpace() # define the satellite occupation model from halotools.empirical_models import Zheng07Sats from halotools.empirical_models import NFWPhaseSpace, SubhaloPhaseSpace from intrinsic_alignments.ia_models.anisotropic_nfw_phase_space import AnisotropicNFWPhaseSpace sats_occ_model = Zheng07Sats() #sats_prof_model = AnisotropicNFWPhaseSpace() sats_prof_model = SubhaloPhaseSpace('satellites', np.logspace(10.5, 15.2, 15)) # define the alignment models from intrinsic_alignments.ia_models.ia_model_components import CentralAlignment,\ RadialSatelliteAlignment, MajorAxisSatelliteAlignment, HybridSatelliteAlignment central_orientation_model = CentralAlignment() satellite_orientation_model = RadialSatelliteAlignment() if sample_name == 'sample_1': cens_occ_model.param_dict['logMmin'] = 12.54 cens_occ_model.param_dict['sigma_logM'] = 0.26 sats_occ_model.param_dict['alpha'] = 1.0 sats_occ_model.param_dict['logM0'] = 12.68 sats_occ_model.param_dict['logM1'] = 13.48 central_orientation_model.param_dict['central_alignment_strength'] = 0.755 satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.279 elif sample_name == 'sample_2': cens_occ_model.param_dict['logMmin'] = 11.93 cens_occ_model.param_dict['sigma_logM'] = 0.26 sats_occ_model.param_dict['alpha'] = 1.0 sats_occ_model.param_dict['logM0'] = 12.05 sats_occ_model.param_dict['logM1'] = 12.85 central_orientation_model.param_dict['central_alignment_strength'] = 0.64 satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.084 elif sample_name =='sample_3': cens_occ_model.param_dict['logMmin'] = 11.61 cens_occ_model.param_dict['sigma_logM'] = 0.26 sats_occ_model.param_dict['alpha'] = 1.0 sats_occ_model.param_dict['logM0'] = 11.8 sats_occ_model.param_dict['logM1'] = 12.6 central_orientation_model.param_dict['central_alignment_strength'] = 0.57172919 satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.01995 # combine model components model_instance = HodModelFactory(centrals_occupation = cens_occ_model, centrals_profile = cens_prof_model, satellites_occupation = sats_occ_model, satellites_profile = sats_prof_model, centrals_orientation = central_orientation_model, satellites_orientation = satellite_orientation_model, model_feature_calling_sequence = ( 'centrals_occupation', 'centrals_profile', 'satellites_occupation', 'satellites_profile', 'centrals_orientation', 'satellites_orientation') ) # populate mock catalog model_instance.populate_mock(halocat) print("number of galaxies: ", len(model_instance.mock.galaxy_table)) mock = model_instance.mock.galaxy_table # galaxy coordinates and orientations coords = np.vstack((mock['x'], mock['y'], mock['z'])).T orientations = np.vstack((mock['galaxy_axisA_x'], mock['galaxy_axisA_y'], mock['galaxy_axisA_z'])).T from halotools.mock_observables import tpcf, tpcf_jackknife rbins = np.logspace(-1,1.5,15) rbin_centers = (rbins[:-1]+rbins[1:])/2.0 xi = tpcf(coords, rbins, period=halocat.Lbox) err=np.zeros(len(xi)) # save measurements fpath = fpath = PROJECT_DIRECTORY + 'modelling_illustris/data/' fname = sim_name + '_' + str(snapnum) + '-' + sample_name +'_model_xi.dat' ascii.write([rbin_centers, xi, err], fpath+fname, names=['r','xi','err'], overwrite=True)
def main(): nu_cens = np.linspace(0,1,2) nu_sats = np.linspace(-0.3,0.8, 2) # get simulation information if len(sys.argv)>1: sim_name = sys.argv[1] snapnum = int(sys.argv[2]) shape_type = sys.argv[3] sample_name = sys.argv[4] else: sim_name = 'TNG300-1' # full physics high-res run snapnum = 99 # z=0 shape_type = 'reduced' # non-reduced, reduced, iterative sample_name = 'sample_3' # load a test halo catalog from halotools.sim_manager import CachedHaloCatalog halocat = CachedHaloCatalog(simname='bolplanck', halo_finder='rockstar', redshift=0.0, dz_tol=0.1, version_name='halotools_v0p4') from halotools.empirical_models import HodModelFactory # define the central occupatoion model from halotools.empirical_models import TrivialPhaseSpace, Zheng07Cens cens_occ_model = Zheng07Cens() cens_prof_model = TrivialPhaseSpace() # define the satellite occupation model from halotools.empirical_models import Zheng07Sats from halotools.empirical_models import NFWPhaseSpace, SubhaloPhaseSpace from intrinsic_alignments.ia_models.anisotropic_nfw_phase_space import AnisotropicNFWPhaseSpace sats_occ_model = Zheng07Sats() #sats_prof_model = AnisotropicNFWPhaseSpace() sats_prof_model = SubhaloPhaseSpace('satellites', np.logspace(10.5, 15.2, 15)) # define the alignment models from intrinsic_alignments.ia_models.ia_model_components import CentralAlignment,\ RadialSatelliteAlignment, MajorAxisSatelliteAlignment, HybridSatelliteAlignment central_orientation_model = CentralAlignment() satellite_orientation_model = RadialSatelliteAlignment() if sample_name == 'sample_1': cens_occ_model.param_dict['logMmin'] = 12.54 cens_occ_model.param_dict['sigma_logM'] = 0.26 sats_occ_model.param_dict['alpha'] = 1.0 sats_occ_model.param_dict['logM0'] = 12.68 sats_occ_model.param_dict['logM1'] = 13.48 central_orientation_model.param_dict['central_alignment_strength'] = 0.755 satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.279 elif sample_name == 'sample_2': cens_occ_model.param_dict['logMmin'] = 11.93 cens_occ_model.param_dict['sigma_logM'] = 0.26 sats_occ_model.param_dict['alpha'] = 1.0 sats_occ_model.param_dict['logM0'] = 12.05 sats_occ_model.param_dict['logM1'] = 12.85 central_orientation_model.param_dict['central_alignment_strength'] = 0.64 satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.084 elif sample_name =='sample_3': cens_occ_model.param_dict['logMmin'] = 11.61 cens_occ_model.param_dict['sigma_logM'] = 0.26 sats_occ_model.param_dict['alpha'] = 1.0 sats_occ_model.param_dict['logM0'] = 11.8 sats_occ_model.param_dict['logM1'] = 12.6 central_orientation_model.param_dict['central_alignment_strength'] = 0.57172919 satellite_orientation_model.param_dict['satellite_alignment_strength'] = 0.01995 # combine model components model_instance = HodModelFactory(centrals_occupation = cens_occ_model, centrals_profile = cens_prof_model, satellites_occupation = sats_occ_model, satellites_profile = sats_prof_model, centrals_orientation = central_orientation_model, satellites_orientation = satellite_orientation_model, model_feature_calling_sequence = ( 'centrals_occupation', 'centrals_profile', 'satellites_occupation', 'satellites_profile', 'centrals_orientation', 'satellites_orientation') ) from intrinsic_alignments.utils.jackknife_observables import jackknife_ed_3d from halotools.mock_observables.alignments import ed_3d rbins = np.logspace(-1,1.5,15) rbin_centers = (rbins[:-1]+rbins[1:])/2.0 N1 = len(nu_cens) N2 = len(nu_sats) fpath = fpath = PROJECT_DIRECTORY + 'modelling_illustris/data/' fname = sim_name + '_' + str(snapnum) + '-' + sample_name +'_model_ed_grid.dat' outF = open(fpath + fname, "w") for i in range(0,N1): for j in range(0,N2): print(i, j) # assign parameters central_orientation_model.param_dict['central_alignment_strength'] = nu_cens[i] satellite_orientation_model.param_dict['satellite_alignment_strength'] = nu_sats[j] # populate mock catalog start = time.time() model_instance.populate_mock(halocat) print("time to populate mock: ", time.time() - start) mock = model_instance.mock.galaxy_table # galaxy coordinates and orientations coords = np.vstack((mock['x'], mock['y'], mock['z'])).T orientations = np.vstack((mock['galaxy_axisA_x'], mock['galaxy_axisA_y'], mock['galaxy_axisA_z'])).T # calculate ED start = time.time() ed = ed_3d(coords, orientations,coords, rbins, period=halocat.Lbox, num_threads=4) print("time to calculate ED stat: ", time.time() - start) # calculate EE start = time.time() ee = ee_3d(coords, orientations, coords, orientations, rbins, period=halocat.Lbox, num_threads=4) print("time to calculate EE stat: ", time.time() - start) s = str(nu_cens[i]) + ' ' + str(nu_sats[j]) + ' ' + np.array_str(ed)[1:-1] + ' ' + np.array_str(ee)[1:-1] outF.write(s) outF.write("\n") outF.close()
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)
-8.16055506e-01, 2.51356140e-01, 5.03253824e-01, 5.08217249e-01, 3.29184438e-01, 1.34667684e-01, 3.97669949e-01, 4.19781524e-01, 1.70968431e-01, 2.40612750e-01, 4.47443368e-01, 6.34967423e-01, 7.59893604e-01, 9.83776744e-01, 1.37576149e+00, 1.55288996e+00, 1.88033657e+00, 2.33929965e+00, 2.93594021e+00, 3.46849456e+00, 4.26576964e+00, 5.18755609e+00, 6.10833984e+00, 7.04389976e+00, 7.09565921e+00 ]]) err = np.sqrt(np.array([cov[i, i] for i in range(len(cov))])) bin_cen = (bin_edges[1:] + bin_edges[:-1]) / 2. cens_occ_model = Zheng07Cens(prim_haloprop_key='halo_vmax') cens_prof_model = TrivialPhaseSpace() sats_occ_model = Zheng07Sats(prim_haloprop_key='halo_vmax') sats_prof_model = NFWPhaseSpace() model_instance = HodModelFactory(centrals_occupation=cens_occ_model, centrals_profile=cens_prof_model, satellites_occupation=sats_occ_model, satellites_profile=sats_prof_model) halocat = CachedHaloCatalog(simname='bolplanck', redshift=0.0) model_instance.populate_mock(halocat) #log liklihood def lnlike(theta): #, wp_val, wperr, model_instance): logMmin, sigma_logM, alpha, logM0, logM1 = theta model_instance.param_dict['logMmin'] = logMmin model_instance.param_dict['sigma_logM'] = sigma_logM