U
    f»{jh  ã                   @   sþ   d dl mZ d dl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mZmZ d dlmZ e
jdd�Zd	Ze ed
¡Ze ede¡Ze ed¡Ze ed¡Zeƒ ejddd„ƒƒZejdd„ ƒZdd„ Zedƒe
jdd„ ƒƒZe
jdd„ ƒZ dS )é    N)Úassert_allclose)Útesting)Úapply_trans)Úrequires_nibabelÚrequires_versionÚuse_log_level)Ú_fake_clickF)ÚdownloadÚsampleZsubjectsZMEGzsample_audvis_trunc_raw.fifzsample_audvis_trunc-trans.fifé   é   c              
   C   s$  ddl }| t ttdd¡¡}t t ttdd¡¡d }tt	j
 |j ¡ ¡|ƒ}t	 |j¡ t¡d \}}}||d |f|d	 |d
 |f|d |d |d f|d |d |d fg}t	 |j¡d }	t	j d¡ t	j |j¡ t	j¡d }
|D ]^}t	 |¡ t¡\}}}d|
t||  ||  d ƒt||  ||  d ƒt||  ||  d ƒf< qî|D ]¤\}}}t	j
 |	t	 |||f¡ ¡dk �s‚t‚dt	j
jt	 t	jt| |d ƒgd Ž ¡dd� |
t|| || d ƒt|| || d ƒt|| || d ƒf< �qR| |
|j¡}t|j ¡ t	 |¡ƒ}||fS )z.Make somewhat realistic CT data with contacts.r   NÚmriz	brain.mgzZbemzouter_skull.surfr   é   é
   é   é   é   é   é   éc   éd   éè  é2   é   )Zaxis)ÚnibabelÚloadÚopÚjoinÚsubjects_dirÚsubjectÚmneZread_surfacer   ÚnpZlinalgÚinvÚheaderZget_vox2ras_tkrÚarrayÚshapeZastypeÚintÚrandomÚseedÚfloat32ÚroundÚsliceZnormÚAssertionErrorZmeshgridÚrangeÚMGHImageZaffine)Z
skull_sizeZcontact_sizeÚnibÚbrainZvertsÚxÚyÚzÚcoordsÚcenterZct_dataZvertÚct© r7   ú]/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/gui/tests/test_ieeg_locate.pyÚ_fake_CT_coords   sN    ÿÿÿ ÿþþ&ÿÿýþr9   c              	   #   sH   t ƒ ‰ ‡ fdd„}|V  ˆ D ]&}z| ¡  W q tk
r@   Y qX qd S )Nc                     s   ˆ   tjj| |Ž¡ ˆ d S )Néÿÿÿÿ)Úappendr    ÚguiZlocate_ieeg)ÚargsÚkwargs©Zguisr7   r8   ÚfunE   s    z_locate_ieeg.<locals>.fun)ÚlistÚcloseÚ	Exception)Zrenderer_interactive_pyvistaqtr@   r<   r7   r?   r8   Ú_locate_ieeg?   s    rD   c              	   C   sn   ddl }t g d¡}| tjdtjd�t d¡¡}tj 	dd¡}t
jtd	d
�� | |||ttƒ W 5 Q R X dS )z7Test the input/output of the intracranial location GUI.r   Nr   )é   rE   rE   )Zdtypeé   Úheadr   z%No channels found in `info` to locate©Úmatch)r   r    Zcreate_infor.   r!   Zzerosr)   ÚeyeZ
transformsZ	TransformÚpytestÚraisesÚ
ValueErrorr   r   )rD   r/   ÚinfoÚ
aligned_ctÚtransr7   r7   r8   Útest_ieeg_elec_locate_ioR   s    ÿÿrQ   Zsphinx_galleryc              	   C   s   t j t¡}|jdd� dddddœ}| t| ¡ ƒ¡ | |¡ | 	d¡ |\}}t  
t¡}tjtd	d
�� | |j||ttd�}W 5 Q R X |d  ¡  t|d d ƒ}	tdt|ƒd�}
tt|d�t|	gƒd�}t |	¡rÞt‚t|ddƒrît‚t j ¡ d||
ƒ t |	¡�st‚|j�st‚dS )z(Test sphinx-gallery scraping of the GUI.T©ZeegúLAMY 1úLAMY 2úLSTN 1úLSTN 2©zEEG 001zEEG 002zEEG 003zEEG 004Nú`pial` surface not foundrH   )r   r   Ú_imagesztemp.pngÚhtml)Zbuilder_nameÚsrc_dir)r<   )Zexample_globalsZimage_path_iteratorÚ_scrapedF)r    ÚioÚread_raw_fifÚraw_pathÚ
pick_typesÚpick_channelsrA   ÚkeysÚrename_channelsÚset_montageÚ
read_transÚfname_transrK   ÚwarnsÚRuntimeWarningrN   r   r   ÚmkdirÚstrÚdictÚiterr   Úisfiler,   Úgetattrr<   Z_GUIScraperr\   )rD   r9   Ztmp_pathÚrawÚch_dictrO   Ú_rP   r<   Z
image_pathZgallery_confZ
block_varsr7   r7   r8   Útest_locate_scraper^   s8     ÿ



 ÿþrr   c              
   C   s  t jjtdd�}|jdd� dddddœ}| t| ¡ ƒ¡ | |¡ | 	d	¡ | 
