Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom function to read MultiNest output data #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion bagpipes/fitting/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import os
import re
import time
import warnings
import h5py
Expand Down Expand Up @@ -44,6 +45,41 @@
from .posterior import posterior


def _read_multinest_data(filename):
"""
Read MultiNest data.

By default, Fortran drops the "E" symbol for 3-digit exponent output
(e.g., '0.148232-104'). This impacts the output files currently
being written by MultiNest. For such caes, this reader inserts
the "E" symbol into the number string so that the number can be
converted to a float.

Parameters
----------
filename : str
The filename to read.
"""
# count the columns in the first row of data;
# without a converter, genfromtxt will read 3-digit exponents as np.nan
ncolumns = np.genfromtxt(filename, max_rows=1).shape[0]

# insert "E" before the "+" or "-" exponent symbol if it is missing,
# so the string can be converted to a float
# '1.148232-104' -> 1.148232e-104
# '1.148232+104' -> 1.148232e+104
# '-1.148232-104' -> -1.148232e-104
# '+1.148232+104' -> 1.148232e+104
# '0.148232-104' -> 1.482320e-105
# '0.148232E-10' -> 1.482320e-011
# '1.148232' -> 1.48232e+000
convert = lambda s: float(re.sub(r'(\d)([\+\-])(\d)', r'\1E\2\3',
s.decode()))
converters = dict(zip(range(ncolumns), [convert] * ncolumns))

return np.genfromtxt(filename, converters=converters)


class fit(object):
""" Top-level class for fitting models to observational data.
Interfaces with MultiNest to sample from the posterior distribution
Expand Down Expand Up @@ -169,7 +205,8 @@ def fit(self, verbose=False, n_live=400, use_MPI=True, sampler="multinest"):

# Load MultiNest outputs and save basic quantities to file.
if sampler == "multinest":
samples2d = np.loadtxt(self.fname + "post_equal_weights.dat")
multinest_fname = self.fname + 'post_equal_weights.dat'
samples2d = _read_multinest_data(multinest_fname)
lnz_line = open(self.fname + "stats.dat").readline().split()
self.results["samples2d"] = samples2d[:, :-1]
self.results["lnlike"] = samples2d[:, -1]
Expand Down