Commit 5461d1f1 authored by Gregory Ashton's avatar Gregory Ashton Committed by Moritz Huebner

Fix sample_to_posterior bug: makes the priors optional

parent edf8d889
......@@ -983,6 +983,17 @@ class Result(object):
fig.savefig(filename, dpi=dpi)
plt.close(fig)
@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
def samples_to_posterior(self, likelihood=None, priors=None,
conversion_function=None):
"""
......@@ -1005,11 +1016,8 @@ class Result(object):
except ValueError:
data_frame = pd.DataFrame(
self.samples, columns=self.search_parameter_keys)
for key in priors:
if isinstance(priors[key], DeltaFunction):
data_frame[key] = priors[key].peak
elif isinstance(priors[key], float):
data_frame[key] = priors[key]
data_frame = self._add_prior_fixed_values_to_posterior(
data_frame, priors)
data_frame['log_likelihood'] = getattr(
self, 'log_likelihood_evaluations', np.nan)
if self.log_prior_evaluations is None:
......
......@@ -266,6 +266,17 @@ class TestResult(unittest.TestCase):
df = pd.read_csv(filename)
self.assertTrue(np.allclose(self.result.posterior.values, df.values))
def test_samples_to_posterior_simple(self):
self.result.posterior = None
x = [1, 2, 3]
y = [4, 6, 8]
self.result.samples = np.array([x, y]).T
self.result.samples_to_posterior()
self.assertTrue(all(self.result.posterior['x'] == x))
self.assertTrue(all(self.result.posterior['y'] == y))
self.assertTrue(np.all(
None == self.result.posterior.log_likelihood.values))
def test_samples_to_posterior(self):
self.result.posterior = None
x = [1, 2, 3]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment