B
    d8                 @   s\   d 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 G dd	 d	e	ZdS )
z[Chisq based on sine-gaussian tiles.
See https://arxiv.org/abs/1709.08974 for a discussion.
    N)apply_fseries_time_shift)sigma)	sinegauss)SingleDetPowerChisq)rankingc               @   sD   e Zd ZdZdejiZdddZedd Z	e
d	d
 Zdd ZdS )SingleDetSGChisqzpClass that handles precomputation and memory management for efficiently
    running the sine-Gaussian chisq
    Zsg_chisqr   Nc             C   s   |dk	rd| _ || _|| _i | _xf|D ]V}|d\}}|jd|fdgd }|jd |t }	x|	D ]}
|| j|
< qjW q&W nd| _ dS )	a   Create sine-Gaussian Chisq Calculator

        Parameters
        ----------
        bank: pycbc.waveform.TemplateBank
            The template bank that will be processed.
        num_bins: str
            The string determining the number of power chisq bins
        snr_threshold: float
            The threshold to calculate the sine-Gaussian chisq
        chisq_locations: list of strs
            List of strings which detail where to place a sine-Gaussian.
            The format is 'region-boolean:q1-offset1,q2-offset2'.
            The offset is relative to the end frequency of the approximant.
            The region is a boolean expression such as 'mtotal>40' indicating
            which templates to apply this set of sine-Gaussians to.
        NT:   )r   elser   template_hashF)	donum_binssnr_thresholdparamssplittableZparse_boolargsastypebool)selfbankr   r   Zchisq_locationsdescrregionvaluesmaskhashesh r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/vetoes/sgchisq.py__init__   s    

zSingleDetSGChisq.__init__c             C   s0   |  d}|jdtdd |jdtddd d S )	NzSine-Gaussian Chisqz--sgchisq-snr-thresholdz%Minimum SNR threshold to use SG chisq)typehelpz--sgchisq-locations+a  Frequency offsets and quality factors of the sine-Gaussians to use, format 'region-boolean:q1-offset1,q2-offset2'. Offset is relative to the end frequency of the approximant. Region is a boolean expression selecting templates to apply the sine-Gaussians to, ex. 'mtotal>40')r   nargsr    )add_argument_groupadd_argumentfloatstr)parsergroupr   r   r   insert_option_group5   s
    

z$SingleDetSGChisq.insert_option_groupc             C   s   | |||j |jS )N)Zsgchisq_snr_thresholdZsgchisq_locations)clsargsr   Z
chisq_binsr   r   r   from_cliA   s    zSingleDetSGChisq.from_clic	       $   
   C   sl  | j s
dS |jj| jkr&tt|S | j|jj d}	| ||}
tt|}xt|D ]\}}t	|| }t
||| ||  }|| jk rq`t|d d }d||j  }t|j|j }t|j|||   }t|| }d}d||< |
d |
d	  }|
d | |j }t||j d
 }d}x|	D ]}|d\}}t|t| }}|| }t||j || }|| }||krtt|S t||j }t||j } td|||t||j |jtj}!t|!|||d}"|!||  |||    }#|#d|!j |" 9 }#||  t	|#d 7  < |d7 }q6W |dkrTd||< q`||  |  < q`W |S )a]   Calculate sine-Gaussian chisq

        Parameters
        ----------
        stilde: pycbc.types.Frequencyseries
            The overwhitened strain
        template: pycbc.types.Frequencyseries
            The waveform template being analyzed
        psd: pycbc.types.Frequencyseries
            The power spectral density of the data
        snrv: numpy.ndarray
            The peak unnormalized complex SNR values
        snr_norm: float
            The normalization factor for the snr
        bchisq: numpy.ndarray
            The Bruce Allen power chisq values for these triggers
        bchisq_dof: numpy.ndarray
            The degrees of freedom of the Bruce chisq
        indics: numpy.ndarray
            The indices of the snr peaks.

        Returns
        -------
        chisq: Array
            Chisq values, one for each sample index
        N,r	      g      ?2   r   g?-)psdZlow_frequency_cutoffZhigh_frequency_cutoffg      @g       @)r   r   r   numpyZoneslenr   Zcached_chisq_bins	enumerateabsr   Znewsnrr   Zdelta_fintZf_lowerr%   epochr   maxr   Zfd_sine_gaussianr   Z	complex64r   sum)$r   Zstildetemplater3   ZsnrvZsnr_normZbchisqZ
bchisq_dofindicesr   ZbinsZchisqiZsnrviZsnrZnsnrNdtZkmintimeZstilde_shiftZqwindowZfstepZfpeakZfstopZdofr   qoffsetZfcenZflowZfhighZkmaxZgtemZgsigmaZgsnrr   r   r   r   G   s^    



zSingleDetSGChisq.values)r   NN)__name__
__module____qualname____doc__r4   Zfloat32Zreturnsr   staticmethodr)   classmethodr,   r   r   r   r   r   r      s   
  
 r   )rG   r4   Zpycbc.waveform.utilsr   Zpycbc.filterr   Zpycbc.waveformr   Zpycbc.vetoes.chisqr   Zpycbc.eventsr   r   r   r   r   r   <module>   s   