{ "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" ] }, "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 }