U
    f»{j™  ã                   @   s:  d Z ddlmZ ddlmZ ddlZddlmZm	Z	 ddl
mZmZmZmZmZ ddlmZmZmZ dd	lmZ d
dl
mZmZmZmZmZmZmZmZmZ ed<dd„ƒZdd„ Z d=dd„Z!d>dd„Z"dd„ Z#dd„ Z$dd„ Z%d?d$d%„Z&d@d'd(„Z'dAd)d*„Z(dBd+d,„Z)dCd-d.„Z*dDd/d0„Z+dEd4d5„Z,d6d7„ Z-dFd:d;„Z.dS )Gz<Functions to plot M/EEG data on topo (one axes per channel).é    )Údeepcopy)ÚpartialNé   )Úchannel_typeÚ
pick_types)Ú_clean_namesÚ_check_optionÚBunchÚfill_docÚ_to_rgb)Ú_merge_ch_dataÚ_pair_grad_sensorsÚfind_layout)Ú_handle_defaulté   )	Ú_check_delayed_sspÚ_draw_proj_checkboxÚadd_background_imageÚplt_showÚ_setup_vmin_vmaxÚDraggableColorbarÚ_setup_ax_spinesÚ
_check_covÚ_plot_masked_imageÚkFc	           	      C   s   t | ||||||||d�	S )a³  Create iterator over channel positions.

    This function returns a generator that unpacks into
    a series of matplotlib axis objects and data / channel
    indices, both corresponding to the sensor positions
    of the related layout passed or inferred from the channel info.
    Hence, this enables convenient topography plot customization.

    Parameters
    ----------
    %(info_not_none)s
    layout : instance of mne.channels.Layout | None
        The layout to use. If None, layout will be guessed.
    on_pick : callable | None
        The callback function to be invoked on clicking one
        of the axes. Is supposed to instantiate the following
        API: ``function(axis, channel_index)``.
    fig : matplotlib.figure.Figure | None
        The figure object to be considered. If None, a new
        figure will be created.
    fig_facecolor : color
        The figure face color. Defaults to black.
    axis_facecolor : color
        The axis face color. Defaults to black.
    axis_spinecolor : color
        The axis spine color. Defaults to black. In other words,
        the color of the axis' edge lines.
    layout_scale : float | None
        Scaling factor for adjusting the relative size of the layout
        on the canvas. If None, nothing will be scaled.
    legend : bool
        If True, an additional axis is created in the bottom right corner
        that can be used to, e.g., construct a legend. The index of this
        axis will be -1.

    Returns
    -------
    gen : generator
        A generator that can be unpacked into:

        ax : matplotlib.axis.Axis
            The current axis of the topo plot.
        ch_dx : int
            The related channel index.
    )Úlegend)Ú_iter_topography)	ÚinfoÚlayoutÚon_pickÚfigÚfig_facecolorÚaxis_facecolorÚaxis_spinecolorÚlayout_scaler   © r%   úK/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/viz/topo.pyÚiter_topography   s    1  þr'   c           
      C   sÞ   ddl m} | dd…df  ¡ | dd…df  ¡  }}d}| D ]d}|d |  kod|d |d  kn  }|d |  koŒ|d |d  kn  }|r>|r>d} q¤q>|r¸|d|d  7 }| d	dd…f \}}	| ||||	g¡S )
z&Add a legend axis to the bottom right.r   Nr   Fr   é   Tg333333ó?éÿÿÿÿ)Úmatplotlib.pyplotÚpyplotÚmaxÚminÚaxes)
ÚposÚpltÚleftÚbottomÚoverlapsÚ_posZ	h_overlapZ	v_overlapZwidZheir%   r%   r&   Ú_legend_axisO   s    *((r5   c              
   #   s  ddl m}m} |dkr | ¡ }ddd„}ddd„}| |¡ |dkrNt| ƒ}|dk	rptt|d�}|j 	d	|¡ |j
 ¡ }|rš|dd…dd
…f  |9  < t| d ƒ‰ ‡ fdd„t|jƒD ƒ}|�r|
dkrê| ddddg¡}| d¡ n|
}t|||jd�|_|jddgddgd� tƒ }|D ]Ö\}}ˆ  |¡}|�sÐ| || ¡}|j |¡ |j ¡ D ]}| |¡ �q\|�s¤|jg g d� | ¡ | ¡  D ]}| d¡ �q’||_||_||_t||d�|_||fV  n$t||| tƒ |||d�}|  |¡ �q |�s|�rt!|ƒ}|dfV  |�rþ||_"t# $|dd…dd
…f |dd…dd
…f |dd…d
d…f ddg  |dd…dd
…f |dd…d
d…f  |dd…dd
…f |dd…d
d…f ddg  gddd
g¡}|	�sæ| %|j&|||dd�¡ |D ]}||jfV  �qêdS )a'  Iterate over topography.

    Has the same parameters as iter_topography, plus:

    unified : bool
        If False (default), multiple matplotlib axes will be used.
        If True, a single axis will be constructed. The former is
        useful for custom plotting, the latter for speed.
    r   )r+   ÚcollectionsNc                 S   s¬   t  | |g¡|dd…dd…f  }t  |dkjdd�¡d }t|ƒdkr”|t j||dd…f d dd� ¡  }||dd…f ||dd…f k  ¡ }nd}|r¨d||  S dS )	ú1Update status bar with channel name under cursor.Nr   r   r   )ÚaxisFú%s (click to magnify)zNo channel here)ÚnpÚarrayÚwhereÚallÚlenÚsumÚargmin)ÚxÚyr/   Úch_namesZpdistZpindZclosestZin_boxr%   r%   r&   Úformat_coord_unifieds   s    "&&ÿÿz._iter_topography.<locals>.format_coord_unifiedc                 S   s   d| S )r7   r9   r%   )rA   rB   Úch_namer%   r%   r&   Úformat_coord_multiaxis‚   s    z0_iter_topography.<locals>.format_coord_multiaxis)Ú	show_funcZbutton_press_eventr   rC   c                    s    g | ]\}}|ˆ kr||f‘qS r%   r%   )Ú.0rA   rB   ©rC   r%   r&   Ú
