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

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

7
import numpy as np
8
import deepdish
9
import pandas as pd
10
import corner
11
import scipy.stats
12
import matplotlib
13
import matplotlib.pyplot as plt
14

15
from . import utils
Colm Talbot's avatar
Colm Talbot committed
16 17 18
from .utils import (logger, infer_parameters_from_function,
                    check_directory_exists_and_if_not_mkdir)
from .prior import Prior, PriorDict, DeltaFunction
19

20

21
def result_file_name(outdir, label):
22 23 24 25 26 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

    Returns
    -------
    str: File name of the output file
    """
35 36 37
    return '{}/{}_result.h5'.format(outdir, label)


38
def read_in_result(filename=None, outdir=None, label=None):
Gregory Ashton's avatar
Gregory Ashton committed
39 40 41 42 43 44
    """ Read in a saved .h5 data file

    Parameters
    ----------
    filename: str
        If given, try to load from this filename
45 46
    outdir, label: str
        If given, use the default naming convention for saved results file
Gregory Ashton's avatar
Gregory Ashton committed
47

48 49
    Returns
    -------
Colm Talbot's avatar
Colm Talbot committed
50
    result: bilby.core.result.Result
51

Moritz Huebner's avatar
Moritz Huebner committed
52
    Raises
53 54
    -------
    ValueError: If no filename is given and either outdir or label is None
Colm Talbot's avatar
Colm Talbot committed
55
                If no bilby.core.result.Result is found in the path
Gregory Ashton's avatar
Gregory Ashton committed
56 57 58

    """
    if filename is None:
59 60 61 62
        if (outdir is None) and (label is None):
            raise ValueError("No information given to load file")
        else:
            filename = result_file_name(outdir, label)
63
    if os.path.isfile(filename):
64
        return Result(**deepdish.io.load(filename))
65
    else:
66 67 68 69 70 71 72 73 74 75 76 77 78
        raise IOError("No result '{}' found".format(filename))


class Result(object):
    def __init__(self, label='no_label', outdir='.', sampler=None,
                 search_parameter_keys=None, fixed_parameter_keys=None,
                 priors=None, sampler_kwargs=None, injection_parameters=None,
                 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,
                 sampling_time=None, nburn=None, walkers=None,
                 max_autocorrelation_time=None, parameter_labels=None,
79
                 parameter_labels_with_unit=None, version=None):
80
        """ A class to store the results of the sampling run
81 82 83

        Parameters
        ----------
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        label, outdir, sampler: str
            The label, output directory, and sampler used
        search_parameter_keys, fixed_parameter_keys: list
            Lists of the search and fixed parameter keys. Elemenents of the
            list should be of type `str` and matchs the keys of the `prior`
        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
        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
        parameter_labels, parameter_labels_with_unit: list
            Lists of the latex-formatted parameter labels
115 116 117
        version: str,
            Version information for software used to generate the result. Note,
            this information is generated when the result object is initialized
118 119 120 121

        Note:
            All sampling output parameters, e.g. the samples themselves are
            typically not given at initialisation, but set at a later stage.
