U
    f»{j-  ã                   @   s²   d dl Z d dlZd dlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ dd	lmZ d
dl
mZmZmZmZmZ dd„ Zdd„ Zeddd„ƒZG dd„ de	ƒZdS )é    Né   )Ú_read_raw_egi_mff)Ú_combine_triggersé   )ÚBaseRaw)Ú_read_segments_fileÚ_create_chs)Ú_empty_info)ÚFIFFé   )ÚverboseÚloggerÚwarnÚ_validate_typeÚ_check_fnamec                 C   sÂ  t  | dd¡d }|dt  |d¡ @ kr:| ¡  t j¡}ntdƒ‚dd„ }t||| ddƒ|| ddƒ|| ddƒ|| ddƒ|| ddƒ|| ddƒ|| d	dƒ|| ddƒ|| ddƒ|| ddƒ|| ddƒ|| ddƒd
�}t  |d¡dkràdnd}t  |d¡}|dk�rtdƒ‚|�r€| 	tddt  | d	d¡d t  | dd¡d g g g dd�¡ t
|d ƒD ].}d t  | dd¡ d¡¡}|d  |¡ �qNntdƒ‚||d< ddddœ| \|d< |d< t  |d ¡|d< |S )zRead EGI binary header.z<i4r   r   é   z<Watchout. This does not seem to be a simple binary EGI file.c                  _   s   t j| |Žd S )Nr   )ÚnpÚfromfile)ÚxÚy© r   úM/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/egi/egi.pyÚmy_fread   s    z_read_header.<locals>.my_freadú>i2z>i4)ÚversionÚyearÚmonthÚdayÚhourÚminuteÚsecondZmillisecondÚ	samp_rateÚ
n_channelsZgainÚbitsÚvalue_rangez&Floating point precision is undefined.)Zn_categoriesÚ
n_segmentsÚ	n_samplesÚn_eventsÚevent_codesZcategory_namesZcategory_lengthsZpre_baseliner'   Ú ZS1é   ZU1r(   z#Only continuous files are supportedÚunsegmented)r   Úshort)z>f4Úfloat)z>f8Údouble)r   r*   r   ÚdtypeÚorig_format)r   r   Zbitwise_andÚbyteswapZastypeZuint32Ú
ValueErrorÚdictÚRuntimeErrorÚupdateÚrangeÚjoinÚappendÚNotImplementedErrorr/   )Úfidr   r   Úinfor+   Ú	precisionÚeventr(   r   r   r   Ú_read_header   s^    











ó
ùþþr>   c                 C   sˆ   t  |d |d |d  g¡}|  d|d d  d¡ t|d ƒD ]@}|  |d |d j d	¡ t  | |d |d ¡|d
d
…|f< qB|S )zRead events.r'   r%   r&   é$   r*   r   r"   r/   r   N)r   ZzerosÚseekr6   Úitemsizer   )r:   r;   ÚeventsÚsir   r   r   Ú_read_eventsL   s    
ÿ$rD   FúE%dc              	   C   sP   t | ddƒ t| ƒ} |  d¡ d¡r:t| |||||||ƒS t| |||||||ƒS )aÐ  Read EGI simple binary as raw object.

    .. note:: This function attempts to create a synthetic trigger channel.
              See the Notes section below.

    Parameters
    ----------
    input_fname : path-like
        Path to the raw file. Files with an extension .mff are automatically
        considered to be EGI's native MFF format files.
    eog : list or tuple
        Names of channels or list of indices that should be designated
        EOG channels. Default is None.
    misc : list or tuple
        Names of channels or list of indices that should be designated
        MISC channels. Default is None.
    include : None | list
       The event channels to be ignored when creating the synthetic
       trigger. Defaults to None.
       Note. Overrides ``exclude`` parameter.
    exclude : None | list
       The event channels to be ignored when creating the synthetic
       trigger. Defaults to None. If None, channels that have more than
       one event and the ``sync`` and ``TREV`` channels will be
       ignored.
    %(preload)s

        .. versionadded:: 0.11
    channel_naming : str
        Channel naming convention for the data channels. Defaults to 'E%%d'
        (resulting in channel names 'E1', 'E2', 'E3'...). The effective default
        prior to 0.14.0 was 'EEG %%03d'.

         .. versionadded:: 0.14.0
    %(verbose)s

    Returns
    -------
    raw : instance of RawEGI
        A Raw object containing EGI data.

    See Also
    --------
    mne.io.Raw : Documentation of attribute and methods.

    Notes
    -----
    The trigger channel names are based on the arbitrary user dependent event
    codes used. However this function will attempt to generate a **synthetic
    trigger channel** named ``STI 014`` in accordance with the general
    Neuromag / MNE naming pattern.

    The event_id assignment equals ``np.arange(n_events) + 1``. The resulting
    ``event_id`` mapping is stored as attribute to the resulting raw object but
    will be ignored when saving to a fiff. Note. The trigger channel is
    artificially constructed based on timestamps received by the Netstation.
    As a consequence, triggers have only short durations.

    This step will fail if events are not mutually exclusive.
    z	path-likeÚinput_fnamez/\z.mff)r   ÚstrÚrstripÚendswithr   ÚRawEGI)rF   ÚeogÚmiscÚincludeÚexcludeÚpreloadÚchannel_namingr   r   r   r   Úread_raw_egiY   s    @
   ÿ ÿrQ   c                       s.   e Zd ZdZed	‡ fdd„	ƒZdd„ Z‡  ZS )
