U
    f»{j'Y ã                   @   s  d dl Z d dlmZ d dlmZ d dlZd dlmZ d dl	Z	d dl
Z
d dlZd dlZd dlZd dlZddlmZ ddlmZ ddlmZmZ ddlmZmZmZmZmZ d	d
lmZmZm Z m!Z!m"Z"m#Z# d	dl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d	dl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddlm>Z>m?Z?m@Z@mAZA ddlBmCZC ddlDmEZEmFZFmGZGmHZHmIZI ddlmJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZV dZWeMG dd„ deXƒƒZYd'dd„ZZdd „ Z[d!d"„ Z\d#d$„ Z]G d%d&„ d&ƒZ^dS )(é    N)Úpartial)ÚBytesIOé   )Úcalculate_lut)Ú_Surface)Úviews_dictsÚ_lh_views_dict)ÚShowViewÚTimeCallBackÚSmartCallBackÚ	UpdateLUTÚUpdateColorbarScaleé   )Ú_show_help_figÚ_get_color_listÚconcatenate_imagesÚ_generate_default_filenameÚ_save_ndarray_imgÚ
safe_event)Ú_process_climÚ_handle_timeÚ_check_viewsÚ_handle_sensor_typesÚ_plot_sensorsÚ_plot_forward)Ú_LayeredMeshé   )Ú_handle_defaultÚDEFAULTS)Ú_point_dataÚ
_cell_data)Úvertex_to_mniÚread_talxfmÚread_freesurfer_lutÚ_get_head_surfaceÚ_get_skull_surfaceÚ_estimate_talxfm_rigid)Ú
pick_types)ÚInfo)Ú
mesh_edgesÚ_mesh_bordersÚ_marching_cubesÚget_meg_helmet_surf)ÚSourceSpaces)Ú	TransformÚapply_transÚ_frame_to_strÚ
_get_transÚ_get_transforms_to_coord_frame)Ú_check_optionÚloggerÚverboseÚfill_docÚ_validate_typeÚuse_log_levelÚBunchÚ_ReuseCycleÚwarnÚget_subjects_dirÚ_check_fnameÚ_to_rgbÚ_ensure_inté
   c                   @   s~  e Zd ZdZdúdd„Zdd„ Zdûdd„Zedd„ ƒZdüdd„Z	dd„ Z
dd„ Zdýdd„Zdd „ Zd!d"„ Zed#d$„ ƒZd%d&„ Zd'd(„ Zd)d*„ Zdþd+d,„Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Zd?d@„ ZdAdB„ ZdCdD„ ZdEdF„ Z dGdH„ Z!dIdJ„ Z"dKdL„ Z#dMdN„ Z$dOdP„ Z%dQdR„ Z&dSdT„ Z'dUdV„ Z(dWdX„ Z)dÿdYdZ„Z*�d d[d\„Z+d]d^„ Z,�dd_d`„Z-�ddadb„Z.dcdd„ Z/dedf„ Z0dgdh„ Z1didj„ Z2e3dkdl„ ƒZ4e4j5dmdl„ ƒZ4dndo„ Z6�ddpdq„Z7drds„ Z8e9�ddvdw„ƒZ:dxdy„ Z;dzd{„ Z<d|d}„ Z=d~d„ Z>d€d�„ Z?�dd‚dƒ„Z@eA�dd„d…„ƒZBd†d‡„ ZCeA�dd‰dŠ„ƒZDd‹dŒ„ ZEeA�dd�d�„ƒZFd‘d’„ ZGeA�d	d“d”„ƒZHd•d–„ ZIeA�d
d™dš„ƒZJd›dœ„ ZKeA�ddŸd „ƒZLe9�dd¢d£„ƒZM�dd¤d¥„ZN�dd§d¨„ZO�dd©dª„ZPd«d¬„ ZQeA�dd­d®„ƒZRd¯d°„ ZSd±d²„ ZTeA�dd³d´„ƒZUeA�dddddddddµœd¶d·„ƒZVd¸d¹„ ZW�dd»d¼„ZXeA�dd½d¾„ƒZYeZj[d¿dÀ„ ƒZ\eA�ddÁdÂ„ƒZ]dÃdÄ„ Z^e3dÅdÆ„ ƒZ_e3dÇdÈ„ ƒZ`eAdÉdÊ„ ƒZadËdÌ„ ZbdÍdÎ„ ZcdÏdÐ„ Zde3dÑdÒ„ ƒZedÓdÔ„ Zf�ddÕdÖ„Zgd×dØ„ Zhe3dÙdÚ„ ƒZie3dÛdÜ„ ƒZje3dÝdÞ„ ƒZke3dßdà„ ƒZl�ddãdä„Zm�ddådæ„ZneA�ddçdè„ƒZodédê„ Zpdëdì„ Zqdídî„ Zr�ddðdñ„Zsdòdó„ Zt�ddôdõ„Zudöd÷„ Zvdødù„ ZwdS (  ÚBrainuñ'  Class for visualizing a brain.

    .. warning::
       The API for this class is not currently complete. We suggest using
       :meth:`mne.viz.plot_source_estimates` with the PyVista backend
       enabled to obtain a ``Brain`` instance.

    Parameters
    ----------
    subject_id : str
        Subject name in Freesurfer subjects dir.
    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.
    surf : str
        FreeSurfer surface mesh name (ie 'white', 'inflated', etc.).
    title : str
        Title for the window.
    cortex : str, list, dict
        Specifies how the cortical surface is rendered. Options:

        1. The name of one of the preset cortex styles:
            ``'classic'`` (default), ``'high_contrast'``,
            ``'low_contrast'``, or ``'bone'``.
        2. A single color-like argument to render the cortex as a single
            color, e.g. ``'red'`` or ``(0.1, 0.4, 1.)``.
        3. A list of two color-like used to render binarized curvature
            values for gyral (first) and sulcal (second). regions, e.g.,
            ``['red', 'blue']`` or ``[(1, 0, 0), (0, 0, 1)]``.
        4. A dict containing keys ``'vmin', 'vmax', 'colormap'`` with
            values used to render the binarized curvature (where 0 is gyral,
            1 is sulcal).

        .. versionchanged:: 0.24
           Add support for non-string arguments.
    alpha : float in [0, 1]
        Alpha level to control opacity of the cortical surface.
    size : int | array-like, shape (2,)
        The size of the window, in pixels. can be one number to specify
        a square window, or a length-2 sequence to specify (width, height).
    background : tuple(int, int, int)
        The color definition of the background: (red, green, blue).
    foreground : matplotlib color
        Color of the foreground (will be used for colorbars and text).
        None (default) will use black or white depending on the value
        of ``background``.
    figure : list of Figure | None
        If None (default), a new window will be created with the appropriate
        views.
    subjects_dir : str | None
        If not None, this directory will be used as the subjects directory
        instead of the value set using the SUBJECTS_DIR environment
        variable.
    %(views)s
    offset : bool | str
        If True, shifts the right- or left-most x coordinate of the left and
        right surfaces, respectively, to be at zero. This is useful for viewing
        inflated surface where hemispheres typically overlap. Can be "auto"
        (default) use True with inflated surfaces and False otherwise
        (Default: 'auto'). Only used when ``hemi='both'``.

        .. versionchanged:: 0.23
           Default changed to "auto".
    show_toolbar : bool
        If True, toolbars will be shown for each view.
    offscreen : bool
        If True, rendering will be done offscreen (not shown). Useful
        mostly for generating images or screenshots, but can be buggy.
        Use at your own risk.
    interaction : str
        Can be "trackball" (default) or "terrain", i.e. a turntable-style
        camera.
    units : str
        Can be 'm' or 'mm' (default).
    %(view_layout)s
    silhouette : dict | bool
       As a dict, it contains the ``color``, ``linewidth``, ``alpha`` opacity
       and ``decimate`` (level of decimation between 0 and 1 or None) of the
       brain's silhouette to display. If True, the default values are used
       and if False, no silhouette will be displayed. Defaults to False.
    %(theme_3d)s
    show : bool
        Display the window as soon as it is ready. Defaults to True.
    block : bool
        If True, start the Qt application event loop. Default to False.

    Attributes
    ----------
    geo : dict
        A dictionary of PyVista surface objects for each hemisphere.
    overlays : dict
        The overlays.

    Notes
    -----
    This table shows the capabilities of each Brain backend ("âœ“" for full
    support, and "-" for partial support):

    .. table::
       :widths: auto

       +-------------------------------------+--------------+---------------+
       | 3D function:                        | surfer.Brain | mne.viz.Brain |
       +=====================================+==============+===============+
       | :meth:`add_annotation`              | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_data`                    | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_dipole`                  |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_foci`                    | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_forward`                 |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_head`                    |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_label`                   | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_sensors`                 |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_skull`                   |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_text`                    | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_volume_labels`           |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`close`                       | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | data                                | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | foci                                | âœ“            |               |
       +-------------------------------------+--------------+---------------+
       | labels                              | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_data`                 |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_dipole`               |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_forward`              |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_head`                 |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_labels`               | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_annotations`          | -            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_sensors`              |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_skull`                |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_text`                 |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`remove_volume_labels`        |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`save_image`                  | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`save_movie`                  | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`screenshot`                  | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`show_view`                   | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | TimeViewer                          | âœ“            | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`get_picked_points`           |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | :meth:`add_data(volume) <add_data>` |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | view_layout                         |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | flatmaps                            |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | vertex picking                      |              | âœ“             |
       +-------------------------------------+--------------+---------------+
       | label picking                       |              | âœ“             |
       +-------------------------------------+--------------+---------------+
    ÚbothÚpialNÚclassicç      ð?é   ÚblackÚautoFÚ	trackballÚmmÚverticalTc           %      C   sL  ddl m}m} |d krd}| j|dd�}|dkr:d| _n|dksFt‚|f| _td	|d
ƒ| _|
d k	rxt|
t	ƒsx| 
|
¡ |d krˆ|| _n|| _d| _t|dd�| _|	d krÀt| jƒdk r¼dnd}	t|	dd�| _~~	t|||ƒ}tdddddd�}t|ƒ|| f}| jdk�r|d d d… }|| _tt |¡ d¡ t	¡jƒ}t|ƒdk�rRtdƒ‚t|ƒdk�rd|n|d }t|ƒ}d| _t ¡ | _ || _!|| _"|| _#t$|ƒ| _%|| _&|| _'|| _(d | _)tƒ | _*tƒ | _+t,ƒ t,ƒ dœ| _-d| _.t,ƒ t,ƒ dœ| _/tƒ | _0tƒ | _1ddg| _2d | _3d| _4| jd|ddœ| _5t6|tt7fdƒ t|tƒ�rV| j5 8|¡ d| _9n|| _9d | _:d| _;i | _<i | _=|  >d¡ |  ?|¡}|d   |d! |d    }|d" |ƒ| _@t6|tAt7fd#ƒ t|tAƒ�rätd#|d$d%d&� |d'k}|�rô|d(k�rød nd)}tB Cd*|› �¡ t6|tAd fd+ƒ || j|| j||
d,�| _D| jD E| jF¡ | jD G|¡ | jDjH| _H|  I¡  dD �]x}|| jk�rz�qdtJ| j&||| j'|| j#| jKdd d-…f d.�}| L¡  | M¡  || j=|< |  N|¡D �]\}}} | j0 O|¡d k�rXtP| jD| j=| jQ| j=| jR| j=| jSd/�}!|! T¡  |!jU| j=| jV|d" |d  |d! g|d0d1� |!| j0|< ||!jW_"n| j0| jX}"| jDjHjY|"dd2� | j9�r¸| j0| }!| jDj5|!jW| j5d3 | j5d4 | j5d5 | j5d6 d7� | jDjZf ddd8œt[| |  —Ž �qÈ�qd|| _\d| _]|�rú|  ^¡  | jD ]0}|  N|¡D ]\}#}$} | j_| |#|$|d9� �q�q |d:k�rH| jD `d;¡ d S )<Nr   )ÚbackendÚ_get_rendererÚvol)rB   ÚsplitrN   ©Úextras)rB   rO   ©ÚlhÚrh©rS   rT   rN   Úview_layout)rK   Ú
horizontalrI   Ú
background©ÚnameÚwÚkÚ
foregroundr   )rS   rT   rB   rO   rN   rW   éÿÿÿÿr   )r   r   z="size" parameter must be an int or length-2 sequence of ints.Fé   é¥   çÍÌÌÌÌÌì?)ÚcolorÚ
line_widthÚalphaÚdecimateÚ
silhouetteTÚnearestÚvminÚvmaxÚcolormapÚoffset)rH   zwhen str©Úextra)ZinflatedÚflatrB   ç        zHemi offset: Útheme)rZ   ÚsizeÚbgcolorÚshapeÚfigr   )ÚunitsÚx_dir)ÚrendererÚverticesÚ	trianglesZnormalsZcurv©Úscalarsrj   ÚrngÚopacityrZ   ©Úrenderrb   rc   rd   re   )Úmeshrb   rc   rd   re   ©ÚupdateÚreset_camera)ÚrowÚcolÚhemirn   Zrubber_band_2d)aZbackends.rendererrL   rM   Ú_check_hemiÚ_hemisÚAssertionErrorr3   Ú_view_layoutÚ
isinstanceÚintZ_check_3d_figureÚ_titleÚ_interactionr>   Ú	_bg_colorÚsumÚ	_fg_colorr   ÚdictÚlenÚ_subplot_shapeÚtupleÚnpZ
atleast_1dÚroundÚastypern   Ú
ValueErrorr<   Útime_viewerÚtimeÚtime_nsÚ_hashÚ_blockÚ_hemiÚ_unitsÚfloatZ_alphaÚ_subject_idÚ_subjects_dirÚ_viewsÚ_timesÚ_vertex_to_label_idÚ_annotation_labelsÚlistÚ_labelsÚ_unnamed_label_idÚ_annotsÚ_layered_meshesÚ_actorsÚ_elevation_rngÚ_lut_lockedÚ_cleanedÚ_silhouetter7   Úboolr‚   rf   Ú_scalar_barÚ_time_label_addedÚ_dataÚgeoÚset_time_interpolationÚ_cortex_colormapÚ_brain_colorÚstrr4   ÚdebugÚ	_rendererZ_window_close_connectÚ_cleanZ_window_set_themeÚplotterÚ_setup_canonical_rotationr   Ú_rigidÚload_geometryZload_curvatureÚ_iter_viewsÚgetr   ÚcoordsÚfacesÚnnÚmapÚadd_overlayZbin_curvÚ	_polydataÚ_actorÚ	add_actorÚ
set_camerar   ÚinteractionÚ_closedÚshowÚ	show_viewÚset_interaction)%ÚselfZ
subject_idr†   ÚsurfÚtitleÚcortexrd   rq   rX   r]   ÚfigureÚsubjects_dirÚviewsrk   Zshow_toolbarZ	offscreenrÍ   ru   rV   rf   rp   rÏ   ÚblockrL   rM   Zcol_dictrs   Z
geo_kwargsÚvalÚhr¶   Ú_Úvr€   ÚactorÚriÚci© rá   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/viz/_brain/_brain.pyÚ__init__ì   s   ÿ



ü

ý

  ÿ



ü
û


û
ÿ

