diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d81bd9ff3d763ed7bafd3cfeccca09b0bebb953..169ab1ac37537eb82430a965d5b658cb33442a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changes currently on master, but not under a tag. ### Changes - Make BBH/BNS parameter conversion more logical - Source frame masses/spins included in posterior +- Make filling in posterior with fixed parameters work ## [0.3] 2018-01-02 diff --git a/bilby/core/result.py b/bilby/core/result.py index 7f8d7550610e4774d1983c6af923f6720184c975..ad78abe7d6a852ada6e1df78b2e753ad7305438d 100644 --- a/bilby/core/result.py +++ b/bilby/core/result.py @@ -196,6 +196,12 @@ class Result(dict): if dictionary.get('priors', False): dictionary['priors'] = {key: str(self.priors[key]) for key in self.priors} + # Convert callable kwargs to strings to avoid pickling issues + if hasattr(self, 'kwargs'): + for key in self.kwargs: + if hasattr(self.kwargs[key], '__call__'): + self.kwargs[key] = str(self.kwargs[key]) + try: deepdish.io.save(file_name, dictionary) except Exception as e: @@ -544,12 +550,14 @@ class Result(dict): if hasattr(self, 'posterior') is False: data_frame = pd.DataFrame( self.samples, columns=self.search_parameter_keys) - data_frame['log_likelihood'] = getattr( - self, 'log_likelihood_evaluations', np.nan) for key in priors: - if getattr(priors[key], 'is_fixed', False): + if isinstance(priors[key], DeltaFunction): data_frame[key] = priors[key].peak - # We save the samples in the posterior and remove the array of samples + elif isinstance(priors[key], float): + data_frame[key] = priors[key] + data_frame['log_likelihood'] = getattr( + self, 'log_likelihood_evaluations', np.nan) + # remove the array of samples del self.samples else: data_frame = self.posterior diff --git a/bilby/core/sampler/base_sampler.py b/bilby/core/sampler/base_sampler.py index b0d996f3f490fc38406fdd5a84f217cda4bf9abe..fdbc5a32553d2fc3c211a82df07066456dcf384b 100644 --- a/bilby/core/sampler/base_sampler.py +++ b/bilby/core/sampler/base_sampler.py @@ -399,6 +399,40 @@ class Sampler(object): class NestedSampler(Sampler): npoints_equiv_kwargs = ['nlive', 'nlives', 'n_live_points', 'npoints', 'npoint', 'Nlive'] + def reorder_loglikelihoods(self, unsorted_loglikelihoods, unsorted_samples, + sorted_samples): + """ Reorders the stored log-likelihood after they have been reweighted + + This creates a sorting index by matching the reweights `result.samples` + against the raw samples, then uses this index to sort the + loglikelihoods + + Parameters + ---------- + sorted_samples, unsorted_samples: array + Sorted and unsorted values of the samples. These should be of the same + shape and contain the same sample values, but in different orders + unsorted_loglikelihoods: array + The loglikelihoods corresponding to the unsorted_samples + + Returns + ------- + sorted_loglikelihoods: array + The loglikelihoods reordered to match that of the sorted_samples + + + """ + + idxs = [] + for ii in range(len(unsorted_loglikelihoods)): + idx = np.where(np.all(sorted_samples[ii] == unsorted_samples, axis=1)) + if len(idx) > 1: + raise ValueError( + "Multiple matches found between sorted and unsorted samples") + else: + idxs.append(idx[0]) + return unsorted_loglikelihoods[idxs] + class MCMCSampler(Sampler): nwalkers_equiv_kwargs = ['nwalker', 'nwalkers', 'draws'] diff --git a/bilby/core/sampler/dynesty.py b/bilby/core/sampler/dynesty.py index 62f2171234df56af13660b8caa602b5d6813c525..1f5129992c8527715974ffb2a29938fbd6788e14 100644 --- a/bilby/core/sampler/dynesty.py +++ b/bilby/core/sampler/dynesty.py @@ -5,7 +5,7 @@ import sys import numpy as np from pandas import DataFrame from deepdish.io import load, save -from ..utils import logger +from ..utils import logger, check_directory_exists_and_if_not_mkdir from .base_sampler import Sampler, NestedSampler @@ -164,9 +164,9 @@ class Dynesty(NestedSampler): out.samples, columns=self.search_parameter_keys) self.result.nested_samples['weights'] = weights self.result.nested_samples['log_likelihood'] = out.logl - idxs = [np.unique(np.where(self.result.samples[ii] == out.samples)[0]) - for ii in range(len(out.logl))] - self.result.log_likelihood_evaluations = out.logl[idxs] + self.result.log_likelihood_evaluations = self.reorder_loglikelihoods( + unsorted_loglikelihoods=out.logl, unsorted_samples=out.samples, + sorted_samples=self.result.samples) self.result.log_evidence = out.logz[-1] self.result.log_evidence_err = out.logzerr[-1] @@ -282,6 +282,7 @@ class Dynesty(NestedSampler): sampler: `dynesty.NestedSampler` NestedSampler to write to disk. """ + check_directory_exists_and_if_not_mkdir(self.outdir) resume_file = '{}/{}_resume.h5'.format(self.outdir, self.label) if os.path.isfile(resume_file): diff --git a/bilby/core/sampler/nestle.py b/bilby/core/sampler/nestle.py index aa2364462b6120b1bff91089c1f297f7b1c0b6b6..8bcfca7d480192b99e9091babd0c7bf83f2097d9 100644 --- a/bilby/core/sampler/nestle.py +++ b/bilby/core/sampler/nestle.py @@ -64,9 +64,9 @@ class Nestle(NestedSampler): out.samples, columns=self.search_parameter_keys) self.result.nested_samples['weights'] = out.weights self.result.nested_samples['log_likelihood'] = out.logl - idxs = [np.unique(np.where(self.result.samples[ii] == out.samples)[0]) - for ii in range(len(out.logl))] - self.result.log_likelihood_evaluations = out.logl[idxs] + self.result.log_likelihood_evaluations = self.reorder_loglikelihoods( + unsorted_loglikelihoods=out.logl, unsorted_samples=out.samples, + sorted_samples=self.result.samples) self.result.log_evidence = out.logz self.result.log_evidence_err = out.logzerr return self.result diff --git a/examples/injection_examples/basic_tutorial.py b/examples/injection_examples/basic_tutorial.py index f4b5011a53bfa5d7035d1e5816dcd81a1b08fd95..2f3766f8adfd656b63e22b706fe80b5fc54d3bf9 100644 --- a/examples/injection_examples/basic_tutorial.py +++ b/examples/injection_examples/basic_tutorial.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate running parameter estimation on a reduced parameter space for an injected signal. diff --git a/examples/injection_examples/binary_neutron_star_example.py b/examples/injection_examples/binary_neutron_star_example.py index 4026c966667cc72c09397a33eb221db9e75bfe26..76cf0c1ec9d27f79099f2798df6847a963070ee1 100644 --- a/examples/injection_examples/binary_neutron_star_example.py +++ b/examples/injection_examples/binary_neutron_star_example.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate running parameter estimation on a binary neutron star system taking into account tidal deformabilities. diff --git a/examples/injection_examples/calibration_example.py b/examples/injection_examples/calibration_example.py index d111d3896bbdc4d2839e79b84c759a9f003254c8..9f2052ffb55d019aa1509ae251aa6f57d4bc9c14 100644 --- a/examples/injection_examples/calibration_example.py +++ b/examples/injection_examples/calibration_example.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate running parameter estimation with calibration uncertainties included. diff --git a/examples/injection_examples/create_your_own_source_model.py b/examples/injection_examples/create_your_own_source_model.py index f304b97abb0d3e418d8b93ff3a41b4d56e32f3a5..254fdfc52f4dd76aed5df74fbb9b31c79b134787 100644 --- a/examples/injection_examples/create_your_own_source_model.py +++ b/examples/injection_examples/create_your_own_source_model.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ A script to demonstrate how to use your own source model """ diff --git a/examples/injection_examples/create_your_own_time_domain_source_model.py b/examples/injection_examples/create_your_own_time_domain_source_model.py index ac45e1ef987ec27868d643ce590b74debd481c53..5ea57fb58897f5e0cace5f9a9ad22c40c0ba3afb 100644 --- a/examples/injection_examples/create_your_own_time_domain_source_model.py +++ b/examples/injection_examples/create_your_own_time_domain_source_model.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python """ A script to show how to create your own time domain source model. A simple damped Gaussian signal is defined in the time domain, injected into noise in diff --git a/examples/injection_examples/eccentric_inspiral.py b/examples/injection_examples/eccentric_inspiral.py index 235536ca48482e4bf39d6e58f7533f9822f22239..75ff93b572e85f9e2abfae62b341057d9c51b2f3 100644 --- a/examples/injection_examples/eccentric_inspiral.py +++ b/examples/injection_examples/eccentric_inspiral.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate running parameter estimation on a reduced parameter space for an injected eccentric binary black hole signal with masses & distnace similar diff --git a/examples/injection_examples/how_to_specify_the_prior.py b/examples/injection_examples/how_to_specify_the_prior.py index 706bdaedc1da97fde24db50b10ded0a56ec0c086..b92288d90237f57fceb84b48704ed296e67bf9bf 100644 --- a/examples/injection_examples/how_to_specify_the_prior.py +++ b/examples/injection_examples/how_to_specify_the_prior.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate how to specify the prior distributions used for parameter estimation. """ diff --git a/examples/injection_examples/marginalized_likelihood.py b/examples/injection_examples/marginalized_likelihood.py index 300649719a3519999ed78eab6256a18f5c98367f..83259d627e80909796d646be386ced0fbd2e2e99 100644 --- a/examples/injection_examples/marginalized_likelihood.py +++ b/examples/injection_examples/marginalized_likelihood.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate how to improve the speed and efficiency of parameter estimation on an injected signal using phase and distance marginalisation. diff --git a/examples/injection_examples/plot_time_domain_data.py b/examples/injection_examples/plot_time_domain_data.py index d03c3200a4f5303c3ecad0f045701dd0e1521efd..f3d056b7a9a594a328a200c84ac334ec50775093 100644 --- a/examples/injection_examples/plot_time_domain_data.py +++ b/examples/injection_examples/plot_time_domain_data.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ """ from __future__ import division, print_function diff --git a/examples/injection_examples/sine_gaussian_example.py b/examples/injection_examples/sine_gaussian_example.py index c63691af107ab8b9ac680fe034da0f30b4f5f581..4bbf7c6b0181c28212c26731acbff17539c29f28 100644 --- a/examples/injection_examples/sine_gaussian_example.py +++ b/examples/injection_examples/sine_gaussian_example.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate running parameter estimation on a sine gaussian injected signal. diff --git a/examples/injection_examples/using_gwin.py b/examples/injection_examples/using_gwin.py index f6f0e28d640c45a9fbbc7fe9c47286e9568df0da..a213c490c6a30bd51dda582baa1675b6c300f3c3 100644 --- a/examples/injection_examples/using_gwin.py +++ b/examples/injection_examples/using_gwin.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ An example of how to use bilby with `gwin` (https://github.com/gwastro/gwin) to perform CBC parameter estimation. diff --git a/examples/open_data_examples/GW150914.py b/examples/open_data_examples/GW150914.py index b0876d243a6634ff76368a72bdcf6cdcf465a625..aa5ca44af19f6575feb8ed7e8b4e373b001bec0a 100644 --- a/examples/open_data_examples/GW150914.py +++ b/examples/open_data_examples/GW150914.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate running parameter estimation on GW150914 using open data. diff --git a/examples/open_data_examples/GW150914_minimal.py b/examples/open_data_examples/GW150914_minimal.py index 339eb3b13558c9b5f05e31916e150c6db6da3b30..520fe6ae9aa3c7428207e6880660fc62dbc9f827 100644 --- a/examples/open_data_examples/GW150914_minimal.py +++ b/examples/open_data_examples/GW150914_minimal.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ Tutorial to demonstrate the minimum number of steps required to run parameter stimation on GW150914 using open data. diff --git a/examples/other_examples/add_multiple_results.py b/examples/other_examples/add_multiple_results.py index b4f99ba9669ae95ab7adb3372b007f41fd840086..f4c565555ad6250390622cd71fd1f433a7599c64 100644 --- a/examples/other_examples/add_multiple_results.py +++ b/examples/other_examples/add_multiple_results.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ An example of running two sets of posterior sample estimations and adding them """ diff --git a/examples/other_examples/gaussian_example.py b/examples/other_examples/gaussian_example.py index 318222e2d1fe62b003bc543ceb5c8c1aedc95d9d..bf7730286f12ddc70ac9bfe647e6e637fd4004ac 100644 --- a/examples/other_examples/gaussian_example.py +++ b/examples/other_examples/gaussian_example.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ An example of how to use bilby to perform paramater estimation for non-gravitational wave data consisting of a Gaussian with a mean and variance diff --git a/examples/other_examples/get_LOSC_event_data.py b/examples/other_examples/get_LOSC_event_data.py index 7a8836ba110f881e687860a718b0c4092c734a35..6197426eb6e02869fb73e18b349d8dde92ca1fc5 100644 --- a/examples/other_examples/get_LOSC_event_data.py +++ b/examples/other_examples/get_LOSC_event_data.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python """ Helper script to faciliate downloading data from LOSC Usage: To download the GW150914 data from https://losc.ligo.org/events/ diff --git a/examples/other_examples/linear_regression_pymc3.py b/examples/other_examples/linear_regression_pymc3.py index 3328d8cf5722fcb5f3db5047f6d423ff645285f7..c10d5d3c93480254bc6ca2fa4d05ea128f0e2718 100644 --- a/examples/other_examples/linear_regression_pymc3.py +++ b/examples/other_examples/linear_regression_pymc3.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ An example of how to use bilby to perform paramater estimation for non-gravitational wave data. In this case, fitting a linear function to diff --git a/examples/other_examples/linear_regression_pymc3_custom_likelihood.py b/examples/other_examples/linear_regression_pymc3_custom_likelihood.py index 2f36967947d1ff0d4087758bb3070b8aa37569f9..9321ea4c82621250201b3af8b97484845cd2e9a7 100644 --- a/examples/other_examples/linear_regression_pymc3_custom_likelihood.py +++ b/examples/other_examples/linear_regression_pymc3_custom_likelihood.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ An example of how to use bilby to perform paramater estimation for non-gravitational wave data. In this case, fitting a linear function to diff --git a/examples/other_examples/occam_factor_example.py b/examples/other_examples/occam_factor_example.py index b164a3298b55fc54ff6225a19136480c63521b73..b977eaeadf53086326ca4047a8a9aa64bb607b0c 100644 --- a/examples/other_examples/occam_factor_example.py +++ b/examples/other_examples/occam_factor_example.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/env python """ As part of the :code:`bilby.result.Result` object, we provide a method to