U
    f»{jå& ã                   @   sp  d Z ddlmZ ddlm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Zdd	lmZmZmZ d
dlmZ d
dlmZmZ d
dlmZmZmZmZ d
dlm Z  d
dl!m"Z"m#Z#m$Z$m%Z%m&Z& d
dl'm(Z( d
dl)m*Z*m+Z+ d
dl,m-Z-m.Z.m/Z/m0Z0m1Z1 d
dl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d
dl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF d
dlGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZU ddlVmWZW ddlGmXZXmYZYmZZZm[Z[m\Z\m]Z] d
dl^m_Z_m`Z`maZa eKZbe(jce(jde(jefZfd½dd„ZgeOd¾d d!„ƒZhd"d#„ ZieKd¿d&dd'œd(d)„ƒZjeKdÀd-d.„ƒZkd/d0„ ZleKdÁd1d2„ƒZmdÂd3d4„ZndÃd6d7„Zod8d9„ Zpd:d;„ Zqd<d=„ ZrdÄd?d@„ZsdAdB„ ZtdÅdCdD„ZudÆdFdG„ZveKdÇdHdI„ƒZwdÈdKdL„ZxdÉdNdO„ZydPdQ„ ZzdRdS„ Z{dTdU„ Z|dÊdWdX„Z}dYdZ„ Z~d[d\„ Zd]d^„ Z€d_d`„ Z�dadb„ Z‚dcdd„ ZƒdËdldm„Z„dÌdndo„Z…dpdq„ Z†eKdÍdvdw„ƒZ‡dxdy„ Zˆdzd{„ Z‰d|d}„ ZŠd~d„ Z‹d€d�„ ZŒd‚dƒ„ Z�eKdÎd†d‡„ƒZŽdÏdˆd‰„Z�eKdÐd�dŽ„ƒZ�eKdÑd•d–„ƒZ‘eKdÒdšddd›dœddd�œdždŸ„ƒZ’dÓd d¡„Z“dÔd£d¤„Z”e •d¥¡Z–d¦gd§ e–dd§…d§f< d¨Z—d©dª„ Z˜d«d¬„ Z™d­d®„ ZšeOdÕd±d²„ƒZ›eƒ G d³d´„ d´ƒƒZœeœdddddµ�Z�eœd¶d¶d¶d·dµ�ZždÖdd¸œd¹dº„ZŸd»d¼„ Z dS )×z+Functions to make 3D plots with M/EEG data.é    )ÚcycleN)ÚIterable)Úpartial)Ú	dataclass)ÚOptionalé   )Ú_check_concat_dipolesÚ_plot_dipole_mri_outlinesÚ_plot_dipole_3dé   )ÚDEFAULTS)Ú_crop_colorbarÚ_get_img_fdata)Ú_read_mri_infoÚ
_check_mriÚ_get_head_surfaceÚ_get_skull_surface)Ú_loc_to_coil_trans)Ú
pick_typesÚchannel_typeÚ	pick_infoÚ_FNIRS_CH_TYPES_SPLITÚ_MEG_CH_TYPES_SPLIT)ÚFIFF)Úread_fiducialsÚcreate_info)Ú_ensure_srcÚ_create_surf_spacingÚ_check_spacingÚSourceSpacesÚread_freesurfer_lut)Úget_meg_helmet_surfÚ_read_mri_surfaceÚ_DistanceQueryÚ_project_onto_surfaceÚ_reorder_ccwÚ_CheckInside)Úapply_transÚrot_to_quatÚcombine_transformsÚ
_get_transÚ_ensure_transÚ	TransformÚrotationÚread_ras_mni_tÚ_print_coord_transÚ_find_transÚtransform_surface_toÚ_frame_to_strÚ_get_transforms_to_coord_frame)Úget_subjects_dirÚloggerÚ_check_subjectÚverboseÚwarnÚhas_nibabelÚcheck_versionÚfill_docÚ_plÚ
get_configÚ_ensure_intÚ_validate_typeÚ_check_optionÚ_to_rgb)Ú_LayeredMesh)Úmne_analyze_colormapÚ_get_color_listÚplt_showÚtight_layoutÚfigure_nobarÚ_check_time_unit)ÚConductorModelÚ_bem_find_surfaceÚ_ensure_bem_surfacesc                    s   | pg } ˆ dk	r"‡ fdd„| D ƒ} dd„ | D ƒ‰ˆrLt  ‡fdd„tD ƒ¡S ˆ dks`ˆ tjk�rt  d¡}| t j¡ | D ]‚}|d tjkrzt j	|d	 d
d… ddd� 
¡ rÎ|d	 ||d	 d dk rÈdnd< qzt j	|d	 ddd… ddd� 
¡ rz|d	 |d
< qzt  |¡ 
¡ �r|S t  g ¡S dS )z+Generate 3x3 array of fiducial coordinates.Nc                    s   g | ]}|d  ˆ kr|‘qS ©Úcoord_frame© ©Ú.0ÚprL   rN   úJ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/viz/_3d.pyÚ
<listcomp>?   s      z$_fiducial_coords.<locals>.<listcomp>c                 S   s$   i | ]}|d  t jkr|d |“qS )ÚkindÚident)r   ZFIFFV_POINT_CARDINALrO   rN   rN   rR   Ú
<dictcomp>@   s    ÿ z$_fiducial_coords.<locals>.<dictcomp>c                    s   g | ]}ˆ | d  ‘qS )ÚrrN   ©rP   Úi)Úpoints_rN   rR   rS   C   s     ©é   r\   rT   rW   r   r   g�íµ ÷Æ°>)Zatolr   )ÚnpÚarrayÚFIDUCIAL_ORDERr   ÚFIFFV_COORD_HEADÚemptyÚfillÚnanÚFIFFV_POINT_HPIÚiscloseÚallÚisfinite)ÚpointsrM   ÚoutrQ   rN   )rM   rZ   rR   Ú_fiducial_coords;   s$    
 ""rj   ÚtracesÚviridisÚzTÚkc	           A   
      s^	  ddl m}	 ddlm}
 ddlm} td|ddgƒ ˆ dkrFtdd	�nˆ }|
||d
d�}|dk	r˜t|ddƒ}|d dd…  	¡ }|dd…df  d9  < t
| ttfƒs¬| g} t| ƒD ]z\}}t |t¡}|jdksà|jd dkrðtd|jf ƒ‚|dk�r&|dd…df  | |d  d |d  7  < || |< q´t dd„ | D ƒ¡}tj| dd�} |	| ƒ\}}}t d|dd…dd…dd…f  dddg¡| ¡d ‰| dddg¡}| dd…dd…f  }d } }‰ˆ dk	�rbtˆ d
ddd�}t|ƒdk�rbdt ‡ fdd„|D ƒt¡ }|dk�r6t | d¡| d¡f¡j‰n,tˆ ƒ}t|dˆ d dd � |d!  d"9  < td# d$ }|dk�r’|dk�r˜|j ddd
d%�d }n
t |¡}|jd&k�r¾td'|jf ƒ‚|d j!}d(d)g}tt"|jˆjƒƒD ]Ð\}\}}||df j#|||d*dd+� ||df j$|d | |dd,g d-� ||df j#|||d*dd+� ||df j$|d | |dd,g d-� |dd,… D ]0}t%dƒD ] }|||f j&|| d.d/� �qŠ�q~�qâtd0ƒD ]2\}}|d|f j$|d1� |d,|f j$d2d3� �q¼|dk	�rütj'‡‡fd4d„t%dƒD ƒdd�} t%dƒD �]Ô}tt%|ƒƒtt%|d dƒƒ }!dd5l(m)}" |"|dd…|!f ˆdd…|!f ƒ}#|dd…|gf ˆdd…|f k }$|# 	¡ }%tj*|%|$ < |dk�r,t +|%¡ '¡ �r,tj,|%dd�}&||&|f }'t +|%|&t -t|&ƒ¡f ¡ | B }(tj.|'|(< ||df j#||'|d6d7dd8� n$||df j/ˆ| d |d6d7dd8� |#})tj*|)|$< t +|)¡ '¡ �rÔtj,|)dd�}&||&|f }*t +|)|&t -t|&ƒ¡f ¡ | B }(tj.|*|(< ||df j#||*|d6d7dd8� n$||df j/ˆ| d |d6d7dd8� �q$t%dƒD ]}||df j$d,dgd9� �q|dk	�	rHt |dd…df t0|dd…dd…f ƒg¡j 1¡ }+t"|j2|+ƒD ]\},}-|,j/|-d.d:dd*d;� �qn�n¶dd<l3m4}. dd=l5m6}/ dd>l7m8}0 |j dtd?d@�dA�\}},ˆdd…tj9f }1tj|1dd,… |1dd… gdd�}2|.|d |dB ƒ}3|/|2||3dC�}4|4 :|dd,… ¡ |, ;|4¡ tddddD�}5tdEdF�}6t"|dGdHd*gƒD �]\}7}8|dd…dd…|5|7 f }9t |t <|d¡f¡}:|,j=ˆdd…df ˆdd…df ˆdd…df |9dd…df |9dd…df |9dd…df f|3||:|8dIœ|6—Ž |dk	�rf|,j=|dJ |dK |dL ||5|7 df ||5|7 df ||5|7 df f||8dMœ|6—Ž �qfˆ d¡};ˆ d¡}<|dk	�rä|,j>|d! j|dN |dOddPœŽ |,j?|jd|dQœŽ t @|;| d¡¡};t A|<| d¡¡}<|<|;  ¡ dR }=|<|; dd…tj9f dR |= |=g \}>}?}@|,j$dSdTdU|>|?|@dV� tB|,ƒ |, CdWdX¡ tD|dY� tE|ƒ |S )Za¹  Plot head positions.

    Parameters
    ----------
    pos : ndarray, shape (n_pos, 10) | list of ndarray
        The head position data. Can also be a list to treat as a
        concatenation of runs.
    mode : str
        Can be 'traces' (default) to show position and quaternion traces,
        or 'field' to show the position as a vector field over time.
    cmap : colormap
        Colormap to use for the trace plot, default is "viridis".
    direction : str
        Can be any combination of "x", "y", or "z" (default: "z") to show
        directional axes in "field" mode.
    show : bool
        Show figure if True. Defaults to True.
    destination : str | array-like, shape (3,) | None
        The destination location for the head, assumed to be in head
        coordinates. See :func:`mne.preprocessing.maxwell_filter` for
        details.

        .. versionadded:: 0.16
    %(info)s If provided, will be used to show the destination position when
        ``destination is None``, and for showing the MEG sensors.

        .. versionadded:: 0.16
    color : color object
        The color to use for lines in ``mode == 'traces'`` and quiver
        arrows in ``mode == 'field'``.

        .. versionadded:: 0.16
    axes : array-like, shape (3, 2)
        The matplotlib axes to use. Only used for ``mode == 'traces'``.

        .. versionadded:: 0.16

    Returns
    -------
    fig : instance of matplotlib.figure.Figure
        The figure.
    r   )Úhead_pos_to_trans_rot_t)Ú_check_destinationr   NÚmoderk   Úfield)Ú
dev_head_tT)Z
head_frameÚheadÚmegÚtransr\   éè  r   é
   zFpos (or each entry in pos if a list) must be dimension (N, 10), got %s)éÿÿÿÿr   ©r   r   c                 S   s   g | ]}t |ƒ‘qS rN   ©Úlen)rP   ÚpprN   rN   rR   rS   ˜   s     z'plot_head_positions.<locals>.<listcomp>©Úaxisz
ijk,ik->ijé   FrN   )ru   Úref_megÚexcludec                    s$   g | ]}ˆ d  | d dd… ‘qS )ÚchsÚlocNr\   rN   )rP   Úpick)ÚinforN   rR   rS   ¥   s   ÿrs   ©ÚcopyÚrrç     @�@ÚcoregÚhelmet_color)Zsharex)r\   r   z#axes must have shape (3, 2), got %sÚxyz)z$q_1$z$q_2$z$q_3$ç      ð?)ÚlwÚzorderry   )ÚylabelÚxlimrW   ©Úcolor)zPosition (mm)zRotation (quat)©ÚtitleúTime (s))Úxlabelc                    sD   g | ]<}ˆd d …|f ˆ |df kˆd d …|f ˆ |df kB ‘qS )Nr   r   rN   ©rP   Úii)ÚlimsÚ	use_transrN   rR   rS   Æ   s   þÿ)Úcdistú-ç      à?©r”   Úlsr�   r�   ©Úylimú:)r”   r¡   r�   r�   ©Ú	Normalize)ÚLine3DCollection©ÚAxes3DÚ3d©Z
projection©Z
subplot_kwéþÿÿÿ©ÚcmapÚnorm©ÚxÚyrm   Útail)Úpivotg      @g      @)r°   r¯   r^   Úlength)r   r\   )r   r\   )r   r\   )r”   r¶   Útrisçš™™™™™¹?)Ú	trianglesr”   ÚalphaZshade)Úsr”   ç       @r²   r³   rm   )r˜   r‘   Zzlabelr’   r£   Úzlimé   é-   )Úfig)FZchpiro   Zpreprocessing.maxwellrp   Úmatplotlib.pyplotÚpyplotr@   Údictr+   rˆ   Ú
isinstanceÚlistÚtupleÚ	enumerater]   r^   ÚfloatÚndimÚshapeÚ
ValueErrorZcumsumÚconcatenateZeinsumZ	transposer   r|   ÚminÚmaxÚTr!   r1   r   ÚsubplotsÚfigureÚzipÚplotÚsetÚrangeÚaxvlineÚanyZscipy.spatial.distancer�   Úinfrg   ÚargminÚarangerc   Úaxhliner(   ÚravelÚaxesÚmatplotlib.colorsr¦   Zmpl_toolkits.mplot3d.art3dr§   Úmpl_toolkits.mplot3dr©   ÚnewaxisÚ	set_arrayZadd_collectionÚrepeatÚquiverÚplot_trisurfÚscatterÚminimumÚmaximumÚ_set_aspect_equalÚ	view_initrF   rE   )AÚposrq   r¯   Ú	directionÚshowÚdestinationr†   r”   rÝ   ro   rp   ÚpltZ	dest_inforš   rQ   Zbordersrv   ZrotÚtZuse_rotZ	use_quatsÚsurfÚrrsZ	meg_picksrŒ   rÀ   ÚlabelsZquatZcoordÚbZjjr–   Zpos_badsZoidxr�   ÚdistsÚleftZleft_dists_allÚidxZ
left_distsZbadsZright_dists_allZright_distsÚvalsÚaxÚvalr¦   r§   r©   ÚptsÚsegmentsr°   ÚlcZdir_idxÚkwargsÚdr¶   Zuse_dirr^   ZminsZmaxsÚscaler’   r£   r½   rN   )r†   r›   rœ   rR   Úplot_head_positionsU   s‚   .ÿ
,
*ÿÿ
ÿÿ
ÿ


ÿ
&&&
þþ""ÿÿ
  ÿ  ÿ
ÿÿ
  ÿ  ÿ

ÿ"

.  ÿ  þþ

ûúú



  ÿ*
  ÿ
r   c                 C   s(   z|   d¡ W n tk
r"   Y nX d S )NÚequal)Z
set_aspectÚNotImplementedError)rø   rN   rN   rR   rè   )  s    rè   út = %0.0f msé   Úterrain)Úinteractionr7   c          $   
      sX  ddl m}
 ‡ fdd„dD ƒ}t|ddƒ t|dƒ}td	|d
