B
    d{                 @   s  d Z 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dl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 G dd dejZe ej_e_G dd dejZe ej_e_ejdd	ddZ ejddddZ!ejddddZ"ejddddZ#ejddddZ$ejddddZ%ejd d	ddZ&ejd dddZ'ejd dddZ(ejd dddZ)ejd dddZ*ejd dddZ+ejd!dd"dZ,ejd!d	ddZ-ejd!dddZ.ejd!dddZ/ejd!dddZ0ejd!dddZ1ejd!dddZ2ejd#dd"dZ3ejd#d	ddZ4ejd#dddZ5ejd#dddZ6ejd#dddZ7ejd#dddZ8ejd#dddZ9G d$d% d%e:Z;d&Z<d'd( Z=d)d* Z>d+d, Z?dGd.d/Z@dHd0d1ZAd2d3 ZBd4d5 ZCd6d7 ZDd8d9 ZEd:d; ZFd<d= ZGd>d? ZHd@dA ZIdBdC ZJdIdEdFZKdS )Ja/  
Burst injection identification library.  Contains code providing the
capacity to search a list of sngl_burst burst candidates for events
matching entries in a sim_burst list of software injections, recording the
matches as burst <--> injection coincidences using the standard coincidence
infrastructure.  Also, any pre-recorded burst <--> burst coincidences are
checked for cases where all the burst events in a coincidence match an
injection, and these are recorded as coinc <--> injection coincidences,
again using the standard coincidence infrastructure.
    N)	lsctables)coincs)process)search_summary)
time_slide)segments   )burca)SimBurstUtilsz"Kipp Cannon <kipp.cannon@ligo.org>)date)versionc               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )	SnglBurst c             C   s
   | j |k S )N)end)selfotherr   r   ^/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalburst/binjfind.py__lt__S   s    zSnglBurst.__lt__c             C   s
   | j |kS )N)r   )r   r   r   r   r   __le__V   s    zSnglBurst.__le__c             C   s
   | j |kS )N)r   )r   r   r   r   r   __eq__Y   s    zSnglBurst.__eq__c             C   s
   | j |kS )N)r   )r   r   r   r   r   __ne__\   s    zSnglBurst.__ne__c             C   s
   | j |kS )N)r   )r   r   r   r   r   __ge___   s    zSnglBurst.__ge__c             C   s
   | j |kS )N)r   )r   r   r   r   r   __gt__b   s    zSnglBurst.__gt__N)
__name__
__module____qualname__	__slots__r   r   r   r   r   r   r   r   r   r   r   J   s   r   c               @   s   e Zd ZdS )SimInspiralN)r   r   r   r   r   r   r   r   q   s   r   excesspowerz$sim_burst<-->sngl_burst coincidences)searchsearch_coinc_typedescription   z'sim_inspiral<-->sngl_burst coincidences   z-sim_burst<-->coinc_event coincidences (exact)   z.sim_burst<-->coinc_event coincidences (nearby)   z0sim_inspiral<-->coinc_event coincidences (exact)   z1sim_inspiral<-->coinc_event coincidences (nearby)
StringCuspomegaz%sngl_burst<-->sngl_burst coincidences	waveburstc               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )DocContentsz,
	A wrapper interface to the XML document.
	c          	      s(  |	_ tj|_ytj|_W n tk
r@   d _Y nX ytj|_	W n tk
rn   d _	Y nX ytj
|}W n tk
r   d }Y nX |d k	r| _ni _jrtjd_nd_t||
 _j	d k	r(tj|i jd|	ddd}xj	D ]}||_qW jd k	rRtj||j|jd|jd_nd _j	d k	rtj||j|jd|jd_nd _ytj||j|jdd W n2 tk
r   d  d _ d _!d _"d _#Y nX jd k	rtj||j|jd|jd_ tj||j|jd|jd_!nd _ d _!j	d k	rntj||j|jd|jd_"tj||j|jd|jd_#nd _"d _#ytj$|_%W n6 tk
r   t&tj$_%|j'd (j% Y nX j%)  ytj*|_+W n6 tk
r   t&tj*_+|j'd (j+ Y nX t,d	d
 jD }t, fdd
