How can I get a catalog of galaxies from a model in halotools?
- User
How can I get a catalog of galaxies from a model in halotools?
- Assistant
You can use the
halotools.sim_managermodule to generate a catalog of galaxies from a model. Here's an example code that shows how to generate a galaxy catalog using thehalotools.empirical_modelspackage:import numpy as np from halotools.empirical_models import PrebuiltHodModelFactory from halotools.sim_manager import CachedHaloCatalog from halotools.empirical_models import enforce_periodicity_of_box # Load a dark matter halo catalog halocat = CachedHaloCatalog(simname='bolplanck', version_name='halotools_v0p4', redshift=0, halo_finder='rockstar') # Initialize an HOD model model = PrebuiltHodModelFactory('hearin15') # Generate a mock galaxy catalog model.populate_mock(halocat) # Enforce periodic boundary conditions on the box enforce_periodicity_of_box(model.mock.galaxy_table, halocat.Lbox) # Save the catalog to a file np.savetxt('galaxy_catalog.txt', model.mock.galaxy_table)In this example, we're loading a dark matter halo catalog, initializing an HOD model using the
PrebuiltHodModelFactoryclass, and then populating the model with galaxies using thepopulate_mockmethod. Finally, we're using theenforce_periodicity_of_boxfunction to impose periodic boundary conditions on the box, and then saving the resulting galaxy catalog to a text file. - User
Now that we've used populate_mock, how can we print the galaxy table?
- Assistant
You can print the galaxy table by accessing it from the model's mock instance. Here's an example code that shows how to print the first five rows of the galaxy table in a readable format:
from halotools.empirical_models import halo_mass_to_mvir # Convert halo mass to virial mass model.mock.galaxy_table['halo_mvir'] = halo_mass_to_mvir( model.mock.galaxy_table['halo_mvir'], model.mock.galaxy_table['halo_nfw_conc'], cosmology=model.mock.cosmology) # Print first five rows of the galaxy table print(model.mock.galaxy_table[['id', 'halo_id', 'halo_mvir', 'x', 'y', 'z']].head())In this example, we're using the
halo_mass_to_mvirfunction to convert halo mass to virial mass, and then printing the first five rows of the galaxy table using theprintandheadfunctions. You can modify the list of columns to print by changing the column names inside the[['id', 'halo_id', 'halo_mvir', 'x', 'y', 'z']]argument. - User
How can I view a list of all the column names
- Assistant
You can view a list of all the column names in the galaxy table by printing the
dtype.namesattribute of thegalaxy_tableproperty of the mock object. Here's an example code that shows how to do this:# Print list of column names print(model.mock.galaxy_table.dtype.names)In this example, we're printing the
dtype.namesattribute of thegalaxy_tableproperty of the mock object, which contains a list of all the column names in the galaxy table. This list can be quite long, but it will give you a complete overview of all the available quantities you can study in your galaxy catalog. - User
How can I calculate the shear of these galaxies for shear-shear analysis
- Assistant
To calculate the shear of galaxies, you first need to obtain the convergence field associated with the dark matter halos, and then use this field to compute the shear field using appropriate numerical derivatives. Halotools does not provide a built-in method for computing gravitational lensing quantities like convergence and shear, but you can use external libraries like
pylensorlenstoolsto perform these calculations.Here's an example code that shows how to compute the convergence and shear fields using the
pylenslibrary:import numpy as np from pylens import MassSheet, Convergence, Shear # Convert halo mass to virial mass model.mock.galaxy_table['halo_mvir'] = halo_mass_to_mvir( model.mock.galaxy_table['halo_mvir'], model.mock.galaxy_table['halo_nfw_conc'], cosmology=model.mock.cosmology) # Define cosmology and box dimensions cosmo = {'H0':model.mock.cosmology.Hubble, 'Om0':model.mock.cosmology.Ob0/model.mock.cosmology.Hubble**2} boxsize = model.mock.Lbox[0] # Convert galaxy positions to physical units L = boxsize / (1.0 + model.mock.redshift) gal_pos = model.mock.galaxy_table[['x', 'y', 'z']] * L # Compute convergence map for halos halo_pos = model.mock.halo_table[['x', 'y', 'z']] * L halo_mass = model.mock.halo_table['halo_mvir'] convergence = Convergence(boxsize, gal_pos.T, halo_pos.T, halo_mass, cosmo) # Compute shear map using numerical derivatives shear = Shear.from_convergence(convergence) shear -= shear.smooth_gaussian(1.5) # Add shear components to galaxy table model.mock.galaxy_table['gamma1'] = np.array([shear.gamma1(x, y) for x, y in gal_pos]) model.mock.galaxy_table['gamma2'] = np.array([shear.gamma2(x, y) for x, y in gal_pos])In this example, we're using the
ConvergenceandShearclasses frompylensto compute the convergence and shear fields associated with the dark matter halos of the mock catalog. We then subtract a Gaussian smoothing of the shear field using a scale of 1.5 Mpc/h, which helps to remove small-scale noise from the map. Finally, we're adding the two components of the shear tensor (gamma1andgamma2) to the galaxy table. Note that thehalo_mass_to_mvirfunction is used to convert halo mass from the catalog to virial mass for use in the lensing calculations. - User
Can you tell me how to do it in lenstools?
- Assistant
Sure! Here's an example code that shows how to compute the convergence field and shear field using the
lenstoolslibrary:from lenstools import ConvergenceMap, ShearMap from lenstools.pipeline.simulation import SimulationBatch # Convert halo mass to virial mass model.mock.galaxy_table['halo_mvir'] = halo_mass_to_mvir( model.mock.galaxy_table['halo_mvir'], model.mock.galaxy_table['halo_nfw_conc'], cosmology=model.mock.cosmology) # Define cosmology and box dimensions cosmo = {'H0':model.mock.cosmology.Hubble, 'Om0':model.mock.cosmology.Ob0/model.mock.cosmology.Hubble**2} boxsize = model.mock.Lbox[0] # Convert galaxy positions to physical units L = boxsize / (1.0 + model.mock.redshift) gal_pos = model.mock.galaxy_table[['x', 'y', 'z']] * L # Compute convergence map for halos halo_pos = model.mock.halo_table[['x', 'y', 'z']] * L halo_mass = model.mock.halo_table['halo_mvir'] convergence = ConvergenceMap.from_halos(halo_pos.T,halo_mass,L=boxsize,spacing=256,cosmo=cosmo) convergence.fill(map_type="mass",kind="linear") # Compute shear map from convergence map shear = ShearMap.from_convergence(convergence) shear.fill() # Add shear components to galaxy table model.mock.galaxy_table['gamma1'] = np.array([shear.getShear(x, y, deltay=0)[0] for x, y in gal_pos]) model.mock.galaxy_table['gamma2'] = np.array([shear.getShear(x, y, deltax=0)[1] for x, y in gal_pos])In this example, we're using the
ConvergenceMapandShearMapclasses fromlenstoolsto compute the convergence and shear fields associated with the dark matter halos of the mock catalog. We first generate aConvergenceMapfrom the halo positions and masses using thefrom_halosmethod. We then generate aShearMapfrom the convergence map using thefrom_convergencemethod and fill both maps using thefillmethod. Finally, we add the two components of the shear tensor (gamma1andgamma2) to the galaxy table by iterating over thegal_posarray and calling thegetShearmethod of theShearMapobject at each galaxy position.