rJ   z'Raw object from EGI simple binary file.NFrE   c	               	      s  t |dddƒ}|d krg }|d kr&g }t|dƒ�b}	t d| ¡ t|	ƒ}
t d¡ t|	|
ƒ}|
d dkrŠ|
d	 dkrŠ|
d d
|
d	   }nd}W 5 Q R X t d¡ g ‰|
d dk�r–t|
d ƒ‰ˆd k�r¤|d krÚddgn|‰‡fdd„tˆƒD ƒ‰g }|d k�r8t|ƒD ],\}}| ¡ dk�r
ˆ| �r
| 	|¡ �q
t
ˆƒt
|ƒ t
ˆƒk�r^tdtƒ n
ˆ |¡ ˆ ¡  ‡fdd„t |
d ¡D ƒ‰‡fdd„tˆƒD ƒ‰n‡fdd„tˆƒD ƒ‰ˆ‰dˆfd|ffD ]P\}}t|tƒ�r|D ]}|ˆk�rætd| ƒ‚�qæn|d k	�rÎtd| ƒ‚�qÎt t
ˆƒ¡d }t d¡ t dd ‡fdd„tˆƒD ƒ¡ ¡ t|ˆ |d �|
d!< tt‡fd"d„ˆD ƒ|ƒƒ| _nd | _d |
d!< t|
d# ƒ}t |
d$ |
d% |
d& |
d' |
d( |
d) ¡}t | ¡ ¡}|df|d*< ‡ fd+d„t|
d, ƒD ƒ}| t|
d ƒ¡ |
d! d k	�r:| 	d-¡ t
|ƒ}t ||¡}tj }tj!}t"|||||d.d.|ƒ}‡fd/d„t|ƒD ƒ}|D ](}||  #tj$d0tj%tj&tj'd1œ¡ �qŠ||d2< d3|_(| )¡  |
d4 d5k�rà|
d4 nd6}t*t+| ƒj,||||g|
d7 d g|
g|d8� d S )9NÚreadTrF   ÚrbzReading EGI header from %s...z    Reading events ...r$   r   r#   g       @g�íµ ÷Æ°>z#    Assembling measurement info ...r'   r(   ÚsyncZTREVc                    s   g | ]\}}|ˆ kr|‘qS r   r   ©Ú.0ÚiÚk)Úexclude_listr   r   Ú
<listcomp>¿   s
     ÿ z#RawEGI.__init__.<locals>.<listcomp>r   z5Did not find any event code with more than one event.c                    s   g | ]}|ˆ kr|‘qS r   r   ©rV   rW   )Úexclude_indsr   r   rZ   Í   s    ÿc                    s   g | ]\}}|ˆ kr|‘qS r   r   rU   ©Úinclude_r   r   rZ   Ï   s    ÿc                    s   g | ]\}}|ˆ kr|‘qS r   r   rU   )rM   r   r   rZ   Ò   s    ÿrM   rN   zCould find event named "%s"z!`%s` must be None or of type listz.    Synthesizing trigger channel "STI 014" ...z    Excluding events {%s} ...z, c                    s   g | ]\}}|ˆ kr|‘qS r   r   rU   r]   r   r   rZ   á   s    ÿ)Z	remappingÚnew_triggerc                    s   g | ]}|ˆ kr|‘qS r   r   )rV   Úe)Úinclude_namesr   r   rZ   å   s
     ÿ r!   r   r   r   r   r   r    Z	meas_datec                    s   g | ]}ˆ |d   ‘qS )r   r   r[   )rP   r   r   rZ   ñ   s     r"   zSTI 014r   c                    s&   g | ]\}}|  d ¡s|ˆ kr|‘qS )ZSTI)Ú
