U
    f»{js’  ã                   @   sä  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl	m
Z ddlmZ ddlZddlZddlZddlZddlZddlmZmZ d	d
lmZmZmZ dŠddœdd„Zd‹dd„ZdŒdddœdd„Zdd„ Zd�dd„Zdd„ Z d d!„ Z!d"d#„ Z"d$d%„ Z#d&d'„ Z$edŽdd)œd*d+„ƒZ%dd,d-d.œd/d0„Z&d1d2„ Z'd�d4d5„Z(d�d6d7„Z)d‘d8d9„Z*d’d:d;„Z+d“d<d=„Z,d”d>d?„Z-d•d@dA„Z.dBdC„ Z/d–dDdE„Z0d—dFdG„Z1dHdI„ Z2G dJdK„ dKe3ƒZ4e4ƒ Z5e6eej7fZ8G dLdM„ dMe3ƒZ9e6fej:e;e5fe8e5fe9ƒ fdNœZ<d˜ddœdOdP„Z=dQdR„ Z>d™dTdU„Z?dšdVdW„Z@dXdY„ ZAdZd[„ ZBd\d]„ ZCd›d^d_„ZDdœdadb„ZEdcdd„ ZFd�dedf„ZGdgdh„ ZHdždjdk„ZIdldm„ ZJdŸdodp„ZKddqœdrds„ZLd dudv„ZMdwdx„ ZNd¡dzd{„ZOd¢d}d~„ZPd£dd€„ZQddd�œd‚dƒ„ZRd„d…„ ZSd†dd‡œdˆd‰„ZTdS )¤zThe check functions.é    )Úinput)Úget_close_matches)Úimport_moduleN)ÚPathé   )Ú_median_complexÚ_compare_versioné   )ÚwarnÚloggerÚverboseÚunknownúan intÚ )Úextrac                C   sn   |rd|› �n|}z"t | tƒr$tƒ ‚tt | ¡ƒ} W n4 tk
rh   t|› d|› |› dt| ƒ› �ƒ‚Y nX | S )z Ensure a variable is an integer.ú z	 must be ú, got )Ú
isinstanceÚboolÚ	TypeErrorÚintÚoperatorÚindexÚtype)ÚxÚnameZmust_ber   © r   úN/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/utils/check.pyÚ_ensure_int   s    
&r   r   c                 C   sœ   t | ddƒ t| ƒ} t|ƒdkr\|  |¡s\d d |dd… ¡|d g¡}td| ||f ƒ‚d d |dd… ¡|d g¡}|  |¡s˜td	| ||f ƒ dS )
a>  Enforce MNE filename conventions.

    Parameters
    ----------
    fname : str
        Name of the file.
    filetype : str
        Type of file. e.g., ICA, Epochs etc.
    endings : tuple
        Acceptable endings for the filename.
    endings_err : tuple
        Obligatory possible endings for the filename.
    ú	path-likeÚfnamer   ú or ú, Néÿÿÿÿz3The filename (%s) for file type %s must end with %sz^This filename (%s) does not conform to MNE naming conventions. All %s files should end with %s)Ú_validate_typeÚstrÚlenÚendswithÚjoinÚIOErrorr
   )r    ZfiletypeZendingsZendings_errZprint_endingsr   r   r   Úcheck_fname)   s    ÿÿ 
þr*   ú0.0TF)ÚstripÚreturn_versionc          	      C   s€   d}d}zt | ƒ} W n tk
r,   d}Y n@X |o8|dk}|p@|}|rX| j}|rXt|ƒ}|rlt|d|ƒrld}|rx||fn|}|S )aÈ  Check minimum library version required.

    Parameters
    ----------
    library : str
        The library name to import. Must have a ``__version__`` property.
    min_version : str
        The minimum version string. Anything that matches
        ``'(\d+ | [a-z]+ | \.)'``. Can also be empty to skip version
        check (just check for library presence).
    strip : bool
        If True (default), then PEP440 development markers like ``.devN``
        will be stripped from the version. This makes it so that
        ``check_version('mne', '1.1')`` will be ``True`` even when on version
        ``'1.1.dev0'`` (prerelease/dev version). This option is provided for
        backward compatibility with the behavior of ``LooseVersion``, and
        diverges from how modern parsing in ``packaging.version.parse`` works.

        .. versionadded:: 1.0
    return_version : bool
        If True (default False), also return the version (can be None if the
        library is missing).

        .. versionadded:: 1.0

    Returns
    -------
    ok : bool
        True if the library exists with at least the specified version.
    version : str | None
        The version. Only returned when ``return_version=True``.
    TNFr+   ú<)r   ÚImportErrorÚ__version__Ú
