U
    f»{j®"  ã                   @   sŠ   d dl mZ d dlmZ d dlZddlmZmZm	Z	 ddl
mZ ddlmZ ee	dd	d
„ƒƒZe	dd„ ƒZe	dd„ ƒZdd„ Zdd„ ZdS )é    )ÚOrderedDict)ÚPathNé   )ÚloggerÚfill_docÚverboseé   )Úcreate_info)ÚEvokedArrayc                 C   s<   t | ƒ} | jdkrt| |ƒS | jdkr0t| |ƒS tdƒ‚dS )a[  Reader function for BESA .avr or .mul files.

    When a .elp sidecar file is present, it will be used to determine electrode
    information.

    Parameters
    ----------
    fname : str | Path
        Path to the .avr or .mul file.
    %(verbose)s

    Returns
    -------
    ev : Evoked
        The evoked data in the .avr or .mul file.
    z.avrz.mulz(Filename must end in either .avr or .mulN)r   ÚsuffixÚ_read_evoked_besa_avrÚ_read_evoked_besa_mulÚ
ValueError)Úfnamer   © r   úO/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/besa/besa.pyÚread_evoked_besa
   s    



r   c           	   	      sŽ  t | ƒ�4}| ¡  ¡ }d|k}|r4| ¡  ¡  ¡ }nd}W 5 Q R X t|ƒ}tj| |rXdnddd�}t| ƒ‰ |ržt|ƒt|ƒkrœt	dt|ƒ› dt|ƒ› d�ƒ‚ndˆ dk	rât
ˆ  ¡ ƒ}t|ƒt|ƒkràt	d	t|ƒ› d
t|ƒ› d�ƒ‚n t d¡ dd„ tt|ƒƒD ƒ}ˆ dk�r&t d¡ dgt|ƒ ‰ n‡ fdd„|D ƒ‰ d|k�r„t|d ƒ|d< |d |jd k�r„t d|j› d|d › d�¡ d|k�rÐt|d ƒ|d< |d |jd k�rÐt d|j› d|d › d�¡ d|k�rìt|d ƒ|d< nt	dƒ‚d|k�rt|d ƒ|d< nd|d< d|k�r4t|d ƒ|d< nd|d< d|k�rNd|d< t|d|d  dd�}t||d  d  ||d d |d |d!�S )"z)Create EvokedArray from a BESA .avr file.zNchan=Nr   é   ©ZskiprowsZndminzGMismatch between the number of channel names defined in the .avr file (z-) and the number of rows in the data matrix (ú).zBMismatch between the number of channels defined in the .avr file (z) and .elp file (zaNo .elp file found and no channel names present in the .avr file. Falling back to generic names. c                 S   s   g | ]}d |d d›�‘qS )ZCHr   Ú02dr   )Ú.0Úir   r   r   Ú
<listcomp>K   s     z)_read_evoked_besa_avr.<locals>.<listcomp>úMarking all channels as EEG.Úeegc                    s   g | ]}ˆ | ‘qS r   r   ©r   Úch©Úch_typesr   r   r   R   s     ZNptsúThe size of the data matrix (z#) does not match the "Npts" field (ZNchanr   z$) does not match the "Nchan" field (ZDIz>No "DI" field present. Could not determine sampling frequency.ZTSBZSBg      ð?ÚSegmentNameÚ éè  ©Zsfreqr   ç    €„.A©ZtminÚcommentr   )ÚopenÚreadlineÚstripÚsplitÚ_parse_headerÚnpÚloadtxtÚ_read_elp_sidecarÚlenÚRuntimeErrorÚlistÚkeysr   ÚinfoÚrangeÚintÚshapeÚwarnÚfloatr	   r
   )	r   r   ÚfÚheaderÚ	new_styleÚch_namesÚfieldsÚdatar4   r   r   r   r   &   sb    
ÿ 









 þr   c              	      s
  t | ƒ�"}| ¡  ¡ }| ¡  ¡  ¡ }W 5 Q R X t|ƒ}tj| ddd�}t|ƒ|jd krzt	dt|ƒ› d|jd › d�ƒ‚t
