U
    f»{j»
 ã                   @   s  d Z ddlmZmZmZ ddlZddlZddlZddlm	Z	m
Z
mZ ddlmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlmZ ejejejejejejej ej!ej"ej#ej!dœZ$eG dd„ deƒƒZ%eG dd„ deƒƒZ&dJdd„Z'dd„ Z(dKdd„Z)dLdd„Z*dd„ Z+dd„ Z,dd „ Z-dMd!d"„Z.d#Z/d$Z0d%Z1d&Z2d'Z3d(Z4d)Z5d*Z6d+Z7d,Z8de/e0e1e2e3e4e5e6ddddddde7e8fZ9e:d-d.„ e9D ƒƒZ;d/d0„ Z<dNd1d2„Z=d3d4gfd5d6„Z>dOd7d8„Z?d9d:„ Z@edPd>d?„ƒZAedQd@dA„ƒZBedRdBdC„ƒZCdDdE„ ZDdFdG„ ZEdHdI„ ZFdS )Sz+Reading tools from EDF, EDF+, BDF, and GDF.é    )ÚdatetimeÚtimezoneÚ	timedeltaNé   )ÚverboseÚloggerÚwarné   )Ú_blk_read_limsÚ_mult_cal_one)ÚBaseRaw)Ú_empty_infoÚ_unique_channel_names)ÚFIFF)Úresample)Úfill_doc)ÚAnnotations)ÚEEGÚSEEGÚECOGÚDBSZEOGZECGZEMGZBIOZRESPZMISCZSAO2c                	       s.   e Zd ZdZed
‡ fdd„	ƒZdd	„ Z‡  ZS )ÚRawEDFaÍ  Raw object from EDF, EDF+ or BDF file.

    Parameters
    ----------
    input_fname : str
        Path to the EDF, EDF+ or BDF file.
    eog : list or tuple
        Names of channels or list of indices that should be designated EOG
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    misc : list or tuple
        Names of channels or list of indices that should be designated MISC
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    stim_channel : 'auto' | str | list of str | int | list of int
        Defaults to 'auto', which means that channels named 'status' or
        'trigger' (case insensitive) are set to STIM. If str (or list of str),
        all channels matching the name(s) are set to STIM. If int (or list of
        ints), the channels corresponding to the indices are set to STIM.
    exclude : list of str
        Channel names to exclude. This can help when reading data with
        different sampling rates to avoid unnecessary resampling.
    infer_types : bool
        If True, try to infer channel types from channel labels. If a channel
        label starts with a known type (such as 'EEG') followed by a space and
        a name (such as 'Fp1'), the channel type will be set accordingly, and
        the channel will be renamed to the original label without the prefix.
        For unknown prefixes, the type will be 'EEG' and the name will not be
        modified. If False, do not infer types and assume all channels are of
        type 'EEG'.

        .. versionadded:: 0.24.1
    include : list of str | str
        Channel names to be included. A str is interpreted as a regular
        expression. 'exclude' must be empty if include is assigned.

        .. versionadded:: 1.1
    %(preload)s
    %(verbose)s

    See Also
    --------
    mne.io.Raw : Documentation of attributes and methods.
    mne.io.read_raw_edf : Recommended way to read EDF/EDF+ files.
    mne.io.read_raw_bdf : Recommended way to read BDF files.

    Notes
    -----
    Biosemi devices trigger codes are encoded in 16-bit format, whereas system
    codes (CMS in/out-of range, battery low, etc.) are coded in bits 16-23 of
    the status channel (see http://www.biosemi.com/faq/trigger_signals.htm).
    To retrieve correct event values (bits 1-16), one could do:

        >>> events = mne.find_events(...)  # doctest:+SKIP
        >>> events[:, 2] &= (2**16 - 1)  # doctest:+SKIP

    The above operation can be carried out directly in :func:`mne.find_events`
    using the ``mask`` and ``mask_type`` parameters (see
    :func:`mne.find_events` for more details).

    It is also possible to retrieve system codes, but no particular effort has
    been made to decode these in MNE. In case it is necessary, for instance to
    check the CMS bit, the following operation can be carried out:

        >>> cms_bit = 20  # doctest:+SKIP
        >>> cms_high = (events[:, 2] & (1 << cms_bit)) != 0  # doctest:+SKIP

    It is worth noting that in some special cases, it may be necessary to shift
    event values in order to retrieve correct event triggers. This depends on
    the triggering device used to perform the synchronization. For instance, in
    some files events need to be shifted by 8 bits:

        >>> events[:, 2] >>= 8  # doctest:+SKIP

    TAL channels called 'EDF Annotations' or 'BDF Annotations' are parsed and
    extracted annotations are stored in raw.annotations. Use
    :func:`mne.events_from_annotations` to obtain events from these
    annotations.

    If channels named 'status' or 'trigger' are present, they are considered as
    STIM channels by default. Use func:`mne.find_events` to parse events
    encoded in such analog stim channels.
    NÚauto© Fc
                    s  t  d |¡¡ tj |¡}t||||||||ƒ\}
}}t  d¡ |d d g}tƒ j|
||g|g|d||	d� t	ƒ t	ƒ t	ƒ   }}}t
|d ƒdkrêt dt¡}|  t d| jf¡|ddt| jƒt t
|ƒdf¡d ¡}t|d ƒ\}}}|  t|||d d	�¡ d S )
Nú$Extracting EDF parameters from {}...úCreating raw.info structure...Únsamplesé   Úint©Ú	filenamesÚ
raw_extrasÚ
last_sampsZorig_formatÚ
orig_unitsr   Útal_idxr   ©ÚonsetÚdurationÚdescriptionZ	orig_time)r   ÚinfoÚformatÚosÚpathÚabspathÚ	_get_infoÚsuperÚ__init__ÚlistÚlenÚnpÚemptyr   Ú_read_segment_fileZn_timesÚonesÚ_read_annotations_edfÚset_annotationsr   )ÚselfÚinput_fnameÚeogÚmiscÚstim_channelÚexcludeÚinfer_typesÚpreloadÚincluder   r)   Úedf_infor#   r"   r&   r'   ÚdescÚidxÚtal_data©Ú	__class__r   úM/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/edf/edf.pyr0   „   sF       þ

  ý     þ
 ÿzRawEDF.__init__c              
   C   s$   t |||||| j| | j| ||ƒ	S ©úRead a chunk of raw data.©r5   Z_raw_extrasZ
