U
    f»{já/  ã                
   @   sÊ   d dl ZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZmZ d
gZddddddddddœ	ZdZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ ZdS )&é    Né   )ÚDigPoint)ÚFIFF)Úcreate_info)Ú	pick_infoé   )Úrotation3d_align_z_axis)ÚwarnÚ_check_pandas_installedÚneuromag306é   g{®Gáz„?gü©ñÒMbP?g�íµ ÷Æ°>g      Y@)	ÚmÚcmÚmmÚVZmVZuVÚTzT/mzT/cmzÓImporting FieldTrip data without an info dict from the original file. Channel locations, orientations and types will be incorrect. The imported data cannot be used for source analysis, channel interpolation etc.c                 C   s   t | tƒrtdƒ‚dS )z*Run validation checks on the ft_structure.z/Loading of data in cell arrays is not supportedN)Ú
isinstanceÚlistÚRuntimeError)Ú	ft_struct© r   úU/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/fieldtrip/utils.pyÚ_validate_ft_struct!   s    
r   c              	      sN  |dkrt tƒ t| ƒ}| d ‰ |�r| ¡ ‰tˆ ƒtˆd ƒ ‰ˆrÐt dtˆƒf ƒ ‡ fdd„ˆD ƒ}‡fdd„ˆ D ƒ}|‰ ˆ | d< d| kr¢t| d |ƒ| d< d	| krÐ| d	 jd
krÐtj	| d	 |dd�| d	< ˆ 
¡ � |ˆd< W 5 Q R X ‡fdd„ˆ D ƒ}tˆ|dd� n<tˆ |ƒ‰t| ƒ\}}ˆj
dd�� ˆj||d� W 5 Q R X ˆS )z5Create MNE info structure from a FieldTrip structure.NÚlabelÚch_nameszžThe following channels are present in the FieldTrip data but cannot be found in the provided info: %s.
These channels will be removed from the resulting data!c                    s   g | ]}ˆ   |¡‘qS r   ©Úindex©Ú.0Úch©r   r   r   Ú
<listcomp>7   s     z _create_info.<locals>.<listcomp>c                    s   g | ]}|ˆ kr|‘qS r   r   r   )Úmissing_channelsr   r   r!   8   s      ÚtrialÚavgr   r   ©ZaxisÚsfreqc                    s   g | ]}ˆ d    |¡‘qS r    r   r   )Úinfor   r   r!   J   s     F)ÚcopyT)Zupdate_redundant)ÚchsÚdig)r	   ÚNOINFO_WARNINGÚ
_set_sfreqr(   ÚsetÚstrÚ#_remove_missing_channels_from_trialÚndimÚnpÚdeleteZ_unlockr   r   Ú_create_info_chs_digÚupdate)r   Zraw_infor&   Úmissing_chan_idxZnew_chsZch_idxr)   r*   r   )r   r'   r"   r   Ú_create_info'   sF    ýþ
þ


r6   c                 C   sd   t | tƒr0tt| ƒƒD ]}t| | |ƒ| |< qn0t | tjƒrX| jdkr`tj| |dd�} nt	dƒ‚| S )Nr   r   r%   z?"trial" field of the FieldTrip structure has an unknown format.)
r   r   ÚrangeÚlenr/   r1   Zndarrayr0   r2   Ú
ValueError)r#   r5   Z	idx_trialr   r   r   r/   U   s    
 ÿ
þr/   c                 C   s  | d }t tjddtjt ddddddddddddg¡tjd�}z| d }W n tk
rd   d}Y nX z| d }W n tk
rŠ   d}Y nX |dkr¤|dkr¤td	ƒ d
|p´|p´d
dikrÀt	dƒ‚t
ƒ }t
ƒ }d}t|ƒD �]4\}}	| ¡ }
|	|
d< |d |
d< |d |
d< |�r\|	|d k�r\t|
|ƒ}
|d7 }t|
d dd… tjtj|d�}| |¡ n¨|�r||	|d k�r|t|
|ƒ}
nˆ|	 d¡�ržtj|
d< tj|
d< nf|	 d¡�rÀtj|
d< tj|
d< nD|	 d¡�râtj|
d< tj|
d< n"td|	f ƒ tj|
d< tj|
d< | |
¡ qØ||fS )z7Create the chs info field from the FieldTrip structure.r   g      ð?r   r   )Úcoord_frameÚcalr7   Zunit_mulÚlocÚunitÚelecNÚgradz–The supplied FieldTrip structure does not have an elec or grad field. No channel locations will extracted and the kind of channel might be inaccurate.ÚchanposzçThis file was created with an old version of FieldTrip. You can convert the data to the new version by loading it into FieldTrip and applying ft_selectdata with an empty cfg structure on it. Otherwise you can supply the Info field.Úch_nameZlognoZscannor<   r   )Úrr:   ÚkindÚidentZEOGrC   Ú	coil_typeZECGZSTIzFCannot guess the correct type of channel %s. Making it a MISC channel.)Údictr   ZFIFFV_COORD_UNKNOWNZFIFF_UNITM_NONEr1   ÚarrayÚFIFF_UNIT_VÚKeyErrorr	   r   r   Ú	enumerater(   Ú_process_channel_eegr   ÚFIFFV_COORD_HEADZFIFFV_POINT_EEGÚappendÚ_process_channel_megÚ
startswithZFIFFV_EOG_CHÚFIFFV_COIL_EEGZFIFFV_ECG_CHZFIFFV_COIL_EEG_BIPOLARZFIFFV_STIM_CHZFIFFV_COIL_NONEZFIFFV_MISC_CH)r   Zall_channelsZch_defaultsr>   r?   r)   r*   ÚcounterZidx_chanZcur_channel_labelÚcur_chÚdr   r   r   r3   g   st     û

ÿ
  þ


ÿ

r3   c                 C   s¸   z| d }W nd t k
rp   z| d }W n t k
rD   tdƒ‚Y n(X t|d ƒt|d ƒ }}d||  }Y nX zt|ƒ}W n4 tk
r²   td|› d�ƒ t| ¡ d ƒ}Y nX |S )zSet the sample frequency.ZfsampleÚtimezNo Source for sfreq foundr   r   zJFieldTrip structure contained multiple sample rates, trying the first of:
z Hz)rI   r9   ÚfloatÚ	TypeErrorr	   Zravel)r   r&   rT   Út1Út2r   r   r   r,   ¬   s    r,   c                    s@   | d ‰ t ‡ fdd„tˆ ƒD ƒƒ}|r4ˆ d d }ntdƒ‚|S )z?Set the start time before the event in evoked data if possible.rT   c                 3   s.   | ]&\}}ˆ | d  ˆ |d  d  kV  qdS )r   r   Nr   )r   ÚiÚx©Útimesr   r   Ú	<genexpr>Ä   s   ÿz_set_tmin.<locals>.<genexpr>r   z>Loading data with non-uniform times per epoch is not supported)ÚallrJ   r   )r   Z
time_checkZtminr   r[   r   Ú	_set_tminÁ   s    ÿr_   c                 C   s¦   d| krdS | d }t t|ƒƒ}|dk r0tdƒ‚d}|jdkrH|jd }||d kr\tdƒ‚t t|ƒ¡}|jdkr„|dd…|f }t t |¡||g¡ 	d¡j
}|S )	z4Create an event matrix from the FieldTrip structure.Ú	trialinfoNr   z!trialinfo_column must be positiver   r   zBtrialinfo_column is higher than the amount ofcolumns in trialinfo.Úint)r7   r8   r9   r0   Úshaper1   ÚzerosZvstackrG   Zastyper   )r   Ztrialinfo_columnZ
event_typeZevent_numberZavailable_ti_colsZevent_trans_valÚeventsr   r   r   Ú_create_eventsÎ   s(    


ÿÿre   c                 C   s,   t dd�}|stdƒ dS | | d ¡}|S )z%Create event metadata from trialinfo.F)Ústrictz]The Pandas library is not installed. Not returning the original trialinfo matrix as metadata.Nr`   )r
   r	   Z	DataFrame)r   ZpandasÚmetadatar   r   r   Ú_create_event_metadataì   s    