122 123

        """
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        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
        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
        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
        self.sampling_time = sampling_time
147
        self.version = version
Colm Talbot's avatar
Colm Talbot committed
148
        self.max_autocorrelation_time = max_autocorrelation_time
149 150

    def __str__(self):
151
        """Print a summary """
152 153
        if getattr(self, 'posterior', None) is not None:
            if getattr(self, 'log_noise_evidence', None) is not None:
154 155 156 157
                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"
158
                        .format(len(self.posterior), self.log_noise_evidence, self.log_evidence,
159 160 161 162 163
                                self.log_evidence_err, self.log_bayes_factor,
                                self.log_evidence_err))
            else:
                return ("nsamples: {:d}\n"
                        "log_evidence: {:6.3f} +/- {:6.3f}\n"
164
                        .format(len(self.posterior), self.log_evidence, self.log_evidence_err))
165 166
        else:
            return ''
167

168 169 170 171 172 173
    @property
    def priors(self):
        if self._priors is not None:
            return self._priors
        else:
            raise ValueError('Result object has no priors')
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    @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")
193

194 195 196 197 198
    @property
    def samples(self):
        """ An array of samples """
        if self._samples is not None:
            return self._samples
199
        else:
200
            raise ValueError("Result object has no stored samples")
201

202 203 204
    @samples.setter
    def samples(self, samples):
        self._samples = samples
205

206 207 208 209 210 211 212
    @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")
213

214 215 216
    @nested_samples.setter
    def nested_samples(self, nested_samples):
        self._nested_samples = nested_samples
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    @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

254 255 256 257 258 259 260 261 262 263 264
    @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

265
    def _get_save_data_dictionary(self):
266
        # This list defines all the parameters saved in the result object
267 268 269 270 271 272 273
        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',
            'fixed_parameter_keys', 'sampling_time', 'sampler_kwargs',
            'log_likelihood_evaluations', 'samples', 'nested_samples',
            'walkers', 'nburn', 'parameter_labels',
274
            'parameter_labels_with_unit', 'version']
275 276 277 278 279 280 281 282
        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
283

Colm Talbot's avatar
Colm Talbot committed
284 285 286 287 288 289 290 291 292 293
    def save_to_file(self, overwrite=False):
        """
        Writes the Result to a deepdish h5 file

        Parameters
        ----------
        overwrite: bool, optional
            Whether or not to overwrite an existing result file.
            default=False
        """
294
        file_name = result_file_name(self.outdir, self.label)
295
        utils.check_directory_exists_and_if_not_mkdir(self.outdir)
296
        if os.path.isfile(file_name):
Colm Talbot's avatar
Colm Talbot committed
297 298 299 300 301 302 303 304
            if overwrite:
                logger.debug('Removing existing file {}'.format(file_name))
                os.remove(file_name)
            else:
                logger.debug(
                    'Renaming existing file {} to {}.old'.format(file_name,
                                                                 file_name))
                os.rename(file_name, file_name + '.old')
305

Gregory Ashton's avatar
Gregory Ashton committed
306
        logger.debug("Saving result to {}".format(file_name))
307 308

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

313 314 315 316 317
        # Convert callable sampler_kwargs to strings to avoid pickling issues
        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'])
318

Gregory Ashton's avatar
Fix #49  
Gregory Ashton committed
319
        try:
320
            deepdish.io.save(file_name, dictionary)
Gregory Ashton's avatar
Fix #49  
Gregory Ashton committed
321
        except Exception as e:
Gregory Ashton's avatar
Gregory Ashton committed
322
            logger.error("\n\n Saving the data has failed with the "
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
323
                         "following message:\n {} \n\n".format(e))
Gregory Ashton's avatar
Gregory Ashton committed
324

325
    def save_posterior_samples(self):
326
        """Saves posterior samples to a file"""
327
        filename = '{}/{}_posterior_samples.txt'.format(self.outdir, self.label)
328
        utils.check_directory_exists_and_if_not_mkdir(self.outdir)
329 330
        self.posterior.to_csv(filename, index=False, header=True)

Gregory Ashton's avatar
Gregory Ashton committed
331
    def get_latex_labels_from_parameter_keys(self, keys):
332 333 334 335 336 337 338 339 340 341 342 343 344
        """ 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
345 346 347
        for k in keys:
            if k in self.search_parameter_keys:
                idx = self.search_parameter_keys.index(k)
348
                latex_labels.append(self.parameter_labels_with_unit[idx])
Gregory Ashton's avatar
Gregory Ashton committed
349
            elif k in self.parameter_labels:
350
                latex_labels.append(k)
Gregory Ashton's avatar
Gregory Ashton committed
351
            else:
