47 from ligo.lw
import ligolw
48 from ligo.lw
import lsctables
50 from lalburst
import snglcoinc
51 from lalinspiral
import thinca
52 from ligo
import segments
53 from ligo.segments
import utils
as segmentsUtils
78 def __init__(self, xmldoc):
99 self.process_params_index.setdefault(row.process_id, []).append(row)
102 self.time_slide_index.setdefault(row.time_slide_id, []).append(row)
108 self.coinc_def, = (row
for row
in self.
coinc_def_table if row.search == thinca.InspiralCoincDef.search
and row.search_coinc_type == thinca.InspiralCoincDef.search_coinc_type)
120 def add(self, events, coinc, coincmaps, coinc_inspiral):
124 assert coinc.coinc_def_id == self.coinc_def.coinc_def_id
125 assert coinc.coinc_event_id == coinc_inspiral.coinc_event_id
128 assert all(event.process_id == coinc.process_id
for event
in events)
129 assert all(coinc_event_map.coinc_event_id == coinc.coinc_event_id
for coinc_event_map
in coincmaps)
130 assert set(event.event_id
for event
in events) == set(coinc_event_map.event_id
for coinc_event_map
in coincmaps)
138 self.sngl_inspiral_index.clear()
139 self.coinc_event_index.clear()
140 self.coinc_event_maps_index.clear()
141 self.coinc_inspiral_index.clear()
144 def sngl_inspirals(self, coinc_event_id):
156 def __getitem__(self, coinc_event_id):
157 newxmldoc = ligolw.Document()
158 ligolw_elem = newxmldoc.appendChild(ligolw.LIGO_LW())
163 new_process_table = ligolw_elem.appendChild(lsctables.New(lsctables.ProcessTable, self.process_table.columnnamesreal))
164 new_process_params_table = ligolw_elem.appendChild(lsctables.New(lsctables.ProcessParamsTable, self.process_params_table.columnnamesreal))
165 new_sngl_inspiral_table = ligolw_elem.appendChild(lsctables.New(lsctables.SnglInspiralTable, self.sngl_inspiral_table.columnnamesreal))
166 new_coinc_def_table = ligolw_elem.appendChild(lsctables.New(lsctables.CoincDefTable, self.coinc_def_table.columnnamesreal))
167 new_coinc_event_table = ligolw_elem.appendChild(lsctables.New(lsctables.CoincTable, self.coinc_event_table.columnnamesreal))
168 new_coinc_inspiral_table = ligolw_elem.appendChild(lsctables.New(lsctables.CoincInspiralTable, self.coinc_inspiral_table.columnnamesreal))
169 new_coinc_event_map_table = ligolw_elem.appendChild(lsctables.New(lsctables.CoincMapTable, self.coinc_event_map_table.columnnamesreal))
170 new_time_slide_table = ligolw_elem.appendChild(lsctables.New(lsctables.TimeSlideTable, self.time_slide_table.columnnamesreal))
172 new_coinc_def_table.append(self.coinc_def)
174 new_time_slide_table.extend(self.
time_slide_index[coincevent.time_slide_id])
177 new_coinc_event_table.append(coincevent)
181 for process_id
in set(new_sngl_inspiral_table.getColumnByName(
"process_id")) | set(new_coinc_event_table.getColumnByName(
"process_id")) | set(new_time_slide_table.getColumnByName(
"process_id")):
207 def push(self, event_ids, offset_vector):
208 if any(offset_vector.values()):
209 if len(event_ids) > 1:
210 self.timeshifted_coincs.update(event_ids)
211 elif len(event_ids) == 1:
212 self.zerolag_singles.update(event_ids)
214 def pull(self, snr_min, two_or_more_instruments, flushed_events):
215 index = dict((id(event), event)
for event
in flushed_events)
216 flushed_ids = set(index)
219 background_ids |= set(event_id
for event_id
in self.
zerolag_singles & flushed_ids
if float(index[event_id].end)
in two_or_more_instruments)
221 return [event
for event
in map(index.__getitem__, background_ids)
if event.snr >= snr_min]
225 def __init__(self, xmldoc, process_id, delta_t, min_instruments = 2, sngls_snr_threshold = None):
234 def set_xmldoc(self, xmldoc, process_id):
235 self.
coinc_tables = thinca.InspiralCoincTables(xmldoc, thinca.InspiralCoincDef)
240 thinca.coincgen_doubles,
241 lsctables.TimeSlideTable.get_table(xmldoc).as_dict(),
248 def push(self, instrument, events, t_complete):
250 Push new triggers into the coinc engine. Returns True if
251 the coinc engine's internal state has changed in a way that
252 might enable new candidates to be constructed, False if
255 return self.time_slide_graph.push(instrument, events, t_complete)
258 def pull(self, rankingstat, fapfar = None, zerolag_rankingstatpdf = None, coinc_sieve = None, flush = False, cluster = False, cap_singles = False, FAR_trialsfactor = 1.0):
275 age = float(self.time_slide_graph.age)
276 snr_segments = segments.segmentlistdict((instrument, ratebinlist[ratebinlist.value_slice_to_index(slice(age,
None))].segmentlist())
for instrument, ratebinlist
in rankingstat.denominator.triggerrates.items())
282 gps_time_now = float(lal.UTCToGPS(time.gmtime()))
286 self.last_coincs.clear()
287 max_last_coinc_snr = {}
288 for node, events
in self.time_slide_graph.pull(newly_reported = newly_reported, flushed = flushed, flushed_unused = flushed_unused, coinc_sieve = coinc_sieve, event_collector = self.
backgroundcollector, flush = flush):
291 coinc, coincmaps, coinc_inspiral = self.coinc_tables.coinc_rows(self.
process_id, node.time_slide_id, events, seglists = snr_segments)
301 rankingstat.zerolag.increment(event)
307 coinc_inspiral.minimum_duration = gps_time_now - float(coinc_inspiral.end)
312 max_last_coinc_snr.setdefault(node,
None)
313 if max_last_coinc_snr[node]
is None or coinc_inspiral.snr > max_last_coinc_snr[node][3].snr:
314 max_last_coinc_snr[node] = (events, coinc, coincmaps, coinc_inspiral)
316 self.coinc_tables.append_coinc(coinc, coincmaps, coinc_inspiral)
321 if zerolag_rankingstatpdf
is not None and coinc.likelihood
is not None:
322 zerolag_rankingstatpdf.zero_lag_lr_lnpdf.count[coinc.likelihood,] += 1
324 self.last_coincs.add(events, coinc, coincmaps, coinc_inspiral)
327 for node
in max_last_coinc_snr:
328 if max_last_coinc_snr[node]
is not None:
329 events, coinc, coincmaps, coinc_inspiral = max_last_coinc_snr[node]
333 if fapfar
is not None:
336 coinc_inspiral.combined_far = fapfar.far_from_rank(coinc.likelihood) * FAR_trialsfactor
337 if len(events) == 1
and cap_singles
and coinc_inspiral.combined_far < 1. / fapfar.livetime:
338 coinc_inspiral.combined_far = 1. / fapfar.livetime
339 coinc_inspiral.false_alarm_rate = fapfar.fap_from_rank(coinc.likelihood)
340 if zerolag_rankingstatpdf
is not None and coinc.likelihood
is not None:
341 zerolag_rankingstatpdf.zero_lag_lr_lnpdf.count[coinc.likelihood,] += 1
343 self.coinc_tables.append_coinc(coinc, coincmaps, coinc_inspiral)
344 self.last_coincs.add(events, coinc, coincmaps, coinc_inspiral)
345 self.sngl_inspiral_table.extend([sngl_trigger
for sngl_trigger
in events
if sngl_trigger.event_id
not in self.
clustered_sngl_ids])
356 two_or_more_instruments = segmentsUtils.vote(snr_segments.values(), 2)
364 two_or_more_instruments.protract(1e-3)
366 for event
in self.backgroundcollector.pull(rankingstat.snr_min, two_or_more_instruments, flushed):
367 rankingstat.denominator.increment(event)
377 self.sngl_inspiral_table.extend(newly_reported)
385 self.sngl_inspiral_table.extend(event
for event
in flushed_unused
if event.snr >= self.
sngls_snr_threshold)