<listcomp>“   s      z$_iter_topography.<locals>.<listcomp>r   Úoff)r/   rC   )ÚxlimÚylim)ZxticklabelsÚyticklabelsF)rE   )Úaxr/   Ú
data_linesÚ_mne_ch_nameÚ_mne_ch_idxÚ_mne_ax_face_colorr)   ç      ð?)Z	facecolorZ	edgecolorÚ	linewidth)NN)N)'Ú
matplotlibr+   r6   ÚfigureÚset_facecolorr   r   Ú_plot_topo_onpickÚcanvasZmpl_connectr/   Úcopyr   Ú	enumerateÚnamesr.   r8   Úformat_coordÚsetÚlistÚindexÚpatchÚspinesÚvaluesÚ	set_colorZget_xticklinesZget_yticklinesZset_visiblerQ   rR   rS   r	   Úappendr5   Ú_mne_axsr:   Z	transposeZadd_collectionZPolyCollection)r   r   r   r    r!   r"   r#   r$   ÚunifiedÚimgr.   r   r0   r6   rD   rF   Úcallbackr/   Ziter_chZunder_axZaxsÚidxÚnameÚch_idxrO   ZspineZtickZvertsr%   rI   r&   r   a   sŠ    



ÿ
 þ
.&.ýü  þr   ÚnoneÚRdBu_rÚwc           !         s  ddl m} |jdkrŠt|ƒ}|jdd…dd…f  |jdd…dd…f  d¡8  < |jdd…dd…f  |jdd…dd…f  d¡  < |d |d  }}|dkr¨|n|}t||||||||d�}|dkrò| ¡ }| 	ddd	d
g¡}| 
|¡ n|j}|�r\|jj|| ||¡d�}| t ||¡¡ |j||ddddd�}| |jj	d¡}|j||d� | d¡ t| |||||	|
||||d�}|D ]b\}}|jdk�rÎ|dk	�rÎdddœt| |ƒ ‰ ‡ fdd„|D ƒ} n|} |||||||| d� �qˆ|dk	�r|jdd
||ddd� |S )zPlot on sensor layout.r   NZcustomr   r)   )ÚtminÚtmaxÚvminÚvmaxrM   Úx_labelÚy_labelg¸…ëQ¸Ž?gš™™™™™™?g
×£p=
ï?çffffffî?)ÚcmapZnormg333333³?ç      à?)r)   ry   )rO   ÚpadZfractionÚshrinkÚanchorrN   ©ÚcolorrK   )
r   r   r    r$   r#   r"   r!   rh   ri   r.   zVectorview-allr   ©ÚmagÚgradc                    s    g | ]}t |ƒr|ˆ  n|‘qS r%   )Ú_check_vlim©rH   Úv©Z	this_typer%   r&   rJ   õ   s     z_plot_topo.<locals>.<listcomp>)rq   rr   rs   rt   rM   g¸…ëQ¸ž?é   Útop)r~   ZfontsizeÚva)r*   r+   Úkindr   r/   r-   r,   r   rW   r.   rX   ÚcmZScalarMappableZ	NormalizeZ	set_arrayr:   ÚlinspaceÚcolorbarZgetprO   Zsetpr8   r   r   Zfigtext)!r   ÚtimesrG   Ú
click_funcr   rs   rt   rM   rŒ   Úborderr"   r!   rx   r$   Útitleru   rv   Ú
font_colorrh   ri   r.   r0   rq   rr   r   r    ÚsmÚcbZ	cb_yticksZmy_topo_plotrO   rm   Úylim_r%   r…   r&   Ú
_plot_topoÈ   sb    
66
  þÿ
   û ÿ

r•   c              
   C   s  | j }ddlm} zØt|dƒr”| j| j }}|jD ]X}||jd kr2||jd kr2||jd |jd  kr2||jd |jd  kr2|} q¤q2W dS nt|dƒs¤W dS |j}|j	}| 
d¡\}	}| |j¡ | |¡ |||ƒ t|	d� W n. tk
�r }
 zt|