Colm Talbot's avatar
Colm Talbot committed
352
                logger.debug(
353 354
                    'key {} not a parameter label or latex label'.format(k))
                latex_labels.append(' '.join(k.split('_')))
355
        return latex_labels
Gregory Ashton's avatar
Gregory Ashton committed
356

Gregory Ashton's avatar
Gregory Ashton committed
357 358 359 360 361 362 363 364 365
    @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
366 367 368
        if self.covariance_matrix.ndim == 0:
            return np.sqrt(self.covariance_matrix)
        else:
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
369 370
            return 1 / np.sqrt(np.abs(np.linalg.det(
                1 / self.covariance_matrix)))
Gregory Ashton's avatar
Gregory Ashton committed
371

372 373
    @staticmethod
    def prior_volume(priors):
Gregory Ashton's avatar
Gregory Ashton committed
374 375 376 377 378 379
        """ 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,

380 381 382
        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
383 384 385 386

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

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    def get_one_dimensional_median_and_error_bar(self, key, fmt='.2f',
                                                 quantiles=[0.16, 0.84]):
        """ 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
        quantiles: list
            A length-2 list of the lower and upper-quantiles to calculate
            the errors bars for.

        Returns
        -------
403 404
        summary: namedtuple
            An object with attributes, median, lower, upper and string
405 406

        """
407 408
        summary = namedtuple('summary', ['median', 'lower', 'upper', 'string'])

409 410 411 412 413
        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)
414 415 416
        summary.median = quants[1]
        summary.plus = quants[2] - summary.median
        summary.minus = summary.median - quants[0]
417 418

        fmt = "{{0:{0}}}".format(fmt).format
419 420 421 422 423
        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
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    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,
                            title_fontsize=16, quantiles=[0.16, 0.84], dpi=300):
        """ Plot a 1D marginal density, either probablility or cumulative.

        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
        quantiles: list
            A length-2 list of the lower and upper-quantiles to calculate
            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()
471 472 473 474 475 476 477 478
        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
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
        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)
            ax.plot(theta, Prior.prob(theta), color='C2')

        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)
501 502 503
            plt.close(fig)
        else:
            return fig
Colm Talbot's avatar
Colm Talbot committed
504

505 506 507 508 509 510 511 512 513 514
    def plot_marginals(self, parameters=None, priors=None, titles=True,
                       file_base_name=None, bins=50, label_fontsize=16,
                       title_fontsize=16, quantiles=[0.16, 0.84], dpi=300):
        """ 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.
515
        priors: {bool (False), bilby.core.prior.PriorDict}
516
            If true, add the stored prior probability density functions to the
517
            one-dimensional marginal distributions. If instead a PriorDict
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
            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
            The fontsizes for the labels and titles
        quantiles: list
            A length-2 list of the lower and upper-quantiles to calculate
            the errors bars for.
        dpi: int
            Dots per inch resolution of the plot

        Returns
        -------
        """
        if isinstance(parameters, dict):
            plot_parameter_keys = list(parameters.keys())
Colm Talbot's avatar
Colm Talbot committed
542
            truths = parameters
543
        elif parameters is None:
Colm Talbot's avatar
Colm Talbot committed
544 545 546 547 548
            plot_parameter_keys = self.posterior.keys()
            if self.injection_parameters is None:
                truths = dict()
            else:
                truths = self.injection_parameters
549 550
        else:
            plot_parameter_keys = list(parameters)
Colm Talbot's avatar
Colm Talbot committed
551 552 553 554
            if self.injection_parameters is None:
                truths = dict()
            else:
                truths = self.injection_parameters
555 556

        if file_base_name is None:
Colm Talbot's avatar
Colm Talbot committed
557 558
            file_base_name = '{}/{}_1d/'.format(self.outdir, self.label)
            check_directory_exists_and_if_not_mkdir(file_base_name)
559 560

        if priors is True:
Colm Talbot's avatar
Colm Talbot committed
561 562
            priors = getattr(self, 'priors', dict())
        elif isinstance(priors, dict):