dgƒ d}|dkrht ‡ fdd„|D ƒ¡}~ˆ jd |  krŠˆ jd ks˜n td| ƒ‚t 	t 
ˆ j| ¡¡}ddg}ddg}tdd�}t t ddddgd¡t ddddgd¡t ddddgd¡g¡}|
|ddd�}| |¡ t|ƒD �]Ø\}}|d }|d }|d  }|d! }|d"k�rntˆ jd#d$d%�}ntˆ jd$d#d#d&�}‡ fd'd„|D ƒ}t|ƒ}t|ƒ}||k�rd(g}|| }t|ƒ�rÖ|d)t|ƒ g7 }|| }t|ƒ�rú|d*t|ƒ g7 }td+ |¡ƒ‚t |ˆ j||f ¡}|dk�r8t t 
|¡¡}t|ƒ}|| }t||d, |d- |d. d/�} |  ¡  t|| d$d0�}!t d1|!g¡}"t |"d2 ¡  tj!¡}#| j"t #t|ƒ¡|#ddg|dd3� | j"||| |gdd4d3� |dk�r(|j$|||| |||d5� �q(|dk	�r:d6|k�r*|d7ˆ j|  ; }|j%d8d8|d9� |j&d:d;d<� | '¡  | (¡ S )=a:  Plot MEG/EEG fields on head surface and helmet in 3D.

    Parameters
    ----------
    evoked : instance of mne.Evoked
        The evoked object.
    surf_maps : list
        The surface mapping information obtained with make_field_map.
    time : float | None
        The time point at which the field map shall be displayed. If None,
        the average peak latency (across sensor types) is used.
    time_label : str | None
        How to print info about the time instant visualized.
    %(n_jobs)s
    fig : instance of Figure3D | None
        If None (default), a new figure will be created, otherwise it will
        plot into the given figure.

        .. versionadded:: 0.20
    vmax : float | None
        Maximum intensity. Can be None to use the max(abs(data)).

        .. versionadded:: 0.21
    n_contours : int
        The number of contours.

        .. versionadded:: 0.21
    %(interaction_scene)s
        Defaults to ``'terrain'``.

        .. versionadded:: 1.1
    %(verbose)s

    Returns
    -------
    fig : instance of Figure3D
        The figure.
    r   ©Ú_get_rendererc                    s   g | ]}|ˆ kr|‘qS rN   rN   ©rP   rï   ©ÚevokedrN   rR   rS   ^  s      z%plot_evoked_field.<locals>.<listcomp>)ÚeegZgradÚmag©NÚnumericÚvmaxÚ
n_contoursr  Ú	trackballr  Nc                    s   g | ]}ˆ j |d �d ‘qS ))Úch_typer   )Zget_peakr	  r
  rN   rR   rS   e  s     r   ry   z,`time` (%0.3f) must be inside `evoked.times`rŽ   rŸ   ©ç333333ã?r  r  )rŽ   rŽ   rŽ   Zvtk©Úformatç        g     ào@)é   r   )r   r   ©r  r  r  ©éX  r  )ÚbgcolorÚsizerð   ÚdatarT   Úch_namesr  FT)ru   r  )ru   r  r�   c                    s   g | ]}ˆ j | ‘qS rN   ©r   )rP   rn   r
  rN   rR   rS   ‚  s     z&Channels in map and data do not match.z%s not in data file. z%s not in map file.ú r‰   r·   Únn)ÚrendererÚverticesr¹   Únormals©rº   )r   r   r   r   éÿ   )ÚscalarsÚcolormapÚrngÚopacityÚnamerr   )Úsurfacer)  ZcontoursÚvminr  r,  r*  ú%rŠ   ç{®Gáz„?)Zx_windowZy_windowÚtextrx   é<   )ÚazimuthÚ	elevation))Úbackends.rendererr  r?   r>   r@   r]   ÚmeanÚtimesrË   rÙ   ÚabsrC   rÌ   ÚtileÚset_interactionrÇ   r   r†   rÔ   r|   rÅ   ÚRuntimeErrorÚjoinÚdotr  rÎ   rÈ   rB   ÚmaprA   r^   ÚroundÚastypeZuint8Zadd_overlayÚonesZcontourZtext2dÚ
set_camerarì   Úscene)$r  Z	surf_mapsÚtimeÚ
time_labelZn_jobsrÀ   r  r  r  r7   r  ÚtypesÚtime_idxÚalphasÚcolorsr*  Zcolormap_linesr$  rš   Zthis_maprð   Zmap_dataZmap_typeZmap_ch_namesr…   r   Zset_ch_namesZset_map_ch_namesÚmessageÚdiffr  rº   Úmeshr”   r¯   ZctablerN   r
  rR   Úplot_evoked_field2  s¨    +
"
þ





üûû	

  þ


rN  ÚautoÚoriginalFc           D         s¢
  ddl m} t|||ƒ\}}}}td|ddgƒ | dkrDtdddƒn| } t| d	ƒ |d
krh|dkrhtƒ }t|tƒrx|g}t|t	ƒ�r| 
¡ }| ¡ D ]~\}}t|ddt|ƒ› �ƒ t|ddt|ƒ› d�ƒ |dk	r”t|ƒ||< d||   krödks”n tdt|ƒ› d|› d�ƒ‚q”ni }t|ƒ}t|ƒD ]\}}t|dd|› d�ƒ �q*t|tdfd�}t|tƒ�st|dk�stt‚td|dddd
gƒ |d
k�r¤|dk�r dnd}|dk	�røt|ƒ}|j}|dk�rÊ|n|}|dk	�rø||k�røtd|› d|› d�ƒ‚|d
k�rt|dd�}t||ƒ}t| d|kd|kt|ƒdk|
||t|ƒdkd�}|dk�r”d |› d!�}|dk	�r’t|d  }||k�r’t|d"|› d#� ƒ‚|dk	�rÊt|d d  } | |k�rÊt|d"| › d$� ƒ‚|d%k	�rê|dk�rêt|d& ƒ‚|j�r|dk�rt|d ƒ‚|dk	�r\|d' �s4|dk�rZt|d( ƒ‚n&|d)gg fk�rŠt|d* |¡d+  ƒ‚n.t|ƒdk�rŠ|dk�rŠt|d* |¡d+  ƒ‚tddƒ}t|ddƒd }!t| |!|d,�}"t	ƒ }#tt |ƒt d)d-d.d/gƒ@ ƒ}$t|$ƒdk�ròtd0|$› d1�ƒ‚|$�r |$d nd%}$|$d%k	�r¾| !| "|$¡¡ |dk	�rL|d' �rL|$d)k�rLt#|d)ƒ|#d2< nr|$d)k�rZd-n|$}$t|dd�}d3D ]N}%t$ ||d4|%› d1|$› �¡}&t$ %|&¡�s®t&d5|› d6|&› d7�ƒ‚t'|&ƒ|#|%< �qnd8‰‡fd9d:„|D ƒ}'t|'ƒdk�rðtd;|'› �ƒ‚|'�rþ|'d nd%}'|'d%k	�r| !| "|'¡¡ nd<|k�r0td=ƒ‚d>d:„ |D ƒ}(|(D ]@})| !| "|)¡¡ t(|) )d?¡d |||d@�}*|)|*dA< |*|#|)< �qBt|ƒdk�rªtdBt*|ƒ› dC|› �ƒ‚t+dD ‰ |�pº| �pÒt| dddE�jdk}+|+�rÞdFndG},dH}-|dk�s
|$�rt,dIdJ„ |D ƒƒ�r|,}-t- .|,dK ddL¡dt|(ƒd … }.|dk�r^|$d%k�r^t|(ƒdk�r^|�s^|,}/n|.d }/t	|-|-d3�}0t	dMdMd3�}1t|(ƒD ]8\}2}3|.|2d  |0|3< dN|2dO  dPdN|2dO  f|1|3< �q†|$d%k	�rê|$|k�rê| !|$¡ |0d2< |0dQ< | ¡ D ]4\}4}5|5dk	�r|5|0|4< |4ˆk�rò|5dk	�rò|5}/�qòt/‡ fdRdJ„dSD ƒƒ}6||dT|› �dMdUdV�}7|7 0|¡ t1|7|'|||||"|/dW�\}8}8}9dX|k�r°t| ddY�jdk�r°t2|7| |"|!|ƒ\}8}8}:|$�rd2|#k�r|d t3j4k�sÔt‚|dZ  
¡ };t5|"d |;ƒ};|7j6|;d[|1d2 |0d2 d\� |�rt7|7| |"|!ƒ td]|	d^ƒ |	�r^|	dk�rPt8|7| |"ƒ t9|7| |"ƒ t:|7| |"|6ƒ |�rvt;|7||||"|6ƒ |# ¡ D ]f\}}<t|<t	ƒ�s¨t|› dCt<|<ƒ› �ƒ‚t=|<||"d |"d gdd_�}<|7j>|<|1| |0| |dXkd`� �q~|jdk�	rt?|7| |"||||||9daƒ
 |dk	�
r^t@ƒ \}=}1|D �]6}>|>db |>dc  AtB¡ }?|>dd |>dc  AtB¡ }@|"t|d d   }At5|A|?ƒ}?t5|A|@d%de�}@|>df dgk�	rÚ|> Cdhd¡}B|Bdk	�	rÔ|B|1k�	rÔ|1|B ddi… }Ct/djdJ„ |CD ƒƒ}Cndk}Cndk}Ct|?ƒdk�	r$|7jD|?dd…df |?dd…df |?dd…dlf |@dd…df |@dd…df |@dd…dlf |CdmdndGdodpdqddr� �	q$|dk	�
r€tE|7||"t|d   ƒ |7jFdsdsdtdpdu� |7 G¡  |7 H¡ S )va9  Plot head, sensor, and source space alignment in 3D.

    Parameters
    ----------
    %(info)s If None (default), no sensor information will be shown.
    %(trans)s
    %(subject)s Can be omitted if ``src`` is provided.
    %(subjects_dir)s
    surfaces : str | list | dict
        Surfaces to plot. Supported values:

        * scalp: one of 'head', 'outer_skin' (alias for 'head'),
          'head-dense', or 'seghead' (alias for 'head-dense')
        * skull: 'outer_skull', 'inner_skull', 'brain' (alias for
          'inner_skull')
        * brain: one of 'pial', 'white', 'inflated', or 'brain'
          (alias for 'pial').

        Can be dict to specify alpha values for each surface. Use None
        to specify default value. Specified values must be between 0 and 1.
        for example::

            surfaces=dict(brain=0.4, outer_skull=0.6, head=None)

        Defaults to 'auto', which will look for a head surface and plot
        it if found.

        .. note:: For single layer BEMs it is recommended to use ``'brain'``.
    coord_frame : 'auto' | 'head' | 'meg' | 'mri'
        The coordinate frame to use. If ``'auto'`` (default), chooses ``'mri'``
        if ``trans`` was passed, and ``'head'`` otherwise.

        .. versionchanged:: 1.0
           Defaults to ``'auto'``.
    %(meg)s
    %(eeg)s
    %(fwd)s
    dig : bool | 'fiducials'
        If True, plot the digitization points; 'fiducials' to plot fiducial
        points only.
    %(ecog)s
    src : instance of SourceSpaces | None
        If not None, also plot the source space points.
    mri_fiducials : bool | str
        Plot MRI fiducials (default False). If ``True``, look for a file with
        the canonical name (``bem/{subject}-fiducials.fif``). If ``str``,
        it can be ``'estimated'`` to use :func:`mne.coreg.get_mni_fiducials`,
        otherwise it should provide the full path to the fiducials file.

        .. versionadded:: 0.22
           Support for ``'estimated'``.
    bem : list of dict | instance of ConductorModel | None
        Can be either the BEM surfaces (list of dict), a BEM solution or a
        sphere model. If None, we first try loading
        ``'$SUBJECTS_DIR/$SUBJECT/bem/$SUBJECT-$SOURCE.fif'``, and then look
        for ``'$SUBJECT*$SOURCE.fif'`` in the same directory. For
        ``'outer_skin'``, the subjects bem and bem/flash folders are searched.
        Defaults to None.
    %(seeg)s
    %(fnirs)s
        .. versionadded:: 0.20
    show_axes : bool
        If True (default False), coordinate frame axis indicators will be
        shown:

        * head in pink.
        * MRI in gray (if ``trans is not None``).
        * MEG in blue (if MEG sensors are present).

        .. versionadded:: 0.16
    %(dbs)s
    fig : Figure3D | None
        PyVista scene in which to plot the alignment.
        If ``None``, creates a new 600x600 pixel figure with black background.

        .. versionadded:: 0.16
    %(interaction_scene)s

        .. versionadded:: 0.16
        .. versionchanged:: 1.0
           Defaults to ``'terrain'``.
    %(verbose)s

    Returns
    -------
    fig : instance of Figure3D
        The figure.

    See Also
    --------
    mne.viz.plot_bem

    Notes
    -----
    This function serves the purpose of checking the validity of the many
    different steps of source reconstruction:

    - Transform matrix (keywords ``trans``, ``meg`` and ``mri_fiducials``),
    - BEM surfaces (keywords ``bem`` and ``surfaces``),
    - sphere conductor model (keywords ``bem`` and ``surfaces``) and
    - source space (keywords ``surfaces`` and ``src``).

    .. versionadded:: 0.15
    r   r  r  r  r  NrŠ   Úmiscr†   rO  Ústrzsurfaces key r  z	surfaces[ú]r   z] (z) must be between 0 and 1)Zextra_allowrM   rt   ru   Úmriz
subject ("z+") did not match the subject name in src ("z")T©Úraise_errorÚsensorsÚref)ru   r�   r  ÚecogÚseegÚdbsÚfnirszEA head<->mri transformation matrix (trans) is required to plot %s in z) coordinates, `trans=None` is not allowedza z-coordinate forward solutionz-coordinate source spaceFzmri fiducialsZ	is_sphereza BEMÚbrainú, z	 surfacesrL   ÚpialÚwhiteÚinflatedz+Only one brain surface can be plotted, got Ú.Úlh©rc  Úrhrð   z#No brain surface found for subject z, expected z	 to exist)rO  rt   Z
outer_skinz
head-denseÚsegheadc                    s   g | ]}|ˆ kr|‘qS rN   rN   ©rP   r»   )Ú	head_keysrN   rR   rS   ¥  s      z"plot_alignment.<locals>.<listcomp>z0Can only supply one head-like surface name, got Ú	projectedz‰A head surface is required to project EEG, "head", "outer_skin", "head-dense" or "seghead" must be in surfaces or surfaces must be "auto"c                 S   s   g | ]}|d kr|‘qS ))Zouter_skullZinner_skullrN   rg  rN   rN   rR   rS   ²  s      Ú_©Úbemr-  zUnknown surface typeú: r‹   )r[  rZ  rŽ   ç      è?rŸ   c                 s   s   | ]}|d  dkV  qdS )Útyperð   NrN   rg  rN   rN   rR   Ú	<genexpr>Ä  s     z!plot_alignment.<locals>.<genexpr>r¼   é   )rŸ   rŸ   rŸ   çffffffî?çš™™™™™É?g333333ë?re  c                 3   s   | ]}ˆ |› d � V  qdS )Ú_colorNrN   )rP   Úkey)ÚdefaultsrN   rR   rp  Ø  s    )ZlpaZnasionZrpazSensor alignment: )é   rw  )r-  r  r  r'  Úhelmet©ru   Zr0r1  )rÿ   r”   r,  Údig)TFZ	fiducialsr‡   ©r.  r”   r,  Úbackface_cullingÚmr‰   Úinuser#  ©Úmovero  ÚvolÚseg_namer\   c                 s   s   | ]}|d  V  qdS )g      p@NrN   rX   rN   rN   rR   rp    s     )rŽ   rŽ   r  r   Úcylinderçú~j¼t“h?ç      Ð?r  é   ©r²   r³   rm   ÚuÚvÚwr”   rq   rÿ   r,  Úglyph_heightÚglyph_centerÚglyph_resolutionr|  éZ   r  )r4  r5  ZdistanceZ
focalpoint)Ir6  r  Ú_handle_sensor_typesr@   r   r?   rÅ   rÄ   rR  rÃ   rˆ   ÚitemsÚreprrÈ   rË   rÇ   rK   rI   ÚAssertionErrorr   Ú_subjectr4   r0   r   r|   r2   r  r=  r,   r*   r3   ÚsortedrÔ   ÚpopÚindexrJ   ÚopÚisfiler<  r"   r   Úsplitr<   r   r×   r]   ÚlinspacerÆ   r;  Ú_plot_head_surfaceÚ_plot_helmetr   r`   r'   ÚsphereÚ
_plot_axesÚ_plot_hpi_coilsÚ_plot_head_shape_pointsÚ_plot_head_fiducialsÚ_plot_mri_fiducialsro  r1   r.  Ú_plot_sensorsr    rA  ÚboolÚgetÚquiver3dÚ_plot_forwardrC  rì   rD  )Dr†   rv   ÚsubjectÚsubjects_dirZsurfacesrM   ru   r  Úfwdrz  rY  ÚsrcÚmri_fiducialsrl  rZ  r\  Z	show_axesr[  rÀ   r  r7   r  Úwarn_megZ
user_alpharu  rù   Úsir»   Úsrc_subjectÚpicksÚmsgZ	fwd_frameZ	src_frameÚ
head_mri_tÚto_cf_tZsurfsr]  ÚhemiZbrain_fnamert   ZskullsZ
skull_nameÚskullZno_deepZ	max_alphaZhemi_valZalpha_rangeZ
head_alpharI  rJ  rö   r-  rn   r‰  Ú
fid_colorsr$  rj  Ú	head_surfÚsrc_surfÚcenterrð   Z	atlas_idsÚssZsrc_rrZsrc_nnZ	src_transr‚  r”   rN   )rv  rh  rR   Úplot_alignmentÀ  sü   p

