3 # Copyright (C) 2017 Sarah Caudill, Shasvath Kapadia
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.
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.
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.
19 ### A program to produce a variety of plots from the template bank and svd banks
22 # =============================================================================
26 # =============================================================================
29 from optparse
import OptionParser
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
38 from gstlal
import svd_bank
39 from gstlal
import dagparts
40 from gstlal.plots
import bank as plotbank
43 # =============================================================================
47 # =============================================================================
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.")
58 options, filenames = parser.parse_args()
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.")
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.")
68 return options, filenames
70 ###################################
74 ###################################
77 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
79 lsctables.use_in(LIGOLWContentHandler)
81 options, filenames = parse_command_line()
83 filename_template = dagparts.T050017_filename(
'H1L1V1',
'GSTLAL_INSPIRAL_PLOTBANKS_%s', (0, 0),
'.png', path = options.output_dir)
86 if options.plot_svd_bank:
88 # First, read in the svd bank cache files
89 svd_bank_files = [CacheEntry(line) for line in open(options.svd_bank_cache)]
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}
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)
97 for tag, sngl_table_list in svd_banks.items():
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)))
105 # Exract the list of file_tags for easier handling
106 file_tags = [params[0]
for params in edgevalues]
108 tag_num = [int(
''.join(c
for c in tag
if c.isdigit())) for tag in file_tags]
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])
115 # Make template bank plots
116 if options.plot_template_bank:
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)
124 plotbank.plot_template_bank(filename_template, sngl_inspiral_table)