{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "06a835ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bilby\n",
    "from pesummary.io import read\n",
    "\n",
    "import numpy as np\n",
    "import h5py\n",
    "\n",
    "import pandas"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b2b82f7c",
   "metadata": {},
   "source": [
    "# Generate fake posteriors with chirp mass railing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3de7be31",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "17:24 bilby INFO    : No prior given, using default BBH priors in /Users/hestelles/miniconda3/envs/pyseobnr_spa/lib/python3.9/site-packages/bilby/gw/prior_files/precessing_spins_bbh.prior.\n",
      "2023-05-02  17:24:59 PESummary WARNING : Using the default load because prior_samples.hdf5 failed the following checks: is_lalinference_file, is_bilby_hdf5_file, is_tgr_pesummary_hdf5_file, is_pesummary_hdf5_file, is_pesummary_hdf5_file_deprecated, is_GWTC1_file\n",
      "2023-05-02  17:24:59 PESummary WARNING : Failed to find 'log_likelihood' in result file. Setting every sample to have log_likelihood 0\n",
      "2023-05-02  17:24:59 PESummary WARNING : Could not find f_final in input file and one was not passed from the command line. Using 1024.0Hz as default\n",
      "2023-05-02  17:24:59 PESummary WARNING : Could not find delta_f in input file and one was not passed from the command line. Using 0.00390625Hz as default\n",
      "2023-05-02  17:24:59 PESummary WARNING : Could not find minimum frequency in input file and one was not passed from the command line. Using 20.0Hz as default\n",
      "2023-05-02  17:24:59 PESummary WARNING : The redshift is being approximated using interpolation. Bear in mind that this does introduce a small error.\n",
      "2023-05-02  17:24:59 PESummary WARNING : Could not find reference_frequency in input file. Using 20Hz as default\n",
      "2023-05-02  17:25:06 PESummary INFO    : Averaging the final spin from the following fits: function bbh_final_spin_precessing_projected_UIB2016 at 0x298efa040, function bbh_final_spin_precessing_projected_Healyetal at 0x298ef7e50, function bbh_final_spin_precessing_HBR2016 at 0x298efa1f0\n",
      "2023-05-02  17:25:06 PESummary INFO    : Averaging the peak luminosity from the following fits: function bbh_peak_luminosity_non_precessing_UIB2016 at 0x298efa280, function bbh_peak_luminosity_non_precessing_Healyetal at 0x298efa310\n",
      "2023-05-02  17:25:08 PESummary INFO    : Averaging the final mass from the following fits: function bbh_final_mass_non_precessing_UIB2016 at 0x298ef7790, function bbh_final_mass_non_precessing_Healyetal at 0x298ee61f0\n"
     ]
    }
   ],
   "source": [
    "# Produce a standard BBH prior\n",
    "prior = bilby.gw.prior.BBHPriorDict()\n",
    "\n",
    "# Set Gaussians for some parameters\n",
    "prior['chirp_mass']=bilby.gw.prior.Gaussian(35.6,2.5)\n",
    "prior['mass_ratio']=bilby.gw.prior.Gaussian(0.80,0.05)\n",
    "prior['luminosity_distance']=bilby.gw.prior.Gaussian(2000.0,400.0)\n",
    "prior['network_matched_filter_snr']=bilby.gw.prior.Gaussian(20.0,3.0)\n",
    "\n",
    "# Sample from prior\n",
    "prior_samples = prior.sample(20000)\n",
    "prior_dataframe = pandas.DataFrame(prior_samples) \n",
    "\n",
    "# Cut samples to produce railing\n",
    "test_samples = prior_dataframe[prior_dataframe['chirp_mass']<36.0]\n",
    "\n",
    "# Store samples in file with format suitable for pesummary\n",
    "h = h5py.File('prior_samples.hdf5','w')\n",
    "h.create_group('posterior_samples')\n",
    "for k, v in test_samples.items():\n",
    "    h['posterior_samples'].create_dataset(k, data=np.array(v.values, dtype=np.float64))\n",
    "h.close()\n",
    "\n",
    "# Produce pesummary posteriors\n",
    "test=read('prior_samples.hdf5',package='gw')\n",
    "test.generate_all_posterior_samples()\n",
    "\n",
    "# Write final file\n",
    "test.write(filename=\"pesum_fake_mchirp_railing_right.h5\", file_format=\"pesummary\", outdir=\"./\", label=\"fake\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d8e2a8b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "17:25 bilby INFO    : No prior given, using default BBH priors in /Users/hestelles/miniconda3/envs/pyseobnr_spa/lib/python3.9/site-packages/bilby/gw/prior_files/precessing_spins_bbh.prior.\n",
      "2023-05-02  17:25:31 PESummary WARNING : Using the default load because prior_samples.hdf5 failed the following checks: is_lalinference_file, is_bilby_hdf5_file, is_tgr_pesummary_hdf5_file, is_pesummary_hdf5_file, is_pesummary_hdf5_file_deprecated, is_GWTC1_file\n",
      "2023-05-02  17:25:31 PESummary WARNING : Failed to find 'log_likelihood' in result file. Setting every sample to have log_likelihood 0\n",
      "2023-05-02  17:25:31 PESummary WARNING : Could not find f_final in input file and one was not passed from the command line. Using 1024.0Hz as default\n",
      "2023-05-02  17:25:31 PESummary WARNING : Could not find delta_f in input file and one was not passed from the command line. Using 0.00390625Hz as default\n",
      "2023-05-02  17:25:31 PESummary WARNING : Could not find minimum frequency in input file and one was not passed from the command line. Using 20.0Hz as default\n",
      "2023-05-02  17:25:31 PESummary WARNING : The redshift is being approximated using interpolation. Bear in mind that this does introduce a small error.\n",
      "2023-05-02  17:25:31 PESummary WARNING : Could not find reference_frequency in input file. Using 20Hz as default\n",
      "2023-05-02  17:25:40 PESummary INFO    : Averaging the final spin from the following fits: function bbh_final_spin_precessing_projected_UIB2016 at 0x298efa040, function bbh_final_spin_precessing_projected_Healyetal at 0x298ef7e50, function bbh_final_spin_precessing_HBR2016 at 0x298efa1f0\n",
      "2023-05-02  17:25:40 PESummary INFO    : Averaging the peak luminosity from the following fits: function bbh_peak_luminosity_non_precessing_UIB2016 at 0x298efa280, function bbh_peak_luminosity_non_precessing_Healyetal at 0x298efa310\n",
      "2023-05-02  17:25:43 PESummary INFO    : Averaging the final mass from the following fits: function bbh_final_mass_non_precessing_UIB2016 at 0x298ef7790, function bbh_final_mass_non_precessing_Healyetal at 0x298ee61f0\n"
     ]
    }
   ],
   "source": [
    "# Produce a standard BBH prior\n",
    "prior = bilby.gw.prior.BBHPriorDict()\n",
    "\n",
    "# Set Gaussians for some parameters\n",
    "prior['chirp_mass']=bilby.gw.prior.Gaussian(35.6,2.5)\n",
    "prior['mass_ratio']=bilby.gw.prior.Gaussian(0.80,0.05)\n",
    "prior['luminosity_distance']=bilby.gw.prior.Gaussian(2000.0,400.0)\n",
    "prior['network_matched_filter_snr']=bilby.gw.prior.Gaussian(20.0,3.0)\n",
    "\n",
    "# Sample from prior\n",
    "prior_samples = prior.sample(20000)\n",
    "prior_dataframe = pandas.DataFrame(prior_samples) \n",
    "\n",
    "# Cut samples to produce railing\n",
    "test_samples = prior_dataframe[prior_dataframe['chirp_mass']>34.0]\n",
    "\n",
    "# Store samples in file with format suitable for pesummary\n",
    "h = h5py.File('prior_samples.hdf5','w')\n",
    "h.create_group('posterior_samples')\n",
    "for k, v in test_samples.items():\n",
    "    h['posterior_samples'].create_dataset(k, data=np.array(v.values, dtype=np.float64))\n",
    "h.close()\n",
    "\n",
    "# Produce pesummary posteriors\n",
    "test=read('prior_samples.hdf5',package='gw')\n",
    "test.generate_all_posterior_samples()\n",
    "\n",
    "# Write final file\n",
    "test.write(filename=\"pesum_fake_mchirp_railing_left.h5\", file_format=\"pesummary\", outdir=\"./\", label=\"fake\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fffa7c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Produce a standard BBH prior\n",
    "prior = bilby.gw.prior.BBHPriorDict()\n",
    "\n",
    "# Set Gaussians for some parameters\n",
    "prior['chirp_mass']=bilby.gw.prior.Gaussian(35.6,2.5)\n",
    "prior['mass_ratio']=bilby.gw.prior.Gaussian(0.80,0.05)\n",
    "prior['luminosity_distance']=bilby.gw.prior.Gaussian(2000.0,400.0)\n",
    "prior['network_matched_filter_snr']=bilby.gw.prior.Gaussian(20.0,3.0)\n",
    "\n",
    "# Sample from prior\n",
    "prior_samples = prior.sample(20000)\n",
    "prior_dataframe = pandas.DataFrame(prior_samples) \n",
    "\n",
    "# Cut samples to produce railing\n",
    "test_samples_1 = prior_dataframe[prior_dataframe['chirp_mass']<38.0]\n",
    "test_samples = test_samples_1[test_samples_1['chirp_mass']>32.0]\n",
    "\n",
    "# Store samples in file with format suitable for pesummary\n",
    "h = h5py.File('prior_samples.hdf5','w')\n",
    "h.create_group('posterior_samples')\n",
    "for k, v in test_samples.items():\n",
    "    h['posterior_samples'].create_dataset(k, data=np.array(v.values, dtype=np.float64))\n",
    "h.close()\n",
    "\n",
    "# Produce pesummary posteriors\n",
    "test=read('prior_samples.hdf5',package='gw')\n",
    "test.generate_all_posterior_samples()\n",
    "\n",
    "# Write final file\n",
    "test.write(filename=\"pesum_fake_mchirp_railing_both.h5\", file_format=\"pesummary\", outdir=\"./\", label=\"fake\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bdfe4d5f",
   "metadata": {},
   "source": [
    "# Generate fake posteriors with mass-ratio railing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4477378c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "09:38 bilby INFO    : No prior given, using default BBH priors in /Users/hestelles/miniconda3/envs/pyseobnr_spa/lib/python3.9/site-packages/bilby/gw/prior_files/precessing_spins_bbh.prior.\n",
      "2023-05-03  09:38:20 PESummary WARNING : Using the default load because prior_samples.hdf5 failed the following checks: is_lalinference_file, is_bilby_hdf5_file, is_tgr_pesummary_hdf5_file, is_pesummary_hdf5_file, is_pesummary_hdf5_file_deprecated, is_GWTC1_file\n",
      "2023-05-03  09:38:20 PESummary WARNING : Failed to find 'log_likelihood' in result file. Setting every sample to have log_likelihood 0\n",
      "2023-05-03  09:38:20 PESummary WARNING : Could not find f_final in input file and one was not passed from the command line. Using 1024.0Hz as default\n",
      "2023-05-03  09:38:20 PESummary WARNING : Could not find delta_f in input file and one was not passed from the command line. Using 0.00390625Hz as default\n",
      "2023-05-03  09:38:20 PESummary WARNING : Could not find minimum frequency in input file and one was not passed from the command line. Using 20.0Hz as default\n",
      "2023-05-03  09:38:20 PESummary WARNING : The redshift is being approximated using interpolation. Bear in mind that this does introduce a small error.\n",
      "2023-05-03  09:38:20 PESummary WARNING : Could not find reference_frequency in input file. Using 20Hz as default\n",
      "2023-05-03  09:38:31 PESummary INFO    : Averaging the final spin from the following fits: function bbh_final_spin_precessing_projected_UIB2016 at 0x298efa040, function bbh_final_spin_precessing_projected_Healyetal at 0x298ef7e50, function bbh_final_spin_precessing_HBR2016 at 0x298efa1f0\n",
      "2023-05-03  09:38:31 PESummary INFO    : Averaging the peak luminosity from the following fits: function bbh_peak_luminosity_non_precessing_UIB2016 at 0x298efa280, function bbh_peak_luminosity_non_precessing_Healyetal at 0x298efa310\n",
      "2023-05-03  09:38:35 PESummary INFO    : Averaging the final mass from the following fits: function bbh_final_mass_non_precessing_UIB2016 at 0x298ef7790, function bbh_final_mass_non_precessing_Healyetal at 0x298ee61f0\n"
     ]
    }
   ],
   "source": [
    "# Produce a standard BBH prior\n",
    "prior = bilby.gw.prior.BBHPriorDict()\n",
    "\n",
    "# Set Gaussians for some parameters\n",
    "prior['chirp_mass']=bilby.gw.prior.Gaussian(35.6,2.5)\n",
    "prior['mass_ratio']=bilby.gw.prior.Gaussian(0.60,0.15)\n",
    "prior['luminosity_distance']=bilby.gw.prior.Gaussian(2000.0,400.0)\n",
    "prior['network_matched_filter_snr']=bilby.gw.prior.Gaussian(20.0,3.0)\n",
    "\n",
    "# Sample from prior\n",
    "prior_samples = prior.sample(20000)\n",
    "prior_dataframe = pandas.DataFrame(prior_samples) \n",
    "\n",
    "# Cut samples to produce railing\n",
    "test_samples = prior_dataframe[prior_dataframe['mass_ratio']>0.4]\n",
    "\n",
    "# Store samples in file with format suitable for pesummary\n",
    "h = h5py.File('prior_samples.hdf5','w')\n",
    "h.create_group('posterior_samples')\n",
    "for k, v in test_samples.items():\n",
    "    h['posterior_samples'].create_dataset(k, data=np.array(v.values, dtype=np.float64))\n",
    "\n",
    "h.close()\n",
    "\n",
    "# Produce pesummary posteriors\n",
    "test=read('prior_samples.hdf5',package='gw')\n",
    "test.generate_all_posterior_samples()\n",
    "\n",
    "# Write final file\n",
    "test.write(filename=\"pesum_fake_massratio_railing.h5\", file_format=\"pesummary\", outdir=\"./\", label=\"fake\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d19a952f",
   "metadata": {},
   "source": [
    "# Generate fake posteriors with distance railing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fef2872c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "09:46 bilby INFO    : No prior given, using default BBH priors in /Users/hestelles/miniconda3/envs/pyseobnr_spa/lib/python3.9/site-packages/bilby/gw/prior_files/precessing_spins_bbh.prior.\n",
      "2023-05-03  09:46:18 PESummary WARNING : Using the default load because prior_samples.hdf5 failed the following checks: is_lalinference_file, is_bilby_hdf5_file, is_tgr_pesummary_hdf5_file, is_pesummary_hdf5_file, is_pesummary_hdf5_file_deprecated, is_GWTC1_file\n",
      "2023-05-03  09:46:18 PESummary WARNING : Failed to find 'log_likelihood' in result file. Setting every sample to have log_likelihood 0\n",
      "2023-05-03  09:46:18 PESummary WARNING : Could not find f_final in input file and one was not passed from the command line. Using 1024.0Hz as default\n",
      "2023-05-03  09:46:18 PESummary WARNING : Could not find delta_f in input file and one was not passed from the command line. Using 0.00390625Hz as default\n",
      "2023-05-03  09:46:18 PESummary WARNING : Could not find minimum frequency in input file and one was not passed from the command line. Using 20.0Hz as default\n",
      "2023-05-03  09:46:18 PESummary WARNING : The redshift is being approximated using interpolation. Bear in mind that this does introduce a small error.\n",
      "2023-05-03  09:46:18 PESummary WARNING : Could not find reference_frequency in input file. Using 20Hz as default\n",
      "2023-05-03  09:46:30 PESummary INFO    : Averaging the final spin from the following fits: function bbh_final_spin_precessing_projected_UIB2016 at 0x298efa040, function bbh_final_spin_precessing_projected_Healyetal at 0x298ef7e50, function bbh_final_spin_precessing_HBR2016 at 0x298efa1f0\n",
      "2023-05-03  09:46:30 PESummary INFO    : Averaging the peak luminosity from the following fits: function bbh_peak_luminosity_non_precessing_UIB2016 at 0x298efa280, function bbh_peak_luminosity_non_precessing_Healyetal at 0x298efa310\n",
      "2023-05-03  09:46:34 PESummary INFO    : Averaging the final mass from the following fits: function bbh_final_mass_non_precessing_UIB2016 at 0x298ef7790, function bbh_final_mass_non_precessing_Healyetal at 0x298ee61f0\n"
     ]
    }
   ],
   "source": [
    "# Produce a standard BBH prior\n",
    "prior = bilby.gw.prior.BBHPriorDict()\n",
    "\n",
    "# Set Gaussians for some parameters\n",
    "prior['chirp_mass']=bilby.gw.prior.Gaussian(35.6,2.5)\n",
    "prior['mass_ratio']=bilby.gw.prior.Gaussian(0.80,0.05)\n",
    "prior['luminosity_distance']=bilby.gw.prior.Gaussian(2000.0,400.0)\n",
    "prior['network_matched_filter_snr']=bilby.gw.prior.Gaussian(20.0,3.0)\n",
    "\n",
    "# Sample from prior\n",
    "prior_samples = prior.sample(20000)\n",
    "prior_dataframe = pandas.DataFrame(prior_samples) \n",
    "\n",
    "# Cut samples to produce railing\n",
    "test_samples = prior_dataframe[prior_dataframe['luminosity_distance']<3000.0]\n",
    "\n",
    "# Store samples in file with format suitable for pesummary\n",
    "h = h5py.File('prior_samples.hdf5','w')\n",
    "h.create_group('posterior_samples')\n",
    "for k, v in test_samples.items():\n",
    "    h['posterior_samples'].create_dataset(k, data=np.array(v.values, dtype=np.float64))\n",
    "\n",
    "h.close()\n",
    "\n",
    "# Produce pesummary posteriors\n",
    "test=read('prior_samples.hdf5',package='gw')\n",
    "test.generate_all_posterior_samples()\n",
    "\n",
    "# Write final file\n",
    "test.write(filename=\"pesum_fake_distance_railing.h5\", file_format=\"pesummary\", outdir=\"./\", label=\"fake\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c7d1fe2",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
