mirror of
https://github.com/Richard-Sti/csiborgtools.git
synced 2024-12-23 01:18:02 +00:00
237 lines
51 KiB
Text
237 lines
51 KiB
Text
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Using a calibrated flow model to predict $z_{\\rm cosmo}$"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Copyright (C) 2024 Richard Stiskalek\n",
|
||
|
"# This program is free software; you can redistribute it and/or modify it\n",
|
||
|
"# under the terms of the GNU General Public License as published by the\n",
|
||
|
"# Free Software Foundation; either version 3 of the License, or (at your\n",
|
||
|
"# option) any later version.\n",
|
||
|
"#\n",
|
||
|
"# This program is distributed in the hope that it will be useful, but\n",
|
||
|
"# WITHOUT ANY WARRANTY; without even the implied warranty of\n",
|
||
|
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n",
|
||
|
"# Public License for more details.\n",
|
||
|
"#\n",
|
||
|
"# You should have received a copy of the GNU General Public License along\n",
|
||
|
"# with this program; if not, write to the Free Software Foundation, Inc.,\n",
|
||
|
"# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n",
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"from h5py import File\n",
|
||
|
"\n",
|
||
|
"import csiborgtools\n",
|
||
|
"\n",
|
||
|
"%load_ext autoreload\n",
|
||
|
"%autoreload 2\n",
|
||
|
"%matplotlib inline\n",
|
||
|
"\n",
|
||
|
"paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def load_calibration(catalogue, simname, nsim, ksmooth):\n",
|
||
|
" fname = f\"/mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/flow_samples_{catalogue}_{simname}_smooth_{ksmooth}.hdf5\" # noqa\n",
|
||
|
" keys = [\"Vext_x\", \"Vext_y\", \"Vext_z\", \"alpha\", \"beta\", \"sigma_v\"]\n",
|
||
|
"\n",
|
||
|
" \n",
|
||
|
" SN_keys = ['mag_cal', 'alpha_cal', 'beta_cal']\n",
|
||
|
" calibration_samples = {}\n",
|
||
|
" with File(fname, 'r') as f:\n",
|
||
|
" for key in keys:\n",
|
||
|
" calibration_samples[key] = f[f\"sim_{nsim}/{key}\"][:]\n",
|
||
|
"\n",
|
||
|
" for key in SN_keys:\n",
|
||
|
" calibration_samples[key] = f[f\"sim_{nsim}/{key}\"][:]\n",
|
||
|
"\n",
|
||
|
" return calibration_samples"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Test running a model"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 27,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"10:11:44: reading the catalogue.\n",
|
||
|
"10:11:44: reading the interpolated field.\n",
|
||
|
"10:11:44: calculating the radial velocity.\n",
|
||
|
"Selected 125/125 galaxies.\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"/mnt/users/rstiskalek/csiborgtools/csiborgtools/flow/flow_model.py:113: UserWarning: The number of radial steps is even. Skipping the first step at 0.0 because Simpson's rule requires an odd number of steps.\n",
|
||
|
" warn(f\"The number of radial steps is even. Skipping the first \"\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"fpath_data = \"/mnt/extraspace/rstiskalek/catalogs/PV_compilation_Supranta2019.hdf5\"\n",
|
||
|
"\n",
|
||
|
"simname = \"Carrick2015\"\n",
|
||
|
"catalogue = \"Foundation\"\n",
|
||
|
"nsim = 0\n",
|
||
|
"loader = csiborgtools.flow.DataLoader(simname, 0, catalogue, fpath_data, paths, ksmooth=0)\n",
|
||
|
"\n",
|
||
|
"calibration_samples = load_calibration(catalogue, simname, nsim, 0)\n",
|
||
|
"flow_model = csiborgtools.flow.get_model(loader, zcmb_max=0.07)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 28,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"model = csiborgtools.flow.Observed2CosmologicalRedshift(calibration_samples, loader.rdist, loader._Omega_m)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 29,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
" 0%| | 0/5000 [00:00<?, ?it/s]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"100%|██████████| 5000/5000 [00:02<00:00, 1789.51it/s]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"zcosmo_mean, zcosmo_std = flow_model.predict_zcosmo_from_calibration(\n",
|
||
|
" calibration_samples[\"mag_cal\"], calibration_samples[\"alpha_cal\"], calibration_samples[\"beta_cal\"])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 38,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Marginalizing: 100%|██████████| 5000/5000 [00:02<00:00, 2048.45it/s]\n",
|
||
|
"Marginalizing: 100%|██████████| 5000/5000 [00:01<00:00, 2598.84it/s]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# n = 2 is a very good test\n",
|
||
|
"# CONVERT DEGREES TO RADIANS!!\n",
|
||
|
"n = 0\n",
|
||
|
"zcos, post = model.posterior_zcosmo(\n",
|
||
|
" loader.cat[\"z_CMB\"][n], np.deg2rad(loader.cat[\"RA\"][n]), np.deg2rad(loader.cat[\"DEC\"][n]),\n",
|
||
|
" loader.los_density[n], loader.los_radial_velocity[n])\n",
|
||
|
"zcos, post_bad = model.posterior_zcosmo(\n",
|
||
|
" loader.cat[\"z_CMB\"][n], loader.cat[\"RA\"][n], loader.cat[\"DEC\"][n],\n",
|
||
|
" loader.los_density[n], loader.los_radial_velocity[n])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 39,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnUAAAHWCAYAAAARl3+JAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/H5lhTAAAACXBIWXMAAA9hAAAPYQGoP6dpAACC9UlEQVR4nO3deVhUZf8/8PfMsAwgOwIuKKAoiAIKSppbSqgtauqTWo8alZVmy9cssye1xcQts8y0LE0tU8sy23BBccUNxV3EXRFQUfadOb8/+M1JEg1k5j4zw/t1XXMlw5nzuadRfHuvKkmSJBARERGRWVMr3QAiIiIiqjuGOiIiIiILwFBHREREZAEY6oiIiIgsAEMdERERkQVgqCMiIiKyAAx1RERERBaAoY6IiIjIAlgp3QBzoNPpcPXqVTg6OkKlUindHCIiIrIAkiQhLy8PjRs3hlpd9342hroauHr1Knx8fJRuBhEREVmgy5cvo2nTpnW+D0NdDTg6OgKo/J/u5OSkcGuIyFQVlhVi16VdsFHbwNbKVn6+qK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qxdWr14tn+Zw8+ZNdO3aFT///DNKSkpw69YtbNmyBZ06dcLVq1fh4OCAUaNG4fXXX6/Vn5e73RMAli5dik6dOt2xt56SOKeOiOge9Kteb++l+6cmTZpgy5YteHfKu5g1YxbWr1yP4wePY8aiGfAL8DNoe37//Xd8+umnACrD3e+//47o6Ghs2LDBoHWIDCU5ORmPP/74v1736KOPIjU1FWlpaXcEvn9q27Y
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.figure()\n",
|
||
|
"\n",
|
||
|
"mask = post > 1e-5\n",
|
||
|
"plt.plot(zcos[mask], post[mask], color=\"black\", label=r\"$p(z_{\\rm cosmo})$\")\n",
|
||
|
"plt.plot(zcos[mask], post_bad[mask], color=\"black\", ls=\"dashed\")\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"plt.ylim(0)\n",
|
||
|
"plt.axvline(zcosmo_mean[n], color=\"green\", label=r\"$z_{\\rm cal}$\")\n",
|
||
|
"plt.fill_betweenx([0, plt.ylim()[1]], zcosmo_mean[n] - zcosmo_std[n],\n",
|
||
|
" zcosmo_mean[n] + zcosmo_std[n], color=\"green\", alpha=0.2)\n",
|
||
|
"plt.axvline(loader.cat[\"z_CMB\"][n], color=\"red\", label=r\"$z_{\\rm CMB}$\")\n",
|
||
|
"\n",
|
||
|
"plt.xlabel(r\"$z$\")\n",
|
||
|
"plt.ylabel(r\"$p(z)$\")\n",
|
||
|
"plt.legend()\n",
|
||
|
"plt.tight_layout()\n",
|
||
|
"# plt.savefig(\"../plots/zcosmo_posterior.png\", dpi=450)\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "venv_csiborg",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.11.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|