B
    d                 @   s^   d dl m Z  d dlmZ d dlmZ dZddlmZ ddlm	Z
 dd	 Zd
d ZdddZdS )    )tqdm)	iterutils)segmentsz"Kipp Cannon <kipp.cannon@ligo.org>   )date)versionc             C   s(   t t| d |d t| d |d S )z;
	Return the smallest segment that contains both a and b.
	r   r   )r   segmentminmax)ab r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalburst/snglcluster.pysmallest_enclosing_seg0   s    r   c          	   C   sT   t | d |t|d | d   ||   | d |t|d | d   ||   S )z
	Return the segment whose start and ends are the weighted arithmetic
	means of the start and ends of the two input segments, using the
	two weights given.

	The arithmetic means are computed in a way that is safe for
	LIGOTimeGPS objects.
	r   r   )r   r   float)Zseg1Zweight1Zseg2Zweight2r   r   r   weighted_average_seg7   s    	r   NFc          	   C   s6  d}x*t dt|  t| | d}|dk	r:| j|d d}d}	x|	t| k r| |	 dkrp|	d7 }	|  qDd}
xtt| |	d d |	d D ]V\}}|dk	r|| |	 |s|| |	 || |	< d| |< d}
q|dk	r|| |	 |rP qW |
rd}qD|	d7 }	|  qDW W dQ R X |sP td	d
 |  d}qW |S )aO  
	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 sortkeyfunc 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 sortkeyfunc as a sort key 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).
	Fzclustering %d events)desctotaldisableN)keyr   r   Tc             S   s   | d k	S )Nr   )eventr   r   r   <lambda>       z cluster_events.<locals>.<lambda>)r   lensortupdate	enumerater   Zinplace_filter)eventsZtestfuncZclusterfuncZsortkeyfuncZbailoutfuncverbosechangedprogressZouter_did_clusteriZinner_did_clusterjZevent_jr   r   r   cluster_eventsL   s<    $r#   )NNF)r   Zlalr   Zligor   
__author__Zgit_versionr   __date__r   __version__r   r   r#   r   r   r   r   <module>   s   