U
    f»{jË#  ã                   @   sZ  d Z ddlm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lmZmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlm Z m!Z! e "ej#dd�d¡Z#e "e#d¡Z$e "e#d¡Z%e "e#d¡Z&e "e 'e(¡ddddd¡Z)e "e)d¡Z*ej+de ,¡ gd�dd„ ƒZ-dd„ Z.dd„ Z/d d!„ Z0d"d#„ Z1d$d%„ Z2dS )&zjTest the current source density and related functions.

For each supported file format, implement a test.
é    N)Úassert_allclose)Úloadmat)Úlinalg)Úmake_dig_montage)Úcreate_infoÚEvokedArrayÚ
pick_typesÚEpochsÚfind_eventsÚread_epochs)Úread_raw_fifÚRawArray)ÚFIFF)Úobject_diff)Útesting)Úcompute_current_source_densityÚcompute_bridged_electrodesF)ÚdownloadZpreprocessingztest_eeg.matztest_eeg_pos.matztest_eeg_csd.matz..ÚioZtestsÚdataztest_raw.fifÚfunction)ÚscopeÚparamsc                  C   s   t tƒd } t tƒd d }t tƒd }t d¡}d}t |d¡}|dd…dgf |dd…d	gf  |dd…d	gf< |dd…dgf< |dd…d	gf  d
9  < tjj|d
d�}t	||d
 dd� dd„ t
t|ƒƒD ƒ}tt||ƒƒ}t|dd�}	t||dd�}
t| |
d
d�}| |	¡ |||fS )zGet the MATLAB EEG data.r   Úcoordsgü©ñÒMbP?Úcsd)r   r   r   gms4“™Âµ?é   é   Nr   éÿÿÿÿ)Zaxisg{®Gáz„?)Zrtolc                 S   s   g | ]}t |ƒ‘qS © )Ústr)Ú.0Úiir   r   ú_/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/preprocessing/tests/test_csd.pyÚ
<listcomp>8   s     z%evoked_csd_sphere.<locals>.<listcomp>Úhead)Zch_posZcoord_frameÚeeg)Úch_namesÚsfreqZch_types)r   ÚinfoÚtmin)r   Ú	eeg_fnameÚcoords_fnameÚ	csd_fnameÚnpÚarrayZrollaxisr   Únormr   ÚrangeÚlenÚdictÚzipr   r   r   Zset_montage)r   r   r   Úspherer'   ÚposÚdistsr&   Z
dig_ch_posZmontager(   Úevokedr   r   r"   Úevoked_csd_sphere'   s"    
B
r8   c              	   C   s†   | \}}}t ||d�}tt |¡ddd� |j|d d  }t||dd� tjtdd	�� t ||d� W 5 Q R X t| ¡ d
dd� dS )z+Test replication of the CSD MATLAB toolbox.©r4   gwJëÿ\?gñhãˆµøä>)Zatolr   é   gH¯¼šò×Š>z,CSD already applied, should not be reapplied©Úmatchgkšwœ¢#™?gü©ñÒMb`?N)	r   r   r   r/   r   ÚpytestÚraisesÚ
ValueErrorÚsum)r8   r7   r   r4   Z
evoked_csdZevoked_csd_datar   r   r"   Útest_csd_matlabC   s    
rA   c           	   	   C   s˜  | \}}}|  ¡ }|jd  |jd ¡ tjtdd�� t|ƒ W 5 Q R X tjtdd�� tdƒ W 5 Q R X |  ¡ }tjtdd��> |jd D ] }t	 
d	d	d	g¡|d
 dd…< qŽt||d� W 5 Q R X tjtdd��. t	j|jd d d
 dd…< t||d� W 5 Q R X tjtdd��0 |  ¡ }| dd„ |jD ƒ¡ t||d� W 5 Q R X tjtdd�� t|d|d� W 5 Q R X tjtdd�� t|d|d� W 5 Q R X tjtdd�� t|d|d� W 5 Q R X tjtdd�� t|d|d� W 5 Q R X tjtdd�� t|d|d� W 5 Q R X tjtdd�� t|d	|d� W 5 Q R X tjtdd�� t|dd� W 5 Q R X tjtdd�� t|dd� W 5 Q R X t t¡� t|d|d � W 5 Q R X t|j|jƒ}t|d	d	d!ggd	|jd" |jd	  dd#d#d$�}| ¡  t|ƒd!k�st‚t| ¡ d	 |jƒ tjtd%d�� t|ƒ W 5 Q R X | ¡  t|ƒ}t	 | ¡ |j¡�r`t‚t|ƒ}t| ¡ |jƒ t|ƒ}t| ¡ d	 |jƒ dS )&zTest degenerate conditions.Úbadsé   zEither drop.*or interpolater;   zmust be an instance ofNzZero or infinite positionÚchsr   Úlocr9   zNo EEG channels found.c                 S   s   i | ]
}|d “qS )Zecogr   )r    Zch_namer   r   r"   Ú
<dictcomp>n   s      z'test_csd_degenerate.<locals>.<dictcomp>Úlambda2Ú0)rG   r4   zlambda2 must be between 0 and 1r:   zstiffness must be)Z	stiffnessr4   zstiffness must be non-negativeéþÿÿÿzn_legendre_terms must begš™™™™™¹?)Zn_legendre_termsr4   z'n_legendre_terms must be greater than 0zsphere must beçš™™™™™¹¿z$sphere radius must be greater than 0)rJ   ç        rK   g      ð¿)Úcopyr4   r   r   F)r)   ZtmaxZbaselineÚpreloadZprojzComputing CSD requires.*preload)rL   r(   Úappendr&   r=   r>   r?   r   Ú	TypeErrorr-   r.   ÚinfZset_channel_typesr   r   r	   ÚtimesZdrop_badr1   ÚAssertionErrorr   Úget_dataÚRuntimeErrorÚ	load_dataZallclose)	r8   r7   r   r4   Zwarn_evokedZfail_evokedÚchÚrawÚepochsr   r   r"   Útest_csd_degenerateW   s‚    

