gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
gstlal_svd_bank
1 #!/usr/bin/env python
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 uuid
34 
35 
36 from glue.ligolw import ligolw
37 from glue.ligolw import array as ligolw_array
38 from glue.ligolw import param as ligolw_param
39 from glue.ligolw import utils as ligolw_utils
40 from pylal import series as lalseries
41 from gstlal import svd_bank
42 from gstlal.far import ThincaCoincParamsDistributions
43 
44 ## @file gstlal_svd_bank
45 # This program will create svd bank files; see gstlal_svd_bank for more information
46 #
47 # ### Usage examples
48 #
49 # - Typical use
50 #
51 # $ gstlal_svd_bank --reference-psd reference_psd.xml --samples-min 1024 --bank-id 0 --snr-threshold 4.0 --ortho-gate-fap 0.5 --flow 40.0 --template-bank /mnt/qfs3/gstlalcbc/engineering/5/bns_bank_40Hz/0000-H1_split_bank-H1-TMPLTBANK-871147516-2048.xml --svd-tolerance 0.9995 --write-svd-bank /mnt/qfs3/gstlalcbc/engineering/5/bns_bank_40Hz/svd_0000-H1_split_bank-H1-TMPLTBANK-871147516-2048.xml --samples-max-64 4096 --clipleft 0 --autocorrelation-length 351 --samples-max-256 1024 --clipright 20 --samples-max 4096
52 #
53 # - Please add more!
54 #
55 # ### Command line options
56 # + `--flow` [float] (Hz): Set the template low-frequency cut-off (default = 40.0).
57 # + `--identity-transform`: Do not perform an SVD; instead, use the original templates as the analyzing templates.
58 # + `--padding` [pad factor] (float): Fractional amount to pad time slices. Default 1.5
59 # + `--svd-tolerance` [match]: Set the SVD reconstruction tolerance (default = 0.9995).
60 # + `--reference-psd` [filename]: Load the spectrum from this LIGO light-weight XML file (required).
61 # + `--template-bank` [filename]: Set the name of the LIGO light-weight XML file from which to load the template bank (required).
62 # + `--ortho-gate-fap` [probability]: Set the orthogonal SNR projection gate false-alarm probability (default = 1e-2).
63 # + `--snr-threshold` [SNR]: Set the SNR threshold (default = 4.0). Currently this cannot be changed.
64 # + `--write-svd-bank` [filename]: Set the filename in which to save the template bank (required).
65 # + `--verbose`: Be verbose (optional).
66 # + `--clipleft` [int]: Remove poorly reconstructable templates from the left edge of each sub-bank.
67 # + `--clipright` [int]: Remove poorly reconstructable templates from the right edge of each sub-bank.
68 # + `--autocorrelation-length` [int]: The minimum number of samples to use for auto-chisquared, default 201 should be odd
69 # + `--samples-min` [int]: The minimum number of samples to use for time slices default 1024
70 # + `--samples-max-256` [int]: The maximum number of samples to use for time slices with frequencies above 256Hz, default 1024
71 # + `--samples-max-64` [int]: The maximum number of samples to use for time slices with frequencies between 64Hz and 256 Hz, default 2048
72 # + `--samples-max` [int]: The maximum number of samples to use for time slices with frequencies below 64Hz, default 4096
73 # + `--bank-id` [id] (string): Set a string to be used as the globally unique ID for this bank (default = generate a UUID)
74 # + `--write-psd`: Write the PSD used to actually whiten the templates after interpolating and conditioning. It will be inserted into the svd bank file.
75 #
76 # ### Review Status
77 #
78 # | Names | Hash | Date |
79 # | ------------------------------------------- | ------------------------------------------- | ---------- |
80 # | Florent, Sathya, Duncan Me., Jolien, Kipp, Chad | 7536db9d496be9a014559f4e273e1e856047bf71 | 2014-04-30 |
81 #
82 # #### Actions
83 #
84 # - Add process params to output
85 #
86 # #### Methods
87 #
88 # @image html svdtable.png
89 #
90 # @image html svd.png
91 
92 
93 
94 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
95  pass
96 ligolw_array.use_in(LIGOLWContentHandler)
97 ligolw_param.use_in(LIGOLWContentHandler)
98 
99 
100 #
101 #
102 # =============================================================================
103 #
104 # Command Line
105 #
106 # =============================================================================
107 #
108 
109 
110 parser = OptionParser(description = __doc__)
111 parser.add_option("--flow", metavar = "Hz", type = "float", default = 40.0, help = "Set the template low-frequency cut-off (default = 40.0).")
112 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.")
113 parser.add_option("--padding", metavar = "pad", type = "float", default = 1.5, help = "Fractional amount to pad time slices.")
114 parser.add_option("--svd-tolerance", metavar = "match", type = "float", default = 0.9995, help = "Set the SVD reconstruction tolerance (default = 0.9995).")
115 parser.add_option("--reference-psd", metavar = "filename", help = "Load the spectrum from this LIGO light-weight XML file (required).")
116 parser.add_option("--template-bank", metavar = "filename", action = "append", help = "Set the name of the LIGO light-weight XML file from which to load the template bank (required).")
117 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).")
118 parser.add_option("--snr-threshold", metavar = "SNR", type = "float", default = 4.0, help = "Set the SNR threshold (default = 4.0). Currently this cannot be changed.")
119 parser.add_option("--write-svd-bank", metavar = "filename", help = "Set the filename in which to save the template bank (required).")
120 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")
121 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")
122 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")
123 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")
124 parser.add_option("--samples-min", type = "int", default = 1024, help = "The minimum number of samples to use for time slices default 1024")
125 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")
126 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")
127 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")
128 parser.add_option("--bank-id", metavar = "id", action = "append", help = "Set a string to be used as the globally unique ID for each bank (default = generate a UUID).")
129 parser.add_option("--write-psd", action = "store_true", default = False, help = "Write the PSD used to actually whiten the templates after interpolating and conditioning. It will be inserted into the svd bank file.")
130 
131 options, filenames = parser.parse_args()
132 
133 required_options = ("reference_psd", "template_bank", "write_svd_bank", "clipleft", "clipright")
134 
135 missing_options = [option for option in required_options if getattr(options, option) is None]
136 if missing_options:
137  raise ValueError("missing required option(s) %s" % ", ".join("--%s" % option.replace("_", "-") for option in sorted(missing_options)))
138 
139 if options.bank_id is None:
140  options.bank_id = [str(uuid.uuid4()) for t in options.template_bank]
141 
142 if not (len(options.template_bank) == len(options.clipleft) == len(options.clipright) == len(options.bank_id)):
143  raise ValueError("must give --template-bank, --clipright and --clipleft options in equal amounts")
144 
145 if not options.autocorrelation_length % 2:
146  raise ValueError("--autocorrelation-length must be odd")
147 
148 if options.snr_threshold != ThincaCoincParamsDistributions.snr_min:
149  raise ValueError("--snr-threshold must be %f" % ThincaCoincParamsDistributions.snr_min)
150 
151 
152 #
153 #
154 # =============================================================================
155 #
156 # Main
157 #
158 # =============================================================================
159 #
160 
161 psd = lalseries.read_psd_xmldoc(ligolw_utils.load_filename(options.reference_psd, verbose=options.verbose, contenthandler=LIGOLWContentHandler))
162 
163 svd_bank.write_bank(
164  options.write_svd_bank,
165  [svd_bank.build_bank(
166  template_bank,
167  psd,
168  options.flow,
169  options.ortho_gate_fap,
170  options.snr_threshold,
171  options.svd_tolerance,
172  padding = options.padding,
173  identity_transform = options.identity_transform,
174  verbose = options.verbose,
175  autocorrelation_length = options.autocorrelation_length,
176  samples_min = options.samples_min,
177  samples_max_256 = options.samples_max_256,
178  samples_max_64 = options.samples_max_64,
179  samples_max = options.samples_max,
180  bank_id = bank_id
181  ) for (template_bank, bank_id) in zip(options.template_bank, options.bank_id)],
182  options.clipleft,
183  options.clipright,
184  write_psd = options.write_psd
185 )