zBrain.__init__c                 C   sP   t  d¡| _zt| j| jƒ}W n tk
r<   t d¡ Y nX || jd d …< d S )Né   zCCould not estimate rigid Talairach alignment, using identity matrix)	r–   ÚeyerÀ   r&   r¢   r£   Ú	Exceptionr4   Úinfo)rÒ   Úxfmrá   rá   râ   r¿   •  s    zBrain._setup_canonical_rotationc           	      C   sæ  ddl m} | jrdS | js$tdƒ‚|| _tt ¡ ƒ| _ddg| _	d| _
d| _tttdƒƒd	ƒ| _d
dg| _dd	g| _d| _d| _ddgdddgdœ| _d| _d| _d| _d}dd„ |D ƒ| _tƒ | _| j d¡ t| jƒ| _d| _d| _d| _ dd„ |D ƒ| _!dd„ |D ƒ| _"t#ƒ | _$tƒ | _%d| _&t#ƒ | _'t#ƒ | _(d| _| j| _)t*|t+t,dfdƒ d| _-t.|t,ƒ�r®d| _/d| _0d | _1|d!k�rŒd| _0n |d"k�ržd"| _1n|d k�s
t2‚n\t.|t+ƒ�rÂ|| _/n<t3|ƒ}d#|  k �râd	k �sòn td$|› �ƒ‚d| _/|| _-d | _1d| _0~|  4¡  |  5¡  |  6¡  |  7¡  |  8¡  |  9¡  |  :¡  |  ;¡  |  <¡  |  =¡  |  >¡  | j? @¡  d%D ].}|  A|¡D ]\}}}| jB|||d&� �q€�qr| j? C¡  | j? D¡  | j/�rÈ| j @¡  | jE�râ|| j?jFjGd' ƒ dS )(aa  Configure the time viewer parameters.

        Parameters
        ----------
        time_viewer : bool
            If True, enable widgets interaction. Defaults to True.

        show_traces : bool
            If True, enable visualization of time traces. Defaults to True.

        Notes
        -----
        The keyboard shortcuts are the following:

        '?': Display help window
        'i': Toggle interface
        's': Apply auto-scaling
        'r': Restore original clim
        'c': Clear all traces
        'n': Shift the time forward by the playback speed
        'b': Shift the time backward by the playback speed
        'Space': Start/Pause playback
        'Up': Decrease camera elevation angle
        'Down': Increase camera elevation angle
        'Left': Decrease camera azimuth angle
        'Right': Increase camera azimuth angle
        r   ©Ú_qt_app_execNz'No data to visualize. See ``add_data``.r^   r_   Fg«ªªªªª0@r   çš™™™™™É?ç       @g{®Gáz„?zPress ? for helpÚmeanÚmaxZ	mean_flipZpca_fliprH   )ÚstcÚsrc)ÚvertexÚlabelrU   c                 S   s   i | ]
}|d “qS ))NNrá   ©Ú.0Úkeyrá   rá   râ   Ú
<dictcomp>Ô  s      z+Brain.setup_time_viewer.<locals>.<dictcomp>z#7f7f7fc                 S   s   i | ]}|t ƒ “qS rá   ©r¨   ró   rá   rá   râ   rö   Ü  s      c                 S   s   i | ]}|t ƒ “qS rá   r÷   ró   rá   rá   râ   rö   Ý  s      ©ÚfminÚfmidÚfmaxÚnumericÚshow_tracesç      Ð?Trñ   Zseparaterò   r   z6show traces, if numeric, must be between 0 and 1, got rR   )Úviewr„   r…   Úapp)HÚbackends._utilsrê   rš   rµ   r™   r¨   r   ÚkeysÚorientationÚdefault_smoothing_rangeÚplaybackÚ
visibilityrî   rŒ   r—   Úrefresh_rate_msZdefault_scaling_rangeÚdefault_playback_speed_rangeÚdefault_playback_speed_valueÚdefault_status_bar_msgÚdefault_label_extract_modesZdefault_trace_modesÚannotÚlabel_extract_modeÚact_data_smoothr   Z
color_listÚremover:   Úcolor_cycleÚ
mpl_canvasÚhelp_canvasÚrmsÚpicked_patchesÚpicked_pointsr’   Ú
pick_tableÚ_spheresÚ_mouse_no_mvtÚ	callbacksÚwidgetsÚplayback_speedr7   r²   rº   Úinteractor_fractionr‹   rý   Úseparate_canvasÚtraces_moder‰   r¡   Ú_configure_time_labelÚ_configure_scalar_barÚ_configure_shortcutsÚ_configure_pickingÚ_configure_tool_barÚ_configure_dockÚ_configure_menuÚ_configure_status_barÚ_configure_playbackÚ_configure_helpÚtoggle_interfacer¼   rÏ   rÂ   rÐ   Z_process_eventsÚ_updaterž   rÖ   Ústore)	rÒ   rš   rý   rê   Úall_keysr†   rß   rà   rÝ   rá   rá   râ   Úsetup_time_viewerŸ  s¤    


þ

ÿ



zBrain.setup_time_viewerc                 C   sþ   | j  ¡  |  ¡  |  ¡  | jD ]}| j|  ¡  q |  ¡  |  ¡  t| dd ƒd k	r^| j	 
¡  t| dd ƒd k	rŒt| j ¡ ƒD ]}d | j|< q|| j jD ]}| ¡  q”dD ]}t| j|d ƒ q¦tƒ | j_t| jdd ƒd k	rÞd | j_dD ]}t| |d ƒ qâd| _d S )Nr  r  )ZlightingÚ
interactorZ_RenderWindowÚpicker)r¾   ZwindowZdockZtool_barZmenu_barr.  r  Ú
time_actorÚpicked_rendererr  r³   Úactionsr  r¶   rµ   T)r¼   Z_window_close_disconnectÚclear_glyphsÚremove_annotationsr¬   r½   Ú_clear_callbacksÚ_clear_widgetsÚgetattrr  Úclearr¨   r  r  Ú_all_renderersZRemoveAllLightsÚsetattrr¾   Ú	_FakeIrenZ_Irenr/  r°   )rÒ   r†   rõ   rw   rá   rá   râ   r½     s,    




zBrain._cleanc              	   C   sz   |dkr| j  | _ n|| _ | j ¡ �B | j rH| j ¡  | jjddd� n| j ¡  | jjddd� W 5 Q R X | j ¡  dS )a  Toggle the interface.

        Parameters
        ----------
        value : bool | None
            If True, the widgets are shown and if False, they
            are hidden. If None, the state of the widgets is
            toggled. Defaults to None.
        Nr  Úvisibility_on©rZ   Ú	icon_nameZvisibility_off)r  r¼   Z_window_ensure_minimum_sizesZ
_dock_showÚ_tool_bar_update_button_iconZ
_dock_hider*  )rÒ   Úvaluerá   rá   râ   r)  @  s     

 ÿ
 ÿzBrain.toggle_interfacec                 C   s   |   ¡  dS )z0Detect automatically fitting scaling parameters.N©Ú_update_auto_scaling©rÒ   rá   rá   râ   Úapply_auto_scaling\  s    zBrain.apply_auto_scalingc                 C   s   | j dd� dS )z$Restore original scaling parameters.T)ÚrestoreNrA  rC  rá   rá   râ   Úrestore_user_scaling`  s    zBrain.restore_user_scalingc                 C   s~   |dkr| j  | _ n|| _ | j r2| jjddd� n| jjddd� | j rz| jd }t |¡}| j|krp|  d¡ t ¡ | _	dS )a  Toggle time playback.

        Parameters
        ----------
        value : bool | None
            If True, automatic time playback is enabled and if False,
            it's disabled. If None, the state of time playback is toggled.
            Defaults to None.
        NÚplayÚpauser=  r›   r   )
r  r¼   r?  rµ   r–   rî   Ú_current_timeÚset_time_pointr›   Ú
_last_tick)rÒ   r@  Ú	time_dataÚmax_timerá   rá   râ   Útoggle_playbackd  s$    
 ÿ ÿ



zBrain.toggle_playbackc                 C   sH   |   ¡  t| jd ƒd }|dkr:| jd | jd dd� | j ¡  dS )zReset view and time step.r›   r   r   Úinitial_time_idxT©Úupdate_widgetN)Ú
reset_viewr“   rµ   r  r¼   r*  )rÒ   rM  rá   rá   râ   Úreset‚  s    þzBrain.resetc                 C   s
   || _ dS )z‰Set the time playback speed.

        Parameters
        ----------
        speed : float
            The speed of the playback.
        N)r  )rÒ   Úspeedrá   rá   râ   Úset_playback_speed�  s    zBrain.set_playback_speedc                 C   s:   | j r6z|  ¡  W n" tk
r4   | jdd� ‚ Y nX d S )NF©r@  )r  Ú_advanceræ   rN  rC  rá   rá   râ   Ú_play—  s    zBrain._playc           	      C   sŽ   t   ¡ }|| j }t   ¡ | _| jd }t | j¡}|| j }t |¡}t| j	| |ƒ}t 
|||¡}| jd |dd� ||krŠ| jdd� d S )Nr›   TrP  FrV  )r›   rK  rµ   r–   ÚarangeÚ_n_timesr  rî   ÚminrI  Úinterpr  rN  )	rÒ   Z	this_timeÚdeltarL  ÚtimesZ
time_shiftrM  Z
time_pointÚidxrá   rá   râ   rW     s    




zBrain._advancec                 C   sF   | j  d¡| _| jd k	rB| j dd¡ | j ¡  ¡  | j ¡  ¡  d S )Nr0  ç      à?g¸…ëQ¸ž?)rµ   rÃ   r0  ÚSetPositionZGetTextPropertyZSetJustificationToCenteredZBoldOnrC  rá   rá   râ   r  ±  s
    
zBrain._configure_time_labelc                 C   s>   | j d k	r:| j  ¡  | j  d¡ | j  d¡ | j  dd¡ d S )Ng333333ã?çš™™™™™©?g{®Gáz”?rë   )r³   ZSetOrientationToVerticalZ	SetHeightZSetWidthra  rC  rá   rá   râ   r   ¸  s
    

zBrain._configure_scalar_barc                 C   s  t | jd ƒd }|dk rd S |d kr.| jjn|}| jjdd�}| jjd|d�| jd< | j |¡ | jjd|d�| jd	< | j |¡ | jjd
|d�| jd< | j ||¡ t	| jd d ƒ}t	| jd d ƒ}| jd  
|d›¡ | jd  
|d›¡ | jd	  
| jd›¡ d S )Nr›   r   F©rK   ú-)r@  ÚlayoutÚmin_timeÚxÚcurrent_timeú+rM  r   r^   z .3f)r“   rµ   r¼   Zdock_layoutÚ_dock_add_layoutÚ_dock_add_labelr  Z_dock_add_stretchÚ_layout_add_widgetr¡   Ú	set_valuerI  )rÒ   re  Úlen_timeÚhlayoutrf  rM  rá   rá   râ   Ú_configure_dock_time_widget¿  s2     ÿ ÿ ÿz!Brain._configure_dock_time_widgetc              	   C   s’  | j  |¡}t| jd ƒd }|dk r<d | jd< d | jd< nVt| | jd�| jd< | j jd| jd d|gd| jd d|d	�| jd< | jd | jd _	|dk rºd | jd
< d | jd< d | jd< n|  
|¡ | jd | jd _|dk rôd | jd< d | jd< nJt| jd�| jd< | j jd| j| j| jd |d�| jd< | jd | jd _	| j}|d k	�sRt‚| jd }t|ƒ�rp||ƒ}n|}| jd k	�rŒ| j |¡ ~d S )Nr›   r   )ÚbrainÚcallbackzTime (s)Útime_idxr   TF)rZ   r@  r|   Údoublerr  Úcompactre  rf  rM  rh  r  ©rr  ZSpeed©rZ   r@  r|   rr  re  Ú
time_label)r¼   Ú_dock_add_group_boxr“   rµ   r  r  r
   Úplot_time_lineÚ_dock_add_sliderÚwidgetrp  rò   r   rU  Ú_dock_add_spin_boxr	  r  rI  r‰   Úcallabler0  ÚSetInput)rÒ   rZ   re  rn  rh  rx  rá   rá   râ   Ú_configure_dock_playback_widgetÔ  sb    
þù	



ÿûÿ



z%Brain._configure_dock_playback_widgetc                    s>  ˆ j  |¡}dd„ ttˆ j jƒƒD ƒ}t|ƒdkr‚‡ fdd„}t|d�ˆ jd< ˆ j jdd	|ˆ jd |d
�ˆ jd< ˆ jd ˆ jd _	ˆ j
dkr”dg}nˆ j}d gt|ƒ }|D ]P}ˆ  |¡D ]@\}}	}
ˆ j  ||	f¡}|
dkrâd }nt|
|||	d�}|||< qºq¬tˆ |d�ˆ jd< ˆ j jd ˆ jd ˆ jˆ jd |d
�ˆ jd< d S )Nc                 S   s   g | ]}t |ƒ‘qS rá   )rº   )rô   Úirá   rá   râ   Ú
<listcomp>  s     z<Brain._configure_dock_orientation_widget.<locals>.<listcomp>r   c                    s$   t | ƒ} ˆ j | ¡}ˆ jj|Ž  d S ©N)rŒ   r¼   Ú_index_to_locr¾   Úsubplot)r_  ÚlocrC  rá   râ   Úselect_renderer  s    zABrain._configure_dock_orientation_widget.<locals>.select_rendererrv  rw   ZRendererÚ0rw  rB   rS   rn   )Údefaultr†   r„   r…   )rq  Údatar  r   )r¼   ry  Úranger“   r9  r   r  Ú_dock_add_combo_boxr  r|  rŸ   rˆ   rÂ   Z_loc_to_indexr’   r	   r  )rÒ   rZ   re  Zrendsr‡  Z	hemis_refZorientation_datar†   rß   rà   rÝ   r_  rµ   rá   rC  râ   Ú"_configure_dock_orientation_widget  sL    ÿûÿ

þûz(Brain._configure_dock_orientation_widgetc           	   	      s¤  ˆ j  |¡}ˆ j jdd|d� tˆ d�‰ˆ jD ]¶}ˆ j jdd�}tˆ ƒ}|f‡fdd„	ˆ j|< ˆ j jd ˆ j	| |ˆ j| d|d	�ˆ j
|< ˆ j jd ˆ j	| ˆ j| ||d
�ˆ j
d|› �< ˆ j
| ˆ j
d|› � gˆj
|< ˆ j  ||¡ q.ˆ j jdd�}ˆ j jdd|d� ˆ j jdˆ j|dd�ˆ j
d< dD ]>\}}}tˆ |d�ˆ j|< ˆ j j|ˆ j| |dd�ˆ j
|< �q&ˆ j  ||¡ ‡ fdd„ˆ jD ƒ}dD ]}|ˆ j| _
�qŒd S )Nzmin / mid / maxT)r@  Úalignre  )rq  Frc  c                    s   ˆ f || iŽS rƒ  rá   )r@  rõ   )Úuprá   râ   Ú<lambda>O  ó    z7Brain._configure_dock_colormap_widget.<locals>.<lambda>)rZ   r@  r|   rr  rt  re  )rZ   r@  rr  r|   re  Zentry_ZRescaleu   â†ºZ
toolbutton)rZ   rr  re  ÚstylerS  ))Úfminusu   âž–g	ï6Ãü’î?)Úfplusu   âž•g2‰è|¿ð?)rq  Úfactorc                    s   i | ]}|ˆ j | “qS rá   )r  ró   rC  rá   râ   rö   ~  s      z9Brain._configure_dock_colormap_widget.<locals>.<dictcomp>)rù   rú   rû   r“  r”  )r¼   ry  rk  r   r  rj  Ú
_get_ranger  r{  rµ   r  r}  rl  Z_dock_add_buttonrF  r   )	rÒ   rZ   re  rõ   ro  r|   ÚcharrÚ   r  rá   )rÒ   r�  râ   Ú_configure_dock_colormap_widgetD  sn    ý

úû ýüþüz%Brain._configure_dock_colormap_widgetc                    sL  ˆ j s
d S ˆ j dd ¡d k	r8ˆ jd jdkr8ˆ  ¡  d S ˆ j |¡}t ˆ ¡}|fdd„}|fdd„}ddl	m
} dd	lm} t ˆ jˆ jd
¡}||dd�}	|	dg }	|	d ˆ _ˆ jd }
||
ƒ}ˆ jd d krâ‡ fdd„|D ƒ}|d ˆ _ˆ jdk�r|dƒ n
|ˆ jƒ ˆ jjdˆ j|	||d�ˆ jd< ˆ jjdˆ j|||d�ˆ jd< d S )Nrð   Úvolumec                 S   sd   |ƒ }|d krd S |  ¡  | ¡  | ¡  | |_| dkrHd|_| ¡  nd|_| ¡  |j ¡  d S )NÚNonerñ   rò   )	r3  Úremove_labelsr4  r  r  Ú_configure_vertex_time_courseÚ_configure_label_time_courser¼   r*  )r  ÚweakselfrÒ   rá   rá   râ   Ú
_set_annot�  s    
z6Brain._configure_dock_trace_widget.<locals>._set_annotc                 S   sª   |ƒ }|d krd S |j dkr d S t |j¡}| |_| ¡  |jD ]8}|| D ]*}|j| | }|jd }| 	|d |¡ qLq@|j
j ¡  |j
j ¡  |j
 ¡  |j ¡  d S )Nrò   r   )r  ÚcopyÚdeepcopyr  r  r3  rˆ   r§   rx   Ú_add_label_glyphr  ÚaxesZrelimZautoscale_viewÚupdate_plotr¼   r*  )Úmoderž  rÒ   Zglyphsr†   Úlabel_idrò   Ú	vertex_idrá   rá   râ   Ú_set_label_mode¡  s"    



z;Brain._configure_dock_trace_widget.<locals>._set_label_moder   )Ú_get_allowed_label_modes)Ú_read_annot_candsrò   F)Zraise_errorrš  r   rï   c                    s   g | ]}|ˆ j d  kr|‘qS )rð   )r  ©rô   ÚmrC  rá   râ   r‚  ½  s
     ÿ z6Brain._configure_dock_trace_widget.<locals>.<listcomp>r^   rñ   Z
Annotationrw  Ú
annotationzExtract modeZextract_mode)rý   rµ   rÃ   Úkindrœ  r¼   ry  ÚweakrefÚrefÚsource_estimater©  rò   rª  ÚopÚjoinr£   r¢   r  r  r  rŒ  r  )rÒ   rZ   re  rž  rŸ  r¨  r©  rª  Údir_nameÚcandsrï   Úmodesrá   rC  râ   Ú_configure_dock_trace_widget‚  sN    ÿ






ûûz"Brain._configure_dock_trace_widgetc                 C   s–   | j  ¡  | jdd� | jdd� | jdd� | jdd� t| jd�| jd< | j j	d| j
d	 | j| jd d
d�| jd< | jd | jd _| j  ¡  d S )NZPlaybackrY   ZOrientationzColor LimitsÚTracerv  Z	smoothingZ	SmoothingÚsmoothing_stepsF)rZ   r@  r|   rr  rt  )r¼   Z_dock_initializer€  r�  r˜  r·  r   Úset_data_smoothingr  r}  rµ   r  r  r|  Z_dock_finalizerC  rá   rá   râ   r$  Ó  s$    
ÿûÿ
zBrain._configure_dockc                 C   sD   | j j| j| j| jd dt| jd ƒd g| jd | jd d� d S )Nrs  r   r›   r   rG  )ÚfuncÚtimeoutr@  r|   Ztime_widgetZplay_widget)r¼   Z_playback_initializerX  r  rµ   r“   r  rC  rá   rá   râ   r'  ê  s    úzBrain._configure_playbackc              	   C   s–   | j j| | j| j| jd�| _t | jd ¡t 	| jd ¡g}t
 ¡ �$ t
jdtd� | jjj|d� W 5 Q R X | js~| j  ¡  | jj| j| jd� d S )N)rq  r  rý   r  r›   Úignore)Úcategory)Úxlim)Zbg_colorZfg_color)r¼   Z_window_get_mplcanvasr  rý   r  r  r–   r[  rµ   rî   ÚwarningsÚcatch_warningsÚfilterwarningsÚUserWarningr£  ÚsetZ_window_adjust_mplcanvas_layoutZ	set_colorr�   r‘   )rÒ   r¿  rá   rá   râ   Ú_configure_mplcanvasô  s$    üÿ

þzBrain._configure_mplcanvasc              
   C   s   | j s
d S | jd kr|  ¡  n|  ¡  t tdd„ | j ¡ D ƒƒ¡}tj	j
|dd�t t|ƒ¡ }~| jjj| jd |ddd| jdd	d
�\| _| jdd� tdddgƒD ]ò\}}| j |d g¡d }|d krÎq¨| j| }|d }| jdksò|dkröd}| jj| | _| jd d k	�r:|d d …t | jd ¡ t¡gf }n|}t tjt |¡d d�|j¡}	|dk�rp|d }
n| j | j!}
||	d  }| j"||
|dd� q¨d S )Nc                 s   s"   | ]}|d  dk	r|d  V  qdS ©r   Nrá   ©rô   rÝ   rá   rá   râ   Ú	<genexpr>  s    ÿz6Brain._configure_vertex_time_course.<locals>.<genexpr>r   ©Úaxisr›   r   ZRMSr`  ú:)Úlwrò   Úzorderrb   rd   ZlsF©r‚   rS   rT   rN   rx   )rB   rT   Úinitial_timers  Úgrid)#rý   r  rÅ  r3  r–   Úconcatenater¨   r  ÚvaluesÚlinalgÚnormÚsqrtr“   r£  Úplotrµ   r‘   r  rz  Ú	enumeraterÃ   rŸ   r¼   r9  r1  r—   r˜   rŒ   Úunravel_indexÚargmaxÚabsrs   r¬   rÉ   Ú_add_vertex_glyph)rÒ   Úyr  r_  r†   Úact_dataÚ	hemi_datarx   Zuse_dataÚindr€   r§  rá   rá   râ   rœ    sR    

     ý

