{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8cf15b3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d5332cc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "import numpy\n",
    "import matplotlib\n",
    "from matplotlib import patches\n",
    "from matplotlib import pyplot\n",
    "from pycbc import waveform\n",
    "from pycbc.inference import io\n",
    "from pycbc import conversions\n",
    "\n",
    "from scripts import scatter_histograms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0dd8e4c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "pyplot.style.use('seaborn-colorblind')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "29a5dff2",
   "metadata": {},
   "outputs": [],
   "source": [
    "color_cycle = [c['color'] for c in matplotlib.rcParams['axes.prop_cycle']]      \n",
    "colors = itertools.cycle(color_cycle)\n",
    "\n",
    "# cycle around\n",
    "for _ in range(5):\n",
    "    next(colors)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7551a5f",
   "metadata": {},
   "source": [
    "# PyCBC Inference Read Samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "29d23980",
   "metadata": {},
   "outputs": [],
   "source": [
    "filenames = {'GW190521': '/work/yifan.wang/gw190521_xrerun/t3-localrun/0_700/result.hdf',\n",
    "             'GW191109': '/work/yifan.wang/testingGR-o3b/t2-o3bevents/o3btgr_output/samples_files/H1L1V1-INFERENCE_GW191109_010717-1126259200-400.hdf',\n",
    "            'GW200220':'/work/yifan.wang/testingGR-o3b/t4-mpi-massive/t2-200220-mpv1000/result.hdf'}\n",
    "samples = {}\n",
    "for lbl, fn in filenames.items():\n",
    "    fp = io.loadfile(fn, 'r')\n",
    "    s = fp.read_samples(['srcmchirp', 'q', 'parity_mpvinverse'])\n",
    "    samples[lbl] = s\n",
    "    fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a94f9251",
   "metadata": {},
   "outputs": [],
   "source": [
    "filelabels = {'GW190521': 'GW190521',\n",
    "              'GW191109': 'GW191109',\n",
    "              'GW200220': 'GW200220'\n",
    "             }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fc371486",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'GW190521': rec.array([(-27873.26400656, 83.26078987, 436.04949975, 1.43400538),\n",
       "            (-27875.2432163 , 71.57790771, 541.07468378, 1.64651789),\n",
       "            (-27875.26155477, 74.09536189, 440.63653902, 1.73495024), ...,\n",
       "            (-27880.60033757, 63.38915481, 277.02222192, 1.29027691),\n",
       "            (-27869.3461176 , 95.44301611, 381.03490203, 1.50594504),\n",
       "            (-27877.14788405, 79.98899193, 313.76083404, 1.28679349)],\n",
       "           dtype=[('loglikelihood', '<f8'), ('srcmchirp', '<f8'), ('parity_mpvinverse', '<f8'), ('q', '<f8')]),\n",
       " 'GW191109': rec.array([(-17938.0557928 , 48.73074706, 144.75730556, 1.16390601),\n",
       "            (-17942.74637522, 52.43607729, 329.71287488, 1.02073016),\n",
       "            (-17942.07580232, 41.9276795 , 180.19740616, 2.19670947), ...,\n",
       "            (-17940.19991335, 48.96967047, 405.20982651, 1.45601162),\n",
       "            (-17935.2245303 , 50.17113232, 235.30007999, 1.16774801),\n",
       "            (-17938.96972275, 53.97280568, 244.20575908, 1.28908152)],\n",
       "           dtype=[('loglikelihood', '<f8'), ('srcmchirp', '<f8'), ('parity_mpvinverse', '<f8'), ('q', '<f8')]),\n",
       " 'GW200220': rec.array([(-81869.72084721,  64.4399525 , 511.48166829, 2.134752  ),\n",
       "            (-81863.8137886 ,  89.29121051, 418.60442017, 3.00415035),\n",
       "            (-81867.14210206,  67.036858  , 239.90584564, 1.45710404), ...,\n",
       "            (-81863.32826024,  59.46329134, 606.33211039, 2.63692637),\n",
       "            (-81864.82296474,  69.8439018 , 467.65624429, 1.19154618),\n",
       "            (-81866.23224365, 104.03905068, 100.25604593, 2.5061787 )],\n",
       "           dtype=[('loglikelihood', '<f8'), ('srcmchirp', '<f8'), ('parity_mpvinverse', '<f8'), ('q', '<f8')])}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ea533ca3",
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = {'srcmchirp': waveform.parameters.srcmchirp.label,\n",
    "          'q': waveform.parameters.q.label,\n",
    "          'parity_mpvinverse':'$M_\\mathrm{PV}^{-1} (\\mathrm{GeV}^{-1})$'\n",
    "         }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d1fc164a",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 'srcmchirp'\n",
    "y = 'q'\n",
    "z = 'parity_mpvinverse'\n",
    "mins = {x: min(s[x].min() for s in samples.values()),\n",
    "        y: min(s[y].min() for s in samples.values()),\n",
    "        z: min(s[z].min() for s in samples.values())\n",
    "       }\n",
    "maxs = {x: max(s[x].max() for s in samples.values()),\n",
    "        y: min(max(s[y].max() for s in samples.values()),6),\n",
    "        z: max(s[z].max() for s in samples.values())\n",
    "       }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e95d473c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'srcmchirp': 31.170769850622765,\n",
       " 'q': 1.0000229039873965,\n",
       " 'parity_mpvinverse': 0.006938599419005487}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6ed4aeed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'srcmchirp': 149.1743322543928,\n",
       " 'q': 6,\n",
       " 'parity_mpvinverse': 999.9916720638518}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "maxs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e84b1343",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a pool for evaluating the kombine kde\n",
    "from pycbc.pool import choose_pool\n",
    "pool = choose_pool(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "37a7a798",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'GW190521': rec.array([(-27873.26400656, 83.26078987, 436.04949975, 1.43400538),\n",
       "            (-27875.2432163 , 71.57790771, 541.07468378, 1.64651789),\n",
       "            (-27875.26155477, 74.09536189, 440.63653902, 1.73495024), ...,\n",
       "            (-27880.60033757, 63.38915481, 277.02222192, 1.29027691),\n",
       "            (-27869.3461176 , 95.44301611, 381.03490203, 1.50594504),\n",
       "            (-27877.14788405, 79.98899193, 313.76083404, 1.28679349)],\n",
       "           dtype=[('loglikelihood', '<f8'), ('srcmchirp', '<f8'), ('parity_mpvinverse', '<f8'), ('q', '<f8')]),\n",
       " 'GW191109': rec.array([(-17938.0557928 , 48.73074706, 144.75730556, 1.16390601),\n",
       "            (-17942.74637522, 52.43607729, 329.71287488, 1.02073016),\n",
       "            (-17942.07580232, 41.9276795 , 180.19740616, 2.19670947), ...,\n",
       "            (-17940.19991335, 48.96967047, 405.20982651, 1.45601162),\n",
       "            (-17935.2245303 , 50.17113232, 235.30007999, 1.16774801),\n",
       "            (-17938.96972275, 53.97280568, 244.20575908, 1.28908152)],\n",
       "           dtype=[('loglikelihood', '<f8'), ('srcmchirp', '<f8'), ('parity_mpvinverse', '<f8'), ('q', '<f8')]),\n",
       " 'GW200220': rec.array([(-81869.72084721,  64.4399525 , 511.48166829, 2.134752  ),\n",
       "            (-81863.8137886 ,  89.29121051, 418.60442017, 3.00415035),\n",
       "            (-81867.14210206,  67.036858  , 239.90584564, 1.45710404), ...,\n",
       "            (-81863.32826024,  59.46329134, 606.33211039, 2.63692637),\n",
       "            (-81864.82296474,  69.8439018 , 467.65624429, 1.19154618),\n",
       "            (-81866.23224365, 104.03905068, 100.25604593, 2.5061787 )],\n",
       "           dtype=[('loglikelihood', '<f8'), ('srcmchirp', '<f8'), ('parity_mpvinverse', '<f8'), ('q', '<f8')])}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "737146e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "filecolors = {}\n",
    "fig, axd = (None, None)\n",
    "for run in samples:\n",
    "    c = next(colors)\n",
    "    filecolors[run] = c\n",
    "    s = samples[run]\n",
    "    fig, axd = scatter_histograms.create_multidim_plot(\n",
    "        ['parity_mpvinverse', 'srcmchirp'], s, labels=labels,\n",
    "        mins=mins, maxs=maxs,\n",
    "        plot_scatter=False, plot_contours=True,\n",
    "        marginal_percentiles=[], contour_percentiles=[50, 90],\n",
    "        fill_color=None, contour_color=c, hist_color=c, line_color=c,\n",
    "        contour_ls=['solid', 'dashed'], contour_labels=False, fold_masses=True,\n",
    "        use_kombine=True, kdeargs={'pool': pool, 'max_samples': 200},\n",
    "        fig=fig, axis_dict=axd,\n",
    "    )\n",
    "    \n",
    "    # add legend\n",
    "handles = []\n",
    "lbls = []\n",
    "for run, c in filecolors.items():\n",
    "    l = filelabels[run]\n",
    "    handles.append(patches.Patch(color=c, label=l))\n",
    "    lbls.append(l)\n",
    "fig.legend(loc=(0.7, 0.75), handles=handles, labels=lbls)\n",
    "fig.savefig('3events.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "95c5bcdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "color_cycle = ['#1f77b4', '#ff7f0e']      \n",
    "colors = itertools.cycle(color_cycle)\n",
    "\n",
    "# cycle around\n",
    "for _ in range(5):\n",
    "    next(colors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e69aacc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "color_cycle = [c['color'] for c in matplotlib.rcParams['axes.prop_cycle']]      \n",
    "colors = itertools.cycle(color_cycle)\n",
    "\n",
    "# cycle around\n",
    "#for _ in range(5):\n",
    "#    next(colors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5902c7f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "4ff63d8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['#0072B2', '#009E73', '#D55E00', '#CC79A7', '#F0E442', '#56B4E9']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color_cycle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2b6d8f06",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'matplotlib' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/local/user/yifan.wang/ipykernel_3285978/1511099479.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m[\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'color'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrcParams\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'axes.prop_cycle'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcolor\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'matplotlib' is not defined"
     ]
    }
   ],
   "source": [
    "[c['color'] for c in matplotlib.rcParams['axes.prop_cycle']]  \n",
    "plt.figure()\n",
    "plt.plot([1,2],color=c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "87080ba5",
   "metadata": {},
   "outputs": [],
   "source": [
    "filecolors = {}\n",
    "fig, axd = (None, None)\n",
    "for run in samples:\n",
    "    c = next(colors)\n",
    "    filecolors[run] = c\n",
    "    s = samples[run]\n",
    "    fig, axd = scatter_histograms.create_multidim_plot(\n",
    "        ['parity_mpvinverse', 'srcmchirp','q'], s, labels=labels,\n",
    "        mins=mins, maxs=maxs,\n",
    "        plot_scatter=False, plot_contours=True,\n",
    "        marginal_percentiles=[], contour_percentiles=[50, 90],\n",
    "        fill_color=None, contour_color=c, hist_color=c, line_color=c,\n",
    "        contour_ls=['solid', 'dashed'], contour_labels=False, fold_masses=True,\n",
    "        use_kombine=True, kdeargs={'pool': pool, 'max_samples': 200},\n",
    "        fig=fig, axis_dict=axd,\n",
    "    )\n",
    "    \n",
    "    # add legend\n",
    "handles = []\n",
    "lbls = []\n",
    "for run, c in filecolors.items():\n",
    "    l = filelabels[run]\n",
    "    handles.append(patches.Patch(color=c, label=l))\n",
    "    lbls.append(l)\n",
    "fig.legend(loc=(0.7, 0.75), handles=handles, labels=lbls)\n",
    "\n",
    "fig.show()\n",
    "fig.savefig('./3events-2.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "132771ea",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}