U
    f»{j¯  ã                :   @   s  d dl mZ d dlZd dlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZ didd	„Zejd
ejdejdejdejdejdejdejdejdejdejdejdejdejdejdejdej dej!dej"dej#dej$dej%dej&dej'dej(dej)dej*dej+dej,diZ-dej.dej/d ifd!ej0d"ej1d#ej2d$ej3d%ej4d&ej5d'ifd!ej6dej7dej8dej9d(ifd)œZ:ed*d+„ ƒZ;g dfd,d-„Z<d.d/„ Z=d0d1„ Z>d2d3„ Z?djd4d5„Z@d6d7„ ZAedkd;d<„ƒZBe	dld>d?„ƒZCd@dA„ ZDg d:fdBdC„ZEe	g g dd=dfdDdE„ƒZFddd=dddg g fdFdG„ZGedmdHdI„ƒZHg d:dd=fdJdK„ZIdLdM„ ZJedNdO„ ƒZKedndPdQ„ƒZLdodRdS„ZMeNd=d=d=ddddddddddd=dddd=d=d=dT�ZOePeQeOƒdg ƒZRdUZSdVZTdWeT dX ZUdYeT dZ ZVd[eT ZWdpd\d]„ZXdqd^d_„ZYdrdadb„ZZdcdd„ Z[dsdedf„Z\dtdgdh„Z]dS )ué    )ÚdeepcopyNé   )ÚFIFFé   )ÚloggerÚverboseÚ_validate_typeÚfill_docÚ_ensure_intÚ_check_optionÚwarnFc                  C   sF  t t tjtjd�t tjtjd�t tjd�t tjtjtjd�t tj	tjtjd�t tj
tjtjd�t tjtjtjd�t tjtjd�t tjtjd�t tjtjd�t tjtjd�t tjtjd�t tjtjd�t tjd�t tjd�t tjd�t tjd�t tjd�t tjd�t tjtjtjtjtjtjtjtjtj tj!g
d�t tj"tjtj#d�t tj"tjtj$d�t tj"tj%tj&d�t tj"tj'd�t tj"tj(tj)d�t tj"tj(tj*d�t tjtj+tj,d�d�}| �rBt tj-d�}t tj.d�}t t tj/d�t tj0d�t tj0tjd�t f ||—Žt f d	tji|—Ž|||||d
�
}| 1¡ D ]\}}||  2|¡ �q(|S )aÙ  Return all known channel types, and associated FIFF constants.

    Parameters
    ----------
    include_defaults : bool
        Whether to include default values for "unit" and "coil_type" for all
        entries (see Notes). Defaults are generally based on values normally
        present for a VectorView MEG system. Defaults to ``False``.

    Returns
    -------
    channel_types : dict
        The keys are channel type strings, and the values are dictionaries of
        FIFF constants for "kind", and possibly "unit" and "coil_type".

    Notes
    -----
        Values which might vary within a channel type across real data
        recordings are excluded unless ``include_defaults=True``. For example,
        "ref_meg" channels may have coil type
        ``FIFFV_COIL_MAGNES_OFFDIAG_REF_GRAD``, ``FIFFV_COIL_VV_MAG_T3``, etc
        (depending on the recording system), so no "coil_type" entry is given
        for "ref_meg" unless ``include_defaults`` is requested.
    )ÚkindÚunit)r   )r   r   Ú	coil_type)r   r   )ÚgradÚmagÚref_megÚeegÚseegÚdbsÚecogÚeogÚemgÚecgÚrespÚbioÚmiscÚstimÚexciÚsystÚiasÚgofÚdipoleÚchpiÚfnirs_cw_amplitudeÚfnirs_fd_ac_amplitudeÚfnirs_fd_phaseÚfnirs_odÚhboÚhbrÚcsd)r   )r   )r   r   r   )
r   r   r   r   r   r   r   r   r   r'   )3Údictr   ÚFIFFV_MEG_CHÚFIFF_UNIT_T_MÚFIFF_UNIT_TÚFIFFV_REF_MEG_CHÚFIFFV_EEG_CHZFIFF_UNIT_VÚFIFFV_COIL_EEGÚFIFFV_SEEG_CHÚFIFFV_DBS_CHÚFIFFV_ECOG_CHÚFIFFV_EOG_CHÚFIFFV_EMG_CHÚFIFFV_ECG_CHÚFIFFV_RESP_CHÚFIFFV_BIO_CHÚFIFFV_MISC_CHÚFIFFV_STIM_CHÚFIFFV_EXCI_CHÚFIFFV_SYST_CHÚFIFFV_IAS_CHÚFIFFV_GOODNESS_FITÚFIFFV_DIPOLE_WAVEÚFIFFV_QUAT_0ÚFIFFV_QUAT_1ÚFIFFV_QUAT_2ÚFIFFV_QUAT_3ÚFIFFV_QUAT_4ÚFIFFV_QUAT_5ÚFIFFV_QUAT_6ÚFIFFV_HPI_GÚFIFFV_HPI_ERRÚFIFFV_HPI_MOVÚFIFFV_FNIRS_CHÚFIFFV_COIL_FNIRS_CW_AMPLITUDEÚ FIFFV_COIL_FNIRS_FD_AC_AMPLITUDEZFIFF_UNIT_RADÚFIFFV_COIL_FNIRS_FD_PHASEÚFIFFV_COIL_FNIRS_ODZFIFF_UNIT_MOLÚFIFFV_COIL_FNIRS_HBOÚFIFFV_COIL_FNIRS_HBRZFIFF_UNIT_V_M2ÚFIFFV_COIL_EEG_CSDÚFIFFV_COIL_NONEZFIFF_UNIT_NONEZFIFFV_COIL_VV_PLANAR_T1ZFIFFV_COIL_VV_MAG_T3ÚitemsÚupdate)Zinclude_defaultsÚbaseZ	coil_noneZ	unit_noneÚdefaultsÚkeyÚvalue© rZ   úJ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/pick.pyÚget_channel_type_constants   s¸    
þþþþ






    üýýýÿþþþÌ7