563
            pass
Colm Talbot's avatar
Colm Talbot committed
564 565
        elif priors in [False, None]:
            priors = dict()
566 567 568 569
        else:
            raise ValueError('Input priors={} not understood'.format(priors))

        for i, key in enumerate(plot_parameter_keys):
Colm Talbot's avatar
Colm Talbot committed
570 571
            if not isinstance(self.posterior[key].values[0], float):
                continue
Colm Talbot's avatar
Colm Talbot committed
572 573
            prior = priors.get(key, None)
            truth = truths.get(key, None)
Colm Talbot's avatar
Colm Talbot committed
574
            for cumulative in [False, True]:
575
                self.plot_single_density(
Colm Talbot's avatar
Colm Talbot committed
576 577
                    key, prior=prior, cumulative=cumulative, title=titles,
                    truth=truth, save=True, file_base_name=file_base_name,
Colm Talbot's avatar
Colm Talbot committed
578 579
                    bins=bins, label_fontsize=label_fontsize, dpi=dpi,
                    title_fontsize=title_fontsize, quantiles=quantiles)
580

581 582 583
    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
584 585 586

        Parameters
        ----------
587 588 589
        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.
590
        priors: {bool (False), bilby.core.prior.PriorDict}
591
            If true, add the stored prior probability density functions to the
592
            one-dimensional marginal distributions. If instead a PriorDict
593
            is provided, this will be plotted.
594 595 596 597 598
        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.
599 600 601 602 603 604
        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
605 606 607 608
        **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
            overridden.
Gregory Ashton's avatar
Gregory Ashton committed
609

610 611 612 613 614 615
        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
616 617 618 619
        Returns
        -------
        fig:
            A matplotlib figure instance
620

