def cluster_events(events, testfunc, clusterfunc, sortfunc=None, bailoutfunc=None, verbose=False): """ Cluster the events in an event list. testfunc will be passed a pair of events in random order, and must return 0 (or False) if they should be clustered. clusterfunc will be passed a pair of events in random order, and must return an event that is the "cluster" of the two. clusterfunc is free to return a new events, or modify one or the other of its parameters in place and return it. If sortfunc and bailoutfunc are both not None (if one is provided the other must be as well), the events will be sorted into "increasing" order using sortfunc as a comparison operator, and then only pairs of events for which bailoutfunc returns 0 (or False) will be considered for clustering. The return value is True if the events in the event list were modified, and False if they were not (although their order might have changed). """ # changed indicates if the event list has changed changed = False while True: if verbose: progress = ProgressBar("clustering %d events" % len(events), max=len(events)) progress.show() else: progress = None if sortfunc is not None: events.sort(sortfunc) # outer_did_cluster indicates if the event list changes on # this pass outer_did_cluster = False i = 0 while i < len(events): if progress is not None: progress.update(i) if events[i] is not None: # inner_did_cluster indicates if events[i] # has changed inner_did_cluster = False for j, event_j in enumerate(events[i + 1:], 1): if event_j is not None: if not testfunc(events[i], event_j): events[i] = clusterfunc(events[i], event_j) events[i + j] = None inner_did_cluster = True elif (sortfunc is not None) and bailoutfunc( events[i], event_j): break if inner_did_cluster: outer_did_cluster = True # don't advance until events[i] # stops changing continue # events[i] has not changed i += 1 del progress # repeat until we do a pass without the listing changing if not outer_did_cluster: break iterutils.inplace_filter(lambda event: event is not None, events) changed = True return changed
def cluster_events(events, testfunc, clusterfunc, sortfunc = None, bailoutfunc = None, verbose = False): """ Cluster the events in an event list. testfunc will be passed a pair of events in random order, and must return 0 (or False) if they should be clustered. clusterfunc will be passed a pair of events in random order, and must return an event that is the "cluster" of the two. clusterfunc is free to return a new events, or modify one or the other of its parameters in place and return it. If sortfunc and bailoutfunc are both not None (if one is provided the other must be as well), the events will be sorted into "increasing" order using sortfunc as a comparison operator, and then only pairs of events for which bailoutfunc returns 0 (or False) will be considered for clustering. The return value is True if the events in the event list were modified, and False if they were not (although their order might have changed). """ # changed indicates if the event list has changed changed = False while True: if verbose: progress = ProgressBar("clustering %d events" % len(events), max = len(events)) progress.show() else: progress = None if sortfunc is not None: events.sort(sortfunc) # outer_did_cluster indicates if the event list changes on # this pass outer_did_cluster = False i = 0 while i < len(events): if progress is not None: progress.update(i) if events[i] is not None: # inner_did_cluster indicates if events[i] # has changed inner_did_cluster = False for j, event_j in enumerate(events[i + 1:], 1): if event_j is not None: if not testfunc(events[i], event_j): events[i] = clusterfunc(events[i], event_j) events[i + j] = None inner_did_cluster = True elif (sortfunc is not None) and bailoutfunc(events[i], event_j): break if inner_did_cluster: outer_did_cluster = True # don't advance until events[i] # stops changing continue # events[i] has not changed i += 1 del progress # repeat until we do a pass without the listing changing if not outer_did_cluster: break iterutils.inplace_filter(lambda event: event is not None, events) changed = True return changed