U
    f»{j~*  ã                   @   sî   d 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 ddlmZ ddl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mZmZ d	dlm Z  d	dl!m"Z" e
ddd„ƒZ#e
G dd„ deƒƒZ$dd„ Z%dS )z Conversion tool from CTF to FIF.é    Né   )Ú_format_dig_pointsé   )ÚverboseÚloggerÚ_clean_namesÚfill_docÚ_check_optionÚ_check_fname)ÚBaseRaw)Ú_mult_cal_oneÚ_blk_read_limsé   )Ú
_read_res4Ú_make_ctf_name)Ú_read_hc)Ú	_read_eegÚ	_read_pos)Ú_make_ctf_coord_trans_set)Ú_compose_meas_infoÚ_read_bad_chansÚ_annotate_bad_segments)ÚCTF)Ú_read_annotations_ctf_callÚtruncateFc                 C   s   t | ||||d�S )a*  Raw object from CTF directory.

    Parameters
    ----------
    directory : str
        Path to the CTF data (ending in ``'.ds'``).
    system_clock : str
        How to treat the system clock. Use "truncate" (default) to truncate
        the data file when the system clock drops to zero, and use "ignore"
        to ignore the system clock (e.g., if head positions are measured
        multiple times during a recording).
    %(preload)s
    clean_names : bool, optional
        If True main channel names and compensation channel names will
        be cleaned from CTF suffixes. The default is False.
    %(verbose)s

    Returns
    -------
    raw : instance of RawCTF
        The raw data.

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

    Notes
    -----
    .. versionadded:: 0.11

    To read in the Polhemus digitization data (for example, from
    a .pos file), include the file in the CTF directory. The
    points will then automatically be read into the `mne.io.Raw`
    instance via `mne.io.read_raw_ctf`.
    )ÚpreloadÚclean_namesr   )ÚRawCTF)Ú	directoryÚsystem_clockr   r   r   © r    úM/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/ctf/ctf.pyÚread_raw_ctf   s    & ÿr"   c                       s6   e Zd ZdZed‡ fdd„	ƒZdd„ Zd	d
„ Z‡  ZS )r   aÆ  Raw object from CTF directory.

    Parameters
    ----------
    directory : str
        Path to the CTF data (ending in ``'.ds'``).
    system_clock : str
        How to treat the system clock. Use "truncate" (default) to truncate
        the data file when the system clock drops to zero, and use "ignore"
        to ignore the system clock (e.g., if head positions are measured
        multiple times during a recording).
    %(preload)s
    clean_names : bool, optional
        If True main channel names and compensation channel names will
        be cleaned from CTF suffixes. The default is False.
    %(verbose)s

    See Also
    --------
    mne.io.Raw : Documentation of attribute and methods.
    r   FNc                    sŠ  t |ddddd�}| d¡s*td|› �ƒ‚td|dd	gƒ t d
| ¡ t|ƒ}t|ƒ}t|ƒ}t	||ƒ}	t
||	ƒ}
t|||	|ƒ}| ¡ �& |d  |
7  < t|d ƒ|d< W 5 Q R X |d  t||ƒ7  < tƒ }tƒ }tƒ }tƒ }tƒ }t|ƒdkrúdn
dt|ƒ }t||dd�\}}|�s6| tj ||¡¡ �qÂt|||ƒ}|d dk�rh| tj ||¡¡ �qÂt|ƒdk�rˆ|d |d  }nd}| |¡ | |d d ¡ | |¡ dgt|ƒ }qêt|ƒdk�rätd|› d|› �ƒ‚tt| ƒj||||||d||d�	 |d  t|d ƒ }t|||d ƒ}t||d |d  |d |d  |d d�}|d k�rf|n|| }|  |¡ |�r†|  ¡  d S )NÚreadTr   )Úneed_dirz.dsz5directory must be a directory ending with ".ds", got r   Úignorer   zds directory : %sÚdigZbadsr   Zmeg4z%d_meg4F)Zraise_errorÚn_sampÚ
block_sizeZsfreqg      ð?r   z?Could not find any data, could not find the following file(s): z2, and the following file(s) had no valid samples: Úint)Úfirst_sampsÚ
last_sampsÚ	filenamesÚ
raw_extrasZorig_formatÚbuffer_size_secr   Zpre_trig_ptsÚ	meas_dateÚnsamp)r   Ztotal_offsetZtrial_durationr/   ) r
   ÚendswithÚ	TypeErrorr	   r   Úinfor   r   r   r   r   r   Z_unlockr   r   ÚlistÚlenr   ÚappendÚosÚpathÚrelpathÚ_get_sample_infoÚIOErrorÚsuperr   Ú__init__Úfloatr   r   Zset_annotationsr   )Úselfr   r   r   r   r   Úres4ZcoilsÚeegZcoord_transZdigsr3   Úfnamesr+   r-   Zmissing_namesZno_sampsÚsuffixZ	meg4_nameÚfoundZsample_infor.   r*   Ú