_strip_devr   )	ZlibraryÚmin_versionr,   r-   ÚokÚversionÚcheck_versionÚget_versionÚoutr   r   r   r5   E   s"    "
r5   c                 C   s(   d}t  || ¡}|d k	r$| ¡ d S | S )Nz5^([0-9]+(?:\.[0-9]+)*)\.?(?:dev|rc|\+)[0-9+a-g\.\-]+$r   )ÚreÚmatchÚgroups)r4   Úexpr9   r   r   r   r1   {   s    r1   c              	   C   sl   t | |dd�\}}|sh|dkr*d|› d�nd}|dkr:dnd	t|ƒ› �}td
| › d|› d|› d|› �ƒ‚dS )zRequire library for a purpose.T)r-   r+   z (version >= ú)r   Nzpackage was not foundzgot zThe z packagez is required to r"   )r5   Úreprr/   )ÚlibÚwhatr4   r3   Úgotr   Úwhyr   r   r   Ú_require_versionŽ   s
    rB   c                  C   s   t ddƒ dd l} | S )NÚh5pyzread MATLAB files >= v7.3r   )rB   rC   )rC   r   r   r   Ú_import_h5py˜   s    
rD   c                  C   s   t ddƒ} | j| jfS )NÚh5iozHDF5-based I/O)Ú_soft_importZ	read_hdf5Z
write_hdf5)rE   r   r   r   Ú_import_h5io_funcsž   s    
rG   c                 C   s   t d| ƒ}|jS )NÚpymatreader)rF   Zread_mat)ÚpurposerH   r   r   r   Ú_import_pymatreader_funcs£   s    
rJ   c                 C   sŒ   | dks| t jkrt jjjS t| tt jfƒr:t jj | ¡S t| t jjjƒrN| S zt| t jjƒrd| W S W n t	k
rz   Y nX t
d|  ƒ‚dS )a5  Turn seed into a numpy.random.mtrand.RandomState instance.

    If seed is None, return the RandomState singleton used by np.random.mtrand.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    NzD%r cannot be used to seed a numpy.random.mtrand.RandomState instance)ÚnpÚrandomZmtrandZ_randr   r   ÚintegerZRandomStateÚ	GeneratorÚAttributeErrorÚ
ValueError)Úseedr   r   r   Úcheck_random_state©   s    

ÿrR   c                 C   sª   | dkr"t t |dd…df ¡ƒ} t| tƒrZ|  ¡ D ]}t|tdƒ q4dd„ |  ¡ D ƒ} nLt| t ƒr�dd„ t	| ƒD ƒ} tt
dd	„ | D ƒ| ƒƒ} nt| d
ƒ} t| ƒ| i} | S )z-Check event_id and convert to default format.Nr   zEvent namesc                 S   s    i | ]\}}|t |d | ƒ“qS ©zevent_id[%s]©r   )Ú.0ÚkeyÚvalr   r   r   Ú
<dictcomp>É   s   ÿ z#_check_event_id.<locals>.<dictcomp>c                 S   s   g | ]\}}t |d | ƒ‘qS rS   rT   )rU   ÚviÚvr   r   r   Ú
<listcomp>Ì   s   ÿz#_check_event_id.<locals>.<listcomp>c                 s   s   | ]}t |ƒV  qd S ©N)r%   )rU   Úir   r   r   Ú	<genexpr>Î   s     z"_check_event_id.<locals>.<genexpr>Úevent_id)ÚlistrK   Úuniquer   ÚdictÚkeysr$   r%   ÚitemsÚ	enumerateÚzipr   )r_   ÚeventsrV   r   r   r   Ú_check_event_idÁ   s     
ÿ
ÿ
rh   ÚFile©r   c                C   sÌ   t | d|ƒ tt| ƒ ¡  ¡ ƒ} t | ¡r²|s8tdƒ‚n|dkrJt 	d¡ |rÈ|rrt 
| ¡s�td|› d| › �ƒ‚nt | ¡s�td|› d| › �ƒ‚t | tj¡sÈt|› d	| › �ƒ‚n|rÈt|› d
| › �ƒ‚| S )zACheck for file existence, and return string of its absolute path.r   zQDestination file exists. Please use option "overwrite=True" to force overwriting.ÚreadzOverwriting existing file.zNeed a directory for z but found a file at zNeed a file for z but found a directory at z! does not have read permissions: z does not exist: )r$   r%   r   Ú
expanduserÚabsoluteÚopÚexistsÚFileExistsErrorr   ÚinfoÚisdirr)   ÚisfileÚosÚaccessÚR_OKÚPermissionErrorÚFileNotFoundError)r    Ú	overwriteZ
must_existr   Úneed_dirr   r   r   r   Ú_check_fnameÕ   s4    ÿ



