forked from martinmcallister/lfit_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_input_file.py
More file actions
64 lines (54 loc) · 1.69 KB
/
update_input_file.py
File metadata and controls
64 lines (54 loc) · 1.69 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Update an existing mcmcfit input file using the results of a
previous chain.
"""
import argparse
import emcee
import h5py
import numpy as np
import pandas as pd
from utils import read_chain
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-t",
"--thin",
action="store",
type=int,
default=5,
help="thinning for chain (default=5)",
)
parser.add_argument(
"-d",
"--discard",
action="store",
type=int,
default=0,
help="thinning for chain (default=5)",
)
parser.add_argument("input_file", help="mcmc input file")
parser.add_argument("chain_file", help="chain output file")
args = parser.parse_args()
print("Reading in the chain")
colkeys, df = read_chain(args.chain_file)
# get median from chain
results = df.quantile(0.5, numeric_only=True)
# strip "core" from core params
results.index = [entry.replace("_core", "") for entry in results.index]
# replace lines in MCMC input file
line_fmt = "{:>10} {:1} {:>16.8f} {:>12} {:>16} {:>16} {:>12}\n"
mcmc_input = open(args.input_file).readlines()
for i, line in enumerate(mcmc_input):
if line.startswith("#"):
continue
fields = line.strip().split() # split line on whitespace
if len(fields) == 0:
continue
parname = fields[0]
if parname in results.index:
# we have an updated value for this parameter!
fields[2] = results[parname]
mcmc_input[i] = line_fmt.format(*fields)
with open(args.input_file, "w") as f:
for line in mcmc_input:
f.write(line)