B
    d.                 @   s   d dl Z d dlZd dlmZ d dlmZ 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Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd#d!d"ZdS )$    N)	lsctables)process)search_summary)segments   )snglclusterz"Kipp Cannon <kipp.cannon@ligo.org>)date)versionZlalapps_buclusterc          	   C   s   t j| td|itdt|dS )Ncluster_algorithmZlscsoft)programZ	paramdictr	   Zcvs_repositoryZcvs_entry_timecomment)ligolw_processZregister_to_xmldocprocess_program_name__version____date__)xmldocr
   r    r   _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalburst/bucluster.pyappend_process7   s    r   c             C   s   t tj|  d S )N)add_ms_columns_to_tabler   SnglBurstTable	get_table)r   r   r   r   add_ms_columnsU   s    r   c          	   C   s   d}x>dD ]6}y|  | W q
 tk
r>   | | d}Y q
X q
W |sLd S x<| D ]4}|j|_|j|_|j|_|j	|_
|j|_|j|_qRW d S )NF)	peak_frequencyZms_start_timeZms_start_time_nsZms_durationZms_flowZms_bandwidthms_hrssms_snrms_confidenceT)ZgetColumnByNameKeyErrorZappendColumnZcentral_freqr   period	ms_periodbandms_band	amplituder   snrr   
confidencer   )sngl_burst_tableaddedZcolnamerowr   r   r   r   Y   s     


r   c             C   s8   t | sdS | d j}x| D ]}t|j| |_qW |S )z>
	For speed, convert peak times to floats relative to epoch.
	Nr   )lenpeakfloat	peak_time)r%   offsetr'   r   r   r   ExcessPowerPreFunc}   s    

r-   c             C   s   x| D ]}||j  |_qW dS )z6
	Restore peak times to absolute LIGOTimeGPS values.
	N)r+   r)   )r%   r,   r'   r   r   r   ExcessPowerPostFunc   s    
r.   c             C   s   | j | j| j| jfS )z]
	Sort key for grouping excess power triggers near triggers with
	which they might cluster.
	)ifochannelsearchstart)ar   r   r   ExcessPowerSortKeyFunc   s    r4   c             C   s.   | j | j| jf|j |j|jfkp,| j|jS )a  
	Returns True if a's and b's (ifo, channel, seach) are different or
	if the periods they span are disjoint.  Knowing excess power
	triggers have been ordered according to ExcessPowerSortKeyFunc(),
	then if for a pair of events this function returns False, we know
	the result will also be False for all other events farther apart in
	the list.  This is used to terminate the scan for events to
	cluster.
	)r/   r0   r1   r   disjoint)r3   br   r   r   ExcessPowerBailoutFunc   s    
r7   c             C   s<   | j | j| jf|j |j|jfkp:| j|jp:| j|jS )z
	Return False if a and b cluster.  To cluster, two events must be
	from the same channel of the same instrument, and their
	time-frequency tiles must be non-disjoint.
	)r/   r0   r1   r   r5   r    )r3   r6   r   r   r   ExcessPowerTestFunc   s    r8   c             C   s\  | j |j kr,| j|jkr,|j| jkr(|S | S |j| jkrP|j| _|j| _|j| _t| j| jd |j|jd | _t| j	| jd |j	|jd | _	| jd | j
 |jd |j
  | jd |jd   | _
| jd | j |jd |j  | jd |jd   | _|  j|j7  _t| jd |jd  | _| j| _t| j|j| _t| j |j | _ | S )a  
	Modify a in place to be a cluster constructed from a and b.  The
	cluster's time-frequency tile is the smallest tile that contains
	the original two tiles, and the "most signficiant" contributor for
	the cluster is the tile whose boundaries are the SNR^{2} weighted
	average boundaries of the two contributing tiles.  The "most
	signficiant" contributor's h_{rss}, SNR, and confidence, are copied
	verbatim from whichever of the two contributing tiles has the
	highest confidence.  The modified event a is returned.
	g       @)r   r    r   r   r   r   weighted_average_segr   r#   r!   r+   r   r"   mathsqrtr$   smallest_enclosing_seg)r3   r6   r   r   r   ExcessPowerClusterFunc   s$    ""66r=   c             C   sD  | j |j kr,| j|jkr,|j| jkr(|S | S |j| jkr@|j| _t| j| jd |j|jd | _t| j| jd |j|jd | _| jd | j |jd |j  | jd |jd   | _| jd | j	 |jd |j	  | jd |jd   | _	|  j
|j
7  _
t| jd |jd  | _t| j|j| _t| j |j | _ | S )a  
	Modify a in place to be a cluster constructed from a and b.  The
	cluster's time-frequency tile is the smallest tile that contains
	the original two tiles, and the "most signficiant" contributor for
	the cluster is the tile whose boundaries are the SNR^{2} weighted
	average boundaries of the two contributing tiles.  The "most
	signficiant" contributor's h_{rss}, SNR, and confidence, are copied
	verbatim from whichever of the two contributing tiles has the
	highest confidence.  The modified event a is returned.
	g       @)r   r    r#   r   r   r9   r   r!   r+   r   r"   r:   r;   r<   )r3   r6   r   r   r   OmegaClusterFunc  s    ""66r>   Fc
             C   s   yt j| }
W n* tk
r:   |	r2tdtjd | dfS X tj| |d	 }|	r`tdtjd ||
}t
j|
|||||	d}|	rtdtjd ||
| | |_tj| || | t|
d | |fS )	a  
	Run the clustering algorithm on the list of burst candidates.  The
	return value is the tuple (xmldoc, changed), where xmldoc is the
	input document, and changed is a boolean that is True if the
	contents of the sngl_burst table were altered, and False if the
	triggers were not modified by the clustering process.

	If the document does not contain a sngl_burst table, then the
	document is not modified (including no modifications to the process
	metadata tables).
	z:document does not contain a sngl_burst table, skipping ...)fileF)r   zpre-processing ...)sortkeyfuncbailoutfuncverbosezpost-processing ...)ZinsegZoutsegZnevents)r   r   r   
ValueErrorprintsysstderrligolw_search_summaryZ%segmentlistdict_fromsearchsummary_outZcoalescer   Zcluster_eventskeysZinstrumentsZappend_search_summaryZ
extent_allr(   )r   r   r   ZprefuncZpostfuncZtestfuncZclusterfuncr@   rA   rB   r%   ZseglistsZpreprocess_outputZtable_changedr   r   r   	buclusterR  s"    


 rI   )NNF)r:   rE   Zligo.lwr   Zligo.lw.utilsr   r   r   rG   Zligor    r   
__author__Zgit_versionr   r   r	   r   r   r   r   r   r-   r.   r4   r7   r8   r=   r>   rI   r   r   r   r   <module>   s.   $	QY  