{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7bf6a16e",
   "metadata": {},
   "source": [
    "# Sample release\n",
    "This notebook serves as a basic introduction to loading and viewing data\n",
    "released in associaton with the publication titled GW190814: Gravitational Waves from the Coalescence of a 23 Msun Black Hole with a 2.6 Msun Compact Object.\n",
    "\n",
    "The released data file can be read in using the PESummary or h5py libraries. For general instructions on how to manipulate the data file and/or read this data file with h5py, see the [PESummary docs](https://lscsoft.docs.ligo.org/pesummary)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69b4e99d",
   "metadata": {},
   "source": [
    "First we import the key python modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7e30586",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import pesummary\n",
    "from pesummary.io import read\n",
    "print(pesummary.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbd34a4d",
   "metadata": {},
   "source": [
    "As part of this sample release, we are releasing the posterior samples generated from 15 different analyses. The samples for each analysis is stored in the data file. This data file can be read in using the 'pesummary' read function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ca8de71",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_name = 'GW190814.h5'\n",
    "data = read(file_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17f44804",
   "metadata": {},
   "source": [
    "The posterior samples can be extracted through the `samples_dict` property. These posterior samples are stored in a custom table structure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74f622d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "samples_dict = data.samples_dict\n",
    "posterior_samples = samples_dict['combined']\n",
    "parameters = list(posterior_samples.keys())\n",
    "print(parameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9a2309d",
   "metadata": {},
   "source": [
    "## combined analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d129c699",
   "metadata": {},
   "source": [
    "'pesummary' allows for the user to easily make plots. As an example, we show the posterior distribution for 'mass_2_source' plotted as a KDE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1db9c81e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = posterior_samples.plot('mass_2_source', type='hist', kde=True)\n",
    "fig.set_size_inches(12, 8)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074bd711",
   "metadata": {},
   "source": [
    "We may also easily generate a spin disk, showing the most probable direction of the spin vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f6a5f52",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = posterior_samples.plot(type='spin_disk', colorbar=True, annotate=True, show_label=True, cmap='Blues')\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b57bf70",
   "metadata": {},
   "source": [
    "Corner plots are very useful for spotting degeneracies between parameters. A corner plot can easily be generated using 'pesummary'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ab6aac9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = posterior_samples.plot(type='corner', parameters=['mass_1', 'mass_2', 'luminosity_distance', 'iota'])\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1db9f6aa",
   "metadata": {},
   "source": [
    "## Comparing multiple analyses"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "057cb7a1",
   "metadata": {},
   "source": [
    "As the 'pesummary' file is able to store multiple analyses in a single file, we are able to easily generate a comparison plot showing the posterior distribution for 'mass_2_source' for each analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5f34176",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = samples_dict.plot('mass_2_source', type='hist', kde=True)\n",
    "fig.set_size_inches(12, 8)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d94006e0",
   "metadata": {},
   "source": [
    "A comparison histogram is not the only way to display this data. We may also generate a violin plot showing the posterior distribution for each analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35db867f",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = samples_dict.plot('mass_2_source', type='violin')\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7797f48",
   "metadata": {},
   "source": [
    "'pesummary' also allows for the user to generate a triangle plot with ease"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a4ff3f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = samples_dict.plot(['mass_1', 'mass_2'], type='reverse_triangle', grid=False)\n",
    "fig[0].show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fe3a16d",
   "metadata": {},
   "source": [
    "It is also useful to see how degeneracies between certain parameters change for different analysis. This can be investigated by generating a comparison corner plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bcbbc55",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = samples_dict.plot(type='corner', parameters=['mass_1', 'mass_2', 'luminosity_distance', 'iota'])\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e276aaa",
   "metadata": {},
   "source": [
    "## PSD data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "979736cf",
   "metadata": {},
   "source": [
    "The 'pesummary' file also stores the PSD that was used for each analysis. This can be extracted and plotted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ae03c1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "psd = data.psd['combined']\n",
    "fig = psd.plot(fmin=30)\n",
    "fig.show()"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}