gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_svd_bank
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2010 Kipp Cannon, Chad Hanna, Leo Singer
4 # Copyright (C) 2009 Kipp Cannon, Chad Hanna
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2 of the License, or (at your
9 # option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 # Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 """Build time-sliced, SVD'd filter banks for use with gstlal_inspiral"""
20 
21 
22 #
23 #
24 # =============================================================================
25 #
26 # Preamble
27 #
28 # =============================================================================
29 #
30 
31 
32 from optparse import OptionParser
33 import numpy
34 
35 
36 import lal.series
37 from lal.utils import CacheEntry
38 from ligo.lw import utils as ligolw_utils
39 from ligo.lw import lsctables
40 from gstlal import svd_bank
41 from gstlal.stats import inspiral_lr
42 
43 
44 ### This program will create svd bank files; see gstlal_svd_bank for more information
45 ###
46 ### Usage examples
47 ### --------------
48 ###
49 ### 1. Typical use::
50 ###
51 ### gstlal_svd_bank --reference-psd reference_psd.xml --samples-min 1024 \
52 ### --bank-id 0 --ortho-gate-fap 0.5 --flow 40.0 \
53 ### --template-bank /mnt/qfs3/gstlalcbc/engineering/5/bns_bank_40Hz/0000-H1_split_bank-H1-TMPLTBANK-871147516-2048.xml \
54 ### --svd-tolerance 0.9995
55 ### --write-svd-bank /mnt/qfs3/gstlalcbc/engineering/5/bns_bank_40Hz/svd_0000-H1_split_bank-H1-TMPLTBANK-871147516-2048.xml \
56 ### --samples-max-64 4096 --clipleft 0 --autocorrelation-length 351 --samples-max-256 1024 --clipright 20 --samples-max 4096
57 ###
58 ### 2. Please add more!
59 ###
60 ### Review Status
61 ### -------------
62 ###
63 ### +-------------------------------------------------+------------------------------------------+------------+
64 ### | Names | Hash | Date |
65 ### +=================================================+==========================================+============+
66 ### | Florent, Sathya, Duncan Me., Jolien, Kipp, Chad | 7536db9d496be9a014559f4e273e1e856047bf71 | 2014-04-30 |
67 ### +-------------------------------------------------+------------------------------------------+------------+
68 ###
69 
70 
71 #
72 #
73 # =============================================================================
74 #
75 # Command Line
76 #
77 # =============================================================================
78 #
79 
80 
81 parser = OptionParser(description = __doc__)
82 parser.add_option("--flow", metavar = "Hz", type = "float", default = 40.0, help = "Set the template low-frequency cut-off (default = 40.0).")
83 parser.add_option("--sample-rate", metavar = "Hz", type = "int", help = "Set the sample rate. If not set, the sample rate will be based on the template frequency. The sample rate must be at least twice the highest frequency in the templates. If provided it must be a power of two")
84 parser.add_option("--padding", metavar = "pad", type = "float", default = 1.5, help = "Fractional amount to pad time slices.")
85 parser.add_option("--svd-tolerance", metavar = "match", type = "float", default = 0.9999, help = "Set the SVD reconstruction tolerance (default = 0.9999).")
86 parser.add_option("--reference-psd", metavar = "filename", help = "Load the spectrum from this LIGO light-weight XML file (required).")
87 parser.add_option("--instrument-override", metavar = "ifo", help = "Override the ifo column of the single inspiral tables")
88 parser.add_option("--template-bank-cache", metavar = "filename", help = "Provide a cache file with the names of the LIGO light-weight XML file from which to load the template bank.")
89 parser.add_option("--ortho-gate-fap", metavar = "probability", type = "float", default = 0.5, help = "Set the orthogonal SNR projection gate false-alarm probability (default = 0.5).")
90 parser.add_option("--write-svd-bank", metavar = "filename", help = "Set the filename in which to save the template bank (required).")
91 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
92 parser.add_option("--autocorrelation-length", type = "int", default = 201, help = "The minimum number of samples to use for auto-chisquared, default 201 should be odd")
93 parser.add_option("--samples-min", type = "int", default = 1024, help = "The minimum number of samples to use for time slices default 1024")
94 parser.add_option("--samples-max-256", type = "int", default = 1024, help = "The maximum number of samples to use for time slices with frequencies above 256Hz, default 1024")
95 parser.add_option("--samples-max-64", type = "int", default = 2048, help = "The maximum number of samples to use for time slices with frequencies between 64Hz and 256 Hz, default 2048")
96 parser.add_option("--samples-max", type = "int", default = 4096, help = "The maximum number of samples to use for time slices with frequencies below 64Hz, default 4096")
97 
98 options, template_banks = parser.parse_args()
99 
100 if options.template_bank_cache:
101  template_banks.extend([CacheEntry(line).url for line in open(options.template_bank_cache)])
102 
103 required_options = ("reference_psd", "write_svd_bank")
104 
105 missing_options = [option for option in required_options if getattr(options, option) is None]
106 if missing_options:
107  raise ValueError("missing required option(s) %s" % ", ".join("--%s" % option.replace("_", "-") for option in sorted(missing_options)))
108 
109 if not options.autocorrelation_length % 2:
110  raise ValueError("--autocorrelation-length must be odd")
111 
112 if options.sample_rate is not None and (not numpy.log2(options.sample_rate) == int(numpy.log2(options.sample_rate))):
113  raise ValueError("--sample-rate must be a power of two")
114 
115 
116 
117 #
118 #
119 # =============================================================================
120 #
121 # Main
122 #
123 # =============================================================================
124 #
125 
126 def extract_subbank_info(banks, verbose = options.verbose):
127  var = {"clipleft": [], "clipright": [], "bank-id": []}
128  for bank in banks:
129  bank_xmldoc = ligolw_utils.load_url(bank, contenthandler = svd_bank.DefaultContentHandler, verbose = verbose)
130  process_params = lsctables.ProcessParamsTable.get_table(bank_xmldoc)
131  for row in process_params:
132  param = row.param.replace("--","")
133  if param in var:
134  var[param].append(row.value)
135  assert (len(var["clipleft"]) == len(var["clipright"]) == len(var["bank-id"]) == len(banks))
136  return var["clipleft"], var["clipright"], var["bank-id"]
137 
138 
139 cliplefts, cliprights, bank_ids = extract_subbank_info(template_banks)
140 cliplefts = [int(cl) for cl in cliplefts]
141 cliprights = [int(cr) for cr in cliprights]
142 
143 psd = lal.series.read_psd_xmldoc(ligolw_utils.load_filename(options.reference_psd, verbose=options.verbose, contenthandler=lal.series.PSDContentHandler))
144 
145 
146 svd_bank.write_bank(
147  options.write_svd_bank,
148  [svd_bank.build_bank(
149  template_bank,
150  psd,
151  options.flow,
152  options.ortho_gate_fap,
153  inspiral_lr.LnLRDensity.snr_min,
154  options.svd_tolerance,
155  padding = options.padding,
156  verbose = options.verbose,
157  autocorrelation_length = options.autocorrelation_length,
158  samples_min = options.samples_min,
159  samples_max_256 = options.samples_max_256,
160  samples_max_64 = options.samples_max_64,
161  samples_max = options.samples_max,
162  bank_id = bank_id,
163  contenthandler = svd_bank.DefaultContentHandler,
164  sample_rate = options.sample_rate,
165  instrument_override = options.instrument_override
166  ) for (template_bank, bank_id) in zip(template_banks, bank_ids)],
167  psd,
168  cliplefts,
169  cliprights
170 )