d
d„ |jD ƒ¡ | d¡ |\}}t  t¡}tjtdd�� | |j||ttdd�}W 5 Q R X tjtdd�� |d |jdd…< W 5 Q R X | |d ¡ | ¡  |j�s|j�rt‚t|dd… dƒD ]ª\}}	t|j|	ƒ}
t dƒ�2 t!|j"d |j"d j#d |
dd… ddd� W 5 Q R X t$|	dd… |jdd… dd|› d�d� t$|	d |jd dd|› d�d� | ¡  �qt%|jƒdk�sÚt‚d|_&| |d ¡ | ¡  t$|d |j'd dd� |j( )¡  |j( *¡ d k�s,t‚d|_&| |d d ¡ | ¡  t$|d d |j'd d!d� |j( )¡  |j( *¡ d"k�s†t‚d|_&| +¡  | ,¡  t- .|j'd ¡ /¡ �s¶t‚t- .|jd# d d$ dd%… ¡ 0¡ �ràt‚t- .|jd# d d$ dd%… ¡ /¡ �s
t‚|j1 2d&¡ |j3d'k�s&t‚|j4 2d(¡ |j5d(k�sBt‚t- 6|j7d) d  8¡ j9¡}|j: 2d*¡ t- 6|j7d) d  8¡ j9¡|k �sŒt‚| ;¡  d+|j7k�s¤t‚d,|j7k�s´t‚| <¡  d,|j7k�sÌt‚d-|j7k�sÜt‚| =¡  d-|j7k�sôt‚d.|j7k�st‚t%|jƒdk�st‚dS )/z:Test that the intracranial location GUI displays properly.T)ZpreloadrR   rS   rT   rU   rV   rW   Zaveragec                 S   s   i | ]
}|d “qS )Zseegr7   )Ú.0Únamer7   r7   r8   Ú
<dictcomp>…   s      z1test_ieeg_elec_locate_display.<locals>.<dictcomp>NrX   rH   )r   r   Úverbosez	read-onlyr   r   Údebugr   r:   ÚdataÚrelease)ZxformÚkindgš™™™™™¹?zcoords[z][:2])ÚatolÚerr_msgz][2]gš™™™™™É?)r{   ZOffg{®Gáz„?ZOnZchsÚlocr   éK   g      è?r   r6   iô  r   Z	local_maxZmipZmip_chs)>r    r]   r^   r_   r`   ra   rA   rb   rc   Zset_eeg_referenceZset_channel_typesZch_namesrd   re   rf   rK   rg   rh   rN   r   r   rL   rM   Z_rasZ_set_rasZ_mark_chÚ_linesZ	_lines_2Dr,   Ú	enumerater   Z
_ras_vox_tr   r   Z_figsZaxesr   ÚlenZ	_ch_indexZ_chsZ_snap_buttonZclickÚtextZ_update_ch_selectionZ
_remove_chr!   ÚisnanÚallÚanyZ_alpha_sliderZsetValueZ	_ch_alphaZ_radius_sliderZ_radiusZnansumrY   Z	get_arrayrx   Z_ct_min_sliderZ_toggle_show_brainZ_toggle_show_maxZ_toggle_show_mip)rD   r9   ro   rp   rO   r4   rP   r<   ÚciZcoordZ	coord_voxZct_sum_beforer7   r7   r8   Útest_ieeg_elec_locate_display{   s–     ÿ




 þ

  ÿ
ÿ
ÿ

**$r‡   )r   r   )!Úos.pathÚpathr   Únumpyr!   Znumpy.testingr   rK   r    Zmne.datasetsr   Zmne.transformsr   Z	mne.utilsr   r   r   Zmne.viz.utilsr   Z	data_pathr   r   r   Z
sample_dirr_   rf   Zfixturer9   rD   rQ   Zrequires_testing_datarr   r‡   r7   r7   r7   r8   Ú<module>   s2   #
