From bbffcf308d82549c11dbe1f2eaea61f5fc59ba90 Mon Sep 17 00:00:00 2001
From: Matthew Pitkin <matthew.pitkin@ligo.org>
Date: Mon, 1 Apr 2019 17:03:42 +0100
Subject: [PATCH] Start attempt at adding an add method to results to combine
 samples

---
 bilby/core/result.py               | 34 ++++++++++++++++++++++++++++--
 bilby/core/sampler/base_sampler.py |  2 +-
 bilby/core/sampler/cpnest.py       |  1 +
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/bilby/core/result.py b/bilby/core/result.py
index aaf0a81df..94b247a4f 100644
--- a/bilby/core/result.py
+++ b/bilby/core/result.py
@@ -98,7 +98,7 @@ class Result(object):
                  log_evidence_err=np.nan, log_noise_evidence=np.nan,
                  log_bayes_factor=np.nan, log_likelihood_evaluations=None,
                  log_prior_evaluations=None, sampling_time=None, nburn=None,
-                 walkers=None, max_autocorrelation_time=None,
+                 walkers=None, max_autocorrelation_time=None, use_ratio=None,
                  parameter_labels=None, parameter_labels_with_unit=None,
                  gzip=False, version=None):
         """ A class to store the results of the sampling run
@@ -137,6 +137,9 @@ class Result(object):
             The samplers taken by a ensemble MCMC samplers
         max_autocorrelation_time: float
             The estimated maximum autocorrelation time for MCMC samplers
+        use_ratio: bool
+            A boolean stating whether the likelihood ratio, as opposed to the
+            likelihood was used during sampling
         parameter_labels, parameter_labels_with_unit: list
             Lists of the latex-formatted parameter labels
         gzip: bool
@@ -169,6 +172,7 @@ class Result(object):
         self.nested_samples = nested_samples
         self.walkers = walkers
         self.nburn = nburn
+        self.use_ratio = use_ratio
         self.log_evidence = log_evidence
         self.log_evidence_err = log_evidence_err
         self.log_noise_evidence = log_noise_evidence
@@ -388,7 +392,7 @@ class Result(object):
             'log_noise_evidence', 'log_bayes_factor', 'priors', 'posterior',
             'injection_parameters', 'meta_data', 'search_parameter_keys',
             'fixed_parameter_keys', 'constraint_parameter_keys',
-            'sampling_time', 'sampler_kwargs',
+            'sampling_time', 'sampler_kwargs', 'use_ratio',
             'log_likelihood_evaluations', 'log_prior_evaluations', 'samples',
             'nested_samples', 'walkers', 'nburn', 'parameter_labels',
             'parameter_labels_with_unit', 'version']
@@ -1167,6 +1171,32 @@ class Result(object):
                                  "keyword argument, e.g. " + caller_func.__name__ + "(outdir='.')")
         return outdir
 
+    def __add__(self, other):
+        """
+        Method to add two Results objects.
+        """
+
+        if not isinstance(other, Result):
+            raise TypeError("Trying to add a non-Result object to a Result "
+                            "object")
+
+        # check that the results have some common features
+
+        # check parameters are the same
+        if not set(self.search_parameter_keys) == set(other.search_parameter_keys):
+            raise ValueError("Results being added contain inconsistent parameters")
+
+        if self.log_noise_evidence != other.log_noise_evidence:
+            raise ValueError("Results being added do not have consistent "
+                             "noise evidences")
+
+        # check priors are the same
+        if self.priors is not None and other.priors is not None:
+            for p in self.search_parameter_keys:
+                if not self.priors[p] == other.priors[p]:
+                    raise ValueError("Results being added used inconsistent "
+                                     "priors") 
+
 
 def plot_multiple(results, filename=None, labels=None, colours=None,
                   save=True, evidences=False, **kwargs):
diff --git a/bilby/core/sampler/base_sampler.py b/bilby/core/sampler/base_sampler.py
index daa204293..da9dd6e02 100644
--- a/bilby/core/sampler/base_sampler.py
+++ b/bilby/core/sampler/base_sampler.py
@@ -213,7 +213,7 @@ class Sampler(object):
             constraint_parameter_keys=self._constraint_keys,
             priors=self.priors, meta_data=self.meta_data,
             injection_parameters=self.injection_parameters,
-            sampler_kwargs=self.kwargs)
+            sampler_kwargs=self.kwargs, use_ratio=self.use_ratio)
 
         if result_class is None:
             result = Result(**result_kwargs)
diff --git a/bilby/core/sampler/cpnest.py b/bilby/core/sampler/cpnest.py
index eec05efc3..45f87ba6c 100644
--- a/bilby/core/sampler/cpnest.py
+++ b/bilby/core/sampler/cpnest.py
@@ -86,6 +86,7 @@ class Cpnest(NestedSampler):
             out.plot()
 
         self.result.posterior = DataFrame(out.posterior_samples)
+        self.result.nested_samples = DataFrame(out.get_nested_samples(filename=None))
         self.result.posterior.rename(columns=dict(
             logL='log_likelihood', logPrior='log_prior'), inplace=True)
         self.result.log_evidence = out.NS.state.logZ
-- 
GitLab