_filenames©r9   ÚdatarD   ÚfiÚstartÚstopÚcalsÚmultr   r   rH   r5   £   s      þzRawEDF._read_segment_file)NNr   r   FFNN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r0   r5   Ú__classcell__r   r   rF   rH   r   .   s   T         þr   c                       s.   e Zd ZdZed
‡ fdd„	ƒZdd	„ Z‡  ZS )ÚRawGDFa†  Raw object from GDF file.

    Parameters
    ----------
    input_fname : str
        Path to the GDF file.
    eog : list or tuple
        Names of channels or list of indices that should be designated EOG
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    misc : list or tuple
        Names of channels or list of indices that should be designated MISC
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    stim_channel : 'auto' | str | list of str | int | list of int
        Defaults to 'auto', which means that channels named 'status' or
        'trigger' (case insensitive) are set to STIM. If str (or list of str),
        all channels matching the name(s) are set to STIM. If int (or list of
        ints), channels corresponding to the indices are set to STIM.
    exclude : list of str
        Channel names to exclude. This can help when reading data with
        different sampling rates to avoid unnecessary resampling.

        .. versionadded:: 0.24.1
    include : list of str | str
        Channel names to be included. A str is interpreted as a regular
        expression. 'exclude' must be empty if include is assigned.

        .. versionadded:: 1.1
    %(preload)s
    %(verbose)s

    See Also
    --------
    mne.io.Raw : Documentation of attributes and methods.
    mne.io.read_raw_gdf : Recommended way to read GDF files.

    Notes
    -----
    If channels named 'status' or 'trigger' are present, they are considered as
    STIM channels by default. Use func:`mne.find_events` to parse events
    encoded in such analog stim channels.
    Nr   r   Fc	              
      s    t  d |¡¡ tj |¡}t|||||d||ƒ\}	}
}t  d¡ |
d d g}tƒ j|	||g|
g|d||d� t	|
| jd ƒ\}}}|  
t|||d d	�¡ d S )
Nr   Tr   r   r   r   r   Úsfreqr%   )r   r)   r*   r+   r,   r-   r.   r/   r0   Ú_get_annotations_gdfr8   r   )r9   r:   r;   r<   r=   r>   r@   rA   r   r)   rB   r#   r"   r&   r'   rC   rF   r   rH   r0   Ø   s2       þ

  ýÿ

 ÿzRawGDF.__init__c              
   C   s$   t |||||| j| | j| ||ƒ	S rI   rK   rL   r   r   rH   r5   ñ   s      þzRawGDF._read_segment_file)NNr   r   FNNrS   r   r   rF   rH   rY   ª   s   ,         þrY   c                 C   s�   |dkr|t j| ||| d�}| dd¡ t¡}|dd…df |dd…df d>  |dd…d	f d
>  }||dk  d8  < nt j| ||d�}|S )z.Read a number of samples for a single channel.Úbdf)ÚdtypeÚcountéÿÿÿÿr   Nr   r   é   r	   é   i  € i   )r3   ÚfromfileÚreshapeÚastypeÚINT32)ÚfidÚsubtypeZsampÚ
dtype_byter]   Úch_datar   r   rH   Ú_read_chø   s    ÿþrj   c	           /   
   C   s¢  ddl m}	 |d }
t|d ƒ}|d }|d }|d }|d }|d	 }| d
t dt¡¡}|d }|d }|d }|d }t || |g¡}g }t|tƒr¬t 	|j
|j¡n|}tjt dg|
g¡tjd�}t|||ƒ\}}}td|d |  dƒ}t|ddd���`}|||d  |  }tdt|ƒ|ƒD �]2} | |d  | }!tt|ƒ|  |ƒ}"| ||! d¡ t|||d |" ||ƒ |"d¡}#||  d }$||"d  || |" d  d  }%||  d }&|| |" d  d }'tjt|ƒ|'|& f| jd�}(t|ƒD �]F\})}*|#dd…||* ||*d  …f  ¡ }+|*|k�r8| |+¡ �qò||) },|+||,  }+|+||, 7 }+|+||, 9 }+|*||, k�svt‚|
|* |k�r|,|k�rèt dd|
|* d d¡}-t dd|d¡}.t |+t t|+ƒdf¡d¡}+|	|-|+ddd�|.ƒ}+nt|+ tj ¡||
|* ddd�}+n|,|k�r$t !|+ t¡d¡}+|+ "¡ |$|%… |(|,< �qòt#| dd…|&|'…f |(|||ƒ �q*W 5 Q R X t|ƒdk�ržt dd„ |D ƒ¡}|tj$dd…f }|S )rJ   r   )Úinterp1dÚn_sampsÚmax_sampÚdtype_nprh   Údata_offsetÚstim_channel_idxsÚselr$   rg   ÚcalÚoffsetsÚunits©r]   i    r_   r   Úrb)Ú	bufferingNTFÚzero)ÚkindÚaxis)Znpadrz   iÿÿ c                 S   s   g | ]}|  ¡ ‘qS r   )Úravel)Ú.0Útalr   r   rH   Ú
<listcomp>c  s     z&_read_segment_file.<locals>.<listcomp>)%Zscipy.interpolaterk   r   Úgetr3   r4   ÚconcatenateÚ
isinstanceÚsliceÚarangerO   rP   ZcumsumÚint64r
   ÚmaxÚopenÚranger2   ÚminÚseekrj   rc   Úzerosr]   Ú	enumerateÚcopyÚappendÚAssertionErrorZlinspacer   rd   Zfloat64Zbitwise_andr{   r   Znewaxis)/rM   rD   rN   rO   rP   r!   r    rQ   rR   rk   rl   Zbuf_lenr]   rh   ro   rp   Zorig_selr$   rg   rr   rs   ZgainsZread_selrE   Zidx_arrZ
ch_offsetsZblock_start_idxZr_limsZd_limsZn_perrf   Zstart_offsetZaiZblock_offsetZn_readZ
many_chunkZr_sidxZr_eidxZd_sidxZd_eidxÚoneÚiiÚciri   Zorig_idxÚoldÚnewr   r   rH   r5     s¤    ÿ ÿ ÿ  
ÿ


  ÿ ÿÿ
   þ
.r5   c                 C   sp   t j | ¡d dd…  ¡ }t d| ¡  ¡ |dkrDt| |||ƒS |dkr\t| ||ƒdfS t	d|› d�ƒ‚dS )aP  Unify EDF, BDF and GDF _read_header call.

    Parameters
    ----------
    fname : str
        Path to the EDF+, BDF, or GDF file.
    exclude : list of str | str
        Channel names to exclude. This can help when reading data with
        different sampling rates to avoid unnecessary resampling. A str is
        interpreted as a regular expression.
    infer_types : bool
        If True, try to infer channel types from channel labels. If a channel
        label starts with a known type (such as 'EEG') followed by a space and
        a name (such as 'Fp1'), the channel type will be set accordingly, and
        the channel will be renamed to the original label without the prefix.
        For unknown prefixes, the type will be 'EEG' and the name will not be
        modified. If False, do not infer types and assume all channels are of
        type 'EEG'.
    include : list of str | str
        Channel names to be included. A str is interpreted as a regular
        expression. 'exclude' must be empty if include is assigned.

    Returns
    -------
    (edf_info, orig_units) : tuple
    r   Nz%s file detected)r\   ÚedfÚgdfz0Only GDF, EDF, and BDF files are supported, got Ú.)
r+   r,   ÚsplitextÚlowerr   r)   ÚupperÚ_read_edf_headerÚ_read_gdf_headerÚNotImplementedError)Úfnamer>   r?   rA   Úextr   r   rH   Ú_read_headerh  s    
ÿrŸ   c           "         sV  |dk	r|ng }|dk	r|ng }t | |||ƒ\}}	t||d ƒ\‰}
|d }|d ‰ d|krf|d }ndgt|ƒ }t|ƒdkr�|d dg }n|d | }|d }|d |d	  }|d
 |d  }t t |¡ |dkB ¡d }t|ƒdk�rtdd ‡ fdd„|D ƒ¡ ƒ d||< t |dk¡d }t|ƒdk�r`tdd ‡ fdd„|D ƒ¡ ƒ d||< t 	d¡ t