j%D _-xDj+D ]:}yj-|j. /||j0  W n tk
r   Y nX qNW ~x6j-1 D ](\}}|j2dd d t3|j-|< qW j-1 _-t, fdd
j%D _4t,fdd
t5jdD _j-j2dd d d S )Ndurationg        TF)
create_newZsuperset_okZnonunique_ok)r,   r!   )r,   r   c             s   s   | ]}|j |fV  qd S )N)event_id).0rowr   r   r   	<genexpr>(  s    z'DocContents.__init__.<locals>.<genexpr>c             3   s"   | ]}|j  kr|jg fV  qd S )N)coinc_def_idcoinc_event_id)r.   r/   )b_b_coinc_def_idr   r   r0   *  s    c             S   s   | j S )N)peak)eventr   r   r   <lambda>5      z&DocContents.__init__.<locals>.<lambda>)keyc             3   s*   | ]"}|j  kr|jj|j fV  qd S )N)r1   r2   offsetvectorstime_slide_id)r.   r/   )r3   r   r   r   r0   ;  s    c             3   s2   | ]*  t  fd djD dd dfV  qdS )c             3   s   | ]}|j  kr|V  qd S )N)ifo)r.   r5   )
instrumentr   r   r0   F  s    z1DocContents.__init__.<locals>.<genexpr>.<genexpr>c             S   s   | j S )N)r4   )r5   r   r   r   r6   F  r7   z0DocContents.__init__.<locals>.<genexpr>.<lambda>)r8   N)sortedsnglbursttable)r.   )r   )r<   r   r0   F  s    r;   c             S   s   | d d j S )Nr   r   )r4   )Zcoinc_id_eventsr   r   r   r6   G  r7   )6r   r   SnglBurstTableZ	get_tabler>   SimBurstTablesimbursttable
ValueErrorSimInspiralTablesiminspiraltableZTimeSlideTableas_dictr9   maxZgetColumnByNamelongestdurationligolw_search_summaryZ!segmentlistdict_fromsearchsummaryZcoalesceseglistsligolw_time_slideZget_time_slide_idfromkeysr:   ligolw_coincsZget_coinc_def_idr   r    r!   sb_b_coinc_def_idsi_b_coinc_def_idKeyErrorsb_c_e_coinc_def_idsb_c_n_coinc_def_idsi_c_e_coinc_def_idsi_c_n_coinc_def_idZ
CoincTable
coinctableZNewZ
childNodesZappendChildZsync_next_idZCoincMapTablecoincmaptabledictr   r2   appendr-   itemssorttuplecoincoffsetsset)r   xmldocb_b_defsb_b_defsi_b_def
sb_c_e_def
sb_c_n_def
si_c_e_def
si_c_n_defr   livetime_programZtimeslidetabler:   simindexr/   r2   eventsr   )r3   r   r   __init__   s    
	
$zDocContents.__init__c                s"   t  fdd| j D g S )z
		Return a list of the burst events whose peak times (with
		offsetvector added) are within window seconds of t.  This
		is not used to define any coincidences, only to provide a
		short list of burst events for use in more costly
		comparison tests.
		c          	   3   sF   | ]>\}}|t | |   t | |    V  qd S )N)bisectbisect_leftbisect_right)r.   r<   rh   )offsetvectortwindowr   r   r0   T  s    z3DocContents.bursts_near_peaktime.<locals>.<genexpr>)sumr>   rX   )r   rn   ro   rm   r   )rm   rn   ro   r   bursts_near_peaktimeI  s    z DocContents.bursts_near_peaktimec                s*   t ||  fddjD S )a  
		Return a list of the (coinc_event_id, event list) tuples in
		which at least one burst event's peak time is within window
		seconds of t.  This is not used to define any coincidences,
		only to provide a short list of coinc events for use in
		more costly comparison tests.
		c                s4   g | ],\}}t | @ rj| r||fqS r   )r\   containsr[   )r.   r2   rh   )near_eventsrm   r   r   r   
<listcomp>j  s    z4DocContents.coincs_near_peaktime.<locals>.<listcomp>)r\   rq   r   )r   rn   ro   rm   r   )rs   rm   r   r   coincs_near_peaktimeV  s    z DocContents.coincs_near_peaktimec             C   sR   | j  }| jj|_||_| j  |_||_|d d|_	d|_
| j | |S )z
		Construct a new coinc_event row attached to the given
		process, and belonging to the set of coincidences defined
		by the given coinc_def_id.
		Nr   )rT   RowTyper   Z
process_idr1   Zget_next_idr2   r:   set_instrumentsneventsZ
likelihoodrW   )r   r1   r:   coincr   r   r   	new_coincl  s    


zDocContents.new_coincN)r   r   r   __doc__ri   rq   ru   rz   r   r   r   r   r*      s    $r*   Zlalapps_binjfindc          	   C   s   t j| td|itdt|dS )zD
	Convenience wrapper for adding process metadata to the document.
	match_algorithmZlscsoft)programZ	paramdictr   Zcvs_repositoryZcvs_entry_timecomment)ligolw_processZregister_to_xmldocprocess_program_name__version____date__)r]   r|   r~   r   r   r   append_process  s    r   c             C   s4   |  |j|}tjd }t|| || |jS )z
	Return False (injection matches event) if an autocorrelation-width
	window centred on the injection is continuous with the time
	interval of the burst.
	r#   )time_at_instrumentr;   r
    stringcusp_autocorrelation_widthr   segmentdisjointperiod)rf   burstrm   tinjro   r   r   r   StringCuspSnglCompare  s    