ÿõr\   Úmegr   r   r   r   r   r   r   r   r   r    r   r   r   r   r#   r"   r!   r   Úfnirsr   r   r   r   r(   r)   r$   r%   r&   r'   r*   )r]   r^   r   c                 C   sn   | d | }zt |d  }W n, tk
rH   td|d |d f ƒ‚Y nX |tkrjt| \}}|||  }|S )aŸ  Get channel type.

    Parameters
    ----------
    %(info_not_none)s
    idx : int
        Index of channel.

    Returns
    -------
    type : str
        Type of channel. Will be one of::

            {'grad', 'mag', 'eeg', 'csd', 'stim', 'eog', 'emg', 'ecg',
             'ref_meg', 'resp', 'exci', 'ias', 'syst', 'misc', 'seeg', 'dbs',
              'bio', 'chpi', 'dipole', 'gof', 'ecog', 'hbo', 'hbr'}
    Úchsr   z*Unknown channel type (%s) for channel "%s"Úch_name)Ú_first_ruleÚKeyErrorÚ
ValueErrorÚ_second_rules)ÚinfoÚidxÚchZ
first_kindrX   Zsecond_rulerZ   rZ   r[   Úchannel_type¬   s    ÿrh   c                 C   s8  t t | ¡ƒt | ƒkrtdƒ‚t|ƒ t|ƒ |s”t|tƒsDt|ƒ}t|tƒsVt|ƒ}g }t| ƒD ].\}}t |ƒdks~||krb||krb| |¡ qbn˜t|t	ƒs¦t	|ƒ}t |ƒdkrºt	| ƒ}t|t	ƒsÌt	|ƒ}t	ƒ t	ƒ  }}|D ]4}|| k�r||k�r| |  
|¡¡ qÞ| |¡ qÞt |ƒ�r,td|f ƒ‚t |t¡S )až  Pick channels by names.

    Returns the indices of ``ch_names`` in ``include`` but not in ``exclude``.

    Parameters
    ----------
    ch_names : list of str
        List of channels.
    include : list of str
        List of channels to include (if empty include all available).

        .. note:: This is to be treated as a set. The order of this list
           is not used or maintained in ``sel``.

    exclude : list of str
        List of channels to exclude (if empty do not exclude any channel).
        Defaults to [].
    ordered : bool
        If true (default False), treat ``include`` as an ordered list
        rather than a set, and any channels from ``include`` are missing
        in ``ch_names`` an error will be raised.

        .. versionadded:: 0.18

    Returns
    -------
    sel : array of int
        Indices of good channels.

    See Also
    --------
    pick_channels_regexp, pick_types
    z0ch_names is not a unique list, picking is unsafer   z6Missing channels from ch_names required by include:
