-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added luminosity function integration test (#741)
- Loading branch information
Showing
6 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |