Maintenance will be performed on git.ligo.org, chat.ligo.org, containers.ligo.org, and docs.ligo.org on Tuesday 7th July 2020 starting at approximately 10am PDT and lasting for around 15 minutes. There will be a short period of downtime towards the end of the maintenance window. Please direct any comments, questions, or concerns to uwm-help@cgca.uwm.edu.

result.py 59.5 KB
Newer Older
Colm Talbot's avatar
Colm Talbot committed
1 2
from __future__ import division

3
import os
4
from collections import OrderedDict, namedtuple
5
from copy import copy
6
from distutils.version import LooseVersion
7
from itertools import product
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
8

9
import corner
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
10
import json
11
import matplotlib
12
import matplotlib.pyplot as plt
13
from matplotlib import lines as mpllines
Moritz's avatar
Moritz committed
14 15 16
import numpy as np
import pandas as pd
import scipy.stats
Moritz's avatar
Moritz committed
17
from scipy.special import logsumexp
18

19
from . import utils
Colm Talbot's avatar
Colm Talbot committed
20
from .utils import (logger, infer_parameters_from_function,
Colm Talbot's avatar
Colm Talbot committed
21 22
                    check_directory_exists_and_if_not_mkdir,
                    BilbyJsonEncoder, decode_bilby_json)
Colm Talbot's avatar
Colm Talbot committed
23
from .prior import Prior, PriorDict, DeltaFunction
24

25

26
def result_file_name(outdir, label, extension='json', gzip=False):
27 28 29 30 31 32 33 34
    """ Returns the standard filename used for a result file

    Parameters
    ----------
    outdir: str
        Name of the output directory
    label: str
        Naming scheme of the output file
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
35 36
    extension: str, optional
        Whether to save as `hdf5` or `json`
37 38
    gzip: bool, optional
        Set to True to append `.gz` to the extension for saving in gzipped format
39 40 41 42 43

    Returns
    -------
    str: File name of the output file
    """
44
    if extension in ['json', 'hdf5']:
45 46 47 48
        if extension == 'json' and gzip:
            return '{}/{}_result.{}.gz'.format(outdir, label, extension)
        else:
            return '{}/{}_result.{}'.format(outdir, label, extension)
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
49
    else:
50
        raise ValueError("Extension type {} not understood".format(extension))
51 52


53
def _determine_file_name(filename, outdir, label, extension, gzip):
54 55 56 57 58 59 60
    """ Helper method to determine the filename """
    if filename is not None:
        return filename
    else:
        if (outdir is None) and (label is None):
            raise ValueError("No information given to load file")
        else:
61
            return result_file_name(outdir, label, extension, gzip)
62 63


64
def read_in_result(filename=None, outdir=None, label=None, extension='json', gzip=False):
65 66 67 68 69 70 71 72 73 74 75
    """ Reads in a stored bilby result object

    Parameters
    ----------
    filename: str
        Path to the file to be read (alternative to giving the outdir and label)
    outdir, label, extension: str
        Name of the output directory, label and extension used for the default
        naming scheme.

    """
76
    filename = _determine_file_name(filename, outdir, label, extension, gzip)
77 78 79

    # Get the actual extension (may differ from the default extension if the filename is given)
    extension = os.path.splitext(filename)[1].lstrip('.')
80 81 82
    if extension == 'gz':  # gzipped file
        extension = os.path.splitext(os.path.splitext(filename)[0])[1].lstrip('.')

83 84 85 86 87 88 89 90
    if 'json' in extension:
        result = Result.from_json(filename=filename)
    elif ('hdf5' in extension) or ('h5' in extension):
        result = Result.from_hdf5(filename=filename)
    elif extension is None:
        raise ValueError("No filetype extension provided")
    else:
        raise ValueError("Filetype {} not understood".format(extension))
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
91
    return result
92 93 94 95 96


class Result(object):
    def __init__(self, label='no_label', outdir='.', sampler=None,
                 search_parameter_keys=None, fixed_parameter_keys=None,
Colm Talbot's avatar
Colm Talbot committed
97 98
                 constraint_parameter_keys=None, priors=None,
                 sampler_kwargs=None, injection_parameters=None,
99 100 101 102
                 meta_data=None, posterior=None, samples=None,
                 nested_samples=None, log_evidence=np.nan,
                 log_evidence_err=np.nan, log_noise_evidence=np.nan,
                 log_bayes_factor=np.nan, log_likelihood_evaluations=None,
103
                 log_prior_evaluations=None, sampling_time=None, nburn=None,
104
                 walkers=None, max_autocorrelation_time=None, use_ratio=None,
105
                 parameter_labels=None, parameter_labels_with_unit=None,
106
                 gzip=False, version=None):
107
        """ A class to store the results of the sampling run
108 109 110

        Parameters
        ----------
111 112
        label, outdir, sampler: str
            The label, output directory, and sampler used
Colm Talbot's avatar
Colm Talbot committed
113 114 115 116
        search_parameter_keys, fixed_parameter_keys, constraint_parameter_keys: list
            Lists of the search, constraint, and fixed parameter keys.
            Elements of the list should be of type `str` and match the keys
            of the `prior`
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        priors: dict, bilby.core.prior.PriorDict
            A dictionary of the priors used in the run
        sampler_kwargs: dict
            Key word arguments passed to the sampler
        injection_parameters: dict
            A dictionary of the injection parameters
        meta_data: dict
            A dictionary of meta data to store about the run
        posterior: pandas.DataFrame
            A pandas data frame of the posterior
        samples, nested_samples: array_like
            An array of the output posterior samples and the unweighted samples
        log_evidence, log_evidence_err, log_noise_evidence, log_bayes_factor: float
            Natural log evidences
        log_likelihood_evaluations: array_like
            The evaluations of the likelihood for each sample point
133 134
        log_prior_evaluations: array_like
            The evaluations of the prior for each sample point
135 136 137 138 139 140 141 142
        sampling_time: float
            The time taken to complete the sampling
        nburn: int
            The number of burn-in steps discarded for MCMC samplers
        walkers: array_like
            The samplers taken by a ensemble MCMC samplers
        max_autocorrelation_time: float
            The estimated maximum autocorrelation time for MCMC samplers
143 144 145
        use_ratio: bool
            A boolean stating whether the likelihood ratio, as opposed to the
            likelihood was used during sampling
146 147
        parameter_labels, parameter_labels_with_unit: list
            Lists of the latex-formatted parameter labels
148 149
        gzip: bool
            Set to True to gzip the results file (if using json format)
150 151 152
        version: str,
            Version information for software used to generate the result. Note,
            this information is generated when the result object is initialized
153

154 155 156 157
        Note
        ---------
        All sampling output parameters, e.g. the samples themselves are
        typically not given at initialisation, but set at a later stage.
158 159

        """
160

161 162 163 164 165
        self.label = label
        self.outdir = os.path.abspath(outdir)
        self.sampler = sampler
        self.search_parameter_keys = search_parameter_keys
        self.fixed_parameter_keys = fixed_parameter_keys
Colm Talbot's avatar
Colm Talbot committed
166
        self.constraint_parameter_keys = constraint_parameter_keys
167 168 169 170 171 172 173 174 175 176 177
        self.parameter_labels = parameter_labels
        self.parameter_labels_with_unit = parameter_labels_with_unit
        self.priors = priors
        self.sampler_kwargs = sampler_kwargs
        self.meta_data = meta_data
        self.injection_parameters = injection_parameters
        self.posterior = posterior
        self.samples = samples
        self.nested_samples = nested_samples
        self.walkers = walkers
        self.nburn = nburn
178
        self.use_ratio = use_ratio
179 180 181 182 183
        self.log_evidence = log_evidence
        self.log_evidence_err = log_evidence_err
        self.log_noise_evidence = log_noise_evidence
        self.log_bayes_factor = log_bayes_factor
        self.log_likelihood_evaluations = log_likelihood_evaluations
184
        self.log_prior_evaluations = log_prior_evaluations
185
        self.sampling_time = sampling_time
186
        self.version = version
Colm Talbot's avatar
Colm Talbot committed
187
        self.max_autocorrelation_time = max_autocorrelation_time
188

189 190 191
        self.prior_values = None
        self._kde = None

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    @classmethod
    def from_hdf5(cls, filename=None, outdir=None, label=None):
        """ Read in a saved .h5 data file

        Parameters
        ----------
        filename: str
            If given, try to load from this filename
        outdir, label: str
            If given, use the default naming convention for saved results file

        Returns
        -------
        result: bilby.core.result.Result

        Raises
        -------
        ValueError: If no filename is given and either outdir or label is None
                    If no bilby.core.result.Result is found in the path

        """
213
        import deepdish
214
        filename = _determine_file_name(filename, outdir, label, 'hdf5', False)
215

216
        if os.path.isfile(filename):
Moritz Huebner's avatar
Moritz Huebner committed
217 218
            dictionary = deepdish.io.load(filename)
            # Some versions of deepdish/pytables return the dictionanary as
219
            # a dictionary with a key 'data'
Moritz Huebner's avatar
Moritz Huebner committed
220 221 222
            if len(dictionary) == 1 and 'data' in dictionary:
                dictionary = dictionary['data']
            try:
223 224
                if isinstance(dictionary.get('posterior', None), dict):
                    dictionary['posterior'] = pd.DataFrame(dictionary['posterior'])
Moritz Huebner's avatar
Moritz Huebner committed
225 226 227
                return cls(**dictionary)
            except TypeError as e:
                raise IOError("Unable to load dictionary, error={}".format(e))
228 229 230
        else:
            raise IOError("No result '{}' found".format(filename))

Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
231
    @classmethod
232
    def from_json(cls, filename=None, outdir=None, label=None, gzip=False):
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        """ Read in a saved .json data file

        Parameters
        ----------
        filename: str
            If given, try to load from this filename
        outdir, label: str
            If given, use the default naming convention for saved results file

        Returns
        -------
        result: bilby.core.result.Result

        Raises
        -------
        ValueError: If no filename is given and either outdir or label is None
                    If no bilby.core.result.Result is found in the path

        """
252
        filename = _determine_file_name(filename, outdir, label, 'json', gzip)
253

Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
254
        if os.path.isfile(filename):
255 256 257 258 259 260 261 262
            if gzip or os.path.splitext(filename)[1].lstrip('.') == 'gz':
                import gzip
                with gzip.GzipFile(filename, 'r') as file:
                    json_str = file.read().decode('utf-8')
                dictionary = json.loads(json_str, object_hook=decode_bilby_json)
            else:
                with open(filename, 'r') as file:
                    dictionary = json.load(file, object_hook=decode_bilby_json)
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
263 264 265 266 267 268 269 270 271 272 273 274 275
            for key in dictionary.keys():
                # Convert the loaded priors to bilby prior type
                if key == 'priors':
                    for param in dictionary[key].keys():
                        dictionary[key][param] = str(dictionary[key][param])
                    dictionary[key] = PriorDict(dictionary[key])
            try:
                return cls(**dictionary)
            except TypeError as e:
                raise IOError("Unable to load dictionary, error={}".format(e))
        else:
            raise IOError("No result '{}' found".format(filename))

276
    def __str__(self):
277
        """Print a summary """
278 279
        if getattr(self, 'posterior', None) is not None:
            if getattr(self, 'log_noise_evidence', None) is not None:
280 281 282 283
                return ("nsamples: {:d}\n"
                        "log_noise_evidence: {:6.3f}\n"
                        "log_evidence: {:6.3f} +/- {:6.3f}\n"
                        "log_bayes_factor: {:6.3f} +/- {:6.3f}\n"
284
                        .format(len(self.posterior), self.log_noise_evidence, self.log_evidence,
285 286 287 288 289
                                self.log_evidence_err, self.log_bayes_factor,
                                self.log_evidence_err))
            else:
                return ("nsamples: {:d}\n"
                        "log_evidence: {:6.3f} +/- {:6.3f}\n"
290
                        .format(len(self.posterior), self.log_evidence, self.log_evidence_err))
291 292
        else:
            return ''
293

294 295 296 297 298 299
    @property
    def priors(self):
        if self._priors is not None:
            return self._priors
        else:
            raise ValueError('Result object has no priors')
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    @priors.setter
    def priors(self, priors):
        if isinstance(priors, dict):
            self._priors = PriorDict(priors)
            if self.parameter_labels is None:
                self.parameter_labels = [self.priors[k].latex_label for k in
                                         self.search_parameter_keys]
            if self.parameter_labels_with_unit is None:
                self.parameter_labels_with_unit = [
                    self.priors[k].latex_label_with_unit for k in
                    self.search_parameter_keys]

        elif priors is None:
            self._priors = priors
            self.parameter_labels = self.search_parameter_keys
            self.parameter_labels_with_unit = self.search_parameter_keys
        else:
            raise ValueError("Input priors not understood")
319

320 321 322 323 324
    @property
    def samples(self):
        """ An array of samples """
        if self._samples is not None:
            return self._samples