ÿÿ

z#Brain._configure_vertex_time_coursec              	   C   sÒ   ddl m} tdddgƒD ]˜\}}| j |¡}|d k	r|d }|jdkrXtjj|dd	�}| d
¡}|d }|dkr¤|d ks~t	‚| 
t t|ƒ¡|t t|ƒ¡ff¡}||f| j|< q| j | j| j| j| j¡ d S )Nr   )ÚsparserN   rS   rT   Úarrayr   r   rÉ  Ú
smooth_matrx   )Zscipyrà  r×  rµ   rÃ   Úndimr–   rÓ  rÔ  r‰   Z
csr_matrixZonesr“   rY  r  r¼   Z_update_picking_callbackÚ_on_mouse_moveÚ_on_button_pressÚ_on_button_releaseÚ_on_pick)rÒ   rà  r_  r†   rÞ  rÝ  râ  rx   rá   rá   râ   r"  ;  s.    

ÿÿüzBrain._configure_pickingc                 C   sè   | j jdd� t | ¡}|fdd„}| j jdd|d� |fdd	„}| j jd
d|dd� | j jdd| jdd� | j jdd| jdd�| j	d< | j jdd| j
d� | j jdd| jd� | j jdd| jd� | j  ¡  | j jdd| jdd� d S )NZToolbarrY   c                 S   s    |ƒ }|d krd S |  | ¡ d S rƒ  )Ú
save_image©Úfilenamerž  rÒ   rá   rá   râ   rè  X  s    z-Brain._configure_tool_bar.<locals>.save_imageÚ
screenshotzTake a screenshot)rZ   Údescr»  c                 S   s*   |ƒ }|d krd S |j | d|j d� d S )NrE   )rê  Útime_dilation)Ú
save_movier  ré  rá   rá   râ   rî  d  s    þz-Brain._configure_tool_bar.<locals>.save_movieZmoviezSave movie...zctrl+shift+s)rZ   rì  r»  Zshortcutr  zToggle Controlsr<  )rZ   rì  r»  r>  rG  z
Play/Pauseú rS  ÚResetÚscalez
Auto-Scaler8  zClear tracesÚhelpÚHelpú?)r¼   Z_tool_bar_initializer¯  r°  Z_tool_bar_add_file_buttonZ_tool_bar_add_buttonr)  Z_tool_bar_add_play_buttonrN  r  rS  rD  r3  Z_tool_bar_add_spacerrò  )rÒ   rž  rè  rî  rá   rá   râ   r#  T  sb    
ýüüüýýý
üzBrain._configure_tool_barc                 C   s"   | j d || j| jƒddd� d S )Nr›   FT)r@  Ztime_as_indexrQ  )r  rI  r  )rÒ   r²  rá   rá   râ   Ú_shift_time•  s
    ýzBrain._shift_timec                 C   s&   | j jj| d }| j j|dd� d S )Nih  F)Úazimuthrƒ   )r¼   rÖ   Z_azimuthrÌ   )rÒ   r@  rö  rá   rá   râ   Ú_rotate_azimuthœ  s    zBrain._rotate_azimuthc                 C   s8   t  | jjj| | jd | jd ¡}| jj|dd� d S )Nr   r   F)Ú	elevationrƒ   )r–   Úclipr¼   rÖ   Z
_elevationr®   rÌ   )rÒ   r@  rø  rá   rá   râ   Ú_rotate_elevation   s    ýzBrain._rotate_elevationc                 C   sÔ   |   ¡  | j d| j¡ | j d| j¡ | j d| j¡ | j d| j¡ | j dt| jdd„ d�¡ | j d	t| jd
d„ d�¡ d| j	dfd| j	dfd| j
dfd| j
dffD ]"\}}}| j |t||t ƒ¡ q¬d S )Nr�  ÚsÚrÚcÚnc                 S   s   | | S rƒ  rá   ©rg  rÜ  rá   rá   râ   r�  ±  r‘  z,Brain._configure_shortcuts.<locals>.<lambda>)r²  Úbc                 S   s   | | S rƒ  rá   rÿ  rá   rá   râ   r�  ³  r‘  ÚLeftr   ÚRightr^   ÚUpÚDown)r5  r¾   Zadd_key_eventr)  rD  rF  r3  r   rõ  r÷  rú  Ú_ARROW_MOVE)rÒ   rõ   r»  Úsignrá   rá   râ   r!  ¨  s"    ÿÿ



ýzBrain._configure_shortcutsc                 C   s4   | j  ¡  | j jddd� | j jddd| jd� d S )Nrò  ró  )rZ   rì  zShow MNE key bindings	?)Z	menu_namerZ   rì  r»  )r¼   Z_menu_initializeZ_menu_add_submenuZ_menu_add_buttonrò  rC  rá   rá   râ   r%  º  s    
þüzBrain._configure_menuc                 C   sB   | j  ¡  | j j| jdd�| _| j  ¡ | _| jd k	r>| j ¡  d S )Nr   )Zstretch)r¼   Z_status_bar_initializeZ_status_bar_add_labelr
  Ú
status_msgZ_status_bar_add_progress_barÚstatus_progressÚhiderC  rá   rá   râ   r&  Ç  s    
 ÿ
zBrain._configure_status_barc                 C   s   | j r|  j d8  _ d S )Nr   ©r  ©rÒ   Ú
vtk_pickerÚeventrá   rá   râ   rä  Ï  s    zBrain._on_mouse_movec                 C   s
   d| _ d S )Nr   r
  r  rá   rá   râ   rå  Ó  s    zBrain._on_button_pressc                 C   sv   | j dkrl| ¡ \}}z| jj ||¡| _W n( tk
rT   | jjj ||¡| _Y nX | jj 	||d| j¡ d| _ d S )Nr   )
r  ZGetEventPositionr¾   ÚirenZFindPokedRendererr1  ÚAttributeErrorr.  r/  ZPick)rÒ   r  r  rg  rÜ  rá   rá   râ   ræ  Ö  s    
ÿÿ
zBrain._on_button_releasec                    s®  | j s
d S | ¡ }| ¡ }|d ks0|dks0| js4d S t| jƒr´| ¡ }d }t| ¡ ƒD ]F}| 	|¡‰ | jD ]$}t
‡ fdd„|jD ƒƒrj|} q�qj|d k	rV qžqV|d k	r´|js°t‚|}t|dƒrÌ|  |¡ d S z
|j}	W n tk
rî   d}	Y nX |	dksüt‚| j|	 d d k�rd S t | ¡ ¡}
|	dk�r0| j|	 d  }}| j|	 d	 }| j|	 d
 | }t|ƒd | }t | ¡ ¡}tj |¡d }| ¡  ¡  ¡ }|
| }|tj |¡ }tjjt |||
 ¡dd�}|jt|ƒfk�sæt‚||k}t  |¡d }t|ƒdk�rd S |t !t "|| ¡¡ }|| }nP| #|¡‰‡fdd„tˆ $¡ ƒD ƒ}|j%| }tj&t"||
 ƒdd�}||d  }| j'dk�rœ|  (|	||¡ n|  )|	||¡ d S )Nr^   c                 3   s   | ]}|ˆ kV  qd S rƒ  rá   ©rô   Úa)rÞ   rá   râ   rÈ  ø  s     z!Brain._on_pick.<locals>.<genexpr>Ú	_is_glyphrN   rR   r   rÐ  rx   Úgrid_coordsrÒ  rì   r   rÉ  c                    s   g | ]}ˆ   |¡‘qS rá   )Z
GetPointId)rô   Zpoint_id)Úvtk_cellrá   râ   r‚  4  s     z"Brain._on_pick.<locals>.<listcomp>rò   )*rý   Z	GetCellIdZ
GetDataSetr  r“   r  Z
GetProp3Dsr‹  ZGetNumberOfItemsZGetItemAsObjectÚanyr­   r  r‰   ÚhasattrÚ_remove_vertex_glyphrŸ   r  r  r–   rá  ZGetPickPositionrµ   r    Z
GetSpacingrÓ  rÔ  ZGetRendererZGetActiveCameraZGetPositionÚcrossrs   ÚwhererÙ  rÚ  ÚGetCellÚGetNumberOfPointsZpointsÚargminr  r¢  rÛ  )rÒ   r  r  Zcell_idr€   Z
collectionZfound_sphereÚiiÚspherer†   ÚposrÐ  rx   rÄ   r{   ÚspacingZmax_distÚoriginÚoriÚdistsÚmaskr_  r§  Úcellrá   )rÞ   r  râ   rç  æ  sv    












ÿ
zBrain._on_pickc                 C   st   |dkrd S | j | | }| j| | }|| j| krF|  ||¡ d S ||jkrp| j|ddd� | j|  |¡ d S )NrN   TF)Úbordersrƒ   )r¦   r§   r  Ú_remove_label_glyphr†   Ú	add_labelÚappend)rÒ   r†   r€   r§  r¦  rò   rá   rá   râ   r¢  ?  s    
zBrain._add_label_glyphc                 C   sV   | j | | }|j ¡  | j |j¡ | j ¡  | j|  	|j
¡ | j|  |¡ d S rƒ  )r§   Ú_liner  r  rE  Ú_colorr  r¤  r¬   Úremove_overlayrZ   r  )rÒ   r†   r¦  rò   rá   rá   râ   r'  N  s    

