diff --git "a/figures/supplementary_figures/supp_fig4_prior_predictive.ipynb" "b/figures/supplementary_figures/supp_fig4_prior_predictive.ipynb" deleted file mode 100644--- "a/figures/supplementary_figures/supp_fig4_prior_predictive.ipynb" +++ /dev/null @@ -1,347 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import pickle\n", - "import torch \n", - "import sbibm\n", - "import numpy as np\n", - "from consbi.simulators import *\n", - "import matplotlib.pyplot as plt\n", - "from scipy.stats import binom\n", - "import numpy as np\n", - "from tqdm import tqdm\n", - "import os\n", - "from sbi.utils import posterior_nn\n", - "from sbi.inference import SNPE, prepare_for_sbi\n", - "from sbi.simulators.simutils import simulate_in_batches\n", - "\n", - "from consbi import DATA_PATH, RESULTS_PATH\n", - "\n", - "\n", - "from sbi.analysis import pairplot\n", - "\n", - "plt.style.use(\"plotting_settings.mplstyle\")\n", - "# Colorblind color palette\n", - "colors = ['#377eb8', '#ff7f00', '#4daf4a',\n", - " '#f781bf', '#a65628', '#984ea3',\n", - " '#999999', '#e41a1c', '#dede00']" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "regenerate_results = False\n", - "\n", - "# Load amortized posterior: hack to get default DSO rule simulator object.\n", - "ss = 50\n", - "with open(RESULTS_PATH.joinpath(\"amortized_posterior_N2000000_Default_ss50_ep1295.p\"), \"rb\") as fh:\n", - " prior, posterior, seed, simulator, density_estimator = pickle.load(fh).values()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "# Sample from prior with filter for reasonable observations.\n", - "xo = torch.tensor([[0.4300, 0.4300, 0.4200, 0.6400, 0.1700, 0.4400, 0.0900]])\n", - "xo_labels = [\"L4\", \"L4SEP\", \"L4SP\", \"L4SS\", \"L5PT\", \"L5IT\", \"L6\"]" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "# load presimated data.\n", - "with open(DATA_PATH.joinpath(\"presimulated_dso_gaussian005_n1000000.p\"), \"rb\") as fh:\n", - " prior, ths, xs = pickle.load(fh).values()\n", - " xs = xs.squeeze()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "4c25505931a7417598ebc482166839f0", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Running 1000 simulations in 50 batches.: 0%| | 0/50 [00:00" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "fig = plt.figure(figsize=(18, 9))\n", - "outer_grid = fig.add_gridspec(1, 2, wspace=-.2, hspace=0, width_ratios=[.35, .65])\n", - "data_labels = [r\"L4\", r\"L4SEP\", r\"L4SP\", r\"L4SS\", r\"L5IT\", r\"L5PT\", r\"L6\"]\n", - "\n", - "# first column\n", - "left_grid = outer_grid[0, 0].subgridspec(2, 2, wspace=0., hspace=.3, height_ratios=[0.5, 0.5])\n", - "ax1 = left_grid.subplots() \n", - "# Correlation matrix\n", - "plt.sca(ax1[0, 0])\n", - "vmin = 0.90\n", - "plt.imshow(np.corrcoef(xs.T), vmin=vmin, vmax=1.0, cmap='viridis')\n", - "cbar = plt.colorbar(shrink=.72, orientation='vertical', pad=0.04, ticks=np.linspace(vmin, 1, 3))\n", - "# cbar.ax.tick_params(labelsize=12)\n", - "plt.xticks(np.arange(7), xo_labels, rotation=45, fontsize=plt.rcParams[\"axes.labelsize\"]);\n", - "plt.yticks(np.arange(7), xo_labels, fontsize=plt.rcParams[\"axes.labelsize\"])\n", - "ax1[0, 1].remove();\n", - "\n", - "# variance decay\n", - "gs = ax1[1, 0].get_gridspec()\n", - "for axi in ax1[1, :]:\n", - " axi.remove()\n", - "ax2 = fig.add_subplot(gs[1, :])\n", - "plt.sca(ax2)\n", - "plt.errorbar(sss, stds_mean, yerr=stds_sem, fmt='-o', markersize=4)\n", - "plt.semilogx(50, get_bernoulli_std(50, xo), \"k^\", markersize=7)\n", - "ax2.spines[\"right\"].set_visible(False)\n", - "ax2.spines[\"top\"].set_visible(False)\n", - "plt.legend([\"estimated from measured data\", \"simulated data\"], frameon=False, loc=1)\n", - "plt.xlabel(\"probed connections\")\n", - "plt.ylabel(r\"standard deviation\",)\n", - "plt.xticks([10, 50, 100, 1000, 10000])\n", - "plt.yticks([0.01, 0.05, 0.10, 0.15]);\n", - "\n", - "# prior predictive\n", - "num_samples = 10000\n", - "right_grid = outer_grid[0, 1].subgridspec(7, 7, wspace=0.1, hspace=.1,)\n", - "ax3 = right_grid.subplots() \n", - "color = colors[3]\n", - "\n", - "fig, ax3 = pairplot(xs[:num_samples], points=xo.squeeze(),\n", - " limits=[[0, 1]]*7, \n", - " labels=data_labels, \n", - " hist_diag = dict(bins=15, density=True),\n", - " points_offdiag=dict(marker=\"+\", markersize=8),\n", - " points_colors=[\"k\"],\n", - " upper=\"scatter\",\n", - " diag=\"hist\",\n", - " scatter_offdiag=dict(s=3, alpha=0.6),\n", - " contour_offdiag=dict(levels=[0.35, 0.68, 0.95]),\n", - " kde_offdiag=dict(bw_method=0.3, bins=150),\n", - " samples_colors=[\"gray\"], #[colors[3]],\n", - " axes=ax3, \n", - " fig=fig,\n", - " )\n", - "\n", - "# dot, = plt.plot(0.1, \"o\", c=color, markersize=4);\n", - "# h[1] = dot\n", - "plt.sca(ax3[0,0])\n", - "plt.legend([\"simulated from prior\", \"measured\"], \n", - " bbox_to_anchor=(4.1, -6.45),\n", - "# bbox_to_anchor=(-.2, -1.),\n", - " loc=2)\n", - "# ax3[2, 0].legend(h, l, \n", - "# frameon=False, markerscale=2, \n", - "# bbox_to_anchor=(0.49, .62),\n", - "# bbox_transform=plt.gcf().transFigure);\n", - "\n", - "weight = \"bold\"\n", - "fontsize = 18\n", - "x = 0.07\n", - "y = 0.9\n", - "fig.text(x, y, \"A\", fontsize=fontsize, fontweight=weight)\n", - "fig.text(.3, y, \"B\", fontsize=fontsize, fontweight=weight)\n", - "fig.text(x, 0.48, \"C\", fontsize=fontsize, fontweight=weight);\n", - "plt.tight_layout();" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "for ftype in [\"png\", \"pdf\",]:\n", - " fig.savefig(f\"supp_fig4_prior_predictive.{ftype}\", dpi=600, bbox_inches='tight')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "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": 4 -}