ÿ
ÿÿr{   zclass subject attributezinput subject)Úraise_errorÚ
first_kindÚsecond_kindc             	   C   s’   |dk	rLt |ddƒ | dk	rH| |krHt|› dt| ƒ› d|› d|› d�ƒ‚|S | dk	rpt | dd|› d|› �ƒ | S |d	krŽtd
|› d|› d�ƒ‚dS )zGet subject name from class.Nr%   zsubject inputz (z) did not match r<   zEither z subject or TzNeither z subject nor z was a string)r$   rP   r=   )ÚfirstÚsecondr|   r}   r~   r   r   r   Ú_check_subjectú   s"    ÿ  ÿr�   c                 C   sh   ddl m} ddlm} ddlm} t| ||fƒr4n0t| |ƒrBdnd}| jsdtd| d||f  ƒ‚d	S )
zEnsure data are preloaded.r   )Ú
BaseEpochs)ÚEvoked)Ú_BaseTFRÚepochsÚrawzKBy default, MNE does not load data into main memory to conserve resources. zb requires %s data to be loaded. Use preload=True (or string) in the constructor or %s.load_data().N)	r…   r‚   Zevokedrƒ   Ztime_frequencyr„   r   ZpreloadÚRuntimeError)ÚinstÚmsgr‚   rƒ   r„   r   r   r   r   Ú_check_preload  s     ÿþÿÿrŠ   Údatac              
   C   sô   ddl m} ddlm}m} ddlm} | |fD ]$}	|	dkrB dS t|	|ƒs0t|	ƒ‚q0|dk	r¼|  	¡ } | 	¡ }| |fD ]D}	|	d rž|	 
¡ � g |	d< W 5 Q R X ||	d |ƒ}
||	|
dd	� qv|| ƒpÆd
}||ƒpÒd
}||krðtd||||f ƒ‚dS )z1Ensure that objects have same compensation_grade.r   ©ÚInfo)Úpick_channelsÚ	pick_info)Úget_current_compNÚcompsÚch_namesF)Úcopyr   z6Compensation grade of %s (%s) and %s (%s) do not match)Úior�   Úio.pickrŽ   r�   Zio.compensatorr�   r   ÚAssertionErrorr“   Z_unlockr‡   )Zinfo1Zinfo2Zname1Zname2r’   r�   rŽ   r�   r�   Zt_infoÚpicksZgrade1Zgrade2r   r   r   Ú_check_compensation_grade!  s0    

ÿÿr˜   c                 C   s¦   dd„ }t ddddddd	d
d� | | ¡}zt| ƒ}|W S  ttfk
r    |r–td|› d| › d�d d |dƒ|d|› d�ƒ|d|› d�ƒf¡ ƒ‚nY dS Y nX dS )aœ  Import soft dependencies, providing informative errors on failure.

    Parameters
    ----------
    name : str
        Name of the module to be imported. For example, 'pandas'.
    purpose : str
        A very brief statement (formulated as a noun phrase) explaining what
        functionality the package provides to MNE-Python.
    strict : bool
        Whether to raise an error if module import fails.
    c                 S   s   |   t| ƒd ¡S )Né   )Úrjustr&   )r   r   r   r   ÚindentP  s    z_soft_import.<locals>.indentzscikit-learnzEDFlib-Pythonzmne-bidszmne-nirszmne-featureszmne-qt-browserzmne-connectivityZ	pyvistaqt)ZsklearnÚEDFlibZmne_bidsZmne_nirsZmne_featuresZmne_qt_browserZmne_connectivityZpyvistazFor z to work, the z module is needed, zbut it could not be imported.
Ú
zGuse the following installation method appropriate for your environment:z'pip install ú'z'conda install -c conda-forge FN)rb   Úgetr   r/   ÚModuleNotFoundErrorr‡   r(   )r   rI   Ústrictr›   Zpip_nameÚmodr   r   r   rF   B  s<    ø ø
ÿ
ýþÿ	rF   c                 C   s   t dd| d�S )úAux function.Zpandaszdataframe integration©r¡   ©rF   r¤   r   r   r   Ú_check_pandas_installedo  s    r¦   c                 C   s   t dd| d�S )r£   Zeeglabiozexporting to EEGLabr¤   r¥   r¤   r   r   r   Ú_check_eeglabio_installedt  s    r§   c                 C   s   t dd| d�S )r£   rœ   zexporting to EDFr¤   r¥   r¤   r   r   r   Ú_check_edflib_installedy  s    r¨   c                 C   s   t dd| d�S )r£   Zpybvzexporting to BrainVisionr¤   r¥   r¤   r   r   r   Ú_check_pybv_installed~  s    r©   c                 C   s   t dd| d�S )r£   rH   zloading v7.3 (HDF5) .MAT filesr¤   r¥   r¤   r   r   r   Ú_check_pymatreader_installedƒ  s
    ýrª   c                 C   s‚   | dkrdS t | tƒr| g} t | tƒs8td t| ƒ¡ƒ‚t| ƒt|ƒ }|r~dtt|ƒdkƒ }t	d d 