ƒ ‚ W 5 d}
~
X Y nX dS )	z<Onpick callback that shows a single channel in a new figure.r   Nrg   r   r   r(   rR   )r    )Úinaxesr*   r+   ÚhasattrÚxdataZydatarg   r/   rR   rS   Zsubplotsr�   rQ   rX   r   Ú	ExceptionÚprint)ÚeventrG   Zorig_axr0   rA   rB   rO   rm   Z
face_colorr    Úerrr%   r%   r&   rY     s4    

ÿþ


rY   c                 C   sœ   t |d tttjfƒr.|d d |d d f}| j}|d |d |d   | _|d | j|d   | _|d |d |d   | _|d | j|d   | _	dS )z)Compute scale factors for a unified plot.r   r   r   r(   N)
Ú
isinstanceÚtupler`   r:   Zndarrayr/   Úx_sÚx_tÚy_sÚy_t)ÚbnrL   rM   r/   r%   r%   r&   Ú_compute_ax_scalings)  s    r¤   c                 C   s   t  | ¡ o| dk	S )zCheck the vlim.N)r:   Zisscalar)Zvlimr%   r%   r&   r‚   4  s    r‚   ©ro   TÚautoÚbothÚGreysçš™™™™™¹?c                 C   sà   ddl m} ddlm} td|dddgƒ |\}}tj|||| jd d	�}t| || |||	||||||||d
�\}}|
dk	rˆ|  	|
¡ |dk	rš|  
|¡ |rÎt|tƒr°|j}n|j|| d�}|rÎt||ƒ| _|| |d�| _|S )z1Show time-frequency map as two-dimensional image.r   )r+   )ÚRectangleSelectorÚyscaler¦   ZlinearÚlogr   )Únum)	Zyvalsrx   rs   rt   Ú
mask_styleÚ
mask_alphaÚ	mask_cmapr«   ÚcnormN)ÚmappablerO   )Úonselect)rV   r+   Zmatplotlib.widgetsrª   r   r:   r‹   Úshaper   Ú
set_xlabelÚ
set_ylabelr�   r   ÚcbarrŒ   ZCBZRS)rO   rm   rq   rr   rs   rt   r³   rM   ÚtfrÚfreqru   rv   rŒ   rx   r«   Úmaskr®   r°   r¯   Zis_jointplotr±   r0   rª   Zinteractive_cmapr�   ri   Zt_endr·   r%   r%   r&   Ú_imshow_tfr9  s@              ý



r»   Tc                 C   s˜   t | ||f|	d |	d fƒ | j}| j}| j| j|  | j| j|  | j| j|	d   | j| j|	d   f}| |j|| d| j	|dd|||d�	¡ dS )z/Show multiple tfrs on topo using a single axes.r   r)   Tr¦   Úlower)Úclip_onÚclip_boxÚextentÚaspectÚoriginrs   rt   rx   N)
r¤   rO   rP   r    rŸ   r¢   r¡   rf   Úimshowr/   )r£   rm   rq   rr   rs   rt   r³   rM   r¸   r¹   Úvlineru   rv   rŒ   Úpickerrx   r�   ÚhlinerO   rP   r¿   r%   r%   r&   Ú_imshow_tfr_unified\  s     ÿ    þrÆ   c              	      s  ddl m} d}tˆ|ˆƒD ]L\}}}|sTˆj||ˆ |dd�d }| d¡ d}qˆj||ˆ |d� q‡‡‡‡‡fdd	„‰ ‡ ‡‡fd
d„ˆ_dd„ }dd„ }dˆ_tˆ ¡ ƒ}t	 
|dddg¡}|dk rÐdndˆ_| d|¡ | d|¡ ˆ ¡ \}}tˆg ˆˆ||dd� ˆj |dk�r$dnd¡ ˆjd  |¡ ˆjd  |¡ ˆjd|dd� ˆjd |dd� ˆj |¡ ˆjj |¡ ˆjj |¡ |dk	�r¦ˆ |¡ |dk	�rÖt|tƒ�rÌˆ |ˆ ¡ n
ˆ |¡ |
�rî|j|
|d!d"d#� |�r|j||d!d$d%� |�r| ¡  dS )&z4Show time series on topo split across multiple axes.r   NFT)r~   rÄ   g    eÍÍAr}   c                    s´  ‡fdd„ˆD ƒ}‡‡fdd„t |ƒD ƒ}| ¡ }d|kr^d|kr^|| d¡d | d¡… nd}dˆ|f }|szd	| S ˆ d
kr�dgt|ƒ nˆ ‰ ‡‡ ‡fdd„|D ƒ}	| ¡ }
d|
krÞd|
krÞ|
|
 d¡d |
 d¡… nd‰dt|ƒ t‡fdd„ˆ D ƒƒ }|j ¡ |jj d d }||k }|}|	D ]€\}}}t	 
|ˆ ¡ ¡ }|d|ˆ|f ˆf 7 }|�r–t|ƒdk�rz|nd|d
d… |dd
… f }||�r¦d| nd7 }�q.|S )z1Create status string based on cursor coordinates.c                    s   g | ]}t  |ˆ  ¡ ¡ ‘qS r%   )r:   Úabsr-   )rH   Útvec)rA   r%   r&   rJ   ~  s     z;_plot_timeseries.<locals>._format_coord.<locals>.<listcomp>c                    s$   g | ]\}}|ˆ ˆ d  k r|‘qS )éd   r%   )rH   r   Ztdiff)rr   rq   r%   r&   rJ     s    ÿú(ú)r   Úsz
%6.3f %s: z%s Nothing hereNÚ c                    s"   g | ]}ˆ | ˆ| ˆ| f‘qS r%   r%   )rH   Ún)ÚdataÚlabelsr�   r%   r&   rJ   ˆ  s     é	   c                    s    g | ]}d t ˆ ƒ t |ƒ ‘qS )é   )r>   )rH   Úlabel)Úyunitr%   r&   rJ   �  s   ÿr   g      @z%7.2f %sé
   z%s..%sé   éþÿÿÿz [%s] ú )r\   Z
get_xlabelÚfindr>   Z
get_ylabelr?   rW   Zget_size_inchesZdpir:   rÇ   r@   )rA   rB   rÐ   rO   ZtdiffsZnearbyZxlabelZxunitZtimestrZnearby_dataZylabelÚslenÚ	bar_widthZtrunc_labelsrÌ   Údata_rÓ   rÈ   rk   )rm   rÏ   r�   rr   rq   )rÐ   rA   rÔ   r&   Ú_format_coord{  sH    ÿÿÿÿÿÿÿ
ÿz'_plot_timeseries.<locals>._format_coordc                    s   ˆ | |ˆˆd�S )N)rÐ   rO   r%   )rA   rB   )rÝ   rO   rÐ   r%   r&   Ú<lambda>›  ó    z"_plot_timeseries.<locals>.<lambda>c                 S   sF   | j }|sdS |jdk	r"|j ¡  |j| j|jd�|_|jj ¡  dS )zDraw cursor (vertical line).Nr}   )	r–   Ú_cursorlineÚremoveÚaxvliner˜   Ú_cursorcolorrW   rZ   Údraw©r›   rO   r%   r%   r&   Ú_cursor_vline�  s    

z'_plot_timeseries.<locals>._cursor_vlinec                 S   s0   | j }|jd k	r |j ¡  d |_|jj ¡  d S ©N)r–   rà   rá   rW   rZ   rä   rå   r%   r%   r&   Ú
_rm_cursor§  s
    

z$_plot_timeseries.<locals>._rm_cursori+  iK  ér   é–   ÚwhiteÚblackZmotion_notify_eventZaxes_leave_event)rÅ   rp   r   r2   r1   rA   r§   )r8   ÚcolorsÚwhichrB   rT   ú--©r~   rU   Z	linestylerÕ   )r~   rU   Zzorder)r*   r+   ÚzipÚplotZset_pickradiusr^   rà   r   Zget_facecolorr:   Údotrã   ÚconnectZget_ylimr   rW   rX   rc   re   Ztick_paramsr�   ZxaxisrÓ   Zyaxisrµ   r�   r`   r¶   râ   ZaxhlinerŒ   )rO   rm   rq   rr   rs   rt   rM   rÏ   r~   r�   rÃ   ru   rv   rŒ   rÅ   Úhvline_colorrÐ   r0   Zpicker_flagrÜ   Úcolor_Útimes_Úlineræ   rè   ZfacecolZface_brightnessZyminZymaxr%   )rÝ   rO   rm   rÏ   rÐ   r�   rr   rq   r&   Ú_plot_timeseriesk  sT    
 