ƒ }t tˆ ƒ¡}t
ƒ }tˆ ƒD �]¸\}}i }d|d< |d |d< |d |d< d|d< tj|d< ||d< tj|d< tj|d< tj|d< tj|d< t dtj¡|d < || }|dk	�rT|tk�rTt |¡|d< |d!k�rTtj|d< d"||< ||k�sv||k�sv|| |k�r”tj|d< tj|d< d"||< n¦||k�s¶||k�s¶|| |k�rÔtj|d< tj|d< d"||< nf|ˆk�r&tj|d< tj|d< tj|d< d"||< ||d< |d ˆ |< d|d# |< n|tk�r:| |¡ | |¡ �qŒt|ƒ�rld$d |¡› �}t 	|¡ ˆ|d%< t|ƒ�r¬d&d'„ tt|ƒ|ƒD ƒ}||   ¡  |d(< }n|  ¡  |d(< }‡fd)d'„t|j!d ƒD ƒ}t|ƒdk�röt
tt|ƒƒƒ}t "||¡  ¡ |d* d  |d* d  }~t#|ƒ}|d+ |d+< ||d,< ˆ |d< |d- }|d. } |j$dk�rbn†t%|ƒ�rÎ|d d/k�r|nP|d d0k�r”d1|d-< n8|d }!zt&|!ƒ}!W n t'k
�rÂ   d1}!Y nX |!|d-< nt&t  |¡ƒ|d-< td2ƒ t (|d- ¡�r d1|d-< | j$dk�rnFt%| ƒ�r:| d d3k�r(nt&| d ƒ|d.< nt&t )| ¡ƒ|d.< td4ƒ t (|d. ¡�rt|d5 d6 |d.< |d- |d. k�r¼td7|d- › d8|d. › d9�ƒ d1|d-< |d5 d6 |d.< d|d:< t*|d; | ƒ|d<< d"|_+| ,¡  || |d< |d	 |d |d   |d=< |d	= |d= |d> d?k�rLd|d ˆ< d|d= ˆ< d|d# ˆ< |||	fS )@z/Extract information from EDF+, BDF or GDF file.NÚch_namesrq   Úch_typesr   rl   ÚnchanÚphysical_maxÚphysical_minÚdigital_maxÚdigital_minz5Scaling factor is not defined in following channels:
z, c                 3   s   | ]}ˆ | V  qd S ©Nr   ©r|   Úi©r    r   rH   Ú	<genexpr>¬  s     z_get_info.<locals>.<genexpr>r   z5Physical range is not defined in following channels:
c                 3   s   | ]}ˆ | V  qd S r§   r   r¨   rª   r   rH   r«   ±  s     z!Setting channel info structure...ç      ð?rr   ZlognoZscannor‡   Zunit_mulÚch_nameÚunitZcoord_frameZ	coil_typery   é   Úloc)r   r   r   r   Frt   zUCould not determine channel type of the following channels, they will be set as EEG:
rp   c                 S   s   g | ]\}}|r|‘qS r   r   )r|   ÚitemÚmaskr   r   rH   r~   ð  s      z_get_info.<locals>.<listcomp>rm   c                    s   g | ]}|ˆ kr|‘qS r   r   ©r|   Úx)rp   r   rH   r~   ø  s    ÿÚrecord_lengthÚ	meas_dateÚchsÚhighpassÚlowpassÚNaNZDCç        zSChannels contain different highpass filters. Highest filter setting will be stored.)rº   Ú0z0.0zQChannels contain different lowpass filters. Lowest filter setting will be stored.rZ   g       @zHighpass cutoff frequency z* is greater than lowpass cutoff frequency z", setting values to 0 and Nyquist.r(   Ú	n_recordsr   rs   rg   r\   )-rŸ   Ú_check_stim_channelr2   r3   ÚwhereÚisfiniter   Újoinr   r)   r1   r6   r‹   r   ZFIFF_UNITM_NONEZFIFF_UNIT_VZFIFFV_COORD_HEADZFIFFV_COIL_EEGÚFIFFV_EEG_CHÚfullÚnanÚCH_TYPE_MAPPINGr   ZFIFFV_COIL_NONEÚFIFFV_EOG_CHÚFIFFV_MISC_CHZFIFF_UNIT_NONEZFIFFV_STIM_CHr�   ÚanyÚzipr‡   r…   ÚshapeZtaker   ÚsizeÚallÚfloatÚ	ExceptionÚisnanrˆ   r   Z	_unlockedZ_update_redundant)"r�   r=   r;   r<   r>   r?   r@   rA   rB   r#   Ú_rq   r¡   rl   r¢   Zphysical_rangesrQ   Zbad_idxr·   Z	pick_maskZchs_without_typesrD   r­   Z	chan_infoÚch_typeÚmsgZpicksrm   Znot_stim_chrZ   r)   r¸   r¹   Úhpr   )r    rp   rH   r.   Ž  s    ÿ
ÿÿ







"


"












ÿ
ÿ




ÿr.   c                 C   sD   t  dd„ dd„ | D ƒD ƒ¡}t  dd„ dd„ | D ƒD ƒ¡}||fS )z1Parse prefilter string from EDF+ and BDF headers.c                 S   s   g | ]}|D ]}|‘qqS r   r   ©r|   rÓ   Úvr   r   rH   r~   N  s     ÿz+_parse_prefilter_string.<locals>.<listcomp>c                 S   s   g | ]}t  d |¡‘qS )zHP:\s*([0-9]+[.]*[0-9]*)©ÚreÚfindall©r|   Zfiltr   r   rH   r~   N  s   ÿc                 S   s   g | ]}|D ]}|‘qqS r   r   rÔ   r   r   rH   r~   R  s     ÿc                 S   s   g | ]}t  d |¡‘qS )zLP:\s*([0-9]+[.]*[0-9]*)rÖ   rÙ   r   r   rH   r~   R  s   ÿ)r3   Úarray)Úprefilteringr¸   r¹   r   r   rH   Ú_parse_prefilter_stringK  s    ÿÿÿÿrÜ   c                 C   s   |   d¡ d¡d S )Núlatin-1ú r   )ÚdecodeÚsplit©r´   r   r   rH   Ú_edf_strX  s    râ   c                 C   s   t | ƒ dd¡S )Nú,r–   )râ   Úreplacerá   r   r   rH   Ú_edf_str_num\  s    rå   c           2         st  dg i}t | dƒ��R‰ˆ d¡ i }ˆ d¡ d¡ ¡ }| d¡}t|ƒr°|d |d< t|ƒd	kr°zt |d
 d¡}W n tk
rŽ   d}Y nX |d |d< ||d< |d |d< i }ˆ d¡ d¡ ¡  d¡}	d}
t|	ƒdk�r@zt |	d d¡}W n tk
�r   d}Y nX d}
||d< |	d
 |d< |	d |d< |	d	 |d< |