|¡|d 
|¡¡ƒ‚| S )zCheck pandas index arguments.NzAindex must be `None` or a string or list of strings, got type {}.)zis not a valid optionzare not valid optionsr	   z."{}" {}. Valid index options are `None`, "{}".ú", ")r   r%   r`   r   Úformatr   Úsetr   r&   rP   r(   )r   ÚvalidÚinvalidÚpluralr   r   r   Ú_check_pandas_index_argumentsŒ  s&    

ÿÿ þr±   c                 C   sJ   | |kr*| dk	r*d  |¡}td | |¡ƒ‚| dkrF|dkrFtdƒ d} | S )zCheck time_format argument.Nr«   zA"{}" is not a valid time format. Valid options are "{}" and None.ÚdatetimezYCannot convert to Datetime when raw.info['meas_date'] is None. Falling back to Timedelta.Ú	timedelta)r(   rP   r¬   r
   )Útime_formatr®   Z	meas_dateZ	valid_strr   r   r   Ú_check_time_formatŸ  s    
 ÿrµ   c                 C   s¦   ddl m}m} |dk	r(|dk	r(tdƒ‚|dk	r>|| |d�} n"|dk	r`|| ||d�}|| |d�} | d }t dd	„ |D ƒ¡}|d
k ¡ p¢t |¡  ¡ p¢t |d¡ S )a…  Check if channel locations exist.

    Parameters
    ----------
    info : Info | None
        `~mne.Info` instance.
    picks : list of int
        Channel indices to consider. If provided, ``ch_type`` must be ``None``.
    ch_type : str | None
        The channel type to restrict the check to. If ``None``, check all
        channel types. If provided, ``picks`` must be ``None``.
    r   )Ú_picks_to_idxr�   Nz1Either picks or ch_type may be provided, not both)rq   Úsel)rq   r—   ÚnoneÚchsc                 S   s   g | ]}|d  dd… ‘qS )ÚlocNé   r   ©rU   Úchr   r   r   r[   Æ  s     z"_check_ch_locs.<locals>.<listcomp>r   ç        )	r•   r¶   r�   rP   rK   ÚarrayÚallÚisfiniteZallclose)rq   r—   Zch_typer¶   r�   r¹   Zlocs3dr   r   r   Ú_check_ch_locs­  s    ÿ
þrÂ   c                 C   s   t | tjƒS r\   )r   ÚnumbersÚNumber)Únr   r   r   Ú_is_numericÌ  s    rÆ   c                   @   s   e Zd Zedd„ ƒZdS )Ú_IntLikec                 C   s,   zt |ƒ W n tk
r"   Y dS X dS d S )NFT)r   r   ©ÚclsÚotherr   r   r   Ú__instancecheck__Ñ  s
    z_IntLike.__instancecheck__N©Ú__name__Ú