%s)ÚlenÚnpÚuniqueÚRuntimeErrorÚ_check_excludes_includesÚ
isinstanceÚsetÚ	enumerateÚappendÚlistÚindexrc   ÚarrayÚint)Úch_namesÚincludeÚexcludeÚorderedÚselÚkÚnameÚmissingrZ   rZ   r[   Úpick_channelsÎ   s<    "






ÿr~   c                    s    t  |¡‰ ‡ fdd„t| ƒD ƒS )at  Pick channels using regular expression.

    Returns the indices of the good channels in ch_names.

    Parameters
    ----------
    ch_names : list of str
        List of channels.

    regexp : str
        The regular expression. See python standard module for regular
        expressions.

    Returns
    -------
    sel : array of int
        Indices of good channels.

    See Also
    --------
    pick_channels

    Examples
    --------
    >>> pick_channels_regexp(['MEG 2331', 'MEG 2332', 'MEG 2333'], 'MEG ...1')
    [0]
    >>> pick_channels_regexp(['MEG 2331', 'MEG 2332', 'MEG 2333'], 'MEG *')
    [0, 1, 2]
    c                    s   g | ]\}}ˆ   |¡r|‘qS rZ   )Úmatch)Ú.0r{   r|   ©ÚrrZ   r[   Ú
<listcomp>0  s     
 z(pick_channels_regexp.<locals>.<listcomp>)ÚreÚcompilerp   )rv   ÚregexprZ   r�   r[   Úpick_channels_regexp  s    
r‡   c                 C   sz   |dkrdS | d t jkr\|dkr&dS |dkr@| d  d¡r@dS |dkrv| d  d¡rvdS n|d	krv| d t jkrvdS d
S )zTriage an MEG pick type.Tr   r   Úplanar1r`   Ú2Úplanar2Ú3r   F)r   r-   Úendswithr.   )rg   r]   rZ   rZ   r[   Ú_triage_meg_pick3  s    r�   c                 C   s¬   |dkrdS | d t jkr&d|kr&dS | d t jkr@d|kr@dS | d t jkrZd|krZdS | d t jkrtd|krtdS | d t jkrŽd|krŽdS | d t jkr¨d|kr¨dS d	S )
zTriage an fNIRS pick type.Tr   r(   r)   r$   r%   r&   r'   F)r   rP   rQ   rL   rM   rN   rO   )rg   r^   ÚwarnedrZ   rZ   r[   Ú_triage_fnirs_pickC  s*    ÿÿÿr�   c                 C   sD   t | tƒr@ddddg}||r"dgng 7 }| |kr@td|| f ƒ‚dS )zEnsure a valid meg type.r   r   rˆ   rŠ   Úautoz+meg value must be one of %s or bool, not %sN)rn   Ústrrc   )r]   Ú
allow_autoZallowed_typesrZ   rZ   r[   Ú_check_meg_typeY  s    
ÿr“   c                 C   sT   t | dƒ |  ¡  |d kr$tdƒ‚n,|dkr:|  dg ¡}nt|ttfƒsPtdƒ‚|S )Nre   z+exclude must be a list of strings or "bads"Úbadsz„exclude must either be "bads" or a list of strings. If only one channel is to be excluded, use [ch_name] instead of passing ch_name.)r   Ú_check_consistencyrc   Úgetrn   rr   Útuple)re   rx   rZ   rZ   r[   Ú_check_info_excludec  s    

r˜   r�   rZ   r”   c           $         s¢  t |ttfdƒ tˆ |ƒ}ˆ d }tj|td�‰t|dd� t|ƒ t|tƒr‚|dkr‚dˆ ko€ˆ d dk	o€tˆ d ƒd	ko€|d
k	}|||||||	|
||||||||||fD ]$}t|tƒsªd}t	| 
t|ƒ¡ƒ‚qªt|||||||	|
||||||||||d�}t|tƒ�rdD ]}|||< �qt|tƒ�r<tD ]}|||< �q,d
g}t|ƒD ]¦}tˆ |ƒ} z||  ˆ|< W n„ tk
�rì   | dt k�sŒt‚| dk�r®tˆ d | |ƒˆ|< n:| dk�rÐtˆ d | |ƒˆ|< ntˆ d | ||ƒˆ|< Y nX �qJ|dk	�rRtjtjtjg}!t ˆ¡d	 D ]6}ˆ d | d |!k�rˆ d | |k�rd
ˆ|< �q‡ ‡fdd„t|ƒD ƒ}"|"|7 }"t|"ƒd	k�rŽt g t¡}#ntˆ d |"|ƒ}#|#S )a‚	  Pick channels by type and names.

    Parameters
    ----------
    %(info_not_none)s
    meg : bool | str
        If True include MEG channels. If string it can be 'mag', 'grad',
        'planar1' or 'planar2' to select only magnetometers, all gradiometers,
        or a specific type of gradiometer.
    eeg : bool
        If True include EEG channels.
    stim : bool
        If True include stimulus channels.
    eog : bool
        If True include EOG channels.
    ecg : bool
        If True include ECG channels.
    emg : bool
        If True include EMG channels.
    ref_meg : bool | str
        If True include CTF / 4D reference channels. If 'auto', reference
        channels are included if compensations are present and ``meg`` is not
        False. Can also be the string options for the ``meg`` parameter.
    misc : bool
        If True include miscellaneous analog channels.
    resp : bool
        If True include response-trigger channel. For some MEG systems this
        is separate from the stim channel.
    chpi : bool
        If True include continuous HPI coil channels.
    exci : bool
        Flux excitation channel used to be a stimulus channel.
    ias : bool
        Internal Active Shielding data (maybe on Triux only).
    syst : bool
        System status channel information (on Triux systems only).
    seeg : bool
        Stereotactic EEG channels.
    dipole : bool
        Dipole time course channels.
    gof : bool
        Dipole goodness of fit channels.
    bio : bool
        Bio channels.
    ecog : bool
        Electrocorticography channels.
    fnirs : bool | str
        Functional near-infrared spectroscopy channels. If True include all
        fNIRS channels. If False (default) include none. If string it can be
        'hbo' (to include channels measuring oxyhemoglobin) or 'hbr' (to
        include channels measuring deoxyhemoglobin).
    csd : bool
        Current source density channels.
    dbs : bool
        Deep brain stimulation channels.
    include : list of str
        List of additional channels to include. If empty do not include any.
    exclude : list of str | str
        List of channels to exclude. If 'bads' (default), exclude channels
        in ``info['bads']``.
    selection : list of str
        Restrict sensor channels (MEG, EEG) to this list of channel names.

    Returns
    -------
    sel : array of int
        Indices of good channels.
    r]   Únchan©ÚdtypeT)r’   r�   ÚcompsNr   FzsParameters for all channel types (with the exception of "meg", "ref_meg" and "fnirs") must be of type bool, not {}.)r   r   r   r   r   r   r   r#   r   r    r   r   r   r"   r!   r   r   r*   )r   r   )r   r   r   r_   r   r   rv   c                    s    g | ]}ˆ| rˆ d  | ‘qS ©rv   rZ   ©r€   r{   ©re   ÚpickrZ   r[   rƒ   ô  s      zpick_types.<locals>.<listcomp>)r   Úboolr‘   r˜   rj   Zzerosr“   rn   ri   rc   ÚformatÚtyper+   Ú_FNIRS_CH_TYPES_SPLITÚrangerh   rb   ÚAssertionErrorr�   r�   r   r,   r/   r0   Úwherert   ru   r~   )$re   r]   r   r   r   r   r   r   r   r   r#   r   r    r   r   r"   r!   r   r   r^   r*   r   rw   rx   Z	selectionr™   ÚparamÚwZ
param_dictrX   rŽ   r{   Úch_typeZsel_kindZ	myincluderz   rZ   rŸ   r[   Ú
pick_typesq  s˜    L
ÿÿ        ÿ
          ý
ÿ

"
ÿÿr«   Tc              	      s  ddl m} ˆ ¡  |r ˆ ¡ nˆ‰|dkr0ˆS t|ƒdkrDtdƒ‚tt t tˆd ƒ¡| ¡ƒ}|t|ƒkr„td|t|ƒf ƒ‚tˆ 	dg ¡ƒdkrø‡fd	d
„|D ƒ}|ˆ|ƒ\}}t|ƒdkrøt
 dtˆd ƒf ¡ ˆ ¡ � g ˆd< W 5 Q R X ˆ ¡ � ‡fdd
„|D ƒˆd< W 5 Q R X ˆ ¡  ‡fdd
„ˆd D ƒˆd< dˆk�rútˆd ƒ}	|	D ]~‰ ‡fdd
„tˆ d d ƒD ƒ}
‡ fdd
„|
D ƒ}ˆ d |
 }|ˆ d< t|ƒˆ d d< |ˆ d d< ˆ d d |
 ˆ d d< �q^ˆ ¡ � |	ˆd< W 5 Q R X ˆ ¡  ˆS )a›  Restrict an info structure to a selection of channels.

    Parameters
    ----------
    %(info_not_none)s
    sel : list of int | None
        Indices of channels to include. If None, all channels
        are included.
    copy : bool
        If copy is False, info is modified inplace.
    %(verbose)s

    Returns
    -------
    res : dict
        Info structure restricted to a selection of channels.
    r   )Ú_bad_chans_compNr   z No channels match the selection.rv   z-Found %d / %d unique names, sel is not uniquerœ   c                    s   g | ]}ˆ d  | ‘qS r�   rZ   )r€   rf   ©re   rZ   r[   rƒ   "  s     zpick_info.<locals>.<listcomp>zURemoving %d compensators from info because not all compensation channels were picked.c                    s   g | ]}ˆ d  | ‘qS )r_   rZ   rž   r­   rZ   r[   rƒ   +  s     r_   c                    s   g | ]}|ˆ d  kr|‘qS r�   rZ   ©r€   rg   r­   rZ   r[   rƒ   -  s      r”   c                    s    g | ]\}}|ˆ d  kr|‘qS r�   rZ   )r€   r{   Únr­   rZ   r[   rƒ   1  s    ÿÚdataÚ	row_namesc                    s   g | ]}ˆ d  d | ‘qS )r°   r±   rZ   )r€   Úi)ÚcrZ   r[   rƒ   3  s     ÚrowcalsÚnrow)Ú	meas_infor¬   r•   Úcopyri   rc   rj   rk   Úaranger–   r   re   Ú_unlockÚ_update_redundantr   rp   )re   rz   r·   r   r¬   Zn_uniquerv   Ú_Zcomps_missingrœ   Zrow_idxr±   r´   rZ   )r³   re   r[   Ú	pick_infoÿ  sN     
ÿþ

 

r¼   c                 C   s*   |D ] }| d | d t jkr dS qdS )z¨Determine if KIT ref channels are chosen.

    This is currently only used by make_forward_solution, which cannot
    run when KIT reference channels are included.
    r_   r   TF)r   ZFIFFV_COIL_KIT_REF_MAG)re   ÚpicksÚprZ   rZ   r[   Ú_has_kit_refs@  s    r¿   c                 C   s‚   t |ƒdkrt |ƒdkr| S t|| jdd�}t| jd ||d�}t |ƒdkrTtdƒ‚t| ƒ}t|j|ƒ|_|j|dd…f |_|S )aQ  Pick channels from evoked data.

    Parameters
    ----------
    orig : Evoked object
        One evoked dataset.
    include : list of str, (optional)
        List of channels to include (if empty, include all available).
    exclude : list of str | str
        List of channels to exclude. If empty do not exclude any (default).
        If 'bads', exclude channels in orig.info['bads']. Defaults to 'bads'.

    Returns
    -------
    res : instance of Evoked
        Evoked data restricted to selected channels. If include and
        exclude are empty it returns orig without copy.
    r   T©re   Ú
allow_badsrv   )rw   rx   z*Warning : No channels match the selection.N)ri   rm   re   r~   rc   r   r¼   r°   )Úorigrw   rx   rz   ÚresrZ   rZ   r[   Úpick_channels_evokedL  s    ÿÿrÄ   c           	   	      s  | d   ¡  t|ƒdkr4t|ƒdkr4|r0|  ¡ S | S t|| d dd�}t| d d |||d�}t| d d |||d�}|r‚t| ƒn| ‰t|ƒt|ƒkržtd	ƒ‚t|ƒ}|dkr¶td
ƒ‚t d|ˆd f ¡ ˆd d |dd…f ˆd d< ˆd |dd…f ˆd< |ˆd d< ‡fdd„|D ƒ‰ |ˆd< ˆ ˆd d< ˆd  	¡ �  ‡fdd„|D ƒˆd d< W 5 Q R X ˆd  
¡  ‡ fdd„ˆd d D ƒˆd d< ˆd dk	�rˆd d |dd…f ˆd d< ˆd |dd…f ˆd< |ˆd d< ‡fdd„|D ƒˆd d< ˆS )a7  Pick channels from forward operator.

    Parameters
    ----------
    orig : dict
        A forward solution.
    include : list of str
        List of channels to include (if empty, include all available).
        Defaults to [].
    exclude : list of str | 'bads'
        Channels to exclude (if empty, do not exclude any). Defaults to [].
        If 'bads', then exclude bad channels in orig.
    ordered : bool
        If true (default False), treat ``include`` as an ordered list
        rather than a set.

        .. versionadded:: 0.18
    copy : bool
        If True (default), make a copy.

        .. versionadded:: 0.19
    %(verbose)s

    Returns
    -------
    res : dict
        Forward solution restricted to selected channels. If include and
        exclude are empty it returns orig without copy.
    re   r   TrÀ   Úsolr±   ©rw   rx   ry   rv   zZForward solution and functional data appear to have different channel names, please check.zNothing remains after pickingz.    %d out of %d channels remain after pickingr™   r°   NZ	_orig_solrµ   c                    s   g | ]}ˆ d  d | ‘qS )rÅ   r±   rZ   rž   ©ÚfwdrZ   r[   rƒ   ¸  s     z)pick_channels_forward.<locals>.<listcomp>c                    s   g | ]}ˆ d  d | ‘qS )re   r_   rZ   rž   rÇ   rZ   r[   rƒ   ¾  s     r_   c                    s   g | ]}|ˆ kr|‘qS rZ   rZ   )r€   Úbr�   rZ   r[   rƒ   À  s      r”   Úsol_gradZ_orig_sol_gradc                    s   g | ]}ˆ d  d | ‘qS )rÊ   r±   rZ   rž   rÇ   rZ   r[   rƒ   Æ  s   ÿ)r•   ri   r·   rm   r~   r   rc   r   re   r¹   rº   )	rÂ   rw   rx   ry   r·   r   Zsel_solZsel_infoZnuserZ   )rv   rÈ   r[   Úpick_channels_forwardw  sT      ÿ ÿ ÿ
ÿ $" 
ÿrË   c	                    sR   | d ‰ t ˆ ||||||||d�	}	t|	ƒdkr6tdƒ‚‡ fdd„|	D ƒ}
t| |
ƒS )a%  Pick by channel type and names from a forward operator.

    Parameters
    ----------
    orig : dict
        A forward solution.
    meg : bool | str
        If True include MEG channels. If string it can be 'mag', 'grad',
        'planar1' or 'planar2' to select only magnetometers, all gradiometers,
        or a specific type of gradiometer.
    eeg : bool
        If True include EEG channels.
    ref_meg : bool
        If True include CTF / 4D reference channels.
    seeg : bool
        If True include stereotactic EEG channels.
    ecog : bool
        If True include electrocorticography channels.
    dbs : bool
        If True include deep brain stimulation channels.
    include : list of str
        List of additional channels to include. If empty do not include any.
    exclude : list of str | str
        List of channels to exclude. If empty do not exclude any (default).
        If 'bads', exclude channels in orig['info']['bads'].

    Returns
    -------
    res : dict
        Forward solution restricted to selected channel types.
    re   )r   r   r   r   rw   rx   r   zNo valid channels foundc                    s   g | ]}ˆ d  | ‘qS r�   rZ   rž   r­   rZ   r[   rƒ   ò  s     z&pick_types_forward.<locals>.<listcomp>)r«   ri   rc   rË   )rÂ   r]   r   r   r   r   r   rw   rx   rz   Zinclude_ch_namesrZ   r­   r[   Úpick_types_forwardÌ  s    !   ÿrÌ   c              
   C   s†   dd„ t D ƒ}|jtƒ tƒ tƒ tƒ tƒ tƒ tƒ tƒ d� t| |dddd�}|D ]2}t| |ƒ}| ¡ D ]}||krd||  |¡ qdqN|S )a  Get indices of channels by type.

    Parameters
    ----------
    %(info_not_none)s
    %(picks_all)s

    Returns
    -------
    idx_by_type : dict
        A dictionary that maps each channel type to a (possibly empty) list of
        channel indices.
    c                 S   s   i | ]}|d kr|t ƒ “qS ))r]   r^   ©rr   ©r€   rX   rZ   rZ   r[   Ú
<dictcomp>  s    ÿ z+channel_indices_by_type.<locals>.<dictcomp>)r   r   r(   r)   r$   r%   r&   r'   ÚallrZ   T)Únonerx   Úallow_empty)Ú_PICK_TYPES_KEYSrU   rr   Ú_picks_to_idxrh   Úkeysrq   )re   r½   Zidx_by_typer{   rª   rX   rZ   rZ   r[   Úchannel_indices_by_type÷  s$      þ  ÿ
rÖ   c           	         sä   |r@ˆ   ¡ ‰ dˆ kr&ˆ d dkr&ˆ d= dˆ kr@ˆ d dkr@ˆ d= |dkrPˆ d n|}tˆ d |||d�}ˆ d sˆˆ d | dd…|f n
ˆ d | }‡ fd	d
„|D ƒ}‡ fdd
„ˆ d D ƒ}|ˆ d< |ˆ d< |ˆ d< t|ƒˆ d< ˆ S )aK  Pick channels from covariance matrix.

    Parameters
    ----------
    orig : Covariance
        A covariance.
    include : list of str, (optional)
        List of channels to include (if empty, include all available).
    exclude : list of str, (optional) | 'bads'
        Channels to exclude (if empty, do not exclude any). Defaults to 'bads'.
    ordered : bool
        If True (default False), ensure that the order of the channels in the
        modified instance matches the order of ``include``.

        .. versionadded:: 0.20.0
    copy : bool
        If True (the default), return a copy of the covariance matrix with the
        modified channels. If False, channels are modified in-place.

        .. versionadded:: 0.20.0

    Returns
    -------
    res : dict
        Covariance solution restricted to selected channels.
    ÚmethodNZloglikr”   ÚnamesrÆ   Zdiagr°   c                    s   g | ]}ˆ d  | ‘qS ©rØ   rZ   rž   ©rÂ   rZ   r[   rƒ   >  s     z%pick_channels_cov.<locals>.<listcomp>c                    s   g | ]}|ˆ d  kr|‘qS rÙ   rZ   )r€   r|   rÚ   rZ   r[   rƒ   ?  s      Údim)r·   r~   ri   )	rÂ   rw   rx   ry   r·   rz   r°   rØ   r”   rZ   rÚ   r[   Úpick_channels_cov  s$    ÿ,rÜ   c                 C   s   t dd„ |  dg ¡D ƒƒS )z7Determine of mag and grad should be dealt with jointly.c                 s   s*   | ]"}|  d i ¡  di ¡  dd¡V  qdS )Zmax_infoZsss_infoZin_orderr   N)r–   )r€   ÚphrZ   rZ   r[   Ú	<genexpr>M  s   ÿz&_mag_grad_dependent.<locals>.<genexpr>Zproc_history)Úanyr–   r­   rZ   rZ   r[   Ú_mag_grad_dependentI  s    
ÿrà   c                    sx   t ˆ ddƒ ttƒ}ttƒ}tdd„ tD ƒ| | ƒ}tdˆ |ƒ ˆdkrXtdˆ f ƒ‚t‡ ‡fdd„t	ˆd	 ƒD ƒƒS )
a  Check whether a certain channel type is in an info object.

    Parameters
    ----------
    %(info_not_none)s
    ch_type : str
        the channel type to be checked for

    Returns
    -------
    has_ch_type : bool
        Whether the channel type is present or not.
    r‘   rª   c                 S   s   g | ]}|d kr|‘qS ©r]   rZ   rÎ   rZ   rZ   r[   rƒ   d  s    ÿz%_contains_ch_type.<locals>.<listcomp>Nz;Cannot check for channels of type "%s" because info is Nonec                 3   s   | ]}ˆ t ˆ|ƒkV  qd S )N©rh   )r€   Úii©rª   re   rZ   r[   rÞ   j  s   ÿz$_contains_ch_type.<locals>.<genexpr>r™   )
r   rr   Ú_MEG_CH_TYPES_SPLITr¤   ÚsortedrÓ   r   rc   rß   r¥   )re   rª   Z
meg_extrasZfnirs_extrasZvalid_channel_typesrZ   rä   r[   Ú_contains_ch_typeQ  s     ÿÿÿ
ÿrç   c                    s‚  t |tdƒ t| |ƒ}|dkr&t| ƒ}g ‰ dd„ tD ƒ‰ t| d ƒD ]¤}| d | d |krDt| |ƒ}zˆ |  |¡ W qD tk
ræ   |dkrâ| d | }t	||ƒrâ|d t
jkrÄˆ d	  |¡ qâ|d t
jkrâˆ d
  |¡ n Y qDX qD‡ fdd„tD ƒ‰ tdd… dk�st‚|�rptˆ d d ƒ�rptˆ d d ƒ�rpˆ  ddt t ˆ  d¡d ˆ  d¡d g¡¡f¡ dd„ ˆ D ƒ‰ ˆ S )aJ  Get data channel indices as separate list of tuples.

    Parameters
    ----------
    %(info_not_none)s
    meg_combined : bool | 'auto'
        Whether to return combined picks for grad and mag.
        Can be 'auto' to choose based on Maxwell filtering status.
    ref_meg : bool
        If True include CTF / 4D reference channels
    exclude : list of str | str
        List of channels to exclude. If 'bads' (default), exclude channels
        in info['bads'].

    Returns
    -------
    picks_list : list of tuples
        The list of tuples of picks and the type string.
    r   r�   c                 S   s   i | ]}|t ƒ “qS rZ   rÍ   ©r€   rª   rZ   rZ   r[   rÏ   ˆ  s      z"_picks_by_type.<locals>.<dictcomp>r™   r_   r`   r   r   r   c                    s    g | ]}|t  ˆ | t¡f‘qS rZ   )rj   rt   ru   rè   ©Z