�rp|d j	}|d j
}|d j}ˆ d¡ nDˆ d¡ d¡}dd„ | d¡D ƒ\}}}|dk �r¬|d n|d }ˆ d¡ d¡}dd„ | d¡D ƒ\}}}zt||||||tjd �}W nT tk
�rN   td!|d"›d#|d$›d#|d$›d|d$›d%|d$›d%|d$›d&�ƒ d'}Y nX ttˆ d¡ƒƒ}ˆ d(¡ tj | ¡d dd'…  ¡ }ttˆ d¡ƒƒ}ttˆ d¡ƒƒ}t |d)g¡}|d dk�rØd)|d< td*ƒ ttˆ d	¡ƒƒ}tt|ƒƒ}‡fd+d„|D ƒ}|�rªg g  }‰ |D ]Š}d,| }}| d¡}t|ƒdk�r�|d  ¡ tk�r�|d  ¡ }d |dd'… ¡}t d-|› d.|› d/|› d0�¡ | |¡ ˆ  |¡ �qnd,g| | }‰ t ˆ ||ƒ}t!ˆ ƒ} t "|| g¡}t #t $tˆ ƒ¡|¡}!|D ]}"ˆ d¡ �qö‡fd1d„|D ƒ‰tƒ |d2< t%ˆƒD ]Z\}#}$|#|k�rB�q,|$d3k�r\|d2  d4¡ n(|$d5k�rv|d2  d6¡ n|d2  d¡ �q,t |d2 t¡|d2< ‡ fd7d„|!D ƒ‰ ‡fd8d„|!D ƒ‰t&ˆ ƒ‰ t't(ˆ ˆƒƒ}%t ‡fd9d„|D ƒ¡|! }&t ‡fd:d„|D ƒ¡|! }'t ‡fd;d„|D ƒ¡|! }(t ‡fd<d„|D ƒ¡|! })‡fd=d„|D ƒd'd>… }*t)|*ƒ\}+},t ‡fd?d„|D ƒ¡}-|j*ˆ |||)|(|+|!|,|||-|||'|&||| d@� ˆ dA| ¡ ¡  ˆ +¡ |k�sÖt,‚ˆ -dd
¡ ˆ +¡ }.|.| }/|dBk�r|/d n|/d
 }0|0t .|-¡ }1||1k�r4tdCƒ |1|dD< ~|dBk�rRd|dE< t/|dF< nd
|dE< t0|dF< W 5 Q R X ||%fS )Gz.Read header information from EDF+ or BDF file.Úeventsrv   r`   éP   rÝ   ú r   Úidé   r	   z%d-%b-%YÚXr   ÚsexÚbirthdayr   ÚnameFé   TÚ	startdateZstudy_idÚ
technicianÚ	equipmentc                 S   s   g | ]}t |ƒ‘qS r   ©r   r³   r   r   rH   r~   �  s     z$_read_edf_header.<locals>.<listcomp>r–   éU   iÐ  il  c                 S   s   g | ]}t |ƒ‘qS r   ró   r³   r   r   rH   r~   ”  s     ©ÚtzinfozInvalid date encountered (Z04dú-Ú02dú:z).Né,   r¬   zåHeader information is incorrect for record length. Default record length set to 1.
It is possible that this file only contains annotations and no signals. In that case, please use mne.read_annotations() to load these annotations.c                    s    g | ]}ˆ   d ¡ ¡  d¡‘qS )ra   rÝ   ©ÚreadÚstriprß   )r|   rÐ   ©rf   r   rH   r~   µ  s     r   z	Channel 'z' recognized as type z (renamed to 'z').c                    s    g | ]}ˆ   d ¡ ¡  d¡‘qS )r`   rÝ   rû   ©r|   Úchrþ   r   rH   r~   Ñ  s     rt   )u   Î¼Vu   ÂµVu   ÂƒÃŠVÚuVç�íµ ÷Æ°>ZmVçü©ñÒMbP?c                    s   g | ]}ˆ | ‘qS r   r   ©r|   rD   rª   r   rH   r~   ß  s     c                    s   g | ]}ˆ | ‘qS r   r   r  )rt   r   rH   r~   à  s     c                    s   g | ]}t tˆ  d ¡ƒƒ‘qS ©r`   ©rÍ   rå   rü   rÿ   rþ   r   rH   r~   ç  s     c                    s   g | ]}t tˆ  d ¡ƒƒ‘qS r  r  rÿ   rþ   r   rH   r~   é  s     c                    s   g | ]}t tˆ  d ¡ƒƒ‘qS r  r  rÿ   rþ   r   rH   r~   ë  s     c                    s   g | ]}t tˆ  d ¡ƒƒ‘qS r  r  rÿ   rþ   r   rH   r~   í  s     c                    s   g | ]}t ˆ  d ¡ƒ ¡ ‘qS ©rç   ©râ   rü   rý   rÿ   rþ   r   rH   r~   î  s     r_   c                    s   g | ]}t tˆ  d ¡ƒƒ‘qS r  )r   râ   rü   rÿ   rþ   r   rH   r~   ò  s     )r    r¡   ro   r¥   r¦   r¸   rq   r¹   r¶   r½   rl   r¢   Úsubject_infor£   r¤   rµ   rg   r$   é    r\   z”Number of records from the header does not match the file size (perhaps the recording was not stopped before exiting). Inferring from the file size.r½   rh   rn   )1r†   rü   rß   Úrstriprà   r2   r   ÚstrptimeÚ
ValueErrorÚdayÚmonthÚyearr   Úutcr   r   râ   r+   r,   r—   r˜   rÍ   r3   rÚ   r1   r‡   r™   rÅ   rÁ   r   r)   r�   Ú_find_exclude_idxÚ_find_tal_idxr€   Ú	setdiff1drƒ   r‹   r   ÚdictrÉ   rÜ   ÚupdateÚtellrŽ   r‰   ÚsumÚUINT8ÚINT16)2r�   r>   r?   rA   rB   ÚpatientZid_infoZ	birthdateÚmeas_idZrec_infoZvalid_startdaterð   r  r  r  r¶   Z	meas_timeÚhourÚminuteÚsecÚheader_nbytesrg   r½   rµ   r¢   ÚchannelsZ	ch_labelsr¡   Zch_labelrÑ   r­   Úpartsr$   rq   r   r©   r®   r#   r¤   r£   r¦   r¥   rÛ   r¸   r¹   rl   Zn_bytesZn_data_bytesZtotal_sampsZread_recordsr   )r    rf   rt   rH   rš   `  s4   






ÿ
:









ÿÿÿÿÿÿÿÿ           ù	ÿ