zBrain._remove_label_glyphc                 C   sÄ  || j | krd S | j| d d kr(d S t| jƒ}| j||||d�}|dkrÄtj|t | ¡ ¡d dd�}|j	|Ž }| 
¡ }	|	 ¡ }
t |
df¡}t|	 ¡ ƒD ]}|	 ||| ¡ qžtj|dd�}n| 
¡  |¡}~z| jjj}W n tk
rþ   | jj}Y nX | | j¡}| j |¡\}}tƒ }tƒ }|  |¡D ]6}| jjt |¡|d	d
�\}}| |¡ | |¡ �q2|D ]*}d|_||_||_||_||_||_ �qn| j |  |¡ | j! "|¡ || j#|< |S )Nr   rÎ  rN   r   ÚF©Úorderr   rÉ  ç      @)Úcenterrb   ZradiusT)$r  r  Únextr  Úplot_time_courser–   rØ  rá  ZGetDimensionsr  Z	GetPointsr  Úemptyr‹  ZGetPointrí   r¼   r9  Z
_renderersr  Úindexr1  r„  r¨   rÂ   Z_spherer)  r  rŸ   r*  r­   r+  Ú
_vertex_idr  Úextendr  )rÒ   r†   r€   r§  r‚   rb   ÚlineÚijkZvoxelZptsZn_ptsr1  r  ÚlstÚrindexr„   r…   ÚactorsÚspheresrÜ   rÞ   r  rá   rá   râ   rÛ  V  s`    
  ÿ
ý



zBrain._add_vertex_glyphc              	   C   sÔ   |j }|| jkrd S |j}|j}| j| }|d j ¡  | j ¡  | j|  |¡ t	j
dd�� t	 d¡ | j |¡ W 5 Q R X |D ]0}| jj|jdd� d |_| j | j |¡¡ q„|rÄ| j ¡  | j |¡ d S )Nr   T)Úrecordr½  Fr~   )r6  r  rŸ   r+  r*  r  r  r¤  r  rÀ  rÁ  Úsimplefilterr  rE  r¾   Úremove_actorr­   r  Úpopr5  r¼   r*  )rÒ   r€   r   r§  r†   rb   r=  r  rá   rá   râ   r  —  s&    




zBrain._remove_vertex_glyphc                 C   sà   | j s
dS t| jƒD ]}| j|dd� qtdd„ | j ¡ D ƒƒdksHt‚t| j	ƒdksZt‚t| jƒdkslt‚| j
D ]$}t| j| ƒD ]}|  ||¡ q„qrtdd„ | j ¡ D ƒƒdks¸t‚| jdk	rÒ| j ¡  d| _| j ¡  dS )zClear the picking glyphs.NFr~   c                 s   s   | ]}t |ƒV  qd S rƒ  ©r“   rÇ  rá   rá   râ   rÈ  ·  s     z%Brain.clear_glyphs.<locals>.<genexpr>r   c                 s   s   | ]}t |ƒV  qd S rƒ  rB  rÇ  rá   rá   râ   rÈ  ½  s     )rš   r¨   r  r  r�   r  rÒ  r‰   r“   r  rˆ   r  r'  r  r  r¼   r*  )rÒ   r  r†   r¦  rá   rá   râ   r3  ±  s     
 

zBrain.clear_glyphsc              	   C   s„  | j dkrdS | jd  ¡ }d}|dkr d}t| j| jƒ}| jdkr`|d dd…df  d9  < tj|| j| d	 d
d�}	| j| d }
t	t 
|d |
¡|	ƒ}nN|dkr¬dnd}z$t||dkrÂdnd| j| jd�}W n tk
rì   d}Y nX |dk	�rdd dd„ |D ƒ¡ }nd}d |t|ƒ d¡|¡}| j| \}}|dk	�rZ||  
|¡d }n||  ¡ }| j j|||d|d|d�}|S )aÓ  Plot the vertex time course.

        Parameters
        ----------
        hemi : str
            The hemisphere id of the vertex.
        vertex_id : int
            The vertex identifier in the mesh.
        color : matplotlib color
            The color of the time course.
        update : bool
            Force an update of the plot. Defaults to True.

        Returns
        -------
        line : matplotlib object
            The time line object.
        Nr›   rN   ÚVrJ   Útransr   ç     @�@Ú
grid_shaper-  r.  Úgrid_src_mri_trS   ÚLÚRr   r   )rx   ÚhemisÚsubjectr×   z MNI: ú, c                 s   s   | ]}d | V  qdS )z%5.1fNrá   r«  rá   rá   râ   rÈ  ð  s     z)Brain.plot_time_course.<locals>.<genexpr>Ú z{}:{}{}é   rE   rä   )rò   rÌ  rb   rÍ  r‚   )r  rµ   r   r"   r¢   r£   r    r–   rØ  r/   Údotr!   ræ   r³  Úformatrº   Úljustr  rÖ  )rÒ   r†   r§  rb   r‚   r›   ZmniZhemi_strrè   r9  Ú	src_mri_trò   rÝ  Úsmoothr8  rá   rá   râ   r3  Ã  s^    
 ÿ
  ÿü



ù	zBrain.plot_time_coursec                 C   sh   | j dkrdS t| jtƒrd| jrd| j}t| dƒsJ| j j|d| jd|d�| _| j 	|¡ |rd| j  
¡  dS )z¥Add the time line to the MPL widget.

        Parameters
        ----------
        update : bool
            Force an update of the plot. Defaults to True.
        NÚ	time_liner›   r   )rg  rò   rb   rÌ  r‚   )r  r‹   rý   r²   rI  r  rz  r‘   rT  Z	set_xdatar¤  )rÒ   r‚   rh  rá   rá   râ   rz    s    

ûzBrain.plot_time_linec                 C   sn   ddddddddd	d
ddg}t |Ž \}}d |¡}d |¡}| jjdddd�| _t||| jj| jjdd� d S )N)rô  zDisplay help window)r�  zToggle interface)rû  zApply auto-scaling)rü  zRestore original clim)rý  zClear all traces)rþ  z,Shift the time forward by the playback speed)r   z-Shift the time backward by the playback speed)ZSpacezStart/Pause playback)r  zDecrease camera elevation angle)r  zIncrease camera elevation angle)r  zDecrease camera azimuth angle)r  zIncrease camera azimuth angleÚ
é   r   éP   )ÚwidthÚheightÚdpiF)Zcol1Zcol2Zfig_helpZaxrÏ   )Úzipr³  r¼   Z_window_get_simple_canvasr  r   rt   r£  )rÒ   ÚpairsZtext1Ztext2rá   rá   râ   r(    s8    ô

  ÿûzBrain._configure_helpc                 C   s   | j  ¡  dS )zDisplay the help window.N)r  rÏ   rC  rá   rá   râ   rò  9  s    z
Brain.helpc                 C   sf   t | dƒsd S | j ¡ D ]"}|d k	rdD ]}t||d ƒ q(q| j ¡  t| dd ƒd k	rb| jj ¡  d S )Nr  )r¾   rq  rr  r|  r  r  )	r  r  rÒ  r:  r8  r7  r¾   r  Zclear_key_event_callbacks)rÒ   rr  rõ   rá   rá   râ   r5  =  s    

zBrain._clear_callbacksc                 C   sJ   t | dƒsd S | j ¡ D ]"}|d k	rdD ]}t||d ƒ q(q| j ¡  d S )Nr  )Z	triggeredZfloatValueChanged)r  r  rÒ  r:  r8  )rÒ   r|  rõ   rá   rá   râ   r6  J  s    
zBrain._clear_widgetsc                 C   s   | j S )zThe interaction style.)rŽ   rC  rá   rá   râ   rÍ   S  s    zBrain.interactionc                 C   s8   t |tdƒ td|dƒ |  d¡D ]}| j |¡ q"dS )zSet the interaction style.rÍ   )rI   ZterrainrN   N)r7   rº   r3   rÂ   r¼   rÑ   )rÒ   rÍ   rÜ   rá   rá   râ   rÍ   X  s    c              	   C   s.  ddl m} ddlm} ttdddd�tddd	d�tdd
dd�tdddd�d�}t|ttttfdƒ t	|tƒrŠ||kr€|| }n
|gd }t	|ttfƒ�rt
dt|ƒddd� t|ƒdkrÄ|gd }t|ƒ}t|ƒD ]\}}t|dd�||< qÔt||dd�ddd�}tt|d ƒt|d ƒ||d ƒd�}|S )z0Return the colormap corresponding to the cortex.r   )Ú	_get_cmapr   )ÚListedColormapZGreysr^   )rj   rh   ri   gš™™™™™¹¿gÍÌÌÌÌÌô?éûÿÿÿrV  Zbone_rgš™™™™™É¿)rD   Zhigh_contrastZlow_contrastÚbonerÕ   zlen(cortex))r   r   zwhen cortex is a list or tuplerl   r   rY   zcustom binaryr   rh   ri   rj   )rh   ri   rj   )Ú_3dr]  Zmatplotlib.colorsr^  r’   r7   rº   r¨   r•   r‹   r3   r“   r×  r>   r¡   )rÒ   rÕ   r]  r^  Zcolormap_maprà   rý  rá   rá   râ   r¸   `  sX     ÿ ÿ ÿ ÿú	


ÿ

 þ


ýzBrain._cortex_colormapc                 C   sl   || j krht dt| j | ƒ› d|› d�¡ | j | D ]}| jjj|dd� q6| j  |¡ |rh| j ¡  dS )z&Remove actors from the rendered scene.z	Removing rï  z	 actor(s)Fr~   N)	r­   r4   r»   r“   r¼   r¾   r@  rA  r*  )rÒ   Úitemr   rÞ   rá   rá   râ   Ú_remove…  s    
ÿzBrain._removec                 C   s,   || j kr| j |  |¡ n|g| j |< dS )z&Add an actor to the internal register.N)r­   r)  )rÒ   rb  rÞ   rá   rá   râ   Ú
_add_actor�  s    
zBrain._add_actorr   çš™™™™™Ù?c           &   	   C   s  t |tdƒ t |ddƒ t |ddƒ td|dgƒ td|dgƒ t |dd	ƒ |dk	rrt|ƒ}|d
k rrtd|› �ƒ‚| j|dgd�}|  |||
¡\}}}
t |¡}|dkr¬|	n|}|| j	d< || j	d< |j
dkrÔd
}n¼|dkrît |jd ¡}n8t |¡}|j|jd fk�r&td|j|jd ff ƒ‚|| j	d< | jdk�rD|| _n2t|ƒ| jk�r^tdƒ‚nt || j¡�svtdƒ‚|dk�r†d
}n
|  |¡}t|d|ƒ\}}ddt|ƒ  }|j
dk�rà|jd dk�ràtd|jd f ƒ‚t|||||ƒ\}}}|dk�r|dk	�rdnd}|dk�r"d}nH|dk�r2d}n8t|tƒ�rX|d
k �rjtd |¡ƒ‚ntd t|ƒ¡ƒ‚|| j	d < || j	d!< || j	d"< || j	d#< || j	d< || j	d$< || j	d%< || j	d&< || j	d'< || j	d< tƒ | j	|< d| j	| d(< d| j	| d)< d| j	| d*< || j	| d+< |
| j	| d,< |	| j	d-< || j	d.< || j	d/< || j	d0< || j	d1< || j	d2< |  ¡  d}|  |¡D ]L}|d3k�rŽ| j| j}n.|jd4k�r¦|d5d… n|}|  |||¡\}}�qr|dk	�sÎt ‚|  !d6|¡ |  "| j#¡ |  $| j	d" ¡ |d7k�r| j%d
 d d
f}|  |¡D ]Ô\} }!}"| |!f|k}#| j&�sz|dk	�rz|#�rz| j'j(d8|| j)||| j*ƒd9d:�}$|$| j	d;< d7| _&|�rÎ| j+dk�rÎ|#�rÎt|d<| j)| j,dd… d=�}%|% -|�pºi ¡ | j'j.f |%Ž| _+| j'j/f d>d>d?œt0| |" —Ž �q| j|	d@� dS )Aa5  Display data from a numpy array on the surface or volume.

        This provides a similar interface to
        :meth:`surfer.Brain.add_overlay`, but it displays
        it with a single colormap. It offers more flexibility over the
        colormap, and provides a way to display four-dimensional data
        (i.e., a timecourse) or five-dimensional data (i.e., a
        vector-valued timecourse).

        .. note:: ``fmin`` sets the low end of the colormap, and is separate
                  from thresh (this is a different convention from
                  :meth:`surfer.Brain.add_overlay`).

        Parameters
        ----------
        array : numpy array, shape (n_vertices[, 3][, n_times])
            Data array. For the data to be understood as vector-valued
            (3 values per vertex corresponding to X/Y/Z surface RAS),
            then ``array`` must be have all 3 dimensions.
            If vectors with no time dimension are desired, consider using a
            singleton (e.g., ``np.newaxis``) to create a "time" dimension
            and pass ``time_label=None`` (vector values are not supported).
        %(fmin_fmid_fmax)s
        %(thresh)s
        %(center)s
        %(transparent)s
        colormap : str, list of color, or array
            Name of matplotlib colormap to use, a list of matplotlib colors,
            or a custom look up table (an n x 4 array coded with RBGA values
            between 0 and 255), the default "auto" chooses a default divergent
            colormap, if "center" is given (currently "icefire"), otherwise a
            default sequential colormap (currently "rocket").
        alpha : float in [0, 1]
            Alpha level to control opacity of the overlay.
        vertices : numpy array
            Vertices for which the data is defined (needed if
            ``len(data) < nvtx``).
        smoothing_steps : int or None
            Number of smoothing steps (smoothing is used if len(data) < nvtx)
            The value 'nearest' can be used too. None (default) will use as
            many as necessary to fill the surface.
        time : numpy array
            Time points in the data array (if data is 2D or 3D).
        %(time_label)s
        colorbar : bool
            Whether to add a colorbar to the figure. Can also be a tuple
            to give the (row, col) index of where to put the colorbar.
        hemi : str | None
            If None, it is assumed to belong to the hemisphere being
            shown. If two hemispheres are being shown, an error will
            be thrown.
        remove_existing : bool
            Not supported yet.
            Remove surface added by previous "add_data" call. Useful for
            conserving memory when displaying different data in a loop.
        time_label_size : int
            Font size of the time label (default 14).
        initial_time : float | None
            Time initially shown in the plot. ``None`` to use the first time
            sample (default).
        scale_factor : float | None (default)
            The scale factor to use when displaying glyphs for vector-valued
            data.
        vector_alpha : float | None
            Alpha level to control opacity of the arrows. Only used for
            vector-valued data. If None (default), ``alpha`` is used.
        clim : dict
            Original clim arguments.
        %(src_volume_options)s
        colorbar_kwargs : dict | None
            Options to pass to :meth:`pyvista.Plotter.add_scalar_bar`
            (e.g., ``dict(title_font_size=10)``).
        %(verbose)s

        Notes
        -----
        If the data is defined for a subset of vertices (specified
        by the "vertices" parameter), a smoothing method is used to interpolate
        the data onto the high resolution surface. If the data is defined for
        subsampled version of the surface, smoothing_steps can be set to None,
        in which case only as many smoothing steps are applied until the whole
        surface is filled with non-zeros.

        Due to a VTK alpha rendering bug, ``vector_alpha`` is
        clamped to be strictly < 1.
        Útransparent©rü   NÚvector_alphaÚscale_factorÚthreshNÚremove_existing©Nrü   Útime_label_sizer   z&time_label_size must be positive, got rN   rP   r   r^   z6time has shape %s, but need shape %s (array.shape[-1])r›   z.New n_times is different from previous n_timesz=Not all time values are consistent with previously set times.rû  rb  çš™™™™™¹?r   z>If array has 3 dimensions, array.shape[1] must equal 3, got %srH   ZmneZhoté   rg   zAExpected value of `smoothing_steps` is positive but {} was given.zGExpected type of `smoothing_steps` is int or NoneType but {} was given.rï   rð   r¹  ÚclimrÏ  rx  rO  rs  Úglyph_datasetÚglyph_mapperÚglyph_actorrá  rx   rd   rj   r1  rù   rú   rû   rR   Úmixedr   rŠ  Tgffffffî?Úright)Úx_windowÚy_windowrb   rq   ÚtextÚjustificationr0  é   )ÚsourceZn_labelsrb   rr   Fr�   ©rd   )1r7   r²   r3   r¡   r™   r‡   Ú
_check_stcr–   Úasarrayrµ   rã  rY  rs   rZ  r¥   r“   Zarray_equalÚ_to_time_indexr   Ú_update_limitsr‹   rŒ   rP  Ú	TypeErrorÚtyper’   Ú
update_lutrÂ   r¬   rÊ   r®  Ú_add_volume_datar‰   rd  r·   Útime_interpolationrº  r”   r´   r¼   Útext2dr‘   rI  r³   r¹   r‚   Z	scalarbarrÌ   r   )&rÒ   rá  rù   rú   rû   rj  r1  rf  rj   rd   rx   r¹  r›   rx  Zcolorbarr†   rk  rm  rÏ  ri  rh  rp  rð   Úvolume_optionsZcolorbar_kwargsr5   rï   rs  rÜ   Zy_txtrÞ   Zsrc_volrß   rà   rÝ   Údor0  Úkwargsrá   rá   râ   Úadd_data—  sø    _




þ




ÿ    ÿ




þþ

















 û

ÿ ÿ
ÿ
zBrain.add_datac                 C   s   | j ddd� dS )z#Remove rendered data from the mesh.rŠ  Tr~   N©rc  rC  rá   rá   râ   Úremove_data‹  s    zBrain.remove_datac           
      c   sÎ   t dgdgdgd�}| jdkr2|jdgddgd� t| jƒD ]Œ\}}t |g|g|gd�}| jdkrr|j||gd� | jdkrˆ|| }}n
|| }}t|| || ƒD ]"\}}	| j ||	¡ ||	|fV  q¤q<dS )	z7Iterate over rows and columns that need to be added to.r   rU   rO   r   )rT   rN   )rN   rK   N)	r’   rŸ   r‚   r×  r¤   rŠ   r[  r¼   r…  )
rÒ   r†   Z	hemi_dictÚvirÿ   Z	view_dictÚrowsÚcolsrß   rà   rá   rá   râ   rÂ   �  s    



zBrain._iter_viewsc                 C   sN   | j D ]8}| j| }| j| D ]}| |j¡ q| j|  ¡  q| j ¡  dS )z)Remove all the ROI labels from the image.N)rˆ   r¬   r©   r,  rZ   r8  r¼   r*  )rÒ   r†   r€   rò   rá   rá   râ   r›     s    

zBrain.remove_labelsc                 C   sV   | j D ]@}|| jkr.| j| }| | j| ¡ || jkr| j|  ¡  q| j ¡  dS )z&Remove all annotations from the image.N)rˆ   r¬   r,  r«   r8  r¼   r*  )rÒ   r†   r€   rá   rá   râ   r4  ©  s    



zBrain.remove_annotationsc           #   
   C   sÔ  ddl m} t|tdƒ td|jdƒ t|tdd fdƒ |dksDt‚t|tƒsht|d k	r`t	|ƒnd d	�}t