r   c             C   s    |  |j||jkp| j|jkS )z
	Return False (injection matches event) if the peak time and centre
	frequency of sim lie within the time-frequency tile of burst.
	)r   r;   r   Z	frequencyZband)rf   r   rm   r   r   r   ExcessPowerSnglCompare  s    r         $@c             C   s    t t| |j||j |kS )z
	Return False (injection matches event) if the time of the sim and
	the peak time of the burst event differ by less than or equal to
	delta_t seconds.
	)absfloatr   r;   r4   )rf   r   rm   delta_tr   r   r   OmegaSnglCompare  s    r   c             C   s    t t| |j||j |kS )z
	Return False (injection matches event) if the time of the sim and
	the peak time of the burst event differ by less than or equal to
	delta_t seconds.
	)r   r   r   r;   r4   )rf   r   rm   r   r   r   r   CWBSnglCompare  s    r   c             C   s:   |  |j|}tjd tj }t|| || |jS )zb
	Return False (injection matches coinc) if the peak time of the sim
	is "near" the burst event.
	r#   )	r   r;   r
   r   burst_is_near_injection_windowr   r   r   r   )rf   r   rm   r   ro   r   r   r   StringCuspNearCoincCompare  s    r   c             C   s0   |  |j|}tj}t|| || |jS )zb
	Return False (injection matches coinc) if the peak time of the sim
	is "near" the burst event.
	)r   r;   r
   r   r   r   r   r   )rf   r   rm   r   ro   r   r   r   ExcessPowerNearCoincCompare  s    r   c             C   s   t | ||d|jd  dS )zb
	Return False (injection matches coinc) if the peak time of the sim
	is "near" the burst event.
	g      4@r#   )r   )r   r+   )rf   r   rm   r   r   r   OmegaNearCoincCompare  s    r   c             C   s   t | ||d|jd  dS )zb
	Return False (injection matches coinc) if the peak time of the sim
	is "near" the burst event.
	g      4@r#   )r   )r   r+   )rf   r   rm   r   r   r   CWBNearCoincCompare  s    r   c                s.   | j j  fdd| j|D S )a  
	Scan the burst table for triggers matching sim.  sieve_window is
	used in a bisection search to quickly identify burst events within
	that many seconds of the injection's peak time at the geocentre;
	it should be larger than the greatest time difference that can
	separate a burst event's peak time from an injection's peak time at
	the geocentre and the two still be considered a match.
	c                s   g | ]} |s|qS r   r   )r.   r   )comparefuncrm   rf   r   r   rt     s    z+find_sngl_burst_matches.<locals>.<listcomp>)r9   r:   rq   time_geocent)contentsrf   r   Zsieve_windowr   )r   rm   rf   r   find_sngl_burst_matches  s    	r   c             C   s   |  ||j}|tdd |D  t||_| j }|j|_|j	j
|_
|j	|_| j| x<|D ]4}| j }|j|_|jj
|_
|j|_| j| qfW |S )z
	Create a coinc_event in the coinc table, and add arcs in the
	coinc_event_map table linking the sim_burst row and the list of
	sngl_burst rows to the new coinc_event row.
	c             s   s   | ]}|j V  qd S )N)r;   )r.   r5   r   r   r   r0   
  s    z&add_sim_burst_coinc.<locals>.<genexpr>)rz   r:   rw   r\   lenrx   rU   rv   r2   simulation_id
table_namer-   rW   )r   rf   rh   r1   ry   coincmapr5   r   r   r   add_sim_burst_coinc  s    





r   c                s*   t |t fdd| D S )z
	Return a set of the coinc_event_ids of the burst<-->burst coincs in
	which all burst events match sim and to which all instruments on at
	the time of the sim contributed events.
	c             3   sH   | ]@\}} td d |D rt fdd|D s|V  qdS )c             s   s   | ]}|j V  qd S )N)r;   )r.   r5   r   r   r   r0   5  s    z5find_exact_coinc_matches.<locals>.<genexpr>.<genexpr>c             3   s   | ]} |V  qd S )Nr   )r.   r5   )r   rm   rf   r   r   r0   5  s    N)issubsetr\   any)r.   r2   rh   )r   rm   on_instrumentsrf   r   r   r0   5  s    z+find_exact_coinc_matches.<locals>.<genexpr>)r
   r   r\   )r   rf   r   rI   rm   r   )r   rm   r   rf   r   find_exact_coinc_matches&  s    r   c                s   t  fdd| D S )zt
	Return a set of the coinc_event_ids of the burst<-->burst coincs in
	which at least one burst event matches sim.
	c             3   s0   | ](\}}t  fd d|D s|V  qdS )c             3   s   | ]} |V  qd S )Nr   )r.   r5   )r   rm   rf   r   r   r0   E  s    z4find_near_coinc_matches.<locals>.<genexpr>.<genexpr>N)all)r.   r2   rh   )r   rm   rf   r   r   r0   E  s    z*find_near_coinc_matches.<locals>.<genexpr>)r\   )r   rf   r   rm   r   )r   rm   rf   r   find_near_coinc_matches8  s    r   c             C   s   |  ||j}t||_| j }|j|_|jj|_|j|_	| j
