30from lal
import iterutils
31import igwn_segments
as segments
34__author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
35from .git_version
import date
as __date__
36from .git_version
import version
as __version__
50 Return the smallest segment that contains both a and b.
52 return segments.segment(
min(a[0], b[0]),
max(a[1], b[1]))
57 Return the segment whose start and ends are the weighted arithmetic
58 means of the start and ends of the two input segments, using the
61 The arithmetic means are computed in a way that is safe for
64 return segments.segment(seg1[0] + weight2 * float(seg2[0] - seg1[0]) / (weight1 + weight2), seg1[1] + weight2 * float(seg2[1] - seg1[1]) / (weight1 + weight2))
76def cluster_events(events, testfunc, clusterfunc, sortkeyfunc = None, bailoutfunc = None, verbose = False):
78 Cluster the events in an event list. testfunc will be passed a
79 pair of events in random order,
and must
return 0 (
or False)
if
80 they should be clustered. clusterfunc will be passed a pair of
81 events
in random order,
and must
return an event that
is the
82 "cluster" of the two. clusterfunc
is free to
return a new events,
83 or modify one
or the other of its parameters
in place
and return
86 If sortkeyfunc
and bailoutfunc are both
not None (
if one
is
87 provided the other must be
as well), the events will be sorted into
88 "increasing" order using sortkeyfunc
as a sort key operator,
and
89 then only pairs of events
for which bailoutfunc returns 0 (
or
90 False) will be considered
for clustering.
92 The
return value
is True if the events
in the event list were
93 modified,
and False if they were
not (although their order might
96 # changed indicates if the event list has changed
99 with tqdm(desc =
"clustering %d events" % len(events), total = len(events), disable =
not verbose)
as progress:
100 if sortkeyfunc
is not None:
101 events.sort(key = sortkeyfunc)
105 outer_did_cluster =
False
107 while i < len(events):
108 if events[i]
is None:
115 inner_did_cluster =
False
116 for j, event_j
in enumerate(events[i + 1:], i + 1):
117 if event_j
is not None:
118 if not testfunc(events[i], event_j):
121 inner_did_cluster =
True
122 elif (sortkeyfunc
is not None)
and bailoutfunc(events[i], event_j):
124 if inner_did_cluster:
125 outer_did_cluster =
True
132 if not outer_did_cluster:
134 iterutils.inplace_filter(
lambda event: event
is not None, events)
static double max(double a, double b)
static double min(double a, double b)
def cluster_events(events, testfunc, clusterfunc, sortkeyfunc=None, bailoutfunc=None, verbose=False)
Cluster the events in an event list.
def weighted_average_seg(seg1, weight1, seg2, weight2)
Return the segment whose start and ends are the weighted arithmetic means of the start and ends of th...
def smallest_enclosing_seg(a, b)
Return the smallest segment that contains both a and b.