start_timeZannotZmarker_annot©Ú	__class__r    r!   r=   ^   sŽ    
ÿ



  ÿ


ÿ
     üÿü
zRawCTF.__init__c                 C   sR  | j | }d}	t||t|d ƒƒ\}
}}t| j| dƒ��}tt|ƒƒD ]ú}||
 |d  }t|d | |d ƒ}tj	dd��, t 
tj¡}|t 
|¡|d  d	 7 }W 5 Q R X | |d¡ tj|d
|d | d�}|d |f|_|dd…||df ||df …f }|dd…||df ||df …f }t|||||ƒ |	|7 }	qHW 5 Q R X dS )zRead a chunk of raw data.r   r(   ÚrbÚ
res4_nsampÚ
n_samp_totÚraise)ZoverÚn_chané   ú>i4)ÚcountNr   )Z_raw_extrasr   r)   ÚopenZ
_filenamesÚranger5   ÚminÚnpZerrstateZint64r   ÚHEADER_SIZEÚseekÚfromfileÚshaper   )r?   ÚdataÚidxÚfiÚstartÚstopZcalsZmultÚsiÚoffsetZtrial_start_idxZr_limsZd_limsÚfidZbiÚsamp_offsetZn_readÚposÚ	this_dataZ	data_viewr    r    r!   Ú_read_segment_file®   s*    

ÿ
$
ÿ$$zRawCTF._read_segment_filec                 C   sV   t t| jt| jƒƒƒ}|  |¡ | jd D ]&}dD ]}t|d | ƒ|d |< q2q*dS )z)Clean up CTF suffixes from channel names.Úcomps)Z	row_namesZ	col_namesrX   N)ÚdictÚzipÚch_namesr   Zrename_channelsr3   )r?   ÚmappingÚcompÚkeyr    r    r!   r   Ç   s
    
zRawCTF._clean_names)r   FNF)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r=   rc   r   Ú__classcell__r    r    rF   r!   r   F   s       ÿOr   c              	   C   sd  t  d| f ¡ tj|d kr0|d  tj¡}nd}t|d ƒD ]\}}|d tjkr@|} q`q@t| dƒ��\}| dtj	¡ | 
¡ }| dd¡ |tj d|d	  |d
   dkrºtdƒ‚|tj d|d
   }||d	  }	|}
|dkrôt  d¡ nÐ|dk�r
t  d¡ nºt  d¡ t|	ƒD ]¦}||d	  }tj||d
  ||d	   d  }| |d¡ t |d|d	 ¡}t|ƒ|d	 k�rŽtd|d  ƒ‚t |dk¡d }t|ƒdk�r||d  }
 �qÄ�qW 5 Q R X |
|d	 k �rüd}	t  d|	|
|
|d
 f ¡ nJ|
|d	  }	||
 }t  d|	|d	 |
|d
 f ¡ |dk�rFt  d| ¡ t|
||d	 |d	 |d
 d�S )z&Determine the number of valid samples.zFinding samples for %s: rg   NZchsZch_namerH   r   rM   r0   Znchanz?The number of samples is not an even multiple of the trial sizezL    System clock channel is not available, assuming all samples to be valid.r%   z3    System clock channel is available, but ignored.zH    System clock channel is available, checking which samples are valid.rN   zCannot read data for trial %dr   z$    %d x %d = %d samples from %d chsz!    %d samples omitted at the end)r'   rJ   r(   rI   rL   )r   r3   r   ZSYSTEM_CLOCK_CHÚindexÚ	enumeraterP   rU   r7   ÚSEEK_ENDÚtellrT   ÚRuntimeErrorrQ   rS   rV   r5   Úwherere   )Úfnamer@   r   Zclock_chÚkÚchr_   Úst_sizerJ   Zn_trialr'   Útr`   r^   rb   ÚendZn_omitr    r    r!   r:   Ò   sv    ÿÿ


ÿÿÿÿÿ
 ÿr:   )r   FFN)&rn   r7   ÚnumpyrS   Z_digitizationr   Úutilsr   r   r   r   r	   r
   Úbaser   r   r   r@   r   r   Zhcr   rA   r   r   Ztransr   r3   r   r   r   Ú	constantsr   Úmarkersr   r"   r   r:   r    r    r    r!   Ú<module>   s,        ÿ) 