gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_plot_banks
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2017 Sarah Caudill, Shasvath Kapadia
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 ### A program to produce a variety of plots from the template bank and svd banks
20 
21 #
22 # =============================================================================
23 #
24 # Preamble
25 #
26 # =============================================================================
27 #
28 
29 from optparse import OptionParser
30 
31 import numpy
32 
33 from ligo.lw import ligolw
34 from ligo.lw import lsctables
35 from ligo.lw import utils as ligolw_utils
36 from lal import CacheEntry
37 
38 from gstlal import svd_bank
39 from gstlal import dagparts
40 from gstlal.plots import bank as plotbank
41 
42 #
43 # =============================================================================
44 #
45 # Command Line
46 #
47 # =============================================================================
48 #
49 
50 def parse_command_line():
51  parser = OptionParser(description = __doc__)
52  parser.add_option("--output-dir", metavar = "output-dir", default = ".", help = "Provide an output directory.")
53  parser.add_option("--plot-svd-bank", default = False, action = "store_true", help = "Make svd bank plots.")
54  parser.add_option("--svd-bank-cache", metavar = "filename", help = "Set the path to the svd bank cache file.")
55  parser.add_option("--plot-template-bank", default = False, action = "store_true", help = "Make template bank plots.")
56  parser.add_option("--template-bank-file", metavar = "filename", help = "Path to the template bank file.")
57 
58  options, filenames = parser.parse_args()
59 
60  if options.plot_template_bank:
61  if options.template_bank_file is None:
62  raise ValueError("Must specify --template-bank-file for template bank plotting.")
63 
64  if options.plot_svd_bank:
65  if options.svd_bank_cache is None:
66  raise ValueError("Must specify --svd-bank-cache for SVD bank plots.")
67 
68  return options, filenames
69 
70 ###################################
71 #
72 # Main
73 #
74 ###################################
75 
76 # Content Handler
77 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
78  pass
79 lsctables.use_in(LIGOLWContentHandler)
80 
81 options, filenames = parse_command_line()
82 
83 filename_template = dagparts.T050017_filename('H1L1V1', 'GSTLAL_INSPIRAL_PLOTBANKS_%s', (0, 0), '.png', path = options.output_dir)
84 
85 # Make svd bank plots
86 if options.plot_svd_bank:
87 
88  # First, read in the svd bank cache files
89  svd_bank_files = [CacheEntry(line) for line in open(options.svd_bank_cache)]
90 
91  # Then, read in the svd banks
92  svd_banks = {cacheentry.description: svd_bank.read_banks(cacheentry.path, svd_bank.DefaultContentHandler) for cacheentry in svd_bank_files}
93 
94  # Determine the min and max mchirp and chi values in each split bank. Store the values
95  # as tuples in a list. The tuple will be (file_tag, min_mchirp, max_mchirp, min_chi, max_chi)
96  edgevalues = []
97  for tag, sngl_table_list in svd_banks.items():
98 
99  for bank in sngl_table_list:
100  rows = bank.sngl_inspiral_table
101  chirpmasses = [row.mchirp for idx,row in enumerate(rows)]
102  chivals = [chi(row) for idx,row in enumerate(rows)]
103  edgevalues.append((tag, min(chirpmasses), max(chirpmasses), min(chivals), max(chivals)))
104 
105  # Exract the list of file_tags for easier handling
106  file_tags = [params[0] for params in edgevalues]
107 
108  tag_num = [int(''.join(c for c in tag if c.isdigit())) for tag in file_tags]
109 
110  # Create plots
111  plotbank.color_split_bank_by_svd(filename_template, tag_num,
112  [params[1] for params in edgevalues], [params[2] for params in edgevalues],
113  [params[3] for params in edgevalues], [params[4] for params in edgevalues])
114 
115 # Make template bank plots
116 if options.plot_template_bank:
117 
118  # Read template bank file
119  tmpltxmldoc = ligolw_utils.load_filename(options.template_bank_file, verbose = True, contenthandler = LIGOLWContentHandler)
120  # Extract sngl_inspiral table
121  sngl_inspiral_table = lsctables.SnglInspiralTable.get_table(tmpltxmldoc)
122 
123  # Create plots
124  plotbank.plot_template_bank(filename_template, sngl_inspiral_table)