{ "cells": [ { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Sample release for GW190412\n", "\n", "This notebook serves as a basic introduction to loading and viewing data released in associaton with the publication titled\n", "__GW190412: Observation of a Binary-Black-Hole Coalescence with Asymmetric Masses__ avaliable\n", "through [DCC](https://dcc.ligo.org/LIGO-P190412).\n", "\n", "The data used in these tutorials can be downloaded from the same [DCC page](https://dcc.ligo.org/LIGO-P190412)." ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "The released data file can be read in using the `PESummary` or `h5py` libraries. For this notebook we'll start with simple stuff using h5py. Then we'll use `PESummary v0.5.1` to read the data files as well as for plotting. 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/data/reading_the_metafile.html)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# import useful python packages\n", "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import h5py\n", "import scipy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# There is a known incompatibility between some older versions of numpy/scipy and seaborn. \n", "# Make sure you have an up-to-date version of scipy installed.\n", "# This notebook has been tested with the following versions\n", "for name, package in zip(('numpy', 'scipy', 'seaborn', 'h5py'), (np, scipy, sns, h5py)):\n", " print('{} version: {}'.format(name, package.__version__))" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "Some simple stuff with \"vanilla\" h5py" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# read in the data\n", "fn = \"posterior_samples.h5\"\n", "data = h5py.File(fn,'r')\n", "\n", "# print out parametrized waveform family names (\"approximants\" in LIGO jargon).\n", "print('approximants:',data.keys())\n", "\n", "# print out top-level data structures for one approximant. Here fore example we use the combined samples\n", "# between IMRPhenomPv3HM and SEOBNRv4PHM. The data structure is the same for all approximants.\n", "print('Top-level data structures:',data['combined'].keys())\n", "\n", "# extract posterior samples for one of the approximants\n", "posterior_samples = data['combined']['posterior_samples']\n", "print('data structures in posterior_samples:',posterior_samples.dtype)\n", "pnames = [item for item in posterior_samples.dtype.names]\n", "print('parameter names:',pnames)\n", "\n", "# get samples for one of the parameters\n", "dL = posterior_samples['luminosity_distance']\n", "print('dL shape, mean, std =',dL.shape,dL.mean(),dL.std())\n", "\n", "# smooth it\n", "from scipy.stats.kde import gaussian_kde\n", "hs = gaussian_kde(dL)\n", "\n", "# histogram, and overlay the smoothed PDF\n", "plt.figure()\n", "h, b, o = plt.hist(dL,bins=100)\n", "hsmoothed = hs(b)*len(dL)*(b[1]-b[0])\n", "plt.plot(b,hsmoothed)\n", "plt.xlabel('luminosity distance')\n", "plt.ylabel('posterior PDF')\n", "plt.show()\n", "\n", "# release memory for the data\n", "#del data" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "Now use PESummary v0.5.1 to read the data files as well as for plotting. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# import ligo-specific python packages. \n", "# pesummary is a ligo-specific python package for reading and plotting the results of Bayesian parameter estimation.\n", "# Install with \"pip install pesummary\" , and make sure you have version >= 0.3.0.\n", "import pesummary\n", "from pesummary.gw.file.read import read\n", "print(pesummary.__version__)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "There are 9 different approximants that were used to analyze __GW190412__ plus the combined samples of IMRPhenomPv3HM and SEOBNRv4PHM. They are all stored in the data file." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "fn = \"posterior_samples.h5\"\n", "data = read(fn)\n", "labels = data.labels\n", "print(labels)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "To illustrate the data structure we pick the combined posteriors and plot the respective data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "samples_dict = data.samples_dict\n", "posterior_samples = samples_dict[\"combined\"]\n", "prior_samples = data.priors[\"samples\"][\"combined\"]\n", "parameters = posterior_samples.keys()\n", "print(parameters)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "As an example, we'll show the different posterior distributions derived for a single waveform and the posterior distribution derived using the different approximants for the `luminosity_distance` parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from pesummary.core.plots.plot import _1d_histogram_plot\n", "from pesummary.gw.plots.latex_labels import GWlatex_labels\n", "\n", "parameter = \"luminosity_distance\"\n", "latex_label = GWlatex_labels[parameter]\n", "\n", "fig = _1d_histogram_plot(\n", " parameter, posterior_samples[parameter], latex_label, prior=prior_samples[parameter]\n", ")\n", "fig.set_size_inches(12, 8)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sanitized_labels = [l.replace('_', '\\_') for l in labels]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from pesummary.core.plots.plot import _1d_comparison_histogram_plot\n", "\n", "samples = []\n", "for label in labels:\n", " samples.append(samples_dict[label][parameter])\n", "\n", "\n", "colors = ['b', 'r', 'k', 'y', 'orange', 'g','purple','cyan','grey','violet']\n", "fig = _1d_comparison_histogram_plot(parameter, samples, colors, latex_label, sanitized_labels, kde=True)\n", "fig.set_size_inches(12, 8)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "Make a corner plot:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from pesummary.gw.plots.plot import _make_corner_plot\n", "\n", "fig = _make_corner_plot(posterior_samples, GWlatex_labels)\n", "plt.show()\n", "#plt.savefig(fn+'_corner.png')" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "The psds that were used for each analysis can also be extracted from this file and plotted" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from pesummary.gw.plots.plot import _psd_plot\n", "\n", "psd = data.psd[\"combined\"]\n", "ifos = list(psd.keys())\n", "frequencies, strains = [], []\n", "for ifo in ifos:\n", " frequencies.append(np.array(psd[ifo]).T[0])\n", " strains.append(np.array(psd[ifo]).T[1])\n", "fig = _psd_plot(frequencies, strains, labels=ifos, fmin=19.4)\n", "fig.set_size_inches(12, 8)\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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": 4 }