| ƒ‰ ˆ dkr¤t d¡ d	gt|ƒ ‰ n‡ fd
d„|D ƒ‰ d|krþt|d ƒ|d< |d |jd krþt d|j› d|d › d�¡ d|k�rJt|d ƒ|d< |d |jd k�rJt d|j› d|d › d�¡ d|k�rft|d ƒ|d< nt	dƒ‚d|k�rŠt|d ƒ|d< nd|d< d|k�r®t|d ƒ|d< nd|d< d|k�rÈd|d< t|d|d  ˆ d�}t|j|d  d ||d d |d |d�S )z)Create EvokedArray from a BESA .mul file.r   r   r   zGMismatch between the number of channel names defined in the .mul file (z0) and the number of columns in the data matrix (r   Nr   r   c                    s   g | ]}ˆ | ‘qS r   r   r   r   r   r   r   �   s     z)_read_evoked_besa_mul.<locals>.<listcomp>Z
TimePointsr   r    z)) does not match the "TimePoints" field (ZChannelsz') does not match the "Channels" field (zSamplingInterval[ms]zPNo "SamplingInterval[ms]" field present. Could not determine sampling frequency.zBeginSweep[ms]g        zBins/uVr!   r"   r#   r$   r%   r&   )r(   r)   r*   r+   r,   r-   r.   r0   r7   r1   r/   r   r4   r6   r8   r9   r	   r
   ÚT)r   r   r:   r;   r=   r>   r?   r4   r   r   r   r   w   sR    
 
ÿ




ÿ
 þr   c                 C   s8   |   ¡ }t|ddd… |ddd… ƒ}tdd„ |D ƒƒS )a’  Parse an .avr or .mul header string into name/val pairs.

    The header line looks like:
        Npts= 256   TSB= 0.000 DI= 4.000000 SB= 1.000 SC= 200.0 Nchan= 27
    No consistent use of separation chars, so parsing this is a bit iffy.

    Parameters
    ----------
    header : str
        The first line of the file.

    Returns
    -------
    fields : dict
        The parsed header fields
    Nr   r   c                 s   s"   | ]\}}|  d d¡|fV  qdS )ú=r"   N)Úreplace)r   ÚnameÚvalr   r   r   Ú	<genexpr>Ç   s     z _parse_header.<locals>.<genexpr>)r+   ÚzipÚdict)r;   ÚpartsZname_val_pairsr   r   r   r,   ´   s    r,   c              	   C   sÐ   | j | jd  }| ¡ s.t d|› d�¡ dS t d|› �¡ tƒ }t|ƒ�z}| ¡ }t|d  	¡ ƒdkr–|D ]$}| 	¡ dd… \}}| 
¡ ||< qnn,t d	¡ |D ]}| 	¡ dd
… }d||< q¤W 5 Q R X |S )aD  Read a possible .elp sidecar file with electrode information.

    The reason we don't use the read_custom_montage for this is that we are
    interested in the channel types, which a DigMontage object does not provide
    us.

    Parameters
    ----------
    fname : Path
        The path of the .avr or .mul file. The corresponding .elp file will be
        derived from this path.

    Returns
    -------
    ch_type : OrderedDict | None
        If the sidecar file exists, return a dictionary mapping channel names
        to channel types. Otherwise returns ``None``.
    z.elpzNo z/ file present containing electrode information.Nz'Reading electrode names and types from r   r   r   zCNo channel types present in .elp file. Marking all channels as EEG.r   r   )ÚparentÚstemÚexistsr   r4   r   r(   Ú	readlinesr0   r+   Úlower)r   Z	fname_elpr   r:   ÚlinesÚlineZch_typeZch_namer   r   r   r/   Ê   s"    

r/   )N)Úcollectionsr   Úpathlibr   Únumpyr-   Úutilsr   r   r   Z	meas_infor	   Zevokedr
   r   r   r   r,   r/   r   r   r   r   Ú<module>   s   
P
<