Gregory Ashton's avatar
Gregory Ashton committed
621
        """
622 623

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

Colm Talbot's avatar
Colm Talbot committed
627
        # bilby default corner kwargs. Overwritten by anything passed to kwargs
628 629 630
        defaults_kwargs = dict(
            bins=50, smooth=0.9, label_kwargs=dict(fontsize=16),
            title_kwargs=dict(fontsize=16), color='#0072C1',
631
            truth_color='tab:orange', quantiles=[0.16, 0.84],
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
632
            levels=(1 - np.exp(-0.5), 1 - np.exp(-2), 1 - np.exp(-9 / 2.)),
633
            plot_density=False, plot_datapoints=True, fill_contours=True,
634 635 636 637 638 639
            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)
640

641 642 643 644
        if 'lionize' in kwargs and kwargs['lionize'] is True:
            defaults_kwargs['truth_color'] = 'tab:blue'
            defaults_kwargs['color'] = '#FF8C00'

645 646 647
        defaults_kwargs.update(kwargs)
        kwargs = defaults_kwargs

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
        # 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
663 664 665 666 667 668 669 670
        # 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}

671 672 673 674 675 676 677 678 679
        # 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)
680

681
        # Get latex formatted strings for the plot labels
682 683
        kwargs['labels'] = kwargs.get(
            'labels', self.get_latex_labels_from_parameter_keys(
684
                plot_parameter_keys))
685

686 687 688 689
        # 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))

690 691
        # Create the data array to plot and pass everything to corner
        xs = self.posterior[plot_parameter_keys].values
692
        fig = corner.corner(xs, **kwargs)
693
        axes = fig.get_axes()
694 695 696

        #  Add the titles
        if titles and kwargs.get('quantiles', None) is not None:
697 698
            for i, par in enumerate(plot_parameter_keys):
                ax = axes[i + i * len(plot_parameter_keys)]
699 700
                if ax.title.get_text() == '':
                    ax.set_title(self.get_one_dimensional_median_and_error_bar(
701
                        par, quantiles=kwargs['quantiles']).string,
702 703 704
                        **kwargs['title_kwargs'])

        #  Add priors to the 1D plots
705 706 707
        if priors is True:
            priors = getattr(self, 'priors', False)
        if isinstance(priors, dict):
708 709
            for i, par in enumerate(plot_parameter_keys):
                ax = axes[i + i * len(plot_parameter_keys)]
710 711
                theta = np.linspace(ax.get_xlim()[0], ax.get_xlim()[1], 300)
                ax.plot(theta, priors[par].prob(theta), color='C2')
712 713 714 715
        elif priors in [False, None]:
            pass
        else:
            raise ValueError('Input priors={} not understood'.format(priors))
716

717
        if save:
718 719 720
            if filename is None:
                utils.check_directory_exists_and_if_not_mkdir(self.outdir)
                filename = '{}/{}_corner.png'.format(self.outdir, self.label)
Gregory Ashton's avatar
Gregory Ashton committed
721
            logger.debug('Saving corner plot to {}'.format(filename))
722
            fig.savefig(filename, dpi=dpi)
723
            plt.close(fig)
724

725
        return fig
726

Gregory Ashton's avatar
Gregory Ashton committed
727
    def plot_walkers(self, **kwargs):
MoritzThomasHuebner's avatar
A typo  
MoritzThomasHuebner committed
728
        """ Method to plot the trace of the walkers in an ensemble MCMC plot """
729
        if hasattr(self, 'walkers') is False:
Gregory Ashton's avatar
Gregory Ashton committed
730
            logger.warning("Cannot plot_walkers as no walkers are saved")
731
            return
732 733 734

        if utils.command_line_args.test:
            return
735 736 737

        nwalkers, nsteps, ndim = self.walkers.shape
        idxs = np.arange(nsteps)
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
738
        fig, axes = plt.subplots(nrows=ndim, figsize=(6, 3 * ndim))
739 740
        walkers = self.walkers[:, :, :]
        for i, ax in enumerate(axes):
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
741
            ax.plot(idxs[:self.nburn + 1], walkers[:, :self.nburn + 1, i].T,
742 743 744 745 746 747 748 749 750 751
                    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()
        filename = '{}/{}_walkers.png'.format(self.outdir, self.label)
Gregory Ashton's avatar
Gregory Ashton committed
752
        logger.debug('Saving walkers plot to {}'.format('filename'))
753
        utils.check_directory_exists_and_if_not_mkdir(self.outdir)
754
        fig.savefig(filename)
755
        plt.close(fig)
756

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    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,
                       maxl_label='max likelihood', dpi=300):
        """ 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.

        """
788 789 790 791 792 793

        # 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]

794 795 796 797
        xsmooth = np.linspace(np.min(x), np.max(x), npoints)
        fig, ax = plt.subplots()
        logger.info('Plotting {} draws'.format(ndraws))
        for _ in range(ndraws):
798
            s = model_posterior.sample().to_dict('records')[0]
799 800
            ax.plot(xsmooth, model(xsmooth, **s), alpha=0.25, lw=0.1, color='r',
                    label=draws_label)
801 802 803
        try:
            if all(~np.isnan(self.posterior.log_likelihood)):
                logger.info('Plotting maximum likelihood')
Colm Talbot's avatar
Colm Talbot committed
804
                s = model_posterior.iloc[self.posterior.log_likelihood.idxmax()]
805 806
                ax.plot(xsmooth, model(xsmooth, **s), lw=1, color='k',
                        label=maxl_label)
807
        except (AttributeError, TypeError):
808 809
            logger.debug(
                "No log likelihood values stored, unable to plot max")
810 811 812 813 814 815 816 817 818 819 820 821 822 823

        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:
824
            utils.check_directory_exists_and_if_not_mkdir(self.outdir)
825 826
            filename = '{}/{}_plot_with_data'.format(self.outdir, self.label)
        fig.savefig(filename, dpi=dpi)
827
        plt.close(fig)
828

829 830
    def samples_to_posterior(self, likelihood=None, priors=None,
                             conversion_function=None):
831
        """
