bin/gwin_plot_acl
#!/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 import numpy from matplotlib import useuse('agg')from matplotlib import pyplot as plt import pycbcfrom pycbc import resultsfrom pycbc.filter import autocorrelation from gwin import (__version__, option_utils) # command line usageparser = argparse.ArgumentParser( description="Histograms autocorrelation length from inference samples.") # verbose optionparser.add_argument("--verbose", action="store_true", default=False, help="Print logging info.")parser.add_argument('--version', action='version', version=__version__, help='show version number and exit')# output plot optionsparser.add_argument("--output-file", type=str, required=True, help="Path to output plot.")parser.add_argument("--bins", type=int, default=10, help="Number of bins in histogram.") # add results groupoption_utils.add_inference_results_option_group(parser) # parse the command lineopts = parser.parse_args() # setup logpycbc.init_logging(opts.verbose) # load the resultsfp, parameters, labels, _ = option_utils.results_from_cli(opts, load_samples=False) # calculate autocorrelation length for each walkerlogging.info("Calculating autocorrelation length")acls = []for param_name in parameters: # loop over walkers and save an autocorrelation length # for each walker for i in range(fp.nwalkers): y = fp.read_samples(param_name, walkers=i, thin_start=opts.thin_start, thin_interval=opts.thin_interval) acl = autocorrelation.calculate_acl(y[param_name], dtype=int) if acl == numpy.inf: acl = fp.niterations acls.append( acl ) # plot autocorrelation lengthlogging.info("Plotting autocorrelation lengths")fig = plt.figure()range_max = max(fp.acl, max(acls))y,x,patches = plt.hist(acls, opts.bins, range=(0,range_max), histtype="step") # get histogram bin widthpoly_xy = patches[0].get_xy()step = poly_xy[2][0] - poly_xy[0][0] plt.xlabel("Iteration")plt.ylabel(r'Autocorrelation Length for %s'%', '.join(labels))plt.ylim(0, int(1.1*y.max()))x_min = max(0, x.min()-2*step)plt.xlim(x_min, x.max()+2*step) # plot autocorrelation length saved in InferenceFileplt.vlines(fp.acl, 0, int(1.1*y.max())) # save figure with meta-datacaption_kwargs = { "parameters" : ", ".join(labels),}caption = """ The histogram (blue) is the autocorrelation length (ACL) from all the walker chains for the parameters. The vertical black line is the ACLread from the input file."""title = "Autocorrelation Length for {parameters}".format(**caption_kwargs)results.save_fig_with_metadata(fig, opts.output_file, cmd=" ".join(sys.argv), title=title, caption=caption)plt.close() # exitfp.close()logging.info("Done")