__module__Ú__qualname__ÚclassmethodrË   r   r   r   r   rÇ   Ð  s   rÇ   c                   @   s   e Zd Zedd„ ƒZdS )Ú	_Callablec                 C   s   t |ƒS r\   )ÚcallablerÈ   r   r   r   rË   à  s    z_Callable.__instancecheck__NrÌ   r   r   r   r   rÑ   ß  s   rÑ   )r%   Únumericr   zint-likerÒ   c                C   s  |dkrt | ||d� dS |dkr.ddlm} t|ttfƒsB|g}tdd„ |D ƒd	ƒ}|rdd
|› �n|}t| |ƒ�s|dkrÒdd„ |D ƒ}t|ƒdkr |d }n2t|ƒdkr¸d |¡}nd|d  |d< d |¡}|dkrÞdn|}t	|› d|› |› dt
| ƒ› d�ƒ‚dS )a¾  Validate that `item` is an instance of `types`.

    Parameters
    ----------
    item : object
        The thing to be checked.
    types : type | str | tuple of types | tuple of str
         The types to be checked against.
         If str, must be one of {'int', 'int-like', 'str', 'numeric', 'info',
         'path-like', 'callable'}.
         If a tuple of str is passed, use 'int-like' and not 'int' for
         integers.
    item_name : str | None
        Name of the item to show inside the error message.
    type_name : str | None
        Possible types to show inside the error message that the checked item
        can be.
    extra : str
        Extra text to append to the warning.
    r   )r   r   Nrq   r   rŒ   c                 s   s8   | ]0}|d krt d ƒfnt|tƒs(|fnt| V  qd S r\   )r   r   r%   Ú_multi)rU   Útype_r   r   r   r^     s
   þÿz!_validate_type.<locals>.<genexpr>r   r   c                 S   s,   g | ]$}|d krdnt |tƒs$|jn|‘qS )NÚNone)r   r%   rÍ   )rU   Zcls_r   r   r   r[     s
   þÿz"_validate_type.<locals>.<listcomp>r	   r   r!   zor r#   r"   ZItemz must be an instance of r   z	 instead.)r   Zmne.ior�   r   r`   ÚtupleÚsumr&   r(   r   r   )ÚitemÚtypesZ	item_nameÚ	type_namer   Zcheck_typesZ
_item_namer   r   r   r$   î  s8    þþþ

ÿr$   c                 C   s.   zt | dd� W dS  tk
r(   Y dS X dS )zæValidate that `item` is `path-like`.

    Parameters
    ----------
    item : object
        The thing to be checked.

    Returns
    -------
    bool
        ``True`` if `item` is a `path-like` object; ``False`` otherwise.
    r   )rÚ   TFN)r$   r   )rÙ   r   r   r   Ú
_path_like#  s
    rÜ   ú to be plottedc                 C   s    t  | ¡ ¡ std |¡ƒ‚dS )z#Raise if any of the values are NaN.zSome of the values {} are NaN.N)rK   rÁ   rÀ   rP   r¬   )r‹   r‰   r   r   r   Ú_check_if_nan7  s    rÞ   c                    s4  ddl m} |d d }tˆ d |ˆ d ƒ}|ˆ ddd�‰‡ fd	d
„ˆD ƒ‰‡fdd
„|D ƒ}|dk	r¢tˆ d ƒt|d ƒkr¢tt|ƒ |d ¡ƒdkr¢t d¡ |dk	rætˆ d ƒt|d ƒkrætt|ƒ |d ¡ƒdkræt d¡ |dk	�rt||j|d ƒ}|dk	�rt||j|d ƒ}‡ fdd
„|D ƒ}|S )zEReturn good channels common to forward model and covariance matrices.r   )Ú
pick_typesrq   r’   ÚbadsFT)ZmegZref_megc                    s   g | ]}ˆ d  | ‘qS ©r’   r   r¼   ©rq   r   r   r[   H  s     z#_check_info_inv.<locals>.<listcomp>c                    s   g | ]}|ˆ kr|‘qS r   r   r¼   )Úref_chsr   r   r[   I  s      Nr   zQinfo["bads"] and data_cov["bads"] do not match, excluding bad channels from both.zRinfo["bads"] and noise_cov["bads"] do not match, excluding bad channels from both.c                    s&   g | ]}|ˆ d  krˆ d    |¡‘qS rá   )r   )rU   Úkrâ   r   r   r[   a  s
     ÿ )	r   rß   Ú_compare_ch_namesr­   r&   Úintersectionr   rq   r’   )rq   ÚforwardÚdata_covÚ	noise_covrß   Zfwd_ch_namesr’   r—   r   )rq   rã   r   Ú_check_info_inv=  s4     ÿ
ÿþ

ÿ
ÿrê   c                    s   ‡ ‡fdd„| D ƒ}|S )z1Return channel names of common and good channels.c                    s    g | ]}|ˆ kr|ˆkr|‘qS r   r   r¼   ©rà   Únames2r   r   r[   h  s       z%_compare_ch_names.<locals>.<listcomp>r   )Znames1rì   rà   r’   r   rë   r   rå   f  s    rå   c                    s@   g }ˆ d D ]}|| krt d| ƒ‚q‡ fdd„t| ƒD ƒ}|S )z„Return data channel indices to be used with spatial filter.

    Unlike ``pick_channels``, this respects the order of ch_names.
    r’   zŸThe spatial filter was computed with channel %s which is not present in the data. You should compute a new spatial filter restricted to the good data channels.c                    s    g | ]\}}|ˆ d  kr|‘qS rá   r   )rU   ÚiiÚch_name©Úfiltersr   r   r[   z  s    ÿz2_check_channels_spatial_filter.<locals>.<listcomp>)rP   re   )r’   rð   r·   rî   r   rï   r   Ú_check_channels_spatial_filterl  s    ýrñ   c                 C   s6   t | dttfdƒ t| tƒr2| dkr2td| f ƒ‚| S )zCheck rank parameter.NÚrank)Úfullrq   z.rank, if str, must be "full" or "info", got %s)r$   rb   r%   r   rP   )rò   r   r   r   Ú_check_rank  s    
ÿrô   c                    s  ddl m}m} ddlm} ddlm} ddlm‰  t	||ƒrft
|d|gdƒ ttt|jƒƒƒ}	|‰n*t
|d|gdƒ t||||d�}	|||	ƒ‰‡ ‡fd	d
„dD ƒ}
t|
ƒdkrÀ|dkrÀtdƒ‚|dkrÚ|ˆdd�}d}n$| ¡ }t	||ƒrúd|krú|d= d}t
|||fdƒ ||	|fS )zECheck number of sensor types and presence of noise covariance matrix.r   )Úmake_ad_hoc_covÚ
Covariance)ÚCrossSpectralDensity)r�   )Ú_contains_ch_typeNré   )rè   ré   c                    s   g | ]}ˆ ˆ|ƒ‘qS r   r   )rU   Útt©rø   Z	info_pickr   r   r[   š  s     z&_check_one_ch_type.<locals>.<listcomp>)ÚmagZgradZeegr	   zqSource reconstruction with several sensor types requires a noise covariance matrix to be able to apply whitening.g      ð?)ÚstdTZ	estimatorF)Zcovrõ   rö   Ztime_frequency.csdr÷   r•   r�   Zchannels.channelsrø   r   r$   r`   Úranger&   r’   rê   rØ   rP   r“   )Úmethodrq   rç   rè   ré   rõ   rö   r÷   r�   r—   Zch_typesZallow_mismatchr   rú   r   Ú_check_one_ch_type‰  s6    
ÿ
ÿrÿ   Ú	depth_mnec                 C   s:   ddl m} t| tƒs0t| dkr$dnt| ƒd�} ||| ƒS )zCheck depth options.r   )Ú_handle_defaultN)r;   )Údefaultsr  r   rb   Úfloat)ÚdepthÚkindr  r   r   r   Ú_check_depth¬  s    
r  c                 C   sT   t | ƒt |ƒ }t|ƒrPt|ƒdkr(dnd}d|› d|› d|› d|› �}t|ƒ‚| S )aÂ  Check that the keys in dictionary are valid against a set list.

    Return the input dictionary if it is valid,
    otherwise raise a ValueError with a readable error message.

    Parameters
    ----------
    mapping : dict
        The user-provided dict whose keys we want to check.
    valid_keys : iterable
        The valid keys.
    key_description : str
        Description of the keys in ``mapping``, e.g., "channel name(s)" or
        "annotation(s)".
    valid_key_source : str
        Description of the ``valid_keys`` source, e.g., "info dict" or
        "annotations in the data".

    Returns
    -------
    mapping
        If all keys are valid the input dict is returned unmodified.
    r	   ZareÚiszInvalid r   z not present in )r­   r&   rP   )ÚmappingZ