832 833 834
        Convert array of samples to posterior (a Pandas data frame)

        Also applies the conversion function to any stored posterior
835

836 837
        Parameters
        ----------
Colm Talbot's avatar
Colm Talbot committed
838
        likelihood: bilby.likelihood.GravitationalWaveTransient, optional
839 840
            GravitationalWaveTransient likelihood used for sampling.
        priors: dict, optional
841
            Dictionary of prior object, used to fill in delta function priors.
842
        conversion_function: function, optional
843 844
            Function which adds in extra parameters to the data frame,
            should take the data_frame, likelihood and prior as arguments.
845
        """
846 847 848
        try:
            data_frame = self.posterior
        except ValueError:
849 850
            data_frame = pd.DataFrame(
                self.samples, columns=self.search_parameter_keys)
851
            for key in priors:
852
                if isinstance(priors[key], DeltaFunction):
853
                    data_frame[key] = priors[key].peak
854 855 856 857
                elif isinstance(priors[key], float):
                    data_frame[key] = priors[key]
            data_frame['log_likelihood'] = getattr(
                self, 'log_likelihood_evaluations', np.nan)
858
        if conversion_function is not None:
859
            data_frame = conversion_function(data_frame, likelihood, priors)
860
        self.posterior = data_frame
Gregory Ashton's avatar
Gregory Ashton committed
861

Colm Talbot's avatar
Colm Talbot committed
862
    def calculate_prior_values(self, priors):
863 864 865 866 867
        """
        Evaluate prior probability for each parameter for each sample.

        Parameters
        ----------
868
        priors: dict, PriorDict
869 870 871 872 873 874 875 876 877 878 879
            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)

Colm Talbot's avatar
Colm Talbot committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    def get_all_injection_credible_levels(self):
        """
        Get credible levels for all parameters in self.injection_parameters

        Returns
        -------
        credible_levels: dict
            The credible levels at which the injected parameters are found.
        """
        if self.injection_parameters is None:
            raise(TypeError, "Result object has no 'injection_parameters'. "
                             "Cannot copmute credible levels.")
        credible_levels = {key: self.get_injection_credible_level(key)
                           for key in self.search_parameter_keys
                           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

923
    def _check_attribute_match_to_other_object(self, name, other_object):
924 925 926 927 928 929 930 931 932 933 934 935 936 937
        """ 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

        """
938
        A = getattr(self, name, False)
939
        B = getattr(other_object, name, False)
Gregory Ashton's avatar
Gregory Ashton committed
940
        logger.debug('Checking {} value: {}=={}'.format(name, A, B))
941 942 943 944 945
        if (A is not False) and (B is not False):
            typeA = type(A)
            typeB = type(B)
            if typeA == typeB:
                if typeA in [str, float, int, dict, list]:
Gregory Ashton's avatar
Gregory Ashton committed
946 947 948 949
                    try:
                        return A == B
                    except ValueError:
                        return False
950 951 952
                elif typeA in [np.ndarray]:
                    return np.all(A == B)
        return False
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    @property
    def kde(self):
        """ Kernel density estimate built from the stored posterior

        Uses `scipy.stats.gaussian_kde` to generate the kernel density
        """
        try:
            return self._kde
        except AttributeError:
            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)

992 993

def plot_multiple(results, filename=None, labels=None, colours=None,
994
                  save=True, evidences=False, **kwargs):