d|ƒ}d
dgdtfgddgddgddgddgf}|D ]$\}}t|| |dt|ƒ› d�ƒ q t|ƒtdd„ |D ƒƒ }t|ƒrútdt|ƒ› �ƒ‚td|d dƒ}	|d }
|
d k�r:| j| d jdk�r6dnd}
t t	|
ƒdd¡}
|d
 }|d }|d k�rtt|
d d ƒ}|d }|d k�rŽ|d! }|d }~| j|  d"¡}| j|  d#¡}| jd$ }|d k�rÂtjd%d&„ |d' d( D ƒd)d*iŽ}t |d' d( t¡}| jd+k�rd,nd-}|d' d. d/  ¡ }|d d…  |9  < |d k	�rT|| d0 }~~t d1d&„ |D ƒ¡j}t||ƒ}t|d2�| j|< | j| d3 }| j| d j d' t|ƒk�s¸t‚t !|d d…d d…f t "t "|¡d d… ¡¡�sît‚t "|¡d d… }|d d…df |d  }t #t $|¡¡}d||< | j% &|||||||	|¡\}}}}|
| j| d< || j| d4< || j| d5< || j| d6< || j| d7< || j| d8< || j| d"< || j| d#< | j%j'j(|d9d d9d9d:�\}}d  }} |d k	�r| j%j'j(|d9d d9d9d:�\}}| j| d5 }|d k	�r¨| j%j'j(|d9d d9d9d9d;�\} }!|!j)| j*d d… Ž  |! +|¡ |d'k�r¨|d'k�r¨|  ,d¡D ](}| j%j-| .¡ | j*d d… ||d<� �q~||| fD ]}"|"d k	�r²||"ƒ �q²||fS )=Nr   )Ú_hide_testing_actorrð   zsrc.kind)r™  rü   r‡  rN   )Ú
resolutionr‘  rl  Úblendingrd   rg  Úsurface_alphaÚsilhouette_alphaÚsilhouette_linewidth)rü   zvolume_options[ú]c                 s   s   | ]}|d  V  qdS rÆ  rá   r  rá   rá   râ   rÈ  Ê  s     z)Brain._add_volume_data.<locals>.<genexpr>z volume_options got unknown keys zvolume_options["blending"])Z	compositeZmiprá  r   re  rE   ro   rì   rn  r0  Úgrid_volume_posÚgrid_volume_negr1  c                 S   s   g | ]}t  |¡‘qS rá   )r–   rY  )rô   rû  rá   rá   râ   r‚  ã  s     z*Brain._add_volume_data.<locals>.<listcomp>r   rs   ZindexingZijrJ   iè  r   rR  rD  rE  c                 S   s   g | ]}|j d d�‘qS )r-  r.  )Úravel)rô   rý  rá   rá   râ   r‚  ë  s     )rÄ   rx   rÐ  Ú	grid_meshr  rG  rF  F)rƒ   rZ   Úcullingr   )rƒ   rZ   r›  Zpickabler   )r€   rb   rc   rd   )/Zbackends._pyvistar�  r7   r-   r3   r®  r’   r‰   r‹   r¡   r   rº   ÚreprrÄ  r“   r™   Úsortedrµ   rã  r–   rù  r[  rÃ   Zmeshgridrá  rŒ   r    r   ÚTr/   r9   r¶   rs   ZallcloseZdiagÚzerosÚprodr¼   Z_volumer¾   rË   ZSetColorr¹   Ú
SetOpacityrÂ   r±   ZGetInput)#rÒ   r†   rð   r‡  r�  Zallowed_typesrõ   ÚtypesZ
extra_keysr’  rd   r‘  r“  r”  r•  Z
volume_posZ
volume_negr1  ZxyzÚ
dimensionsÚmultrR  rÄ   rx   r   r!  r{   rÐ  rš  Z	actor_posrÜ   Z	actor_negZ
actor_meshÚproprÞ   rá   rá   râ   r„  ³  s     ÿ
þ
ú
ÿÿþ




ÿÿ

" ÿ
  þÿ
   þ

   þ

    þ

ü

zBrain._add_volume_datac	                 C   sÔ  ddl m}	 t|tƒrØ|dkr"d}tj |¡rX|}
|	|
ƒ}|j}tj |
¡ 	d¡d }nr|  
|¡}|}d ||dg¡}|dkr”t | j| jd|¡}
nt | j| jd||¡}
tj |
¡sÂtd|
 ƒ‚|	|
ƒ}|j}|j}n¬z‚|j}|j}|jdk�rd	t| jƒ |_|  jd7  _t|jƒ}|dk�rHt|d
ƒ�rD|jdk	�rD|j}nd}|dk	�rX|j}W n tk
�rx   tdƒ‚Y nX |  
|¡}|dk	�rš|||k }| j�r(| j�r(| jdk�r(| jd }| jd }|j||| jd�}|jdk�rð|d n|dddd…f }t| j ƒ}| j!j"| jd |||d�}nd}|}t#||dd�}t$ %d|g¡}t$ &|d ¡ 't$j(¡}t$ )| j*| j+j,d ¡}d||< |�r$t-| j*| j.|ƒ}t$j)|j/t$j0d�}t|t1ƒ�rt2|ƒD ]R}t$ 3| j*| j. 4¡ |¡}| j*| j.j,|_,| j*| j.t$j5|dd� }t$ 6|¡}�qÀd||< ||9 }|  7|¡D ]–\}}}| j8| }|j9||t$ :|¡t$ ;|¡g||d� |�rŠ| j<j=f ddit>| | —Ž | j�r²| j�r²| jdk�r²||_?||_@| jA|  B|¡ �q.| j< C¡  dS )a–  Add an ROI label to the image.

        Parameters
        ----------
        label : str | instance of Label
            Label filepath or name. Can also be an instance of
            an object with attributes "hemi", "vertices", "name", and
            optionally "color" and "values" (if scalar_thresh is not None).
        color : matplotlib-style color | None
            Anything matplotlib accepts: string, RGB, hex, etc. (default
            "crimson").
        alpha : float in [0, 1]
            Alpha level to control opacity.
        scalar_thresh : None | float
            Threshold the label ids using this value in the label
            file's scalar field (i.e. label only vertices with
            scalar >= thresh).
        borders : bool | int
            Show only label borders. If int, specify the number of steps
            (away from the true border) along the cortical mesh to include
            as part of the border definition.
        hemi : str | None
            If None, it is assumed to belong to the hemipshere being
            shown.
        subdir : None | str
            If a label is specified as name, subdir can be used to indicate
            that the label file is in a sub-directory of the subject's
            label directory rather than in the label directory itself (e.g.
            for ``$SUBJECTS_DIR/$SUBJECT/label/aparc/lh.cuneus.label``
            ``brain.add_label('cuneus', subdir='aparc')``).
        reset_camera : bool
            If True, reset the camera view after adding the label. Defaults
            to True.

        Notes
        -----
        To remove previously added labels, run Brain.remove_labels().
        r   )Ú
read_labelNZcrimsonÚ.r   rò   zLabel file %s does not existÚunnamedrb   z­Label was not a filename (str), and could not be understood as a class. The class must have attributes "hemi", "vertices", "name", and (if scalar_thresh is not None)"values"rï   rð   )rð   r¥  r   r   r›   )rò   rb   Tr|  )r   r   r   r   éÿ   ©ÚdtyperÉ  rz   r‚   F)Drò   r¦  r‹   rº   ÚosÚpathÚisfiler†   ÚbasenamerO   r‡   r³  r²  r£   r¢   Úexistsr™   rx   rÒ  rZ   rª   r  rb   ræ   rš   rý   r  rµ   Zextract_label_time_courser  rã  r2  r  r  rÖ  r>   r–   rá  r—   r˜   Úuint8rŸ  r¶   rÄ   rs   r*   rÅ   rq   Úint64rŒ   r‹  Úin1dr™  r  ÚuniquerÂ   r¬   rÈ   r[  rî   r¼   rÌ   r   r+  r*  r©   r)  r*  )rÒ   rò   rb   rd   Zscalar_threshr&  r†   Úsubdirrƒ   r¦  ÚfilepathZ
label_nameZlabel_fnameÚidsr{   rï   rð   Ztcr8  Z
orig_colorÚcmapÚctableÚkeep_idxrÏ   rÜ   rÝ   r€   rá   rá   râ   r(  "  sÒ    )

 ÿ  ÿÿ





ÿ

ÿ&
  þ ÿ ÿ
ûÿzBrain.add_labelc           
      C   sÔ   t |dddd�d }~|dkr0| jdkr,dnd	}d
}|d tkr~t|d  }|dkr`tddƒ}q�|dkrn|}q�t| |¡ƒ‚nt| |d ¡ƒ‚t| j||| jdkr¨dnd||d�D ]}	|  d|	¡ q´| j 	¡  dS )as  Add a quiver to render positions of dipoles.

        Parameters
        ----------
        %(fwd)s
        %(trans_not_none)s
        %(alpha)s Default 1.
        scale : None | float
            The size of the arrow representing the dipoles in
            :class:`mne.viz.Brain` units. Default 1.5mm.

        Notes
        -----
        .. versionadded:: 1.0
        ÚheadÚmriF©Ú
allow_noner   NrJ   g      ø?gú~j¼t“X?zEUnexpected forward model coordinate frame {}, must be "head" or "mri"Úcoord_framerE  r   )Z	fwd_scalerñ  rd   Úforward)
r1   r    r0   r.   ÚRuntimeErrorrP  r   r¼   rd  r*  )
rÒ   ZfwdrD  rd   rñ  Ú
head_mri_tÚ	error_msgZ	fwd_frameZ	fwd_transrÞ   rá   rá   râ   Úadd_forward¶  s.       ý
zBrain.add_forwardc                 C   s   | j ddd� dS )z/Remove forward sources from the rendered scene.rÀ  Tr~   Nr‹  rC  rá   rá   râ   Úremove_forwardß  s    zBrain.remove_forwardÚredc              	   C   sR  t |dddd�d }~t|ƒ}t|ttfƒs6|g| }t|ƒ|kr\tdt|ƒ› d|› d�ƒ‚d	d
„ t|ƒD ƒ}|dkrˆ| jdkr„dnd}t|ttfƒs |g| }t|ƒ|krÆtdt|ƒ› d|› d�ƒ‚t||j	ƒ}|| jdkrâdnd9 }|  
d¡D ]P}	t||j||ƒD ]:\}
}}}| jj|
|ž||d|dœŽ\}}	|  d|¡ �qqò| j ¡  dS )a½  Add a quiver to render positions of dipoles.

        Parameters
        ----------
        dipole : instance of Dipole
            Dipole object containing position, orientation and amplitude of
            one or more dipoles or in the forward solution.
        %(trans_not_none)s
        colors : list | matplotlib-style color | None
            A single color or list of anything matplotlib accepts:
            string, RGB, hex, etc. Default red.
        %(alpha)s Default 1.
        scales : list | float | None
            The size of the arrow representing the dipole in
            :class:`mne.viz.Brain` units. Default 5mm.

        Notes
        -----
        .. versionadded:: 1.0
        r»  r¼  Fr½  r   zThe number of colors (z) and dipoles (z) must matchc                 S   s$   g | ]\}}t |d |› d�d�‘qS ©zcolors[r–  rY   ©r>   ©rô   rà   rb   rá   rá   râ   r‚  	  s   ÿz$Brain.add_dipole.<locals>.<listcomp>NrJ   rV  g{®Gázt?zThe number of scales (rE  r   rN   Úarrow)rb   r}   r¥  rñ  Údipole)r1   r“   r‹   r¨   r•   r™   r×  r    r/   r  rÂ   r[  r"  r¼   Úquiver3drd  r*  )rÒ   rË  rD  Úcolorsrd   ÚscalesrÂ  Z	n_dipolesr  rÜ   Zthis_posZthis_orirb   rñ  rÞ   rá   rá   râ   Ú
add_dipoleã  sH    
ÿ
   ÿ ÿ  þzBrain.add_dipolec                 C   s   | j ddd� dS )z.Remove dipole objects from the rendered scene.rË  Tr~   Nr‹  rC  rá   rá   râ   Úremove_dipole	  s    zBrain.remove_dipoleÚgrayr`  c           	   	   C   s’   t |r
dnd| j| jƒ}|d |d  }}|| jdkr:dnd9 }t|ƒ}|  d¡D ]0}| jj|j|||d	d	d
œŽ\}}|  	d|¡ qR| j 
¡  dS )aD  Add a mesh to render the outer head surface.

        Parameters
        ----------
        dense : bool
            Whether to plot the dense head (``seghead``) or the less dense head
            (``head``).
        %(color_matplotlib)s
        %(alpha)s

        Notes
        -----
        .. versionadded:: 0.24
        Úsegheadr»  ÚrrÚtrisrJ   rE  r   rN   F©ry   rb   r}   rƒ   r   N)r$   r¢   r£   r    r>   rÂ   r¼   r€   rž  rd  r*  )	rÒ   Zdenserb   rd   rÓ   Úvertsry   rÜ   rÞ   rá   rá   râ   Úadd_head	  s$     ÿ    þzBrain.add_headc                 C   s   | j ddd� dS )z,Remove head objects from the rendered scene.r»  Tr~   Nr‹  rC  rá   rá   râ   Úremove_head9	  s    zBrain.remove_headc           	   	   C   s’   t |r
dnd| j| jƒ}|d |d  }}|| jdkr:dnd9 }t|ƒ}|  d¡D ]0}| jj|j|||d	d	d
œŽ\}}|  	d|¡ qR| j 
¡  dS )a%  Add a mesh to render the skull surface.

        Parameters
        ----------
        outer : bool
            Adds the outer skull if ``True``, otherwise adds the inner skull.
        %(color_matplotlib)s
        %(alpha)s

        Notes
        -----
        .. versionadded:: 0.24
        ÚouterÚinnerrÓ  rÔ  rJ   rE  r   rN   FrÕ  ÚskullN)r%   r¢   r£   r    r>   rÂ   r¼   r€   rž  rd  r*  )	rÒ   rÙ  rb   rd   rÓ   rÖ  ry   rÜ   rÞ   rá   rá   râ   Ú	add_skull=	  s$     ÿ    þzBrain.add_skullc                 C   s   | j ddd� dS )z-Remove skull objects from the rendered scene.rÛ  Tr~   Nr‹  rC  rá   rá   râ   Úremove_skullZ	  s    zBrain.remove_skullú