valid_keysZkey_descriptionZvalid_key_sourceÚmissingZ_isr‰   r   r   r   Ú_check_dict_keys´  s    r
  c                 C   sÄ   ||kr|S |rd|› �n|}d}t |ƒ}t|ƒdkrJdt|d ƒ› �}n`d}t|ƒdkrt|d d	d
„ |D ƒ¡7 }n6|d dd
„ |dd… D ƒ¡7 }|dt|d ƒ› �7 }t|j| |||d�ƒ‚dS )aï  Check the value of a parameter against a list of valid options.

    Return the value if it is valid, otherwise raise a ValueError with a
    readable error message.

    Parameters
    ----------
    parameter : str
        The name of the parameter to check. This is used in the error message.
    value : any type
        The value of the parameter to check.
    allowed_values : list
        The list of allowed values for the parameter.
    extra : str
        Extra string to append to the invalid value sentence, e.g.
        "when using ico mode".

    Raises
    ------
    ValueError
        When the value of the parameter is not one of the valid options.

    Returns
    -------
    value : any type
        The value if it is valid.
    r   z[Invalid value for the '{parameter}' parameter{extra}. {options}, but got {value!r} instead.r	   zThe only allowed value is r   zAllowed values are r   z and c                 s   s   | ]}t |ƒV  qd S r\   ©r=   ©rU   rZ   r   r   r   r^      s     z _check_option.<locals>.<genexpr>r"   c                 s   s   | ]}t |ƒV  qd S r\   r  r  r   r   r   r^     s     Nr#   z, and )Ú	parameterÚoptionsÚvaluer   )r`   r&   r=   r(   rP   r¬   )r  r  Zallowed_valuesr   r‰   r  r   r   r   Ú_check_option×  s      
 ÿr  c                 C   s0   | d j d }| D ]}||j d kr dS qdS )z>Check if a collection of instances all have the same channels.r   r’   FTrâ   )Z	instancesr’   rˆ   r   r   r   Ú_check_all_same_channel_names  s
    r  ©ÚmeanÚmedianrü   c                    s€   | dkr‡ fdd„}nf| dkr,‡ fdd„}nP| dks>| t jkrL‡ fdd„}n0t| ƒrZ| }n"tdd	 |¡ d
