44 from glue
import iterutils
45 from glue
import segments
46 from glue.ligolw
import lsctables
47 from pylal
import ligolw_thinca
48 from pylal
import snglcoinc
67 allowed_instrument_combos = frozenset([frozenset((
"H1",
"H2",
"L1")), frozenset((
"H1",
"L1",
"V1")), frozenset((
"H1",
"L1")), frozenset((
"H1",
"V1")), frozenset((
"L1",
"V1")), frozenset((
"H1H2",
"L1")), frozenset((
"H1H2",
"L1",
"V1")), frozenset((
"E1",
"E2")), frozenset((
"E1",
"E3")), frozenset((
"E2",
"E3")), frozenset((
"E1",
"E2",
"E3"))])
84 def event_comparefunc(event_a, offset_a, event_b, offset_b, light_travel_time, delta_t):
89 return float(abs(event_a.get_end() + offset_a - event_b.get_end() - offset_b)) > light_travel_time + delta_t
100 def get_effective_snr(self, fac):
117 Returns an immutable hashable object (it can be used as a
118 dictionary key) uniquely identifying the template that
119 produced the given event.
121 return event.mass1, event.mass2, event.spin1x, event.spin1y, event.spin1z, event.spin2x, event.spin2y, event.spin2z
123 def make_index(self):
126 self.index.setdefault(self.
template(event), []).append(event)
127 for events
in self.index.values():
128 events.sort(
lambda a, b: cmp(a.end_time, b.end_time)
or cmp(a.end_time_ns, b.end_time_ns))
130 def get_coincs(self, event_a, offset_a, light_travel_time, e_thinca_parameter, comparefunc):
135 end = event_a.get_end() + offset_a - self.offset
155 return [event_b
for event_b
in events[bisect.bisect_left(events, end - self.dt) : bisect.bisect_right(events, end + self.dt)]
if not comparefunc(event_a, offset_a, event_b, self.offset, light_travel_time, e_thinca_parameter)]
163 ligolw_thinca.InspiralEventList = InspiralEventList
181 def __init__(self, coincidence_threshold, thinca_interval = 50.0, sngls_snr_threshold = None):
201 def set_coinc_params_distributions(self, coinc_params_distributions):
204 def del_coinc_params_distributions(self):
207 coinc_params_distributions = property(
None, set_coinc_params_distributions, del_coinc_params_distributions,
"ThincaCoincParamsDistributions instance with which to compute likelihood ratio values.")
210 def add_events(self, xmldoc, process_id, events, boundary, fapfar = None):
222 self.
sngl_inspiral_table = lsctables.New(lsctables.SnglInspiralTable, lsctables.SnglInspiralTable.get_table(xmldoc).columnnames)
229 for old_event
in events:
230 new_event = ligolw_thinca.SnglInspiral()
231 for col
in self.sngl_inspiral_table.columnnames:
232 setattr(new_event, col, getattr(old_event, col))
233 self.sngl_inspiral_table.append(new_event)
239 return self.
run_coincidence(xmldoc, process_id, boundary, fapfar = fapfar)
242 def run_coincidence(self, xmldoc, process_id, boundary, fapfar = None):
248 coincidence_back_off = max(abs(offset)
for offset
in lsctables.TimeSlideTable.get_table(xmldoc).getColumnByName(
"offset"))
260 noncoinc_sngls = [row
for row
in self.
sngl_inspiral_table if row.get_end() < discard_boundary
and row.event_id
not in self.
ids]
261 iterutils.inplace_filter(
lambda row: row.get_end() >= discard_boundary, self.
sngl_inspiral_table)
268 coinc_event_map_table = lsctables.New(lsctables.CoincMapTable)
269 coinc_event_table = lsctables.New(lsctables.CoincTable)
270 coinc_inspiral_table = lsctables.New(lsctables.CoincInspiralTable)
273 real_sngl_inspiral_table = lsctables.SnglInspiralTable.get_table(xmldoc)
274 real_coinc_event_map_table = lsctables.CoincMapTable.get_table(xmldoc)
275 real_coinc_event_table = lsctables.CoincTable.get_table(xmldoc)
276 real_coinc_inspiral_table = lsctables.CoincInspiralTable.get_table(xmldoc)
278 xmldoc.childNodes[-1].replaceChild(coinc_event_map_table, real_coinc_event_map_table)
279 xmldoc.childNodes[-1].replaceChild(coinc_event_table, real_coinc_event_table)
280 xmldoc.childNodes[-1].replaceChild(coinc_inspiral_table, real_coinc_inspiral_table)
284 coinc_event_table.set_next_id(real_coinc_event_table.next_id)
289 def ntuple_comparefunc(events, offset_vector, seg = segments.segment(self.
last_boundary, boundary)):
290 return frozenset(event.ifo
for event
in events)
not in allowed_instrument_combos
or ligolw_thinca.coinc_inspiral_end_time(events, offset_vector)
not in seg
293 orig_get_effective_snr, ligolw_thinca.SnglInspiral.get_effective_snr = ligolw_thinca.SnglInspiral.get_effective_snr, get_effective_snr
296 ligolw_thinca.ligolw_thinca(
298 process_id = process_id,
299 coinc_definer_row = ligolw_thinca.InspiralCoincDef,
300 event_comparefunc = event_comparefunc,
302 ntuple_comparefunc = ntuple_comparefunc,
311 ligolw_thinca.SnglInspiral.get_effective_snr = orig_get_effective_snr
314 if fapfar
is not None:
315 coinc_event_index = dict((row.coinc_event_id, row)
for row
in coinc_event_table)
316 gps_time_now = float(lal.UTCToGPS(time.gmtime()))
317 for coinc_inspiral_row
in coinc_inspiral_table:
318 likelihood_ratio = coinc_event_index[coinc_inspiral_row.coinc_event_id].likelihood
319 coinc_inspiral_row.combined_far = fapfar.far_from_rank(likelihood_ratio)
321 coinc_inspiral_row.false_alarm_rate = fapfar.fap_from_rank(likelihood_ratio)
326 coinc_inspiral_row.minimum_duration = gps_time_now - float(coinc_inspiral_row.get_end())
330 self.
last_coincs = ligolw_thinca.sngl_inspiral_coincs(xmldoc)
334 real_coinc_event_table.set_next_id(coinc_event_table.next_id)
338 xmldoc.childNodes[-1].replaceChild(real_coinc_event_map_table, coinc_event_map_table)
339 xmldoc.childNodes[-1].replaceChild(real_coinc_event_table, coinc_event_table)
340 xmldoc.childNodes[-1].replaceChild(real_coinc_inspiral_table, coinc_inspiral_table)
343 if coinc_event_map_table:
349 self.
ids &= set(index)
350 newids = set(coinc_event_map_table.getColumnByName(
"event_id")) - self.
ids
355 real_sngl_inspiral_table.append(index[id])
356 map(real_coinc_event_map_table.append, coinc_event_map_table)
357 map(real_coinc_event_table.append, coinc_event_table)
358 map(real_coinc_inspiral_table.append, coinc_inspiral_table)
365 for event
in noncoinc_sngls:
367 real_sngl_inspiral_table.append(event)
373 return noncoinc_sngls
376 def flush(self, xmldoc, process_id, fapfar = None):
384 noncoinc_sngls = self.
run_coincidence(xmldoc, process_id, segments.infinity(), fapfar = fapfar)
392 self.sngl_inspiral_table.unlink()
404 return noncoinc_sngls