aparc+asegra   c              
      sT  ddl }| d¡s td|› �ƒ‚tt | j| jd|d ¡ddd	�}|}	| |	¡}t	 
|j¡}
|j ¡ }| jd
krtdnd}|dd…  |9  < ~tƒ \‰‰ |dkrÈdd„ ˆ ¡ D ƒ‰‡fdd„td D ƒ}t|tdfdƒ t|tdfdƒ |dkrüt|ƒdk }|dk�r‡ fdd„|D ƒ}nt|ttfƒ�s8|gt|ƒ }dd„ t|ƒD ƒ}t|
‡fdd„|D ƒ||d�}t|||ƒD ]Œ\}}\}}t|ƒdk�r¶tdˆ| › dt|ƒ› d|	› �ƒ �qtt||ƒ}|  d¡D ]2}| j j!|j"|||dddœŽ\}}|  #d |¡ �qÊ�qt|�st|t$ƒ�rFt|t$ƒ�r$|nt$ƒ }| j j%j&tt||ƒƒf|Ž | j  '¡  dS )!aÔ  Add labels to the rendering from an anatomical segmentation.

        Parameters
        ----------
        %(aseg)s
        labels : list
            Labeled regions of interest to plot. See
            :func:`mne.get_montage_volume_labels`
            for one way to determine regions of interest. Regions can also be
            chosen from the :term:`FreeSurfer LUT`.
        colors : list | matplotlib-style color | None
            A list of anything matplotlib accepts: string, RGB, hex, etc.
            (default :term:`FreeSurfer LUT` colors).
        %(alpha)s
        %(smooth)s
        fill_hole_size : int | None
            The size of holes to remove in the mesh in voxels. Default is None,
            no holes are removed. Warning, this dilates the boundaries of the
            surface by ``fill_hole_size`` number of voxels so use the minimal
            size.
        legend : bool | None | dict
            Add a legend displaying the names of the ``labels``. Default (None)
            is ``True`` if the number of ``labels`` is 10 or fewer.
            Can also be a dict of ``kwargs`` to pass to
            :meth:`pyvista.Plotter.add_legend`.

        Notes
        -----
        .. versionadded:: 0.24
        r   NÚasegz+`aseg` file path must end with "aseg", got r¼  z.mgzÚreadT)Ú	overwriteZ
must_existr¬  gü©ñÒMbP?r   r   c                 S   s   i | ]\}}||“qS rá   rá   )rô   r\   rÝ   rá   rá   râ   rö   ”	  s      z+Brain.add_volume_labels.<locals>.<dictcomp>c                    s   g | ]}ˆ | ‘qS rá   rá   )rô   r_  )Úlut_rrá   râ   r‚  •	  s     z+Brain.add_volume_labels.<locals>.<listcomp>Zvolume_label_indicesÚfill_hole_sizeÚlegendé   c                    s   g | ]}ˆ | d  ‘qS )r©  rá   ©rô   rò   )Ú	fs_colorsrá   râ   r‚  �	  s     c                 S   s$   g | ]\}}t |d |› d�d�‘qS rÇ  rÈ  rÉ  rá   rá   râ   r‚   	  s   ÿc                    s   g | ]}ˆ | ‘qS rá   rá   ræ  )Úlutrá   râ   r‚  £	  s     )rS  rã  zValue z not found for label z in: rN   FrÕ  Úvolume_labels)(ZnibabelÚendswithrÁ  r=   r²  r³  r£   r¢   Úloadr–   r~  ZdataobjÚheaderZget_vox2ras_tkrr    r#   Úitemsr   r7   rŒ   r²   r“   r‹   r¨   r•   r×  r+   r[  r;   rœ  r/   rÂ   r¼   r€   rž  rd  r’   r¾   Z
add_legendr*  )rÒ   rß  ÚlabelsrÍ  rd   rS  rã  rä  ZnibZ
aseg_fnameZ	aseg_dataZ	vox_mri_tr¤  Zsurfsrò   rb   rÖ  ry   rÜ   rÞ   rá   )rç  rè  râ  râ   Úadd_volume_labels^	  s~    "
ÿ ÿ þ



ÿ  þ"
    þÿÿzBrain.add_volume_labelsc                 C   s   | j ddd� | jj ¡  dS )z1Remove the volume labels from the rendered scene.ré  Tr~   N)rc  r¼   r¾   Zremove_legendrC  rá   rá   râ   Úremove_volume_labels¹	  s    zBrain.remove_volume_labelsÚwhiteé2   c
                 C   sL  | j |dgd�}|r(| j| j| }d}|dk	r”ddlm}
 t| j||| jd| j| j	ddd…f d�}| 
¡  tj|
|j|ƒdd�}| j| j| }t|ƒ}| jd	kr®|d
 }|  |¡D ]:\}}}| jj||d| ||	d� | jjf t| | Ž q¸|}d| j |g ¡k�r$t | j| d |f¡}| j |tƒ ¡| j|< || j| d< dS )a�  Add spherical foci, possibly mapping to displayed surf.

        The foci spheres can be displayed at the coordinates given, or
        mapped through a surface geometry. In other words, coordinates
        from a volume-based analysis in MNI space can be displayed on an
        inflated average surface by finding the closest vertex on the
        white surface and mapping to that vertex on the inflated mesh.

        Parameters
        ----------
        coords : ndarray, shape (n_coords, 3)
            Coordinates in stereotaxic space (default) or array of
            vertex ids (with ``coord_as_verts=True``).
        coords_as_verts : bool
            Whether the coords parameter should be interpreted as vertex ids.
        map_surface : str | None
            Surface to project the coordinates to, or None to use raw coords.
            When set to a surface, each foci is positioned at the closest
            vertex in the mesh.
        scale_factor : float
            Controls the size of the foci spheres (relative to 1cm).
        %(color_matplotlib)s
        %(alpha)s Default is 1.
        name : str
            Internal name to use.
        hemi : str | None
            If None, it is assumed to belong to the hemipshere being
            shown. If two hemispheres are being shown, an error will
            be thrown.
        resolution : int
            The resolution of the spheres.
        rN   rP   Nr   )Úcdistr   )rk   ru   rv   rÉ  r¬  rE  g      $@)r1  rb   rñ  r}   r‘  Zfoci)r‡   r¶   rÄ   Zscipy.spatial.distanceró  r   r¢   r£   r    rÀ   rÁ   r–   r  r>   rÂ   r¼   r  rÌ   r   rµ   rÃ   Úvstackr’   )rÒ   rÄ   Zcoords_as_vertsZmap_surfaceri  rb   rd   rZ   r†   r‘  ró  Z	foci_surfZ	foci_vtxsrÜ   rÝ   Z	data_focirá   rá   râ   Úadd_foci¾	  s<    $
  þ

 þzBrain.add_fociÚoriginalc
                 C   s’  t |tdƒ t|||ƒ\}}}}
t|d|kd|kt|ƒdk|||t|ƒdkd�}t|dddd	�d }~t||dd
�}t|ddd�jdkr¢d|kr¢td| j	| j
ƒ}nd}|  d¡D ]Ò}|jdk�rt| j|||||||
|| jƒ
}| ¡ D ]\}}|D ]}|  ||¡ qòqæd|kr°t|dd�jdkr°t||ƒ}|d | jdk�r@dnd }| jj|j|d fžtd d ddddœŽ\}}|  d|¡ q°| j ¡  dS )aH  Add mesh objects to represent sensor positions.

        Parameters
        ----------
        %(info_not_none)s
        %(trans_not_none)s
        %(meg)s
        %(eeg)s
        %(fnirs)s
        %(ecog)s
        %(seeg)s
        %(dbs)s
        %(verbose)s

        Notes
        -----
        .. versionadded:: 0.24
        rç   Zsensorsr°  r   )ÚmegZref_megÚeegÚecogÚseegÚdbsÚfnirsr»  r¼  Fr½  )r¿  Trá   )rø  ÚexcludeZ	projectedrÒ  NrN   Úhelmet)r÷  rÓ  r¬  r   rE  rÔ  ZcoregZhelmet_colorrþ   )rb   r}   rƒ   r   )r7   r(   r   r'   r“   r1   r2   rq   r$   r¢   r£   rÂ   r   r¼   r    rí  rd  r,   r€   rž  r   r*  )rÒ   rç   rD  r÷  rø  rü  rù  rú  rû  r5   Zwarn_megZpicksrÂ  Zto_cf_tZ	head_surfrÜ   Zsensors_actorsrb  r<  rÞ   rÓ   rÖ  rá   rá   râ   Úadd_sensors
  sr    
 
  
ý  ÿÿ  ÿ        þ
 ÿ
  ýzBrain.add_sensorsc                 C   sh   d}|dkr&|D ]}| j |dd� qn4t|tƒr6|g}|D ]}td||ƒ q:| j |dd� | j ¡  dS )a0  Remove sensors from the rendered scene.

        Parameters
        ----------
        kind : str | list | None
            If None, removes all sensor-related data including the helmet.
            Can be "meg", "eeg", "fnirs", "ecog", "seeg", "dbs" or "helmet"
            to remove that item.
        )r÷  rø  rü  rù  rú  rû  rþ  NFr~   r®  )rc  r‹   rº   r3   r¼   r*  )rÒ   r®  Z	all_kindsrb  Z	this_kindrá   rá   râ   Úremove_sensorsA
  s    

zBrain.remove_sensorsr   c              	   C   s¾   t |tdfdƒ |dkr|n|}d| jkrH|| jd krHtd|› d�ƒ‚|  d¡D ]f\}}}|dksl||krR|dks|||krR| jj|||||	|
d�}d| jkrªtƒ | jd< || jd |< qRdS )a   Add a text to the visualization.

        Parameters
        ----------
        x : float
            X coordinate.
        y : float
            Y coordinate.
        text : str
            Text to add.
        name : str
            Name of the text (text label can be updated using update_text()).
        color : tuple
            Color of the text. Default is the foreground color set during
            initialization (default is black or white depending on the
            background color).
        opacity : float
            Opacity of the text (default 1.0).
        row : int | None
            Row index of which brain to use. Default is the top row.
        col : int | None
            Column index of which brain to use. Default is the left-most
            column.
        font_size : float | None
            The font size to use.
        justification : str | None
            The text justification.
        NrZ   rx  zText with the name z already existsrN   )rv  rw  rx  rb   rq   ry  )r7   rº   r­   r™   rÂ   r¼   r†  r’   )rÒ   rg  rÜ  rx  rZ   rb   r}   r„   r…   Z	font_sizery  rß   rà   rÜ   rÞ   rá   rá   râ   Úadd_textW
  s"         þ
zBrain.add_textc                 C   s¸   t |tdfdƒ |dkrL| jd  ¡ D ]}| jjj|dd� q&| j d¡ n^dg}d| jkrr|t| jd  	¡ ƒ7 }t
d||ƒ | jjj| jd | dd� | jd  |¡ | j ¡  dS )z»Remove text from the rendered scene.

        Parameters
        ----------
        name : str | None
            Remove specific text by name. If None, all text will be removed.
        NrZ   rx  Fr~   )r7   rº   r­   rÒ  r¼   r¾   r@  rA  r¨   r  r3   r*  )rÒ   rZ   rÞ   Únamesrá   rá   râ   Úremove_text‚
  s    
 ÿzBrain.remove_textc                 C   sÒ   ddl m} | jsd S | jd kr*|  ¡  n|  ¡  d| _| j| jddd� | j	dd� | j 
¡  | jD ]f}|| j| j|| jd	�}t | j| jjd
 d¡| j|< || j|< t|ƒD ]\}}|| j| |j< q²qfd S )Nr   )Úread_labels_from_annotrò   r[   g      è?)rb   rd   FrÎ  )rK  Zparcr†   r×   r   r^   )rò   r  rý   r  rÅ  r3  r  Úadd_annotationr  rz  r¤  rˆ   r¢   r£   r–   Úfullr¶   rÄ   rs   r¦   r§   r×  rx   )rÒ   r  r†   rî  r_  rò   rá   rá   râ   r�  ™
  s0    



ü ÿ

z"Brain._configure_label_time_coursec                 C   s¦  ddl m} |  |¡}t|tƒ�rdtj |¡rÚ|}	tj |	¡d }
tj 	|	¡ d¡dd… \}}t
|ƒdkrÒ|dd… dkrš|	t |
d	|dd…  ¡g}qØ|dd… d
krÈt |
d|dd…  |	¡g}qØtdƒ‚n|	g}nRg }|D ]H}t | j| jdd ||dg¡¡}	tj |	¡�s td|	 ƒ‚||	g7 }qâg }t||ƒD ]&\}}	||	ƒ\}}}| ||f¡ �q:nt
|ƒdk�rx|gn|}d}t||ƒD �]
\}\}}|  |||¡ d|dd…df< t t | j¡d ¡ t¡}d|d< ||dd…df dk df  d7  < |||dd…df dkdd…f< t |dk¡�rdt |dd…df dk¡�sdt |t |dgg¡f¡}t |dd…df ¡}|| }t |dd…df |¡}|dd…dd…f }|dd…df }|d ||dk< |dk	�r
t t  t!|ƒd¡¡}| |j"¡|dd…dd…f< | tj#¡}|  $|¡D ]r}| j%| }|j&||t '|¡t (|¡g||d� | j)|  |¡ | j*�rv| j+dk�r | j, -|j.| tj/¡d¡ �q �qŠ| j, 0¡  dS )a÷  Add an annotation file.

        Parameters
        ----------
        annot : str | tuple
            Either path to annotation file or annotation name. Alternatively,
            the annotation can be specified as a ``(labels, ctab)`` tuple per
            hemisphere, i.e. ``annot=(labels, ctab)`` for a single hemisphere
            or ``annot=((lh_labels, lh_ctab), (rh_labels, rh_ctab))`` for both
            hemispheres. ``labels`` and ``ctab`` should be arrays as returned
            by :func:`nibabel.freesurfer.io.read_annot`.
        borders : bool | int
            Show only label borders. If int, specify the number of steps
            (away from the true border) along the cortical mesh to include
            as part of the border definition.
        %(alpha)s Default is 1.
        hemi : str | None
            If None, it is assumed to belong to the hemipshere being
            shown. If two hemispheres are being shown, data must exist
            for both hemispheres.
        remove_existing : bool
            If True (default), remove old annotations.
        color : matplotlib-style color code
            If used, show all annotations in the same (specified) color.
            Probably useful only when showing annotation borders.
        r   )Ú_read_annotr   r§  Nr   r   zlh.rT   zrh.rS   zOTo add both hemispheres simultaneously, filename must begin with "lh." or "rh."rò   r  z!Annotation file %s does not existr­  r©  r^   rä   i   rz   rñ   )1rò   r  Ú_check_hemisr‹   rº   r¬  r­  r®  rO   r¯  r“   r²  r³  rÁ  r£   r¢   r°  r™   r[  r)  Ú_to_bordersr–   r—   rá  r¹   r˜   rŒ   r  rô  rÑ  ZargsortZsearchsortedÚmultiplyr>   r«  Zfloat64rÂ   r¬   rÈ   r[  rî   r«   rš   r  r¼   Ú_set_colormap_rangerÊ   r±  r*  )rÒ   r  r&  rd   r†   rk  rb   r  rJ  r¶  r­  Z	file_hemiZ	filepathsZannotsrî  r¸  rÜ   rr   r/  r·  Z	alpha_vecÚrgbr¹  r€   rá   rá   râ   r  µ
  sˆ    

ýÿ$ ,

û 
 ÿzBrain.add_annotationc                 C   s   d| _ | j ¡  dS )z-Close all figures and cleanup data structure.TN)rÎ   r¼   ÚcloserC  rá   rá   râ   r  '  s    zBrain.closec                 C   s2   ddl m} | j ¡  | jr.|| jjjd ƒ dS )zDisplay the window.r   ré   r   N)r  rê   r¼   rÏ   rž   rÖ   r+  )rÒ   rê   rá   rá   râ   rÏ   ,  s    
