B
    d3                 @   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 Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdS )zFunctions to compute the area corresponding to different CBC on the m1 & m2
plane when given a central mchirp value and uncertainty.
It also includes a function that calculates the source frame when given the
detector frame mass and redshift.
    N)mass2_from_mchirp_mass1)quad)	_redshift)FlatLambdaCDMc             C   s   |  d}|jdtdddddgdd	 |jd
tddd |jdtdddd |jdtdddd |jdtddddd |jdddd |jddd d d S )!NzuArguments for estimating the source probabilities of a candidate event using the snr, mchirp, and effective distance.z--src-class-mass-limits   )ZMIN_M2ZMAX_NSZMAX_M1g      ?g      @g     F@zMinimum and maximum values for the mass of the binary components and maximum mass of a neutron star, used as limits when computing the area correspondingto different CBC sources.)typenargsmetavardefaulthelpz--src-class-mass-gap-maxZMAX_GAPzUpper limit of the mass gap, corresponding to the minimum mass of a black hole. Used as limit of integration of the different CBC regions when considering the MassGap category.)r   r	   r   z--src-class-mchirp-to-deltam0TzZCoefficient to estimate the value of the mchirp uncertainty by mchirp_delta = m0 * mchirp.)r   r	   requiredr   z--src-class-eff-to-lum-distancea0zvCoefficient to estimate the value of the luminosity distance from the minimum eff distance by D_lum = a0 * min(D_eff).z!--src-class-lum-distance-to-delta   )b0b1zCoefficients to estimate the value of the uncertainty on the luminosity distance from the estimated luminosity distance and the coinc snr by delta_lum = D_lum * exp(b0) * coinc_snr ** b1.)r   r   r	   r   r   z--src-class-mass-gap-separate
store_truezQGives separate probabilities for each kind of mass gap CBC sources: GNS, GG, BHG.)actionr   z--src-class-lal-cosmologyz^Uses the Planck15 cosmology defined in lalsuite instead of the astropy Planck15 default model.)add_argument_groupadd_argumentfloat)parserZmchirp_group r   ^/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/mchirp_area.pyinsert_args   s.    

r   c             C   s   t | j}| jrr| j|d k r(|d |d |d d|d | jd| j| jd | jd | jdd| j| jd	S |d |d d|d |d d| j| jd | jd | jdd
| j| jd	S )N   z/MAX_GAP value cannot be lower than MAX_NS limitr   r   )max_m1min_m2)ns_maxgap_max)r   r   r   r   T)mass_limits
mass_bdaryestimation_coeffmass_gapmass_gap_separatelal_cosmologyF)	sortedZsrc_class_mass_limitsZsrc_class_mass_gap_maxerrorZsrc_class_eff_to_lum_distanceZsrc_class_lum_distance_to_deltaZsrc_class_mchirp_to_deltaZsrc_class_mass_gap_separateZsrc_class_lal_cosmology)argsr   Zmass_limits_sortedr   r   r   from_cliC   s4    





r)   c       	      C   s\   |rt ddd}nd}t| |d}t| | |d}t| | |d}d||  }||d}|S )a  Takes values of distance and its uncertainty and returns a
       dictionary with estimates of the redshift and its uncertainty.
       If the argument 'lal_cosmology' is True, it uses Planck15 cosmology
       model as defined in lalsuite instead of the astropy default.
       Constants for lal_cosmology taken from Planck15_lal_cosmology() in
       https://git.ligo.org/lscsoft/pesummary/-/blob/master/pesummary/gw/
       cosmology.py.
    gP@gV-?)ZH0ZOm0N)	cosmologyg      ?)centraldelta)r   r   )	ZdistanceZdistance_stdr%   r*   Zz_estimationZ	z_est_maxZ	z_est_minZz_std_estimationzr   r   r   redshift_estimationf   s    	
r.   c             C   s8   |d|   }||| d |d|   d  d  }||fS )zTakes values of redshift, redshift uncertainty, detector mass and its
    uncertainty and computes the source mass and its uncertainty.
    g      ?g       @g      ?r   )r-   Zdel_zZmdetZdel_mdetZmsrcZdel_msrcr   r   r   src_mass_from_z_det_mass}   s    r/   c             C   s   t dd ||| d}|d S )zfReturns the integral of m2 over m1 between x_min and x_max,
       assuming that mchirp is fixed.
    c             S   s
   t || S )N)m2mcm1)xmcr   r   r   <lambda>       zintmc.<locals>.<lambda>)r(   r   )r   )r2   Zx_minZx_maxZintegralr   r   r   intmc   s    r5   c                s  | d | d  }| d | d  }d| }d| } dkrJ|}	|}
