U
    f»{j÷6  ã                   @   s(  d dl Zd dlZddlmZ ddlmZmZ ddl	m
Z
mZm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eddddddddddddeddddd�dg dddddddd dddd!dfd"d#„ƒZeddd$ddddddddeddd%ejd�dg ddd&dd'dd ddddd!dfd(d)„ƒZdS )*é    Né   )ÚEpochs)Úcompute_proj_evokedÚcompute_proj_epochs)ÚloggerÚverboseÚwarn)Ú
pick_types)Úmake_eeg_average_ref_projé   )Úfind_ecg_events)Úfind_eog_eventsc                 C   s   || kr| |= dS )ztAux function.

    Use this function when preparing rejection parameters
    instead of directly deleting keys.
    N© )Zdict_Úkeyr   r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/preprocessing/ssp.pyÚ_safe_del_key   s    r   c           *      C   sŽ  |r|  ¡ n|}~| ¡  |r$g }n"t |jd ¡}t dt|ƒ ¡ |r^t|jƒ}| |¡ |dkrj|}| dksvt	‚t d|  ¡ | dkr¬t
||||||||d�\} }!}!nt|||||||d�} | jd	 d
k rötd| › d�ƒ g | f|rðg fnd S t d¡ t |j¡}"|"d  |7  < |dk	�rÆtt|"dddddd�ƒd	k�rNt|dƒ tt|"dddddd�ƒd	k�rvt|dƒ tt|"dddddd�ƒd	k�ržt|dƒ tt|"dddddd�ƒd	k�rÆt|dƒ |dk	�rptt|"dddddd�ƒd	k�røt|dƒ tt|"dddddd�ƒd	k�r t|dƒ tt|"dddddd�ƒd	k�rHt|dƒ tt|"dddddd�ƒd	k�rpt|dƒ t|"d ƒd	k}#t|"dddd|#dd�}$|j||	|$||||ddddd� t|| d||dd|$||dd�}%|%j}&|%jjd	 d
k �rtdƒ g | f|�r|&fnd S |
�r.|% ¡ }'t|'||||d�}(nt|%|||||d �}(|(D ]})| d! |)d"  |)d"< �qF| |(¡ t d#¡ || f|�rˆ|&fnd S )$z5Compute SSP/PCA projections for ECG or EOG artifacts.Úprojsz)Including %d SSP projectors from raw fileN)ÚECGÚEOGzRunning %s SSP computationr   )Úch_nameÚevent_idÚl_freqÚh_freqÚtstartÚqrs_thresholdÚfilter_length)r   r   r   r   r   r   r   r   zNo z events foundr   zComputing projectorÚbadsÚgradF)ÚmegÚeegÚeogÚref_megÚexcludeÚmagTr   r    Úcomps)r   r   r    Úecgr!   r"   g      à?zzero-doubleZfirwin2)	Úpicksr   Ún_jobsÚmethodÚ
iir_paramsZl_trans_bandwidthZh_trans_bandwidthZphaseZ
fir_design)ZbaselineZpreloadr&   ÚrejectÚflatÚprojzNo good epochs found)Ún_gradÚn_magÚn_eegr   )r-   r.   r/   r'   r   ú-ÚdesczDone.)ÚcopyZ	load_dataÚcpÚdeepcopyÚinfor   Úlenr
   ÚappendÚAssertionErrorr   r   Úshaper   r	   r   Úfilterr   Údrop_logÚeventsÚaverager   r   Úextend)*ÚmodeÚrawÚ	raw_eventÚtminÚtmaxr-   r.   r/   r   r   r=   r   r'   r   r*   r+   r   Úavg_refÚno_projr   Z