ÿ




   
þ


ÿ

ÿ



  ÿÿ
ÿ

ÿÿ

   ÿÿ$"("
ÿ ÿ
      þ    ÿ
ÿ

 ÿ"   ÿþ
  ÿ



        ú

ÿ ÿr»  c                 C   s|  |dkrdg}n|dkrt ƒ }| dk	}| dkr:dddg} n | dkrLddg} n| dkrZt ƒ } |dkrjdg}n|dkrxt ƒ }t| tƒrˆ| g} t|tƒr˜|g}t|tƒr¨|g}td	|| |fƒD ]>\}}t|t tfƒràtd
d„ |D ƒƒs¸t|› dt|ƒ› �ƒ‚q¸t| ƒD ]\}}t	d|› d�|dƒ �q t|ƒD ]\}}t	d|› d�|dƒ �q(t|ƒD ]\}}t	d|› d�|dƒ �qP| |||fS )z)Handle plotting inputs for sensors types.TrP  FNrx  rW  rX  Úpairs)r  ru   r\  c                 s   s   | ]}t |tƒV  qd S ©N)rÄ   rR  )rP   r²   rN   rN   rR   rp  W  s     z'_handle_sensor_types.<locals>.<genexpr>z# must be list or tuple of str, got zmeg[rS  )rx  rW  rX  zeeg[)rP  ri  zfnirs[)Úchannelsr¼  ÚsourcesÚ	detectors)
rÅ   rÄ   rR  rÒ   rÆ   rf   Ú	TypeErrorro  rÇ   r@   )ru   r  r\  r­  rT   ÚvarÚxir²   rN   rN   rR   r�  :  sF    



ÿÿr�  c              
   C   st  ddl m} ttƒ tƒ tƒ d�}tƒ }tƒ }t| d ƒD �]ê}t| |ƒ}	|	|krb||	  d7  < nd||	< ttddƒd�}
|	tkrœ|
jtdd	ƒtd	d
ƒd� |
 	¡ D �]|\}}|	t
d k�r\t| d | d ƒ}| d | g}z||dd�d }W n& tk
�r   ||dd�d }Y nX t|ƒ\}}t||ƒ}t|ƒdk�rp|�rptd| j| › d�ƒ n| d | d | }| d | d }|tjtjtjtjfk�r¶td| j| › d|› d�ƒ‚|tjk�rØ| | j| ¡ tj}t|t|  |ƒ}|	t
d k�r||f|| | j| < q¤||| | j| < q¤q8|�r<td|› d�ƒ t dd dd„ | 	¡ D ƒ¡ ¡ |d |d |d  fS )!z?Transform positions from head/device/mri to a coordinate frame.r   )Ú_create_meg_coils)Úch_posr¿  rÀ  Znchanr   r   r\   )rÅ  é   é	   )r¿  rÀ  )r�   rƒ   r„   Zaccurate)ÚaccÚnormalzMEG sensor z not found.rM   zChannel z has coordinate frame z!, must be "meg", "head" or "mri".z#Got coordinate frame "unknown" for z, assuming "head" coordinates.zChannel types::	r^  c                 S   s   g | ]\}}|› d |› �‘qS )rm  rN   )rP   r  ÚcountrN   rN   rR   rS   �  s     z*_ch_pos_in_coord_frame.<locals>.<listcomp>rÅ  r¿  rÀ  )ÚforwardrÄ  rÃ   rÅ   rÕ   r   Úslicer   Úupdater�  r   r   r<  Ú_sensor_shaper'   r|   r8   r   r   ZFIFFV_COORD_UNKNOWNZFIFFV_COORD_DEVICEr`   ÚFIFFV_COORD_MRIÚappendr2   r5   r†   r=  )r†   r³  r­  r7   rÄ  rƒ   Zunknown_chsZtype_countsrö   r  Ztype_slicesÚ	type_nameZ
type_sliceZ
coil_transZ	this_coilÚcoilÚch_coordr¹   Zch_coord_framerN   rN   rR   Ú_ch_pos_in_coord_framed  sd    

ýÿ
 ÿ
ÿrÔ  c	                 C   sx   |dkrt d d n|}d}	d }
}|dk	rnt||||d�}
t|
||d |d gdd	�}
| j|
||dd
�\}	}|	||
fS )z$Render a head surface in a 3D scene.Nr‹   Z
head_colorFrk  rT  rt   Tr‡   r{  )r   r   r1   r.  )r$  rt   r¨  r©  rl  rM   r³  rº   r”   Úactorr¸  Údst_surfrN   rN   rR   r›  ¡  s$      þ  þ
r›  r…  c           
      C   sp   |d krt d d n|}t||ƒ}|d tjks4t‚t|||d |d gdd�}| j|||dd	�\}}	||	|fS )
Nr‹   rŒ   rM   rT  rt   Tr‡   Fr{  )r   r!   r   rÏ  r’  r1   r.  )
r$  r†   r³  r²  rM   rº   r”   r¸  rÕ  rÖ  rN   rN   rR   rœ  ²  s     
   ÿ  þ
rœ  c                 C   s2  |d dfg}t  |d t  d¡¡s6| |d df¡ t|dd�jd	krZ| |d
 df¡ tƒ }|D ]È}t  |d	 d dd…df d¡ d¡j	\}}}	|d	 d dd…dd…f \}
}}| j
t  |d	 |d	 |	d	 f¡|d dd�\}}| |¡ | j|||	|
||dd|d dddddgd�\}}| |¡ qd|S )z!Render different axes a 3D scene.rt   )gÍÌÌÌÌÌì?ç333333Ó?r×  rv   r€   rT  r  Try  r   ru   )r  r  r  Nr\   r[   r   r„  )r¹  r”   rÿ   Úarrowg{®Gáz”?Úscalarr†  ç…ëQ¸Õ?ç…ëQ¸å?rŽ   )r²   r³   rm   rˆ  r‰  rŠ  rq   rÿ   r”   Ú
scale_modeÚ
resolutionr)  )r]   ÚallcloseÚeyerÐ  r   r  rÅ   r:  ÚreshaperÏ   r�  Zcolumn_stackr¦  )r$  r†   r³  r²  rÝ   Úactorsrø   r²   r³   rm   rˆ  r‰  rŠ  rÕ  rj  rN   rN   rR   rž  ¿  s0    ." ÿ

   ý
rž  c                 C   s‚   t d }t|d tjƒ}t|d |ƒ}t|ƒdkr:tdƒ tƒ }t||ƒD ]2\}}| j	|||d |d dd	�\}	}
| 
|	¡ qJ|S )
Nr‹   rz  rt   r   z8Digitization points not found. Cannot plot digitization.Zdig_fid_scaleZdig_fid_opacityT©r¹  r”   rÿ   r,  r|  )r   rj   r   r`   r'   r|   r8   rÅ   rÒ   r�  rÐ  )r$  r†   r³  r¶  rv  Zcar_locrá  r”   r  rÕ  rj  rN   rN   rR   r¡  Õ  s    ý
r¡  c                 C   sh  ddl m} td }|dkrLt|dd�}|d kr8tdƒ‚t ||d|d ¡}t|tƒrˆ|d	krj|||ƒ}nt	|ƒ\}}|t
jkrˆtd
ƒ‚t|tjƒrš|}	nt|t
jƒ}	t|d |	ƒ}	t d¡}
|d d d d…d d…f |d  |
d d…d d…f< |
tddtjd ƒ }
tƒ }t||	ƒD ]J\}}| j|d |d |d ddd|dd|d d|
d�\}}| |¡ �q|S )Nr   )Úget_mni_fiducialsr‹   TrU  zGSubject needs to be specified to automatically find the fiducials file.rl  z-fiducials.fifZ	estimatedzFiducials are not in MRI spacerT  r€   rv   r\   Zmri_fid_scaler   r   rŽ   r  ÚoctZmri_fid_opacity)r²   r³   rm   rˆ  r‰  rŠ  r”   rq   rÿ   r,  r|  Zsolid_transform)r‹   rã  r   r4   rË   r—  r=  rÄ   rR  r   r   rÏ  r]   Úndarrayrj   r'   rß  r-   ÚpirÅ   rÒ   r¦  rÐ  )r$  r¬  r©  r¨  r³  r¶  rã  rv  ÚcfZfid_locZ	transformrá  r”   r  rÕ  rj  rN   rN   rR   r¢  å  sT    
ÿ


ÿ        ü
r¢  rŸ   c
                 C   sp   t d }
|d kr|
d n|}t dd„ |d p0g D ƒ¡}t|d |ƒ}t| ||
d |||||d||	d	�\}}|S )
Nr‹   Z	hpi_scalec                 S   s0   g | ](}|d  t jkr|d t jkr|d ‘qS ©rT   rM   rW   )r   rd   r`   ©rP   rþ   rN   rN   rR   rS     s   þz#_plot_hpi_coils.<locals>.<listcomp>rz  rt   Z	hpi_colorT)r$  r„   r”   rÿ   r,  Úorient_glyphsÚscale_by_distancerð   r|  Úcheck_insideÚnearest©r   r]   r^   r'   Ú_plot_glyphs)r$  r†   r³  r,  rÿ   rê  rë  rð   rì  rí  rv  Zhpi_locrÕ  rj  rN   rN   rR   rŸ    s$    

ÿ    û
rŸ  c                 C   s>   |   |¡d }t|| j| ƒ}t||jd | dd�}||fS )Nr   r#  Fr  )Úqueryr'   r  rð   )rí  rì  Úproject_to_transÚproj_rrrö   Úproj_ptsÚproj_nnrN   rN   rR   Ú_get_nearest  s     ÿ þrõ  c                 C   sº   |d krt |dd�}|d kr(t|d ƒ}t d¡}tj |¡}t|| ƒ}t||||ƒ\}	}
| |	 }|
}|r€|s€||ƒ  t	¡}nt 
t| ƒ¡}tjj|ddd�}d| d	 | }|||	fS )
NZpyvista©rq   r‰   r€   ry   T)r   Zkeepdimséú   r   )r&   r#   r]   rß  ÚlinalgÚinvr'   rõ  rA  ÚintrB  r|   r°   )rú   rð   Zproject_to_surfaceÚmark_insiderì  rí  rñ  Z	inv_transrò  ró  rô  Úvecr#  r)  ÚdistÚvectorsrN   rN   rR   Ú_orient_glyphs)  s*    

   ÿrÿ  rƒ  c                 C   s  |
d k	röt |ƒdkrötd }t||
||	||ƒ\}}}|	rpddlm} t |d¡}|t d|g¡ƒ}d }ddg}nd }d }d }|r„dnd}|r�dnd	}|rž|jn|j\}}}|j\}}}| j	||||||||||d
 d|d
  dfddd |||||d�S | j
|||||d�S d S )Nr   r‹   ©ÚListedColormapr   )r   r   r   r   rƒ  r�  ZvectorÚnoneÚeegp_heightr  é   )r”   rÿ   rq   r‹  rŒ  rÝ  r�  Zglyph_radiusr,  rÜ  r)  r*  Úclimrâ  )r|   r   rÿ  rÞ   r  r]   rÐ  r^   rÏ   r¦  r�  )r$  r„   r”   rÿ   r,  rq   rê  rë  Úproject_pointsrû  rð   r|  rì  rí  rv  r)  rþ  ró  r  r*  r  rÜ  r²   r³   rm   rˆ  r‰  rŠ  rN   rN   rR   rï  ?  sZ     þ

             ú
þrï  c                 C   sv   t d }t dd„ |d pg D ƒ¡}t|d |ƒ}|d k	rB|| n|}t| ||d |d |||||d|	|
d	�\}}|S )
Nr‹   c                 S   s0   g | ](}|d  t jkr|d t jkr|d ‘qS rè  )r   ZFIFFV_POINT_EXTRAr`   ré  rN   rN   rR   rS   i  s   þz+_plot_head_shape_points.<locals>.<listcomp>rz  rt   Zextra_colorZextra_scaleT)r$  r„   r”   rÿ   r,  rê  rë  rû  rð   r|  rì  rí  rî  )r$  r†   r³  r,  rê  rë  rû  rð   Úmaskrì  rí  r7   rv  Zext_locrÕ  rj  rN   rN   rR   r   c  s&    

ÿ   ù
r   çú~j¼t“X?c              
   C   sú   ddl m} t||gƒ |d jd }|d }|d tjkrP|d  ddd	¡}	n|d  dd	d	¡}	t||ƒ| }t||	d
d�}	d}
d}d}tƒ }t	t
|	jd ƒ|
||fƒD ]J\}}| j|j|	d d …|f jž|ddt |¡||dœŽ\}}| |¡ qª|S )Nr   )ÚForwardZ	source_rrr   Z
source_oriZ	source_nnry   r   r\   Fr  )rŽ   r  r  )r  rŽ   r  )r  r  rŽ   rØ  rÙ  )r”   rq   rÜ  r)  rÿ   r,  )rË  r	  r?   rÊ   r   ZFIFFV_MNE_FIXED_ORIrà  r'   rÅ   rÒ   rÕ   r¦  rÏ   r]   rB  rÐ  )r$  rª  Z	fwd_transZ	fwd_scalerÿ   rº   r	  Z	n_dipolesZfwd_rrZfwd_nnÚredÚgreenÚbluerá  Úorir”   rÕ  rj  rN   rN   rR   r§  z  s6    " ÿ    ýr§  çš™™™™™é?c           '         s´  t d }ttˆ|ƒ||d�\‰ }}ttƒ tƒ tƒ tƒ tƒ tƒ tƒ d�}ttƒ tƒ tƒ tƒ tƒ tƒ d�}|	dkrrdnd}ˆ  ¡ D �]`\}}tˆˆj |¡ƒ}|t	kr¨d}n|t
kr´d	}|dksÄd
|koÒ|dkpÒd|k}||d  }t|tƒ�r&|\}}| jt|| |d�|ddd�\}}||  |¡ n|�r:||  |¡ ||k�r`d|k�r`|d  || ¡ ||k�r†d|k�r†|d  || ¡ ||kr~||kr~d|kr~| j|| tj | || tj | d| d�\}}||  |¡ q~| ¡ D ]œ} t||  ƒdk�rêt ||  ¡}!|!t |!¡jdd�  }!|| d  }|| d  }"t| |!| ||"| |
||||||d�\}}| dk�rvd} ||   |¡ �qêtˆdd�}#|#jdk�r°d|k�r°t d ¡ t ‡ ‡fd!d"„|#D ƒ¡}$|$t |$¡jdd�  }$t|$|ddd#�d$d%… \}%}&~$|%|9 }%|d& | }"| j|%d'd'…df |%d'd'…df |%d'd'…d$f |&d'd'…df |&d'd'…df |&d'd'…d$f |d( d)|"d*|d+ d,|d+  d- dfd.dd/�\}}|d  |¡ |S )0zRender sensors in a 3D scene.r‹   )r³  r­  )ru   r�   r  r\  rY  rZ  r[  )r  r\  rY  rZ  ÚsourceÚdetectorr}  r   rŠ   r\  ru   r¾  r  rP  rt  )r‰   r·   r…  Tr{  r¿  r  rÀ  r  r¼  çü©ñÒMbP?)Úoriginrí   Zradiusr   r~   Z_scale)r$  r„   r”   rÿ   r,  rê  rë  r  rð   rì  rí  )r  r  )r  ri  z&Projecting sensors to the head surfacec                    s   g | ]}ˆ ˆj |  ‘qS rN   r!  )rP   rö   ©rÅ  r†   rN   rR   rS   Û  s    z!_plot_sensors.<locals>.<listcomp>)Zproject_rrsZ	return_nnr   r€   Z
eegp_scaleNZ
eegp_colorrƒ  r  r  r  r¼   r†  r‡  )r   rÔ  r   rÃ   rÅ   r�  r   r   r–  r   r   rÄ   rÆ   r.  rÐ  Ztuber]   rà   Úkeysr|   r^   Úisnanr×   rï  r   r  r5   r†   r$   r¦  )'r$  r†   r³  r°  ru   r  r\  r­  r·  ÚunitsZsensor_opacityrê  rë  r  rð   rì  rí  rv  r¿  rÀ  rá  ZlocsrÙ  Úch_namerÓ  r  Zplot_sensorsr”   Zvertsr¹   rÕ  rj  Zsensor_typeZsens_locrÿ   Zeeg_indicesZeeg_locZeegp_locZeegp_nnrN   r  rR   r£  ”  sÖ      ÿ  ÿ ÿÿ  þ
ÿý

  ù