z
Brain.showc                 C   s\   t |dƒ}t |dƒ}| jD ]<}|  |¡D ],\}}}||kr(||kr(| j ¡     S q(qdS )aˆ  Get the camera orientation for a given subplot display.

        Parameters
        ----------
        row : int
            The row to use, default is the first one.
        col : int
            The column to check, the default is the first one.

        Returns
        -------
        %(roll)s
        %(distance)s
        %(azimuth)s
        %(elevation)s
        %(focalpoint)s
        r„   r…   )NNNNN)r?   rˆ   rÂ   r¼   Z
get_camera)rÒ   r„   r…   rÛ   rß   rà   rÜ   rá   rá   râ   Úget_view3  s    


zBrain.get_view)r„   r…   r†   rŽ  rö  rø  Ú
focalpointc                C   s*  t |ddƒ t |ddƒ |dkr&| jn|}|dkr`| jdkrD|dksV| jdkr\|dkr\d	}nd
}t |tdfdƒ t||	|||
d�}|dk	r²dd„ | ¡ D ƒ}tt|  |¡f|Ž}|r¼| jnd}| j	D ]T}|  
|¡D ]D\}}}|dksî||krÔ|dk�s ||krÔ| jjf |d|dœ—Ž qÔqÆ| j ¡  dS )aÌ  Orient camera to display view.

        Parameters
        ----------
        %(view)s
        %(roll)s
        %(distance)s
        row : int | None
            The row to set. Default all rows.
        col : int | None
            The column to set. Default all columns.
        hemi : str | None
            Which hemi to use for view lookup (when in "both" mode).
        %(align_view)s
        %(azimuth)s
        %(elevation)s
        %(focalpoint)s

        Notes
        -----
        The builtin string views are the following perspectives, based on the
        :term:`RAS` convention. If not otherwise noted, the view will have the
        top of the brain (superior, +Z) in 3D space shown upward in the 2D
        perspective:

        ``'lateral'``
            From the left or right side such that the lateral (outside)
            surface of the given hemisphere is visible.
        ``'medial'``
            From the left or right side such that the medial (inside)
            surface of the given hemisphere is visible (at least when in split
            or single-hemi mode).
        ``'rostral'``
            From the front.
        ``'caudal'``
            From the rear.
        ``'dorsal'``
            From above, with the front of the brain pointing up.
        ``'ventral'``
            From below, with the front of the brain pointing up.
        ``'frontal'``
            From the front and slightly lateral, with the brain slightly
            tilted forward (yielding a view from slightly above).
        ``'parietal'``
            From the rear and slightly lateral, with the brain slightly tilted
            backward (yielding a view from slightly above).
        ``'axial'``
            From above with the brain pointing up (same as ``'dorsal'``).
        ``'sagittal'``
            From the right side.
        ``'coronal'``
            From the rear.

        Three letter abbreviations (e.g., ``'lat'``) of all of the above are
        also supported.
        )zint-likeNr„   r…   NrO   rK   r   rW   rT   rS   rÿ   )rö  rø  ÚrollÚdistancer  c                 S   s   i | ]\}}|d k	r||“qS rƒ  rá   )rô   ÚparamrÚ   rá   rá   râ   rö   —  s    ÿ z#Brain.show_view.<locals>.<dictcomp>F)rƒ   Zrigid)r7   rŸ   rŠ   rº   r’   rí  r   rÃ   rÀ   rˆ   rÂ   r¼   rÌ   r*  )rÒ   rÿ   r  r  r„   r…   r†   rŽ  rö  rø  r  Zview_paramsrè   rÛ   rß   rà   rÜ   rá   rá   râ   rÐ   N  s:    <ÿÿ ÿ
"  ÿzBrain.show_viewc                 C   sD   | j D ]8}|  |¡D ](\}}}| jjf t| | ddi—Ž qqdS )zReset the camera.rƒ   FN)rˆ   rÂ   r¼   rÌ   r   )rÒ   rÛ   rÜ   rÝ   rá   rá   râ   rR  ¢  s
    
ÿzBrain.reset_viewr  c                 C   s(   |dkrt dƒ}t|| j|dd�ƒ dS )zÙSave view from all panels to disk.

        Parameters
        ----------
        filename : str
            Path to new image file.
        mode : str
            Either 'rgb' or 'rgba' for values to return.
        Nz.pngT)r¥  rš   )r   r   rë  )rÒ   rê  r¥  rá   rá   râ   rè  ©  s    
 ÿzBrain.save_imagec                    sn  |dkrdnd}| j  |¡}t d|j› �¡ |�rj| j�rj| j�rj| j�sjddlm	} | j
jj}| ¡  | j
j}tƒ �Â}| ¡ }	| ¡ ‰ t‡ fdd„|	D ƒƒ}
|
d |
d	  }t d
|	› dˆ › d|
› d|› d�	¡ |j|ˆ d| jdd� | d¡ ||dd�dd…dd…d|…f }t t |d ¡dd¡ tj¡}W 5 Q R X t | jd|… ¡d }t||g||d�}|S )a'  Generate a screenshot of current view.

        Parameters
        ----------
        mode : str
            Either 'rgb' or 'rgba' for values to return.
        %(time_viewer_brain_screenshot)s

        Returns
        -------
        screenshot : array
            Image pixel values.
        r  r   rä   zGot screenshot of size r   )Úimreadc                 3   s   | ]}|ˆ  V  qd S rƒ  rá   )rô   rg  ©rZ  rá   râ   rÈ  Ú  s     z#Brain.screenshot.<locals>.<genexpr>r   zSaving figure of size z @ z DPI (z = z pixels)ZpngÚnone)rZ  rP  Z	facecolorZ	edgecolor)rP  Nr©  )rr   Ú
n_channels)r¼   rë  r4   r»   rs   rš   rý   r  Zmatplotlib.imager  r  rt   ÚcanvasZ	draw_idler   Zget_size_inchesZget_dpir•   Zsavefigr�   Úseekr–   rù  r—   r˜   r±  rá  r¹   r   )rÒ   r¥  rš   r  Zimgr  r  rt   ÚoutputZsize_inZ	want_sizeZn_pixZ	trace_imgrr   rá   r  râ   rë  ¸  sL    ÿþ
ÿ
 ÿ
"  ÿÿ
ÿzBrain.screenshotc                 c   s$   | j }|| _ z
d V  W 5 || _ X d S rƒ  )r¯   )rÒ   ÚwhyÚorigrá   rá   râ   Ú_no_lut_updateï  s
    
zBrain._no_lut_updatec              	      s  |› d|› d|› d|› �}ˆ j dk	r8t d|› �¡ dS t d|› �¡ ˆ jd }ˆ jd }ˆ jd }‡ fdd	„d
D ƒ}	t|	|||d
� tdd„ |	 ¡ D ƒƒs¢t‚ˆ j |	¡ t	 
t|fd||dœ|	—Žd ¡ t	j¡ˆ jd< ˆ j}
ˆ jd }dD ]Ú}ˆ j |¡}|dk	rò|ˆ jk�rRˆ j| }|jdˆ jd ||
d� ˆ j |j|ˆ j|
ˆ j¡ | d¡}| d¡}||fD ]*}|dk	�rnˆ j |||d ˆ j|
¡ �qn| d¡}|dk	rò|D ]}ˆ j ||ˆ j|
¡ �q°qòˆ j�rˆ  d|› �¡�$ d
D ]}ˆ j| |	| ƒ �qìW 5 Q R X ˆ j ¡  dS )znUpdate color map.

        Parameters
        ----------
        %(fmin_fmid_fmax)s
        %(alpha)s
        rL  NzLUT update postponed with zUpdating LUT with r1  rj   rf  c                    s   i | ]}|ˆ j | “qS rá   ©rµ   ró   rC  rá   râ   rö   	  s      z$Brain.update_lut.<locals>.<dictcomp>rø   c                 s   s   | ]}|d k	V  qd S rƒ  rá   )rô   rÚ   rá   rá   râ   rÈ    s     z#Brain.update_lut.<locals>.<genexpr>rE   )rd   r1  rf  r©  r¹  rU   rŠ  )rZ   rj   r}   r|   r—  r˜  rd   rs  zupdate_lut )r¯   r4   r»   rµ   Ú_update_monotonicÚallrÒ  r‰   r‚   r–   r—   r   r˜   r±  Ú_cmap_rangerÃ   r¬   Úupdate_overlayr¼   r  rÊ   r³   r¹   Z_set_volume_rangerš   r  r  r*  )rÒ   rù   rú   rû   rd   Úargsr1  rj   rf  Úlimsr|   r¹  r†   rÞ  r€   r—  r˜  Zgrid_volumers  Zglyph_actor_rõ   rá   rC  râ   rƒ  ø  s†    	




ÿÿþÿý


ý   þ


   þ
   ÿ
 zBrain.update_lutc              
   C   sú   ddl m} dD ]Ê}| j |¡}|dk	rt|d ƒ| j| jjd krHq|d }|dkrztdt|ƒ| j| jjd f ƒ‚|d	kr†d
n|}t	dƒ�4 || j| j
t t| j| jƒ¡||ddd�}W 5 Q R X || j| d< q|  | jd ¡ || jd< dS )z�Set the number of smoothing steps.

        Parameters
        ----------
        n_steps : int
            Number of smoothing steps.
        r   )Ú_hemi_morphrR   Nrá  r   rx   zClen(data) < nvtx (%s < %s): the vertices parameter must not be Noner^   rg   F)Úmapsr;   râ  rs  r¹  )Zmorphr$  rµ   rÃ   r“   r¶   rg  rs   r™   r8   Ú
orig_facesr–   rY  rÄ   rJ  )rÒ   Zn_stepsr$  r†   rÞ  rx   Zmorph_n_stepsrâ  rá   rá   râ   rº  5  s4    þÿ

   ýzBrain.set_data_smoothingc                 C   s   | j d k	rt| j ƒS d S rƒ  )r¥   r“   rC  rá   rá   râ   rZ  S  s    zBrain._n_timesc                 C   s   | j S )zThe interpolation mode.)Ú_time_interpolationrC  rá   rá   râ   r…  W  s    zBrain.time_interpolationc                 C   s„   t d|dƒ| _tƒ | _d| _| jdk	r€t | j¡}dD ]:}| j	 
|¡}|dk	r6|d }t||| jddd�| j|< q6t|| jƒ| _dS )	zpSet the interpolation mode.

        Parameters
        ----------
        %(interpolation_brain_time)s
        Úinterpolation)Úlinearrg   ÚzeroZslinearZ	quadraticZcubicNrU   rá  r^   T©rÊ  Úassume_sorted)r3   r'  r’   Ú_time_interp_funcsÚ_time_interp_invr¥   r–   rY  rZ  rµ   rÃ   Ú_safe_interp1d)rÒ   r(  r_  r†   rÞ  rá  rá   rá   râ   r·   \  s*    ý
   þzBrain.set_time_interpolationc                 C   sÜ  t ƒ | _| j dd¡}| j dd¡}dD �]˜}| j |¡}|dk	r(|d }d}|jdkrd|}d| _nD| j| |ƒ}|  |¡| _|jdkrœ|}tj	j
|dd	�}|  |¡| _|| j|< |dk	rÒ|dk	rÒ| || jƒ¡ | d
¡}	|	dk	�rB| jd d }
| jd }| j}| jd dk	�rdn|d }t|	ƒd  |¡ |t|	ƒd |
< | d¡}|dk	�r`| |¡}|| jk�r®| j| }d|jk�r’|jd|d� n|j|| jd | jddd� |dk	r(|  ||¡ q(|| jd< | j ¡  dS )zìSet the time point shown (can be a float to interpolate).

        Parameters
        ----------
        time_idx : int | float
            The time index to use. Can be a float to use interpolation
            between indices.
        r0  Nrx  rU   rá  r   r   r   rÉ  rÐ  rN   rx   r1  rÒ  râ  rŠ  )rZ   r{   r¹  rz   rs  )r’   Ú_current_act_datarµ   rÃ   rã  rI  r-  r.  r–   rÓ  rÔ  r  r   r    ÚfillrO  r¬   Z	_overlaysr!  rÈ   Ú_update_glyphsr¼   r*  )rÒ   rs  r0  rx  r†   rÞ  rá  ÚvectorsrÝ  rÐ  rx   rÒ  r|   r1  râ  r€   rá   rá   râ   rJ  v  s\    	










û	
zBrain.set_time_pointc              	   C   sˆ   | j dkrtdƒ‚npt| j ƒ|  kr4t| j ƒkr\n n$|  t t|ƒ| j t | j	¡¡¡ n(td|› dt| j ƒ› dt| j ƒ› d�ƒ‚dS )z”Set the time to display (in seconds).

        Parameters
        ----------
        time : float
            The time to show, in seconds.
        Nz0Cannot set time when brain has no defined times.zRequested time (z- s) is outside the range of available times (rd  z s).)