325
        else:
326
            raise ValueError("Result object has no stored samples")
327

328 329 330
    @samples.setter
    def samples(self, samples):
        self._samples = samples
331

332 333 334 335 336 337 338
    @property
    def nested_samples(self):
        """" An array of unweighted samples """
        if self._nested_samples is not None:
            return self._nested_samples
        else:
            raise ValueError("Result object has no stored nested samples")
339

340 341 342
    @nested_samples.setter
    def nested_samples(self, nested_samples):
        self._nested_samples = nested_samples
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    @property
    def walkers(self):
        """" An array of the ensemble walkers """
        if self._walkers is not None:
            return self._walkers
        else:
            raise ValueError("Result object has no stored walkers")

    @walkers.setter
    def walkers(self, walkers):
        self._walkers = walkers

    @property
    def nburn(self):
        """" An array of the ensemble walkers """
        if self._nburn is not None:
            return self._nburn
        else:
            raise ValueError("Result object has no stored nburn")

    @nburn.setter
    def nburn(self, nburn):
        self._nburn = nburn

    @property
    def posterior(self):
        """ A pandas data frame of the posterior """
        if self._posterior is not None:
            return self._posterior
        else:
            raise ValueError("Result object has no stored posterior")

    @posterior.setter
    def posterior(self, posterior):
        self._posterior = posterior

380 381 382 383 384 385 386 387 388 389 390
    @property
    def version(self):
        return self._version

    @version.setter
    def version(self, version):
        if version is None:
            self._version = 'bilby={}'.format(utils.get_version_information())
        else:
            self._version = version

391
    def _get_save_data_dictionary(self):
392
        # This list defines all the parameters saved in the result object
393 394 395 396
        save_attrs = [
            'label', 'outdir', 'sampler', 'log_evidence', 'log_evidence_err',
            'log_noise_evidence', 'log_bayes_factor', 'priors', 'posterior',
            'injection_parameters', 'meta_data', 'search_parameter_keys',
Colm Talbot's avatar
Colm Talbot committed
397
            'fixed_parameter_keys', 'constraint_parameter_keys',
398
            'sampling_time', 'sampler_kwargs', 'use_ratio',
399 400
            'log_likelihood_evaluations', 'log_prior_evaluations', 'samples',
            'nested_samples', 'walkers', 'nburn', 'parameter_labels',
401
            'parameter_labels_with_unit', 'version']
402 403 404 405 406 407 408 409
        dictionary = OrderedDict()
        for attr in save_attrs:
            try:
                dictionary[attr] = getattr(self, attr)
            except ValueError as e:
                logger.debug("Unable to save {}, message: {}".format(attr, e))
                pass
        return dictionary
410

411 412
    def save_to_file(self, filename=None, overwrite=False, outdir=None,
                     extension='json', gzip=False):
Colm Talbot's avatar
Colm Talbot committed
413
        """
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
414
        Writes the Result to a json or deepdish h5 file
Colm Talbot's avatar
Colm Talbot committed
415 416 417

        Parameters
        ----------
418 419
        filename: optional,
            Filename to write to (overwrites the default)
Colm Talbot's avatar
Colm Talbot committed
420 421 422
        overwrite: bool, optional
            Whether or not to overwrite an existing result file.
            default=False
423 424
        outdir: str, optional
            Path to the outdir. Default is the one stored in the result object.
Gregory Ashton's avatar
Gregory Ashton committed
425 426 427
        extension: str, optional {json, hdf5, True}
            Determines the method to use to store the data (if True defaults
            to json)
428 429 430
        gzip: bool, optional
            If true, and outputing to a json file, this will gzip the resulting
            file and add '.gz' to the file extension.
Colm Talbot's avatar
Colm Talbot committed
431
        """
Gregory Ashton's avatar
Gregory Ashton committed
432 433 434 435

        if extension is True:
            extension = "json"

436
        outdir = self._safe_outdir_creation(outdir, self.save_to_file)
437 438
        if filename is None:
            filename = result_file_name(outdir, self.label, extension, gzip)
439

440
        if os.path.isfile(filename):
Colm Talbot's avatar
Colm Talbot committed
441
            if overwrite:
442 443
                logger.debug('Removing existing file {}'.format(filename))
                os.remove(filename)
Colm Talbot's avatar
Colm Talbot committed
444 445
            else:
                logger.debug(
446 447 448
                    'Renaming existing file {} to {}.old'.format(filename,
                                                                 filename))
                os.rename(filename, filename + '.old')
449

450
        logger.debug("Saving result to {}".format(filename))
451 452

        # Convert the prior to a string representation for saving on disk
453
        dictionary = self._get_save_data_dictionary()
454 455 456
        if dictionary.get('priors', False):
            dictionary['priors'] = {key: str(self.priors[key]) for key in self.priors}

457
        # Convert callable sampler_kwargs to strings
458 459 460 461
        if dictionary.get('sampler_kwargs', None) is not None:
            for key in dictionary['sampler_kwargs']:
                if hasattr(dictionary['sampler_kwargs'][key], '__call__'):
                    dictionary['sampler_kwargs'][key] = str(dictionary['sampler_kwargs'])
462

Gregory Ashton's avatar
Gregory Ashton committed
463
        try:
464
            if extension == 'json':
465 466 467 468
                if gzip:
                    import gzip
                    # encode to a string
                    json_str = json.dumps(dictionary, cls=BilbyJsonEncoder).encode('utf-8')
469
                    with gzip.GzipFile(filename, 'w') as file:
470 471
                        file.write(json_str)
                else:
472
                    with open(filename, 'w') as file:
473
                        json.dump(dictionary, file, indent=2, cls=BilbyJsonEncoder)
474
            elif extension == 'hdf5':
475
                import deepdish
476 477 478
                for key in dictionary:
                    if isinstance(dictionary[key], pd.DataFrame):
                        dictionary[key] = dictionary[key].to_dict()
479
                deepdish.io.save(filename, dictionary)
Sylvia Biscoveanu's avatar
Sylvia Biscoveanu committed
480
            else:
481
                raise ValueError("Extension type {} not understood".format(extension))
Gregory Ashton's avatar
Gregory Ashton committed
482
        except Exception as e:
Gregory Ashton's avatar
Gregory Ashton committed
483
            logger.error("\n\n Saving the data has failed with the "
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
484
                         "following message:\n {} \n\n".format(e))
Gregory Ashton's avatar
Gregory Ashton committed
485

486
    def save_posterior_samples(self, outdir=None):