rš   z<i1z<u1z<i2z<u2z<i4z<u4z<i8z<u8z<f4z<f8c                 c   s&   | ]}|d k	rt  |¡jndV  qd S )Nr   )r3   r]   Úitemsizer³   r   r   rH   r«   %  s   ÿr«   c                 C   sV   t tƒdkst‚dd„ | D ƒ}dd„ | D ƒ}tt |¡ƒdkrFtdƒ‚|d |d fS )Né*   c                 S   s   g | ]}t | ‘qS r   ©ÚGDFTYPE_BYTE©r|   Útr   r   rH   r~   +  s     z%_check_dtype_byte.<locals>.<listcomp>c                 S   s   g | ]}t | ‘qS r   )Ú
GDFTYPE_NPr'  r   r   rH   r~   ,  s     r   z)Reading multiple data types not supportedr   )r  r&  rŽ   r2   r3   ÚuniqueÚRuntimeError)Útypesrh   rn   r   r   rH   Ú_check_dtype_byte)  s    r-  c           ;         s„  t ƒ }d}t| dƒ��H‰ˆ d¡ ¡ }|dd…  |d< |d< t|dd… ƒ|d< d}|d d	k �rTˆ d
¡ d¡}| dd¡}i }t|ƒdkr¨|d |d< |d |d< i }	tˆ d
¡ƒ ¡ |	d< tˆ d¡ƒ ¡ }
zª|
dd… dk�r|
dd… d |
dd…  }
t	t
|
dd… ƒt
|
dd… ƒt
|
dd… ƒt
|
dd… ƒt
|
dd… ƒt
|
dd… ƒt
|
dd… ƒtddƒ tjd�}W n tk
�r”   Y nX t ˆtd¡d }t ˆtd¡d |	d< t ˆtd¡d |	d< t ˆtd¡d |	d< ˆ dd¡ t ˆtd¡d }t ˆtd¡}|d dk�r4d|d< td ƒ t ˆtd¡d }tt|ƒƒ}‡fd!d"„|D ƒ‰ tˆ ||ƒ}t t tˆ ƒ¡|¡}ˆ d
t|ƒ d¡ ‡fd#d"„|D ƒ}tƒ |d$< t|ƒD ]H\}}||k�rÔ�q¾|dd… d%k�rö|d$  d&¡ n|d$  d¡ �q¾t |d$ t¡|d$< ‡ fd'd"„|D ƒ‰ t ˆtt|ƒ¡}t ˆtt|ƒ¡}t ˆtt|ƒ¡}t ˆtt|ƒ¡}‡fd(d"„|D ƒdd)… }t|ƒ\}}t ˆt t|ƒ¡‰t ˆt t|ƒ¡}t !‡fd*d"„t|ƒD ƒ¡}t"|ƒ\}}|j#|ˆ |||||||||||	|ˆ|||||d+� ˆ d,|d-  d¡ ˆ $¡ |k�s@t%‚|||d.   }ˆ |¡ t ˆtd¡d } | d/k�rZt ˆtd¡}!|!d }"tdt|!ƒƒD ]}|"|!| d|d    }"�qšt ˆtd¡d }#t ˆt|#¡d }$t ˆt&|#¡}%| dk�rt ˆt&|#¡}&t ˆt|#¡}'ntj'|#tj(d0�}&tj)|#td0�}'tj*|'d|'d1� |#|$|%|&|'g}�
nd2}(d3})d4}*ˆ d5¡ ¡ }| dd¡}i }t|ƒdk�r¤|d |d< |d |d< ˆ dd¡ t ˆtd¡d }+|*|+d  |d6< |*|+d? d  |d7< |*|+d? d  |d8< |*|+d? d  |d9< t ˆtd¡d |d:< |d: dk�s@|d: d;k�rHd|d:< t ˆtd¡d |d<< |d< dk�sz|d< d;k�r‚d|d<< t ˆtd¡d },|)|,d  |d=< |(|,d? d  |d>< |*|,d? d  |d?< i }	tˆ d@¡ƒ ¡ |	d< t ˆtd¡}-i }.|-d dk�r€dt
|-d d? ƒ t
|-d d ƒ |.dA< dt
|-d d? ƒ t
|-d d ƒ |.dB< dt
|-d d? ƒ t
|-d d ƒ |.dC< ndD|.dA< dD|.dB< dD|.dC< d|.dE< tt ˆtd¡d ƒdF |.dG< tt ˆtd¡d ƒdF |.dH< tt ˆt d¡d ƒdI |.dJ< |.|	dK< t ˆt+d¡d }|dk�rHt	dddtjd�t,|tddLƒ dM ƒ }nd}t ˆt+d¡ -¡ d }/|/dk�r€t	dddtjd�}/n(t	dddtjd�t,|/tddLƒ dM ƒ }/|/|dN< |dN t	dddddtjd�k�	r$t	j.tjdO�}0|0j/|dN j/ |dP< |0j0|dN j/dQ�}0|0|dN k �	r,|dP  d8  < nd|dP< t ˆt&d¡d dR }ˆ dd¡ t ˆtd¡|	d< t ˆtd¡|	dS< t ˆt&d¡|dT< t 1|dT tj2¡|dT< tj3 4|dT t 5|dT d¡d¡ 6¡ |dT< t ˆt7d¡}1t ˆt7d¡}2t ˆtd¡d }t ˆtd¡}|d dk�
rd|d< td ƒ t ˆt&d¡d }ˆ dd¡ tt|ƒƒ}‡fdUd"„|D ƒ‰ tˆ ||ƒ}t t tˆ ƒ¡|¡}ˆ d
t|ƒ d¡ ˆ dt|ƒ d¡ t ˆt&t|ƒ¡ -¡ }t |dd… ¡}3tƒ |d$< t|ƒD ]š\}}||k�
rò�
qÜ|dVk�r|d$  d&¡ nh|dWk�r&|d$  dX¡ nN|dYk�r@|d$  d¡ n4|dk�rZ|d$  d¡ ntdZ| ƒ |d$  d¡ �
qÜt |d$ t¡|d$< ‡ fd[d"„|D ƒ‰ t ˆtt|ƒ¡}t ˆtt|ƒ¡}t ˆtt|ƒ¡}t ˆtt|ƒ¡}ˆ d\t|ƒ d¡ t ˆt7t|ƒ¡}t ˆt7t|ƒ¡}t ˆt7t|ƒ¡}4t ˆt t|ƒ¡‰t ˆt t|ƒ¡}i }5‡fd]d"„|D ƒ|5d^< |d d_k �rÊt ˆtt|ƒ¡ 8t¡}6tj9|6|6d;k< td|6d ƒ|5d`< ˆ dat|ƒ d¡ n€t ˆt7dbt|ƒ ¡}7|7dddb… }7|7dd… }8|7dd… }6|3dc@ ddkgd }9d|6t :|9¡< |3dc@ dekgd }9d|8t :|9¡d < ˆ $¡ |k�s\t%‚t !‡fdfd"„t|ƒD ƒ¡}t"|ƒ\}}|j#|ˆ |||||||2|||6|||	|ˆ||4|||||1dg� |dh |di |d.   }ˆ |¡ ˆ d¡ ¡ } | djk�rZt ;| t¡ -¡ d } |d dkk �rrt ˆtd¡}!|!d }"tdt|!ƒƒD ]}|"|!| d|d    }"�q>t ˆtd¡d }#nVt ˆtd¡}:|:d }#tdt|:ƒƒD ]}|#|:| d|d    }#�q–t ˆt7d¡d }"t ˆt|#¡d }$t ˆt&|#¡}%| dk�rt ˆt&|#¡}&t ˆt|#¡}'n tj'|#tj<d0�}&tj)|#tj<d0�}'tj=|'dtj>|'d1� |#|$|%|&|'g}|"|dl< W 5 Q R X |j#|t t|dm ƒ¡dn� |S )oz%Read GDF 1.x and GDF 2.x header info.Nrv   r`   r   Útyperg   rê   Únumbergffffffþ?rç   rÝ   rè   r	   r   ré   r   rî   Zrecording_idra   é   z  Z00é   é
   r¯   rõ   rò   Úhospitalrñ   é   r¬   zRHeader information is incorrect for record length. Default record length set to 1.c                    s   g | ]}t ˆ  d ¡ƒ ¡ ‘qS ©ra   r  rÿ   rþ   r   rH   r~   k  s     z$_read_gdf_header.<locals>.<listcomp>c                    s   g | ]}t ˆ  d ¡ƒ ¡ ‘qS r  r  rÿ   rþ   r   rH   r~   o  s     rt   r  r  c                    s   g | ]}ˆ | ‘qS r   r   r  rª   r   rH   r~   z  s     c                    s   g | ]}t ˆ  d ¡ƒ‘qS r  )râ   rü   rÿ   rþ   r   rH   r~     s     r_   c                    s    g | ]\}}t | ˆ |  ‘qS r   r%  ©r|   r©   r(  ©rl   r   rH   r~   ‰  s   ÿ)Ú	bytes_totr    ro   r¦   r¥   rh   rn   r>   r¸   rq   r¹   r¶   r  r½   rl   r¢   r	  r£   r¤   rµ   r
  r¢   r8  )r   r   ru   )Úout)ÚUnknownZRightZLeftZEqual)r:  ZMaleZFemale)r:  ZNoZYesZ	CorrectedéB   ÚsmokingZalcohol_abuseZ
