-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreweight_samples.py
54 lines (47 loc) · 1.81 KB
/
reweight_samples.py
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
#!/usr/bin/env python
import logging
import shutil
import argparse
import numpy
import pycbc
from pycbc.inference import io
from scipy.special import logsumexp
"""Reweights samples from a prior uniform in q > 1 to a prior uniform in
m1, m2.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--input-file', required=True)
parser.add_argument('--output-file', required=True)
parser.add_argument('--input-is-posterior', action='store_true')
opts = parser.parse_args()
pycbc.init_logging(True)
logging.info("Copying input to output")
shutil.copy(opts.input_file, opts.output_file)
logging.info('Reweighting samples')
fp = io.loadfile(opts.output_file, 'a')
input_is_posterior = opts.input_is_posterior
if input_is_posterior:
fp.attrs['filetype'] = 'posterior_file'
fp.close()
fp = io.loadfile(opts.output_file, 'a')
samples = fp.read_samples(['srcmtotal', 'q'])
else:
samples = fp.read_samples(['srcmtotal', 'q', 'logwt'],
raw_samples=True)
logjacobian = samples['2*log(1+q) - log(srcmtotal)']
if input_is_posterior:
logwt = -numpy.log(samples.size)
else:
logwt = samples['logwt']
logwt -= logjacobian
# renormalize and save
logz = fp.log_evidence[0]
lognorm = logsumexp(logwt - logz)
logging.info("lognorm is: %f", lognorm)
logwt -= lognorm
if input_is_posterior:
fp['samples/logwt'] = logwt
fp.attrs['filetype'] = 'dynesty_file'
else:
fp['samples/logwt'][:] = logwt
fp.close()