galsbi.sham_ot package
Submodules
galsbi.sham_ot.histograms module
- class galsbi.sham_ot.histograms.HaloHistogram(z_min=None, z_max=None, min_mass=None, max_mass=6000000000000000.0, min_time_since_merger=0, max_time_since_merger=6, n_bins_mass=500, n_bins_z=500, n_bins_time=100)[source]
Bases:
objectClass to handle histograms of halos for optimal use in the SHAM-OT framework. The class saves the following histograms:
2D histogram of redshift vs halo mass for all halos, shape (n_z_bins, n_mass_bins)
2D histogram of redshift vs halo mass for central halos, shape (n_z_bins, n_mass_bins)
3D histogram of redshift vs halo mass vs time since last merger for subhalos, shape (n_z_bins, n_mass_bins, n_time_bins)
The indexing follows: hist[z_bin_index, mass_bin_index, time_bin_index], i.e. histogram[0] gives the 0th redshift bin as a histogram over log masses.
- add_halo_catalog(halo_catalog, mass_field='m', redshift_field='z_true', is_central_field='is_halo', time_since_merger_field='delta_t', pinocchio_output_filename=None)[source]
Adds a halo catalog to the histograms. The catalog should be a recarray or dictionary-like object containing the necessary fields: - Halo mass - Redshift - Is central halo - Time since last merger
- Parameters:
halo_catalog – A recarray or dictionary-like object containing halo data.
mass_field – Field name for halo mass (default: ‘m’)
redshift_field – Field name for redshift (default: ‘z_true’)
is_central_field – Field name for central halo indicator (default: ‘is_halo’)
time_since_merger_field – Field name for time since last merger (default: ‘delta_t’)
- add_halo_histogram(halo_histogram)[source]
Adds another HaloHistogram instance to the current one.
- Parameters:
halo_histogram – Another HaloHistogram instance to add.
- get_histogram(galaxy_type='blue', model='Berner+24', **kwargs)[source]
Returns the histogram for red or blue galaxies for the specified SHAM model. Currently, the following models are supported:
“Berner+24”: free parameters m_limit and quenching time, see arXiv:2310.15223
“Peng+10”: quenching fractions based on mass, see arXiv:1003.4747
- Parameters:
galaxy_type – Type of galaxy (“blue” or “red”)
model – Model name (default: “Berner+24”)
m_limit – Mass limit for central halos in Berner+24 (default: 9e12)
quenching_time – Quenching time for subhalos in Berner+24 (default: 2)
mlim_sigma – Scatter in mass limit for extended Berner+24 (default: 0.3)
initial_quenched_fraction – Initial quenched fraction for subhalos in extended Berner+24 (default: 0.1)
quenching_time_sigma – Scatter in quenching time for extended Berner+24 (default: 0.5)
M_mu – Characteristic mass for Peng+10 (default: 11.935)
sigma – Mass scatter for Peng+10 (default: 0.51)
b – Satellite quenching efficiency for Peng+10 (default: 0.41)
- Returns:
Histogram for the specified galaxy type
- get_mass_bins()[source]
Returns the mass bin edges and log centers. :return: Array of mass bin edges and log centers.
- get_time_bins()[source]
Returns the time since last merger bin edges and centers. :return: Array of time since last merger bin edges and centers.
- get_total_counts()[source]
Returns the total counts of all histograms.
- Returns:
Tuple of total counts for all, central, and subhalo histograms.
- get_z_bins()[source]
Returns the redshift bin edges and centers. :return: Array of redshift bin edges and centers.
- property log_mass_bins
Returns the logarithmic mass bins for the histogram. If min_log_mass or max_log_mass is not set, it returns None.
- property max_log_mass
Returns the maximum log mass of the histogram. If max_mass is not set, it returns None.
- property min_log_mass
Returns the minimum log mass of the histogram. If min_mass is not set, it returns None.
- galsbi.sham_ot.histograms.load_halo_histogram(filename)[source]
Loads a HaloHistogram instance from a file.
- Parameters:
filename – Name of the file to load the instance from.
- Returns:
Loaded HaloHistogram instance.
- galsbi.sham_ot.histograms.read_pinocchio_config_details(pinocchio_output_filename)[source]
Reads the Pinocchio output file to extract details about the simulation, such as particle mass, minimum number of particles, and redshift range. :param pinocchio_output_filename: Path to the Pinocchio output file. :return: Tuple containing particle mass, minimum number of particles,
minimum redshift, and maximum redshift.
- galsbi.sham_ot.histograms.rebin_halo_histogram(halo_hist, new_n_bins_mass=None, new_n_bins_z=None, new_n_bins_time=None)[source]
Rebins a HaloHistogram to a new number of bins by combining neighboring bins. The new number of bins must be a divisor of the current number of bins.
- Parameters:
halo_hist – HaloHistogram instance to rebin
new_n_bins_mass – New number of mass bins (must divide current n_bins_mass)
new_n_bins_z – New number of redshift bins (must divide current n_bins_z)
new_n_bins_time – New number of time bins (must divide current n_bins_time)
- Returns:
New HaloHistogram instance with rebinned histograms
galsbi.sham_ot.sham_ot module
- galsbi.sham_ot.sham_ot.compute_faintest_absmag(luminosity_function, z0, z1, n_halos, pixarea, initial_guess=-20, grid_size=100)[source]
Compute the faintest absolute magnitude for a given number of halos in a given redshift range. This is done by computing the integral of the luminosity function over the redshift range and from the maximum absolute magnitude to the faintest absolute magnitude.
- Parameters:
luminosity_function – luminosity function instance
z0 – lower redshift limit
z1 – upper redshift limit
n_halos – number of halos in the redshift range
pixarea – sky area in square radians of the simulation
initial_guess – initial guess for the faintest absolute magnitude
grid_size – number of points in the redshift grid for integration
- Returns:
faintest absolute magnitude
- galsbi.sham_ot.sham_ot.compute_n_halos_given_faintest_absmag(luminosity_function, z0, z1, faintest_absmag, pixarea, grid_size=100)[source]
Compute the number of halos given the faintest absolute magnitude in a given redshift range. This is done by computing the integral of the luminosity function over the redshift range and from the maximum absolute magnitude to the faintest absolute magnitude.
- Parameters:
luminosity_function – luminosity function instance
z0 – lower redshift limit
z1 – upper redshift limit
faintest_absmag – faintest absolute magnitude
pixarea – sky area in square radians of the simulation
grid_size – number of points in the redshift grid for integration
- Returns:
number of halos in the redshift range
- galsbi.sham_ot.sham_ot.find_ot_solution(logmasses, halo_histogram, luminosity_function, z0, z1, pixarea, magnitude_bins=100, m_max=-5, faintest_mag_initial_guess=-20, epsilon=None, mu_0=9.94, alpha=-1.7, regularizer='sinkhorn')[source]
Finds the OT solution for the given mass distribution and luminosity function. Returns an interpolator predicting the absolute magnitude given the log halo mass.
- Parameters:
logmasses – log-masses of the halos at which the histogram is evaluated
halo_histogram – histogram of the halos at the given redshift
luminosity_function – luminosity function instance
z0 – lower redshift limit
z1 – upper redshift limit
pixarea – sky area in square radians of the simulation
magnitude_bins – number of bins in the absolute magnitude histogram
m_max – maximum absolute magnitude to consider (default: -5)
faintest_mag_initial_guess – initial guess for faintest magnitude
epsilon – regularization parameter for OT (None for unregularized)
mu_0 – halo mass scale parameter for cost matrix modification (Fischbacher+25)
alpha – exponent for cost matrix scaling (Fischbacher+25)
regularizer – regularizer type for OT
- Returns:
OT solution interpolator, faintest magnitude
- galsbi.sham_ot.sham_ot.generate_galaxy_histogram(luminosity_function, faintest_absmag, brightest_absmag=-27, nbins=100, redshift=0)[source]
Generate a histogram of galaxies in absolute magnitude space based on the given luminosity function and redshift.
- Parameters:
luminosity_function – luminosity function instance
faintest_absmag – faintest absolute magnitude
brightest_absmag – brightest absolute magnitude
nbins – number of bins in the histogram
redshift – redshift of the galaxies
- Returns:
absolute magnitudes, histogram
- galsbi.sham_ot.sham_ot.get_interpolated_ot_solution(ot_solution, x1, x2)[source]
Returns a deterministic interpolator for unregularized OT solution. To ensure a smooth interpolator, all constant segments in the OT solution are removed.
- Parameters:
ot_solution – OT solution matrix (n x m)
x1 – x values of the source distribution (n)
x2 – x values of the target distribution (m)
- Returns:
interpolator
- galsbi.sham_ot.sham_ot.get_regularized_ot_solution(ot_solution, x1, x2)[source]
Returns a callable for regularized OT solution that samples from probability distributions. For each query logmass, it interpolates between nearby probability distributions and samples. Vectorized implementation for efficient processing of large arrays.
- Parameters:
ot_solution – OT solution matrix (n x m)
x1 – x values of the source distribution (logmasses) (n)
x2 – x values of the target distribution (absolute magnitudes) (m)
- Returns:
callable that takes logmass and returns sampled absolute magnitude
- galsbi.sham_ot.sham_ot.sample_catalog_from_simulation(halo_catalog, ot_solution)[source]
Sample a galaxy catalog from a halo catalog using the OT solution.
- Parameters:
halo_catalog – Halo catalog (masked for redshift)
ot_solution – OT solution interpolator
zmin – lower redshift limit
zmax – upper redshift limit
- Returns:
abs. magnitudes, redshifts, ra, dec, is_halo, logmasses, time_since_merger
- galsbi.sham_ot.sham_ot.select_type(halo_catalog, is_blue, model='Berner+24', **kwargs)[source]
Select galaxies based on the SHAM model and type (blue/red). Depending on the model, different additional arguments are needed.
- For the Berner+24 model: m_limit (mass limit in Msun/h) and quenching_time
(time after which a galaxy is considered red in Gyr).
- For the Berner+24_ext model: m_limit, quenching_time,
mlim_sigma (transition width for the mass limit), initial_quenched_fraction (baseline quenched fraction at low mass for subhalos), quenching_time_sigma (transition width for the quenched fraction).
- Parameters:
halo_catalog – Halo catalog
is_blue – flag to select blue galaxies
model – SHAM model to use for selecting galaxies
Mlimit – mass limit for the SHAM model (in Msun/h)
quenching_time – time after which a galaxy is considered red (in Gyr)
M_mu – mass limit for the Peng model (in log Msun/h)
sigma – transition width from star-forming to quenched (only for Peng model
b – baseline quenched fraction at low mass for subhalos (only for Peng model)
- Returns:
boolean mask for selected galaxies
- galsbi.sham_ot.sham_ot.select_type_berner(halo_catalog, is_blue, m_limit=8000000000000.0, quenching_time=2)[source]
Select galaxies based on the Berner SHAM model. See arXiv:2310.15223 for details.
- Parameters:
halo_catalog – Halo catalog
is_blue – flag to select blue galaxies (True) or red galaxies (False)
Mlimit – mass limit for the SHAM model (in Msun/h)
quenching_time – time after which a galaxy is considered red (in Gyr)
- Returns:
boolean mask for selected galaxies
- galsbi.sham_ot.sham_ot.select_type_berner_ext(halo_catalog, is_blue, m_limit=8000000000000.0, quenching_time=2, mlim_sigma=0.2, initial_quenched_fraction=0.0, quenching_time_sigma=0.2)[source]
Select galaxies based on the extended Berner SHAM model.
- Parameters:
halo_catalog – Halo catalog
is_blue – flag to select blue galaxies (True) or red galaxies (False)
m_limit – mass limit for the SHAM model (in Msun/h)
quenching_time – time after which a galaxy is considered red (in Gyr)
mlim_sigma – transition width for the mass limit
initial_quenched_fraction – baseline quenched fraction at low mass for subhalos
quenching_time_sigma – transition width for the quenched fraction
- Returns:
boolean mask for selected galaxies
- galsbi.sham_ot.sham_ot.select_type_peng(halo_catalog, is_blue, M_mu=11.935, sigma=0.51, b=0.41)[source]
” Select galaxies based on the Peng SHAM model. See arXiv:1003.4747 for details
- Parameters:
halo_catalog – Halo catalog
is_blue – flag to select blue galaxies (True) or red galaxies (False)
M_mu – mass limit for the SHAM model (in log Msun/h)
sigma – transition width from star-forming to quenched
b – baseline quenched fraction at low mass for subhalos
- Returns:
boolean mask for selected galaxies
- galsbi.sham_ot.sham_ot.solve_ot(masses_histogram, absmags_histogram, logmasses=None, absmags=None, epsilon=None, mu_0=9.94, alpha=-1.7, regularizer='sinkhorn', norm='sqeuclidean')[source]
Solve the Optimal Transport problem between two histograms. This function assumes that the histograms are already consistently normalized. If unregularized OT is used (epsilon=None), it is not necessary to define the logmasses and absmags. This makes only a difference if the regularized OT is used.
- Parameters:
masses_histogram – Histogram of the halo masses
absmags_histogram – Histogram of the galaxy absolute magnitudes
logmasses – log-masses of the halos
absmags – absolute magnitudes of the galaxies
epsilon – Regularization parameter
mu_0 – halo mass scale parameter for cost matrix modification (Fischbacher+25)
alpha – exponent for cost matrix scaling (Fischbacher+25)
regularizer – Regularizer to use
norm – Norm to use
- Returns:
cost matrix, ot solution
galsbi.sham_ot.utils module
- galsbi.sham_ot.utils.adapted_err_function(x, mu, sigma, min_val=-1, max_val=1)[source]
Adapted error function that adapts mean, width, and limits of the classical error function.
The error function erf(x/sqrt(2)) is the probability that a normally distributed random variable with mean 0 and standard deviation 1 is less than x. The adapted error function has additinional parameters mu and sigma to adapt the mean and standard deviation of the distribution.
The range of the error function is normally [-1, 1], but can be adapted to a different range by setting min_val and max_val. If min_val and max_val are not set, the range is [-1, 1].
- Parameters:
x – input value
mu – x coordinate of the center of the error function
sigma – width of the error function
min_val – minimum value of the error function
max_val – maximum value of the error function
- Returns:
adapted error function value
- galsbi.sham_ot.utils.determine_skyarea_of_pinocchio_sim(pinocchio_output_filename)[source]
Determines the sky area of a Pinocchio simulation from the output log file.
- Parameters:
pinocchio_output_filename – Filename of the Pinocchio output log file
- Returns:
Sky area in square radians
- galsbi.sham_ot.utils.opening_to_skyarea(opening_degrees)[source]
For a given lightcone opening angle in degrees, return the corresponding sky area in radians^2. Opening = 90 degrees corresponds to half-sky, i.e. area = 2 * np.pi. Opening = 180 degrees corresponds to full-sky, i.e. area = 4 * np.pi.
- Parameters:
opening_degrees – Opening angle in degrees
- Returns:
Sky area in square radians