exg_l_freqZ
exg_h_freqr   r   Úfilter_methodr)   Úreturn_drop_logr2   r   r   r   Zeeg_projr<   Ú_Zmy_infor!   r&   Úepochsr;   ZevokedZev_projsÚpr   r   r   Ú_compute_exg_proj   s   ÿ

  ü  ý

 ÿÿ
 ÿÿ
 ÿÿ
 ÿÿ

 ÿÿ
 ÿÿ
 ÿÿ
 ÿÿ
 ÿ    ý   ÿ ÿ  ÿ

rK   gš™™™™™É¿gš™™™™™Ù?g      ð?g     €A@TZ10sg»½×Ùß|ë=gßÄAfcŠ=g-Cëâ6
?gü©ñÒMb0?)r   r#   r   r    Fiç  é   é#   g        ÚautoZfirZseparatec                 C   sB   t d| |||||||||	|
||||||||||||||||||ƒS )aø  Compute SSP (signal-space projection) vectors for ECG artifacts.

    %(compute_proj_ecg)s

    .. note:: Raw data will be loaded if it hasn't been preloaded already.

    Parameters
    ----------
    raw : mne.io.Raw
        Raw input file.
    raw_event : mne.io.Raw or None
        Raw file to use for event detection (if None, raw is used).
    tmin : float
        Time before event in seconds.
    tmax : float
        Time after event in seconds.
    n_grad : int
        Number of SSP vectors for gradiometers.
    n_mag : int
        Number of SSP vectors for magnetometers.
    n_eeg : int
        Number of SSP vectors for EEG.
    l_freq : float | None
        Filter low cut-off frequency for the data channels in Hz.
    h_freq : float | None
        Filter high cut-off frequency for the data channels in Hz.
    average : bool
        Compute SSP after averaging. Default is True.
    filter_length : str | int | None
        Number of taps to use for filtering.
    %(n_jobs)s
    ch_name : str | None
        Channel to use for ECG detection (Required if no ECG found).
    reject : dict | None
        Epoch rejection configuration (see Epochs).
    flat : dict | None
        Epoch flat configuration (see Epochs).
    bads : list
        List with (additional) bad channels.
    avg_ref : bool
        Add EEG average reference proj.
    no_proj : bool
        Exclude the SSP projectors currently in the fiff file.
    event_id : int
        ID to use for events.
    ecg_l_freq : float
        Low pass frequency applied to the ECG channel for event detection.
    ecg_h_freq : float
        High pass frequency applied to the ECG channel for event detection.
    tstart : float
        Start artifact detection after tstart seconds.
    qrs_threshold : float | str
        Between 0 and 1. qrs detection threshold. Can also be "auto" to
        automatically choose the threshold that generates a reasonable
        number of heartbeats (40-160 beats / min).
    filter_method : str
        Method for filtering ('iir' or 'fir').
    iir_params : dict | None
        Dictionary of parameters to use for IIR filtering.
        See mne.filter.construct_iir_filter for details. If iir_params
        is None and method="iir", 4th order Butterworth will be used.
    copy : bool
        If False, filtering raw data is done in place. Defaults to True.
    return_drop_log : bool
        If True, return the drop log.

        .. versionadded:: 0.15
    meg : str
        Can be 'separate' (default) or 'combined' to compute projectors
        for magnetometers and gradiometers separately or jointly.
        If 'combined', ``n_mag == n_grad`` is required and the number of
        projectors computed for MEG will be ``n_mag``.

        .. versionadded:: 0.18
    %(verbose)s

    Returns
    -------
    proj : list
        Computed SSP projectors.
    ecg_events : ndarray
        Detected ECG events.
    drop_log : list
        The drop log, if requested.

    See Also
    --------
    find_ecg_events
    create_ecg_epochs

    Notes
    -----
    Filtering is applied to the ECG channel while finding events using
    ``ecg_l_freq`` and ``ecg_h_freq``, and then to the ``raw`` instance
    using ``l_freq`` and ``h_freq`` before creation of the epochs used to
    create the projectors.
    r   ©rK   )r@   rA   rB   rC   r-   r.   r/   r   r   r=   r   r'   r   r*   r+   r   rD   rE   r   Z
