diff --git "a/figures/supplementary_figures/supp_fig5-two_param_DSO_rule.ipynb" "b/figures/supplementary_figures/supp_fig5-two_param_DSO_rule.ipynb" deleted file mode 100644--- "a/figures/supplementary_figures/supp_fig5-two_param_DSO_rule.ipynb" +++ /dev/null @@ -1,317 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "0a4bb9ee", - "metadata": {}, - "outputs": [], - "source": [ - "import matplotlib.pyplot as plt\n", - "from matplotlib.gridspec import GridSpec\n", - "from matplotlib import rcParams\n", - "import numpy as np\n", - "import pickle\n", - "import sys\n", - "import torch\n", - "\n", - "sys.path.append(\"../\")\n", - "from plotting_utils import custom_marginal_plot\n", - "\n", - "from sbi.simulators import simulate_in_batches\n", - "from sbi.inference import prepare_for_sbi\n", - "from sbi.analysis import pairplot, conditional_pairplot\n", - "from sbi.inference import SNPE\n", - "\n", - "from consbi import DATA_PATH, RESULTS_PATH\n", - "\n", - "plt.style.use('../plotting_settings.mplstyle')\n", - "%matplotlib inline\n", - "\n", - "# Colorblind color palette\n", - "colors = ['#377eb8', '#ff7f00', '#4daf4a',\n", - " '#f781bf', '#a65628', '#984ea3',\n", - " '#999999', '#e41a1c', '#dede00']\n", - "import warnings\n", - "warnings.filterwarnings('ignore')" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "00a0b982", - "metadata": {}, - "outputs": [], - "source": [ - "# Load literature inference results\n", - "with open(RESULTS_PATH.joinpath(\"npe_dso_constrained_2p_gaussian_05_n500000r2x100k.p\"), \"rb\") as fh:\n", - " prior, estimator, posteriors, thos, xos, kwargs, seed = pickle.load(fh).values()\n", - " \n", - "# Notes: posteriors contains the amortized posterior and the 2nd-round one conditioned on xo." - ] - }, - { - "cell_type": "markdown", - "id": "807356ca", - "metadata": {}, - "source": [ - "## Load predictive samples and data" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "caaef6a1", - "metadata": {}, - "outputs": [], - "source": [ - "xo = np.array([[0.4300, 0.4300, 0.4200, 0.6400, 0.1700, 0.4400, 0.0900]])\n", - "obs_labels = [r\"L4\", r\"L4SEP\", r\"L4SP\", r\"L4SS\", r\"L5IT\", r\"L5PT\", r\"L6\"]\n", - "param_labels = [r\"$\\theta_{pre}$\", r\"$\\theta_{post}$\", r\"$\\theta_{postAll}$\"]" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "a2a3e833", - "metadata": {}, - "outputs": [], - "source": [ - "with open(DATA_PATH.joinpath(\"presimulated_dso_constrained_2p_gaussian_05_n500000.p\"), \"rb\") as fh: \n", - " prior, ths, xs = pickle.load(fh).values()\n", - "\n", - "ndims = ths.shape[1]" - ] - }, - { - "cell_type": "markdown", - "id": "b1efda01", - "metadata": {}, - "source": [ - "## Run SBC" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "d6e0710d", - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "d8435b216c9b4fee85e055a74d5d8695", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Running 1000 sbc samples.: 0%| | 0/1000 [00:00" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "fig = plt.figure(figsize=(18, 6))\n", - "outer_grid = fig.add_gridspec(1, 2, wspace=.2, hspace=0, width_ratios=[.4, .6])\n", - "num_samples = 10000\n", - "# set KDE values\n", - "kde_bins = 100\n", - "hist_bins = 20\n", - "handlelength = 0.8\n", - "bw_method = 0.3\n", - "dim_data = 7\n", - "dim_params = 3\n", - "\n", - "# left column\n", - "left_grid = outer_grid[0, 0].subgridspec(ndims, ndims, wspace=0.1, hspace=0.1)\n", - "ax1 = left_grid.subplots() # Create all subplots for the inner\n", - "\n", - "# right column\n", - "right_grid = outer_grid[0, 1].subgridspec(2, 1, wspace=0.1, hspace=0.4)\n", - "ax2 = right_grid[0, 0].subgridspec(1, 7, wspace=0.2, hspace=0.1).subplots()\n", - "ax3 = right_grid[1, 0].subgridspec(1, 2, wspace=0.4, hspace=0.1).subplots()\n", - "\n", - "\n", - "pairplot_opts = dict(upper=\"kde\", \n", - " diag=\"kde\",\n", - " contour_offdiag=dict(levels=[0.99]), \n", - " points_colors=[\"k\"],\n", - " points_offdiag=dict(marker=\"+\", markersize=7), \n", - " kde_offdiag=dict(bins=kde_bins, bw_method=bw_method),\n", - " kde_diag=dict(bins=kde_bins, bw_method=bw_method),\n", - " labels = param_labels,\n", - " )\n", - "\n", - "## posterior predictives DSO\n", - "fig, ax1 = pairplot([ths[:num_samples], thos[:num_samples]], \n", - "# points=xo.squeeze(), \n", - " limits=[[-1.5, 3.5] * ndims],\n", - " labels=param_labels, \n", - " points_offdiag=pairplot_opts[\"points_offdiag\"],\n", - " points_colors=[\"k\"],\n", - " upper=\"contour\", \n", - " kde_offdiag=dict(bins=kde_bins, bw_method=bw_method),\n", - " contour_offdiag=dict(levels=[0.99]),\n", - " diag=\"hist\",\n", - " hist_diag=dict(bins=hist_bins, histtype=\"step\", density=True),\n", - " samples_colors=[\"gray\", colors[0]],\n", - " axes=ax1,\n", - " fig=fig,\n", - " )\n", - "\n", - "plt.sca(ax1[0, 0])\n", - "plt.legend([\"prior\", \"posterior\", \"measured\"], \n", - " bbox_to_anchor=(.7, -.8), \n", - " handlelength=handlelength,\n", - " loc=0)\n", - "\n", - "## Posterior predictive\n", - "bins = np.linspace(0, 1, 15)\n", - "# prior predictive\n", - "custom_marginal_plot(ax2, xs.numpy(),\n", - " points=xo,\n", - " x_label=\"prior\", \n", - " points_label=\"measured\", \n", - " show_xlabels=False,\n", - "# show_tick_labels=False, # not implemented\n", - " num_bins=bins,\n", - " color=\"gray\", \n", - " histtype=\"stepfilled\",\n", - " alpha=0.4,\n", - " plot_legend=False,\n", - " )\n", - "custom_marginal_plot(ax2, xos.numpy(),\n", - " x_label=\"posterior\", \n", - " points=None,\n", - " points_label=\"measured\", \n", - " show_xlabels=True,\n", - " labels=obs_labels,\n", - "# show_tick_labels=False, # not implemented\n", - " num_bins=bins,\n", - " color=colors[0], \n", - " plot_legend=False,)\n", - "\n", - "plt.sca(ax2[-1])\n", - "plt.legend([\"measured\", \"prior\", \"posterior\"], \n", - " bbox_to_anchor=(.2, .9), \n", - " handlelength=handlelength,\n", - " loc=2)\n", - "# plt.sca(ax3[1])\n", - "# plt.axis(\"off\")\n", - "fig, ax3 = sbc_rank_plot(ranks, \n", - " num_posterior_samples=1000, \n", - " parameter_labels=param_labels, \n", - " colors=[colors[1], colors[2]],\n", - " fig=fig, \n", - " ax=ax3,\n", - " kwargs=dict(\n", - " params_in_subplots=True,\n", - "# ranks_labels=[\"\"],\n", - " uniform_region_alpha=0.2, \n", - " line_alpha=1.\n", - " ), \n", - " )\n", - "plt.sca(ax3[0])\n", - "plt.legend([])\n", - "\n", - "\n", - "# Add Letters.\n", - "weight = \"bold\"\n", - "fontsize = 18\n", - "y = 0.91\n", - "x = 0.11\n", - "dx = .315\n", - "dy = -0.43\n", - "fig.text(x, y, \"A\", fontsize=fontsize, fontweight=weight)\n", - "fig.text(x + dx, y, \"B\", fontsize=fontsize, fontweight=weight)\n", - "fig.text(x + dx, y + dy, \"C\", fontsize=fontsize, fontweight=weight);" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "980bbb4d", - "metadata": {}, - "outputs": [], - "source": [ - "for ftype in [\"png\", \"pdf\"]:\n", - " fig.savefig(f\"supp_fig5-two_param_dso.{ftype}\", dpi=600, bbox_inches='tight')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bf223641", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.7.16" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": {}, - "toc_section_display": true, - "toc_window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}