| x8|D ]0}| j }|j|_|j|_||_	| j
| qNW |S )z
	Create a coinc_event in the coinc table, and add arcs in the
	coinc_event_map table linking the sim_burst row and the list of
	coinc_event rows to the new coinc_event row.
	)rz   r:   r   rx   rU   rv   r2   r   r   r-   rW   )r   rf   Zcoinc_event_idsr1   ry   r   r2   r   r   r   add_sim_coinc_coincH  s    




r   Fc             C   sV  |rt dtjd tjtjttd| }tt	t
td| }ttttd| }ttttd| }	ttttd| }
ttttd| }ttttd| }t | ||||	|
|||ddd d d| d
}t!j"t!j# d }||j$7 }|t%j&d	 d
ddd| 7 }|t%j' }|j(d k	rt)|j(}|r.t d|j* tjd x^t+|j(D ]P\}}|rdt dd| |  dtjd t,||||}|r:t-||||j. q:W |rt dtjd |rt d|	j*|
j*f tjd xt+|j(D ]\}}|rt dd| |  dtjd |j/|j0 }|1|j2||}t3||||j4|}t5||||}|6|sDt7|rZt8||||j9 |rt8||||j: qW |rt dtjd n|rt dt;j<j= tjd |j>d k	r6t)|j>}|rt d|j* tjd x^t+|j>D ]P\}}|rt dd| |  dtjd t,||||}|rt-||||j? qW |rLt dtjd |rlt d|j*|j*f tjd xt+|j>D ]\}}|rt dd| |  dtjd |j/|j0 }|1|j2||}t3||||j4|}t5||||}|6|st7|rt8||||j@ |rxt8||||jA qxW |rRt dtjd n|rRt dt;jBj= tjd | S )Nzindexing ...)file)r'   r   r)   r(   ZStringSearchZlalapps_power)r'   r   r(   r)   )
r]   r^   r_   r`   ra   rb   rc   rd   r   re   g      ?r#   g        g      >@zconstructing %s:z	%.1f%%g      Y@ )r   r   z	100.0%zconstructing %s and %s:z!no %s table in document, skipping)Cprintsysstderrr	   ZStringCuspBBCoincDefZExcessPowerBBCoincDefCWBBBCoincDefOmegaBBCoincDefStringCuspSBBCoincDefExcessPowerSBBCoincDefCWBSBBCoincDefOmegaSBBCoincDefStringCuspSIBCoincDefExcessPowerSIBCoincDefCWBSIBCoincDefOmegaSIBCoincDefStringCuspSBCCoincDefExcessPowerSBCCoincDefCWBSBCCoincDefOmegaSBCCoincDefStringCuspSBCNearCoincDefExcessPowerSBCNearCoincDefCWBSBCNearCoincDefOmegaSBCNearCoincDefStringCuspSICCoincDefExcessPowerSICCoincDefCWBSICCoincDefOmegaSICCoincDefStringCuspSICNearCoincDefExcessPowerSICNearCoincDefCWBSICNearCoincDefOmegaSICNearCoincDefr*   lalZ	REARTH_SIZC_SIrG   r
   r   r   rA   r   r!   	enumerater   r   rM   r9   r:   ru   r   r   rI   r   r   AssertionErrorr   rP   rQ   r   r@   Z	tableNamerD   rN   rR   rS   rC   )r]   r   r   ZsnglcomparefuncZnearcoinccomparefuncverboser^   r_   r`   ra   rb   rc   rd   r   Zburst_peak_time_windowZcoinc_peak_time_windowNnrf   rh   rm   r   Zexact_coinc_event_idsZnear_coinc_event_idsr   r   r   binjfindk  s    




r   )r   )r   )F)Lr{   rj   r   Zligo.lwr   Zligo.lw.utilsr   rL   r   r   r   rH   r   rJ   r   Zligor    r	   r
   
__author__Zgit_versionr   r   r   r   r   r?   rv   r   rC   ZCoincDefr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   objectr*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>$   s|    f

	

$#