2024-04-01 13:44:20 +00:00
{
"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",
2024-04-01 22:19:14 +00:00
" # SN_keys = ['mag_cal', 'alpha_cal', 'beta_cal']\n",
" SN_keys = []\n",
2024-04-01 13:44:20 +00:00
" 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",
2024-04-01 22:19:14 +00:00
"execution_count": 74,
2024-04-01 13:44:20 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-04-01 22:19:14 +00:00
"23:16:25: reading the catalogue.\n",
"23:16:25: reading the interpolated field.\n",
"23:16:25: calculating the radial velocity.\n",
"Selected 100/100 galaxies.\n"
2024-04-01 13:44:20 +00:00
]
},
{
"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": [
2024-04-01 22:19:14 +00:00
"# fpath_data = \"/mnt/extraspace/rstiskalek/catalogs/PV_compilation_Supranta2019.hdf5\"\n",
"fpath_data = \"/mnt/extraspace/rstiskalek/catalogs/PV_mock_CB2_17417_small.hdf5\"\n",
2024-04-01 13:44:20 +00:00
"\n",
2024-04-01 22:19:14 +00:00
"simname = \"csiborg2_main\"\n",
"catalogue = \"CB2_small\"\n",
"# nsim = 0\n",
"loader = csiborgtools.flow.DataLoader(simname, 19 - 5, catalogue, fpath_data, paths, ksmooth=1)\n",
2024-04-01 13:44:20 +00:00
"\n",
2024-04-01 22:19:14 +00:00
"calibration_samples = load_calibration(catalogue, simname, 17417 - 500, 1)\n",
2024-04-01 13:44:20 +00:00
"flow_model = csiborgtools.flow.get_model(loader, zcmb_max=0.07)"
]
},
{
"cell_type": "code",
2024-04-01 22:19:14 +00:00
"execution_count": 75,
2024-04-01 13:44:20 +00:00
"metadata": {},
"outputs": [],
"source": [
"model = csiborgtools.flow.Observed2CosmologicalRedshift(calibration_samples, loader.rdist, loader._Omega_m)"
]
},
{
"cell_type": "code",
2024-04-01 22:19:14 +00:00
"execution_count": 76,
2024-04-01 13:44:20 +00:00
"metadata": {},
2024-04-01 22:19:14 +00:00
"outputs": [],
2024-04-01 13:44:20 +00:00
"source": [
2024-04-01 22:19:14 +00:00
"# zcosmo_mean, zcosmo_std = flow_model.predict_zcosmo_from_calibration(\n",
" # calibration_samples[\"mag_cal\"], calibration_samples[\"alpha_cal\"], calibration_samples[\"beta_cal\"])\n",
"zcosmo_mean = loader.cat[\"zcosmo\"]"
2024-04-01 13:44:20 +00:00
]
},
{
"cell_type": "code",
2024-04-01 22:19:14 +00:00
"execution_count": 77,
2024-04-01 13:44:20 +00:00
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2024-04-01 22:19:14 +00:00
"Marginalizing: 0%| | 0/5000 [00:00<?, ?it/s]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Marginalizing: 100%|██████████| 5000/5000 [00:02<00:00, 2262.93it/s]\n"
2024-04-01 13:44:20 +00:00
]
}
],
"source": [
"# n = 2 is a very good test\n",
"# CONVERT DEGREES TO RADIANS!!\n",
2024-04-01 22:19:14 +00:00
"n = 40\n",
2024-04-01 13:44:20 +00:00
"zcos, post = model.posterior_zcosmo(\n",
2024-04-01 22:19:14 +00:00
" loader.cat[\"zobs\"][n], np.deg2rad(loader.cat[\"RA\"][n]), np.deg2rad(loader.cat[\"DEC\"][n]),\n",
2024-04-01 13:44:20 +00:00
" loader.los_density[n], loader.los_radial_velocity[n])\n",
2024-04-01 22:19:14 +00:00
"# zcos, post_bad = model.posterior_zcosmo(\n",
"# loader.cat[\"zobs\"][n], loader.cat[\"RA\"][n], loader.cat[\"DEC\"][n],\n",
"# loader.los_density[n], loader.los_radial_velocity[n])"
2024-04-01 13:44:20 +00:00
]
},
{
"cell_type": "code",
2024-04-01 22:19:14 +00:00
"execution_count": 78,
2024-04-01 13:44:20 +00:00
"metadata": {},
"outputs": [
{
"data": {
2024-04-01 22:19:14 +00:00
"image/png": "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K/O1vfwPA4Vii5pKWloauXbvinXfewfDhwxESEgKdTofdu3cbilmPHj3Qu3dvbN68GUDVxJAPP/wQ165dQ79+/eDp6WlYjh8/Xq/P79y5M9LS0tC9e3dER0djyZIlAFCtLyiVSsPvlUpltbOLtSktLcWWLVswceLEeuVpEInuqbCwUAIgFRYWio5CREZ25swZycHBQQIgzZw5847vAZBUKpWUm5srKKG8FJUVSXgPEt6DVFRWJDoOSZIkFRVJElC1FBVJN2/elE6ePCndvHlTdLJ6mzx5sjRmzJh7rrdt2zbJ399f0ul091z3xIkTUkBAgFRQUCBJkiTl5eVJhw4dkoKDg6XS0lIpPz9f8vX1la5evSpduXLF8Oe2ceNGacKECdKFCxek4OBgw/aGDx8u7dq1S5IkSUpOTpaeeOIJSZKkWrcpSZL02WefSY888shdc97tuNWnh8jqGjsiouZUWlqKkSNHoqioCIMGDUJsbGy173fu3BkhISE4fPgwvv76a0yePFlQUiLLkJaWVqdZ6E888QTOnj2LK1euwNvb+67rBgQEYNq0aRgwYACsrKzw6KOPYsGCBXj22WcRHBwMhUKB999/H56entixYwfefPNNqFQqtGjRAv/5z3/qnD0kJKTGbQKAtbU1Fi9eXOdtNYZCkiSpWT7JhGm1Wjg5OaGwsLDalGsiMm2TJk3C8uXL0bp1a6SlpdV4b6yPP/4Y0dHR6N+/P/bt2ycgpbwUlxf
2024-04-01 13:44:20 +00:00
"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",
2024-04-01 22:19:14 +00:00
"# plt.plot(zcos[mask], post_bad[mask], color=\"black\", ls=\"dashed\")\n",
2024-04-01 13:44:20 +00:00
"\n",
"\n",
"plt.ylim(0)\n",
"plt.axvline(zcosmo_mean[n], color=\"green\", label=r\"$z_{\\rm cal}$\")\n",
2024-04-01 22:19:14 +00:00
"# 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[\"zobs\"][n], color=\"red\", label=r\"$z_{\\rm CMB}$\")\n",
2024-04-01 13:44:20 +00:00
"\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
}