gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_plot_snr
1 #!/usr/bin/env python3
2 """
3 Plotter for gstlal_inspiral_calc_snr
4 """
5 from optparse import OptionParser
6 import numpy
7 import os
8 import sys
9 
10 from gstlal import svd_bank_snr
11 from gstlal.plots import snr as plotsnr
12 
13 from lal.utils import CacheEntry
14 
15 def parse_command_line():
16  parser = OptionParser(description = __doc__)
17 
18  parser.add_option("-o", "--outdir", metavar = "directory", help = "The output directory for the SNR plot (require).")
19  parser.add_option("-i", "--input", metavar = "cache", help = "The input cache containing SNR urls (require).")
20  parser.add_option("-f", "--format", metavar = "file_type", help = "The format of the output plot (default = .png).")
21  parser.add_option("--center", metavar = "gpsSeconds", type = "float", help = "Center the plot to --center (optional).")
22  parser.add_option("--width", metavar = "inch", default = 8, type = "int", help = "The output width of the figure.")
23  parser.add_option("--span", metavar = "seconds", type = "float", help = "The time span around --center (optional).")
24  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
25 
26  options, args = parser.parse_args()
27 
28  if options.input is None:
29  raise ValueError("Missing --input.")
30  if options.outdir is None:
31  raise ValueError("Missing --outdir.")
32  if options.center is not None and options.span is None:
33  raise ValueError("--span should also be provided if --center is provided.")
34  if options.span is not None and options.center is None:
35  raise ValueError("--center should also be provided if --span is provided.")
36 
37  return options
38 
39 options = parse_command_line()
40 
41 suffix = ".png" if (options.format is None) else options.format
42 urls = []
43 description = None
44 segment = None
45 with open(options.input) as cache:
46  for line in cache.readlines():
47  entry = CacheEntry(line)
48  urls.append(entry.url)
49  if description is None and segment is None:
50  description = entry.description
51  segment = "-" + str(entry.segment[0]) + "-" + str(entry.segment[1] - entry.segment[0])
52  else:
53  assert description + segment == entry.description + "-" + str(entry.segment[0]) + "-" + str(entry.segment[1] - entry.segment[0]), "Cannot plot snrs with different segments or for different template."
54 
55 # done checking file consistency, overwrite output name with customized segment
56 if options.center and options.span:
57  segment = "-" + str(int(options.center - options.span)) + "-" + str(int(options.span * 2))
58 
59 SNRs_dict = {}
60 autocorrelations_dict = {}
61 for url in urls:
62  snr_dict, ac_dict = svd_bank_snr.read_xmldoc(svd_bank_snr.read_url(url, svd_bank_snr.SNRContentHandler, verbose = options.verbose))
63  SNRs_dict.update(snr_dict)
64  autocorrelations_dict.update(ac_dict)
65 snrs_groups = zip(*SNRs_dict.values())
66 acs_groups = zip(*autocorrelations_dict.values())
67 
68 #=============================================================================================
69 #
70 # Plot SNRs
71 #
72 #=============================================================================================
73 for snrs_group in snrs_groups:
74  figure = plotsnr.plot_snr(dict(zip(SNRs_dict.keys(), zip(snrs_group))), width = options.width, center = options.center, span = options.span, verbose = options.verbose)
75  if len(zip(*SNRs_dict.values())) == 1:
76  figure.savefig(os.path.join(options.outdir, "%s-" % "".join(sorted(SNRs_dict.keys())) + description + segment + suffix))
77  else:
78  figure.savefig(os.path.join(options.outdir, "%s-" % "".join(sorted(SNRs_dict.keys())) + description.replace("ALL_ALL", "_".join(snrs_group[0].name.split("_")[1:3])) + segment + suffix))
79 
80 
81 if len(snrs_groups) != len(acs_groups):
82  raise ValueError("The number of SNR time series and template autocorrelations does not matched.")
83 
84 #=============================================================================================
85 #
86 # Plot SNRs with autocorrelation
87 #
88 #=============================================================================================
89 # This is only useful when SNRs are complex time series.
90 if numpy.iscomplexobj(SNRs_dict.values()[0][0].data.data):
91  row = 0
92  for snrs_group, acs_group in zip(snrs_groups, acs_groups):
93  figure = plotsnr.plot_snr_with_ac(dict(zip(SNRs_dict.keys(), zip(snrs_group))), dict(zip(autocorrelations_dict.keys(), zip(acs_group))), width = options.width, ref_trigger_time = options.center, verbose = options.verbose)
94 
95  all_instruments = set(SNRs_dict) & set(autocorrelations_dict)
96  # just pick one, they must be the same length
97  if len(zip(*SNRs_dict.values())) == 1:
98  figure.savefig(os.path.join(options.outdir, "%s-" % "".join(sorted(all_instruments)) + description + "_with_autocorrlation" + segment+ suffix))
99  else:
100  figure.savefig(os.path.join(options.outdir, "%s-" % "".join(sorted(all_instruments)) + description.replace("ALL_ALL", "_".join(snrs_group[0].name.split("_")[1:3])) + "_with_autocorrlation" + segment + suffix))
101  row += 1
def plot_snr_with_ac
Definition: snr.py:51