gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_inspiral_grid_bank
1 #!/usr/bin/env python3
2 #
3 # Copyright (C) 2019 Kipp Cannon, Chad Hanna
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 #
21 # =============================================================================
22 #
23 # Preamble
24 #
25 # =============================================================================
26 #
27 
28 import sys
29 from optparse import OptionParser
30 import numpy
31 import scipy
32 from scipy.stats import logistic
33 
34 from lal import series
35 from lal import MSUN_SI
36 
37 from ligo.lw import ligolw
38 from ligo.lw import lsctables
39 from ligo.lw import array as ligolw_array
40 from ligo.lw import param as ligolw_param
41 from ligo.lw import utils as ligolw_utils
42 from ligo.lw.utils import process as ligolw_process
43 from lalinspiral.sbank.waveforms import SnglInspiralTable
44 from lalsimulation import SimIMRSEOBNRv4ROMTimeOfFrequency as lalsim_chirptime
45 
46 from gstlal import far
47 from gstlal import svd_bank
48 from gstlal import templates
49 
50 @ligolw_array.use_in
51 @ligolw_param.use_in
52 @lsctables.use_in
53 class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
54  pass
55 
56 __author__ = "Chad Hanna <chad.hanna@ligo.org>"
57 __version__ = "git id %s" % "" # FIXME
58 __date__ = "" # FIXME
59 
60 
61 #
62 # =============================================================================
63 #
64 # Command Line
65 #
66 # =============================================================================
67 #
68 
69 
70 
71 def chispace(start, stop, numpoints):
72  x = numpy.linspace(0., 5.5, numpoints)
73  return start + ((logistic.cdf(x) - 0.5) / (logistic.cdf(x[-1]) - 0.5)) * (stop - start)
74 
75 def mchirp(m1, m2):
76  return (m1 * m2)**.6 / (m1 + m2)**.2
77 
78 parser = OptionParser(
79  version = "Name: %%prog\n%s" % "" # FIXME
80 )
81 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
82 parser.add_option("--instrument", action = "append", help = "Append to a list of instruments to create dist stats for. List must be whatever instruments you intend to analyze.")
83 parser.add_option("--output-name", type = "string", default = "gstlal_inspiral_grid_bank.xml.gz", help = "Specify output file name. Default gstlal_inspiral_grid_bank.xml.gz")
84 parser.add_option("--psd-xml", type = "string", help = "Specify a PSD to use for computing template bandwidth. Required if df is bandwidth")
85 parser.add_option("--m1", type = "string", help = "Specify m1 as start:stop:numpoints")
86 parser.add_option("--m2", type = "string", help = "Specify m2 as start:stop:numpoints")
87 parser.add_option("--chi", type = "string", help = "Specify chi as start:stop:numpoints")
88 parser.add_option("--bandwidth-min", type = "float", help = "Specify minimum bandwidth to keep")
89 parser.add_option("--min-q", type = "float", default = 1.0, help = "Specify min q, default 1.0")
90 parser.add_option("--max-q", type = "float", default = float("inf"), help = "Specify max q, default inf")
91 parser.add_option("--min-mtot", type = "float", default = 0., help = "Specify min mtotal, default 0")
92 parser.add_option("--max-mtot", type = "float", default = float("inf"), help = "Specify max mtotal, default inf")
93 options, filenames = parser.parse_args()
94 
95 
96 if not options.instrument:
97  raise ValueError("must specify at least one --instrument")
98 options.instrument = set(options.instrument)
99 
100 psd = {}
101 if options.psd_xml:
102  for ifo, p in series.read_psd_xmldoc(ligolw_utils.load_filename(options.psd_xml, verbose = options.verbose, contenthandler = series.PSDContentHandler)).items():
103  f = numpy.arange(len(p.data.data)) * p.deltaF
104  psd[ifo] = scipy.interpolate.interp1d(f, p.data.data)
105 
106 m1start, m1stop, m1num = [float(x) for x in options.m1.split(":")]
107 m2start, m2stop, m2num = [float(x) for x in options.m2.split(":")]
108 chistart, chistop, s1num = [float(x) for x in options.chi.split(":")]
109 
110 numtmps = 0
111 m1s = []
112 m2s = []
113 s1s = []
114 s2s = []
115 durs = []
116 for m1 in numpy.logspace(numpy.log10(m1start), numpy.log10(m1stop), m1num):
117  for m2 in numpy.logspace(numpy.log10(m2start), numpy.log10(m2stop), m2num):
118  for chi in chispace(chistart, chistop, s1num):
119  s1 = chi
120  s2 = (chi * (m1 + m2) - m1 * s1) / m2
121  if m1 < m2:
122  continue
123  if m1 / m2 > options.max_q or m1 / m2 < options.min_q:
124  continue
125  if m1 + m2 > options.max_mtot or m1 + m2 < options.min_mtot:
126  continue
127  if options.bandwidth_min is not None:
128  bw = templates.bandwidth(m1, m2, s1, s2, f_min = 10.0, f_max = 1024., delta_f = 0.25, psd = psd[ifo])
129  if bw < options.bandwidth_min:
130  continue
131  dur = lalsim_chirptime(10.0, MSUN_SI*m1, MSUN_SI*m2, s1, s2)
132  numtmps += 1
133  m1s.append(m1)
134  m2s.append(m2)
135  s1s.append(s1)
136  s2s.append(s2)
137  durs.append(dur)
138  print(m1, m2, s1, s2, numtmps)
139 
140 # prepare a new XML document for writing template bank
141 xmldoc = ligolw.Document()
142 xmldoc.appendChild(ligolw.LIGO_LW())
143 #sngl_inspiral_columns = ("process:process_id", "mass1", "mass2", "spin1x", "spin1y", "spin1z", "spin2x", "spin2y", "spin2z", "mchirp")
144 lsctables.SnglInspiralTable.RowType = SnglInspiralTable
145 tbl = lsctables.New(lsctables.SnglInspiralTable)
146 xmldoc.childNodes[-1].appendChild(tbl)
147 # FIXME make a real process table
148 process = ligolw_process.register_to_xmldoc(xmldoc, sys.argv[0], {})
149 ligolw_process.set_process_end_time(process)
150 
151 if options.verbose:
152  print("Writing output document", file=sys.stderr)
153 
154 for n, (m1, m2, s1, s2, dur) in enumerate(zip(m1s, m2s, s1s, s2s, durs)):
155  row = SnglInspiralTable()
156  row.mass1, row.mass2, row.spin1x, row.spin1y, row.spin1z, row.spin2x, row.spin2y, row.spin2z, row.mchirp, row.template_duration = (m1, m2, 0., 0., s1, 0., 0., s2, mchirp(m1, m2), dur)
157  row.event_id = n
158  row.ifo = "H1" # FIXME
159  row.process_id = process.process_id
160  tbl.append(row)
161 
162 ligolw_utils.write_filename(xmldoc, options.output_name)
163 
164 import matplotlib
165 matplotlib.use('agg')
166 from matplotlib import pyplot
167 pyplot.loglog(m1s, m2s, '*')
168 pyplot.savefig("bw.png")
169