ÿ  þ þ
      ø
	r£  c                 C   sP   t  | d dft¡}t  d| ¡|dd…df< |dd…df d |dd…df< |S )z:Make tris given a number of vertices of a circle-like obj.r   r\   Nr   )r]   Zzerosrú  rÚ   )Zn_vertr·   rN   rN   rR   Ú_make_tris_fanò  s     r  c                 C   sü  ddl m}m} zddl m} W n  tk
r@   ddlm} Y nX | d d@ }d}|tjtjtj	tj
fk�r&| d }d}t || d	 g|d	 | d	 g|d	 |d	 g||d	 g| | d	 g| d	 | d	 g| d	 |d	 g| |d	 gg¡}tjtd
ƒtd
ƒdd…ddd…f d
 fdd�}	�n˜|tjtjtjtjtjfk�r�|dk�rRdn
| d d	 }
t ddgddgddgddgg¡|
 }td
ƒ}	�n.|tjtjtjtjtjtjfk�rd}t dtj t |¡ t|ƒ ¡}t dg|f¡}|| d d	 9 }t |j|jg¡j }t|d ƒ}	�n¢|tj!tj"tj#tj$tj%tj&tj'tj(tj)tj*f
k�r|dk�r`| d nd}d}t dtj t d|¡ t|d ƒ ¡}d|d< || d d	 9 }t t |j|d	  |j|d	  g¡t |j|j g¡g¡j }t t|d ƒt|d ƒ| d g¡}	n°| d  +¡ }zt,|||ƒj-ƒ}	W nŠ |k
�r¸   t. /d¡ tj0j1|j dd�\}}}d|dd…df< || }||dd…dd…f ƒj-}	t |	|	dd…ddd…f f¡}	Y nX d}|�rÔtj2|ddd �}|j3dk�rð|j4d d!k�sôt5‚||	fS )"zGet the sensor shape vertices.r   )Ú
ConvexHullÚDelaunay)Ú
QhullErrorro  iÿÿ  Tr  g{®Gázd?r¼   r€   Nry   r~   iÐ  r  g      ð¿rŽ   é   y               @r  r   )iŒ  i¥  Úbaser  Z	rmag_origzFalling back to planar geometryF)Zfull_matricesr   )rz   )r   r   Zconstantrö  r\   )6Zscipy.spatialr  r  r  ÚImportErrorZscipy.spatial.qhullr   ZFIFFV_COIL_NM_122ZFIFFV_COIL_VV_PLANAR_WZFIFFV_COIL_VV_PLANAR_T1ZFIFFV_COIL_VV_PLANAR_T2r]   r^   rÌ   r  ZFIFFV_COIL_POINT_MAGNETOMETERZFIFFV_COIL_VV_MAG_T1ZFIFFV_COIL_VV_MAG_T2ZFIFFV_COIL_VV_MAG_T3ZFIFFV_COIL_KIT_REF_MAGZFIFFV_COIL_MAGNES_MAGZFIFFV_COIL_MAGNES_REF_MAGZFIFFV_COIL_CTF_REF_MAGZFIFFV_COIL_BABY_MAGZFIFFV_COIL_BABY_REF_MAGZFIFFV_COIL_ARTEMIS123_REF_MAGÚexpræ  rÚ   rÈ   ÚrealÚimagrÏ   ZFIFFV_COIL_MAGNES_GRADZFIFFV_COIL_CTF_GRADZFIFFV_COIL_CTF_REF_GRADZFIFFV_COIL_CTF_OFFDIAG_REF_GRADZFIFFV_COIL_MAGNES_REF_GRADZ"FIFFV_COIL_MAGNES_OFFDIAG_REF_GRADZFIFFV_COIL_KIT_GRADZFIFFV_COIL_BABY_GRADZFIFFV_COIL_ARTEMIS123_GRADZFIFFV_COIL_ARTEMIS123_REF_GRADrˆ   r%   Z	simplicesr5   Údebugrø  ZsvdÚpadrÉ   rÊ   r’  )rÒ  r  r  r  Zid_r#  Z	long_sideÚoffsetrñ   r·   r  Zn_ptsÚcircleZbaselinerˆ  rj  Zrr_rotrN   rN   rR   rÎ  ú  s¶    ý
ø	
ÿÿ
ü&û"÷(ÿýÿ
& rÎ  c                 C   s@   dd l m} t| tƒr2| dkr2tdddgdd�} n
| | ¡} | S )Nr   )ÚmneÚmne_analyzer   r   Ú
matplotlibr  )rÁ   rÂ   rÄ   rR  rC   Úget_cmap)r*  rî   rN   rN   rR   Ú	_get_cmap^  s
    
r*  r  c                 C   sJ  ddl m} t|t|fdƒ t |¡}t|tƒrv|dkrn| dkr\|rV|dk  ¡ rVd}qnd}nd| krjd}nd}t|ƒ}t||ƒs„t	‚dd	d
dddddddddg}|dd„ |D ƒ7 }|ddg7 }| dkrð|rÚ|j
|krÚd}nd}dd|dddgi} t| tƒ�s
td| f ƒ‚d| kd| k dk�r.td| f ƒ‚d| k�rF|�sFtd ƒ‚d| k}t | |�r^dnd t¡}	t |	t¡}	|	jd!k�r�td"|	jf ƒ‚t |	¡dk  ¡ �r²td#|	f ƒ‚|  dd¡}
td$|
d%d&dgƒ |
dk�r
|�rêt |¡n|}t ||	¡}	t d'|	f ¡ t|	ƒd(k�st	‚td%d)�} |	| |�r4dnd< t| ||d*�}|S )+z“Convert colormap/clim options to dict.

    This fills in any "auto" entries properly such that round-trip
    calling gives the same results.
    r   )ÚColormapr*  rO  r&  Úhotr›   ZPiYGZPRGnZBrBGZPuOrZRdGyZRdBuZRdYlBuZRdYlGnZSpectralZcoolwarmZbwrZseismicc                 S   s   g | ]}|d  ‘qS )Ú_rrN   ré  rN   rN   rR   rS   ‚  s     z!_process_clim.<locals>.<listcomp>r'  Úpos_limsrT   Úpercenté`   g     `X@gÍÌÌÌÌüX@z%"clim" must be "auto" or dict, got %sr   zBExactly one of lims and pos_lims must be specified in clim, got %sz2Cannot use "pos_lims" for clim, use "lims" instead©r\   z"clim has shape %s, it must be (3,)z8colormap limits must be monotonically increasing, got %szclim['kind']ÚvalueÚvalueszUsing control points %sr\   )rT   )r  r*  Útransparent)rÞ   r+  r?   rR  r]   ZasarrayrÄ   r×   r*  r’  r-  rÃ   rË   r^   rÈ   rÊ   rL  r¥  r@   r9  Z
percentiler5   r†   r|   )r  r*  r4  r  Úallow_pos_limsr+  Zdiverging_mapsru  Ú	divergingÚctrl_ptsZ	clim_kindZ	perc_dataÚmapdatarN   rN   rR   Ú_process_climg  sv    

    þÿÿÿ

r9  c                 C   sÔ   d| d k}|rdnd}t  | d | ¡}|jdks8t‚tt|ƒƒdkr||d dkrltdƒ t jdtd	�}qÄ|d
ddg9 }nHtt|ƒƒdkrÄ|d |d kr dnd}|d ||d |d    |d< || d |< dS )z0Help plotters that cannot handle limit equality.r.  r  r›   r1  r   r   zAll data were zeror\   )Zdtyper  rŸ   r   gñhãˆµøä>gñhãˆµøä¾N)	r]   r^   rÊ   r’  r|   rÔ   r8   rÚ   rÈ   )r8  r6  ru  r7  ZbumprN   rN   rR   Ú_separate_mapª  s     r:  c                 C   sx  ddl m} d| d k}| d |r&dnd }|rž|d  |d g}t |d d d…  |d  dg||d  g¡d d }dd	dddd
dg}dddddddg}n^|d |d g}|d |d  }|dkrÚ|d |d  | nd}	d|	dg}dddg}dddg}t ddd¡}
t | d t |
||¡ƒ¡}| d �rJt |
||¡|d d …df< t |d t |¡|d g¡}||ƒ}||fS )Nr   r   r.  r  r›   r   ry   rŸ   r…  rn  r   é   r*  r4  r\   )rÞ   r  r]   rÌ   rš  r^   Úinterpr7  )r8  r  r6  Ú	scale_ptsr›   Z	ctrl_normZlinear_normZ
trans_normÚrange_ÚmidZ	interp_tor*  rN   rN   rR   Ú_linearize_map¿  s:    
ÿÿÿ 



ÿ
r@  c                 C   sÎ   d| d k}| d |rdnd }d|d |d   }|d |d | krV|d d d… }|d |d | krx|d d d… }|rÄt |d dkƒ}tt ||d … ¡d d d…  ƒdg t||d … ƒ }t |¡S )	Nr.  r  r›   r1  r   r   r   ry   )rú  rÅ   r]   r^   )r8  r6  ÚticksÚdeltarö   rN   rN   rR   Ú_get_map_ticksÛ  s    8rC  c                    sœ   t ˆ dtdfdƒ ˆ dkrN|dk	rJt|ƒdkrJ|dkr<d‰ qN|dkrNd	‰ nd‰ tˆ tƒrjˆ ‰‡ ‡fd
d„‰ ˆ dks~tˆ ƒs~t‚|dk	r”t||ƒ\}}ˆ |fS )z#Handle time label string and units.NÚcallablerF  rO  r   r»   ztime=%0.3fsÚmsztime=%0.1fmsc                    s(   z
ˆ|  W S  t k
r"   ˆ  Y S X d S r½  )Ú	Exception)r²   ©rF  Ztime_label_fmtrN   rR   rF  ø  s    
z _handle_time.<locals>.time_label)r?   rR  r|   rÄ   rD  r’  rH   )rF  Ú	time_unitr8  rj  rN   rG  rR   Ú_handle_timeé  s    
rI  c                 C   sÐ   d}| j  d¡r&d}| j  d¡d | _ | j dkr4dS | jj}|jj}|d j}|d	 d
krb|d }t 	t 
|| ¡¡}| j dkr”t d|| f¡}n$| j dkr¸t t|ƒd || f¡}|| }|j |¡ dS )z*Handle key presses for time_viewer slider.r   Zctrlrq  ú+ry   )rõ   ÚrightNÚstcrH  rE  rŠ   rõ   r   rK  )ru  Ú
startswithr™  ÚcanvasrÑ   Úsliderrù   r8  r]   rÙ   r9  rÎ   rÍ   r|   Zset_val)ÚeventÚparamsÚstepÚtime_viewerr2  r8  rH  Ú	this_timerN   rN   rR   Ú_key_pressed_slider  s$    



rU  c                 C   sn  ddl m} |d }|d }| ¡  |j}|d dkr:dnd}| d	krLd
}nt t || |  ¡¡}|d d
kr–|jd	t|j	d
 ƒ…||d …f }n$|jt|j	d
 ƒd	…||d …f }||d |d |d |d d	dd�}	|	| }
|d d |d d
  }|
|d d
  | }|d }|d }|d }|j
|d j|dd
ddœŽ}tj|| dd� ¡ }tj|d | dd� ¡ }||ƒ}|d	d	…d	d…f  |d	d	…dgf 9  < |d	d	…d	d…f  ||ƒd	d	…d	d…f d|d	d	…dgf   7  < d|d	d	…df< | |¡ |d d	k	�r4|j|d || | ƒdd� t|ƒ | d ¡ |jd!d"gd#d!d"gd$� |jj ¡  d	S )%z.Smooth source estimate data and plot with mpl.r   )Ú_hemi_morphrø   rL  rH  rE  rŠ   rŽ   Nr   Úhemi_idxr   r·   r~  r%  Úsmoothing_stepsT)Úmapsr8   r=  ÚfacesÚgreymapr¯   ÚcoordsF)r¹   Zantialiasedr/  r  r~   Úcurvr\   rF  rŠ  r“   Úoffé°ÿÿÿéP   )r_  r`  ©r’   r£   r½   )ÚmorphrV  Úclearr8  r]   rÙ   r9  r  r|   r%  rä   rÏ   r7  ÚflattenÚset_facecolorÚ	set_titlerè   r   rÔ   rÑ   rN  Údraw)rT  rQ  rV  rø   rL  r8  ZscalerrH  r  rb  Z
array_plotr>  rJ  rZ  r[  r¯   ZpolycZ	color_aveZcurv_averN   rN   rR   Ú_smooth_plot  s\    &$    þ  ÿ*F
ÿ
rh  ra  rc  rx   Úlatr»   ÚblackÚoct6c           ;      C   sÜ  ddl m} ddlm} ddlm} ddlm} ddl}ddl	m
} ddlm} |d	krdtd
| ƒ‚dddœdddœdddœdddœdddœdddœdddœdddœdœ}dddœdddœdddœdddœdddœdddœdddœdddœdœ}|dkrüdn|}t||d	�}|dk�rdn|}td|t| ¡ ƒƒ t|	||| jƒ}t|ƒ t|ƒ\}}~~t||| jƒ\}}|
dk�r‚|jdd�n|
}z||dd�} W n tk
�r´   ||ƒ} Y nX | | ¡ |dk�rÎdnd}!t ||dd ||f ¡}"|d!k�r|j |"¡\}#}$tdƒ}%njt |ƒ\}&}'}(})t!|"|||&|(|ƒ}"|"d" }%|"d# }$|"d$ |% }#|$j"}*| #|$d%¡ $|*¡d }$t% &|$¡ 't(¡}$~"| j)|! }+t*|+ƒ},|||ƒ|! }-| +|¡}.| +d&¡}/|j ,t ||dd'| ¡¡|% }0t% -t% .|0dkt%j/¡d(d)¡}0t| | |#|$|!|+|-||,|%|.|0||/||d*�}1t0||1ƒ | j1f || | Ž z|  2|¡ W n  t3k
�r^   |  4|¡ Y nX |�rît5d+d�}||_6| 7¡ }2|dk�rŒd}||2d,|d |d- |d.�}3|3|_8t9t0|1d/�}4|3 :|4¡ t9t;|1d/�}5|j< =d0|5¡ |j>d1d2d3d4d5� |j>d6d6d7d7d5� dd8l?m@}6 |jjA|.| B|d |d ¡d9�}7|6| d:d;d<d=d>�}8|jC| D|8d?¡d@dA� |7 Et% F|d |d dB¡¡ |�rÐ|jG|7|8dCdD�}9| D|8dE¡}:|jC|:d@dA� |8jHdFdG� |9jI 2dH¡ |8jJ|d |d fdI� tKdJƒ |S )KzPlot source estimate using mpl.r   Nr¨   )Úcm)ÚSlider)Ústatsr   )Ú_get_subject_sphere_trisrd  z8hemi must be 'lh' or 'rh' when using matplotlib. Got %s.é´   ©ZelevZazimrŽ  i¦ÿÿÿgžï§ÆK¯Z@r¾   iˆÿÿÿ)ri  ZmedZrosZcauZdorZvenÚfroZpargw¾Ÿ/½0@r3  iÄÿÿÿrO  Fri  Úviews)rÆ  rÆ  )Zfigsize)Zauto_add_to_figurerc  r   rð   z%s.%srf   ÚvertnoÚuse_trisr‰   ZdenseZGreysz%s.curvrÚ  rÛ  )rø   rL  r\  rZ  rW  r%  r·   rX  Ún_vertsr~  r¯   r]  r=  r[  rF  rH  )g      @r…  ZTimery   )rø   ÚlabelZvalminZvalmaxZvalinit©rQ  Úkey_press_eventg¸…ëQ¸¾?çš™™™™™©?rn  rr  )rõ   ÚbottomrK  Útopr  rŽ   )Ú
inset_axesr®   z80%z5%é   g      @)ÚwidthÚheightr„   Z	borderpadZxticklabelsrŠ  r“   r;  Ú
horizontal)ÚcaxÚorientationZyticklabelsr  )Z	labelsizeú0.5©r’   T)LrÁ   rÂ   rß   r©   r(  rl  Zmatplotlib.widgetsrm  ÚnibabelZscipyrn  rb  ro  rË   rÃ   r@   r”  r  r9  r  r:  r@  rI  r8  rÑ   rF  Zadd_axesr—  r=  Z
freesurferZread_geometryrÌ  r   r   rÊ   Zrankdatarà  r]   r@  rA  rú  r%  r|   r)  Zread_morph_dataÚclipr^   Zint64rh  ré   re  ÚAttributeErrorZset_axis_bgcolorrG   rS  rÝ   rO  r   Z
on_changedrU  rN  Úmpl_connectZsubplots_adjustZ%mpl_toolkits.axes_grid1.inset_locatorr}  ZScalarMappabler¦   ZsetpZgetprá   rš  ÚcolorbarZtick_paramsrø   rÔ   rE   );rL  r¨  r.  r´  r*  rF  rX  r©  rs  r  rÑ   Úinitial_timerH  Ú
backgroundÚspacingrS  rŠ  r4  rî   r©   rl  rm  Únibrn  ro  Z	lh_kwargsZ	rh_kwargsrý   r8  r=  r8  rÀ   rø   rW  rð   r\  rZ  r~  ÚstypeZsvalZico_surfZsrc_type_strrÊ   r%  rv  r·   r¯   r[  r]  rQ  Úax_timerO  Zcallback_sliderZcallback_keyr}  Úsmr‚  ÚcbZ	cb_yticksrN   rN   rR   Ú_plot_mpl_stcI  sü    ÿùù


