{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "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",
    "from os.path import exists\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from corner import corner\n",
    "from getdist import plots\n",
    "import scienceplots\n",
    "from os.path import exists\n",
    "import seaborn as sns\n",
    "\n",
    "\n",
    "from reconstruction_comparison import *\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%matplotlib inline\n",
    "\n",
    "paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)\n",
    "fdir = \"/mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quick checks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "catalogue = \"CF4_TFR_i\"\n",
    "simname = \"IndranilVoid_exp\"\n",
    "zcmb_max=0.05\n",
    "sample_beta = None\n",
    "no_Vext = True\n",
    "\n",
    "fname = paths.flow_validation(\n",
    "    fdir, simname, catalogue, inference_method=\"bayes\",\n",
    "    sample_alpha=False, sample_beta=sample_beta,\n",
    "    no_Vext=no_Vext, zcmb_max=zcmb_max)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = samples_to_getdist(get_samples(fname, False), \"Test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = [\"rLG\", \"sigma_v\"]\n",
    "\n",
    "\n",
    "# params = [\"beta\", f\"a_{catalogue}\", f\"b_{catalogue}\", f\"e_mu_{catalogue}\"]\n",
    "# params = [\"Vmag\", \"l\", \"b\", \"sigma_v\", \"beta\", f\"mag_cal_{catalogue}\", f\"alpha_cal_{catalogue}\", f\"beta_cal_{catalogue}\", f\"e_mu_{catalogue}\"]\n",
    "\n",
    "with plt.style.context(\"science\"):\n",
    "    g = plots.get_subplot_plotter()\n",
    "    g.settings.figure_legend_frame = False\n",
    "    g.settings.alpha_filled_add = 0.75\n",
    "    \n",
    "    g.triangle_plot(X, params=params, filled=True, legend_loc='upper right')\n",
    "    plt.gcf().suptitle(catalogue_to_pretty(catalogue), y=1.025)\n",
    "    plt.gcf().tight_layout()\n",
    "    plt.gcf().show()\n",
    "    # plt.gcf().savefig(f\"../../plots/method_comparison_{simname}_{catalogue}.png\", dpi=500, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# catalogue = [\"LOSS\", \"Foundation\"]\n",
    "catalogue = \"CF4_TFR_i\"\n",
    "simname = \"IndranilVoid_exp\"\n",
    "zcmb_max = 0.05\n",
    "sample_alpha = False\n",
    "\n",
    "fname = paths.flow_validation(\n",
    "    fdir, simname, catalogue, inference_method=\"mike\",\n",
    "    sample_mag_dipole=True,\n",
    "    sample_beta=False,\n",
    "    sample_alpha=sample_alpha, zcmb_max=zcmb_max)\n",
    "\n",
    "\n",
    "samples = get_samples(fname, convert_Vext_to_galactic=True)\n",
    "\n",
    "samples, labels, keys = samples_for_corner(samples)\n",
    "fig = corner(samples, labels=labels, show_titles=True,\n",
    "             title_kwargs={\"fontsize\": 12}, smooth=1)\n",
    "# fig.savefig(\"../../plots/test.png\", dpi=250)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paper plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. No $V_{\\rm ext}$ and no $\\beta$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for simname in [\"IndranilVoid_exp\", \"IndranilVoid_gauss\", \"IndranilVoid_mb\"]:\n",
    "    X = []\n",
    "    for catalogue in [\"2MTF\", \"SFI_gals\", \"CF4_TFR_i\", \"CF4_TFR_w1\"]:\n",
    "\n",
    "        fname = paths.flow_validation(\n",
    "            fdir, simname, catalogue, inference_method=\"bayes\",\n",
    "            sample_alpha=False, sample_beta=None,\n",
    "            no_Vext=True, zcmb_max=0.05)\n",
    "\n",
    "        X_i = samples_to_getdist(get_samples(fname, False), catalogue_to_pretty(catalogue))\n",
    "        X.append(X_i)\n",
    "\n",
    "    params = [\"rLG\", \"sigma_v\"]\n",
    "    with plt.style.context(\"science\"):\n",
    "        g = plots.get_subplot_plotter()\n",
    "        g.settings.figure_legend_frame = False\n",
    "        g.settings.alpha_filled_add = 0.75\n",
    "\n",
    "        g.triangle_plot(X, params=params, filled=True, legend_loc='upper right')\n",
    "        plt.gcf().suptitle(simname_to_pretty(simname), y=1.025)\n",
    "        plt.gcf().tight_layout()\n",
    "        plt.gcf().show()\n",
    "        plt.gcf().savefig(f\"../../plots/void_{simname}_noVext_nobeta.png\", dpi=500, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. No  $V_{\\rm ext}$ but sampling $\\beta$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for simname in [\"IndranilVoid_exp\", \"IndranilVoid_gauss\", \"IndranilVoid_mb\"]:\n",
    "    X = []\n",
    "    for catalogue in [\"2MTF\", \"SFI_gals\", \"CF4_TFR_i\", \"CF4_TFR_w1\"]:\n",
    "\n",
    "        fname = paths.flow_validation(\n",
    "            fdir, simname, catalogue, inference_method=\"bayes\",\n",
    "            sample_alpha=False, sample_beta=True,\n",
    "            no_Vext=True, zcmb_max=0.05)\n",
    "\n",
    "        X_i = samples_to_getdist(get_samples(fname, False), catalogue_to_pretty(catalogue))\n",
    "        X.append(X_i)\n",
    "\n",
    "    params = [\"rLG\", \"sigma_v\", \"beta\"]\n",
    "    with plt.style.context(\"science\"):\n",
    "        g = plots.get_subplot_plotter()\n",
    "        g.settings.figure_legend_frame = False\n",
    "        g.settings.alpha_filled_add = 0.75\n",
    "\n",
    "        g.triangle_plot(X, params=params, filled=True, legend_loc='upper right')\n",
    "        plt.gcf().suptitle(simname_to_pretty(simname), y=1.025)\n",
    "        plt.gcf().tight_layout()\n",
    "        plt.gcf().show()\n",
    "        plt.gcf().savefig(f\"../../plots/void_{simname}_noVext_beta.png\", dpi=500, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Yes $V_{\\rm ext}$ and no $\\beta$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for simname in [\"IndranilVoid_exp\", \"IndranilVoid_gauss\", \"IndranilVoid_mb\"]:\n",
    "    X = []\n",
    "    for catalogue in [\"2MTF\", \"SFI_gals\", \"CF4_TFR_i\", \"CF4_TFR_w1\"]:\n",
    "\n",
    "        fname = paths.flow_validation(\n",
    "            fdir, simname, catalogue, inference_method=\"bayes\",\n",
    "            sample_alpha=False, sample_beta=False,\n",
    "            no_Vext=None, zcmb_max=0.05)\n",
    "\n",
    "        X_i = samples_to_getdist(get_samples(fname, False), catalogue_to_pretty(catalogue))\n",
    "        X.append(X_i)\n",
    "\n",
    "    params = [\"rLG\", \"sigma_v\", \"Vx\", \"Vy\", \"Vz\"]\n",
    "    with plt.style.context(\"science\"):\n",
    "        g = plots.get_subplot_plotter()\n",
    "        g.settings.figure_legend_frame = False\n",
    "        g.settings.alpha_filled_add = 0.75\n",
    "\n",
    "        g.triangle_plot(X, params=params, filled=True, legend_loc='upper right')\n",
    "        plt.gcf().suptitle(simname_to_pretty(simname), y=1.025)\n",
    "        plt.gcf().tight_layout()\n",
    "        plt.gcf().show()\n",
    "        plt.gcf().savefig(f\"../../plots/void_{simname}_Vext_nobeta.png\", dpi=500, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Yes $V_{\\rm ext}$ and yes $\\beta$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for simname in [\"IndranilVoid_exp\", \"IndranilVoid_gauss\", \"IndranilVoid_mb\"]:\n",
    "    X = []\n",
    "    for catalogue in [\"2MTF\", \"SFI_gals\", \"CF4_TFR_i\", \"CF4_TFR_w1\"]:\n",
    "\n",
    "        fname = paths.flow_validation(\n",
    "            fdir, simname, catalogue, inference_method=\"bayes\",\n",
    "            sample_alpha=False, sample_beta=True,\n",
    "            no_Vext=None, zcmb_max=0.05)\n",
    "\n",
    "        X_i = samples_to_getdist(get_samples(fname, False), catalogue_to_pretty(catalogue))\n",
    "        X.append(X_i)\n",
    "\n",
    "    params = [\"rLG\", \"sigma_v\", \"beta\", \"Vx\", \"Vy\", \"Vz\"]\n",
    "    with plt.style.context(\"science\"):\n",
    "        g = plots.get_subplot_plotter()\n",
    "        g.settings.figure_legend_frame = False\n",
    "        g.settings.alpha_filled_add = 0.75\n",
    "\n",
    "        g.triangle_plot(X, params=params, filled=True, legend_loc='upper right')\n",
    "        plt.gcf().suptitle(simname_to_pretty(simname), y=1.025)\n",
    "        plt.gcf().tight_layout()\n",
    "        plt.gcf().show()\n",
    "        plt.gcf().savefig(f\"../../plots/void_{simname}_Vext_beta.png\", dpi=500, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  5. $V_{\\rm ext}$ along the model axis and $\\beta = 1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_exp_2MTF_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 09:39:52\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_exp_SFI_gals_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:11:18\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_exp_CF4_TFR_i_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:01:14\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_exp_CF4_TFR_w1_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:01:20\n",
      "Removed no burn in\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2151114/3148255757.py:36: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.\n",
      "  plt.gcf().tight_layout()\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_gauss_2MTF_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 09:46:13\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_gauss_SFI_gals_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:27:30\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_gauss_CF4_TFR_i_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:09:19\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_gauss_CF4_TFR_w1_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 09:56:52\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:auto bandwidth for rLG very small or failed (h=0.0005439226807811613,N_eff=8994.407942602313). Using fallback (h=0.0028506479348732166)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Removed no burn in\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2151114/3148255757.py:36: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.\n",
      "  plt.gcf().tight_layout()\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_mb_2MTF_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:01:37\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_mb_SFI_gals_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:18:28\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_mb_CF4_TFR_i_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:23:29\n",
      "Removed no burn in\n",
      "File:          /mnt/extraspace/rstiskalek/csiborg_postprocessing/peculiar_velocity/samples_IndranilVoid_mb_CF4_TFR_w1_bayes_zcmb_max_0.05_no_Vext_sample_Vmag_vax.hdf5\n",
      "Last modified: 27/09/2024 10:40:41\n",
      "Removed no burn in\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2151114/3148255757.py:36: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.\n",
      "  plt.gcf().tight_layout()\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 99664 characters]",
      "text/plain": [
       "<Figure size 600x600 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 77376 characters]",
      "text/plain": [
       "<Figure size 600x600 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 89736 characters]",
      "text/plain": [
       "<Figure size 600x600 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def profile2vvoid(profile):\n",
    "    if \"mb\" in profile:\n",
    "        return 1586\n",
    "    elif \"gauss\" in profile:\n",
    "        return 2018\n",
    "    elif \"exp\" in profile:\n",
    "        return 2307\n",
    "    else:\n",
    "        raise ValueError(\"Invalid profile\")\n",
    "\n",
    "\n",
    "for simname in [\"IndranilVoid_exp\", \"IndranilVoid_gauss\", \"IndranilVoid_mb\"]:\n",
    "    X = []\n",
    "    for catalogue in [\"2MTF\", \"SFI_gals\", \"CF4_TFR_i\", \"CF4_TFR_w1\"]:\n",
    "\n",
    "        fname = paths.flow_validation(\n",
    "            fdir, simname, catalogue, inference_method=\"bayes\",\n",
    "            sample_alpha=False, sample_beta=None,\n",
    "            no_Vext=True, zcmb_max=0.05, sample_Vmag_vax=True)\n",
    "\n",
    "        Xi = get_samples(fname, False)\n",
    "        Xi[\"Vvoid\"] = Xi[\"Vext_axis_mag\"] - profile2vvoid(simname)\n",
    "\n",
    "        X_i = samples_to_getdist(Xi, catalogue_to_pretty(catalogue))\n",
    "\n",
    "        X.append(X_i)\n",
    "\n",
    "    params = [\"rLG\", \"sigma_v\", \"Vvoid\"]\n",
    "    with plt.style.context(\"science\"):\n",
    "        g = plots.get_subplot_plotter()\n",
    "        g.settings.figure_legend_frame = False\n",
    "        g.settings.alpha_filled_add = 0.75\n",
    "\n",
    "        g.triangle_plot(X, params=params, filled=True, legend_loc='upper right')\n",
    "        plt.gcf().suptitle(simname_to_pretty(simname), y=1.025)\n",
    "        plt.gcf().tight_layout()\n",
    "        plt.gcf().show()\n",
    "        plt.gcf().savefig(f\"../../plots/void_{simname}_Vext_along_axis_no_beta.png\", dpi=500, bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