487
        """Saves posterior samples to a file"""
488 489
        outdir = self._safe_outdir_creation(outdir, self.save_posterior_samples)
        filename = '{}/{}_posterior_samples.txt'.format(outdir, self.label)
490 491
        self.posterior.to_csv(filename, index=False, header=True)

Gregory Ashton's avatar
Gregory Ashton committed
492
    def get_latex_labels_from_parameter_keys(self, keys):
493 494 495 496 497 498 499 500 501 502 503 504 505
        """ Returns a list of latex_labels corresponding to the given keys

        Parameters
        ----------
        keys: list
            List of strings corresponding to the desired latex_labels

        Returns
        -------
        list: The desired latex_labels

        """
        latex_labels = []
Gregory Ashton's avatar
Gregory Ashton committed
506 507 508
        for k in keys:
            if k in self.search_parameter_keys:
                idx = self.search_parameter_keys.index(k)
509
                latex_labels.append(self.parameter_labels_with_unit[idx])
Gregory Ashton's avatar
Gregory Ashton committed
510
            elif k in self.parameter_labels:
511
                latex_labels.append(k)
Gregory Ashton's avatar
Gregory Ashton committed
512
            else:
Colm Talbot's avatar
Colm Talbot committed
513
                logger.debug(
514 515
                    'key {} not a parameter label or latex label'.format(k))
                latex_labels.append(' '.join(k.split('_')))
516
        return latex_labels
Gregory Ashton's avatar
Gregory Ashton committed
517

Gregory Ashton's avatar
Gregory Ashton committed
518 519 520 521 522 523 524 525 526
    @property
    def covariance_matrix(self):
        """ The covariance matrix of the samples the posterior """
        samples = self.posterior[self.search_parameter_keys].values
        return np.cov(samples.T)

    @property
    def posterior_volume(self):
        """ The posterior volume """
Gregory Ashton's avatar
Gregory Ashton committed
527 528 529
        if self.covariance_matrix.ndim == 0:
            return np.sqrt(self.covariance_matrix)
        else:
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
530 531
            return 1 / np.sqrt(np.abs(np.linalg.det(
                1 / self.covariance_matrix)))
Gregory Ashton's avatar
Gregory Ashton committed
532

533 534
    @staticmethod
    def prior_volume(priors):
Gregory Ashton's avatar
Gregory Ashton committed
535 536 537 538 539 540
        """ The prior volume, given a set of priors """
        return np.prod([priors[k].maximum - priors[k].minimum for k in priors])

    def occam_factor(self, priors):
        """ The Occam factor,

541 542 543
        See Chapter 28, `Mackay "Information Theory, Inference, and Learning
        Algorithms" <http://www.inference.org.uk/itprnn/book.html>`_ Cambridge
        University Press (2003).
Gregory Ashton's avatar
Gregory Ashton committed
544 545 546 547

        """
        return self.posterior_volume / self.prior_volume(priors)

548 549 550 551 552 553 554 555 556 557 558 559 560
    @property
    def bayesian_model_dimensionality(self):
        """ Characterises how many parameters are effectively constraint by the data

        See <https://arxiv.org/abs/1903.06682>

        Returns
        -------
        float: The model dimensionality
        """
        return 2 * (np.mean(self.posterior['log_likelihood']**2) -
                    np.mean(self.posterior['log_likelihood'])**2)

561
    def get_one_dimensional_median_and_error_bar(self, key, fmt='.2f',
562
                                                 quantiles=(0.16, 0.84)):
563 564 565 566 567 568 569 570
        """ Calculate the median and error bar for a given key

        Parameters
        ----------
        key: str
            The parameter key for which to calculate the median and error bar
        fmt: str, ('.2f')
            A format string
571 572
        quantiles: list, tuple
            A length-2 tuple of the lower and upper-quantiles to calculate
573 574 575 576
            the errors bars for.

        Returns
        -------
577 578
        summary: namedtuple
            An object with attributes, median, lower, upper and string
579 580

        """
581 582
        summary = namedtuple('summary', ['median', 'lower', 'upper', 'string'])

583 584 585 586 587
        if len(quantiles) != 2:
            raise ValueError("quantiles must be of length 2")

        quants_to_compute = np.array([quantiles[0], 0.5, quantiles[1]])
        quants = np.percentile(self.posterior[key], quants_to_compute * 100)
588 589 590
        summary.median = quants[1]
        summary.plus = quants[2] - summary.median
        summary.minus = summary.median - quants[0]
591 592

        fmt = "{{0:{0}}}".format(fmt).format
593 594 595 596 597
        string_template = r"${{{0}}}_{{-{1}}}^{{+{2}}}$"
        summary.string = string_template.format(
            fmt(summary.median), fmt(summary.minus), fmt(summary.plus))
        return summary

Colm Talbot's avatar
Colm Talbot committed
598 599 600
    def plot_single_density(self, key, prior=None, cumulative=False,
                            title=None, truth=None, save=True,
                            file_base_name=None, bins=50, label_fontsize=16,
601 602
                            title_fontsize=16, quantiles=(0.16, 0.84), dpi=300):
        """ Plot a 1D marginal density, either probability or cumulative.
Colm Talbot's avatar
Colm Talbot committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

        Parameters
        ----------
        key: str
            Name of the parameter to plot
        prior: {bool (True), bilby.core.prior.Prior}
            If true, add the stored prior probability density function to the
            one-dimensional marginal distributions. If instead a Prior
            is provided, this will be plotted.
        cumulative: bool
            If true plot the CDF
        title: bool
            If true, add 1D title of the median and (by default 1-sigma)
            error bars. To change the error bars, pass in the quantiles kwarg.
            See method `get_one_dimensional_median_and_error_bar` for further
            details). If `quantiles=None` is passed in, no title is added.
        truth: {bool, float}
            If true, plot self.injection_parameters[parameter].
            If float, plot this value.
        save: bool:
            If true, save plot to disk.
        file_base_name: str, optional
            If given, the base file name to use (by default `outdir/label_` is
            used)
        bins: int
            The number of histogram bins
        label_fontsize, title_fontsize: int
            The fontsizes for the labels and titles
631 632
        quantiles: tuple
            A length-2 tuple of the lower and upper-quantiles to calculate
Colm Talbot's avatar
Colm Talbot committed
633 634 635 636 637 638 639 640 641 642 643 644
            the errors bars for.
        dpi: int
            Dots per inch resolution of the plot

        Returns
        -------
        figure: matplotlib.pyplot.figure
            A matplotlib figure object
        """
        logger.info('Plotting {} marginal distribution'.format(key))
        label = self.get_latex_labels_from_parameter_keys([key])[0]
        fig, ax = plt.subplots()