| t| ƒf  ƒ‚|S )Nr  c                    s   t j| ˆ d�S ©N©Úaxis)rK   r  ©r‹   r  r   r   Úfun  s    z_check_combine.<locals>.funrü   c                    s   t j| ˆ d�S r  )rK   rü   r  r  r   r   r    s    r  c                    s   t | ˆ d�S r  )r   r  r  r   r   r    s    zCombine option must be r"   z or callable, got %s (type %s).)rK   r  rÒ   rP   r(   r   )Úmoder®   r  r  r   r  r   Ú_check_combine  s    
ÿÿr  c                 C   s>   ddl m} t||dƒ | dkr:|jdkr:td|jf ƒ‚d S )Nr   )ÚSourceSpacesÚsrcÚnormal)ZsurfaceZdiscretez]Normal source orientation is supported only for surface or discrete SourceSpaces, got type %s)Zsource_spacer  r$   r  r‡   )Zpick_orir  r  r   r   r   Ú_check_src_normal$  s    þr  çH¯¼šò×z>c                 C   s.   t  t  | j¡¡}||kr*tdd|  ƒ d S )NzéThe maximum current magnitude is %0.1f nAm, which is very large. Are you trying to apply the forward model to noise-normalized (dSPM, sLORETA, or eLORETA) values? The result will only be correct if currents (in units of Am) are used.g    eÍÍA)rK   ÚmaxÚabsr‹   r
   )ZstcÚ	thresholdZmax_curr   r   r   Ú_check_stc_units-  s
    ür$  )Ú
return_apic                 C   s¦   zddl m}m} W n tk
r0   d }}Y nbX z
|j}W n tk
rV   |j}Y nX tjdkr’|dkr’t	|ddƒs’t
d|› d	|› d
|› d�ƒ | rž||fS |S dS )zCheck if Qt is installed.r   )ÚQtCoreÚAPI_NAMENÚdarwin)ZPyQt5ZPySide2z>=z5.10zmacOS users should use z >= 5.10 for GUIs, got z.. Please upgrade e.g. with:

    pip install "z>=5.10"
)Zqtpyr&  r'  Ú	Exceptionr0   rO   ZQT_VERSION_STRÚsysÚplatformr   r
   )r%  r&  Úapir4   r   r   r   Ú_check_qt_version7  s    
r-  Úmc              	   C   s  ddl m} ddlm}m}m} | d krd|} |d k	rdz||ddd� W n ttfk
r^   Y nX d} t| t	ƒ�r:| dkr†td	| › �ƒ‚|d k	s’t
‚| dkrÄ||d
dd�\}}}	t|ƒ|f } d}�q’| dk�r’| ¡ }
|
d krætdƒ‚|
 ¡ d }d}d|k�r|d |d< |d= n4d|k�rHd|k�rHt d¡ |d dddg |d< |D ]@}||k�rLdd |¡› d|› �}|dk�r‚|d7 }t|ƒ‚�qLt |d d |d d |d d |d d g¡ ¡ }tƒ }tdƒD ]@\}}t |d | |d | |d | |d | g¡||< �qÒ|d |d  |d! |f} d}~~~
~nXt| |ƒ�r’| d" �rbt| d# ƒdk�rptd$| f ƒ‚t| d% ƒ| d# d d& f } d}tj| td'�} | jd(k�rÂt d)gd* | gg¡} | jd+k�rÞtd,| jf ƒ‚td-|d.ƒ |d/k�rü| d0 } t | t¡} | S )1Nr   )ÚHEAD_SIZE_DEFAULT)Úfit_sphere_to_headshapeÚConductorModelÚget_fitting_digr   Úerrorrj   Úauto)r4  Úeeglabz0sphere, if str, must be "auto" or "eeglab", got Fr.  )r   Zunitsr5  z°No montage was set on your data, but sphere="eeglab" can only work if digitization points for the EEG channels are available. Consider calling set_montage() to apply a montage.Úch_pos)ÚFpzÚOzÚT7ÚT8ZFPzr7  r8  zBApproximating Fpz location by mirroring Oz along the X and Y axes.r#   r	   z_sphere="eeglab" requires digitization points of the following electrode locations in the data: r"   z, but could not find: z4, and was unable to approximate its location from Ozr9  r   r:  )ÚXÚYÚZr;  r<  r=  Z	is_sphereZlayersznsphere, if a ConductorModel, must be spherical with multiple layers, not a BEM or single-layer sphere (got %s)Úr0Zrad)Údtyper   r¾   r»   )é   zJsphere must be float or 1D array of shape (4,), got array-like of shape %sÚsphere_units)r.  ÚmmrB  g     @�@)r  r/  Zbemr0  r1  r2  r‡   rP   r   r%   r–   r×   Zget_montageZget_positionsr   rq   r(   rK   r"  r  rb   re   r&   r¿   r  ÚshapeZconcatenater  )Zsphererq   rA  r/  r0  r1  r2  ÚRr>  Ú_Zmontager6  Zhorizon_ch_namesrî   r‰   ZradiusZsphere_locsÚidxr  r   r   r   Ú_check_sphereM  sª    ÿ
ÿ
ÿ
ÿ
ÿ



ü





ü   ÿ
þÿ
rG  c                  C   s(   ddl m}  | dƒ}|d kr$tdƒ‚|S )Nr	   )Ú
get_configZFREESURFER_HOMEz4The FREESURFER_HOME environment variable is not set.)ÚconfigrH  r‡   )rH  Zfs_homer   r   r   Ú_check_freesurfer_home¿  s    ÿrJ  ç…ëQ¸å?c                 C   sF   t | ||d�}t|ƒdkrdS t|ƒdkr8d|d f S d|f S d S )N)Úcutoffr   r   r	   z Did you mean %r?z Did you mean one of %r?)r   r&   )rW   r  rL  r   r   r   Ú_suggestÈ  s    rM  Ú
on_missingc                 C   s"   t | t|ƒ t|| dddgƒ d S )NÚraiser
   Úignore)r$   r%   r  )rN  r   r   r   r   Ú_check_on_missingÒ  s    rQ  c                 C   sn   t | |ƒ |d krtn|}| dkr&dn| } | dkr6dn| } | dkrL||ƒ‚n| dkr^t|ƒ n| dksjt‚d S )Nr3  rO  Úwarningr
   rP  )rQ  rP   r
   r–   )rN  r‰   r   Zerror_klassr   r   r   Ú_on_missing×  s    