picks_listrZ   r[   rƒ   š  s   ÿz"_picks_by_type.<locals>.<listcomp>Nr   )r   r   r   r   r]   c                 S   s   g | ]}t |d  ƒr|‘qS )r   ©ri   ©r€   r¾   rZ   rZ   r[   rƒ   ¢  s      )r   r¡   r˜   rà   Ú_DATA_CH_TYPES_SPLITr¥   rh   rq   rb   r�   r   r.   r-   r¦   ri   Úinsertrj   rk   ZconcatenateÚpop)re   Zmeg_combinedr   rx   r{   Z	this_typerg   rZ   ré   r[   Ú_picks_by_typen  sB    



ÿ* ÿÿrï   c                 C   sr   ddl m} t| ttttjfƒsn|dkrXt||ƒs<tdƒ‚qn| dkrNtdƒ‚qn|d } ntdd 	t
| ƒ¡ ƒ‚| S )	aÑ  Ensure that inputs to exclude/include are list-like or "bads".

    Parameters
    ----------
    chs : any input, should be list, tuple, set, str
        The channels passed to include or exclude.
    allow_bads : bool
        Allow the user to supply "bads" as a string for auto exclusion.

    Returns
    -------
    chs : list
        Channels to be excluded/excluded. If allow_bads, and chs=="bads",
        this will be the bad channels found in 'info'.
    r   ©ÚInfoTz+Supply an info object if allow_bads is truer”   z%If chs is a string, it must be "bads"z9include/exclude must be list, tuple, ndarray, or "bads". zYou provided type {})r¶   rñ   rn   rr   r—   ro   rj   Zndarrayrc   r¢   r£   )r_   re   rÁ   rñ   rZ   rZ   r[   rm   ¦  s    



ÿÿrm   )r]   r   r*   r   r   r   r   r   r   r#   r   r    r   r   r"   r!   r   r   r^   r   )r   r   rˆ   rŠ   )r(   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whitened)r   r   r   r   r   r   r   r   r"   r!   r   r   r   )r   r*   )r   r   r   r*   r   r   r   c                 C   s8   t }|r"| ¡ }|jddddd� t| f||dœ|—ŽS )zPick only data channels.T)r   r   r   r   )r   rx   )Ú_PICK_TYPES_DATA_DICTr·   rU   r«   )re   rx   Úwith_ref_megZwith_auxÚkwargsrZ   rZ   r[   Ú_pick_data_channelsÛ  s
    rõ   c                 C   s8   t dd„ | d D ƒƒr&t| |dd�}nt| |dd�}|S )zPick only data or ICA channels.c                 s   s   | ]}|  d ¡V  qdS )ZICAN)Ú