ÿrù   c                 C   sÌ  ddl m} |r"tdd„ |D ƒƒrFtdd„ |D ƒƒtdd„ |D ƒƒg}t| ||f|ƒ | j}| j}| j}t	|||	ƒD ]F\}}}| 
|j| j| j|  | j| j||   d|d|d	�d ¡ qt|
rüt |
¡| j | j }
|j|
|d
 |d
 |d  |ddd� |�r<t |¡| j | j }|j||d |d |d  |dd� |dk	�rn|j|d |d d  |d
 |ddd� |dk	�rºt|tƒ�rŒ|| n|}|j|d |d
 |d d  |dddd� |�rÈ| ¡  dS )z6Show multiple time series on topo using a single axes.r   Nc                 s   s   | ]}|d kV  qd S rç   r%   rƒ   r%   r%   r&   Ú	<genexpr>Ü  s     z+_plot_timeseries_unified.<locals>.<genexpr>c                 s   s   | ]}t  |¡V  qd S rç   )r:   r-   ©rH   Údr%   r%   r&   rú   Ý  s     c                 s   s   | ]}t  |¡V  qd S rç   )r:   r,   rû   r%   r%   r&   rú   Ý  s     ry   T)rU   r~   r½   r¾   r   r(   rï   rð   r   )r~   rU   g       @Úcenterr‡   )ZhorizontalalignmentÚverticalalignmentÚrightÚmiddleéZ   )Zhorizontalignmentrþ   Zrotation)r*   r+   Úanyr-   r,   r¤   r/   rP   rO   rñ   rf   rò   r    rŸ   r¢   r¡   r:   r;   ZvlinesZhlinesÚtextr�   r`   rŒ   )r£   rm   rq   rr   rs   rt   rM   rÏ   r~   r�   rÃ   ru   rv   rŒ   rÅ   rõ   r0   r/   rP   rO   rÜ   rö   r÷   r%   r%   r&   Ú_plot_timeseries_unifiedÖ  sV    $    þþ ÿÿ
 ÿ
 þr  c                 C   sæ   ddl m} ddlm} |dd…|dd…f }|dk	rB|| \}}t|
ƒrV|
|j|ƒ}
|
dk	rf||
 }|	dkr~|j||	dd�}| j|||dt|ƒgdd||d|d	d
�	}| 	¡ } |dk	rÀ|  
|¡ |dk	rÒ|  |¡ |râ|j|d� dS )z#Plot erfimage on sensor topography.r   ©ÚndimageNç        ©Úsigmar8   r¦   r¼   TÚnearest©r¿   rÀ   rÁ   rs   rt   rÄ   rx   Úinterpolation)r²   )Úscipyr  r*   r+   Úcallabler�   Úgaussian_filter1drÂ   r>   Zgcarµ   r¶   rŒ   )rO   rm   rq   rr   rs   rt   rM   rÏ   Úepochsr	  ÚorderÚscalingsrÃ   ru   rv   rŒ   rx   Ú
vlim_arrayr  r0   Ú	this_datari   r%   r%   r&   Ú_erfimage_imshowü  s6         þ

r  c                 C   sú   ddl m} t| ||fdt|jƒfƒ | j}| j}| j| j|  | j| j|  | j	| j	| j
t|jƒ  f}|dd…|dd…f }|dkrŒdn|| \}}t|
ƒr¬|
|j|ƒ}
|
dk	r¼||
 }|	dkrÔ|j||	dd�}| |j||dd||d	|d
d�	¡ dS )z-Plot erfimage topography using a single axis.r   r  N)NNr  r  r¦   r¼   Tr
  r  )r  r  r¤   r>   ÚeventsrO   rP   r    rŸ   r¢   r¡   r  r�   r  rf   rÂ   )r£   rm   rq   rr   rs   rt   rM   rÏ   r  r	  r  r  rÃ   ru   rv   rŒ   rx   r  r  rO   rP   r¿   r  r%   r%   r&   Ú_erfimage_imshow_unified  s.     ÿ   ýr  ç=
×£p=î?©r  Úbadsc           6         sú  ddl m} ddlm‰ tˆƒttfkr.ˆg‰tˆˆd jƒ‰ˆdk	r\‡‡fdd„ˆD ƒ‰ndd„ ˆD ƒ‰ˆd j‰ˆd j	‰ t
d|ƒ}t‡ fd	d
„ˆD ƒƒs¦tdƒ‚tˆ ƒ‰ |�r¸tˆdˆd�}tƒ }|ddd… D ]2}ˆd | }|d dd… d |d< | |¡ qÖˆjddd�� |ˆd< tƒ ˆd< W 5 Q R X tƒ }ˆD ]J}t|j| dg ƒ\}}ˆdk�rp||d 9 }||_| tt|ƒƒ¡ �q@|}dg}ˆdk�r¬t
dƒd nd}d| } ˆdk�rÎtˆˆd�‰|�s¾‡fdd„ˆ D ƒ}!‡ ‡fdd„|!D ƒ}tt |¡ƒ}dd„ |D ƒ}tdd„ |D ƒƒdk}"tdd„ |D ƒƒdk}#|"�rpt|ƒddd… }‡‡fdd„|D ƒ}nT|#�ržt|ƒddd… }‡‡fd d„|D ƒ}n&d!d"„ |D ƒ}$tˆfdˆd#œ|$—Žg}t|tƒ�rât|ƒt|ƒk�sæt‚ˆdk�r(ˆD ]2}t||ƒD ] \}}%|j|  ||% 9  < �q�qô|dk�rVtd$d
„ ˆD ƒƒ�rVd%d„ ˆD ƒ‰n|d&k�rtˆD ]}t|ƒ �qdtƒ } tt|!ƒƒD ]6}&ˆdk�r¨t
dƒtˆ|&ƒ }nd}|  d'| ¡ �q†|dk�rt ‡fd(d„|D ƒ¡}'t ‡fd)d„|D ƒ¡}(|(|'f‰n~t|tƒ�rpt
d*|ƒ‰‡fd+d„|D ƒ‰tˆƒd,k�rBˆd ‰n,d-d„ ˆD ƒ‰|"�s\|#�r€tt ttˆŽ ƒƒ‰nt!d.t|ƒ ƒ‚d/d„ ˆD ƒ}d0d„ ˆD ƒ})d1d„ ˆD ƒ}*t"t#|||*|	|
|d2�}+t"t$|||*|	|
||)d3�},t%d4d„ |*D ƒƒ}-t&d5d„ |*D ƒƒ}.t'ˆ|-|.g|+|,ˆdˆd||||||d6| d|d7�}/t(|/|ƒ |dk	�rª|dk�rNdn|}0d8d„ ˆD ƒ}1|j)|1|0d9d:id;�}| *¡  +|¡ | ,¡ }2t|2|ƒD ]\}3}4|3 -|4¡ �q”|d&k�rîˆD ]}t|ƒ �q¸tˆ|*t.ˆd jd< |/d=�}5t/d|5ƒ t0|ƒ |/S )>ad  Plot 2D topography of evoked responses.

    Clicking on the plot of an individual sensor opens a new figure showing
    the evoked response for the selected sensor.

    Parameters
    ----------
    evoked : list of Evoked | Evoked
        The evoked response to plot.
    layout : instance of Layout | None
        Layout instance specifying sensor positions (does not need to
        be specified for Neuromag data). If possible, the correct layout is
        inferred from the data.
    layout_scale : float
        Scaling factor for adjusting the relative size of the layout
        on the canvas.
    color : list of color objects | color object | None
        Everything matplotlib accepts to specify colors. If not list-like,
        the color specified will be repeated. If None, colors are
        automatically drawn.
    border : str
        Matplotlib borders style to be used for each sensor plot.
    ylim : dict | None
        ylim for plots (after scaling has been applied). The value
        determines the upper and lower subplot limits. e.g.
        ylim = dict(eeg=[-20, 20]). Valid keys are eeg, mag, grad. If None,
        the ylim parameter for each channel type is determined by the minimum
        and maximum peak.
    scalings : dict | None
        The scalings of the channel types to be applied for plotting. If None,`
        defaults to ``dict(eeg=1e6, grad=1e13, mag=1e15)``.
    title : str
        Title of the figure.
    proj : bool | 'interactive'
        If true SSP projections are applied before display. If 'interactive',
        a check box for reversible selection of SSP projection vectors will
        be shown.
    vline : list of floats | None
        The values at which to show a vertical line.
    hline : list of floats | None
        The values at which to show a horizontal line.
    fig_facecolor : color
        The figure face color. Defaults to black.
    fig_background : None | array
        A background image for the figure. This must be a valid input to
        `matplotlib.pyplot.imshow`. Defaults to None.
    axis_facecolor : color
        The face color to be used for each sensor plot. Defaults to black.
    font_color : color
        The color of text in the colorbar and title. Defaults to white.
    merge_channels : bool
        Whether to use RMS value of gradiometer pairs. Only works for Neuromag
        data. Defaults to False.
    legend : bool | int | string | tuple
        If True, create a legend based on evoked.comment. If False, disable the
        legend. Otherwise, the legend is created and the parameter value is
        passed as the location parameter to the matplotlib legend call. It can
        be an integer (e.g. 0 corresponds to upper right corner of the plot),
        a string (e.g. 'upper right'), or a tuple (x, y coordinates of the
        lower left corner of the legend in the axes coordinate system).
        See matplotlib documentation for more details.
    axes : instance of matplotlib Axes | None
        Axes to plot into. If None, axes will be created.
    noise_cov : instance of Covariance | str | None
        Noise covariance used to whiten the data while plotting.
        Whitened data channels names are shown in italic.
        Can be a string to load a covariance from disk.
    exclude : list of str | 'bads'
        Channels names to exclude from being shown. If 'bads', the
        bad channels are excluded. By default, exclude is set to 'bads'.
    show : bool
        Show figure if True.

        .. versionadded:: 0.16.0

    Returns
    -------
    fig : instance of matplotlib.figure.Figure
        Images of evoked responses at sensor locations
    r   Nr   )Úwhiten_evokedc                    s   g | ]}ˆ|ˆ ƒ‘qS r%   r%   ©rH   Úe)Ú	noise_covr  r%   r&   rJ   š  s     z%_plot_evoked_topo.<locals>.<listcomp>c                 S   s   g | ]}|  ¡ ‘qS r%   ©r[   r  r%   r%   r&   rJ   œ  s     r  c                 3   s   | ]}|j ˆ kV  qd S rç   rI   r  rI   r%   r&   rú      s     z$_plot_evoked_topo.<locals>.<genexpr>z!All evoked.picks must be the sameF)Ztopomap_coordsÚexcludeÚchsrE   r)   ÚXT)Zupdate_redundantZcheck_afterr  r�   ZunitsZNAzRMS amplitude (%s))r   c                    s   g | ]}|ˆ j kr|‘qS r%   )r]   ©rH   rE   )r   r%   r&   rJ   ¾  s    
