CoolFace
Apppublic

Aluode/PerceptionLabPortable

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

2'�Yin+�@s�ddlZddlmZddlmZddlmZddlm	Z	ddl3mZddlm
Z
mZdd	lmZmZdd4lmZddlmZddlmZmZmZmZmZd
d�Zdd�Ze					ddd�dd��Ze							ddd��ZdS) �N)�minimize_scalar)�gaussian_kde�)�FIFF)�5pick_types)�fit_sphere_to_headshape)�_calc_g�_calc_h)�6BaseEpochs�make_fixed_length_epochs)�Evoked)�BaseRaw)�_check_preload�_ensure_int�_validate_type�logger�verbosecCsJ|jddt|�d�|7<tj�|�}|�d�}t�|�}|||fS)N�r)�flat�len�np�linalg�inv�sum)�G�lambda2�Gi�TC�sgi�r��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\mne/preprocessing/_csd.py�7_prepare_Gs8 91011r!cCs�|jd}t�|�}|�d�}||}|\}}}	t�||�}12tj|13dd�|	}|14t�|dd�tjf|tjdd�f�}t�|j|�j|d}
|
S)zCompute the CSD.r��axisNr)�shaper�eye�mean�dotr�newaxis�T)�
G_precomputed�H�radiusZ15n_channels�data�mu�ZrrrZCp2Zc02ZC2�Xrrr �_compute_csd&s16171819,r1�auto��h㈵��>��2T)rcs`t�tttfd�t�d��jdtjkrtd��t|t	d�|r&��20�n��t�jddgd��t��fd	d21��jdD��rDtd��t
��d
krNtd��t|dd�d
|kr^dksfntd|����t|dd�|d
krwtd|����t|d�}|dkr�td|����t|t�r�|dkr�t�j�\}}}	||	\}22}}|23|||f}zt�|t�}|\}24}}}Wnty�td|����wt|25dd�t|dd�t|dd�t|dd�|d
kr�td��t��fdd26��D��}
t�|
����rt�|
d��d����rtd ��|
|27||f8}
|
tjj|
ddd!�}
t�t�|
|
j�d"d�}~
t|||d#�}t|||d#�}t ||�}t!|||d$�}t�t��sI�j"gn�j"}|D]
}t�||��|�<�qN�j�#��tj�jd<Wd%�n	1�sswY�D]}�jd&|j$tj%tj&d'��qzt�t��r��j'�r�d(�j'v�r��j'd(=�j(�r�d(�j(v�r��j(d(=�S))a�Get the current source density (CSD) transformation.28 29    Transformation based on spherical spline surface Laplacian30    :footcite:`PerrinEtAl1987,PerrinEtAl1989,Cohen2014,KayserTenke2015`.31 32    This function can be used to re-reference the signal using a Laplacian33    (LAP) "reference-free" transformation.34 35    Parameters36    ----------37    inst : instance of Raw, Epochs or Evoked38        The data to be transformed.39    sphere : array-like, shape (4,) | str40        The sphere, head-model of the form (x, y, z, r) where x, y, z41        is the center of the sphere and r is the radius in meters.42        Can also be "auto" to use a digitization-based fit.43    lambda2 : float44        Regularization parameter, produces smoothness. Defaults to 1e-5.45    stiffness : float46        Stiffness of the spline.47    n_legendre_terms : int48        Number of Legendre terms to evaluate.49    copy : bool50        Whether to overwrite instance data or create a copy.51    %(verbose)s52 53    Returns54    -------55    inst_csd : instance of Raw, Epochs or Evoked56        The transformed data. Output type will match input type.57 58    Notes59    -----60    .. versionadded:: 0.2061 62    References63    ----------64    .. footbibliography::65    �instz
Computing CSDZcustom_ref_appliedz,CSD already applied, should not be reapplied�copyFT)Zmeg�eeg�excludecs g|]}|t��j��v�qSr)r�array�ch_names)�.0�ch�r6�picksrr �66<listcomp>ss z2compute_current_source_density.<locals>.<listcomp>Zbadsz�CSD cannot be computed with bad EEG channels. Either drop (inst.drop_channels(inst.info['bads']) or interpolate (`inst.interpolate_bads()`) bad EEG channels.rzNo EEG channels found.�numericrrz%lambda2 must be between 0 and 1, got �	stiffnessz#stiffness must be non-negative got �n_legendre_termsz-n_legendre_terms must be greater than 0, got r2z9sphere must be "auto" or array-like with shape (4,), got �x�y�zr,z2sphere radius must be greater than 0, got {radius}cs&g|]}�jd|ddd��qS)�chs�locN�)�info)r<�pick)r6rr r@�s&gz&Zero or infinite position found in chs)r#Zkeepdims�����)rBrC)r*r+r,NrG)Z	coil_type�unitr8))rr67r
rrrJrZFIFFV_MNE_CUSTOM_REF_CSD�68ValueError�boolr7r�anyrr�69isinstance�strrrr:�float�	Exception�isfinite�all�iscloserZnormZclipr'r)rr	r!r1�_dataZ_unlock�updateZFIFFV_COIL_EEG_CSDZFIFF_UNIT_V_M2Zrejectr)r6ZsphererrBrCr7rr,Zorigin_headZ
origin_devicerDrErF�posZcos_distrr+r*Z	trans_csd�epochsZeporKrr>r �compute_current_source_density6s�270�71���(72�73�r\���?�c74sHt|d�|��}t|jdd�}t|�dkrtd��|j|||dd�t|t�r0t	||ddd�}|j75|d	�}	t|t�rB|	tj
d76f}	|	jd}77t�|78|j|jf�tj}t|j�D])}t|d|j�D]}
tj|	dd�|f|	dd�|
fdd
�|dd�||
f<qcqY|d9}t�}|t�|�}||�kj|79|kr�||fSt||�k|d��tjdd��tt��fdd��j���}Wd�n1s�wYt�d|�d��t|j�D]B}t|d|j�D]7}
t�|dd�||
f|k�}||80|k�rt�d|j||�d|j||
���|�||||
f�q�q�||fS)a~Compute bridged EEG electrodes using the intrinsic Hjorth algorithm.81 82    First, an electrical distance matrix is computed by taking the pairwise83    variance between electrodes. Local minimums in this matrix below84    ``lm_cutoff`` are indicative of bridging between a pair of electrodes.85    Pairs of electrodes are marked as bridged as long as their electrical86    distance is below ``lm_cutoff`` on more than the ``epoch_threshold``87    proportion of epochs.88 89    Based on :footcite:`TenkeKayser2001,GreischarEtAl2004,DelormeMakeig2004`90    and the `EEGLAB implementation91    <https://psychophysiology.cpmc.columbia.edu/>`__.92 93    Parameters94    ----------95    inst : instance of Raw, Epochs or Evoked96        The data to compute electrode bridging on.97    lm_cutoff : float98        The distance in :math:`{\mu}V^2` cutoff below which to99        search for a local minimum (lm) indicative of bridging.100        EEGLAB defaults to 5 :math:`{\mu}V^2`. MNE defaults to101        16 :math:`{\mu}V^2` to be conservative based on the distributions in102        :footcite:t:`GreischarEtAl2004`.103    epoch_threshold : float104        The proportion of epochs with electrical distance less than105        ``lm_cutoff`` in order to consider the channel bridged.106        The default is 0.5.107    l_freq : float108        The low cutoff frequency to use. Default is 0.5 Hz.109    h_freq : float110        The high cutoff frequency to use. Default is 30 Hz.111    epoch_duration : float112        The time in seconds to divide the raw into fixed-length epochs113        to check for consistent bridging. Only used if ``inst`` is114        :class:`mne.io.BaseRaw`. The default is 2 seconds.115    bw_method : None116        ``bw_method`` to pass to :class:`scipy.stats.gaussian_kde`.117    %(verbose)s118 119    Returns120    -------121    bridged_idx : list of tuple122        The indices of channels marked as bridged with each bridged123        pair stored as a tuple.124    ed_matrix : ndarray of float, shape (n_epochs, n_channels, n_channels)125        The electrical distance matrix for each pair of EEG electrodes.126 127    Notes128    -----129    .. versionadded:: 1.1130 131    References132    ----------133    .. footbibliography::134    zComputing bridged electrodesT)r8rz8No EEG channels found, cannot compute electrode bridgingF)�l_freq�h_freqr?r)�durationZpreloadr)r?.rNr"g��mB)�	bw_method�ignore)�invalidcs|�kr|dkr�|�StjS)Nr)r�inf)rD�Zkde�	lm_cutoffrr �<lambda>1sz,compute_bridged_electrodes.<locals>.<lambda>zLocal minimum z foundzBridge detected between z and ) rr7rrJr�RuntimeError�filterrQr
r�get_datarrr(r$Zzeros�size�nan�range�var�list�isnanrZerrstaterSrrD�itemrrr;�append)r6rhZepoch_thresholdr`raZepoch_durationrcrr?r-Zn_epochsZ	ed_matrix�i�jZbridged_idxZed_flatZ
local_minimumZ
bridged_countrrgr �compute_bridged_electrodes�sb135B136�137138:���������139rw)r2r3r4r5T)r]r^r^r_rNN) �numpyrZscipy.optimizerZscipy.statsrZ_fiff.constantsrZ140_fiff.pickrZbemrZchannels.interpolationrr	r[r141rZevokedr�ior
�utilsrrrrrr!r1r\rwrrrr �<module>s@���
Aluode/PerceptionLabPortable · CoolFace