r¥   r™   r[  rî   rJ  r–   r\  r¡   rY  rZ  )rÒ   r›   rá   rá   râ   Úset_time¾  s    
ÿ$
ÿ"ÿzBrain.set_timec                 C   s°  | j  |¡}|d k	st‚|d }| j d }| j d }|d krDtd ƒn|}t | j| j¡| j\}}}	|d d kr‚d}
t	ƒ |d< nd}
d}|  
|¡D �]}|d d k�r| jj|||	|d d …df |d d …d	f |d d …d
f d dd||t|ƒd d�\}}||d< ||d< n|d }|t|ƒd< |d }|
�rz| j |¡}| ¡ }| d¡ | |¡ | jjj|dd� |d  |¡ n|d | }|d	7 }| jj|| j d d | jd� q”d S )Nrx   rh  ri  rs  TFr   rq  r   r   Z2darrowZvectorZ_glyph)rb   r¥  Z
scale_moderñ  r}   rZ   rr  Zvecrì   r~   r¹  )rÞ   r¹  Z
scalar_barr|   )rµ   rÃ   r‰   Úslicer–   rá  r¶   rÄ   rž  r¨   rÂ   r¼   rÌ  rº   r   rÊ   ZGetPropertyZSetLineWidthr¡  r¾   rË   r)  r  r   )rÒ   r†   r3  rÞ  rx   rh  ri  rg  rÜ  ÚzÚaddÚcountrÜ   rr  rq  rs  r¥  rá   rá   râ   r2  Ñ  s`    

    
ø




üzBrain._update_glyphsc                 C   s8   | j d }| j d d kr$| j d }nd| }||g}|S )Nrû   r1  rù   r^   r  )rÒ   Zdt_maxZdt_minr|   rá   rá   râ   r     s    
zBrain._cmap_rangec                 C   s>   | j d | }| j d | }| j d | }| j|||d� dS )zScale the colorbar points.rù   rú   rû   rø   N)rµ   rƒ  )rÒ   Zfscalerù   rú   rû   rá   rá   râ   Ú_update_fscale  s    zBrain._update_fscalec              	   C   sè   | j d }|d k	r d|kr d}nd}|d k	r6|r6|}nd}| j d }| j d }t|||t t| j ¡ ƒ¡|ƒ}d|d k}|d }|d |r�dnd }	|d }~|	\}
}}|r²d	nd }|| j d
< || j d< || j d< | j|
||d� d S )Nrp  r#  FTrH   rj   rf  Zpos_limsro   r1  rø   )rµ   r   r–   rÑ  r¨   r0  rÒ  rƒ  )rÒ   rE  Z	user_climZallow_pos_limsrp  rj   rf  ZmapdataZ	divergingZ	scale_ptsrù   rú   rû   r1  rá   rá   râ   rB    s6    


  ý



zBrain._update_auto_scalingc                 C   s&   | j d }t ||t t|ƒ¡¡}|S )z;Return the interpolated time index of the given time value.r›   )rµ   r–   r\  rY  r“   )rÒ   r@  r›   rá   rá   râ   r  0  s    
zBrain._to_time_indexc                 C   s   | j S )z/Data used by time viewer and color bar widgets.r  rC  rá   rá   râ   rŠ  6  s    z
Brain.datac                 C   s   | j S rƒ  )r©   rC  rá   rá   râ   rî  ;  s    zBrain.labelsc                 C   s   | j S rƒ  )r¤   rC  rá   rá   râ   rØ   ?  s    zBrain.viewsc                 C   s   | j S rƒ  )rˆ   rC  rá   rá   râ   rJ  C  s    zBrain.hemisr0  é   c              
   K   sx   dd l }| j ¡ � |  ||||||	|
¡}W 5 Q R X d|krD||d< |d k	rT||d< |d k	rd||d< |j||f|Ž d S )Nr   ZfpsÚcodecÚbitrate)Úimageior¼   Z_disabled_interactionÚ_make_movie_framesZmimwrite)rÒ   rê  rí  ÚtminÚtmaxÚ	framerater(  r;  r<  rr  rš   r‰  r=  Úimagesrá   rá   râ   Ú_save_movieG  s$         þzBrain._save_moviec                    s’   ‡ ‡fdd„}ˆj  ¡ }ˆj  ˆj  d¡¡ zRz$ˆjˆ |||||||||
f
|Ž W n( ttfk
rz   tdt 	¡  ƒ Y nX W 5 ˆj  |¡ X d S )Nc                    s¦   | |kr8ˆj  dˆ  ¡ ˆj  ¡  ˆj ¡  ˆj ¡  nVˆj  d| d |f ¡ ˆj  ¡  ˆj ¡  ˆj d|d g¡ ˆj | ¡ ˆj ¡  ˆj  ¡  ˆj ¡  d S )NzSaving with ImageIO: %sz$Rendering images (frame %d / %d) ...r   r   )	r  rm  rÏ   r  r	  r¼   Z_status_bar_updateZ	set_ranger‚   ©ÚframeÚn_frames©rê  rÒ   rá   râ   Úframe_callback\  s*    ÿÿ


ÿÿ



z,Brain._save_movie_tv.<locals>.frame_callbackZ
WaitCursorzMovie saving aborted:
)
r¼   Z_window_get_cursorZ_window_set_cursorZ_window_new_cursorrC  ræ   ÚKeyboardInterruptr;   Ú	tracebackÚ
format_exc)rÒ   rê  rí  r?  r@  rA  r(  r;  r<  rr  rš   r‰  rH  Zdefault_cursorrá   rG  râ   Ú_save_movie_tvX  s&    

ÿ    þþzBrain._save_movie_tvc                 K   sD   |dkrt dƒ}| jr| jn| j}||||||||||	|
f
|Ž dS )aŽ  Save a movie (for data with a time axis).

        The movie is created through the :mod:`imageio` module. The format is
        determined by the extension, and additional options can be specified
        through keyword arguments that depend on the format, see
        :doc:`imageio's format page <imageio:formats/index>`.

        .. Warning::
            This method assumes that time is specified in seconds when adding
            data. If time is specified in milliseconds this will result in
            movies 1000 times longer than expected.

        Parameters
        ----------
        filename : str
            Path at which to save the movie. The extension determines the
            format (e.g., ``'*.mov'``, ``'*.gif'``, ...; see the :mod:`imageio`
            documentation for available formats).
        time_dilation : float
            Factor by which to stretch time (default 4). For example, an epoch
            from -100 to 600 ms lasts 700 ms. With ``time_dilation=4`` this
            would result in a 2.8 s long movie.
        tmin : float
            First time point to include (default: all data).
        tmax : float
            Last time point to include (default: all data).
        framerate : float
            Framerate of the movie (frames per second, default 24).
        %(interpolation_brain_time)s
            If None, it uses the current ``brain.interpolation``,
            which defaults to ``'nearest'``. Defaults to None.
        codec : str | None
            The codec to use.
        bitrate : float | None
            The bitrate to use.
        callback : callable | None
            A function to call on each iteration. Useful for status message
            updates. It will be passed keyword arguments ``frame`` and
            ``n_frames``.
        %(time_viewer_brain_screenshot)s
        **kwargs : dict
            Specify additional options for :mod:`imageio`.
        Nz.mp4)r   rš   rL  rC  )rÒ   rê  rí  r?  r@  rA  r(  r;  r<  rr  rš   r‰  r»  rá   rá   râ   rî  �  s    /
    þþzBrain.save_moviec              	      sj  ddl m} |d kr ˆ jd }n$|ˆ jd k rDtd|ˆ jd f ƒ‚|d krXˆ jd }n$|ˆ jd kr|td|ˆ jd f ƒ‚||| | | ƒ}	tj|	td�}
|
||  }
|
|7 }
t |
ˆ jt ˆ j¡¡}t	|ƒ}|dkrâtdƒ‚t
 d|
|f ¡ ˆ jˆd	� ˆ j}|d k	�rˆ  |¡ z ‡ ‡fd
d„ˆ  ||¡D ƒ}W 5 ˆ  |¡ X |d k	�rf|t	|ƒt	|ƒd� |S )Nr   )Úfloorz1tmin=%r is smaller than the first time point (%r)r^   z2tmax=%r is greater than the latest time point (%r)rª  zNo time points selectedz(Save movie for time points/samples
%s
%s©rš   c                    s   g | ]}ˆ j ˆd �‘qS )rN  )rë  )rô   rÜ   ©rÒ   rš   rá   râ   r‚  Û  s   ÿz,Brain._make_movie_frames.<locals>.<listcomp>rD  )ÚmathrM  r¥   r™   r–   rY  r¡   r\  rZ  r“   r4   r»   rë  r…  r·   Ú
_iter_time)rÒ   rí  r?  r@  rA  r(  rr  rš   rM  rF  r^  rs  Zn_timesZold_moderB  rá   rO  râ   r>  ·  sH    ÿÿÿ


þ

zBrain._make_movie_framesc                 c   sn   | j rt| jd dd�}n| j}| jd }t|ƒD ].\}}||ƒ |dk	rZ||t|ƒd� |V  q2||ƒ dS )aÇ  Iterate through time points, then reset to current time.

        Parameters
        ----------
        time_idx : array_like
            Time point indexes through which to iterate.
        callback : callable | None
            Callback to call before yielding each frame.

        Yields
        ------
        idx : int | float
            Current index.

        Notes
        -----
        Used by movie and image sequence saving functions.
        r›   TrP  rs  NrD  )rš   r   r  rJ  rµ   r×  r“   )rÒ   rs  rr  r»  Zcurrent_time_idxr  r_  rá   rá   râ   rQ  ä  s    
ÿ
zBrain._iter_timec                 C   s  ddl m}m}m}m} t||ƒrö|}d  }	}
t||ƒr>|}	nXt||ƒrr|dkrX| ¡ nd }	|dkrl| ¡ nd }
n$t||ƒrŽ|dkrˆ|nd }
ntdƒ‚|	d kr®|
d kr®t	dƒ‚|	d k	rÚt
|	|d ƒ}|	j|dkrÔdnd	 }|
d k	rú|
j}t |
j¡}nd }|||fS )
Nr   )Ú_BaseSourceEstimateÚ_BaseSurfaceSourceEstimateÚ_BaseMixedSourceEstimateÚ_BaseVolSourceEstimaterN   zstc not supportedzNo data to be addedrµ   rS   r   r   )r±  rR  rS  rT  rU  r‹   Úsurfacer™  r�  r™   r7  rx   rŠ  r–   rÑ  )rÒ   r†   rá  rx   rR  rS  rT  rU  rï   Zstc_surfZstc_volrá   rá   râ   r}    s,    



zBrain._check_stcrá   c                 C   sH   t |dtfdƒ |dkr0| jdkr*tdƒ‚| j}td|dt|ƒ ƒ |S )z.Check for safe single-hemi input, returns str.Nr†   rR   z9hemi must not be None when both hemispheres are displayed)r7   rº   rŸ   r™   r3   r•   )rÒ   r†   rQ   rá   rá   râ   r‡   $  s    
zBrain._check_hemic                 C   sX   |dkr&| j dkrddg}qT| j g}n.|dkrN| j dkr<dnd}td| ƒ‚n|g}|S )z7Check for safe dual or single-hemi input, returns list.NrR   rS   rT   z or NonerM  z hemi must be either "lh" or "rh")rŸ   r™   )rÒ   r†   rm   rá   rá   râ   r  /  s    


zBrain._check_hemisc                 C   s  t |ttfƒr|dk rtdƒ‚|�r|j}t| j| jƒ}| ¡ }||j	 ||j
 k}tj|tjd�}t |j	| ¡}	t |tƒròt|ƒD ]P}
t | j| j ¡ |	¡}	| j| jj|	_| j| jtj|	dd� }	t |	¡}	qˆ|dk	rò|	t |	|¡ }	d||	< ||9 }dS )z Convert a label/parc to borders.r   z*borders must be a bool or positive integerrª  r   rÉ  N)r‹   r²   rŒ   r™   rq   r)   r¶   r&  Ztocoor„   r…   r–   rŸ  r²  r´  r‹  r³  r™  rs   r  )rÒ   rò   r†   r&  Zrestrict_idxZ
n_verticesÚedgesZborder_edgesrÏ   rº  rÜ   rá   rá   râ   r	  =  s0    
 ÿ
ÿzBrain._to_bordersc                 C   s   t | dƒr| jS dS )z«Return the vertices of the picked points.

        Returns
        -------
        points : list of int | None
            The vertices picked by the time viewer.
        rš   N)r  r  rC  rá   rá   râ   Úget_picked_pointsU  s    
zBrain.get_picked_pointsc                 C   s   | j S )zHash the object.)r�   rC  rá   rá   râ   Ú__hash__`  s    zBrain.__hash__)rB   rC   NrD   rE   rF   rG   NNNrH   rH   FFrI   rJ   rK   FNTF)TT)N)N)N)T)T)T)T)F)NNNNNFrH   r   NNNrH   TNNNNNNNNre  NN)Nr   NFNNT)r   N)rÆ  r   N)TrÑ  r`  )TrÑ  r`  )rÞ  NNr`  ra   NN)FNr   rñ  r   NNrò  )Nrö  TTTTN)N)NNrE   r   r   NN)N)Tr   NTN)r   r   )NNN)Nr  )r  F)NNNN)F)	r0  NNr:  NNNNF)	r0  NNr:  NNNNF)
Nr0  NNr:  NNNNF)rá   )N)xÚ__name__Ú
__module__Ú__qualname__Ú__doc__rã   r¿   r-  r   r½   r)  rD  rF  rN  rS  rU  rX  rW  r  r   rp  r€  r�  r˜  r·  r$  r'  rÅ  rœ  r"  r#  rõ  r÷  rú  r!  r%  r&  rä  rå  ræ  rç  r¢  r'  rÛ  r  r3  r3  rz  r(  rò  r5  r6  ÚpropertyrÍ   Úsetterr¸   rc  rd  r5   rŠ  rŒ  rÂ   r›  r4  r„  r(  r6   rÄ  rÅ  rÏ  rÐ  r×  rØ  rÜ  rÝ  rï  rð  rõ  rÿ  r   r  r  r�  r  r  rÏ   r  rÐ   rR  rè  rë  Ú
contextlibÚcontextmanagerr  rƒ  rº  rZ  r…  r·   rJ  r4  r2  r   r9  rB  r  rŠ  rî  rØ   rJ  rC  rL  rî  r>  rQ  r}  r‡   r  r	  rX  rY  rá   rá   rá   râ   rA   6   sä   5                               ú
 *

~
"




?1>Q
3AY
AA	

%                                   ù t	
o      þ
 (2      þZ          þG       ÿ:       ÿ
+   ÿq     þS6
<


H2
	



          þ
          þ
)          þ5-"rA   r)  r^   Fc                    sB   ddl m} ˆjˆ  dkr,‡ ‡fdd„}|S || ˆ|ˆ |d�S dS )z5Work around interp1d not liking singleton dimensions.r   )Úinterp1dr   c                    s    t jˆt  t  | ¡jt¡ˆ d�S )NrÉ  )r–   ZtakerŸ  r~  rs   rŒ   )rg  ©rÊ  rÜ  rá   râ   r»  i  s    z_safe_interp1d.<locals>.funcr+  N)Zscipy.interpolaterb  rs   )rg  rÜ  r®  rÊ  r,  rb  r»  rá   rc  râ   r/  e  s
    r/  c                 C   sÈ   |d krF| d kr&|j dkr"| ¡ nd} |d krz|j dkr@| ¡ nd}n4| d krRd} |d krz|j dkrvt || ¡ ¡ nd}|d krŽ| | d }| |kr¦td| |f ƒ‚||kr¾td||f ƒ‚| ||fS )Nr   r   rì   z%min must be < mid, got %0.4g >= %0.4gz%mid must be < max, got %0.4g >= %0.4g)rq   r[  rî   r–   rÚ  rÁ  )rù   rú   rû   r1  rá  rá   rá   râ   r€  p  s(     ÿÿr€  c                 C   sÈ  |d k	rl|| d< | d |k r>t  d| d › d|› �¡ || d< | d |k rlt  d| d › d|› �¡ || d< | d | d   krŒ| d ks’n t‚|d k	rþ|| d< | d |krÐt  d| d › d|› �¡ || d< | d |k rþt  d| d › d|› �¡ || d< | d | d   k�r"| d k�s(n t‚|d k	�rš|| d< | d |k�rjt  d| d › d|› �¡ || d< | d |k�ršt  d| d › d|› �¡ || d< | d | d   k�r¾| d k�sÄn t‚d S )Nrù   rû   z    Bumping fmax = z to rú   z    Bumping fmid = z    Bumping fmin = )r4   r»   r‰   )r#  rù   rú   rû   rá   rá   râ   r  ˆ  s6    &*
r  c                 C   s.   t  t  t| j ¡ ƒ¡¡}t  |¡t  |¡gS rƒ  )r–   rÚ  rÑ  r¨   r0  rÒ  r[  rî   )rq  rÚ   rá   rá   râ   r–  ¦  s    r–  c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )r;  c                 C   s   d S rƒ  rá   rC  rá   rá   râ   Ú
EnterEvent¬  s    z_FakeIren.EnterEventc                 C   s   d S rƒ  rá   rC  rá   rá   râ   ÚMouseMoveEvent¯  s    z_FakeIren.MouseMoveEventc                 C   s   d S rƒ  rá   rC  rá   rá   râ   Ú
LeaveEvent²  s    z_FakeIren.LeaveEventc                 O   s   d S rƒ  rá   ©rÒ   r"  r‰  rá   rá   râ   ÚSetEventInformationµ  s    z_FakeIren.SetEventInformationc                 C   s   d S rƒ  rá   rC  rá   rá   râ   Ú	CharEvent¸  s    z_FakeIren.CharEventc                 O   s   d S rƒ  rá   rg  rá   rá   râ   ÚKeyPressEvent»  s    z_FakeIren.KeyPressEventc                 O   s   d S rƒ  rá   rg  rá   rá   râ   ÚKeyReleaseEvent¾  s    z_FakeIren.KeyReleaseEventN)
rZ  r[  r\  rd  re  rf  rh  ri  rj  rk  rá   rá   rá   râ   r;  «  s   r;  )r)  r^   F)_r`  Ú	functoolsr   Úior   r¬  Úos.pathr­  r²  r›   r   rJ  rÀ  r¯  Únumpyr–   rj   r   rV  r   rÿ   r   r   rr  r	   r
   r   r   r   Úutilsr   r   r   r   r   r   ra  r   r   r   r   r   r   Z_3d_overlayr   Údefaultsr   r   Úfixesr   r    Z_freesurferr!   r"   r#   r$   r%   r&   Zio.pickr'   Zio.meas_infor(   r)   r*   r+   r,   Zsource_spacer-   Z
transformsr.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r  ÚobjectrA   r/  r€  r  r–  r;  rá   rá   rá   râ   Ú<module>
   s|      <                            J
