U
    f»{jÎ  ã                   @   sŒ   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
mZmZmZmZ edd	d
„ƒZdd„ Zeddd„ƒZddd„Zddd„ZdS )é    Né   )Úcompute_whitener)Ú	pick_info)Úapply_forward)ÚloggerÚverboseÚcheck_random_stateÚ_check_preloadÚ_validate_typeé   Tc	                 C   s|   t | |||d�}	|dkr|	S |tjk rZt|	|||ƒ}
|	 j|
jt |¡ 7  _t |¡|	_| 	dd¡rx|	 
|d ¡ ¡  |	S )aW  Generate noisy evoked data.

    .. note:: No projections from ``info`` will be present in the
              output ``evoked``. You can use e.g.
              :func:`evoked.add_proj <mne.Evoked.add_proj>` or
              :func:`evoked.set_eeg_reference <mne.Evoked.set_eeg_reference>`
              to add them afterward as necessary.

    Parameters
    ----------
    fwd : instance of Forward
        A forward solution.
    stc : SourceEstimate object
        The source time courses.
    %(info_not_none)s Used to generate the evoked.
    cov : Covariance object | None
        The noise covariance. If None, no noise is added.
    nave : int
        Number of averaged epochs (defaults to 30).

        .. versionadded:: 0.15.0
    iir_filter : None | array
        IIR filter coefficients (denominator) e.g. [1, -1, 0.2].
    %(random_state)s
    %(use_cps)s

        .. versionadded:: 0.15
    %(verbose)s

    Returns
    -------
    evoked : Evoked object
        The simulated evoked data.

    See Also
    --------
    simulate_raw
    simulate_stc
    simulate_sparse_stc

    Notes
    -----
    To make the equivalence between snr and nave, when the snr is given
    instead of nave::

        nave = (1 / 10 ** ((actual_snr - snr)) / 20) ** 2

    where actual_snr is the snr to the generated noise before scaling.

    .. versionadded:: 0.10.0
    )Úuse_cpsNZprojs)r   ÚnpÚinfÚ_simulate_noise_evokedÚdataÚmathÚsqrtZint64ÚnaveÚgetZadd_projZ
apply_proj)ZfwdZstcÚinfoÚcovr   Ú
iir_filterÚrandom_stater   r   ÚevokedÚnoise© r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/simulation/evoked.pyÚsimulate_evoked   s    6
r   c                 C   s(   |   ¡ }d|jd d …< t||||dd�S )Nr   F)Úallow_subselection)Úcopyr   Ú
_add_noise)r   r   r   r   r   r   r   r   r   T   s
    
ÿr   c                 C   s   t | |||ƒS )aï  Create noise as a multivariate Gaussian.

    The spatial covariance of the noise is given from the cov matrix.

    Parameters
    ----------
    inst : instance of Evoked, Epochs, or Raw
        Instance to which to add noise.
    cov : instance of Covariance
        The noise covariance.
    iir_filter : None | array-like
        IIR filter coefficients (denominator).
    %(random_state)s
    %(verbose)s

    Returns
    -------
    inst : instance of Evoked, Epochs, or Raw
        The instance, modified to have additional noise.

    Notes
    -----
    Only channels in both ``inst.info['ch_names']`` and
    ``cov['names']`` will have noise added to them.

    This function operates inplace on ``inst``.

    .. versionadded:: 0.18.0
    )r    )Úinstr   r   r   r   r   r   r   Ú	add_noise[   s    !r"   c                 C   s\  ddl m} ddlm} ddlm} ddlm} t||dƒ t| |||fddƒ t	| d	ƒ | j
}	|	jd
ksnt‚|	jdkr‚|	tj }	| j}
|
 ¡  tdƒ }}|�r"tt|
d ƒt|d ƒ@ ƒ}t t |
d |¡¡d }t dt|ƒt|
d ƒt|d ƒf ¡ t| j|ƒ}
|
 ¡  t |
d ¡}|	D ]0}||  t|
||||jd |d�d 7  < �q&| S )z.Add noise, possibly with channel subselection.r   )Ú
Covariance)ÚBaseRaw)Ú
BaseEpochs)ÚEvokedr   r!   zRaw, Epochs, or EvokedzAdding noise)r   é   NZch_namesÚnamesr   z3Adding noise to %d/%d channels (%d channels in cov)ZchsZnchané   )Úpicks)r   r#   Úior$   Zepochsr%   r   r&   r
   r	   Ú_dataÚndimÚAssertionErrorr   Znewaxisr   Z_check_consistencyÚsliceÚlistÚsetÚwhereZin1dr   Úlenr   ZarangeÚ_generate_noiseÚshape)r!   r   r   r   r   r#   r$   r%   r&   r   r   r*   Z	gen_picksZuse_chsÚepochr   r   r   r       sD     ÿ


ÿ ÿÿr    c                 C   sš   ddl m} t|ƒ}t|| dd|dd�\}	}	}
t |
| |
jd |f¡¡}|dk	rŽ|dkrtt t	|
ƒt	|ƒd f¡}|dg||d|d	�\}}nd}||fS )
z;Create spatially colored and temporally IIR-filtered noise.r   )ÚlfilterTF)ZpcaZreturn_colorerr*   r   r)   Néÿÿÿÿ)ZaxisÚzi)
Zscipy.signalr7   r   r   r   ÚdotZstandard_normalr5   Zzerosr3   )r   r   r   r   Z	n_samplesr9   r*   r7   ÚrngÚ_Zcolorerr   Úzfr   r   r   r4       s    
 ÿr4   )Nr   NNTN)NNN)T)NN)r   Únumpyr   r   r   Zio.pickr   Úforwardr   Úutilsr   r   r   r	   r
   r   r   r"   r    r4   r   r   r   r   Ú<module>   s(         ÿB  ÿ#
!  ÿ