ÿc                    s   g | ]}t ˆˆ  |¡ƒ‘qS r%   )r   ra   )rH   Úch)rC   r   r%   r&   rJ   À  s   ÿc                 S   s   g | ]}|d kr|‘qS )Úref_megr%   )rH   Ú
types_usedr%   r%   r&   rJ   Å  s    ÿc                 S   s   g | ]}|d kr|‘qS )r   r%   ©rH   rA   r%   r%   r&   rJ   È  s      c                 S   s   g | ]}|d kr|‘qS ))ZhboZhbrZfnirs_cw_amplitudeZfnirs_odr%   r'  r%   r%   r&   rJ   É  s
     ÿ c                    s   g | ]}t ˆ|d ˆ d�‘qS )F)Úmegr%  r   ©r   ©rH   Úkk©r   r   r%   r&   rJ   Í  s   ÿc                    s   g | ]}t ˆ|d ˆ d�‘qS )F)Zfnirsr%  r   r)  r*  r,  r%   r&   rJ   Ñ  s   ÿc                 S   s   i | ]
}|d “qS )Tr%   ©rH   Útr%   r%   r&   Ú
<dictcomp>Ô  s      z%_plot_evoked_topo.<locals>.<dictcomp>)r(  r   c                 s   s   | ]}|j d k	V  qdS )TN)Úprojr  r%   r%   r&   rú   Þ  s     c                 S   s   g | ]}|  ¡ ‘qS r%   )Ú
apply_projr  r%   r%   r&   rJ   ß  s     ÚinteractivezAmplitude (%s)c                    s"   g | ]‰ t ‡ fd d„ˆD ƒƒ‘qS )c                 3   s   | ]}|j ˆ   ¡ V  qd S rç   )rÏ   r,   r  ©r.  r%   r&   rú   î  s     ú/_plot_evoked_topo.<locals>.<listcomp>.<genexpr>)r,   ©rH   ©Úevokedr3  r&   rJ   î  s   ÿc                    s"   g | ]‰ t ‡ fd d„ˆD ƒƒ‘qS )c                 3   s   | ]}|j ˆ   ¡ V  qd S rç   )rÏ   r-   r  r3  r%   r&   rú   ð  s     r4  )r-   r5  r6  r3  r&   rJ   ð  s   ÿrM   c                    s   g | ]}ˆ | ‘qS r%   r%   r*  )r”   r%   r&   rJ   ö  s     r   c                 S   s   g | ]}t  |¡‘qS r%   )r:   r;   )rH   Zylr%   r%   r&   rJ   û  s     z$ylim must be None or a dict. Got %s.c                 S   s   g | ]
}|j ‘qS r%   )rÏ   r  r%   r%   r&   rJ     s     c                 S   s   g | ]
}|j ‘qS r%   ©Úcommentr  r%   r%   r&   rJ     s     c                 S   s   g | ]
}|j ‘qS r%   )r�   r  r%   r%   r&   rJ     s     )rÏ   r~   r�   rÃ   rÅ   rõ   )rÏ   r~   r�   rÃ   rÅ   rõ   rÐ   c                 S   s   g | ]}|d  ‘qS )r   r%   r-  r%   r%   r&   rJ     s     c                 S   s   g | ]}|d  ‘qS )r)   r%   r-  r%   r%   r&   rJ     s     úTime (s))r   r�   rG   rŽ   r   rŒ   rM   rx   r$   r�   r!   r‘   r"   r�   ru   rv   rh   r.   c                 S   s   g | ]}|j r|j nd ‘qS )ÚUnknownr8  r  r%   r%   r&   rJ     s     ÚsizerÕ   )ÚlocÚpropÚprojs)Úevokedsr�   Zplot_update_proj_callbackr?  r    )1r*   r+   Zcovr  Útyperž   r`   r   r   rC   r   r=   Ú
ValueErrorr   r   rf   Z_unlockr   rÏ   Úranger>   r   ÚdictÚfromkeysr   r�   ÚAssertionErrorrñ   r   r   r:   r;   ÚmapÚ	TypeErrorr   r  rù   r-   r,   r•   r   r   Z	get_framerX   Z	get_textsre   Ú_plot_update_evoked_topo_projr   r   )6r7  r   r$   r~   r�   rM   r  r�   r0  rÃ   rÅ   r!   Úfig_backgroundr"   r‘   Zmerge_channelsr   r.   r   Úshowr  r0   Zpicksr!  Úpickr$  Z	new_picksr  rÏ   Ú_r&  Úunitrv   Zchs_in_layoutZis_megZis_nirsZtypes_used_kwargsÚch_typerm   ZymaxesZyminsÚcommentsr�   rG   rŽ   Ztime_minZtime_maxr    Z
legend_locrÐ   ZtxtsÚtxtÚcolÚparamsr%   )rC   r7  r   r   r   r  r  r”   r&   Ú_plot_evoked_topo;  s0   W




