gstlal-inspiral  1.9.3
 All Classes Namespaces Files Functions
gstlal_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 gstlal import svd_bank
40 from gstlal.stats import inspiral_lr
41 
42 
43 ### This program will create svd bank files; see gstlal_svd_bank for more information
44 ###
45 ### Usage examples
46 ### --------------
47 ###
48 ### 1. Typical use::
49 ###
50 ### gstlal_svd_bank --reference-psd reference_psd.xml --samples-min 1024 \
51 ### --bank-id 0 --ortho-gate-fap 0.5 --flow 40.0 \
52 ### --template-bank /mnt/qfs3/gstlalcbc/engineering/5/bns_bank_40Hz/0000-H1_split_bank-H1-TMPLTBANK-871147516-2048.xml \
53 ### --svd-tolerance 0.9995
54 ### --write-svd-bank /mnt/qfs3/gstlalcbc/engineering/5/bns_bank_40Hz/svd_0000-H1_split_bank-H1-TMPLTBANK-871147516-2048.xml \
55 ### --samples-max-64 4096 --clipleft 0 --autocorrelation-length 351 --samples-max-256 1024 --clipright 20 --samples-max 4096
56 ###
57 ### 2. Please add more!
58 ###
59 ### Review Status
60 ### -------------
61 ###
62 ### +-------------------------------------------------+------------------------------------------+------------+
63 ### | Names | Hash | Date |
64 ### +=================================================+==========================================+============+
65 ### | Florent, Sathya, Duncan Me., Jolien, Kipp, Chad | 7536db9d496be9a014559f4e273e1e856047bf71 | 2014-04-30 |
66 ### +-------------------------------------------------+------------------------------------------+------------+
67 ###
68 
69 
70 #
71 #
72 # =============================================================================
73 #
74 # Command Line
75 #
76 # =============================================================================
77 #
78 
79 
80 parser = OptionParser(description = __doc__)
81 parser.add_option("--flow", metavar = "Hz", type = "float", default = 40.0, help = "Set the template low-frequency cut-off (default = 40.0).")
82 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")
83 parser.add_option("--identity-transform", action = "store_true", default = False, help = "Do not perform an SVD; instead, use the original templates as the analyzing templates.")
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.9995, help = "Set the SVD reconstruction tolerance (default = 0.9995).")
86 parser.add_option("--reference-psd", metavar = "filename", help = "Load the spectrum from this LIGO light-weight XML file (required).")
87 parser.add_option("--template-bank", metavar = "filename", action = "append", default = [], help = "Set the name of the LIGO light-weight XML file from which to load the template bank (required).")
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("--clipleft", type = "int", metavar = "N", action = "append", help = "Remove N poorly reconstructable templates from the left edge of each sub-bank. Required")
93 parser.add_option("--clipright", type = "int", metavar = "N", action = "append", help = "Remove N poorly reconstructable templates from the right edge of each sub-bank. Required")
94 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")
95 parser.add_option("--samples-min", type = "int", default = 1024, help = "The minimum number of samples to use for time slices default 1024")
96 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")
97 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")
98 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")
99 parser.add_option("--bank-id", metavar = "id", action = "append", help = "Set a string to be used as the globally unique ID for each bank, required. IDs should be of the form <int>ID_<int>N where N is for the sub bank in each file and this option must be given N times")
100 
101 options, filenames = parser.parse_args()
102 
103 if options.template_bank_cache:
104  options.template_bank.extend([CacheEntry(line).url for line in open(options.template_bank_cache)])
105 
106 required_options = ("reference_psd", "template_bank", "write_svd_bank", "clipleft", "clipright", "bank_id")
107 
108 missing_options = [option for option in required_options if getattr(options, option) is None]
109 if missing_options:
110  raise ValueError("missing required option(s) %s" % ", ".join("--%s" % option.replace("_", "-") for option in sorted(missing_options)))
111 
112 if not (len(options.template_bank) == len(options.clipleft) == len(options.clipright) == len(options.bank_id)):
113  raise ValueError("must give --template-bank, --clipright and --clipleft options in equal amounts")
114 
115 if not options.autocorrelation_length % 2:
116  raise ValueError("--autocorrelation-length must be odd")
117 
118 if options.sample_rate is not None and (not numpy.log2(options.sample_rate) == int(numpy.log2(options.sample_rate))):
119  raise ValueError("--sample-rate must be a power of two")
120 
121 
122 
123 #
124 #
125 # =============================================================================
126 #
127 # Main
128 #
129 # =============================================================================
130 #
131 
132 
133 psd = lal.series.read_psd_xmldoc(ligolw_utils.load_filename(options.reference_psd, verbose=options.verbose, contenthandler=lal.series.PSDContentHandler))
134 
135 
136 svd_bank.write_bank(
137  options.write_svd_bank,
138  [svd_bank.build_bank(
139  template_bank,
140  psd,
141  options.flow,
142  options.ortho_gate_fap,
143  inspiral_lr.LnLRDensity.snr_min,
144  options.svd_tolerance,
145  padding = options.padding,
146  identity_transform = options.identity_transform,
147  verbose = options.verbose,
148  autocorrelation_length = options.autocorrelation_length,
149  samples_min = options.samples_min,
150  samples_max_256 = options.samples_max_256,
151  samples_max_64 = options.samples_max_64,
152  samples_max = options.samples_max,
153  bank_id = bank_id,
154  contenthandler = svd_bank.DefaultContentHandler,
155  sample_rate = options.sample_rate
156  ) for (template_bank, bank_id) in zip(options.template_bank, options.bank_id)],
157  psd,
158  options.clipleft,
159  options.clipright
160 )