gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_svd_bank_pipe
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2010 Kipp Cannon, Chad Hanna, Drew Keppel
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 """
20 This program makes a dag to generate svd banks
21 """
22 
23 __author__ = 'Chad Hanna <chad.hanna@ligo.org>'
24 
25 #
26 # import standard modules
27 #
28 
29 import sys, os, math
30 from optparse import OptionParser
31 
32 #
33 # import the modules we need to build the pipeline
34 #
35 
36 from ligo import segments
37 from ligo.lw import ligolw
38 from ligo.lw import lsctables
39 from ligo.lw import utils as ligolw_utils
40 from ligo.lw.utils import process as ligolw_process
41 from gstlal import dagparts
42 from gstlal import inspiral_pipe
43 from gstlal import far
44 from lal.utils import CacheEntry
45 
46 ## @file gstlal_inspiral_svd_bank_pipe
47 # This program will make a HTCondor DAG to automate the creation of svd bank files; see gstlal_inspiral_svd_bank_pipe for more information
48 #
49 #
50 # ### Graph of the condor DAG
51 #
52 # @dot
53 # digraph G {
54 # // graph properties
55 #
56 # rankdir=LR;
57 # compound=true;
58 # node [shape=record fontsize=10 fontname="Verdana"];
59 # edge [fontsize=8 fontname="Verdana"];
60 #
61 # // nodes
62 #
63 # "gstlal_svd_bank" [URL="\ref gstlal_svd_bank"];
64 # }
65 # @enddot
66 #
67 # This DAG implements only a single job type; gstlal_svd_bank
68 #
69 # ### Usage cases
70 #
71 # - Typical usage case for H1
72 #
73 # $ gstlal_inspiral_svd_bank_pipe --autocorrelation-length 351 --instrument H1 --reference-psd reference_psd.xml --bank-cache H1_split_bank.cache --overlap 10 --flow 15 --num-banks 1 --output-name H1_bank
74 #
75 # - Please add more!
76 #
77 # ### Command line options
78 #
79 # + `--instrument` [ifo]: set the name of the instrument, required
80 # + `--reference-psd` [file]: Set the name of the reference psd file, required
81 # + `--bank-cache` [file]: Set the name of the bank cache, required
82 # + `--overlap` [int]: Set the factor that describes the overlap of the sub banks, must be even!
83 # + `--identity-transform`: Turn off the SVD and use the identity reconstruction matrix
84 # + `--autocorrelation-length` [int]: The number of samples to use for auto-chisquared, default 201 should be odd
85 # + `--samples-min` [int]: The minimum number of samples to use for time slices default 1024
86 # + `--samples-max-256` [int]: The maximum number of samples to use for time slices with frequencies above 256Hz, default 1024
87 # + `--samples-max-64` [int]: The maximum number of samples to use for time slices with frequencies between 64Hz and 256 Hz, default 2048
88 # + `--samples-max` [int]: The maximum number of samples to use for time slices with frequencies below 64Hz, default 4096
89 # + `--tolerance` [float]: Set the SVD tolerance, default 0.9995
90 # + `--flow` [float]: Set the low frequency cutoff, default 40 (Hz)
91 # + `--num-banks [str]: The number of banks per job. can be given as a list like 1,2,3,4 then it will split up the bank cache into N groups with M banks each.")
92 # + `--output-name` [file]: Set the base name of the output, required
93 # + `--verbose`: Be verbose.
94 #
95 # ### Review Status
96 #
97 # | Reviewers | Hash | Date | Diff to Head of Master |
98 # | --------------------------------------- | ---------------------------------------- | ---------- | -------- |
99 # | Florent, Duncan Me., Jolien, Kipp, Chad | 67f86a6f2830f399c8d7c4cec6f357940a4b0abb | 2014-04-29 | <a href="@gstlal_inspiral_cgit_diff/bin/gstlal_inspiral_svd_bank_pipe?id=HEAD&id2=67f86a6f2830f399c8d7c4cec6f357940a4b0abb">gstlal_inspiral_svd_bank_pipe</a> |
100 
101 
102 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
103  pass
104 lsctables.use_in(LIGOLWContentHandler)
105 
106 
107 def get_chirpmass_from_bank_file(filenames, options):
108  chirpmasses = []
109  for filename in filenames:
110  xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose, contenthandler = LIGOLWContentHandler)
111  sngl_inspiral_table = lsctables.SnglInspiralTable.get_table(xmldoc)
112  chirpmasses.extend((row.mchirp for row in sngl_inspiral_table))
113  return max(chirpmasses)
114 
115 
116 def get_ac_length_from_mchirp(ac_length_segments, mchirp):
117  for (seg, ac_length) in ac_length_segments:
118  if mchirp in seg:
119  return ac_length
120 
121 
122 def parse_command_line():
123  parser = OptionParser()
124  parser.add_option("--instrument", help = "set the name of the instrument, required")
125  parser.add_option("--reference-psd", metavar = "file", help = "Set the name of the reference psd file, required")
126  parser.add_option("--bank-cache", metavar = "file", help = "Set the name of the bank cache, required")
127  parser.add_option("--overlap", metavar = "num", type = "int", default = 0, help = "set the factor that describes the overlap of the sub banks, must be even!")
128  parser.add_option("--identity-transform", default = False, action = "store_true", help = "turn off the SVD and use the identity reconstruction matrix")
129  parser.add_option("--autocorrelation-length", action = "append", help = "The number of samples to use for auto-chisquared in a given chirp mass range given as e.g., 0:1e6:351 which means 351 for chirpmass between 0 and 1 million.")
130  parser.add_option("--samples-min", type = "int", default = 1024, help = "The minimum number of samples to use for time slices default 1024")
131  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")
132  parser.add_option("--samples-max-64", type = "int", default = 2048, help = "The maximum number of samples to use for time slices with frequencies above 64Hz, default 2048")
133  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")
134  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")
135  parser.add_option("--tolerance", metavar = "float", type = "float", default = 0.9995, help = "set the SVD tolerance, default 0.9995")
136  parser.add_option("--flow", metavar = "num", type = "float", default = 40, help = "set the low frequency cutoff, default 40 (Hz)")
137  parser.add_option("--output-name", help = "set the base name of the output, required")
138  parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
139  parser.add_option("--num-banks", metavar = "str", help = "the number of banks per job. can be given as a list like 1,2,3,4 then it will split up the bank cache into N groups with M banks each.")
140  parser.add_option("--condor-command", action = "append", default = [], metavar = "command=value", help = "set condor commands of the form command=value; can be given multiple times")
141  parser.add_option("--singularity-image", metavar = "filename", help = "If set, uses the Singularity image provided as the build environment and sets Singularity-specific condor options.")
142  options, filenames = parser.parse_args()
143 
144  ac_seglist = []
145  for opt in options.autocorrelation_length:
146  start, stop, value = opt.split(":")
147  ac_seglist.append((segments.segment((float(start), float(stop))), int(value)))
148  options.autocorrelation_length = ac_seglist
149 
150  if options.overlap % 2:
151  raise ValueError("overlap must be even")
152 
153  options.num_banks = [int(s) for s in options.num_banks.split(",")]
154 
155  return options, filenames
156 
157 
158 #
159 # get input arguments
160 #
161 
162 
163 options, filenames = parse_command_line()
164 ifo = options.instrument
165 ref_psd = options.reference_psd
166 
167 
168 #
169 # Do some setup
170 #
171 
172 
173 try:
174  os.mkdir("logs")
175 except:
176  pass
177 
178 dag = dagparts.DAG(options.output_name)
179 
180 condor_opts = {
181  "want_graceful_removal": "True",
182  "kill_Sig": "15"
183 }
184 if options.singularity_image:
185  condor_opts['+SingularityImage'] = '"{}"'.format(options.singularity_image)
186 
187 svdJob = dagparts.DAGJob("gstlal_svd_bank", tag_base = "gstlal_svd_bank_%s" % ifo, condor_commands = dagparts.condor_command_dict_from_opts(options.condor_command, condor_opts))
188 
189 # Assumes cache is sorted by chirpmass or whatever the SVD sorting algorithm that was chosen
190 files = [CacheEntry(line).path for line in open(options.bank_cache)]
191 
192 
193 #
194 # loop over files to set up svd bank jobs
195 #
196 
197 groups = list(inspiral_pipe.group(files, options.num_banks))
198 bank_ids = [0]
199 for i, f in enumerate(groups):
200  # handle the edges by not clipping so you retain the template bank as intended.
201  mchirp = get_chirpmass_from_bank_file(f, options)
202  clipleft = [options.overlap // 2] * len(f) # overlap must be even
203  clipright = [options.overlap // 2] * len(f) # overlap must be even
204  bank_ids = range(bank_ids[-1] + 1, bank_ids[-1] + 1 + len(f))
205  svd_bank_name = dagparts.T050017_filename(ifo, "GSTLAL_SVD_BANK_%d" % i, (0, 0), ".xml.gz", path = svdJob.output_path)
206  svd_bank_name = os.path.join(os.getcwd(), svd_bank_name)
207  dag.output_cache.append(CacheEntry(ifo, "GSTLAL_SVD_BANK_%d" % i, segments.segment(0, 0), "file://localhost%s" % (svd_bank_name,)))
208 
209  svdNode = dagparts.DAGNode(svdJob, dag, [],
210  opts = {"flow":options.flow,
211  "svd-tolerance":options.tolerance,
212  "ortho-gate-fap":0.5,
213  "samples-min":options.samples_min,
214  "samples-max":options.samples_max,
215  "samples-max-64":options.samples_max_64,
216  "samples-max-256":options.samples_max_256,
217  "sample-rate":options.sample_rate,
218  "clipleft":clipleft,
219  "clipright":clipright,
220  "autocorrelation-length":get_ac_length_from_mchirp(options.autocorrelation_length, mchirp),
221  "bank-id":bank_ids
222  },
223  input_files = {
224  "template-bank":f,
225  "reference-psd":ref_psd
226  },
227  output_files = {
228  "write-svd-bank":svd_bank_name
229  }
230  )
231  if options.identity_transform:
232  svdNode.add_var_arg("--identity-transform")
233 
234 
235 #
236 # Write out the dag files
237 #
238 
239 
240 dag.write_sub_files()
241 dag.write_dag()
242 dag.write_script()
243 dag.write_cache()