ÿÿÿÿÿ"



ÿÿ


  þ
  þ
         ù	

ÿ
 þ
rT  c           	      C   s®   dd„ | d D ƒ}| d }dd„ t | d |ƒD ƒ}|| d< |D ]}|j|dd	� | ¡  q>|jd
 jD ]8}t |j|ƒD ]&\}}| |j|j|j	|j
   ¡ qvqf|j ¡  dS )zUpdate topo sensor plots.c                 S   s   g | ]}|  ¡ ‘qS r%   r  r  r%   r%   r&   rJ   2  s     z1_plot_update_evoked_topo_proj.<locals>.<listcomp>r@  r    c                 S   s   g | ]\}}|r|‘qS r%   r%   )rH   r0  Úbr%   r%   r&   rJ   4  s      r?  Z
proj_boolsT)Zremove_existingr   N)rñ   Zadd_projr1  r.   rg   rP   Z	set_ydatar¢   r¡   rÏ   rR   rZ   rä   )	rS  Zboolsr@  r    r?  r  rO   rø   r7  r%   r%   r&   rI  0  s    
"rI  r  rw   c                    s°  t dˆƒ‰ˆ  ¡  ¡ ‰ |dkr(tˆ jƒ}t|jƒtˆ jƒ@ }‡ fdd„|D ƒ}ˆ  |¡‰ ˆ  	¡ }‡fdd„|D ƒ}ˆ  j
t |¡dd…tjf 9  _
ˆ  ¡ }tƒ ‰t|ƒD ]4}|dd…t t |¡|k¡f }t|||ƒˆ|< qªt ‡fdd„|D ƒ¡}|dk�rtt|ƒƒdk}|�r8|dk�r8|dk�r8|d \}}tt|||ˆ |||d	�}tt|||ˆ |||d
d�	}tˆ jˆ j|||||||||	|||ddd
d
d�}t||ƒ t|ƒ |S )u 
  Plot Event Related Potential / Fields image on topographies.

    Parameters
    ----------
    epochs : instance of :class:`~mne.Epochs`
        The epochs.
    layout : instance of Layout
        System specific sensor positions.
    sigma : float
        The standard deviation of the Gaussian smoothing to apply along
        the epoch axis to apply in the image. If 0., no smoothing is applied.
    vmin : float
        The min value in the image. The unit is ÂµV for EEG channels,
        fT for magnetometers and fT/cm for gradiometers.
    vmax : float
        The max value in the image. The unit is ÂµV for EEG channels,
        fT for magnetometers and fT/cm for gradiometers.
    colorbar : bool | None
        Whether to display a colorbar or not. If ``None`` a colorbar will be
        shown only if all channels are of the same type. Defaults to ``None``.
    order : None | array of int | callable
        If not None, order is used to reorder the epochs on the y-axis
        of the image. If it's an array of int it should be of length
        the number of good epochs. If it's a callable the arguments
        passed are the times vector and the data as 2d array
        (data.shape[1] == len(times)).
    cmap : colormap
        Colors to be mapped to the values.
    layout_scale : float
        Scaling factor for adjusting the relative size of the layout
        on the canvas.
    title : str
        Title of the figure.
    scalings : dict | None
        The scalings of the channel types to be applied for plotting. If
        ``None``, defaults to ``dict(eeg=1e6, grad=1e13, mag=1e15)``.
    border : str
        Matplotlib borders style to be used for each sensor plot.
    fig_facecolor : color
        The figure face color. Defaults to black.
    fig_background : None | array
        A background image for the figure. This must be a valid input to
        :func:`matplotlib.pyplot.imshow`. Defaults to ``None``.
    font_color : color
        The color of tick labels in the colorbar. Defaults to white.
    show : bool
        Whether to show the figure. Defaults to ``True``.

    Returns
    -------
    fig : instance of :class:`matplotlib.figure.Figure`
        Figure distributing one image per channel across sensor topography.

    Notes
    -----
    In an interactive Python session, this plot will be interactive; clicking
    on a channel image will pop open a larger view of the image; this image
    will always have a colorbar even when the topo plot does not (because it
    shows multiple sensor types).
    r  Nc                    s   g | ]}ˆ j  |¡‘qS r%   )rC   ra   r#  )r  r%   r&   rJ   ‹  s     z*plot_topo_image_epochs.<locals>.<listcomp>c                    s   g | ]}ˆ   |d ¡‘qS )r   )Úget©rH   rO  )r  r%   r&   rJ   �  s     c                    s   g | ]}ˆ | ‘qS r%   r%   rW  )Ú	vlim_dictr%   r&   rJ   ˜  s     r   r   )r  r  rÏ   r  r	  rx   r  T)r  r  rÏ   r  r	  rx   r  rŒ   r:  ZEpoch)r   r�   rŽ   rG   r   rŒ   rs   rt   rx   r$   r�   r!   r‘   r�   ru   rv   rh   ri   )r   r[   Z	load_datar   r   r_   r]   rC   rL  Zget_channel_typesÚ_datar:   r;   ZnewaxisÚget_datarD  r<   r   r>   r   r  r  r•   r�   r   r   )r  r   r	  rs   rt   rŒ   r  rx   r$   r�   r  r�   r!   rJ  r‘   rK  rC   ZidxsZch_typesZscale_coeffsrÏ   rO  r  r  rG   Z
erf_imshowr    r%   )r  r  rX  r&   Úplot_topo_image_epochsB  sr    A


" 
    þ    þ
          ú
r[  )NNNr   r   r   NF)r   r   r   NFFNF)NNNNNNrn   r   r   ro   NNNNrp   FFN)NNNNNFr¥   r¦   Nr§   r¨   r©   FN)NNNNNNFTro   NN)NNNFNrp   N)NNNFNrp   )NNNNNNNNNFro   N)NNNNNNNNNFro   N)Nr  Nrn   NNNFr  r  r   Nr   rp   FTNr  TN)Nr  NNNNro   rw   NNrn   r   Nrp   T)/Ú__doc__r[   r   Ú	functoolsr   Únumpyr:   Zio.pickr   r   Úutilsr   r   r	   r
   r   Zchannels.layoutr   r   r   Údefaultsr   r   r   r   r   r   r   r   r   r   r'   r5   r   r•   rY   r¤   r‚   r»   rÆ   rù   r  r  r  rT  rI  r[  r%   r%   r%   r&   Ú<module>   s,  	,         þ5            ý
g                             ü
:'                       ü
$                   ý
           ý
l         ý
&                  ý
!                   ü
                               ú
 v                     ü