gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_plot_psd_horizon
1 #!/usr/bin/env python3
2 
3 """ Plot horizon history from ranking statistics"""
4 
5 import sys
6 import os
7 import itertools
8 import matplotlib
9 matplotlib.use("Agg")
10 from matplotlib import pyplot
11 from optparse import OptionParser
12 import numpy
13 
14 from gstlal import far
15 from gstlal.plots import util as plotutil
16 from lal.utils import CacheEntry
17 
18 
19 def parse_command_line():
20  parser = OptionParser(description = __doc__)
21 
22  parser.add_option("-o", "--outdir", default = ".", help = "output directory for plots (Default: current directory)" )
23  parser.add_option("-v", "--verbose", action = "store_true", help = "be verbose")
24 
25  options, args = parser.parse_args()
26 
27  if len(args) == 0:
28  raise ValueError("Ranking statistics cannot be empty.")
29 
30  return options, args
31 
32 options, caches = parse_command_line()
33 
34 urls = [CacheEntry(line).url for cache in caches for line in open(cache)]
35 
36 for key, group in itertools.groupby(sorted(urls,key = lambda x: CacheEntry.from_T050017(x).description), lambda x: CacheEntry.from_T050017(x).description):
37  rankingstat = far.marginalize_pdf_urls(list(group), "RankingStat", verbose = options.verbose)
38  horizon_history_dict = rankingstat.numerator.horizon_history
39 
40  fig, ax = pyplot.subplots(1, 2, figsize=(12,4))
41  pyplot.tight_layout(pad = 2.5, w_pad = 2.5, h_pad = 2.5)
42 
43  gmint = []
44  for detector, horizon_history in horizon_history_dict.items():
45  GPSTime = numpy.array(horizon_history.keys())
46  horizon_dist = horizon_history.values()
47 
48  minh, maxh = (float("inf"), 0)
49  maxh = max(maxh, max(horizon_dist))
50  minh = min(minh, min(horizon_dist))
51  mint = int(GPSTime.min())
52  SinceGPSTime = (GPSTime - mint)/1000.
53  binvec = numpy.linspace(minh, maxh, 25)
54  gmint.append(mint)
55 
56  ax[0].semilogy(SinceGPSTime, horizon_dist, "x", color = plotutil.colour_from_instruments([detector]), label = detector)
57  ax[1].hist(horizon_dist, binvec, alpha = 0.5, color = plotutil.colour_from_instruments([detector]), label = detector)
58 
59  if options.verbose:
60  sys.stderr.write("plotting " + key + ".png\n")
61 
62  #pyplot.suptitle("%s : Horizon Distance" % detector)
63  ax[0].set_xlabel("Time (ks) from GPS {:d}".format(min(gmint)))
64  ax[0].set_ylabel("Mpc")
65  ax[0].legend(loc = "best")
66  ax[0].grid()
67 
68  ax[1].set_xlabel("Mpc")
69  ax[1].set_ylabel("Count")
70  ax[1].legend(loc = "best")
71  fig.savefig(os.path.join(options.outdir, key + ".png"))
72  pyplot.close()
73 
74 if options.verbose:
75  sys.stderr.write("done\n")
def group
group a list roughly according to the distribution in parts, e.g.