U
    f»{j  ã                   @   s>   d dl Z d dlZdd„ Zddd„Zejfdd„Zdd
d„ZdS )é    Nc                 C   s*   ddl m} t || ƒ¡tj dtj  S )a.  Compute normalized phase angles.

    Parameters
    ----------
    data : ndarray, shape (n_epochs, n_sources, n_times)
        The data to compute the phase angles for.

    Returns
    -------
    phase_angles : ndarray, shape (n_epochs, n_sources, n_times)
        The normalized phase angles.
    r   )Úhilberté   )Zscipy.signalr   ÚnpZangleÚpi)Údatar   © r   úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/preprocessing/ctps_.pyÚ_compute_normalized_phase	   s    r	   Tc           	      C   s¤   | j dkstd| j  ƒ‚|r&t| ƒ}n| }t |d ¡}t |d ¡}tt |dddg¡ƒD ]4\}}t|ƒ\}}|||dd…f< |||dd…f< q\|||rž|ndfS )aÕ  Compute cross-trial-phase-statistics [1].

    Note. It is assumed that the sources are already
    appropriately filtered

    Parameters
    ----------
    data: ndarray, shape (n_epochs, n_channels, n_times)
        Any kind of data of dimensions trials, traces, features.
    is_raw : bool
        If True it is assumed that data haven't been transformed to Hilbert
        space and phase angles haven't been normalized. Defaults to True.

    Returns
    -------
    ks_dynamics : ndarray, shape (n_sources, n_times)
        The kuiper statistics.
    pk_dynamics : ndarray, shape (n_sources, n_times)
        The normalized kuiper index for ICA sources and
        time slices.
    phase_angles : ndarray, shape (n_epochs, n_sources, n_times) | None
        The phase values for epochs, sources and time slices. If ``is_raw``
        is False, None is returned.

    References
    ----------
    [1] Dammers, J., Schiek, M., Boers, F., Silex, C., Zvyagintsev,
        M., Pietrzyk, U., Mathiak, K., 2008. Integration of amplitude
        and phase statistics for complete artifact removal in independent
        components of neuromagnetic recordings. Biomedical
        Engineering, IEEE Transactions on 55 (10), 2353-2362.
    é   z$Data must have 3 dimensions, not %i.r   é   r   N)ÚndimÚ
ValueErrorr	   r   Z
zeros_likeÚ	enumerateZ	transposeÚkuiper)	r   Zis_rawZphase_anglesZks_dynamicsZpk_dynamicsÚiiÚsourceÚksÚpkr   r   r   Úctps   s    !

r   c                 C   sÄ   t j| dd� |¡} | j}t|ƒ}|d }t j||d�d t|ƒ }t j||d�t|ƒ }|dkr†|dd…t jf }|dd…t jf }||  jdd�}| | jdd�}|}	|| }
|
t	|
|	|d�fS )aw  Kuiper's test of uniform distribution.

    Parameters
    ----------
    data : ndarray, shape (n_sources,) | (n_sources, n_times)
           Empirical distribution.
    dtype : str | obj
        The data type to be used.

    Returns
    -------
    ks : ndarray
        Kuiper's statistic.
    pk : ndarray
        Normalized probability of Kuiper's statistic [0, 1].
    r   )Úaxis)Údtypeç      ð?r   N)
r   ÚsortZastypeÚshapeÚlenÚarangeÚfloatZnewaxisÚmaxÚ_prob_kuiper)r   r   r   Zn_dimZn_trialsZj1Új2Zd1Zd2Ún_effÚdr   r   r   r   P   s    r   Úf8c                 C   sÈ   ddl m} t | ¡}d}t |¡}|d d|  |  }|d }t |¡d d }	|	 |¡ ||¡}	d	|	 | d
 }
d|	 | }d|
 }|||dd� d|  }t 	|dk d|¡}t 	|d
kd
|¡}|S )a•  Test for statistical significance against uniform distribution.

    Parameters
    ----------
    d : float
        The kuiper distance value.
    n_eff : int
        The effective number of elements.
    dtype : str | obj
        The data type to be used. Defaults to double precision floats.

    Returns
    -------
    pk_norm : float
        The normalized Kuiper value such that 0 < ``pk_norm`` < 1.

    References
    ----------
    [1] Stephens MA 1970. Journal of the Royal Statistical Society, ser. B,
    vol 32, pp 115-122.

    [2] Kuiper NH 1962. Proceedings of the Koninklijke Nederlands Akademie
    van Wetenschappen, ser Vol 63 pp 38-47
    r   )Ú	logsumexpéd   g×£p=
×Ã?g¸…ëQ¸Î?g       @r   r   g      @r   g       À)Úbr   gš™™™™™Ù?g        )
Zscipy.specialr#   r   ÚsizeÚmathÚsqrtr   ÚrepeatZreshapeÚwhere)r!   r    r   r#   Zn_time_slicesZn_pointsÚenZk_lambdaÚl2r   ZfactÚar%   Zpk_normr   r   r   r   w   s    

r   )T)r"   )r'   Únumpyr   r	   r   Zfloat64r   r   r   r   r   r   Ú<module>   s
   
6'