ÿÿÿ 
    þrY   c               	   C   s(  t tƒ ¡ } g | jd< t| jddd�}d| ks2t‚|  d¡}t|ƒdksLt‚t| ƒ}d|ks`t‚| d¡}||k 	¡ rzt‚|jd t
jksŽt‚|j ¡ � d	|jd< W 5 Q R X |D ]X}|jd
 | }|d t
jksÖt‚|d t
jksèt‚|jt
jt
jd� | j| |j|< q²t| j|jƒdk�s$t‚dS )zTest applying CSD to FIF data.rB   FT©Úmegr%   r   r%   é<   Zcustom_ref_appliedr   rD   Ú	coil_typeÚunit)r]   r^   Ú N)r   Ú	raw_fnamerU   r(   r   rR   rS   r1   r   Úanyr   ZFIFFV_MNE_CUSTOM_REF_CSDZ_unlockZFIFFV_COIL_EEG_CSDZFIFF_UNIT_V_M2ÚupdateZFIFFV_COIL_EEGZFIFF_UNIT_VÚ_datar   )rW   ÚpicksZorig_eegZraw_csdZnew_eegÚpickrV   r   r   r"   Útest_csd_fif¤   s(    


rf   c                 C   sp   t tƒ}|jddd� ¡  t|ƒ}t||tdd�dd�}t|ƒ}| d }| |¡ t	|dd�}t
|j|jƒ dS )	z/Test making epochs, saving to disk and loading.T)r%   Zstimg-Cëâ6?)r%   )ZrejectrM   ztest_csd_epo.fif)rM   N)r   r`   r   rU   r
   r	   r2   r   Úsaver   r   rc   )Ztmp_pathrW   ÚeventsrX   Z	epo_fnameZepochs2r   r   r"   Útest_csd_epochs¾   s    
ri   c               	   C   s6  t tƒ ¡ } | jdd� tjtdd�� t| ƒ\}}W 5 Q R X d}t tƒ ¡ } | j 	d¡}| j 	d¡}| j
| | j
|< t| |d�\}}|||fgks˜t‚t| jd	dd
�}|j| jj|| jd   |j|jfksÒt‚t|ƒ}t |dd…| 	|¡| 	|¡f dk¡�st‚t t |d t t|ƒd¡ ¡¡�s2t‚dS )z"Test computing bridged electrodes.T)r[   zNo EEG channels foundr;   rC   zEEG 001zEEG 002)Úepoch_durationFrZ   r'   Nr   r   )r   r`   rU   r   r=   r>   rT   r   r&   Úindexrc   rR   r(   ÚshaperQ   ÚsizeÚlistr-   ÚallÚisnanZtril_indicesr1   )rW   Zbridged_idxZ	ed_matrixrj   Zidx0Zidx1rd   r   r   r"   Útest_compute_bridged_electrodesË   s0     ÿ
 ÿÿ.rq   )3Ú__doc__Úos.pathÚpathÚopÚnumpyr-   r=   Znumpy.testingr   Zscipy.ior   Zscipyr   Zmne.channelsr   Zmner   r   r   r	   r
   r   Zmne.ior   r   Zmne.io.constantsr   Z	mne.utilsr   Zmne.datasetsr   Zmne.preprocessingr   r   ÚjoinZ	data_pathr*   r+   r,   ÚdirnameÚ__file__Zio_pathr`   ZfixtureZ_pytest_paramr8   rA   rY   rf   ri   rq   r   r   r   r"   Ú<module>   s4    
M