ÿ


ÿÿ
       û


 ÿ

ÿÿr“  c           
      C   sÊ   ddl m} |ƒ dkr&td |ƒ ¡ƒ‚ddlm}m} t| tƒsF| g} t	| ƒdkrZt
dƒ‚| D ](}t||ƒs~td t|ƒ¡ƒ‚| ¡  q^d	d
„ | D ƒ}	|	 |	d ¡t	|	ƒkr´tdƒ‚|| ||||d� dS )aû  Plot multiple SourceEstimate objects with PyVista.

    Parameters
    ----------
    brains : list, tuple or np.ndarray
        The collection of brains to plot.
    time : bool
        If True, link the time controllers. Defaults to True.
    camera : bool
        If True, link the camera controls. Defaults to False.
    colorbar : bool
        If True, link the colorbar controllers. Defaults to True.
    picking : bool
        If True, link the vertices picked with the mouse. Defaults to False.
    r   )Ú_get_3d_backendÚ	pyvistaqtz2Expected 3d backend is pyvistaqt but {} was given.)ÚBrainÚ_LinkViewerr   z"The collection of brains is empty.z(Expected type is Brain but {} was given.c                 S   s   g | ]
}|j ‘qS rN   )Z_subject_id)rP   r]  rN   rN   rR   rS   ç  s     zlink_brains.<locals>.<listcomp>z+Cannot link brains from different subjects.)ÚbrainsrE  ÚcamerarŠ  ÚpickingN)r6  r”  r  r  Z_brainr–  r—  rÄ   r   r|   rË   rÁ  ro  Úsetup_time_viewerrÊ  r<  )
r˜  rE  r™  rŠ  rš  r”  r–  r—  r]  ZsubjectsrN   rN   rR   Úlink_brainsÇ  s4    
ÿ

ÿ
ûrœ  c                 C   sN   ddl m}m} t| |ƒrdS t|tddƒ t| |ƒrFtd|ddd	� d
S d S )Nr   )Ú_BaseSurfaceSourceEstimateÚ_BaseMixedSourceEstimateFr«  z1src when stc is a mixed or volume source estimater.  )r`  r_  z%when plotting a mixed source estimate©ÚextraT)Úsource_estimater�  rž  rÄ   r?   r   r@   )rL  r«  r.  Úbackend_namer�  rž  rN   rN   rR   Ú_check_volumeõ  s    
ÿ
  þr£  rŽ   Úclassicrw  Úverticalc           %      C   s4  ddl m}m}  ddlm}!m}" |"| |ƒ t| |!ddƒ t|
dd�}
t| j	|ƒ}t
d	|d
dddgƒ |dk}#|#s¨|d
kr¨z
|ƒ }W n$ ttfk
r¦   tdƒ d}#Y nX t|||||||
|||||||	||d�}$|#rèt| fd|i|$—ŽS | |ƒ�: t| f||||||d||||||dœ|$—ŽW  5 Q R £ S Q R X dS )aX  Plot SourceEstimate.

    Parameters
    ----------
    stc : SourceEstimate
        The source estimates to plot.
    %(subject_none)s
        If ``None``, ``stc.subject`` will be used.
    surface : str
        The type of surface (inflated, white etc.).
    hemi : str
        Hemisphere id (ie 'lh', 'rh', 'both', or 'split'). In the case
        of 'both', both hemispheres are shown in the same window.
        In the case of 'split' hemispheres are displayed side-by-side
        in different viewing panes.
    %(colormap)s
        The default ('auto') uses 'hot' for one-sided data and
        'mne' for two-sided data.
    %(time_label)s
    smoothing_steps : int
        The amount of smoothing.
    %(transparent)s
    alpha : float
        Alpha value to apply globally to the overlay. Has no effect with mpl
        backend.
    time_viewer : bool | str
        Display time viewer GUI. Can also be 'auto', which will mean True
        for the PyVista backend and False otherwise.

        .. versionchanged:: 0.20.0
           "auto" mode added.
    %(subjects_dir)s
    figure : instance of Figure3D | instance of matplotlib.figure.Figure | list | int | None
        If None, a new figure will be created. If multiple views or a
        split view is requested, this must be a list of the appropriate
        length. If int is provided it will be used to identify the PyVista
        figure by it's id or create a new figure with the given id. If an
        instance of matplotlib figure, mpl backend is used for plotting.
    %(views)s

        When plotting a standard SourceEstimate (not volume, mixed, or vector)
        and using the PyVista backend, ``views='flat'`` is also supported to
        plot cortex as a flatmap.

        Using multiple views (list) is not supported by the matplotlib backend.

        .. versionchanged:: 0.21.0
           Support for flatmaps.
    colorbar : bool
        If True, display colorbar on scene.
    %(clim)s
    cortex : str or tuple
        Specifies how binarized curvature values are rendered.
        Either the name of a preset Brain cortex colorscheme (one of
        'classic', 'bone', 'low_contrast', or 'high_contrast'), or the name of
        a colormap, or a tuple with values (colormap, min, max, reverse)
        to fully specify the curvature colors. Has no effect with mpl backend.
    size : float or tuple of float
        The size of the window, in pixels. can be one number to specify
        a square window, or the (width, height) of a rectangular window.
        Has no effect with mpl backend.
    background : matplotlib color
        Color of the background of the display window.
    foreground : matplotlib color | None
        Color of the foreground of the display window. Has no effect with mpl
        backend. None will choose white or black based on the background color.
    initial_time : float | None
        The time to display on the plot initially. ``None`` to display the
        first time sample (default).
    time_unit : 's' | 'ms'
        Whether time is represented in seconds ("s", default) or
        milliseconds ("ms").
    backend : 'auto' | 'pyvistaqt' | 'matplotlib'
        Which backend to use. If ``'auto'`` (default), tries to plot with
        pyvistaqt, but resorts to matplotlib if no 3d backend is available.

        .. versionadded:: 0.15.0
    spacing : str
        Only affects the matplotlib backend.
        The spacing to use for the source space. Can be ``'ico#'`` for a
        recursively subdivided icosahedron, ``'oct#'`` for a recursively
        subdivided octahedron, or ``'all'`` for all points. In general, you can
        speed up the plotting by selecting a sparser source space.
        Defaults  to 'oct6'.

        .. versionadded:: 0.15.0
    title : str | None
        Title for the figure. If None, the subject name will be used.

        .. versionadded:: 0.17.0
    %(show_traces)s
    %(src_volume_options)s
    %(view_layout)s
    %(add_data_kwargs)s
    %(brain_kwargs)s
    %(verbose)s

    Returns
    -------
    figure : instance of mne.viz.Brain | matplotlib.figure.Figure
        An instance of :class:`mne.viz.Brain` or matplotlib figure.

    Notes
    -----
    Flatmaps are available by default for ``fsaverage`` but not for other
    subjects reconstructed by FreeSurfer. We recommend using
    :func:`mne.compute_source_morph` to morph source estimates to ``fsaverage``
    for flatmap plotting. If you want to construct your own flatmap for a given
    subject, these links might help:

    - https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOccipitalFlattenedPatch
    - https://openwetware.org/wiki/Beauchamp:FreeSurfer
    r   )r”  Úuse_3d_backendr   )Ú_BaseSourceEstimateÚ_check_stc_srcrL  zsource estimateT©r©  rV  ÚbackendrO  r(  r•  Únotebookz0No 3D backend found. Resorting to matplotlib 3d.)r¨  r.  r´  r*  rF  rX  r©  rs  r  rÑ   r‹  rH  rŒ  rS  rŠ  r4  r�  N)Úoverlay_alphaÚbrain_alphaÚvector_alphaÚcortexÚ
foregroundr  Úscale_factorÚshow_tracesr«  Úvolume_optionsÚview_layoutÚadd_data_kwargsÚbrain_kwargs)r6  r”  r¦  r¡  r§  r¨  r?   r4   r6   r¨  r@   r  ÚModuleNotFoundErrorr8   rÃ   r“  Ú	_plot_stc)%rL  r¨  r.  r´  r*  rF  rX  r4  rº   rS  r©  rÑ   rs  rŠ  r  r¯  r  rŒ  r°  r‹  rH  rª  r�  r–   r²  r«  r³  r´  rµ  r¶  r7   r”  r¦  r§  r¨  Zplot_mplrý   rN   rN   rR   Úplot_source_estimates  sv    ~
ÿ
ÿ

          ú
ÿ       úúr¹  c           1         s0  ddl m}m} ddlm}  | jdk}!t|dd�}t| j|ƒ}|ƒ }"~|ƒ }#t	|||| |"ƒ}t
d|d	d
ddgƒ t
d|dƒ t||| jƒ\}}$t|||$ƒ\}}|!r¬|  ¡ jn| j}%t|	|||%|! d�}&t| |||"ƒ}'t|&ƒ |&d }d|&d k}(|&d |(rþdnd })|&d }~&|dk�r"d	d
g}*n|g}*|d k�r6|}|dk�rDd}t|*ƒdk�rV|nd||*d f }+||||+|||||
|||dœ},|d k	�r˜|, |¡ d|,d< ||,d< tjdd�� |#f |,Ž‰ W 5 Q R X ~,|d k�rþt ‡ fdd„|*D ƒ¡}-d|- |)d  }|d k�rd}|(�rd nd }.| |||$||||||||.|)d |)d |)d |	||d d!œ},|d k	�rf|, |¡ |*D ]z}t| | ƒ�r€ �qæ| j|d	k�r’dnd }/t|/ƒdk�rª�qj|, ¡ }0|0j|d"� tjdd�� ˆ jf |0Ž W 5 Q R X �qj|'�r|, ¡ }0|0jd#d"� ˆ jf |0Ž ~,|�r$ˆ j||d$� nˆ  ¡  ˆ S )%Nr   )r”  Úget_brain_classr   )Ú_BaseVolSourceEstimater\   Tr©  r´  rc  re  r™  Úbothr´  )r¥  r�  )r5  r*  r.  r  r›   r4  )r¼  r™  r   z%s - %s)Z
subject_idr´  rð   r–   r¯  r  rŒ  r°  rÑ   r©  rs  rº   Frì   ©Úrecordc                    s4   g | ],}|ˆ j krt ˆ j | jd d …df ¡‘qS )Nr   )Zgeor]   Zptpr\  )rP   r´  ©r]  rN   rR   rS   ì  s    
ÿz_plot_stc.<locals>.<listcomp>gš™™™™™™?ry   r  )r^   r*  rX  rE  rF  rº   rŠ  r®  r±  r‹  r4  r¹  ZfminZfmidZfmaxr  r«  r³  r7   )r´  r�  )rS  r²  )r6  r”  rº  r¡  r»  Z
_data_ndimr4   r6   r¨  Ú_check_viewsr@   rI  r8  Ú_check_st_tvZ	magnituder  r9  r£  r:  r|   rÍ  ÚwarningsÚcatch_warningsr]   r7  rÄ   r%  rˆ   Zadd_datar›  rì   )1rL  r¨  r.  r´  r*  rF  rX  r©  rs  r  rÑ   r‹  rH  rŒ  rS  rŠ  r4  r­  r¬  r®  r¯  r°  r  r±  r²  r«  r³  r´  rµ  r¶  r”  rº  r»  rü  rª  r–  r8  Zuser8  Úvolumer6  r=  Zhemisr–   rý   r  r¹  r%  Z
use_kwargsrN   r¿  rR   r¸  ©  sÚ    
ÿ  ÿ
ÿ



"       û


ÿ
 î

ÿr¸  c                 C   s|   t d|dƒ t| ttdfdƒ t| tƒr8t d| ddd� |dkrDd	}| dkrd|ob|d k	obt|ƒd
k} | rt|sttdƒ‚| |fS )NrS  )TFrO  r  r²  )rO  ZseparateZvertexrw  zwhen a stringrŸ  rO  Tr   z1show_traces cannot be used when time_viewer=False)r@   r?   rR  r¤  rÄ   r|   rË   )r²  rS  r8  rN   rN   rR   rÁ  #  s$    
þÿ
ýrÁ  c                 C   sT   | d ||f| d ||f| d ||ffD ]&\}}}|j |dd� |j|dd� q(d S )NÚax_yÚax_xÚax_zz0.75r“   )rÖ   rÛ   )rQ  r²   r³   rm   rø   Úaró   rN   rN   rR   Ú_glass_brain_crosshairs8  s    þrÉ  c              	   C   sF   t tj |j¡| ƒ}t t |¡ t¡dt 	|j
d d… ¡d ¡}|S )Nr   r\   r   )r'   r]   rø  rù  Úaffiner‡  r@  rA  rú  r^   rÊ   )Ú
cut_coordsÚimgÚijkrN   rN   rR   Ú_cut_coords_to_ijk@  s    .rÎ  c                 C   s   t |j| ƒS r½  )r'   rÊ  )rÍ  rÌ  rN   rN   rR   Ú_ijk_to_cut_coordsF  s    rÏ  c                 C   sP   dd l }ddlm} t| d|f|ƒ t| tƒrLt|j|ƒ}| t	| ||ƒ¡} | S )Nr   )ÚSpatialImagez	path-like)
r†  Znibabel.spatialimagesrÐ  r?   rÄ   rR  r6   r¨  Úloadr   )rT  rL  r¨  r©  r-  rŽ  rÐ  rN   rN   rR   Ú_load_subject_mriJ  s    
rÒ  Ústat_mapúT1.mgzc           -         sØ  ddl m}m} ddl‰ddlm} ddlm} tddƒsBt	d	ƒ‚dd
l
m}m} ddlm‰
 tdˆdƒ t||d�ˆ ‰tˆ|dƒ t||ƒrÀ|jˆdddd�‰	|jˆddd�‰d|j }}n(t|ddd�}ˆj|dd�‰	d|j }}~ttddˆ	jƒdddd� t|||d�}t tjˆjd ˆ	j dd… d d!�¡j!}|j t"ˆjd ƒdfk�sXt#‚~t$|d"d#�‰‡‡fd$d%„‰td&d'd(d)�‰‡‡fd*d+„‰ ‡fd,d-„}‡	‡
‡fd.d/„‰‡‡fd0d1„‰t%dg‡ ‡‡‡‡‡fd2d3„	ƒ}ˆd4k�rHtˆj&|ƒ}t'||ƒ}t (|d5 t )d6¡¡�sBt|d7dd8� t* +d9¡ ˆ ,ˆ	j-t .|d5 ˆ	j¡¡‰	d}n|dk�rVd:}t/|ˆ||d;ƒ}|dk�r|t0ddƒ}n<t1|ƒ}t* +d<|f ¡ t 2t 3ˆj4| ¡¡}t0||d= ƒ}|dk�rt t 3ˆj5dd…|f ¡ 6¡ ˆj5dd…|f j ¡\}}||j77 }nvt |t1¡}|j d>k�r.t8d?|j f ƒ‚|d@9 }t* +dA| 9¡ f ¡ ˆ|ˆ	ƒ}|dk	�rf|j7}nt 6t 3ˆj5| ¡¡}ˆ
ˆ	|ƒ}|j ˆ	j dd… k�s t#‚~~|| }t:||ƒ}tj; <t=||ƒ|¡ t* +dBˆj4| ft>|ƒ t>|ƒ ˆjd | f  ¡ ~| ?d¡\} \‰‰ˆj@g g dC� t"ˆj4ƒd=k�r6dDnd}!ˆj5| }"ˆjAˆj4|"dE|!dF�d }#t"ˆj4ƒd=k�r‚ˆj@ˆj4ddGg dH� ˆj@dIdJdK� ˆjB|#gd9gdLdM�‰ˆ|ƒ ˆjCˆj4| dNdO�}$|  D¡  ˆd4k}%tE|||	ˆj5|%ƒ}&tF|&ƒ dP|&dQ k‰tG|&ƒ}'tH|&ƒ\}‰~&tIˆd |" I¡ fƒtJˆdG |" J¡ fƒg}(t |(¡t dGd=g¡dR t K|(¡d   }(d})ˆj5 I¡ dk �rŒˆjLdSdTdUdVddW� ˆ })tM|'ƒ}*|)�r´|*dgtMt |'¡ ƒ 7 }*t N|*¡}*ˆj@|*dX� ˆj@|(dY� ~*ˆ�s2ˆdk  O¡ �rôt8dZƒ‚| P|¡}|t Qt RdGd=d[¡ˆˆd  ddVd=g¡ƒ}| S|¡}ˆdG }+tdˆd\|+| |||d"d"d]�
‰‡‡‡‡‡‡‡fd^d_„},tˆˆ|,|| |$ˆ|'d`�‰|,ˆda d9|db� |
�r¦| T¡  | jU VdctW|ˆ|dd�¡ | jU VdetW|ˆdf�¡ | S )haª  Plot Nutmeg style volumetric source estimates using nilearn.

    Parameters
    ----------
    stc : VectorSourceEstimate
        The vector source estimate to plot.
    src : instance of SourceSpaces | instance of SourceMorph
        The source space. Can also be a SourceMorph to morph the STC to
        a new subject (see Examples).

        .. versionchanged:: 0.18
           Support for :class:`~nibabel.spatialimages.SpatialImage`.
    %(subject_none)s
        If ``None``, ``stc.subject`` will be used.
    %(subjects_dir)s
    mode : str
        The plotting mode to use. Either 'stat_map' (default) or 'glass_brain'.
        For "glass_brain", activation absolute values are displayed
        after being transformed to a standard MNI brain.
    bg_img : instance of SpatialImage | str
        The background image used in the nilearn plotting function.
        Can also be a string to use the ``bg_img`` file in the subject's
        MRI directory (default is ``'T1.mgz'``).
        Not used in "glass brain" plotting.
    colorbar : bool, optional
        If True, display a colorbar on the right of the plots.
    %(colormap)s
    %(clim)s
    %(transparent)s
    show : bool
        Show figures if True. Defaults to True.
    initial_time : float | None
        The initial time to plot. Can be None (default) to use the time point
        with the maximal absolute value activation across all voxels
        or the ``initial_pos`` voxel (if ``initial_pos is None`` or not,
        respectively).

        .. versionadded:: 0.19
    initial_pos : ndarray, shape (3,) | None
        The initial position to use (in m). Can be None (default) to use the
        voxel with the maximum absolute value activation across all time points
        or at ``initial_time`` (if ``initial_time is None`` or not,
        respectively).

        .. versionadded:: 0.19
    %(verbose)s

    Returns
    -------
    fig : instance of Figure
        The figure.

    Notes
    -----
    Click on any of the anatomical slices to explore the time series.
    Clicking on any time point will bring up the corresponding anatomical map.

    The left and right arrow keys can be used to navigate in time.
    To move in time by larger steps, use shift+left and shift+right.

    In ``'glass_brain'`` mode, values are transformed to the standard MNI
    brain using the FreeSurfer Talairach transformation
    ``$SUBJECTS_DIR/$SUBJECT/mri/transforms/talairach.xfm``.

    .. versionadded:: 0.17

    .. versionchanged:: 0.19
       MRI volumes are automatically transformed to MNI space in
       ``'glass_brain'`` mode.

    Examples
    --------
    Passing a :class:`mne.SourceMorph` as the ``src``
    parameter can be useful for plotting in a different subject's space
    (here, a ``'sample'`` STC in ``'fsaverage'``'s space)::

    >>> morph = mne.compute_source_morph(src_sample, subject_to='fsaverage')  # doctest: +SKIP
    >>> fig = stc_vol_sample.plot(morph)  # doctest: +SKIP
    r   )rÂ   rJ  Nr   )ÚVolSourceEstimate)ÚSourceMorphZnilearnz0.4z%This function requires nilearn >= 0.4)Úplot_stat_mapÚplot_glass_brain)Ú	index_imgrq   )rÓ  Úglass_brainrL  Znifti1F)Úmri_resolutionZ	mri_spacezmorph.subject_torÄ  z or SourceMorph)rT   r   )rÛ  zsrc subjectZ	mri_voxelÚraszImage affine Úmmr"  )Úprefixr  Úlevel)Z
