U
    f»{j  ã                   @   sn   d dl mZ d dlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ dd	„ Zeddd„ƒZdd„ ZdS )é    )ÚpartialNé   )Ú_COLAÚ_Storer)Ú_picks_to_idx)Ú_normalize_vectors)ÚloggerÚverbosec                 C   sx   t j | ¡\}}t  |jf¡}|t  t¡j|d  k}t j||d�}d||  ||< ||9 }t  	|j
| |¡}|||fS )z2Use a covariance matrix to compute the SVD faster.éÿÿÿÿ)Úoutg      ð?)ÚnpZlinalgZeighZonesÚsizeZfinfoÚfloatÚepsÚsqrtÚdotÚT)ÚcovÚdataÚsÚuZnormÚmaskÚv© r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/preprocessing/otp.pyÚ_svd_cov   s    r   ç      $@c              	   C   sš  t  d¡ t| j|dd�}tƒ tƒ  }}t|ƒD ]2\}}| j| | jd krX| |¡ q0| |¡ q0t |t	¡}t |t	¡}t	t
t|ƒ| jd  ƒƒ}|t|ƒd k rÊtd|| jd  |t|ƒd f ƒ‚|d }	|  ¡ jd	d
�}
ttt||d�t|
j|d�t| jƒ||	| jd ƒ}ttdt| jƒ|ƒƒt| jƒg }t|dd… |dd… ƒD ]D\}}t  dt| j||d g ƒ ¡ | | |||…f d ¡ �qP|
S )aá  Denoise MEG channels using leave-one-out temporal projection.

    Parameters
    ----------
    raw : instance of Raw
        Raw data to denoise.
    duration : float | str
        The window duration (in seconds; default 10.) to use. Can also
        be "min" to use as short a window as possible.
    %(picks_all_data)s
    %(verbose)s

    Returns
    -------
    raw_clean : instance of Raw
        The cleaned data.

    Notes
    -----
    This algorithm is computationally expensive, and can be several times
    slower than realtime for conventional M/EEG datasets. It uses a
    leave-one-out procedure with parallel temporal projection to remove
    individual sensor noise under the assumption that sampled fields
    (e.g., MEG and EEG) are oversampled by the sensor array
    :footcite:`LarsonTaulu2018`.

    OTP can improve sensor noise levels (especially under visual
    inspection) and repair some bad channels. This noise reduction is known
    to interact with :func:`tSSS <mne.preprocessing.maxwell_filter>` such
    that increasing the ``st_correlation`` value will likely be necessary.

    Channels marked as bad will not be used to reconstruct good channels,
    but good channels will be used to process the bad channels. Depending
    on the type of noise present in the bad channels, this might make
    them usable again.

    Use of this algorithm is covered by a provisional patent.

    .. versionadded:: 0.16

    References
    ----------
    .. footbibliography::
    z9Processing MEG data using oversampled temporal projectionr   )ÚexcludeZbadsZsfreqé   zTduration (%s) yielded %s samples, which is fewer than the number of channels -1 (%s)r   F)r	   )Ú
picks_goodÚ	picks_bad)Úpicksr   Nr
   z!    Denoising % 8.2f - % 8.2f sec)r   Úinfor   ÚlistÚ	enumerateZch_namesÚappendr   ÚarrayÚintÚroundr   ÚlenÚ
ValueErrorÚcopyZ	load_datar   r   Ú_otpr   Ú_dataÚtimesÚrangeÚzipÚtupleÚfeed)ÚrawÚdurationr!   r	   r   r    ÚiiÚpiZ	n_samplesZ	n_overlapZraw_otpZotpZ	read_limsÚstartÚstopr   r   r   Úoversampled_temporal_projection   sB    /

ÿþ   ý""ÿr9   c                 C   s4  t  | ¡ ¡ stdƒ‚t j| ddd�}| |8 } | | }t|ƒ}t  ||j¡}t|ƒdkrft	||ƒd }t
|ƒD ]‚\}}	tt|ƒƒtt|d t|ƒƒƒ }
t	|t  |
|
¡ ||
 ƒd }t  t  || |j¡|¡}||| 9 }|||	 7 }|| |	< qn|D ]6}	t  t  | |	 |j¡|¡| |	< | |	  ||	 7  < qö| gS )z#Perform OTP on one segment of data.z2non-finite data (inf or nan) found in raw instancer
   T)ZaxisZkeepdimsr   r   r   )r   ÚisfiniteÚallÚRuntimeErrorZmeanr   r   r   r)   r   r$   r#   r/   Zix_)r   r   r    Z
data_meansZ	data_goodZnormsr   Z
full_basisÚmiÚpickÚidxZt_basisÚxr   r   r   r,   m   s(    "
r,   )r   NN)Ú	functoolsr   Únumpyr   Z_olar   r   Zio.pickr   Zsurfacer   Úutilsr   r	   r   r9   r,   r   r   r   r   Ú<module>   s     ÿM