startswith)r€   r`   rZ   rZ   r[   rÞ   ç  s     z$_pick_data_or_ica.<locals>.<genexpr>rv   T)rx   r   ©rx   ró   )rß   r«   rõ   )re   rx   r½   rZ   rZ   r[   Ú_pick_data_or_icaå  s    rø   r°   c              	   C   s¸  ddl m} d}t| |ƒr$| d }	nt| ddƒ} | }	|	dks@t‚|}
d}|d	kr~t| tƒrpt |	¡}d
|	f }n|}d|f }t|tƒr–t |	¡| }t 	|¡}t
|ƒdkrºtjg td�n|}|jdkrØtd|jf ƒ‚|jjdk�rt| |||||||
ƒ}|�r|d }|d }|jjdk�r0td|jf ƒ‚~| t¡}t
|ƒdk�r^|�s^td|
f ƒ‚||	 k  ¡ �r€td|	 |
f ƒ‚||	k ¡ �ržtd|	|
f ƒ‚||	; }|�r´||fS |S )z Convert and check pick validity.r   rð   Fr™   re   zan int or Infor   Ú Nz, treated as range(%d)zNone, treated as "%s"rš   zpicks must be 1D, got %sD)ÚSÚU)r²   ÚuzApicks must be a list of int or list of str, got a data type of %sz6No appropriate channels found for the given picks (%r)zAll picks must be >= %d, got %rz+All picks must be < n_channels (%d), got %r)r¶   rñ   rn   r
   r¦   ru   rj   r¸   ÚsliceZ
atleast_1dri   rt   Úndimrc   r›   ÚcharÚ_picks_str_to_idxr   Ú	TypeErrorZastyperß   )re   r½   rÑ   rx   rÒ   ró   Úreturn_kindrñ   Úpicked_ch_type_or_genericZn_chanÚ
orig_picksÚ
extra_reprrZ   rZ   r[   rÔ   î  sj    








  þÿ
ÿÿÿrÔ   c              	   C   s   t | tƒrtdƒ‚tƒ }t|ƒdkrÊ|d dkrÊ|d dkrh|dkrL| d n|}	t| d | d |	d�}n4|d d	kr„t| ||d
�}n|d dkrœt| |d�}t|ƒdkrÊ|dkrÊ|sÊtdt|ƒ| f ƒ‚g }
tƒ }|D ]>}z| 	| d  
|¡¡ W qØ tk
�r   |
 	|¡ Y qØX qØd}tƒ }tdd�}tƒ tƒ  }}|D ]P}|tk�rVd||< n6|tk�rl||hO }n |tk�r‚||hO }n
|} �qT�q>tƒ }t|ƒdk�rÖ| dd¡�sÖ|D ]}|tt| |d|d�ƒO }�q¶t|ƒdk�r| dd¡�st|ƒdk�rt|ƒd |d< nt|ƒ|d< t| fd|i|—Ž}t|ƒdk�rTtt|ƒt|ƒB ƒ}|||f}dd„ |D ƒ}t|ƒdk�r¬|�sžtdt|ƒ| t|
ƒ|f ƒ‚t g t¡}n4t|ƒdk�rÄtdƒ‚nt |t |¡d d  ¡}t|ƒ }t|
ƒdk�r|�std|
› d�ƒ |�r||fS |S )z'Turn a list of str into ndarray of int.z@picks as str can only be used when measurement info is availabler   r   )rÐ   r°   Údata_or_icarÐ   r”   rv   ©rx   r°   r÷   r  NzApicks (%s) yielded no channels, consider passing picks explicitlyFrá   Tr]   )r]   r   rx   r^   rx   c                 S   s   g | ]}t |ƒd k‘qS )r   rê   rë   rZ   rZ   r[   rƒ   …  s     z%_picks_str_to_idx.<locals>.<listcomp>zŽpicks (%s) could not be interpreted as channel names (no channel "%s"), channel types (no type "%s"), or a generic type (just "all" or "data")zgSome channel names are ambiguously equivalent to channel types, cannot use string-based picks for thesezChannel(s) zH could not be picked, because they are not present in the info instance.)rn   ru   rc   rr   ri   r~   rõ   rø   Úreprrq   rs   r+   ro   rÓ   rå   r¤   r–   r«   ræ   Úsumr‘   rj   rt   rl   r§   r   )re   r½   rx   ró   r  r  rÒ   r  Zpicks_genericZuse_excludeZ	bad_namesZ
picks_namer    Zbad_typeZ
picks_typerô   r]   r^   Zextra_picksZuse_megZ	all_picksZ	any_foundr  rZ   rZ   r[   r   5  sž    
  ÿÿÿþ





   ÿ
ýÿ

r   c                    sr   |dkrˆ   ¡ ‰ tˆ j|g d�}‡ fdd„|D ƒ}ˆ  |¡ |dkr\‡ fdd„ˆ jd D ƒ}|dk	rnˆ  |¡ ˆ S )z5Return an instance with picked and excluded channels.Tr  c                    s   g | ]}ˆ j d  | ‘qS r�   r­   ©r€   r    ©ÚinstrZ   r[   rƒ   ¤  s     z_pick_inst.<locals>.<listcomp>r”   c                    s   g | ]}|ˆ j d  kr|‘qS r�   r­   r®   r  rZ   r[   rƒ   ¨  s    ÿN)r·   rÔ   re   r~   Zdrop_channels)r  r½   rx   r·   Z
pick_namesrZ   r  r[   Ú
_pick_instŸ  s    

r  c                    s`   |rdnd}t ˆ ||ddd�}‡ fdd„|D ƒ}|rBdd„ |D ƒ}|r\td	d
„ |D ƒ ¡ ƒ}|S )z/Get the data channel types in an info instance.r°   rÐ   rZ   F)rÒ   c                    s   g | ]}t ˆ |ƒ‘qS rZ   râ   r
  r­   rZ   r[   rƒ   ³  s     z&_get_channel_types.<locals>.<listcomp>c                 S   s   g | ]}|t kr|‘qS rZ   )rì   rè   rZ   rZ   r[   rƒ   µ  s    ÿc                 S   s   i | ]
}||“qS rZ   rZ   rž   rZ   rZ   r[   rÏ   ¹  s      z&_get_channel_types.<locals>.<dictcomp>)rÔ   rr   rÕ   )re   r½   rk   Zonly_data_chsrÑ   Zch_typesrZ   r­   r[   Ú_get_channel_types¯  s    r  )F)F)FFFFFFr�   FFFFFFFFFFFFFFrZ   r”   N)rZ   TN)N)FFr”   )NF)r”   TF)rZ   )r°   r”   FTF)T)NFF)^r·   r   r„   Únumpyrj   Ú	constantsr   Úutilsr   r   r   r	   r
   r   r   r\   r,   r/   r0   r;   r5   r6   r7   r8   r:   r<   r>   r=   r2   r3   r9   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   r@   r?   r4   rK   ra   r-   r.   rP   rQ   rL   rM   rN   rO   r1   ZFIFFV_COIL_EEG_BIPOLARrS   rR   rd   rh   r~   r‡   r�   r�   r“   r˜   r«   r¼   r¿   rÄ   rË   rÌ   rÖ   rÜ   rà   rç   rï   rm   r+   rò   r—   rr   rÓ   rå   r¤   Z_DATA_CH_TYPES_ORDER_DEFAULTZ_VALID_CHANNEL_TYPESrì   rõ   rø   rÔ   r   r  r  rZ   rZ   rZ   r[   Ú<module>   s†  $
f                             ã! ÿ  ø
   ýó
!C"

                                   ü @+ ÿT   ÿ
+ÿ
4
7
                 üýýþþÿ  ÿ


	    ÿ
Gj
