CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
interpolate.cpython-310.pyc112 linesDownload Raw Back to __pycache__
1o

2'�Yi�!�@s�dZddlmZddlZddlmZddlmZddl	m3Z4mZddlm
Z
mZdd	lmZmZdd5lmZmZddlmZmZddd�Zddd�Zdd�ZdS)zTools for data interpolation.�)�chainN)�connected_components�)�create_info)�6BaseEpochs�EpochsArray)�Evoked�EvokedArray)�BaseRaw�RawArray)�_cart_to_sph�_sph_to_cart)�_ensure_int�_validate_type��?Tcsd�kr7dksntd�����ttt�dd�|D����}t|dt�r/dd�|D��ndd�|D��g�|D]���t��fdd	�t	|�D��t8����q:��fd9d�t	|�D�}|rhdd�|D�}|D]}tt|jd�t|��r|j
|d
�||jd<qj|S)a]Interpolate or mark bads consistently for a list of instances.10 11    Once called on a list of instances, the instances can be concatenated12    as they will have the same list of bad channels.13 14    Parameters15    ----------16    insts : list17        The list of instances (Evoked, Epochs or Raw) to consider18        for interpolation. Each instance should have marked channels.19    interp_thresh : float20        A float between 0 and 1 (default) that specifies the fraction of time21        a channel should be good to be eventually interpolated for certain22        instances. For example if 0.5, a channel which is good at least half23        of the time will be interpolated in the instances where it is marked24        as bad. If 1 then channels will never be interpolated and if 0 all bad25        channels will be systematically interpolated.26    copy : bool27        If True then the returned instances will be copies.28 29    Returns30    -------31    insts_bads : list32        The list of instances, with the same channel(s) marked as bad in all of33        them, possibly with some formerly bad channels interpolated.34    r�z+interp_thresh must be between 0 and 1, got cSsg|]}|jd�qS)�bads��info��.0�inst�r��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\mne/preprocessing/interpolate.py�35<listcomp>2�z!equalize_bads.<locals>.<listcomp>cSsg|]}t|�t|j��qSr��len�timesrrrrr4scSsg|]}t|j��qSrrrrrrr6rc3s*�|]\}}�|jdvr�|VqdS)rNr)r�kr)�ch_name�	durationsrr�	<genexpr>;s���z equalize_bads.<locals>.<genexpr>cs g|]\}}�|�kr|�qSrr)rr�ch)�36good_times�
interp_threshrrrCs cSsg|]}|���qSr)�copyrrrrrE�r)�exclude)�37ValueError�list�setr�
from_iterable�38isinstancer�append�sum�	enumerate�nprr�interpolate_bads)Zinstsr%r&Zall_badsZ	bads_keeprr)r r!r$r%r�
equalize_badss.���	r3�cs�t�tttf�t|d�}|dkrtd|�d������}|dur%td��|��}|ddkr8td	|d�����j	d39}t40��j	d41<ttt
|����t�t��t��f�}|D]}t��|d�}t��|d�}	d|||	f<d||	|f<qXt|dd
�\}42���fdd�t��D�}�fdd�|D�}|D]}
t|
�|kr�tdd�|
��d���q�t�}t�}t|�D]�\}}�fdd�|D�}
|�|
�}t|d|
�}td|d��g�j	dd�}||ddddd�<�j|
d�}t�t��rtj|dd��dd�}t||�jd�}n=t�t��r%tj|dd��t|�dd�}t||�j d �}n tj|dd��dd�}t!tj|dd��dd�|�j �j"�j#d!�}||d|d��<q��j$t43|�%��d"d#�t44|��j	d45<��&���'t46|�(���|�j	d47<�S)$a�Interpolate bridged electrode pairs.48 49    Because bridged electrodes contain brain signal, it's just that the50    signal is spatially smeared between the two electrodes, we can51    make a virtual channel midway between the bridged pairs and use52    that to aid in interpolation rather than completely discarding the53    data from the two channels.54 55    Parameters56    ----------57    inst : instance of Epochs, Evoked, or Raw58        The data object with channels that are to be interpolated.59    bridged_idx : list of tuple60        The indices of channels marked as bridged with each bridged61        pair stored as a tuple.62    bad_limit : int63        The maximum number of electrodes that can be bridged together64        (included) and interpolated. Above this number, an error will be65        raised.66 67        .. versionadded:: 1.268 69    Returns70    -------71    inst : instance of Epochs, Evoked, or Raw72        The modified data object.73 74    See Also75    --------76    mne.preprocessing.compute_bridged_electrodes77    �	bad_limitrzEArgument 'bad_limit' should be a strictly positive integer. Provided z is invalid.Nz&No channel positions found in ``inst``Zcoord_frame�headz2Montage channel positions must be in ``head`` got rrF)Zdirectedcs,g|]}�fdd�t��|k�dD��qS)c�g|]}�|�qSrr)r�j)�nodesrrr�r'�=interpolate_bridged_electrodes.<locals>.<listcomp>.<listcomp>r)r1�where�rr)�labelsr9rrr�s,z2interpolate_bridged_electrodes.<locals>.<listcomp>csg|]}�fdd�|D��qS)c�g|]}�jj|�qSr�rZch_namesr<�rrrr��r:r)r�	group_idxr@rrr�s�z
The channels z, ze are bridged together and form a large area of bridged electrodes. Interpolation might be inaccurate.cr>rr?r<r@rrr�rA�ch_poszvirtual ZsfreqZeegZchs�loc�)Zpicks�Zaxis�����)�78first_samp)�tmin)rI�nave�kindT)Zforce_update_info))rr79rrrr)Zget_montage�RuntimeErrorZ
get_positionsrr*�sortedr+rr1ZzerosrZsearchsortedr�join�dictr0�union�_find_centroid_spherer�get_datar-�averageZreshaperrHrrIr	rJrKZadd_channels�valuesr2Z
drop_channels�keys)rZbridged_idxr5Zmontage�posZ	bads_origZG_denseZbridgeZidx0Zidx1�_Z80groups_idxZgroups_names�group_namesZvirtual_chsrrrBZpos_virtualZvirtual_info�dataZ81virtual_chr)rr=r9r�interpolate_bridged_electrodesOs� 82���8384���85�86rZcsht��fdd�|D��}t|�}tj|dd�}t|�}tj|dd�d}||d<t|�ddd�f}|S)aCompute the centroid position between N electrodes.87 88    The centroid should be determined in spherical "head" coordinates which is89    more accurante than cutting through the scalp by averaging in cartesian90    coordinates.91 92    A simple way is to average the location in cartesian coordinate, convert93    to spehrical coordinate and replace the radius with the average radius of94    the N points in spherical coordinates.95 96    Parameters97    ----------98    ch_pos : OrderedDict99        The position of all channels in cartesian coordinates.100    group_names : list | tuple101        The name of the N electrodes used to determine the centroid.102 103    Returns104    -------105    pos_centroid : array of shape (3,)106        The position of the centroid in cartesian coordinates.107    cr7rr)rr �rCrrr�r'z)_find_centroid_sphere.<locals>.<listcomp>rrF)rrN)r1�arrayrrSr
)rCrXZcartesian_positionsZsphere_positionsZcartesian_pos_centroidZsphere_pos_centroidZ108avg_radiusZpos_centroidrr[rrQ�srQ)rT)r4)�__doc__�	itertoolsr�numpyr1Zscipy.sparse.csgraphrZ_fiff.meas_inforZepochsrrZevokedrr	�ior109rZ110transformsrr
�utilsrrr3rZrQrrrr�<module>s111112;x