drug_abuseZ
medicationÚweightéÿ   Úheightrì   Ú
handednessZvisualé@   ZvertpreZhorzprerË   é   Úversioni€î6 ZlatitudeZ	longitudeéd   Zaltituder°   iàÿÿÿio  rí   )ÚtzZage)r  é   ÚipZheadsizec                    s   g | ]}t ˆ  d ¡ƒ ¡ ‘qS r5  r  rÿ   rþ   r   rH   r~     s     i³  i²  r  i   z}Unsupported physical dimension for channel %d (assuming dimensionless). Please contact the MNE-Python developers for support.c                    s   g | ]}ˆ | ‘qS r   r   r  rª   r   rH   r~   A  s     éD   c                    s   g | ]}t  ˆ td ¡d ‘qS )r   r   )r3   rb   ÚFLOAT32rÿ   rþ   r   rH   r~   S  s   ÿZxyzg…ëQ¸…@Ú	impedanceé   rï   iàÿ  i   iÀ  c                    s    g | ]\}}t | ˆ |  ‘qS r   r%  r6  r7  r   rH   r~   k  s   ÿ)r8  r    ro   rh   rn   r¦   r¥   r>   Úgndr¸   rq   rJ  r¹   r¶   r  r½   rl   r¢   Únotchr	  r£   r¤   rµ   Úrefro   r½   Ú g
×£p=
ÿ?Zevent_sfreqr    )ræ   rq   )?r  r†   rü   rß   rÍ   rà   r2   râ   rý   r   r   Úpowr   r  rÎ   r3   rb   ÚINT64r  r‰   ÚUINT32r   r1   r‡   r  r  rƒ   r‹   r�   rÚ   ÚFLOAT64rÜ   re   r  r-  r  r  rŽ   ÚUINT16rŠ   Zint32r6   ÚmaximumÚUINT64r   ÚtolistÚnowr  rä   ZasarrayZfloat32ÚmaZmasked_arrayÚequalZfilledrI  rd   rÄ   r¿   Ú
fromstringZuint32ZclipÚinf);r�   r>   rA   rB   ræ   rC  r¶   Úpidr  r  Útmr   r½   rµ   r¢   r!  rq   rt   r©   r®   r¤   r£   r¦   r¥   rÛ   r¸   r¹   r]   r8  rn   rh   ZetpZetmodeÚsrZevent_srZn_eventsÚposÚtypZchnZdurr@  ZgenderÚscaleZsadmZghiZvhsvr°   rí   ÚtodayrN  rL  Z	unitcodesrM  ZchannelrJ  ÚtmpZfZr   Úner   )r    rf   rl   rH   r›   3  sv      û


ÿ           ÷


((*ÿÿ
ÿ
ÿÿ
 þ





þ
ÿ
ÿÿÿ              öÿ


r›   úEDF AnnotationsúBDF Annotationsc           
         sr  ddg}| dks| dkr g g fS | dkr.d} n¶t | tƒrf| dkrZdˆ krTttdƒ qd|}qä|  ¡ g}n~t | tƒr€ˆ |   ¡ g}ndt | tƒrÜtdd	„ | D ƒƒr¬d
d	„ | D ƒ}qätdd	„ | D ƒƒrÒ‡ fdd	„| D ƒ}qätdƒ‚ntdƒ‚‡fdd	„|D ƒ}t	|ƒ�rd 
|¡}t|ƒ‚dd	„ ˆ D ƒ‰tt|ƒtˆƒ@ ƒ}|�sBg g fS ‡fdd	„|D ƒ}‡ fdd	„|D ƒ}	||	fS dS )z?Check that the stimulus channel exists in the current datafile.ÚstatusÚtriggerNFTr   zzUsing `stim_channel='auto'` when auto also corresponds to a channel name is ambiguous. Please use `stim_channel=['auto']`.c                 S   s   g | ]}t |tƒ‘qS r   )r�   Ústr©r|   Úsr   r   rH   r~   ¾  s     z'_check_stim_channel.<locals>.<listcomp>c                 S   s   g | ]}|  ¡ ‘qS r   ©r˜   rk  r   r   rH   r~   ¿  s     c                 S   s   g | ]}t |tƒ‘qS r   )r�   r   rk  r   r   rH   r~   À  s     c                    s   g | ]}ˆ |   ¡ ‘qS r   rm  rk  rª   r   rH   r~   Á  s     zInvalid stim_channelc                    s"   g | ]}|d d„ ˆ D ƒkr|‘qS )c                 S   s   g | ]}|  ¡ ‘qS r   rm  r'  r   r   rH   r~   É  s     z2_check_stim_channel.<locals>.<listcomp>.<listcomp>r   rÿ   )Útal_ch_namesr   rH   r~   È  s    ÿz‘The synthesis of the stim channel is not supported since 0.18. Please remove {} from `stim_channel` and use `mne.events_from_annotations` insteadc                 S   s   g | ]}|  ¡ ‘qS r   rm  rÿ   r   r   rH   r~   Ñ  s     c                    s   g | ]}ˆ   |¡‘qS r   )Úindex)r|   Úf)Úch_names_lowr   rH   r~   ×  s     c                    s   g | ]}ˆ | ‘qS r   r   r  rª   r   rH   r~   Ø  s     )r�   rj  r   ÚRuntimeWarningr˜   r   r1   rÌ   r  r2   r*   Úset)
r=   r    rn  ZDEFAULT_STIM_CH_NAMESZvalid_stim_ch_namesZtal_ch_names_foundZ_msgÚfoundrp   Únamesr   )r    rq  rn  rH   r¾   ¤  sB    




