27Populate a sim_inspiral table with random draws from an ASCII table.
29from optparse
import Option, OptionParser
31from igwn_ligolw
import ligolw
32from igwn_ligolw
import lsctables
33import igwn_ligolw.utils.process
36from lalinference
import bayespputils
as bppu
47 (
'geocent_end_time',
'f8'),
48 (
'geocent_end_time_ns',
'f8'),
52 (
'inclination',
'f8'),
54 (
'polarization',
'f8'),
62 (
'numrel_data',
'|S64')
66 """Determine name of input: either the sole positional command line argument,
69 infilename =
'/dev/stdin'
73 parser.error(
"Too many command line arguments.")
77 for name
in possible_names:
78 if name
in params: params[params.index(name)] = desired_name
94 params = samples.dtype.names
95 has_mc =
'mchirp' in params
96 has_eta =
'eta' in params
98 has_ms =
'mass1' in params
and 'mass2' in params
99 has_mtotal =
'mtotal' in params
102 mc = samples[
'mchirp']
105 eta = bppu.q2eta(samples[
'q'])
107 raise ValueError(
"Chirp mass given with no mass ratio.")
112 m1, m2 = bppu.mc2ms(mc, eta)
117 m1 = samples[
'mass1']
118 m2 = samples[
'mass2']
120 eta = m1 * m2 / (mtotal * mtotal)
121 mc = mtotal * np.power(eta, 3./5.)
124 mtotal = samples[
'mtotal']
127 mc = mtotal * np.power(eta, 3./5.)
128 m1, m2 = bppu.mc2ms(mc, eta)
130 m1 = mtotal / (1 + samples[
'q'])
133 raise ValueError(
"Chirp mass given with no mass ratio.")
134 return mc, eta, m1, m2, mtotal
137if __name__ ==
"__main__":
138 parser = OptionParser(
139 description = __doc__,
140 usage =
"%prog [options] [INPUT]",
142 Option(
"-o",
"--output", metavar=
"FILE.xml",
143 help=
"name of output XML file"),
144 Option(
"--num-of-injs", metavar=
"NUM", type=int, default=200,
145 help=
"number of injections"),
146 Option(
"--approx", metavar=
"APPROX", default=
"SpinTaylorT4threePointFivePN",
147 help=
"approximant to be injected"),
148 Option(
"--taper", metavar=
"TAPER", default=
"TAPER_NONE",
149 help=
"Taper methods for injections"),
150 Option(
"--flow", metavar=
"FLOW", type=float, default=
None,
151 help=
"Taper methods for injections"),
152 Option(
"--amporder", metavar=
"AMPORDER", type=int, default=0,
153 help=
"pN order in amplitude for injection")
157 opts, args = parser.parse_args()
161 with open(infilename,
'r')
as inp:
162 params = inp.readline().split()
164 samples = np.loadtxt(inp, dtype=[(p, np.float)
for p
in params])
168 raise ValueError(
"{} injections requested, but {} samples were provided.".format(N, len(samples)))
171 selection = np.arange(len(samples))
172 np.random.shuffle(selection)
173 samples = samples[selection[:N]]
176 injections = np.zeros((N,), dtype=sim_inspiral_dt)
185 if 'a1' in params
and 'theta1' in params
and 'phi1' in params:
186 s1x, s1y, s1z = bppu.sph2cart(samples[
'a1'], samples[
'theta1'], samples[
'phi1'])
187 elif 'a1z' in params:
189 s1x = np.zeros_like(s1z)
190 s1y = np.zeros_like(s1z)
192 s1x = np.zeros_like(m1)
193 s1y = np.zeros_like(m1)
194 s1z = np.zeros_like(m1)
197 if 'a2' in params
and 'theta2' in params
and 'phi2' in params:
198 s2x, s2y, s2z = bppu.sph2cart(samples[
'a2'], samples[
'theta2'], samples[
'phi2'])
199 elif 'a2z' in params:
201 s2x = np.zeros_like(s2z)
202 s2y = np.zeros_like(s2z)
204 s2x = np.zeros_like(m2)
205 s2y = np.zeros_like(m2)
206 s2z = np.zeros_like(m2)
208 system_frame_params = set([ \
216 theta_jn=np.array([np.arccos(i)
for i
in samples[
'costheta_jn']])
217 if set(params).intersection(system_frame_params) == system_frame_params:
218 inclination, theta1, phi1, theta2, phi2, _ = bppu.physical2radiationFrame(
229 inclination = inclination.flatten()
230 theta1 = theta1.flatten()
231 phi1 = phi1.flatten()
232 theta2 = theta2.flatten()
233 phi2 = phi2.flatten()
234 s1x, s1y, s1z = bppu.sph2cart(samples[
'a1'], theta1, phi1)
235 s2x, s2y, s2z = bppu.sph2cart(samples[
'a2'], theta2, phi2)
237 inclination = theta_jn
241 if opts.flow
is None:
243 flow = samples[
'flow']
245 raise ValueError(
"No f_low found in input file or command line arguments.")
252 print((
'f_low given in both input file and command line.'
253 ' Using command line argument: %r' % opts.flow))
254 flow = [opts.flow
for i
in range(N)]
257 injections[
'waveform'] = [opts.approx
for i
in range(N)]
258 injections[
'taper'] = [opts.taper
for i
in range(N)]
259 injections[
'f_lower'] = flow
260 injections[
'mchirp'] = mc
261 injections[
'eta'] = eta
262 injections[
'mass1'] = m1
263 injections[
'mass2'] = m2
264 injections[
'geocent_end_time'] = np.modf(samples[
'time'])[1]
265 injections[
'geocent_end_time_ns'] = np.modf(samples[
'time'])[0] * 10**9
266 injections[
'distance'] = samples[
'distance']
267 injections[
'longitude'] = samples[
'longitude']
268 injections[
'latitude'] = samples[
'latitude']
269 injections[
'inclination'] = inclination
270 injections[
'coa_phase'] = samples[
'phi_orb']
271 injections[
'polarization'] = samples[
'polarization']
272 injections[
'spin1x'] = s1x
273 injections[
'spin1y'] = s1y
274 injections[
'spin1z'] = s1z
275 injections[
'spin2x'] = s2x
276 injections[
'spin2y'] = s2y
277 injections[
'spin2z'] = s2z
278 injections[
'amp_order'] = [opts.amporder
for i
in range(N)]
279 injections[
'numrel_data'] = [
"" for _
in range(N)]
282 xmldoc = ligolw.Document()
283 xmldoc.appendChild(ligolw.LIGO_LW())
284 proc = igwn_ligolw.utils.process.register_to_xmldoc(doc, sys.argv[0], {})
285 sim_table = lsctables.New(lsctables.SimInspiralTable)
286 xmldoc.childNodes[0].appendChild(sim_table)
290 row = sim_table.RowType()
291 for slot
in row.__slots__: setattr(row, slot, 0)
292 sim_table.append(row)
295 for i,row
in enumerate(sim_table):
296 row.process_id = proc.process_id
297 row.simulation_id = sim_table.get_next_id()
300 for field
in injections.dtype.names:
301 vals = injections[field]
302 for row, val
in zip(sim_table, vals): setattr(row, field, val)
305 output_file = open(opts.output,
'w')
306 xmldoc.write(output_file)
def standardize_param_names(params)
def compute_mass_parameterizations(samples)
def get_input_filename(parser, args)
Determine name of input: either the sole positional command line argument, or /dev/stdin.
def standardize_param_name(params, possible_names, desired_name)