Skip to content

Commit

Permalink
make start file from dynesty output (#3444)
Browse files Browse the repository at this point in the history
* script to convert dynesty to emcee_pt_file

* test

* update

* update

* fixes

* update

* fixes
  • Loading branch information
ahnitz authored Oct 2, 2020
1 parent ee9ae79 commit c2dc94d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions bin/inference/pycbc_inference_start_from_samples
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/env python
""" Convert inference file to parallel temperred compatible start format
"""

import argparse
import numpy
import h5py
from numpy.random import choice
from pycbc.inference.io import loadfile
from pycbc.inference.sampler import samplers

parser = argparse.ArgumentParser()
parser.add_argument('--input-file')
parser.add_argument('--output-file')
parser.add_argument('--sampler', default='emcee_pt',
help="The output sampler type, if none, we assume emcee_pt")
parser.add_argument('--ntemps', type=int)
parser.add_argument('--nwalkers', type=int)
args = parser.parse_args()

# populate an emcee start file with
# values chosen from a dynesty file
# each temperature and walker will get a random
# point from the dynesty output

ntemps = args.ntemps
nwalkers = args.nwalkers

f = loadfile(args.input_file)
params = list(f.variable_params) + ['loglikelihood']
samples = f.read_samples(params)
nsample = len(samples)

# These are the ids we'll use for the temps / walkers
use = choice(nsample, replace=False, size=ntemps * nwalkers)

o = loadfile(args.output_file, 'w', filetype=samplers[args.sampler]._io.name)
for k in params:
data = samples[k][use]
o['samples/' + k] = data.reshape(ntemps, nwalkers, 1)

o.attrs['static_params'] = []
o.attrs['variable_params'] = f.variable_params
o.create_group('sampler_info')
o['sampler_info'].attrs['nchains'] = nwalkers

o.close()
f.close()

0 comments on commit c2dc94d

Please sign in to comment.