rh   c                 C   sš   t  |d ¡}t  || d k¡d d }t  |d |dd…f ¡}|d }|t|  }t  |t  d¡f¡| d< tj| d< tj	| d	< tj
| d
< tj| d< | S )aA  Convert EEG channel from FieldTrip to MNE.

    Parameters
    ----------
    cur_ch: dict
        Channel specific dictionary to populate.

    elec: dict
        elec dict as loaded from the FieldTrip structure

    Returns
    -------
    cur_ch: dict
        The original dict (cur_ch) with the added information
    r   rA   r   r@   Nr=   )é	   r<   rC   rE   r:   )r1   Ú
asanyarrayÚwhereÚsqueezeÚ
_unit_dictÚhstackrc   r   rH   ZFIFFV_EEG_CHrP   rL   )rR   r>   Ú
all_labelsZchan_idx_in_elecÚpositionÚposition_unitr   r   r   rK   ù   s    



rK   c                 C   sX  t  |d ¡}t  || d k¡d d }|d }|d | }|d }t  |d |dd…f ¡}|t|  }|d	k�rd
|k�rd|k�rt  |d
 |dd…f dk¡d }|d |dd…f }	|	t|  }	|d |dd…f }
|dkrú|	d d|
d   }|dk�r|	d|
  }t j|dd�}t  |d |dd…f ¡}zt|ƒj}W n  tk
