Skip to content

Commit

Permalink
Added luminosity function integration test (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillJRoper authored Oct 1, 2024
2 parents 33f0554 + 1243505 commit 55bf4c8
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 8 deletions.
97 changes: 97 additions & 0 deletions examples/cosmo/luminosity_function_integration_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""
Camels luminosity function example
==================================
Use test cosmological simulation data (from the [CAMELS simulations](
https://www.camel-simulations.org/)) to generate SDSS photometry and
plot the luminosity functions.
Note: this is an integration test, and requires the full camels
snapshot and subhalo catalogue. These can be downloaded from the
tests/data/ folder using the `download_camels.sh` script (an internet
connection is required).
"""

import matplotlib.pyplot as plt
import numpy as np

from synthesizer.conversions import lnu_to_absolute_mag
from synthesizer.emission_models import IncidentEmission
from synthesizer.filters import FilterCollection
from synthesizer.grid import Grid
from synthesizer.load_data.load_camels import load_CAMELS_IllustrisTNG


def calc_df(x, volume, binLimits):
hist, dummy = np.histogram(x, bins=binLimits)
hist = np.float64(hist)
phi = (hist / volume) / (binLimits[1] - binLimits[0])

phi_sigma = (np.sqrt(hist) / volume) / (
binLimits[1] - binLimits[0]
) # Poisson errors

return phi, phi_sigma, hist


h = 0.6711
grid_dir = "../../tests/test_grid"
grid_name = "test_grid"
grid = Grid(grid_name, grid_dir=grid_dir)
incident = IncidentEmission(grid)

filter_codes = [f"SLOAN/SDSS.{f}" for f in ["g"]]
fc = FilterCollection(filter_codes=filter_codes, new_lam=grid.lam)

gals = load_CAMELS_IllustrisTNG(
"../../tests/data/",
snap_name="CV_0_snap_086.hdf5",
group_name="CV_0_fof_subhalo_tab_086.hdf5",
)

# Filter by stellar mass
mstar_mask = np.array([np.sum(g.stars.masses) > 1e9 for g in gals])
gals = [g for g, _m in zip(gals, mstar_mask) if _m]

# Calculate g-band magnitudes
specs = np.vstack([g.stars.get_spectra(incident).lnu for g in gals])
phot = np.vstack(
[g.stars.get_photo_lnu(fc)["incident"]["SLOAN/SDSS.g"] for g in gals]
)
mags = lnu_to_absolute_mag(phot)

# Calculate g-band luminosity function
binlims = np.linspace(-25, -16, 12)
bins = binlims[:-1] + (binlims[1] - binlims[0]) / 2
phi, phi_sigma, _ = calc_df(mags, (25 / h) ** 3, binlims)

# Plot luminosity function
fig, ax = plt.subplots(1, 1)

ax.plot(bins, np.log10(phi), label="Current version")

# Previous 'working' version LF for reference
# Commit hash:
# b683b81e5de1e8c6ab6938d047cab54e7c5a2fdf
phi_previous = [
-np.inf,
-np.inf,
-4.63,
-3.63,
-3.23,
-2.66,
-2.59,
-2.85,
-2.73,
-2.89,
-3.33,
]

ax.plot(bins, phi_previous, label="Previous version", ls="dashed")

ax.legend()
ax.set_xlabel("$m_{g}^{AB}$")
ax.set_ylabel(r"$\phi \,/\, \mathrm{Mpc^{-3} \; dex^{-1}}$")

plt.show()
# plt.savefig('test_lf.png'); plt.close()
2 changes: 1 addition & 1 deletion examples/cosmo/plot_parametric_young_stars.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
model = IncidentEmission(grid)

# Select a single galaxy
gal = gals[1]
gal = gals[2]

# Age limit at which we replace star particles
age_lim = 500 * Myr
Expand Down
Binary file modified tests/data/camels_snap.hdf5
Binary file not shown.
Binary file modified tests/data/camels_subhalo.hdf5
Binary file not shown.
6 changes: 3 additions & 3 deletions tests/data/create_camels_small_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import numpy as np

_dir = "."
snap_name = "LH_1_snap_031.hdf5"
group_name = "LH_1_fof_subhalo_tab_031.hdf5"
snap_name = "CV_0_snap_086.hdf5"
group_name = "CV_0_fof_subhalo_tab_086.hdf5"
N = 10 # number of galaxies to extract
ignore_N = 1 # number of galaxies to ignore
ignore_N = 10 # number of galaxies to ignore

with h5py.File(f"{_dir}/{snap_name}", "r") as hf:
form_time = hf["PartType4/GFM_StellarFormationTime"][:]
Expand Down
8 changes: 4 additions & 4 deletions tests/data/download_camels.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash
dir=./
# $1
snap=031
snap=086
# $2
lh=1
cv=0
# $3

wget "https://users.flatironinstitute.org/~camels/Sims/IllustrisTNG/LH/LH_${lh}/snap_$snap.hdf5" --output-document "${dir}/LH_${lh}_snap_${snap}.hdf5"
wget "https://users.flatironinstitute.org/~camels/Sims/IllustrisTNG/CV/CV_${cv}/snapshot_$snap.hdf5" --output-document "${dir}/CV_${cv}_snap_${snap}.hdf5"

wget "https://users.flatironinstitute.org/~camels/FOF_Subfind/IllustrisTNG/LH/LH_${lh}/fof_subhalo_tab_$snap.hdf5" --output-document "$dir/LH_${lh}_fof_subhalo_tab_$snap.hdf5"
wget "https://users.flatironinstitute.org/~camels/FOF_Subfind/IllustrisTNG/CV/CV_${cv}/groups_$snap.hdf5" --output-document "$dir/CV_${cv}_fof_subhalo_tab_$snap.hdf5"

0 comments on commit 55bf4c8

Please sign in to comment.