ýr¾   c                    sÎ   ˆr~ˆ rt dˆ › d�ƒ‚tˆtƒrhg }t| ƒD ]\}}t ˆ|¡r.| |¡ q.t t 	t
| ƒ¡|¡}|S ‡fdd„t| ƒD ƒS tˆ tƒr¸g }t| ƒD ]\}}t ˆ |¡r”| |¡ q”|S ‡ fdd„t| ƒD ƒS )z»Find indices of all channels to exclude.

    If there are several channels called "A" and we want to exclude "A", then
    add (the index of) all "A" channels to the exclusion list.
    z6'exclude' must be empty if 'include' is assigned. Got r–   c                    s   g | ]\}}|ˆ kr|‘qS r   r   ©r|   rD   r   )rA   r   rH   r~   ï  s      z%_find_exclude_idx.<locals>.<listcomp>c                    s   g | ]\}}|ˆ kr|‘qS r   r   rv  )r>   r   rH   r~   ø  s      )r  r�   rj  r‹   r×   Úmatchr�   r3   r  rƒ   r2   )r    r>   rA   Zindices_includerD   r   Úindicesr   )r>   rA   rH   r  Ü  s(    
ÿ

r  c                 C   s"   ddg}t  t  | |¡¡d }|S )Nrf  rg  r   )r3   r¿   Zin1d)r    Zaccepted_tal_ch_namesZtal_channel_idxr   r   rH   r  û  s    r  r   r   Fc	           
      C   sZ   t j | ¡} t j | ¡d dd…  ¡ }	|	dkr@td|	› d�ƒ‚t| ||||||||d�	S )a0  Reader function for EDF or EDF+ files.

    Parameters
    ----------
    input_fname : str
        Path to the EDF or EDF+ file.
    eog : list or tuple
        Names of channels or list of indices that should be designated EOG
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    misc : list or tuple
        Names of channels or list of indices that should be designated MISC
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    stim_channel : 'auto' | str | list of str | int | list of int
        Defaults to 'auto', which means that channels named 'status' or
        'trigger' (case insensitive) are set to STIM. If str (or list of str),
        all channels matching the name(s) are set to STIM. If int (or list of
        ints), channels corresponding to the indices are set to STIM.
    exclude : list of str | str
        Channel names to exclude. This can help when reading data with
        different sampling rates to avoid unnecessary resampling. A str is
        interpreted as a regular expression.
    infer_types : bool
        If True, try to infer channel types from channel labels. If a channel
        label starts with a known type (such as 'EEG') followed by a space and
        a name (such as 'Fp1'), the channel type will be set accordingly, and
        the channel will be renamed to the original label without the prefix.
        For unknown prefixes, the type will be 'EEG' and the name will not be
        modified. If False, do not infer types and assume all channels are of
        type 'EEG'.

        .. versionadded:: 0.24.1
    include : list of str | str
        Channel names to be included. A str is interpreted as a regular
        expression. 'exclude' must be empty if include is assigned.

        .. versionadded:: 1.1
    %(preload)s
    %(verbose)s

    Returns
    -------
    raw : instance of RawEDF
        The raw instance.

    See Also
    --------
    mne.io.read_raw_bdf : Reader function for BDF files.
    mne.io.read_raw_gdf : Reader function for GDF files.
    mne.export.export_raw : Export function for EDF files.

    Notes
    -----
    It is worth noting that in some special cases, it may be necessary to shift
    event values in order to retrieve correct event triggers. This depends on
    the triggering device used to perform the synchronization. For instance, in
    some files events need to be shifted by 8 bits:

        >>> events[:, 2] >>= 8  # doctest:+SKIP

    TAL channels called 'EDF Annotations' are parsed and extracted annotations
    are stored in raw.annotations. Use :func:`mne.events_from_annotations` to
    obtain events from these annotations.

    If channels named 'status' or 'trigger' are present, they are considered as
    STIM channels by default. Use func:`mne.find_events` to parse events
    encoded in such analog stim channels.

    The EDF specification allows optional storage of channel types in the
    prefix of the signal label for each channel. For example, ``EEG Fz``
    implies that ``Fz`` is an EEG channel and ``MISC E`` would imply ``E`` is
    a MISC channel. However, there is no standard way of specifying all
    channel types. MNE-Python will try to infer the channel type, when such a
    string exists, defaulting to EEG, when there is no prefix or the prefix is
    not recognized.

    The following prefix strings are mapped to MNE internal types:

        - 'EEG': 'eeg'
        - 'SEEG': 'seeg'
        - 'ECOG': 'ecog'
        - 'DBS': 'dbs'
        - 'EOG': 'eog'
        - 'ECG': 'ecg'
        - 'EMG': 'emg'
        - 'BIO': 'bio'
        - 'RESP': 'resp'
        - 'MISC': 'misc'
        - 'SAO2': 'bio'

    The EDF specification allows storage of subseconds in measurement date.
    However, this reader currently sets subseconds to 0 by default.
    r   Nr”   z"Only EDF files are supported, got r–   ©	r:   r;   r<   r=   r>   r?   r@   rA   r   ©r+   r,   r-   r—   r˜   rœ   r   ©