dd }n t | }	t | }
 fdd}t |}t |}fdd}t|	||}t|||}t|
||}t|||}t|||}t|||}t|||d }t|||d }|| | | }|S )	a  Returns the area under the chirp mass contour in a region of the m1-m2
    plane (m1 > m2).

    Parameters
    ----------
    trig_mc : sequence of two values
        first represents central estimate of mchirp in source frame,
        second its uncertainty
    lim_h1, lim_h2 : floats or the string 'diagonal'
        upper and lower horizontal limits of the region (limits on m2)
    lim_v1, lim_v2 : floats
        right and left vertical limits of the region (limits on m1)

    Returns
    -------
    area : float
    r   r   gu溆a?diagonalc             S   s   | S )Nr   )r1   r   r   r   r3      r4   zget_area.<locals>.<lambda>c                s    S )Nr   )r1   )lim_h1r   r   r3      r4   c                s    S )Nr   )r1   )lim_h2r   r   r3      r4   )r0   npZclipr5   r   )trig_mcr7   r8   Zlim_v1Zlim_v2mc_maxmc_minZmi_maxZmi_minZmax_h1Zmin_h1Zfun_supZmax_h2Zmin_h2Zfun_infZlim_max1Zlim_max2Zlim_min1Zlim_min2Zint_maxZint_minZintline_supZintline_infZarear   )r7   r8   r   get_area   s0    




r=   c             C   s   t |d |d | d | d }|d }|d }|d }	|d }
t|d|
|
|}t||
|	|
|}t||	||
|}t|d|	|	|
}t||	||	|
}t|d|||	}|r|r||||||dS ||||| | d	S |||d
S )zComputes the area inside the lines of the second component mass as a
    function of the first component mass for the two extreme values
    of mchirp: mchirp +/- mchirp_uncertainty, for each region of the source
    classifying diagram.
    r+   r,   r   r   r   r   r6   )BNSGNSNSBHGGBHGBBH)r>   r@   rC   zMass Gap)r>   r@   rC   )r/   r=   )trig_mc_detr    r!   r-   r#   r$   r:   Zm2_minZm1_maxr   r   ZabbhZabhgZansbhZaggZagnsZabnsr   r   r   
calc_areas   s8    
rE   c                sl  |d }|d }|d }| | |d  d}|d | }|t |d  ||d   }	t||	|d	 }
|d
 }|d }|d d }|d d }|d |d|
d   kr|r|rddddddd}qddddd}ndddd}n|d |d|
d   k r6|r(|rddddddd}nddddd}ndddd}n2t||||
|| t   fdd D }|S )a  Computes the different probabilities that a candidate event belongs to
       each CBC source category taking as arguments the chirp mass, the
       coincident SNR and the effective distance, and estimating the
       chirp mass uncertainty, the luminosity distance (and its uncertainty)
       and the redshift (and its uncertainty). Probability is estimated to be
       directly proportional to the area of the corresponding CBC region.
    r    r!   r"   r   )r+   r,   r   r   r   r%   r#   r$   r   gu溆a?r   r+   r   g        g      ?)r>   r?   r@   rA   rB   rC   )r>   r@   rC   zMass Gap)r>   r@   rC   c                s   i | ]} |  |qS r   r   ).0key)areas
total_arear   r   
<dictcomp>)  s    z&calc_probabilities.<locals>.<dictcomp>)mathexpr.   rE   sumvalues)ZmchirpZsnrZeff_distanceZsrc_argsr    r!   ZcoeffrD   Zdist_estimationZdist_std_estimationr-   r#   r$   r;   r<   Zprobabilitiesr   )rH   rI   r   calc_probabilities   sD    



rO   )__doc__rK   numpyr9   Zpycbc.conversionsr   r0   Zscipy.integrater   Zpycbc.cosmologyr   Zastropy.cosmologyr   r   r)   r.   r/   r5   r=   rE   rO   r   r   r   r   <module>	   s   0#
32