first_kindr\   ÚF©ÚorderT)Zallow_kdtreec                    sd   t | |ƒ}~ t dt|ƒ ¡ ˆ  |tj ¡\}}|d |d  }}t dˆjd | |f ¡ |S )z/Convert voxel coordinates to index in stc.data.z2    Affine remapped cut coords to [%d, %d, %d] idxr   z.    Using vertex %d at a distance of %d voxels)rÎ  r5   r"  rÆ   rð  r]   rà   r%  )rË  rÌ  rÍ  rý  Úloc_idx)Údist_to_vertsrL  rN   rR   Ú_cut_coords_to_idxÍ  s    
ÿÿz8plot_volume_source_estimates.<locals>._cut_coords_to_idxzX (sagittal)zY (coronal)z	Z (axial)r±   c                    sH  | j |d kr8d}|d jd  ¡ d }| j| j }}n~| j |d krpd}|d jd  ¡ d }| j| j }}nF| j |d kr¨d}| j| j }}|d jd  ¡ d }nt d	¡ d
S t 	|||f¡}t d¡ |d dk�r(d 
ˆ |d j¡¡}t|ƒdk�st‚| dd¡ dd¡ dd¡}| tdddd�| ¡}t t|d ƒ¡}	t||d ƒ}
|dk�ržt |	d
d
…|
d |
d f ¡|
d< t d|
d f ¡ n||dk�rât |	|
d d
d
…|
d f ¡|
d< t d|
d f ¡ n8t |	|
d |
d d
d
…f ¡|
d< t d|
d f ¡ t|
|d ƒ}t dˆ | ft|ƒ  ¡ |S )z'Get voxel coordinates from mouse click.rÆ  r²   rÇ  r   rÅ  r³   rm   r   z    Click outside axesNÚ rq   rÚ  Úimg_idxr\   ÚLÚRÚPÚAÚIÚSr±   r   z    MIP: i = %d idxz    MIP: j = %d idxz    MIP: k = %d idxz/    Cut coords for %s: (%0.1f, %0.1f, %0.1f) mm)ÚinaxesÚlinesÚ	get_xdataÚxdataÚydataÚ	get_ydatar5   r"  r]   r^   r=  Zaff2axcodesrÊ  r|   r’  Úreplacer–  rÃ   r9  r   rÎ  ÚargmaxrÏ  rÆ   )rP  rQ  rø   r²   r³   rm   rË  Úcodesrö   Zimg_datarÍ  )Úax_namerŽ  rN   rR   Ú_click_to_cut_coordsÛ  sH    


$
$$ÿz:plot_volume_source_estimates.<locals>._click_to_cut_coordsc                    s¼   |d   ¡ }|d  |¡d }| jdkr8td|d ƒ}nh| jdkrRtd|d ƒ}nN| jdkrzt|d jd	 d	 |d ƒ}n&| jd
kr t|d jd	 d	 |d ƒ}ˆ ||ƒ |d j ¡  dS )zManage keypress on the plot.ÚlxrL  r   rõ   r   z
shift+leftrx   rK  r   zshift+rightrÀ   N)rð  Útime_as_indexru  rÎ   rÍ   rÊ   rN  rg  )rP  rQ  rê   rö   )Ú_update_timeslicerN   rR   Ú_press	  s    




z,plot_volume_source_estimates.<locals>._pressc                    s   |d   | |d j |d j ¡ |d |d |d   }}}|d }ˆdkrXddd	gnd
d
dg}|j|d   ¡ d |j|d	   ¡ d |j|d   ¡ d f}| ¡  | ¡  | ¡  | dˆˆ | ƒi¡ | dd|d j|   i¡ ||d d|d� d S )Nrù  rL  rÆ  rÅ  rÇ  Ú	plot_funcrÓ  r   r   r­   ry   r   rç  r–   zActivation (t=%.3f s.)ræ  ©r–   rË  )	Z	set_xdataZsfreqZtminrï  rð  ró  rc  rÍ  r8  )rö   rQ  rÆ  rÅ  rÇ  Úplot_map_callbackZidxsrË  )rÌ  rÙ  rq   rN   rR   rû  	  s,    ÿýÿ  ÿz7plot_volume_source_estimates.<locals>._update_timeslicec                    s"   ˆ  ¡ d  ˆ jd |  › ¡ d S )Nr   )Z	get_textsZset_textr%  )rã  )rL  Úvert_legendrN   rR   Ú_update_vertlabel+	  s    z7plot_volume_source_estimates.<locals>._update_vertlabelc                    sð   |d |d |d   }}}|d }| j |d krT|d j| jdd�d	 }ˆ||ƒ ˆ | |ƒ}|d
krjd
S | ¡  | ¡  | ¡  ||d d|d� ˆ||d ƒ}	ˆj|	 }
|	d
k	rÆˆjd	  |
¡ nˆjd	  d¡ ˆ|	ƒ |d j ¡  d
S )zManage clicks on the plot.rÆ  rÅ  rÇ  rý  r�  rL  T)Zuse_roundingr   Nrç  ræ  rþ  r  rÀ   )	rî  rú  rñ  rc  r  rï  Z	set_ydatarN  rg  )rP  rQ  r7   rÆ  rÅ  rÇ  rÿ  rö   rË  rã  rò  )rø  rå  rû  r  r�  rL  rN   rR   Ú_onclick.	  s4     ÿÿ


ÿ
z.plot_volume_source_estimates.<locals>._onclickrÚ  rv   r€   zTransforming subject )rÞ  r  ræ  rÔ  Úbg_imgzFixing initial time: %s secr   r1  z?initial_pos must be float ndarray with shape (3,), got shape %srw   zFixing initial position: %s mmzKShowing: t = %0.3f s, (%0.1f, %0.1f, %0.1f) mm, [%d, %d, %d] vox, %d vertex)ZxticksÚyticksÚorn   )r”   Úmarkerry   r…  r—   Ú
Activation)r˜   r‘   ZVertexr•   Úgr“   r.  r  rz  r  r„  rž   rŸ   r    )r  r¢   z«Negative colormap limits for sequential control points clim["lims"] not supported currently, consider shifting or flipping the sign of your data for visualization purposesr;  rí  )
Ú	thresholdrÝ   Zresampling_interpolationr  rÑ   rŠ  r  r¯   Zblack_bgZsymmetric_cbarc               	      s  ˆ   ¡  ˆ d¡d k	r.ˆd r.ˆd jj  ¡  tjdd��( t dt¡ tˆfˆŽ| |Žˆd< W 5 Q R X ˆd jj	 
d¡ dD ]"}ˆ d| ˆd j| ji¡ q~ˆd rðˆd jj d	¡ ˆsÜtˆd jfˆd
dg žŽ  ˆd j ˆd ¡ ˆdk�rtˆf|d žŽ  d S )NZfig_anatrŠ  Tr½  ÚignoreFr�   Zax_r„  r   ry   Ú
cbar_ticksrÚ  rË  )rc  r¥  Z_cbarrø   rÂ  rÃ  ÚsimplefilterÚDeprecationWarningr   ÚoutlineÚset_visiblerÍ  rÝ   re  r   Z	set_ticksrÉ  )Úargsrý   ru  )rÝ   r6  rq   rQ  rý  Úplot_kwargsr=  rN   rR   Úplot_and_correctÈ	  s.    ÿÿ ÿ 
z6plot_volume_source_estimates.<locals>.plot_and_correct)rL  r�  rý  rç  rÀ   rù  rq   r  rç  )Zstat_map_imgr–   rË  Zbutton_press_event)rQ  r7   ry  rx  )N)Xr(  rÂ   rJ  r†  r¡  rÕ  rb  rÖ  r:   r<  Znilearn.plottingr×  rØ  Znilearn.imagerÙ  r@   rÃ   r?   rÄ   ÚapplyZ
subject_tor   Z	as_volumer“  r/   r,   rÊ  r6   r]   r^   Zunravel_indexr%  rÊ   rÏ   r|   r’  r#   Úverbose_decr¨  r.   rÞ  rß  r5   r†   ZNifti1ImageZdataobjr>  rÒ  rÌ  rÈ   rÙ   r9  r8  r  rõ  ÚstartrË   ÚtolistrÏ  ZtestingZassert_allcloserÎ  rÆ   rÐ   rÔ   rÓ   ZlegendrÖ   rF   r9  r:  rC  r@  rÍ   rÎ   rL  rÛ   rÅ   Úuniquer×   r)  r<  rš  r  rì   rN  r‰  r   )-rL  r«  r¨  r©  rq   r  rŠ  r*  r  r4  rì   r‹  Zinitial_posr7   rî   rJ  rÕ  rÖ  r×  rØ  rT   r¯  Zstc_ijkrü  r  Z	ras_mni_tZtime_slrã  rH  rç  rÍ  rË  rÀ   r  rò  Úhrù  r5  r8  rA  r£   Zdup_negr  r  r  rN   )rø  rå  rû  r  r÷  r�  rÝ   rä  r6  rÌ  rÙ  rq   rŽ  rQ  rý  r  r=  rL  r   rR   Úplot_volume_source_estimatesT  sp   V
ÿÿ
  ÿÿ,

  ÿ
 ÿ
    ÿ

 ÿÿÿ



ÿþ
ÿÿ*


þÿ
      ü    ÿ
ÿÿ
ÿr  c           
      C   sF  ddl m} ddlm} t|tttfdƒ t|tƒr:|gnt|ƒ}| dkrft	d|dgdgfƒ dg}nt
