forked from martinmcallister/lfit_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (24 loc) · 982 Bytes
/
utils.py
File metadata and controls
28 lines (24 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
import pandas as pd
import emcee
import h5py
def read_chain(chain_fname):
"""
Reads an old style text chain or a new style HDF5 chain
Returns pandas DataFrame and list of variable names
"""
try:
df = pd.read_csv(chain_fname, delim_whitespace=True)
colKeys = list(df.columns.values)[1:-1]
except UnicodeDecodeError:
reader = emcee.backends.HDFBackend(chain_fname, read_only=True)
samples = reader.get_chain(discard=0, flat=True, thin=1)
nwalkers, npars = reader.shape
nsamples = samples.size // npars // nwalkers
with h5py.File(chain_fname, "r") as f:
colKeys = list(f["mcmc"].attrs["var_names"])
df = pd.DataFrame(samples, columns=colKeys)
df["ln_prob"] = reader.get_log_prob(discard=0, flat=True, thin=1)
nsamples = samples.size // npars // nwalkers
df["walker_no"] = np.array(list(range(nwalkers)) * nsamples)
return colKeys, df