645 646 647 648 649 650 651 652
        try:
            ax.hist(self.posterior[key].values, bins=bins, density=True,
                    histtype='step', cumulative=cumulative)
        except ValueError as e:
            logger.info(
                'Failed to generate 1d plot for {}, error message: {}'
                .format(key, e))
            return
Colm Talbot's avatar
Colm Talbot committed
653 654 655 656 657 658 659 660 661 662 663 664 665
        ax.set_xlabel(label, fontsize=label_fontsize)
        if truth is not None:
            ax.axvline(truth, ls='-', color='orange')

        summary = self.get_one_dimensional_median_and_error_bar(
            key, quantiles=quantiles)
        ax.axvline(summary.median - summary.minus, ls='--', color='C0')
        ax.axvline(summary.median + summary.plus, ls='--', color='C0')
        if title:
            ax.set_title(summary.string, fontsize=title_fontsize)

        if isinstance(prior, Prior):
            theta = np.linspace(ax.get_xlim()[0], ax.get_xlim()[1], 300)
666
            ax.plot(theta, prior.prob(theta), color='C2')
Colm Talbot's avatar
Colm Talbot committed
667 668 669 670 671 672 673 674

        if save:
            fig.tight_layout()
            if cumulative:
                file_name = file_base_name + key + '_cdf'
            else:
                file_name = file_base_name + key + '_pdf'
            fig.savefig(file_name, dpi=dpi)
675 676 677
            plt.close(fig)
        else:
            return fig
Colm Talbot's avatar
Colm Talbot committed
678

679 680
    def plot_marginals(self, parameters=None, priors=None, titles=True,
                       file_base_name=None, bins=50, label_fontsize=16,
681 682
                       title_fontsize=16, quantiles=(0.16, 0.84), dpi=300,
                       outdir=None):
683 684 685 686 687 688 689
        """ Plot 1D marginal distributions

        Parameters
        ----------
        parameters: (list, dict), optional
            If given, either a list of the parameter names to include, or a
            dictionary of parameter names and their "true" values to plot.
690
        priors: {bool (False), bilby.core.prior.PriorDict}
691
            If true, add the stored prior probability density functions to the
692
            one-dimensional marginal distributions. If instead a PriorDict
693 694 695 696 697 698 699 700 701 702 703 704
            is provided, this will be plotted.
        titles: bool
            If true, add 1D titles of the median and (by default 1-sigma)
            error bars. To change the error bars, pass in the quantiles kwarg.
            See method `get_one_dimensional_median_and_error_bar` for further
            details). If `quantiles=None` is passed in, no title is added.
        file_base_name: str, optional
            If given, the base file name to use (by default `outdir/label_` is
            used)
        bins: int
            The number of histogram bins
        label_fontsize, title_fontsize: int
705 706 707
            The font sizes for the labels and titles
        quantiles: tuple
            A length-2 tuple of the lower and upper-quantiles to calculate
708 709 710
            the errors bars for.
        dpi: int
            Dots per inch resolution of the plot
711 712
        outdir: str, optional
            Path to the outdir. Default is the one store in the result object.
713 714 715 716 717 718

        Returns
        -------
        """
        if isinstance(parameters, dict):
            plot_parameter_keys = list(parameters.keys())
Colm Talbot's avatar
Colm Talbot committed
719
            truths = parameters
720
        elif parameters is None:
Colm Talbot's avatar
Colm Talbot committed
721 722 723 724 725
            plot_parameter_keys = self.posterior.keys()
            if self.injection_parameters is None:
                truths = dict()
            else:
                truths = self.injection_parameters
726 727
        else:
            plot_parameter_keys = list(parameters)
Colm Talbot's avatar
Colm Talbot committed
728 729 730 731
            if self.injection_parameters is None:
                truths = dict()
            else:
                truths = self.injection_parameters
732 733

        if file_base_name is None:
734 735
            outdir = self._safe_outdir_creation(outdir, self.plot_marginals)
            file_base_name = '{}/{}_1d/'.format(outdir, self.label)
Colm Talbot's avatar
Colm Talbot committed
736
            check_directory_exists_and_if_not_mkdir(file_base_name)
737 738

        if priors is True:
Colm Talbot's avatar
Colm Talbot committed
739 740
            priors = getattr(self, 'priors', dict())
        elif isinstance(priors, dict):
741
            pass
Colm Talbot's avatar
Colm Talbot committed
742 743
        elif priors in [False, None]:
            priors = dict()
744 745 746 747
        else:
            raise ValueError('Input priors={} not understood'.format(priors))

        for i, key in enumerate(plot_parameter_keys):
Colm Talbot's avatar
Colm Talbot committed
748 749
            if not isinstance(self.posterior[key].values[0], float):
                continue
Colm Talbot's avatar
Colm Talbot committed
750 751
            prior = priors.get(key, None)
            truth = truths.get(key, None)
Colm Talbot's avatar
Colm Talbot committed
752
            for cumulative in [False, True]:
753
                self.plot_single_density(
Colm Talbot's avatar
Colm Talbot committed
754 755
                    key, prior=prior, cumulative=cumulative, title=titles,
                    truth=truth, save=True, file_base_name=file_base_name,
Colm Talbot's avatar
Colm Talbot committed
756 757
                    bins=bins, label_fontsize=label_fontsize, dpi=dpi,
                    title_fontsize=title_fontsize, quantiles=quantiles)
758

759 760 761
    def plot_corner(self, parameters=None, priors=None, titles=True, save=True,
                    filename=None, dpi=300, **kwargs):
        """ Plot a corner-plot
Gregory Ashton's avatar
Gregory Ashton committed
762 763 764

        Parameters
        ----------
765 766 767
        parameters: (list, dict), optional
            If given, either a list of the parameter names to include, or a
            dictionary of parameter names and their "true" values to plot.
768
        priors: {bool (False), bilby.core.prior.PriorDict}
769
            If true, add the stored prior probability density functions to the
770
            one-dimensional marginal distributions. If instead a PriorDict
771
            is provided, this will be plotted.
772 773 774 775 776
        titles: bool
            If true, add 1D titles of the median and (by default 1-sigma)
            error bars. To change the error bars, pass in the quantiles kwarg.
            See method `get_one_dimensional_median_and_error_bar` for further
            details). If `quantiles=None` is passed in, no title is added.
777 778 779 780 781 782
        save: bool, optional
            If true, save the image using the given label and outdir
        filename: str, optional
            If given, overwrite the default filename
        dpi: int, optional
            Dots per inch resolution of the plot
783 784 785
        **kwargs:
            Other keyword arguments are passed to `corner.corner`. We set some
            defaults to improve the basic look and feel, but these can all be
786 787
            overridden. Also optional an 'outdir' argument which can be used
            to override the outdir set by the absolute path of the result object.
Gregory Ashton's avatar
Gregory Ashton committed
788

789 790 791 792 793 794
        Notes
        -----
            The generation of the corner plot themselves is done by the corner
            python module, see https://corner.readthedocs.io for more
            information.

Gregory Ashton's avatar
Gregory Ashton committed
795 796 797 798
        Returns
        -------
        fig:
            A matplotlib figure instance
799

Gregory Ashton's avatar
Gregory Ashton committed
800
        """
801 802

        # If in testing mode, not corner plots are generated
803 804
        if utils.command_line_args.test:
            return
Gregory Ashton's avatar
Gregory Ashton committed
805

Colm Talbot's avatar
Colm Talbot committed
806
        # bilby default corner kwargs. Overwritten by anything passed to kwargs
807 808 809
        defaults_kwargs = dict(
            bins=50, smooth=0.9, label_kwargs=dict(fontsize=16),
            title_kwargs=dict(fontsize=16), color='#0072C1',
810
            truth_color='tab:orange', quantiles=[0.16, 0.84],
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
811
            levels=(1 - np.exp(-0.5), 1 - np.exp(-2), 1 - np.exp(-9 / 2.)),
812
            plot_density=False, plot_datapoints=True, fill_contours=True,
813 814 815 816 817 818
            max_n_ticks=3)

        if LooseVersion(matplotlib.__version__) < "2.1":
            defaults_kwargs['hist_kwargs'] = dict(normed=True)
        else:
            defaults_kwargs['hist_kwargs'] = dict(density=True)
819

820 821 822 823
        if 'lionize' in kwargs and kwargs['lionize'] is True:
            defaults_kwargs['truth_color'] = 'tab:blue'
            defaults_kwargs['color'] = '#FF8C00'

824 825 826
        defaults_kwargs.update(kwargs)
        kwargs = defaults_kwargs

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
        # Handle if truths was passed in
        if 'truth' in kwargs:
            kwargs['truths'] = kwargs.pop('truth')
        if kwargs.get('truths'):
            truths = kwargs.get('truths')
            if isinstance(parameters, list) and isinstance(truths, list):
                if len(parameters) != len(truths):
                    raise ValueError(
                        "Length of parameters and truths don't match")
            elif isinstance(truths, dict) and parameters is None:
                parameters = kwargs.pop('truths')
            else:
                raise ValueError(
                    "Combination of parameters and truths not understood")

Gregory Ashton's avatar
Gregory Ashton committed
842 843 844 845 846 847 848 849
        # If injection parameters where stored, use these as parameter values
        # but do not overwrite input parameters (or truths)
        cond1 = getattr(self, 'injection_parameters', None) is not None
        cond2 = parameters is None
        if cond1 and cond2:
            parameters = {key: self.injection_parameters[key] for key in
                          self.search_parameter_keys}

850 851 852 853 854 855 856 857 858
        # If parameters is a dictionary, use the keys to determine which
        # parameters to plot and the values as truths.
        if isinstance(parameters, dict):
            plot_parameter_keys = list(parameters.keys())
            kwargs['truths'] = list(parameters.values())
        elif parameters is None:
            plot_parameter_keys = self.search_parameter_keys
        else:
            plot_parameter_keys = list(parameters)
859

860
        # Get latex formatted strings for the plot labels
861 862
        kwargs['labels'] = kwargs.get(
            'labels', self.get_latex_labels_from_parameter_keys(
863
                plot_parameter_keys))
864

865 866 867 868
        # Unless already set, set the range to include all samples
        # This prevents ValueErrors being raised for parameters with no range
        kwargs['range'] = kwargs.get('range', [1] * len(plot_parameter_keys))

869 870
        # Create the data array to plot and pass everything to corner
        xs = self.posterior[plot_parameter_keys].values
871
        fig = corner.corner(xs, **kwargs)
872
        axes = fig.get_axes()
873 874 875

        #  Add the titles
        if titles and kwargs.get('quantiles', None) is not None:
876 877
            for i, par in enumerate(plot_parameter_keys):
                ax = axes[i + i * len(plot_parameter_keys)]
878 879
                if ax.title.get_text() == '':
                    ax.set_title(self.get_one_dimensional_median_and_error_bar(
880
                        par, quantiles=kwargs['quantiles']).string,
881 882 883
                        **kwargs['title_kwargs'])

        #  Add priors to the 1D plots
884 885 886
        if priors is True:
            priors = getattr(self, 'priors', False)
        if isinstance(priors, dict):
887 888
            for i, par in enumerate(plot_parameter_keys):
                ax = axes[i + i * len(plot_parameter_keys)]
889 890
                theta = np.linspace(ax.get_xlim()[0], ax.get_xlim()[1], 300)
                ax.plot(theta, priors[par].prob(theta), color='C2')
891 892 893 894
        elif priors in [False, None]:
            pass
        else:
            raise ValueError('Input priors={} not understood'.format(priors))
895

896
        if save:
897
            if filename is None:
898 899
                outdir = self._safe_outdir_creation(kwargs.get('outdir'), self.plot_corner)
                filename = '{}/{}_corner.png'.format(outdir, self.label)
Gregory Ashton's avatar
Gregory Ashton committed
900
            logger.debug('Saving corner plot to {}'.format(filename))
901
            fig.savefig(filename, dpi=dpi)
902
            plt.close(fig)
903

904
        return fig
905

Gregory Ashton's avatar
Gregory Ashton committed
906
    def plot_walkers(self, **kwargs):
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
907
        """ Method to plot the trace of the walkers in an ensemble MCMC plot """
908
        if hasattr(self, 'walkers') is False:
Gregory Ashton's avatar
Gregory Ashton committed
909
            logger.warning("Cannot plot_walkers as no walkers are saved")
910
            return
911 912 913

        if utils.command_line_args.test:
            return
914 915 916

        nwalkers, nsteps, ndim = self.walkers.shape
        idxs = np.arange(nsteps)
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
917
        fig, axes = plt.subplots(nrows=ndim, figsize=(6, 3 * ndim))
