bin/gwin_plot_acceptance_rate
#!/usr/bin/env python # Copyright (C) 2016 Christopher M. Biwer## This program is free software; you can redistribute it and/or modify it# under the terms of the GNU General Public License as published by the# Free Software Foundation; either version 3 of the License, or (at your# option) any later version.## This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General# Public License for more details.## You should have received a copy of the GNU General Public License along# with this program; if not, write to the Free Software Foundation, Inc.,# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import argparseimport loggingimport sys from matplotlib import useuse('agg')from matplotlib import pyplot as plt from pycbc import results from gwin import __version__from gwin.io.hdf import InferenceFile # command line usageparser = argparse.ArgumentParser( usage="pycbc_inference_plot_acceptance_rate [--options]", description="Plots histogram of the fractions of steps accepted by walkers.")parser.add_argument('--version', action='version', version=__version__, help='show version number and exit') # add data optionsparser.add_argument("--input-file", type=str, required=True, help="Path to input HDF file.") # output plot optionsparser.add_argument("--output-file", type=str, required=True, help="Path to output plot.") # add walkers to plotparser.add_argument("--walkers", type=int, nargs='+', default=None, help="Specify walkers whose acceptance fraction would " "be plotted. The acceptance fraction for a walker is the " "fraction of steps accepted by it. Default is plot for " "all walkers.") # add temperatures if using parallel tempered samplersparser.add_argument("--temps", type=int, nargs='+', default=None, help="Specify temperatures, the acceptance fraction of " "whose walkers would be plotted. Default is plot for all " "temperatures.") # add number of bins for histogramparser.add_argument("--bins", type=int, default=10, help="Specify number of bins for the histogram plot.") # verbose optionparser.add_argument("--verbose", action="store_true", default=False, help="") # parse the command lineopts = parser.parse_args() # setup logif opts.verbose: log_level = logging.DEBUGelse: log_level = logging.WARNlogging.basicConfig(format="%(asctime)s : %(message)s", level=log_level) # if using a parallel-tempered sampler, then# add the temperature arguments if it is specifiedadditional_args = {}if opts.temps is not None: additional_args['temps'] = opts.temps # read input filelogging.info("Reading input file")fp = InferenceFile(opts.input_file, "r")acceptance_fraction = fp.read_acceptance_fraction(walkers=opts.walkers, **additional_args)# plot acceptance rate and drawn valueslogging.info("Plotting acceptance fraction")fig = plt.figure()plt.hist(acceptance_fraction, opts.bins, histtype="step", lw=2, normed=True)plt.ylabel("Number of walkers")plt.xlabel("Mean Acceptance Rate") # save figure with meta-datacaption = """This plot shows a histogram of the acceptance rate of thewalkers. The acceptance rate of a walker is the fraction of steps acceptedby it."""results.save_fig_with_metadata(fig, opts.output_file, cmd=" ".join(sys.argv), title="Acceptance Rate", caption=caption)plt.close() # exitfp.close()logging.info("Done")