startswith)rV   rW   Úname)r(   r   r   rZ   û   s    
 ÿg      ð?)Zunit_mulÚcalÚkindZ	coil_typeÚunitÚchsFr0   r-   Zsingler&   )r0   Ú	filenamesZ
last_sampsZ
raw_extrasr   )-r   Úopenr   r;   r>   rD   ÚlistÚ	enumerateÚsumr8   Úlenr   ÚRuntimeWarningÚextendÚsortr   ZarangeÚ
isinstancer2   r7   r   r3   ÚzipZevent_idr	   ÚdatetimeÚtimeÚmktimeÚ	timetupler6   Úrepeatr
   ZFIFFV_COIL_EEGZFIFFV_EEG_CHr   r5   ZFIFF_UNITM_NONEZFIFFV_STIM_CHZFIFFV_COIL_NONEZFIFF_UNIT_NONEZ	_unlockedZ_update_redundantÚsuperrJ   Ú__init__) ÚselfrF   rK   rL   rM   rN   rO   rP   r   r:   Úegi_infoZ
egi_eventsrd   Zmore_excludesÚiir=   ÚkkÚvrX   Z	event_idsr;   Zmy_timeZmy_timestampZch_namesZnchanÚcalsZch_coilZch_kindrg   Z
sti_ch_idxÚidxr0   ©Ú	__class__)rP   r(   r\   rY   rM   r^   ra   r   ry   ¥   sÊ    




ÿ



ÿ ÿ
ÿ
    þ

ÿ
ý
ÿÿ
    ýzRawEGI.__init__c                 C   s^   | j | }|d }	|d |d  }
d|d d  }|d }t| ||||||||	|
||d� dS )	z#Read a segment of data from a file.r/   r"   r'   r?   r*   r_   )r/   r"   ÚoffsetÚ
trigger_chN)Z_raw_extrasr   )rz   Údatar€   ÚfiÚstartÚstopr   Zmultr{   r/   Zn_chan_readrƒ   r„   r   r   r   Ú_read_segment_file  s    
  þzRawEGI._read_segment_file)NNNNFrE   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ry   r‰   Ú__classcell__r   r   r�   r   rJ   ¢   s            þfrJ   )NNNNFrE   N)rs   rt   Únumpyr   Zegimffr   rB   r   Úbaser   Úutilsr   r   Z	meas_infor	   Ú	constantsr
   r   r   r   r   r   r>   rD   rQ   rJ   r   r   r   r   Ú<module>   s(   8         þH