918 919
        walkers = self.walkers[:, :, :]
        for i, ax in enumerate(axes):
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
920
            ax.plot(idxs[:self.nburn + 1], walkers[:, :self.nburn + 1, i].T,
921 922 923 924 925 926 927 928 929
                    lw=0.1, color='r')
            ax.set_ylabel(self.parameter_labels[i])

        for i, ax in enumerate(axes):
            ax.plot(idxs[self.nburn:], walkers[:, self.nburn:, i].T, lw=0.1,
                    color='k')
            ax.set_ylabel(self.parameter_labels[i])

        fig.tight_layout()
930 931
        outdir = self._safe_outdir_creation(kwargs.get('outdir'), self.plot_walkers)
        filename = '{}/{}_walkers.png'.format(outdir, self.label)
Gregory Ashton's avatar
Gregory Ashton committed
932
        logger.debug('Saving walkers plot to {}'.format('filename'))
933
        fig.savefig(filename)
934
        plt.close(fig)
935

936 937 938
    def plot_with_data(self, model, x, y, ndraws=1000, npoints=1000,
                       xlabel=None, ylabel=None, data_label='data',
                       data_fmt='o', draws_label=None, filename=None,
939
                       maxl_label='max likelihood', dpi=300, outdir=None):
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
        """ Generate a figure showing the data and fits to the data

        Parameters
        ----------
        model: function
            A python function which when called as `model(x, **kwargs)` returns
            the model prediction (here `kwargs` is a dictionary of key-value
            pairs of the model parameters.
        x, y: np.ndarray
            The independent and dependent data to plot
        ndraws: int
            Number of draws from the posterior to plot
        npoints: int
            Number of points used to plot the smoothed fit to the data
        xlabel, ylabel: str
            Labels for the axes
        data_label, draws_label, maxl_label: str
            Label for the data, draws, and max likelihood legend
        data_fmt: str
            Matpltolib fmt code, defaults to `'-o'`
        dpi: int
            Passed to `plt.savefig`
        filename: str
            If given, the filename to use. Otherwise, the filename is generated
            from the outdir and label attributes.
965 966
        outdir: str, optional
            Path to the outdir. Default is the one store in the result object.
967 968

        """
969 970 971 972 973 974

        # Determine model_posterior, the subset of the full posterior which
        # should be passed into the model
        model_keys = infer_parameters_from_function(model)
        model_posterior = self.posterior[model_keys]

975 976 977 978
        xsmooth = np.linspace(np.min(x), np.max(x), npoints)
        fig, ax = plt.subplots()
        logger.info('Plotting {} draws'.format(ndraws))
        for _ in range(ndraws):
979
            s = model_posterior.sample().to_dict('records')[0]
980 981
            ax.plot(xsmooth, model(xsmooth, **s), alpha=0.25, lw=0.1, color='r',
                    label=draws_label)
982 983 984
        try:
            if all(~np.isnan(self.posterior.log_likelihood)):
                logger.info('Plotting maximum likelihood')
Colm Talbot's avatar
Colm Talbot committed
985
                s = model_posterior.iloc[self.posterior.log_likelihood.idxmax()]
986 987
                ax.plot(xsmooth, model(xsmooth, **s), lw=1, color='k',
                        label=maxl_label)
988
        except (AttributeError, TypeError):
989 990
            logger.debug(
                "No log likelihood values stored, unable to plot max")
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

        ax.plot(x, y, data_fmt, markersize=2, label=data_label)

        if xlabel is not None:
            ax.set_xlabel(xlabel)
        if ylabel is not None:
            ax.set_ylabel(ylabel)

        handles, labels = plt.gca().get_legend_handles_labels()
        by_label = OrderedDict(zip(labels, handles))
        plt.legend(by_label.values(), by_label.keys())
        ax.legend(numpoints=3)
        fig.tight_layout()
        if filename is None:
1005 1006
            outdir = self._safe_outdir_creation(outdir, self.plot_with_data)
            filename = '{}/{}_plot_with_data'.format(outdir, self.label)
1007
        fig.savefig(filename, dpi=dpi)
1008
        plt.close(fig)
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    @staticmethod
    def _add_prior_fixed_values_to_posterior(posterior, priors):
        if priors is None:
            return posterior
        for key in priors:
            if isinstance(priors[key], DeltaFunction):
                posterior[key] = priors[key].peak
            elif isinstance(priors[key], float):
                posterior[key] = priors[key]
        return posterior

1021 1022
    def samples_to_posterior(self, likelihood=None, priors=None,
                             conversion_function=None):
1023
        """
1024 1025 1026
        Convert array of samples to posterior (a Pandas data frame)

        Also applies the conversion function to any stored posterior
1027

1028 1029
        Parameters
        ----------
Colm Talbot's avatar
Colm Talbot committed
1030
        likelihood: bilby.likelihood.GravitationalWaveTransient, optional
1031
            GravitationalWaveTransient likelihood used for sampling.
Colm Talbot's avatar
Colm Talbot committed
1032
        priors: bilby.prior.PriorDict, optional
1033
            Dictionary of prior object, used to fill in delta function priors.
1034
        conversion_function: function, optional
1035 1036
            Function which adds in extra parameters to the data frame,
            should take the data_frame, likelihood and prior as arguments.
1037
        """
1038 1039 1040
        try:
            data_frame = self.posterior
        except ValueError:
1041 1042
            data_frame = pd.DataFrame(
                self.samples, columns=self.search_parameter_keys)
1043 1044
            data_frame = self._add_prior_fixed_values_to_posterior(
                data_frame, priors)
1045 1046
            data_frame['log_likelihood'] = getattr(
                self, 'log_likelihood_evaluations', np.nan)
Colm Talbot's avatar
Colm Talbot committed
1047 1048 1049
            if self.log_prior_evaluations is None and priors is not None:
                data_frame['log_prior'] = priors.ln_prob(
                    dict(data_frame[self.search_parameter_keys]), axis=0)
1050 1051
            else:
                data_frame['log_prior'] = self.log_prior_evaluations
1052
        if conversion_function is not None:
1053
            data_frame = conversion_function(data_frame, likelihood, priors)
1054
        self.posterior = data_frame
Gregory Ashton's avatar
Gregory Ashton committed
1055

Colm Talbot's avatar
Colm Talbot committed
1056
    def calculate_prior_values(self, priors):
1057 1058 1059 1060 1061
        """
        Evaluate prior probability for each parameter for each sample.

        Parameters
        ----------
1062
        priors: dict, PriorDict
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
            Prior distributions
        """
        self.prior_values = pd.DataFrame()
        for key in priors:
            if key in self.posterior.keys():
                if isinstance(priors[key], DeltaFunction):
                    continue
                else:
                    self.prior_values[key]\
                        = priors[key].prob(self.posterior[key].values)

