Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInspiral 5.0.3.1-da3b9d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
thinca_brute_force_coinc.py
Go to the documentation of this file.
1##python
2import itertools
3from optparse import OptionParser
4import sys
5from tqdm import tqdm
6
7
8from lalburst import snglcoinc
9from lalinspiral.thinca import InspiralCoincDef
10
11
12from igwn_ligolw import lsctables
13from igwn_ligolw import utils as ligolw_utils
14from igwn_ligolw.utils import process as ligolw_process
15
16
18 parser = OptionParser(
19 )
20 parser.add_option("--delta-t", metavar = "seconds", type = float, default = 0.005, help = "Coincidence window (default = 0.005 s).")
21 parser.add_option("--min-instruments", metavar = "count", type = int, default = 2, help = "Require candidates to have at least this many instruments participating (default = 2).")
22 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
23
24 options, filenames = parser.parse_args()
25
26 process_params = options.__dict__.copy()
27
28 if options.delta_t < 0.:
29 raise ValueError("--delta-t must be >= 0")
30 if options.min_instruments < 1:
31 raise ValueError("--min-instruments must be >= 1")
32
33 return options, process_params, filenames
34
35
36options, process_params, filenames = parse_command_line()
37
38
39for filename in filenames:
40 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose)
41
42 process = ligolw_process.register_to_xmldoc(xmldoc, "brute_force_coinc", process_params)
43
44 # don't bother with coinc_inspiral table
45 coinctables = snglcoinc.CoincTables(xmldoc, InspiralCoincDef)
46
47 # this is normally not the correct way to get the instrument list
48 # from a document, but here we only care about instruments that
49 # made triggers. if others were included in the analysis we don't
50 # care about them.
51 instruments = list(set(row.ifo for row in lsctables.SnglInspiralTable.get_table(xmldoc)))
52 dt = dict((frozenset(pair), options.delta_t + snglcoinc.light_travel_time(*pair)) for pair in itertools.combinations(instruments, 2))
53
54 sngls = dict.fromkeys(row.template_id for row in lsctables.SnglInspiralTable.get_table(xmldoc))
55 for template_id in sngls:
56 sngls[template_id] = dict((instrument, []) for instrument in instruments)
57 for row in lsctables.SnglInspiralTable.get_table(xmldoc):
58 sngls[row.template_id][row.ifo].append(row)
59 for template_id in sngls:
60 for instrument in instruments:
61 sngls[template_id][instrument].sort(key = lambda row: row.end)
62
63 offsetvectors = lsctables.TimeSlideTable.get_table(xmldoc).as_dict()
64
65 # iterate over instrument-->trigger list dictionaries, one for each
66 # template
67
68 for sngls_by_instrument in tqdm(sngls.values()):
69 for time_slide_id, offsetvector in offsetvectors.items():
70 # set of event_id sets to exclude from the coinc
71 # set because they've already been found in a
72 # higher-order coinc
73 exclude = set()
74
75 for n in range(len(instruments), options.min_instruments - 1, -1):
76 for select_instruments in itertools.combinations(instruments, n):
77 offsets = tuple(offsetvector[instrument] for instrument in select_instruments)
78 for sngls in itertools.product(*map(sngls_by_instrument.__getitem__, select_instruments)):
79 event_ids = frozenset(sngl.event_id for sngl in sngls)
80 if event_ids in exclude:
81 continue
82 ends = tuple((instrument, sngl.end + offset) for instrument, sngl, offset in zip(select_instruments, sngls, offsets))
83 if any(abs(t_a - t_b) > dt[frozenset((instrument_a, instrument_b))] for (instrument_a, t_a), (instrument_b, t_b) in itertools.combinations(ends, 2)):
84 continue
85 coinctables.append_coinc(*coinctables.coinc_rows(process.process_id, time_slide_id, sngls, "sngl_inspiral"))
86 exclude |= {frozenset(x) for i in range(n - 1, options.min_instruments - 1, -1) for x in itertools.combinations(event_ids, i)}
87
88 process.set_end_time_now()
89
90 ligolw_utils.write_filename(xmldoc, filename, verbose = options.verbose)