ecg_l_freqZ
ecg_h_freqr   r   rF   r)   r2   rG   r   r   r   r   r   Úcompute_proj_ecgŽ   s@    l                         ûrP   gš™™™™™É?gü©ñÒMb@?iæ  é
   c                 C   sB   t d| |||||||||	|
|||||||||||d||||||ƒS )aÝ  Compute SSP (signal-space projection) vectors for EOG artifacts.

    %(compute_proj_eog)s

    .. note:: Raw data must be preloaded.

    Parameters
    ----------
    raw : mne.io.Raw
        Raw input file.
    raw_event : mne.io.Raw or None
        Raw file to use for event detection (if None, raw is used).
    tmin : float
        Time before event in seconds.
    tmax : float
        Time after event in seconds.
    n_grad : int
        Number of SSP vectors for gradiometers.
    n_mag : int
        Number of SSP vectors for magnetometers.
    n_eeg : int
        Number of SSP vectors for EEG.
    l_freq : float | None
        Filter low cut-off frequency for the data channels in Hz.
    h_freq : float | None
        Filter high cut-off frequency for the data channels in Hz.
    average : bool
        Compute SSP after averaging. Default is True.
    filter_length : str | int | None
        Number of taps to use for filtering.
    %(n_jobs)s
    reject : dict | None
        Epoch rejection configuration (see Epochs).
    flat : dict | None
        Epoch flat configuration (see Epochs).
    bads : list
        List with (additional) bad channels.
    avg_ref : bool
        Add EEG average reference proj.
    no_proj : bool
        Exclude the SSP projectors currently in the fiff file.
    event_id : int
        ID to use for events.
    eog_l_freq : float
        Low pass frequency applied to the E0G channel for event detection.
    eog_h_freq : float
        High pass frequency applied to the EOG channel for event detection.
    tstart : float
        Start artifact detection after tstart seconds.
    filter_method : str
        Method for filtering ('iir' or 'fir').
    iir_params : dict | None
        Dictionary of parameters to use for IIR filtering.
        See mne.filter.construct_iir_filter for details. If iir_params
        is None and method="iir", 4th order Butterworth will be used.
    ch_name : str | None
        If not None, specify EOG channel name.
    copy : bool
        If False, filtering raw data is done in place. Defaults to True.
    return_drop_log : bool
        If True, return the drop log.

        .. versionadded:: 0.15
    meg : str
        Can be 'separate' (default) or 'combined' to compute projectors
        for magnetometers and gradiometers separately or jointly.
        If 'combined', ``n_mag == n_grad`` is required and the number of
        projectors computed for MEG will be ``n_mag``.

        .. versionadded:: 0.18
    %(verbose)s

    Returns
    -------
    proj: list
        Computed SSP projectors.
    eog_events: ndarray
        Detected EOG events.
    drop_log : list
        The drop log, if requested.

    See Also
    --------
    find_eog_events
    create_eog_epochs

    Notes
    -----
    Filtering is applied to the EOG channel while finding events using
    ``eog_l_freq`` and ``eog_h_freq``, and then to the ``raw`` instance
    using ``l_freq`` and ``h_freq`` before creation of the epochs used to
    create the projectors.
    r   rN   rO   )r@   rA   rB   rC   r-   r.   r/   r   r   r=   r   r'   r*   r+   r   rD   rE   r   Z
eog_l_freqZ
eog_h_freqr   rF   r)   r   r2   rG   r   r   r   r   r   Úcompute_proj_eog  s@    g                         ûrR   )r2   r3   ÚnumpyÚnprI   r   r,   r   r   Úutilsr   r   r   Zio.pickr	   Úior
   r%   r   r    r   r   rK   ÚdictrP   ÚinfrR   r   r   r   r   Ú<module>   sŒ   
p        ÿ          ÷s      ÿ          ø