1074
    def get_all_injection_credible_levels(self, keys=None):
Colm Talbot's avatar
Colm Talbot committed
1075
        """
1076 1077 1078 1079 1080 1081 1082
        Get credible levels for all parameters

        Parameters
        ----------
        keys: list, optional
            A list of keys for which return the credible levels, if None,
            defaults to search_parameter_keys
Colm Talbot's avatar
Colm Talbot committed
1083 1084 1085 1086 1087 1088

        Returns
        -------
        credible_levels: dict
            The credible levels at which the injected parameters are found.
        """
1089 1090
        if keys is None:
            keys = self.search_parameter_keys
Colm Talbot's avatar
Colm Talbot committed
1091 1092
        if self.injection_parameters is None:
            raise(TypeError, "Result object has no 'injection_parameters'. "
1093
                             "Cannot compute credible levels.")
Colm Talbot's avatar
Colm Talbot committed
1094
        credible_levels = {key: self.get_injection_credible_level(key)
1095
                           for key in keys
Colm Talbot's avatar
Colm Talbot committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
                           if isinstance(self.injection_parameters[key], float)}
        return credible_levels

    def get_injection_credible_level(self, parameter):
        """
        Get the credible level of the injected parameter

        Calculated as CDF(injection value)

        Parameters
        ----------
        parameter: str
            Parameter to get credible level for
        Returns
        -------
        float: credible level
        """
        if self.injection_parameters is None:
            raise(TypeError, "Result object has no 'injection_parameters'. "
                             "Cannot copmute credible levels.")
        if parameter in self.posterior and\
                parameter in self.injection_parameters:
            credible_level =\
                sum(self.posterior[parameter].values <
                    self.injection_parameters[parameter]) / len(self.posterior)
            return credible_level
        else:
            return np.nan

1125
    def _check_attribute_match_to_other_object(self, name, other_object):
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
        """ Check attribute name exists in other_object and is the same

        Parameters
        ----------
        name: str
            Name of the attribute in this instance
        other_object: object
            Other object with attributes to compare with

        Returns
        -------
        bool: True if attribute name matches with an attribute of other_object, False otherwise

        """
1140 1141 1142 1143 1144 1145 1146 1147
        a = getattr(self, name, False)
        b = getattr(other_object, name, False)
        logger.debug('Checking {} value: {}=={}'.format(name, a, b))
        if (a is not False) and (b is not False):
            type_a = type(a)
            type_b = type(b)
            if type_a == type_b:
                if type_a in [str, float, int, dict, list]:
Gregory Ashton's avatar
Gregory Ashton committed
1148
                    try:
1149
                        return a == b
Gregory Ashton's avatar
Gregory Ashton committed
1150 1151
                    except ValueError:
                        return False
1152 1153
                elif type_a in [np.ndarray]:
                    return np.all(a == b)
1154
        return False
1155

1156 1157 1158 1159 1160 1161
    @property
    def kde(self):
        """ Kernel density estimate built from the stored posterior

        Uses `scipy.stats.gaussian_kde` to generate the kernel density
        """
1162
        if self._kde:
1163
            return self._kde
1164
        else:
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
            self._kde = scipy.stats.gaussian_kde(
                self.posterior[self.search_parameter_keys].values.T)
            return self._kde

    def posterior_probability(self, sample):
        """ Calculate the posterior probabily for a new sample

        This queries a Kernel Density Estimate of the posterior to calculate
        the posterior probability density for the new sample.

        Parameters
        ----------
        sample: dict, or list of dictionaries
            A dictionary containing all the keys from
            self.search_parameter_keys and corresponding values at which to
            calculate the posterior probability

        Returns
        -------
        p: array-like,
            The posterior probability of the sample

        """
        if isinstance(sample, dict):
            sample = [sample]
        ordered_sample = [[s[key] for key in self.search_parameter_keys]
                          for s in sample]
        return self.kde(ordered_sample)

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    def _safe_outdir_creation(self, outdir=None, caller_func=None):
        if outdir is None:
            outdir = self.outdir
        try:
            utils.check_directory_exists_and_if_not_mkdir(outdir)
        except PermissionError:
            raise FileMovedError("Can not write in the out directory.\n"
                                 "Did you move the here file from another system?\n"
                                 "Try calling " + caller_func.__name__ + " with the 'outdir' "
                                 "keyword argument, e.g. " + caller_func.__name__ + "(outdir='.')")
        return outdir

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    def get_weights_by_new_prior(self, old_prior, new_prior, prior_names=None):
        """ Calculate a list of sample weights based on the ratio of new to old priors

            Parameters
            ----------
            old_prior: PriorDict,
                The prior used in the generation of the original samples.

            new_prior: PriorDict,
                The prior to use to reweight the samples.

            prior_names: list
                A list of the priors to include in the ratio during reweighting.

            Returns
            -------
            weights: array-like,
                A list of sample weights.

                """
        weights = []

        # Shared priors - these will form a ratio
        if prior_names is not None:
            shared_parameters = {key: self.posterior[key] for key in new_prior if
                                 key in old_prior and key in prior_names}
        else:
            shared_parameters = {key: self.posterior[key] for key in new_prior if key in old_prior}
        parameters = [{key: self.posterior[key][i] for key in shared_parameters.keys()}
                      for i in range(len(self.posterior))]

        for i in range(len(self.posterior)):
            weight = 1
            for prior_key in shared_parameters.keys():
                val = self.posterior[prior_key][i]
                weight *= new_prior.evaluate_constraints(parameters[i])
                weight *= new_prior[prior_key].prob(val) / old_prior[prior_key].prob(val)

            weights.append(weight)

        return weights

1248

Moritz's avatar
Moritz committed
1249
class ResultList(list):
1250 1251 1252 1253 1254 1255 1256 1257 1258

    def __init__(self, results=None):
        """ A class to store a list of :class:`bilby.core.result.Result` objects
        from equivalent runs on the same data. This provides methods for
        outputing combined results.

        Parameters
        ----------
        results: list
Moritz's avatar
Moritz committed
1259
            A list of `:class:`bilby.core.result.Result`.
1260
        """
Moritz's avatar
Moritz committed
1261 1262 1263
        list.__init__(self)
        for result in results:
            self.append(result)
1264

Moritz's avatar
Moritz committed
1265
    def append(self, result):