995 996 997 998 999
    """ Generate a corner plot overlaying two sets of results

    Parameters
    ----------
    results: list
Colm Talbot's avatar
Colm Talbot committed
1000
        A list of `bilby.core.result.Result` objects containing the samples to
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
        plot.
    filename: str
        File name to save the figure to. If None (default), a filename is
        constructed from the outdir of the first element of results and then
        the labels for all the result files.
    labels: list
        List of strings to use when generating a legend. If None (default), the
        `label` attribute of each result in `results` is used.
    colours: list
        The colours for each result. If None, default styles are applied.
    save: bool
        If true, save the figure
    kwargs: dict
        All other keyword arguments are passed to `result.plot_corner`.
        However, `show_titles` and `truths` are ignored since they would be
        ambiguous on such a plot.
1017 1018 1019
    evidences: bool, optional
        Add the log-evidence calculations to the legend. If available, the
        Bayes factor will be used instead.
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

    Returns
    -------
    fig:
        A matplotlib figure instance

    """

    kwargs['show_titles'] = False
    kwargs['truths'] = None

    fig = results[0].plot_corner(save=False, **kwargs)
Gregory Ashton's avatar
Gregory Ashton committed
1032
    default_filename = '{}/{}'.format(results[0].outdir, 'combined')
1033 1034 1035 1036 1037 1038 1039
    lines = []
    default_labels = []
    for i, result in enumerate(results):
        if colours:
            c = colours[i]
        else:
            c = 'C{}'.format(i)
Gregory Ashton's avatar
Gregory Ashton committed
1040 1041
        hist_kwargs = kwargs.get('hist_kwargs', dict())
        hist_kwargs['color'] = c
1042 1043 1044 1045 1046
        fig = result.plot_corner(fig=fig, save=False, color=c, **kwargs)
        default_filename += '_{}'.format(result.label)
        lines.append(matplotlib.lines.Line2D([0], [0], color=c))
        default_labels.append(result.label)

Gregory Ashton's avatar
Gregory Ashton committed
1047 1048 1049 1050 1051
    # Rescale the axes
    for i, ax in enumerate(fig.axes):
        ax.autoscale()
    plt.draw()

1052 1053 1054
    if labels is None:
        labels = default_labels

1055 1056
    if evidences:
        if np.isnan(results[0].log_bayes_factor):
1057
            template = ' $\mathrm{{ln}}(Z)={lnz:1.3g}$'
1058
        else:
1059 1060 1061 1062
            template = ' $\mathrm{{ln}}(B)={lnbf:1.3g}$'
        labels = [template.format(lnz=result.log_evidence,
                                  lnbf=result.log_bayes_factor)
                  for ii, result in enumerate(results)]
1063

1064 1065
    axes = fig.get_axes()
    ndim = int(np.sqrt(len(axes)))
MoritzThomasHuebner's avatar
MoritzThomasHuebner committed
1066
    axes[ndim - 1].legend(lines, labels)
1067 1068 1069 1070 1071 1072 1073

    if filename is None:
        filename = default_filename

    if save:
        fig.savefig(filename)
    return fig
Colm Talbot's avatar
Colm Talbot committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115


def make_pp_plot(results, filename=None, save=True, **kwargs):
    """
    Make a P-P plot for a set of runs with injected signals.

    Parameters
    ----------
    results: list
        A list of Result objects, each of these should have injected_parameters
    filename: str, optional
        The name of the file to save, the default is "outdir/pp.png"
    save: bool, optional
        Whether to save the file, default=True
    kwargs:
        Additional kwargs to pass to matplotlib.pyplot.plot

    Returns
    -------
    fig:
        Matplotlib figure
    """
    fig = plt.figure()
    credible_levels = pd.DataFrame()
    for result in results:
        credible_levels = credible_levels.append(
            result.get_all_injection_credible_levels(), ignore_index=True)
    n_parameters = len(credible_levels.keys())
    x_values = np.linspace(0, 1, 101)
    for key in credible_levels:
        plt.plot(x_values, [sum(credible_levels[key].values < xx) /
                            len(credible_levels) for xx in x_values],
                 color='k', alpha=min([1, 4 / n_parameters]), **kwargs)
    plt.plot([0, 1], [0, 1], linestyle='--', color='r')
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.tight_layout()
    if save:
        if filename is None:
            filename = 'outdir/pp.png'
        plt.savefig(filename)
    return fig