U
    f»{j2N  ã                   @   s
  d dl Z d dlZd dlmZ d dlZddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZ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 m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' e$G dd„ deƒƒZ(dd„ Z)dd„ Z*e$ddd„ƒZ+dS )é    Né   )ÚFIFF)Ú	fiff_openÚ_fiff_get_fidÚ_get_next_fname)Úread_meas_info)Údir_tree_find)Úread_tagÚread_tag_info)ÚBaseRawÚ	_RawShellÚ_check_raw_compatibilityÚ_check_maxshield)Ú_mult_cal_oneé   )ÚAnnotationsÚ_read_annotations_fif)ÚAcqParserFIF)Úcheck_fnameÚloggerÚverboseÚwarnÚfill_docÚ
_file_likeÚ_on_missingÚ_check_fnamec                       s\   e Zd ZdZed‡ fdd„	ƒZeddd	„ƒZed
d„ ƒZdd„ Z	dd„ Z
edd„ ƒZ‡  ZS )ÚRawa$  Raw data in FIF format.

    Parameters
    ----------
    fname : str | file-like
        The raw filename to load. For files that have automatically been split,
        the split part will be automatically loaded. Filenames not ending with
        ``raw.fif``, ``raw_sss.fif``, ``raw_tsss.fif``, ``_meg.fif``,
        ``_eeg.fif``,  or ``_ieeg.fif`` (with or without an optional additional
        ``.gz`` extension) will generate a warning. If a file-like object is
        provided, preloading must be used.

        .. versionchanged:: 0.18
           Support for file-like objects.
    allow_maxshield : bool | str (default False)
        If True, allow loading of data that has been recorded with internal
        active compensation (MaxShield). Data recorded with MaxShield should
        generally not be loaded directly, but should first be processed using
        SSS/tSSS to remove the compensation signals that may also affect brain
        activity. Can also be "yes" to load without eliciting a warning.
    %(preload)s
    %(on_split_missing)s
    %(verbose)s

    Attributes
    ----------
    %(info_not_none)s
    ch_names : list of string
        List of channels' names.
    n_times : int
        Total number of time points in the raw file.
    times :  ndarray
        Time vector in seconds. Starts from 0, independently of `first_samp`
        value. Time interval between consecutive time samples is equal to the
        inverse of the sampling frequency.
    preload : bool
        Indicates whether raw data are in memory.
    %(verbose)s
    FÚraiseNc                    s°  g }t |ƒ }|}|d k	rn|  ||||¡\}	}}
d}| |	¡ |d k	rt |¡sd|› d�}t||dd� qnqt |ƒrzd }t|ƒ tt| ƒj	t
 |d j¡ddd„ |D ƒd	d„ |D ƒd