rS  )ÚaltÚusec             	   C   sJ   z
t | ƒW S  tk
rD   |d k	r*| Y S td| › d|› d�ƒ‚Y nX d S )Nz,Could not use input() to get a response to:
z	
You can z to avoid this error.)r   ÚEOFErrorr‡   )r‰   rT  rU  r   r   r   Ú_safe_inputä  s    
ÿrW  c              
   C   sÖ   dt | ƒ› �}tddƒrfzt | ¡} W q’ tk
rb } zt|ƒ d¡rPt|ƒ‚n‚ W 5 d }~X Y q’X n,tj	dd�� t 
d¡ t | ¡} W 5 Q R X t | jtj¡sªt|ƒ‚| jdksÂ| jd	 d
krÒtd| j› �ƒ‚| S )Nz0events should be a NumPy array of integers, got Únumpyz1.24zXsetting an array element with a sequence. The requested array has an inhomogeneous shapeT)ÚrecordrP  r   r	   r»   z$events must be of shape (N, 3), got )r   r5   rK   ZasarrayrP   r%   Ú
startswithr   ÚwarningsÚcatch_warningsÚsimplefilterZ
issubdtyper?  rM   ÚndimrC  )rg   Úerr_msgZnp_errr   r   r   Ú_ensure_eventsï  s(    
ÿ


ÿr`  Úcolor)r   Úalphac                 G   s€   ddl m} |r|jn|j}z
||Ž W S  tk
rz   t|ƒdkrJ|d n|}td|rZdnd› d| › dt|ƒ› �ƒd ‚Y nX d S )	Nr   )ÚcolorConverterr	   zInvalid RGBÚAr   z argument(s) for z: )Zmatplotlib.colorsrc  Zto_rgbaZto_rgbrP   r&   r=   )r   rb  Úargsrc  Úfuncr   r   r   Ú_to_rgb
  s    
 ÿþrg  )r   r   )r   )r+   )r+   )FFri   F)r‹   N)T)T)T)T)T)T)N)NN)NNN)rÝ   )NN)NN)r   )r   )r  r   )r   )Nr.  )rK  )rN  )rN  N)UÚ__doc__Úbuiltinsr   Údifflibr   Ú	importlibr   r   rt   Úos.pathÚpathrn   Úpathlibr   r8   r*  r[  rÃ   rX  rK   Úfixesr   r   Z_loggingr
   r   r   r   r*   r5   r1   rB   rD   rG   rJ   rR   rh   r{   r�   rŠ   r˜   rF   r¦   r§   r¨   r©   rª   r±   rµ   rÂ   rÆ   ÚobjectrÇ   Zint_liker%   ÚPathLikeZ	path_likerÑ   Zfloatingr  rÔ   r$   rÜ   rÞ   rê   rå   rñ   rô   rÿ   r  r
  r  r  r  r  r$  r-  rG  rJ  rM  rQ  rS  rW  r`  rg  r   r   r   r   Ú<module>   s¬   
ÿ6

  ÿÿ$þ   ÿ
!
-




	


û	ÿ5

)

#
#
1	
	


r	