�rf   t  	d¡}Y nX |j
dk�sxt‚| ¡ }t  ||f¡| d< tj| d< |dk�r¼tj| d< tj| d< nŽ|dk�rÜtj| d< tj| d< nn|dk�rütj| d< tj| d< nN|dk�rtj| d< tj| d< n.|dk�r<tj| d< tj| d< ntd|f ƒ‚tj| d< | S )a2  Convert MEG channel from FieldTrip to MNE.

    Parameters
    ----------
    cur_ch: dict
        Channel specific dictionary to populate.

    grad: dict
        grad dict as loaded from the FieldTrip structure

    Returns
    -------
    dict: The original dict (cur_ch) with the added information
    r   rA   r   ÚtypeÚchantyper=   r@   Nr   ZtraZcoilposZcoiloriZmegmagga2U0*©3?Z	megplanarr%   Zchanorir   )r   r   r<   rC   rE   ZrefmagZrefgradZmeggradzUnexpected coil type: %s.r:   )r1   rj   rk   rl   rm   Zaverager   r   ÚAssertionErrorÚeyerb   Úflattenrn   r   ZFIFFV_MEG_CHZFIFFV_COIL_POINT_MAGNETOMETERZFIFF_UNIT_TZFIFFV_COIL_VV_PLANAR_T1ZFIFF_UNIT_T_MZFIFFV_COIL_MAGNES_REF_MAGZFIFFV_COIL_MAGNES_REF_GRADZFIFFV_COIL_AXIAL_GRAD_5CMr   rL   )rR   r?   ro   Zchan_idx_in_gradZgradtypers   rq   rp   Zidx_in_coilposZcur_coilposZcur_coiloriZtmp_posZoriginal_orientationZorientationr   r   r   rN     s\    "











ÿ
rN   )Únumpyr1   Z_digitizationr   Ú	constantsr   Z	meas_infor   Úpickr   Z
transformsr   Úutilsr	   r
   Z_supported_megsrm   r+   r   r6   r/   r3   r,   r_   re   rh   rK   rN   r   r   r   r   Ú<module>   s8   ø
.E 