d„ |D ƒdd„ |D ƒ|d jd |
|d�
 | j|d jdd� | jD ]†}dd„ |d D ƒ}|d d d… | }|d dd … | d }|| d | jd  }t|| jd  |d| jd d�}|  j|7  _qú|�r”|  |¡ nd| _dd„ | jD ƒ| _d S )NFz&Split raw file detected but next file zí does not exist. Ensure all files were transferred properly and that split and original files were not manually renamed on disk (split files should be renamed by loading and re-saving with MNE-Python to preserve proper filename linkage).Úon_split_missing)Únamer   c                 S   s   g | ]
}|j ‘qS © )Ú
first_samp©Ú.0Úrr    r    úN/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/fiff/raw.pyÚ
<listcomp>g   s     z Raw.__init__.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r    )Ú	last_sampr"   r    r    r%   r&   g   s     c                 S   s   g | ]
}|j ‘qS r    )Úfilenamer"   r    r    r%   r&   h   s     c                 S   s   g | ]
}|j ‘qS r    )Ú_raw_extrasr"   r    r    r%   r&   h   s     )Úbuffer_size_secr   )Zemit_warningc                 S   s   g | ]}|d k‘qS )Nr    )r#   Úentr    r    r%   r&   q   s     r+   Úboundséÿÿÿÿé   g      ð?ÚsfreqZBAD_ACQ_SKIPÚ	meas_date)ÚonsetÚdurationÚdescriptionZ	orig_timec                 S   s   g | ]}t |ƒ‘qS r    )Ú_get_fname_rep)r#   Úfnamer    r    r%   r&   ‚   s     )r   Ú_read_raw_fileÚappendÚopÚexistsr   r   Úsuperr   Ú__init__ÚcopyÚdeepcopyÚinfoÚorig_formatÚset_annotationsÚannotationsr)   r   Z_annotationsZ_preload_dataÚpreloadÚ
_filenames)Úselfr5   Úallow_maxshieldrB   r   r   ZrawsÚdo_check_extÚ
next_fnameÚrawr*   ÚmsgÚextraÚmaskÚstartÚstopr2   Zannot©Ú	__class__r    r%   r;   I   s`    
 ÿÿ


ÿ
     û
ýzRaw.__init__Tc           $         sR  t  d| ¡ t|ƒsv|r@d}|tdd„ |D ƒƒ7 }t|d|ƒ t|dddƒ}tj |¡d	  	¡ }d
|krn|nd}~n|s‚t
dƒ‚d}t|ƒ}	t||d�\}
}}|
��}t||dd�\}}t||ƒ}t|tjƒ}t|ƒdk�rBt|tjƒ}t|ƒdk�rBt|tjƒ}t|ƒdk�rt
d|	 ƒ‚t|ƒ | ¡ � d|d< W 5 Q R X ~t|ƒd	k�rZ|d }|d }|d }t|d ƒ}d}d}d}|| jtjk�rÀt||| jƒ}t|jƒ}|d	7 }t||ƒ || jtjk�rþt||| jƒ}t|jƒ}|d	7 }t||ƒ tƒ }||_ ||_!|d dk�r>|dk	�r>| j"||d  8  _"| #|¡ t$ƒ ‰ d}d}t%||ƒD �]>}|| }|jtjk�r–t||jƒ}t|jƒ}�q`|jtj&k�r`|j'tj(k�rÂ|j)d|  }nÂ|j'tj*k�rà|j)d|  }n¤|j'tj+k�rþ|j)d|  }n†|j'tj,k�r|j)d|  }nh|j'tj-k�r:|j)d|  }nJ|j'tj.k�rX|j)d|  }n,|j'tj/k�rv|j)d|  }nt
d|j' ƒ‚|dk�r|j'tj(k�r¢d}nv|j'tj*k�r¶d}nb|j'tj+k�rÊd}nN|j'tj,k�rÞd}n:|j'tj-k�ròd }n&|j'tj.k�rd}n|j'tj/k�rd}|dk�r8||| 7 }||_!d}|dk�rxˆ  0t1d||| |||  d	 d!�¡ ||| 7 }d}ˆ  0t1|||| d	 |d"�¡ ||7 }�q`t2||	|ƒ}W 5 Q R X ‡ fd#d$„ˆ d D ƒ‰ ˆ D ]$} | d%k�rÒt3 4ˆ |  t¡ˆ | < �qÒt3 5ˆ d& dd'… ˆ d( d	d… d	 ¡�s*t6d)ƒ‚t3 7t3 8ˆ d( dd	… ˆ d* g¡¡}!|!ˆ d+< tˆ d+ ƒtˆ d% ƒd	 k�sxt9‚t3 :ˆ d* ¡|d  }"ˆ d(= ˆ d&= ˆ d*= |d	 |_;||_<t3 =|d ¡}#t%|d ƒD ]*}|d, | d- |d, | d.  |#|< �qÊ|#|_>ˆ |_?t  d/|j!|j;t@|j!ƒ|d  t@|j;ƒ|d  f ¡ ||_t  d0¡ |||"fS )1z+Read in header information from a raw file.zOpening raw data file %s...)zraw.fifzraw_sss.fifzraw_tsss.fifz_meg.fifz_eeg.fifz	_ieeg.fifc                 S   s   g | ]}|› d �‘qS )ú.gzr    )r#   Úer    r    r%   r&   �   s     z&Raw._read_raw_file.<locals>.<listcomp>rH   ÚreadTr5   r.   rP   Fz+preload must be used with file-like objects)rB   )Z
clean_badsr   zNo raw data in %sZ	maxshieldÚ	directoryÚnentÚnchanr0   Nr/   r   é   é   é   z%Cannot handle data buffers of type %dÚshortZsingleÚdoubleÚint)r+   ÚfirstÚnsampÚlast)r+   r\   r^   r]   c                    s    i | ]‰ ˆ ‡ fd d„ˆD ƒ“qS )c                    s   g | ]}|ˆ  ‘qS r    r    r"   ©Úkeyr    r%   r&     s     z1Raw._read_raw_file.<locals>.<dictcomp>.<listcomp>r    )r#   ©Z
raw_extrasr_   r%   Ú
<dictcomp>  s   ÿ z&Raw._read_raw_file.<locals>.<dictcomp>r+   r^   r-   r\   zFIF file appears to be brokenr]   r,   ZchsÚrangeÚcalz-    Range : %d ... %d =  %9.3f ... %9.3f secszReady.)Ar   r>   r   Útupler   r   ÚosÚpathÚsplitextÚlowerÚ
ValueErrorr4   r   r   r   r   r   ZFIFFB_RAW_DATAÚlenZFIFFB_CONTINUOUS_DATAZFIFFB_IAS_RAW_DATAr   Z_unlockr[   ÚkindZFIFF_FIRST_SAMPLEr	   ÚposÚdataÚ_check_entryZFIFF_DATA_SKIPr   r(   r!   r1   r@   Úlistrc   ZFIFF_DATA_BUFFERÚtypeZFIFFT_DAU_PACK16ÚsizeZFIFFT_SHORTZFIFFT_FLOATZFIFFT_DOUBLEZ	FIFFT_INTÚFIFFT_COMPLEX_FLOATÚFIFFT_COMPLEX_DOUBLEr7   Údictr   ÚnpÚarrayZarray_equalÚRuntimeErrorZcumsumZconcatenateÚAssertionErrorZmedianr'   r?   ZzerosZ_calsr)   Úfloat)$rD   r5   rE   rB   rF   r   ZendingsÚextZ
whole_fileZ	fname_repÚffÚtreeÚ_Úfidr>   ZmeasrA   Zraw_noderS   rT   rU   r\   r!   Z
first_skipÚtagrH   Znskipr?   Úkr+   r]   rG   r`   r,   r*   Úcalsr    ra   r%   r6   „   s(   






ÿ


  þ

þ
ÿ
ÿÿ"
( ý
zRaw._read_raw_filec                 C   sÆ   | j dk	r| j S d}t| j| jƒD ]ˆ\}}|d D ]j}|dk	r2t|ƒ�D}| |jd¡ t|ƒ}|dk	r†|jt	j
t	jfkr€tj}ntj}W 5 Q R X |dk	r2 qžq2|dk	r" q¬q"|dkr¼tdƒ‚|| _ |S )z-Get the dtype to use to store data from disk.Nr+   r   zbug in reading)Z_dtype_Úzipr)   rC   r   Úseekrm   r
   rq   r   rs   rt   rv   Z
complex128Zfloat64rx   )rD   ZdtypeZ	raw_extrar(   r+   r   r€   r    r    r%   Ú_dtypeB  s.    

ÿz
Raw._dtypec              
   C   sx  d}t | j| ƒ��Z}	| j| d }
| j| d }| j| d }||
dd… k||
dd… k @ }d}t |¡d D ]Î}|
| }|
|d  }|| }|| }t|| dƒ}t||| ƒ}|| }|dk	�r8t|	|j||f||fd�j	}z||f|_
W n tk
�r   ||7 }Y n(X t|dd…||| …f |j|||ƒ ||7 }qr|�rXtd	|› d
�ƒ ||| k�sjt‚W 5 Q R X dS )z#Read a segment of data from a file.r   r,   r+   Z
orig_nchanNr-   r.   )ÚshapeZrlimsz<FIF raw buffer could not be read, acquisition error likely: z samples set to zero)r   rC   r)   rv   ÚwhereÚmaxÚminr	   rm   rn   r†   ÚAttributeErrorr   ÚTr   ry   )rD   rn   ÚidxÚfirL   rM   r‚   ZmultZn_badr   r,   ZentsrU   ZuseÚoffsetÚeir\   r^   r]   r+   Z
first_pickZ	last_pickZpicksampÚoner    r    r%   Ú_read_segment_file]  sD     
þ   ÿ
zRaw._read_segment_filec                 C   s   ddl m} || jƒ | S )aù  Fix Elekta magnetometer coil types.

        Returns
        -------
        raw : instance of Raw
            The raw object. Operates in place.

        Notes
        -----
        This function changes magnetometer coil types 3022 (T1: SQ20483N) and
        3023 (T2: SQ20483-A) to 3024 (T3: SQ20950N) in the channel definition
        records in the info structure.

        Neuromag Vectorview systems can contain magnetometers with two
        different coil sizes (3022 and 3023 vs. 3024). The systems
        incorporating coils of type 3024 were introduced last and are used at
        the majority of MEG sites. At some sites with 3024 magnetometers,
        the data files have still defined the magnetometers to be of type
        3022 to ensure compatibility with older versions of Neuromag software.
        In the MNE software as well as in the present version of Neuromag
        software coil type 3024 is fully supported. Therefore, it is now safe
        to upgrade the data files to use the true coil type.

        .. note:: The effect of the difference between the coil sizes on the
                  current estimates computed by the MNE software is very small.
                  Therefore the use of mne_fix_mag_coil_types is not mandatory.
        r   )Úfix_mag_coil_types)Zchannelsr’   r>   )rD   r’   r    r    r%   r’   €  s    
zRaw.fix_mag_coil_typesc                 C   s"   t | ddƒdkrt| jƒ| _| jS )zoThe AcqParserFIF for the measurement info.

        See Also
        --------
        mne.AcqParserFIF
        Ú
_acqparserN)Úgetattrr   r>   r“   )rD   r    r    r%   Ú	acqparser   s    zRaw.acqparser)FFr   N)TN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r;   r6   Úpropertyr…   r‘   r’   r•   Ú__classcell__r    r    rN   r%   r      s"   (    ÿ:   ÿ >
# r   c                 C   s   t | ƒs| S dS d S )Nz	File-like)r   )r5   r    r    r%   r4   ­  s    r4   c                 C   s   | |krt dƒ‚dS )zSanity check entries.z3Could not read data, perhaps this is a corrupt fileN)ÚIOError)r\   rT   r    r    r%   ro   ´  s    ro   Fr   c                 C   s   t | ||||d�S )as  Reader function for Raw FIF data.

    Parameters
    ----------
    fname : str | file-like
        The raw filename to load. For files that have automatically been split,
        the split part will be automatically loaded. Filenames should end
        with raw.fif, raw.fif.gz, raw_sss.fif, raw_sss.fif.gz, raw_tsss.fif,
        raw_tsss.fif.gz, or _meg.fif. If a file-like object is provided,
        preloading must be used.

        .. versionchanged:: 0.18
           Support for file-like objects.
    allow_maxshield : bool | str (default False)
        If True, allow loading of data that has been recorded with internal
        active compensation (MaxShield). Data recorded with MaxShield should
        generally not be loaded directly, but should first be processed using
        SSS/tSSS to remove the compensation signals that may also affect brain
        activity. Can also be "yes" to load without eliciting a warning.
    %(preload)s
    %(on_split_missing)s
    %(verbose)s

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

    Notes
    -----
    .. versionadded:: 0.9.0

    When reading a FIF file, note that the first N seconds annotated
    ``BAD_ACQ_SKIP`` are **skipped**. They are removed from ``raw.times`` and
    ``raw.n_times`` parameters but ``raw.first_samp`` and ``raw.first_time``
    are updated accordingly.
    )r5   rE   rB   r   r   )r   )r5   rE   rB   r   r   r    r    r%   Úread_raw_fifº  s
    ( þr�   )FFr   N),r<   rf   Úos.pathrg   r8   Únumpyrv   Ú	constantsr   Úopenr   r   r   Z	meas_infor   r}   r   r€   r	   r
   Úbaser   r   r   r   Úutilsr   rA   r   r   Úeventr   r   r   r   r   r   r   r   r   r   r4   ro   r�   r    r    r    r%   Ú<module>	   s2   (       ÿ