r:   r;   r<   r=   r>   r?   rA   r@   r   rž   r   r   rH   Úread_raw_edf  s    b   ýr|  c	           
      C   sZ   t j | ¡} t j | ¡d dd…  ¡ }	|	dkr@td|	› d�ƒ‚t| ||||||||d�	S )aé  Reader function for BDF files.

    Parameters
    ----------
    input_fname : str
        Path to the BDF file.
    eog : list or tuple
        Names of channels or list of indices that should be designated EOG
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    misc : list or tuple
        Names of channels or list of indices that should be designated MISC
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    stim_channel : 'auto' | str | list of str | int | list of int
        Defaults to 'auto', which means that channels named 'status' or
        'trigger' (case insensitive) are set to STIM. If str (or list of str),
        all channels matching the name(s) are set to STIM. If int (or list of
        ints), channels corresponding to the indices are set to STIM.
    exclude : list of str | str
        Channel names to exclude. This can help when reading data with
        different sampling rates to avoid unnecessary resampling. A str is
        interpreted as a regular expression.
    infer_types : bool
        If True, try to infer channel types from channel labels. If a channel
        label starts with a known type (such as 'EEG') followed by a space and
        a name (such as 'Fp1'), the channel type will be set accordingly, and
        the channel will be renamed to the original label without the prefix.
        For unknown prefixes, the type will be 'EEG' and the name will not be
        modified. If False, do not infer types and assume all channels are of
        type 'EEG'.

        .. versionadded:: 0.24.1
    include : list of str | str
        Channel names to be included. A str is interpreted as a regular
        expression. 'exclude' must be empty if include is assigned.

        .. versionadded:: 1.1
    %(preload)s
    %(verbose)s

    Returns
    -------
    raw : instance of RawEDF
        The raw instance.

    See Also
    --------
    mne.io.read_raw_edf : Reader function for EDF and EDF+ files.
    mne.io.read_raw_gdf : Reader function for GDF files.

    Notes
    -----
    Biosemi devices trigger codes are encoded in 16-bit format, whereas system
    codes (CMS in/out-of range, battery low, etc.) are coded in bits 16-23 of
    the status channel (see http://www.biosemi.com/faq/trigger_signals.htm).
    To retrieve correct event values (bits 1-16), one could do:

        >>> events = mne.find_events(...)  # doctest:+SKIP
        >>> events[:, 2] &= (2**16 - 1)  # doctest:+SKIP

    The above operation can be carried out directly in :func:`mne.find_events`
    using the ``mask`` and ``mask_type`` parameters (see
    :func:`mne.find_events` for more details).

    It is also possible to retrieve system codes, but no particular effort has
    been made to decode these in MNE. In case it is necessary, for instance to
    check the CMS bit, the following operation can be carried out:

        >>> cms_bit = 20  # doctest:+SKIP
        >>> cms_high = (events[:, 2] & (1 << cms_bit)) != 0  # doctest:+SKIP

    It is worth noting that in some special cases, it may be necessary to shift
    event values in order to retrieve correct event triggers. This depends on
    the triggering device used to perform the synchronization. For instance, in
    some files events need to be shifted by 8 bits:

        >>> events[:, 2] >>= 8  # doctest:+SKIP

    TAL channels called 'BDF Annotations' are parsed and extracted annotations
    are stored in raw.annotations. Use :func:`mne.events_from_annotations` to
    obtain events from these annotations.

    If channels named 'status' or 'trigger' are present, they are considered as
    STIM channels by default. Use func:`mne.find_events` to parse events
    encoded in such analog stim channels.
    r   Nr\   ú"Only BDF files are supported, got r–   ry  rz  r{  r   r   rH   Úread_raw_bdfn  s    [   ýr~  c           	   
   C   sX   t j | ¡} t j | ¡d dd…  ¡ }|dkr@td|› d�ƒ‚t| |||||||d�S )aÚ  Reader function for GDF files.

    Parameters
    ----------
    input_fname : str
        Path to the GDF file.
    eog : list or tuple
        Names of channels or list of indices that should be designated EOG
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    misc : list or tuple
        Names of channels or list of indices that should be designated MISC
        channels. Values should correspond to the electrodes in the file.
        Default is None.
    stim_channel : 'auto' | str | list of str | int | list of int
        Defaults to 'auto', which means that channels named 'status' or
        'trigger' (case insensitive) are set to STIM. If str (or list of str),
        all channels matching the name(s) are set to STIM. If int (or list of
        ints), channels corresponding to the indices are set to STIM.
    exclude : list of str | str
        Channel names to exclude. This can help when reading data with
        different sampling rates to avoid unnecessary resampling. A str is
        interpreted as a regular expression.
    include : list of str | str
        Channel names to be included. A str is interpreted as a regular
        expression. 'exclude' must be empty if include is assigned.
    %(preload)s
    %(verbose)s

    Returns
    -------
    raw : instance of RawGDF
        The raw instance.

    See Also
    --------
    mne.io.read_raw_edf : Reader function for EDF and EDF+ files.
    mne.io.read_raw_bdf : Reader function for BDF files.

    Notes
    -----
    If channels named 'status' or 'trigger' are present, they are considered as
    STIM channels by default. Use func:`mne.find_events` to parse events
    encoded in such analog stim channels.
    r   Nr•   r}  r–   )r:   r;   r<   r=   r>   r@   rA   r   )r+   r,   r-   r—   r˜   rœ   rY   )	r:   r;   r<   r=   r>   rA   r@   r   rž   r   r   rH   Úread_raw_gdfÓ  s    0   þr  c              	   C   sˆ  d}t | tƒr8t| dd��}t || ¡ ¡}W 5 Q R X n¨tƒ }t | ¡} | D ]€}| 	¡ }|j
tkrž|jtd�}| dd¡}|dd…dd…f  	¡ }| |¡ qL| tj¡}| t |d	 |d	 g¡ d
¡¡ qLt || d¡¡}g }d}t|ƒD ]x\}	}
t|
d ƒ| }|
d �rt|
d ƒnd}|
d  d¡dd… D ].}|�rT| |||g¡ n|	dk�r8| }�q8qð|�rxt|Ž S tƒ tƒ tƒ fS )aÏ  Annotation File Reader.

    Parameters
    ----------
    annotations : ndarray (n_chans, n_samples) | str
        Channel data in EDF+ TAL format or path to annotation file.

    Returns
    -------
    onset : array of float, shape (n_annotations,)
        The starting time of annotations in seconds after ``orig_time``.
    duration : array of float, shape (n_annotations,)
        Durations of the annotations in seconds.
    description : array of str, shape (n_annotations,)
        Array of strings containing description for each annotation. If a
        string, all the annotations are given the same description. To reject
        epochs, use description starting with keyword 'bad'. See example above.
    z&([+-]\d+\.?\d*)((\d+\.?\d*))?(.*?) rÝ   )Úencodingru   r_   rê   Nr   rF  ÚFÚutf8r»   r   r	   úr   )r�   rj  r†   r×   rØ   rü   Ú	bytearrayr3   Z
atleast_2dr{   r]   re   Úviewr  rc   Úextendrd   r„   Zuint8Úflattenrß   r‹   rÍ   rà   r�   rÉ   r1   )ÚannotationsÚpatZ
annot_fileÚtriggersZtalsZchanZ	this_chanræ   ÚoffsetÚkZevr&   r'   r(   r   r   rH   r7     s:    


ÿ
	r7   c                 C   s"   dd„ t tt| ƒƒdd�D ƒ}|S )Nc                 S   s   i | ]\}}||“qS r   r   )r|   ÚnÚar   r   rH   Ú
<dictcomp>O  s      z-_get_edf_default_event_id.<locals>.<dictcomp>r   )rO   )r‹   Úsortedrs  )ZdescriptionsÚmappingr   r   rH   Ú_get_edf_default_event_idN  s    r’  c                 C   sf   t ƒ t ƒ t ƒ   }}}|  dd ¡}|d k	r\|d jd dkr\|d | }|d | }|d }|||fS )Nræ   r   r   rê   r	   )r1   r   rÊ   )rB   rZ   r&   r'   rC   ræ   r   r   rH   r[   S  s    r[   )N)N)N)N)N)N)NNr   r   FNFN)NNr   r   FNFN)NNr   r   NFN)GrW   r   r   r   r+   r×   Únumpyr3   Úutilsr   r   r   r
   r   Úbaser   Z	meas_infor   r   Ú	constantsr   Úfilterr   r   rˆ  r   rÂ   ZFIFFV_SEEG_CHZFIFFV_ECOG_CHZFIFFV_DBS_CHrÆ   ZFIFFV_ECG_CHZFIFFV_EMG_CHZFIFFV_BIO_CHZFIFFV_RESP_CHrÇ   rÅ   r   rY   rj   r5   rŸ   r.   rÜ   râ   rå   rš   ZINT8r  r  rT  re   rR  rQ  rV  rI  rS  r)  Útupler&  r-  r›   r¾   r  r  r|  r~  r  r7   r’  r[   r   r   r   rH   Ú<module>   sÆ   õ{M
]
' ÿ
 >
 9         þÿ

  tÿ
8
         þk         þd        ÿ8B