|ƒdkr„|d dkr„d	g}|dgkr¼|d k	r¤t||d
dƒ |d k	r¼|dkr¼tdƒ‚|dgk| dkA rêtdt| ƒ› dt|ƒ› �ƒ‚t	d|dƒ |dk�s
|d k�rdn|}t|ƒD ]&\}}	t	d|› d�|	t|| ƒƒ �q|S )Nr   )Úviews_dictsr   )ÚSourceEstimaters  ZflatrO  r   ÚlateralrL  z%SourceEstimate when a flatmap is used)r•  r«  z5The PyVista 3D backend must be used to plot a flatmapz;surface="flat" must be used with views="flat", got surface=z and views=r´  )r™  r¼  rc  re  r�  Nr™  rc  zviews[rS  )Z_brain.viewr  r¡  r  r?   rÅ   rÆ   rR  rÄ   r@   r|   r<  rË   r‘  rÇ   r”  )
rð   rs  r´  rL  rª  r  r  Zuse_hemiÚvir‰  rN   rN   rR   rÀ  í	  s0    
ÿ rÀ  r,  çš™™™™™Ù?r  c                  C   s^   ddl m} t| |ddƒ t| |d|||||||||||||||||	||||
||||||d�S )a³  Plot VectorSourceEstimate with PyVista.

    A "glass brain" is drawn and all dipoles defined in the source estimate
    are shown using arrows, depicting the direction and magnitude of the
    current moment at the dipole. Additionally, an overlay is plotted on top of
    the cortex with the magnitude of the current.

    Parameters
    ----------
    stc : VectorSourceEstimate | MixedVectorSourceEstimate
        The vector source estimate to plot.
    %(subject_none)s
        If ``None``, ``stc.subject`` will be used.
    hemi : str, 'lh' | 'rh' | 'split' | 'both'
        The hemisphere to display.
    %(colormap)s
        This should be a sequential colormap.
    %(time_label)s
    smoothing_steps : int
        The amount of smoothing.
    %(transparent)s
    brain_alpha : float
        Alpha value to apply globally to the surface meshes. Defaults to 0.4.
    overlay_alpha : float
        Alpha value to apply globally to the overlay. Defaults to
        ``brain_alpha``.
    vector_alpha : float
        Alpha value to apply globally to the vector glyphs. Defaults to 1.
    scale_factor : float | None
        Scaling factor for the vector glyphs. By default, an attempt is made to
        automatically determine a sane value.
    time_viewer : bool | str
        Display time viewer GUI. Can be "auto", which is True for the PyVista
        backend and False otherwise.

        .. versionchanged:: 0.20
           Added "auto" option and default.
    subjects_dir : str
        The path to the freesurfer subjects reconstructions.
        It corresponds to Freesurfer environment variable SUBJECTS_DIR.
    figure : instance of Figure3D | list | int | None
        If None, a new figure will be created. If multiple views or a
        split view is requested, this must be a list of the appropriate
        length. If int is provided it will be used to identify the PyVista
        figure by it's id or create a new figure with the given id.
    %(views)s
    colorbar : bool
        If True, display colorbar on scene.
    %(clim_onesided)s
    cortex : str or tuple
        Specifies how binarized curvature values are rendered.
        either the name of a preset Brain cortex colorscheme (one of
        'classic', 'bone', 'low_contrast', or 'high_contrast'), or the
        name of a colormap, or a tuple with values (colormap, min,
        max, reverse) to fully specify the curvature colors.
    size : float or tuple of float
        The size of the window, in pixels. can be one number to specify
        a square window, or the (width, height) of a rectangular window.
    background : matplotlib color
        Color of the background of the display window.
    foreground : matplotlib color | None
        Color of the foreground of the display window.
        None will choose black or white based on the background color.
    initial_time : float | None
        The time to display on the plot initially. ``None`` to display the
        first time sample (default).
    time_unit : 's' | 'ms'
        Whether time is represented in seconds ("s", default) or
        milliseconds ("ms").
    %(show_traces)s
    %(src_volume_options)s
    %(view_layout)s
    %(add_data_kwargs)s
    %(brain_kwargs)s
    %(verbose)s

    Returns
    -------
    brain : mne.viz.Brain
        A instance of :class:`mne.viz.Brain`.

    Notes
    -----
    .. versionadded:: 0.15

    If the current magnitude overlay is not desired, set ``overlay_alpha=0``
    and ``smoothing_steps=1``.
    r   )Ú_BaseVectorSourceEstimaterL  zvector source estimater`  )r¨  r.  r´  r*  rF  rX  r©  rs  r  rÑ   r‹  rH  rŒ  rS  rŠ  r4  r­  r¬  r®  r¯  r°  r  r±  r²  r«  r³  r´  rµ  r¶  )r¡  r  r?   r¸  )rL  r¨  r´  r*  rF  rX  r4  r­  r¬  r®  r±  rS  r©  rÑ   rs  rŠ  r  r¯  r  rŒ  r°  r‹  rH  r²  r«  r³  r´  rµ  r¶  r7   r  rN   rN   rR   Úplot_vector_source_estimates	
  sN    h   ÿ                    ör   é   ©rz  r   r¸   rs  ©çffffffæ?r$  r$  ©Úconer�  ©r   r  c           0         s  ddl m} ddlm} ddddd	d
ddddddddddg‰ddg‰t|ttfƒrht‡fdd„|D ƒƒsptdƒ‚t|tƒs€|g}|dk	r˜t|tƒs˜|g}ˆ dkr¦t	ƒ ‰ t
ˆƒ‰‡fdd„tt|ƒƒD ƒ‰| d d ‰| d d }tjˆ|f }| d d }| d d }tj||f }|	�r4| d d }| d d }n| d d }| d d }tj|ˆjd | f }|d 9 }‡fd!d„|D ƒ}t t |¡ ¡ ¡}||d"|
d#�}|jf |dd…df |dd…df |dd…d$f |||d%d%|d&œ	|—Ž | |¡}| ¡  | d'¡}t
ˆ ƒ‰ t d(t|ƒ ¡ |dk	�rP‡ fd)d„tt t |¡ ¡ ¡jƒD ƒ‰ t|ƒD �]^\} ‰‡fd*d„t|ƒD ƒ}!t|!ƒdk}"|dk�r˜tˆ ƒ}#n ˆ ||!d  ||!d  ˆk  }#|"�rÆ|d n|d }$|"�rÜ|d n|d }%t|%tjttfƒ�rt|ƒt|%ƒk�r|%|  }%|ˆ \}&}'}(|ˆ \})}*}+|j|&|'|(|)|*|+t|#ƒ|$|%d+�	 |!D ]b},||, }-|-ˆk}.t |.¡dk�szt ‚ˆ|, }/|j!d,||, j" d-||, j#|.  ¡  |#||/d.� �qR�qX|j$d/d0d1� |j%d2d0d1� |
dk	�rê| &|
¡ t'|ƒ | (¡  | )¡ S )3a2  Plot source estimates obtained with sparse solver.

    Active dipoles are represented in a "Glass" brain.
    If the same source is active in multiple source estimates it is
    displayed with a sphere otherwise with a cone in 3D.

    Parameters
    ----------
    src : dict
        The source space.
    stcs : instance of SourceEstimate or list of instances of SourceEstimate
        The source estimates.
    colors : list
        List of colors.
    linewidth : int
        Line width in 2D plot.
    fontsize : int
        Font size.
    bgcolor : tuple of length 3
        Background color in 3D.
    opacity : float in [0, 1]
        Opacity of brain mesh.
    brain_color : tuple of length 3
        Brain color.
    show : bool
        Show figures if True.
    high_resolution : bool
        If True, plot on the original (non-downsampled) cortical mesh.
    fig_name : str
        PyVista figure name.
    fig_number : int
        Matplotlib figure number.
    labels : ndarray or list of ndarray
        Labels to show sources in clusters. Sources with the same
        label and the waveforms within each cluster are presented in
        the same color. labels should be a list of ndarrays when
        stcs is a list ie. one label for each stc.
    modes : list
        Should be a list, with each entry being ``'cone'`` or ``'sphere'``
        to specify how the dipoles should be shown.
        The pivot for the glyphs in ``'cone'`` mode is always the tail
        whereas the pivot in ``'sphere'`` mode is the center.
    scale_factors : list
        List of floating point scale factors for the markers.
    %(verbose)s
    **kwargs : kwargs
        Keyword arguments to pass to renderer.mesh.

    Returns
    -------
    surface : instance of Figure3D
        The 3D figure containing the triangular mesh surface.
    r   Nr   r  )Úsolidr(  )Údashedr)  )Údottedr*  )Údashdotr+  )zloosely dotted)r   )r   rx   )r*  ©r   )r   r   )zdensely dottedr,  )zloosely dashed)r   )rq  rx   )r)  )r   )rq  rq  )zdensely dashed)r   )rq  r   )zloosely dashdotted)r   )r\   rx   r   rx   )Z
dashdotted)r   )r\   rq  r   rq  )zdensely dashdotted)r   )r\   r   r   r   )Zdashdotdotted)r   )r\   rq  r   rq  r   rq  )zloosely dashdotdotted)r   )r\   rx   r   rx   r   rx   )zdensely dashdotdotted)r   )r\   r   r   r   r   r   r&  r�  c                 3   s   | ]}|ˆ kV  qd S r½  rN   )rP   rq   )Úknown_modesrN   rR   rp  Ø
  s     z/plot_sparse_source_estimates.<locals>.<genexpr>z6mode must be a list containing only "cone" or "sphere"c                    s   g | ]}t ˆ ƒd  ‘qS )r   ©Únext©rP   rj  )Ú
linestylesrN   rR   rS   ä
  s     z0plot_sparse_source_estimates.<locals>.<listcomp>r‰   r#  r·   ru  éª   c                    s(   g | ] }t j|jˆ jd  |j f ‘qS )r   )r]   Úr_Z	lh_vertnorÊ   Z	rh_vertno)rP   rL  )Ú	lh_pointsrN   rR   rS   ú
  s   ÿr  )r  r  r-  r   T)	r²   r³   rm   r¹   r”   r,  r|  Zshadingr&  éo   z"Total number of active sources: %dc                    s   g | ]}t ˆ ƒ‘qS rN   r.  r0  )rJ  rN   rR   rS     s     c                    s   g | ]\}}ˆ |kr|‘qS rN   rN   )rP   rn   rt  )r‰  rN   rR   rS     s      )	r²   r³   rm   rˆ  r‰  rŠ  r”   rq   rÿ   rŠ   ç    eÍÍA)ÚcÚ	linewidthÚ	linestylez	Time (ms)r!  )ÚfontsizezSource amplitude (nAm))*rÁ   rÂ   r6  r  rÄ   rÅ   rÆ   rf   rË   rD   r   rÕ   r|   r]   r3  rÊ   r  rÌ   rÜ   rM  rÑ   ZclfZadd_subplotr5   r†   r  rÇ   r/  rå  r¦  rA   Úsumr’  rÓ   r8  r  Ú
set_xlabelÚ
set_ylabelrf  rE   rì   rD  )0r«  ZstcsrJ  r8  r:  r  r,  Zbrain_colorrì   Zhigh_resolutionZfig_nameZ
fig_numberrò   ÚmodesZscale_factorsr7   rý   rî   r  Z	rh_pointsrh   Z
lh_normalsZ
rh_normalsr&  Zuse_lh_facesZuse_rh_facesZ	use_facesZvertnosZunique_vertnosr$  rÀ   rø   rö   ÚindZ	is_commonr7  rq   r±  r²   r³   rm   ÚnxÚnyZnzrn   rt  r  r9  rN   )rJ  r-  r4  r1  r‰  rR   Úplot_sparse_source_estimates�
  sØ    >ðÿ

ÿ      ýý



ÿ

 ÿ þ$  ÿ

rB  Ú	orthoviewrT  ÚgofrW   rf  r_  )Úhighlight_colorrÀ   r–   Úhead_sourcerð   r  r7   c                C   sÊ   t |tdƒ t |tdƒ td|dƒ |dkr8t|dd�}t||||||	|
|||d�
}t| ƒ} |dkr~t| f|||d	œ|—Ž}nH|d
kr t| f|||dœ|—Ž}n&|dks°t|ƒ‚t	| ||||||d�}|S )ug  Plot dipole locations.

    If mode is set to 'arrow' or 'sphere', only the location of the first
    time point of each dipole is shown else use the show_all parameter.

    Parameters
    ----------
    dipoles : list of instances of Dipole | Dipole
        The dipoles to plot.
    trans : dict | None
        The mri to head trans.
        Can be None with mode set to '3d'.
    subject : str |Â None
        The FreeSurfer subject name (will be used to set the FreeSurfer
        environment variable ``SUBJECT``).
        Can be ``None`` with mode set to ``'3d'``.
    %(subjects_dir)s
    mode : str
        Can be:

        ``'arrow'`` or ``'sphere'``
            Plot in 3D mode using PyVista with the given glyph type.
        ``'orthoview'``
            Plot in matplotlib ``Axes3D`` using matplotlib with MRI slices
            shown on the sides of a cube, with the dipole(s) shown as arrows
            extending outward from a dot (i.e., the arrows pivot on the tail).
        ``'outlines'``
            Plot in matplotlib ``Axes`` using a quiver of arrows for the
            dipoles in three axes (axial, coronal, and sagittal views),
            with the arrow pivoting in the middle of the arrow.

        .. versionchanged:: 1.1
           Added support for ``'outlines'``.
    coord_frame : str
        Coordinate frame to use: 'head' or 'mri'. Can also be 'mri_rotated'
        when mode equals ``'outlines'``. Defaults to 'mri'.

        .. versionadded:: 0.14.0
        .. versionchanged:: 1.1
           Added support for ``'mri_rotated'``.
    idx : int | 'gof' | 'amplitude'
        Index of the initially plotted dipole. Can also be 'gof' to plot the
        dipole with highest goodness of fit value or 'amplitude' to plot the
        dipole with the highest amplitude. The dipoles can also be browsed
        through using up/down arrow keys or mouse scroll. Defaults to 'gof'.
        Only used if mode equals 'orthoview'.

        .. versionadded:: 0.14.0
    show_all : bool
        Whether to always plot all the dipoles. If ``True`` (default), the
        active dipole is plotted as a red dot and its location determines the
        shown MRI slices. The non-active dipoles are plotted as small blue
        dots. If ``False``, only the active dipole is plotted.
        Only used if ``mode='orthoview'``.

        .. versionadded:: 0.14.0
    ax : instance of matplotlib Axes3D | list of matplotlib Axes | None
        Axes to plot into. If None (default), axes will be created.
        If mode equals ``'orthoview'``, must be a single ``Axes3D``.
        If mode equals ``'outlines'``, must be a list of three ``Axes``.

        .. versionadded:: 0.14.0
    block : bool
        Whether to halt program execution until the figure is closed. Defaults
        to False.
        Only used if mode equals 'orthoview'.

        .. versionadded:: 0.14.0
    show : bool
        Show figure if True. Defaults to True.
        Only used if mode equals 'orthoview'.
    scale : float
        The scale (size in meters) of the dipoles if ``mode`` is not
        ``'orthoview'``. The default is 0.03 when mode is ``'outlines'`` and
        0.005 otherwise.
    color : tuple
        The color of the dipoles.
        The default (None) will use ``'y'`` if mode is ``'orthoview'`` and
        ``show_all`` is True, else 'r'. Can also be a list of colors to use
        when mode is ``'outlines'``.

        .. versionchanged:: 0.19.0
           Color is now passed in orthoview mode.
    highlight_color : color
        The highlight color. Only used in orthoview mode with
        ``show_all=True``.

        .. versionadded:: 0.19.0
    fig : instance of Figure3D | None
        3D figure in which to plot the alignment.
        If ``None``, creates a new 600x600 pixel figure with black background.
        Only used when mode is ``'arrow'`` or ``'sphere'``.

        .. versionadded:: 0.19.0
    title : str | None
        The title of the figure if ``mode='orthoview'`` (ignored for all other
        modes). If ``None``, dipole number and its properties (amplitude,
        orientation etc.) will be shown. Defaults to ``None``.

        .. versionadded:: 0.21.0
    %(head_source)s
        Only used when mode equals ``'outlines'``.

        .. versionadded:: 1.1
    surf : str | None
        Brain surface to show outlines for, can be ``'white'``, ``'pial'``, or
        ``None``. Only used when mode is ``'outlines'``.

        .. versionadded:: 1.1
    width : float | None
        Width of the matplotlib quiver arrow, see
        :meth:`matplotlib:matplotlib.axes.Axes.quiver`. If None (default),
        when mode is ``'outlines'`` 0.015 will be used, and when mode is
        ``'orthoview'`` the matplotlib default is used.
    %(verbose)s

    Returns
    -------
    fig : instance of Figure3D or matplotlib.figure.Figure
        The PyVista figure or matplotlib Figure.

    Notes
    -----
    .. versionadded:: 0.9.0
    rq   rM   )rC  ÚoutlinesrØ  r�  )rC  rG  TrU  )
rv   r¨  r©  rM   rø   Úblockrì   r”   r–   r  rC  )rö   Úshow_allrE  rG  )rF  rð   rÿ   )rØ  r�  )rv   rM   r”   rÀ   rÿ   rq   )
r?   rR  r@   r4   rÃ   r   Ú_plot_dipole_mri_orthoviewr	   r’  r
   )Zdipolesrv   r¨  r©  rq   rM   rö   rI  rø   rH  rì   rÿ   r”   rE  rÀ   r–   rF  rð   r  r7   rý   rN   rN   rR   Úplot_dipole_locations<  sd            ýÿ þþÿ  ÿÿ     þrK  c                 C   s2  ddl m} ddlm} ddlm} | dkr4tdƒ‚t||ƒrb| ¡ }t	dd	„ | 
¡ D ƒŽ \}}n|t||ƒr’d
d	„ |d D ƒ}dd	„ |d D ƒ}nLt|tƒrÖtdd„ | ¡ D ƒƒsºtdƒ‚t	dd	„ | 
¡ D ƒŽ \}}ntdƒ‚|| dd�}	t |¡}|	j||d�}
|dk�r|
 d¡ |	 ¡ }|
 d¡ |
j|fS )aÌ  Take a snapshot of a PyVista Scene and project channels onto 2d coords.

    Note that this will take the raw values for 3d coordinates of each channel,
    without applying any transforms. If brain images are flipped up/dn upon
    using `~matplotlib.pyplot.imshow`, check your matplotlib backend as this
    behavior changes.

    Parameters
    ----------
    fig : instance of Figure3D
        The figure on which you've plotted electrodes using
        :func:`mne.viz.plot_alignment`.
    montage : instance of DigMontage or Info | dict
        The digital montage for the electrodes plotted in the scene. If
        :class:`~mne.Info`, channel positions will be pulled from the ``loc``
        field of ``chs``. dict should have ch:xyz mappings.
    hide_sensors : bool
        Whether to remove the spheres in the scene before taking a snapshot.
        The sensors will always be shown in the final figure. If you want an
        image of just the brain, use :class:`mne.viz.Brain` instead.

    Returns
    -------
    xy : array, shape (n_channels, 2)
        The 2d location of each channel on the image of the current scene view.
    im : array, shape (m, n, 3)
        The screenshot of the current scene view.
    r   )Ú
DigMontage)ÚInfor   r  NzThe figure must have a scenec                 S   s   g | ]\}}||f‘qS rN   rN   ©rP   ÚichZixyzrN   rN   rR   rS      s     z*snapshot_brain_montage.<locals>.<listcomp>c                 S   s   g | ]}|d  dd… ‘qS )r„   Nr\   rN   ©rP   rO  rN   rN   rR   rS     s     rƒ   c                 S   s   g | ]}|d  ‘qS )r  rN   rP  rN   rN   rR   rS     s     c                 s   s   | ]}t |ƒd kV  qdS )r\   Nr{   r™   rN   rN   rR   rp    s     z)snapshot_brain_montage.<locals>.<genexpr>z(All electrode positions must be length 3c                 S   s   g | ]\}}||f‘qS rN   rN   rN  rN   rN   rR   rS     s     z>montage must be an instance of `DigMontage`, `Info`, or `dict`T)rì   )r�   r   F)r¾  rL  ræ  rM  r6  r  rË   rÄ   Z_get_ch_posrÒ   r�  rÃ   rf   r3  rÁ  r]   ZvstackÚprojectÚvisibleZ
screenshotZxy)rÀ   ZmontageZhide_sensorsrL  rM  r  rƒ   r   r�   r$  ZprojZimrN   rN   rR   Úsnapshot_brain_montageÚ  s0    






rS  rt   c                 C   sx  ddl m} ddlm} tƒ s&tdƒ‚td|ddgƒ |dkrLt | j	¡}n(|d	krht t 
| j¡¡}nt|d
dƒ}t| ||||ƒ\}}}}t|ƒ}|d }|dkr¾|jdtdd�d�\}}nt||dddd� | ¡ }tjt | ||¡t | ||¡dd�\}}|||| ||||||||
|||dœ}tf |Ž |jddd� tt|d�}|j d|¡ |j d|¡ t|	|d� |S )z)Plot dipoles on top of MRI slices in 3-D.r   Nr¨   zThis function requires nibabel.rM   rt   rT  rD  Ú	amplituderö   z%an int or one of ["gof", "amplitude"]r   r   rª   r«   r¬   rø   r©   zwhen mode is "orthoview"rŸ  Zij)Zindexing)rø   r  rö   ÚdipoleÚvoxÚgridxÚgridyr  rM   rI  rê   r”   rE  r–   r  r¾   itÿÿÿrq  rx  Zscroll_eventry  )rH  )rÁ   rÂ   rß   r©   r9   r  r@   r]   rõ  rD  r9  rT  r>   Ú_get_dipole_locr|   rÐ   rÃ   r?   Ú
get_figureZmeshgridrš  Ú_plot_dipoleré   r   Ú_dipole_changedrN  r‰  rE   )rU  rv   r¨  r©  rM   rö   rI  rø   rH  rì   r”   rE  r–   r  rî   r©   rV  r  rê   r  ÚdimsÚddrÀ   rW  rX  rQ  Zcallback_funcrN   rN   rR   rJ    sb        ÿ
ÿ ÿ
      û
rJ  r€   i€ÿÿÿr\   )r;  r;  r;  c                 C   sJ  ddl }ddlm} td|ddgƒ t|dd�}t ||dd	¡}| |¡}t|d
d�\}	}
}}
}
t	|ddd�d  
¡ }|d dd…df  d9  < ~| jd }| j}tj |	d jtj¡j}|dkrØt||ddƒd }n"t||ƒ}t|| jdd�}|d }t t ||¡|	d ¡}||t|fdd�}ttj t¡|ƒ}t|ƒ}||||fS )z*Get the dipole locations and orientations.r   N)Úresample_from_torM   rt   rT  Tr©  rÔ  rÝ  )r  )rr  Útorv   r\   rw   rŠ   rÜ  Fr  rá  )r†  Znibabel.processingr_  r@   r4   r—  r=  rÑ  r   r*   rˆ   rê   r  r]   rø  ZsolverÏ   Ú
RAS_AFFINEr)   r'   r>  Ú	RAS_SHAPErù  r   )rU  rv   r¨  r©  rM   rŽ  r_  Zt1_fnameÚt1Z	vox_ras_trj  Z	mri_ras_tr²  rê   r  ZupsampZ
dest_ras_trÊ  rV  Zt1_datarN   rN   rR   rY  P  sF    ÿ
 ÿ
	   ÿÿ
rY  c           #   
   C   sæ  ddl m} t || ¡ t¡\}}}|| }|dd…|f }|dd…dd…|f }|| }|dkrp|	rldnd}t t|dd�¡}t t|ddd�¡}|	rätj|tj	 t
|ƒdd	�}|||< t d
t
|ƒ¡}d||< t t
|ƒ¡}n|}d}|}t |¡}|
}| j||df ||df ||df d||d� t |||df |¡}t |||df |¡}t |||df |¡}| j|t ||df t
|ƒ¡||df dd|d� | jt ||df t
|ƒ¡|||df dd|d� | jt ||df t
|ƒ¡t ||df t
|ƒ¡|dd|d� td|dd�}|dk	�r0||d< | j||df ||df ||df |d |d |d f|Ž t t
|ƒd t
|ƒd g¡}| j| | |d� | j||||ddddd� | j||||ddddd� | j||||ddddd� t t|| ƒt|ƒ gd ¡} tdƒD ]"}!|d dg| |!|!|!d gf< �q| j| jddi|—Ž |d k�r\d!nd"}"|dk�r®d#|d t
|jƒ|j| |j| |j| d$ |"f d%t|| ƒ  }|  ¡  |¡ |  d¡ |  d¡ |  d¡ |  ¡  dS )&zPlot dipoles.r   Nr³   rW   Tr'  rE  )r-  rº   r~   rq  r†  r   r   )ZxsZysÚzsr�   r»   Ú	facecolorrž   )rd  r�   r9  r”   é2   r´   )r¶   r”   rµ   r  g       Àr¼   ra  r²   ÚgrayrŸ   )r$  Zzdirr¯   r�   rº   rm   r\   rº   rn  rt   ZHeadZMRIz2Dipole #%s / %s @ %.3fs, GOF: %.1f%%, %.1fnAm
%s: r6  z(%0.1f, %0.1f, %0.1f) mm)!rÁ   rÂ   r]   r@  rA  rú  r^   rA   râ   rà   r|   rÚ   rÍ   rå   rš  rÓ   rÃ   rã   rÔ   ZcontourfrÅ   rÕ   rÏ   r8  rD  rT  rÆ   rZ  Zsuptitler<  r=  Z
set_zlabelrg  )#rø   r  rV  rö   rU  rW  rX  r  rM   rI  rê   r”   rE  r–   r  rî   ZxidxZyidxZzidxZxsliceZysliceZzslicerJ  r  rR  r$  r�   ZxxÚyyÚzzZq_kwargsr]  r  rš   Zcoord_frame_namerN   rN   rR   r[  ‚  sº      ÿ

   ÿ( ÿ( ÿ   þ
6ÿ  ÿ  ÿ  ÿ  
    þý


r[  c                 C   s´   | j dk	rH| j dkr&|d  d7  < qt| j dkrB|d  d8  < qtdS n,| jdkrd|d  d7  < n|d  d8  < ttd|d ƒt|d jƒd ƒ|d< |d  ¡  tf |Ž dS )	z'Handle dipole plotter scroll/key event.NÚuprö   r   Zdownr   rU  rø   )ru  rR  rÍ   rÎ   r|   rê   rc  r[  )rP  rQ  rN   rN   rR   r\  Ò  s    



&r\  r  r„  c                 C   s¸   ddl m} ddlm} t|||ƒ}	t|	ƒ}
t|	ƒ\}}~	||d |d d�}|| |||
||d�}|jj|d� |j	 
d¡ |j d	¡ d
d|dkr˜dndfD ]}| j|  
d¡ qž|S )a'  Plot a colorbar that corresponds to a brain activation map.

    Parameters
    ----------
    ax : instance of Axes
        The Axes to plot into.
    %(clim)s
    %(colormap)s
    %(transparent)s
    orientation : str
        Orientation of the colorbar, can be "vertical" or "horizontal".
    label : str
        The colorbar label.
    bgcolor : color
        The color behind the colorbar (for alpha blending).

    Returns
    -------
    cbar : instance of ColorbarBase
        The colorbar.

    Notes
    -----
    .. versionadded:: 0.19
    r   )ÚColorbarBaser¥   r   )r/  r  )r¯   r°   rA  rw  rƒ  )re  FTrõ   r|  r¥  r{  rK  )Zmatplotlib.colorbarrk  rÞ   r¦   r9  rC  r@  rø   rÔ   r  r  Zset_frame_onZspines)rø   r  r*  r4  rƒ  rw  r  rk  r¦   r8  rA  r›   r°   Zcbarru  rN   rN   rR   Úplot_brain_colorbarä  s&    
 ÿÿrl  c                   @   s>   e Zd ZU ee ed< ee ed< ee ed< ee ed< dS )Ú_3d_OptionsÚ	antialiasÚdepth_peelingÚsmooth_shadingÚmulti_samplesN)Ú__name__Ú
__module__Ú__qualname__r   r¤  Ú__annotations__rú  rN   rN   rN   rR   rm    s   
rm  ©rn  ro  rp  rq  ÚtrueÚ4)rq  c                C   sL   | dk	rt | ƒt_|dk	r$t |ƒt_|dk	r6t |ƒt_|dk	rHt|ƒt_dS )ae  Set 3D rendering options.

    Parameters
    ----------
    antialias : bool | None
        If bool, whether to enable or disable full-screen anti-aliasing.
        False is useful when renderers have problems (such as software
        MESA renderers). If None, use the default setting. This option
        can also be controlled using an environment variable, e.g.,
        ``MNE_3D_OPTION_ANTIALIAS=false``.
    depth_peeling : bool | None
        If bool, whether to enable or disable accurate transparency.
        False is useful when renderers have problems (for instance
        while X forwarding on remote servers). If None, use the default
        setting. This option can also be controlled using an environment
        variable, e.g., ``MNE_3D_OPTION_DEPTH_PEELING=false``.
    smooth_shading : bool | None
        If bool, whether to enable or disable smooth color transitions
        between polygons. False is useful on certain configurations
        where this type of shading is not supported or for performance
        reasons. This option can also be controlled using an environment
        variable, e.g., ``MNE_3D_OPTION_SMOOTH_SHADING=false``.
    multi_samples : int
        Number of multi-samples. Should be 1 for MESA for volumetric rendering
        to work properly.

        .. versionadded:: 1.1

    Notes
    -----
    .. versionadded:: 0.21.0
    N)r¤  Ú_3d_optionsrn  ro  rp  rú  rq  rv  rN   rN   rR   Úset_3d_options+  s    "


rz  c                 C   s^   t | ddƒ tt| ƒ}|d krZtt| ƒ}td|  ¡ › �|ƒ}| dkrNt|ƒ}n| ¡ dk}|S )NrR  ru  ZMNE_3D_OPTION_rq  rw  )r?   Úgetattrry  Ú_3d_defaultr=   Úupperrú  Úlower)ru  ÚoptÚdefault_valuerN   rN   rR   Ú_get_3d_optionW  s    


r�  )N)rk   rl   rm   TNNrn   N)Nr  NNNr  )NNNNrO  rO  NrP  NFTNFNTTFTNr  N)TN)N)r…  N)rŸ   NFFNNN)FFNN)
r   rƒ  FFFFNFNN)	r…  FFFNNNNF)r   r  r   )r  FFFNNN)r  T)Nra  rc  rO  rO  rx   Nri  rO  NNr»   rj  rk  FTT)TFTF)Nra  rc  rO  rO  rx   TrŽ   rO  NNrO  TrO  r¤  rw  rj  NNr»   rO  rk  NrO  NrŽ   r¥  NNN)NNrÓ  rÔ  TrO  rO  NTNNN)NN)Nrc  r,  rO  rx   Nr  NrŽ   NrO  NNr  TrO  r¤  rw  rj  NNr»   rO  NrŽ   r¥  NNN)Nr   r!  r"  rs  r#  TFNNNr%  r'  N)NNNrC  rT  rD  TNFTNN)T)Nrt   rD  TNFTNrW   NN)rO  Tr¥  r  r„  )NNN)¡Ú__doc__Ú	itertoolsr   Úos.pathÚpathr—  rÂ  Úcollections.abcr   Ú	functoolsr   Údataclassesr   Útypingr   Únumpyr]   Z_dipoler   r	   r
   rv  r   Úfixesr   r   Z_freesurferr   r   r   r   Úior   Zio.pickr   r   r   r   r   Zio.constantsr   Zio.meas_infor   r   Zsource_spacer   r   r   r   r    r.  r!   r"   r#   r$   r%   r&   Z
transformsr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   Úutilsr4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   Z_3d_overlayrB   rC   rD   rE   rF   rG   rH   rl  rI   rJ   rK   r  ZFIFFV_POINT_LPAZFIFFV_POINT_NASIONZFIFFV_POINT_RPAr_   rj   r   rè   rN  r»  r�  rÔ  r›  rœ  rž  r¡  r¢  rŸ  rõ  rÿ  rï  r   r§  r£  r  rÎ  r*  r9  r:  r@  rC  rI  rU  rh  r“  rœ  r£  r¹  r¸  rÁ  rÉ  rÎ  rÏ  rÒ  r  rÀ  r   rB  rK  rS  rJ  rß  ra  rb  rY  r[  r\  rl  rm  ry  r|  rz  r�  rN   rN   rN   rR   Ú<module>   s~   <@ ÿ
         þ T	        ÿ þ                               û  {*= ÿ
   ÿ
'         þ

    ÿ
              ý
$              ý  ÿ
          ü
^d	
C0                        û
~  ÿ
.                                            õ #z
                û   
                                       òw                  ù ;                ý    û 
?                  ý
2
2P     þ0üüÿ,