CoolFace
Apppublic

Aluode/PerceptionLabPortable

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

2'�Yi���@s�ddlZddlmZmZddlmZddlmZ	ddlm3Zddlm
Z
mZddlmZdd	lmZmZmZdd4lmZddlmZmZmZmZmZmZmZmZm Z dd
l!m"Z"m#Z#d_dd�Z$dd�Z%dd�Z&e
r�e�d_dd��Z'e�dd��Z(e�dd��Z)ne$Z'e%Z(e&Z)e�dd��Z*e�dd��Z+e�dd��Z,d d!�Z-d"d#�Z.d$d%�Z/d`d&d'�Z0d`d(d)�Z1dad+d,�Z2							-dbd.d/�Z3d0d1�Z4d2d3�Z5d4d5�Z6d6d7�Z7d8d9�Z8d:d;�Z9d<d=�Z:d>d?�Z;dcd@dA�Z<dBdC�Z=dDdE�Z>dddGdH�Z?e		I										J	-	K	dedLdM��Z@e		I										J	-	K	dedNdO��ZAe		I										J	-	K	dedPdQ��ZBe		I										J	-	K	dedRdS��ZCdadTdU�ZDed_dVdW��ZEdXdY�ZF	dfd]d^�ZGdS)g�N)�ndimage�sparse)�connected_components)�f)�t�)�	has_numba�jit)�
parallel_func)�MixedSourceEstimate�SourceEstimate�VolSourceEstimate)�SourceSpaces)	�ProgressBar�
_check_option�_pl�_validate_type�check_random_state�logger�5split_list�verbose�warn�)�f_oneway�ttest_1samp_no_pcCsL|durt�|�d}n|||}|tj|||dd�}d||<|��S)NrT)Z
assume_uniqueF)�np�where�isin�tolist)�r�s�	neighbors�indices�buddies�r$��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\mne/stats/cluster_level.py�_get_buddies_fallbacksr&cCst|t|||d�}|t|||dt|�d�}|||�}	|	||	}6|7||||8k}9d||10<|11��S�NrrF)�max�min�lenr)rr �ind�indsr�t_border�max_step�start�stopr"�selvesr$r$r%�_get_selves_fallback)s"r2cCstt�|��}|dkr
d}|S)Nr�����)�intr�argmax)�x�next_indr$r$r%�_where_first_fallback3sr8c12Cs�t�}|durt|�}nt|�}t|�D]/}|dur|}n||}||rC||}tt|��D]}	|||	krB|�|�d||<nq/q|S)NF)�listr*�range�append)13rr r!r"r#Zn_check�ii�this_idxZthis_s�nir$r$r%�_get_buddies?s$1415��r?cCs�t�}|t|||d�}|t|||dt|�d�}	t||	�D]}16||17}||r?||||kr?|�|�d||<q$|Sr')r9r(r)r*r:r;)rr r+r,rr-r.r1r/r0r<r=r$r$r%�_get_selvesVs"18�r@cCs&tt|��D]19}||r|SqdS)Nr3)r:r*)r6r<r$r$r%�_where_firstbs20�rAcCst�||�S�N)r�sum)r6�cr$r$r%�_masked_sumpsrEcCs&t�t�||�t�||�|�SrB)rrC�sign�abs)r6rD�t_powerr$r$r%�_masked_sum_powerus&rIcCst�|�t�|dk�|S)Nr)rrF�logical_not)�data�tstepr$r$r%�_sum_cluster_datazsrMc	Cs�t�|jt�}t�}|jdkrdnd}|dkrT|g}d||<d}|t|�krE||d}t|||||�}|�|�|d7}|t|�ks&t	|�}|�21||�|dks|S)z�Form spatial clusters using neighbor lists.22 23    This is equivalent to _get_components with n_times = 1, with a properly24    reconfigured adjacency matrix (formed as "neighbors" list)25    rr3r)r�ones�shape�boolr9�sizer*r?�extendrAr;)	r r!r�clustersr7�t_inds�icountr+r#r$r$r%�_get_clusters_spatials"	26��
rVcCsL||||k<t�||d||df�||d<tjgtd�||d<dS)zReassign cluster numbers.r��dtypeN)r�concatenate�arrayr4)�checkrS�base�numr$r$r%�	_reassign�s&r^csZt|��t|�}d}tj|�ftd�}t�}t|�D].\�}t||�}t|�D]\}}	|||�|	f<q'|t|�7}��fdd�|D�}||7}qt|dd�|dd�|dd��D]H\}27}}||||28|dk}|29|}
||}t�|�}|D]'}|
||k}t�	|�}t�|||k�D]	}t30|31|||�q�t32||||�q{q[dd�|D�}|S)	aDirectly calculate clusters.33 34    This uses knowledge that time points are35    only adjacent to immediate neighbors for data organized as time x space.36 37    This algorithm time increases linearly with the number of time points,38    compared to with the square for the standard (graph) algorithm.39 40    This algorithm creates clusters for each time point using a method more41    efficient than the standard graph method (but otherwise equivalent), then42    combines these clusters across time points in a reasonable way.43    rrWcsg|]}|���qSr$r$��.0�cl�r<�n_srcr$r%�44<listcomp>��z*_get_clusters_st_1step.<locals>.<listcomp>Nr3rcSsg|]45}t|�dkr|�qS�r�r*r_r$r$r%rd��)r*r�zerosr4r9�	enumeraterV�zip�uniquer)r^)�keepersr!�n_timesZenum_offsetr[rS�krD�ciraZcheck1Zcheck2r,Zcheck1_d�nZnextsr]Zprevsr\�prr$rbr%�_get_clusters_st_1step�s4
4647.4849�rscCsxt|�}t|�}t�}|�d�t|�D]\}}|||||<|�||t|��qt�|�}t�|�}|}t||�\}	}50tj|	j	t51d�}t�}t�|d||�}
|52jdkr[dnd}|dkr�|g}d||<d}|t|�kr�||d}t
||53||
|	||�}|
||	|||	|d�}t||54||55||�}|||7}|d7}|t|�kspt|�}|�||�|dksa|S)a,Directly calculate clusters.56 57    This uses knowledge that time points are58    only adjacent to immediate neighbors for data organized as time x space.59 60    This algorithm time increases linearly with the number of time points,61    compared to with the square for the standard (graph) algorithm.62    rrWr3Fr)r*r9r;rjrrZrY�divmodrNrOrP�arangerQr@r?rA)rmr!r.rcrnr-Zkiro�vrr rrSr,r7rTrUr+r1Z63these_indsr#r$r$r%�_get_clusters_st_multistep�s>	646566 �67�rwc
Cst|�}|j|}t�|�d}t|�dkr�tjgtd�g|}t�|||f�\}}dg}	t|t�r9|g}|g}n&t�|�}68||69}||70}|	t�t�	|�dk�dd�71�7}	|	�t|��t|	dd�|	dd��D]\}}t�
|||��|||<ql|dkr�t||�St|||�SgS)z"Choose the most efficient version.rrWrNr3)r*rQrrrZr4�
unravel_index�72isinstanceZargsort�diffrr;rk�sortrsrw)
�x_inr!r.rcrnZcl_goodsrm�row�colZlims�orderr/�endr$r$r%�_get_clusters_sts*737475$"76r�TcCsR|durt�t|��}nSt�||j||j�}|j|}|j|}|j|}|j}t�|�d}	t�	||	f�}t�	||	f�}t�	|tj77t|	�|jd�f�}tj
|||ff|d�}t|�\}78}|r�t�|�}t�|�}dd�t||dd�D�}
tjt|
�td�}t|�D]\}}|
|�|�||||7<q�dd�t|
|�D�}|S|S)	z<Get connected components from a mask and a adjacency matrix.FrrW�rOcSsg|]}t��qSr$)r9�r`�ir$r$r%rd4sz#_get_components.<locals>.<listcomp>rcSsg|]\}}|rt�|��qSr$�rrZ)r`ro�mr$r$r%rd9s)rrur*�logical_andr}r~rKrOrrYrNrXr�	coo_arrayrr)r(r:rirPrjr;rk)r|�	adjacencyZreturn_list�79components�maskrKr}r~rO�idx�_r/r0Z	comp_listr<�comprSr$r$r%�_get_components!s08081828384r�Fc	85s�td|gd��t�|�}t���s�t�t�std��t�fdd�dD��s*td��d}	|t�	|�}86|87j88d	kr<td89��|dkrZ�dd	krJtd
���dd	krTtd��t�
|90�}n|dkrdt�|91�}ntt�|92�t�
|93��}~94t��d|�dt�}��dd�}
��dd�}|dur�t|�d	kr�td�d�d|���nt�dt|�|d	|d|
|�t�|j95�}n�g}d}	|dur�tj|jtd�}|dvr�t�t�|�d	k�s�td��|dkr�t�t�|�d	k�s�td��t�}t�}t|�D]�\}}t�}|d	k�rt�||k|�t�||k|�g}n|dk�r#t�||k|�g}n	t�||k|�g}|D] }t�|��rMt||||||t �}||d	7}|�!|d��q.|	�r�|d	k�r\t"|�}n96t"|||d�}||
}|D]:}t|t#��r{|j$|j%}nt|t&��r�t|�}n|j't�'t�k�r�t�(|�}nt|�}|||||7<�qlq�|�r�t�)|�nt�*g�}|	�r�|}d}||fS)a1Find all clusters which are above/below a certain threshold.97 98    When doing a two-tailed test (tail == 0), only points with the same99    sign will be clustered together.100 101    Parameters102    ----------103    x : 1D array104        Data105    threshold : float | dict106        Where to threshold the statistic. Should be negative for tail == -1,107        and positive for tail == 0 or 1. Can also be an dict for108        threshold-free cluster enhancement.109    tail : -1 | 0 | 1110        Type of comparison111    adjacency : scipy.sparse.coo_array, None, or list112        Defines adjacency between features. The matrix is assumed to113        be symmetric and only the upper triangular half is used.114        If adjacency is a list, it is assumed that each entry stores the115        indices of the spatial neighbors in a spatio-temporal dataset x.116        Default is None, i.e, a regular lattice adjacency.117        False means no adjacency.118    max_step : int119        If adjacency is a list, this defines the maximal number of steps120        between vertices along the second dimension (typically time) to be121        considered adjacent.122    include : 1D bool array or None123        Mask to apply to the data of points to cluster. If None, all points124        are used.125    partitions : array of int or None126        An array (same size as X) of integers indicating which points belong127        to each partition.128    t_power : float129        Power to raise the statistical values (usually t-values) by before130        summing (sign will be retained). Note that t_power == 0 will give a131        count of nodes in each cluster, t_power == 1 will weight each node by132        its statistical score.133    show_info : bool134        If True, display information about thresholds used (for TFCE). Should135        only be done for the standard permutation.136 137    Returns138    -------139    clusters : list of slices or list of arrays (boolean masks)140        We use slices for 1D signals and mask to multidimensional141        arrays. None is returned if threshold is a dict (TFCE)142    sums : array143        Sum of x values in clusters.144    �tail�r3rrzLthreshold must be a number, or a dict for threshold-free cluster enhancementc3s�|]}|�vVqdSrBr$)r`�key��	thresholdr$r%�	<genexpr>���z!_find_clusters.<locals>.<genexpr>)r/�stepz9threshold, if dict, must have at least "start" and "step"Trz7No finite values found in the observed statistic valuesr3r/z.threshold["start"] must be <= 0 for tail == -1r�z,threshold["step"] must be < 0 for tail == -1r�h_powerr�e_power��?zthreshold["start"] (z?) is more extreme than data statistics with most extreme value z[Using %d thresholds from %0.2f to %0.2f for TFCE computation (h_power=%0.2f, e_power=%0.2f)FNrW�rrz+Thresholds must be monotonically increasingz+Thresholds must be monotonically decreasing)+rrZ145asanyarrayZisscalarry�dict�	TypeError�all�KeyError�isfiniterQ�RuntimeError�146ValueErrorr)r(ru�float�getr*rr�inforirNrOrPrzr9rjr��any�_find_clusters_1dir_partsrr;rG�slicer0r/�tuplerXrCrYrZ)r6r�r�r�r.�include�147partitionsrH�	show_info�tfceZuse_xr0Z148thresholdsr�r�ZscoresrS�sums�tiZthreshZx_insr|�out�hrDZlen_cr$r�r%�_find_clusters?s�<149150151�152����	153�154��155156157�r�cCs�|durt||||||�\}}||fSt�}t�}tt�|�d�D] }	t�|||	k�}158t||159||||�}||d7}|�|d�q"t�|�}||fS)z?Deal with partitions, and pass the work to _find_clusters_1dir.Nrr)�_find_clusters_1dirr9r:rr(r�r;rY)r6r|r�r.r�rHrrSr��pZx_ir�r$r$r%r��s�
�160r�c
s�|dur�|�|�\}}�jdkrE|�||�}|st�}	n�ttd|d��}161�dkr2|j�||162d�}	n�|jt���t����||163d�}	n�t�}t�	|�}	t|�D]2}||dk}|�164|����dkrnt��|�|	|<qQt�t��|�t��|���|	|<qQnP�jdkr�td��t
|tj�r�t�|�}t�|�s�|dur�t||�}nt
|t�r�t|||�}n	tdt|������dkrˇfdd�|D�}	n165��fd	d�|D�}	|t�|	�fS)166z'Actually call the clustering algorithm.Nr)�indexz<Data should be 1D when using a adjacency to define clusters.Fz.adjacency must be a sparse array or list, got csg|]}t�|��qSr$)rE�r`rD)r6r$r%rd/�z'_find_clusters_1dir.<locals>.<listcomp>csg|]}t�|���qSr$)rIr��rHr6r$r%rd1re)�label�ndimZfind_objectsr9r:rCrrFrG�emptyr;�ravel�	ExceptionryrZspmatrixr��issparser�r�r��typeZ167atleast_1d)
r6r|r�r.rHr�labelsZn_labelsrSr�r�r�rDr$r�r%r�sL168�169,�170�171172�r�cCsVt|�D]$\}}|stj|td�||<d|||<qt|��|��d�f||<q|S)zPConvert to the old format of clusters, which were bool arrays (or slices in 1D).rWTr)rjrrirPr�r)r()r�Zn_tot�	slice_outrprDr$r$r%�_cluster_indices_to_mask6sr�cCs�t|�D]\\}}t|tj�rt�|�|��||<qt|t�r)t�|j|j	�||<qt|t173�s4Jt|���t|�}t|�D]\}}t|t�rPt�|j|j	�||<q<t�|�d||<q<t174|�||<q|S)z>Convert to the old format of clusters, which were bool arrays.r)
rjryr�ndarrayr�reshaper�rur/r0r�r�r9)r�rOrprDr<�ccr$r$r%�_cluster_mask_to_indicesCs175176r�csd|dkrt��fdd�|D��}|S|dkr$t��fdd�|D��}|St��fdd�|D��}|S)z�Get p-values from stats values given an H0 distribution.177 178    For each stat compute a p-value as percentile of its statistics179    within all statistics in surrogate data180    r3csg|]	}t��|k��qSr$�r�mean�r`r��H0r$r%rd^�z(_pval_from_histogram.<locals>.<listcomp>rcsg|]	}t��|k��qSr$r�r�r�r$r%rd`r�cs"g|]
}t�t��t|�k��qSr$)rr�rGr�r�r$r%rdb�"r�)�Tr�r�Zpvalr$r�r%�_pval_from_histogramVs��r�cs�t���s	td���jd|kr�����St|�jd�\}}||ks(|dkr3td�jd�d�����������fdd�tt	�j181�d�D���S)Nz?If adjacency matrix is given, it must be a SciPy sparse matrix.rzadjacency (len a	) must be of the correct size, i.e. be equal to or evenly divide the number of tests ({n_tests}).182 183If adjacency was computed for a source space, try using the fwd["src"] or inv["src"] as some original source space vertices can be excluded during forward computationcs*g|]}�j�j|�j|d��qS�r)r"�indptrr��r�r$r%rdzs��z$_setup_adjacency.<locals>.<listcomp>r)rr�r�rOZtocoortZ	transposeZtocsrr:r*r�)r��n_testsrnZ	got_times�modr$r�r%�_setup_adjacencygs"184���185�r�cs��j\}}�dur|�krd�tjt|186�tjd�}�dur'��fdd�|D�}t|187�D]�\}��dus5J��fdd�|D�}�durP�fdd�|D�}||�}nGtj|�jd�}td|��D]8}t|�|�|}t|�D]\}}�||||�f||dd�d|�f<qm||�}|d|�||||�<q^|dur�||_t	||||||||	d�}|d}t|�dkr�t�188|�||<nd||<|
�|d�q+|S)	NrWcs(g|]}tjt�|��f�jd��qS)rW)rr�r*rX�r`r )�X_full�buffer_sizer$r%rd�s�z$_do_permutations.<locals>.<listcomp>csg|]}�|�qSr$r$r�)rr$r%rd��csg|]189}�|dd�f�qSrBr$)r`r�)r�r$r%rd�rhr�r�r�r.r�r�r�rHr)rOrr�r*�doublerjrXr:r)r�r(�update)r��slicesr�r�r��stat_funr.r�r�rH�orders�sample_shaper��progress_bar�n_samp�n_vars�max_cluster_sumsZX_buffer�seed_idxZidx_shuffle_listZX_shuffle_list�190t_obs_surr�pos�191n_var_loopr�r��tmpr��perm_clusters_sumsr$)r�r�rr%�_do_permutations�sN192�193*�194r�cCs�|j\}}|dusJ�|dur||krd}tjt|195�tjd�}|dur-tj||f|jd�}t|196�D]�\}}t|tj�s=J�|j	|ksDJ�d|dd�df�197t�d}t�t�
t�|�d��sctd��|dur|jjrx||9}||�}||9}nG|||�}n@tj||jd�}td||�D]1}t|||�|}||dd�|||�f|dd�d|�f<||�}|d|�||||�<q�|dur�||_t||||||||	d�}|d}t|�dkr�t�t�|��}||||<nd||<|
�|d�q1|S)NrWrrzsigns from rng must be +/- 1rr�)rOrr�r*r�rXrjryr�rQ�astyper4r��equalrGr��flagsZ	writeabler:r)r�r5r�)�Xr�r�r�r�r�r.r�r�rHr�r�r�r�r�r�r�Z
X_flip_bufferr�r�signsr�r�r�r�r�r�Zidx_maxr$r$r%�_do_1samp_permutations�sX198199,�200r�cCs�d|}t�||ft|��}|�|�|d}|||d�df<td|�D]}|ddd�|df}||d|�|f<|||d�|f<q$|S)a�Ndim permutations with repetitions of (a,b).201 202    Returns an array with all the possible permutations with repetitions of203    (0,1) in ndim dimensions.  The array is shaped as (2**ndim,ndim), and is204    ordered with the last index changing fastest.  For examble, for ndim=3:205 206    Examples207    --------208    >>> bin_perm_rep(3)209    array([[0, 0, 0],210           [0, 0, 1],211           [0, 1, 0],212           [0, 1, 1],213           [1, 0, 0],214           [1, 0, 1],215           [1, 1, 0],216           [1, 1, 1]])217    rNrr)rr�r��fillr:)r��a�bZnpermsZpermsZ218half_point�jZhalf_colr$r$r%�bin_perm_rep,s219r�cs@d�|dkd}d}t|t�r|dkrtd��|}t|�}||kr0d}t��d|d�}nk�dkrH|j||dd	d220�}�fdd�|D�}nSt�|d�ft�}i}d}�|dk}	||dkr�t|j	|	d
�dk�221t��}222|223|vr�|224||d|	�f<|dkr�|�	�dkr�d||||<d||225<|d7}||dksb|||fS)zGet the 1samp orders.rrr�r�z(n_permutations as a string must be "all"z
 (exact test)�F)�replacecs&g|]}tjt�|d��td��qS)rrW)rZfromiterZbinary_reprr4r���	n_samplesr$r%rd`s�z%_get_1samp_orders.<locals>.<listcomp>)rQr�N)ry�strr�r4r��choicerrir��uniformr�)r��n_permutationsr��rngZ	max_perms�extrar��hashesr<Zuse_samplesr�r$r�r%�_get_1samp_ordersPs:226227��228rc*std|ddg�td�gd��t�t�s;t����dkr!�dks1�dkr)�dks1�dkr;�dkr;td��d�����d	d229�|D�}|djd}|djd}|djdd��	|D]}|jdd��	krjtd
��q[dd230�|D�}|djd}�dur��dur�t�||��|	dur�|	j|ks�td���|�}t|t	j231d�t�dt	�
|��dt	�|�����dur�t	�|�}td|��D]����fdd232�|D��|����<q�t	�||k�s�td�d�|jt	��	�kr�td|j�d��d�	�����du�s�du�r�	|_|	du�rt	�|	�}nd}|
du�r&�du�r&�du�r&t�|��nd�t�d�t|����|��
dd�	}|\}}�	|_t�t�}|�r^|�|j�t	�|�}dd233�t|j�D�}t�dt|��dt|�����du�rw�du�sz|�r�|dk�r��dut�	�dk@}t|||�}n|dk�r�t||j�}d}t|��~t|�dk�r�t}|d�d�234t||���\}}}n9t |�}t!}t	j"|dd ��d!d235�|D�}t	�#dgt	�$|����fd"d236�tt|��D��237��fd#d238�t|d�D�}�t%||dd$�\}�}t|�dk�rtd%�|t	�&g�t	�&g�t	�&g�fSd} d}!d}"d}#| dk�r�|du�r;|"du�r8t	�'||"��n|�n|"�t(tt|��d&|��d'��%�|��������	�239��
���fd(d)�t)||dd*�D��}$Wd�n	1�sswY�d+k�r�|�
�}%n�dk�r�|��}%nt*|���}%|$�+d|%g�t	�"|$�}$t�,d,�t-||$��}&t	�.|&|240k�d}'|'j|!} |'j}!t	j/|t0d-�}"|'D]	}(d|"||(<�qÈdu�rڈdu�rڈ	|"_|#d7}#|241dk�r�|#dk�r�d.nd})t�d/|#| |)t| ��| dk�s't1|�	�}|||&|$fS)0z�Aux Function.242 243    Note. X is required to be a list. Depending on the length of X244    either a 1 sample t-test or an F test / more sample permutation scheme245    is elicited.246    �out_typer�r"r�r�rz+incompatible tail and threshold signs, got z and cSs,g|]}|jdkr|dd�tjfn|�qS)rN)r�rZnewaxis�r`r6r$r$r%rd�s,z-_permutation_cluster_test.<locals>.<listcomp>rNz#All samples mush have the same sizecSs"g|]
}t�||jddf��qS)rr3)rr�rOrr$r$r%rd�r�Fz&exclude must be the same shape as X[0]zreturn value of stat_funzstat_fun(H1): min=z max=cs$g|]}|dd�����f�qSrBr$r)r�r�r$r%rd�s$zVProvided stat_fun does not treat variables independently. Setting buffer_size to None.zt_obs.shape z provided by stat_fun z) is not compatible with the sample shape TuRunning initial clustering …)r.r�r�rHr�cSsg|]}t�|g��qSr$r�r�r$r$r%rd�rezFound z clusterr��ZaxiscSsg|]}|jd�qSrfr�rr$r$r%rdr�cs"g|]
}t�|�|d��qSr�)r�)r`ro)�247splits_idxr$r%rdr�csg|]	}��t����qSr$)Zpermutationr*)r`r�)r�rr$r%rdr�)rz?No clusters found, returning empty H0, clusters, and cluster_pvZ	Permuting)�iterableZmesgc3s<�|]\}}����
���	����248|����|��VqdSrB)Zsubset)r`r�r)r�r�r�r.�my_do_perm_funcr�r�r�r�r�rHr��this_includer�r$r%r�5s(���249�z,_permutation_cluster_test.<locals>.<genexpr>)r�r3zComputing cluster p-valuesrWzadditional z[Step-down-in-jumps iteration #%i found %i %scluster%s to exclude from subsequent iterations)2rryr�r�r�rOr�rQrrr�rr�r)r(Z250zeros_liker:r�r�prodrJ�_get_partitions_from_adjacencyr�r�rFr*rr�r�rr�rr4r�rYr;Zcumsumr251rZr�rrrG�insert�debugr�rrNrP�_reshape_clusters)*r�r�r�r�r�r��n_jobs�seedr.�exclude�step_down_prHr�check_disjointr�r�rnr6r��t_obsZt_obs_bufferr�r�rSZ
cluster_statsr�r�rZdo_perm_funcr�Zn_samples_per_condition�parallelZ	n_removedZ
total_removedZstep_down_includeZn_step_downsr��origZ252cluster_pv�	to_remover�Za_textr$)r�r�r�r.r	r�r�r�rr�r�rr�rHr�r253r�r%�_permutation_cluster_testzs2254��"255����256257�258259�260��261262263�&264��265266267268269270271272�273274Br�withinc	CsB|dkrK|dur?|dur|turtd|���dd|dk}t|�}t�||d�}t�|�dkr6|}t�d|d���|durEtn|}||fS|d	ksQJ�|dur�|dure|t	uretd275|���n|dkrmtd�d}t|�d}t�276dd
�|D��t|�}t�d|||�}t�d|d���|dur�t	n|}||fS)z(Check the stat_fun and threshold values.rNzOAutomatic threshold is only valid for stat_fun=None (or ttest_1samp_no_p), got 皙�����?rrzUsing a threshold of z.6f�betweenzGAutomatic threshold is only valid for stat_fun=None (or f_oneway), got z4Ignoring argument "tail", performing 1-tailed F-testcSsg|]}t|��qSr$rgrr$r$r%rd�r�z_check_fun.<locals>.<listcomp>��?)rrr*�tstatZppfrrFrr�rrC�fstat)	r�r�r�r��kind�p_threshr��dfnZdfdr$r$r%�277_check_funnsB�����r#�r"��cCs:t||||d�\}}t||||||||||	|278|||
|d�S)a�Cluster-level statistical permutation test.279 280    For a list of :class:`NumPy arrays <numpy.ndarray>` of data,281    calculate some statistics corrected for multiple comparisons using282    permutations and cluster-level correction. Each element of the list ``X``283    should contain the data for one group of observations (e.g., 2D arrays for284    time series, 3D arrays for time-frequency power values). Permutations are285    generated with random partitions of the data. For details, see286    :footcite:p:`MarisOostenveld2007,Sassenhagen2019`.287 288    Parameters289    ----------290    X : list of array, shape (n_observations, p[, q][, r])291        The data to be clustered. Each array in ``X`` should contain the292        observations for one group. The first dimension of each array is the293        number of observations from that group; remaining dimensions comprise294        the size of a single observation. For example if ``X = [X1, X2]``295        with ``X1.shape = (20, 50, 4)`` and ``X2.shape = (17, 50, 4)``, then296        ``X`` has 2 groups with respectively 20 and 17 observations in each,297        and each data point is of shape ``(50, 4)``. Note: that the298        *last dimension* of each element of ``X`` should correspond to the299        dimension represented in the ``adjacency`` parameter300        (e.g., spectral data should be provided as301        ``(observations, frequencies, channels/vertices)``).302    %(threshold_clust_f)s303    %(n_permutations_clust_int)s304    %(tail_clust)s305    %(stat_fun_clust_f)s306    %(adjacency_clust_n)s307    %(n_jobs)s308    %(seed)s309    %(max_step_clust)s310    %(exclude_clust)s311    %(step_down_p_clust)s312    %(f_power_clust)s313    %(out_type_clust)s314    %(check_disjoint_clust)s315    %(buffer_size_clust)s316    %(verbose)s317 318    Returns319    -------320    F_obs : array, shape (p[, q][, r])321        Statistic (F by default) observed for all variables.322    clusters : list323        List type defined by out_type above.324    cluster_pv : array325        P-value for each cluster.326    H0 : array, shape (n_permutations,)327        Max cluster level stats observed under permutation.328 329    Notes330    -----331    %(threshold_clust_f_notes)s332 333    References334    ----------335    .. footbibliography::336    r�r�r�r�r�r�r�rrr.rrrHrrr��r#r�r�r�r�r�r�r�rrr.rrrHrrr�rr$r$r%�permutation_cluster_test�s$N�r)cCs:t||||�\}}t|g|||||||||	|337|||
|d�S)a�338Non-parametric cluster-level paired t-test.339 340    For details, see :footcite:p:`MarisOostenveld2007,Sassenhagen2019`.341 342    Parameters343    ----------344    X : array, shape (n_observations, p[, q][, r])345        The data to be clustered. The first dimension should correspond to the346        difference between paired samples (observations) in two conditions.347        The subarrays ``X[k]`` can be 1D (e.g., time series), 2D (e.g.,348        time series over channels), or 3D (e.g., time-frequencies over349        channels) associated with the kth observation. For spatiotemporal data,350        see also :func:`mne.stats.spatio_temporal_cluster_1samp_test`.351    %(threshold_clust_t)s352    %(n_permutations_clust_all)s353    %(tail_clust)s354    %(stat_fun_clust_t)s355    %(adjacency_clust_1)s356    %(n_jobs)s357    %(seed)s358    %(max_step_clust)s359    %(exclude_clust)s360    %(step_down_p_clust)s361    %(t_power_clust)s362    %(out_type_clust)s363    %(check_disjoint_clust)s364    %(buffer_size_clust)s365    %(verbose)s366 367    Returns368    -------369    t_obs : array, shape (p[, q][, r])370        T-statistic observed for all variables.371    clusters : list372        List type defined by out_type above.373    cluster_pv : array374        P-value for each cluster.375    H0 : array, shape (n_permutations,)376        Max cluster level stats observed under permutation.377 378    Notes379    -----380    From an array of paired observations, e.g. a difference in signal381    amplitudes or power spectra in two conditions, calculate if the data382    distributions in the two conditions are significantly different.383    The procedure uses a cluster analysis with permutation test384    for calculating corrected p-values. Randomized data are generated with385    random sign flips. See :footcite:`MarisOostenveld2007` for more386    information.387 388    Because a 1-sample t-test on the difference in observations is389    mathematically equivalent to a paired t-test, internally this function390    computes a 1-sample t-test (by default) and uses sign flipping (always)391    to perform permutations. This might not be suitable for the case where392    there is truly a single observation under test; see :ref:`disc-stats`.393    %(threshold_clust_t_notes)s394 395    If ``n_permutations`` exceeds the maximum number of possible permutations396    given the number of observations, then ``n_permutations`` and ``seed``397    will be ignored since an exact test (full permutation test) will be398    performed (this is the case when399    ``n_permutations >= 2 ** (n_observations - (tail == 0))``).400 401    If no initial clusters are found because all points in the true402    distribution are below the threshold, then ``clusters``, ``cluster_pv``,403    and ``H0`` will all be empty arrays.404 405    References406    ----------407    .. footbibliography::408    r&r'r(r$r$r%�permutation_cluster_1samp_test�s$Z�r*cCsX|	durtt�|jdd��|jd|	d�}nd}t|||||||||||409|||
|d�S)u�Non-parametric cluster-level paired t-test for spatio-temporal data.410 411    This function provides a convenient wrapper for412    :func:`mne.stats.permutation_cluster_1samp_test`, for use with data413    organized in the form (observations × time × space),414    (observations × frequencies × space), or optionally415    (observations × time × frequencies × space). For details, see416    :footcite:p:`MarisOostenveld2007,Sassenhagen2019`.417 418    Parameters419    ----------420    X : array, shape (n_observations, p[, q], n_vertices)421        The data to be clustered. The first dimension should correspond to the422        difference between paired samples (observations) in two conditions.423        The second, and optionally third, dimensions correspond to the424        time or time-frequency data. And, the last dimension should be spatial.425    %(threshold_clust_t)s426    %(n_permutations_clust_all)s427    %(tail_clust)s428    %(stat_fun_clust_t)s429    %(adjacency_clust_st1)s430    %(n_jobs)s431    %(seed)s432    %(max_step_clust)s433    spatial_exclude : list of int or None434        List of spatial indices to exclude from clustering.435    %(step_down_p_clust)s436    %(t_power_clust)s437    %(out_type_clust)s438    %(check_disjoint_clust)s439    %(buffer_size_clust)s440    %(verbose)s441 442    Returns443    -------444    t_obs : array, shape (p[, q], n_vertices)445        T-statistic observed for all variables.446    clusters : list447        List type defined by out_type above.448    cluster_pv : array449        P-value for each cluster.450    H0 : array, shape (n_permutations,)451        Max cluster level stats observed under permutation.452 453    Notes454    -----455    %(threshold_clust_t_notes)s456 457    References458    ----------459    .. footbibliography::460    Nrr3T�r�r�r�r�r�rrr.rrrHrrr�)�_st_mask_from_s_indsrrrOr*�r�r�r�r�r�r�rrr.Zspatial_excluderrHrrr�rrr$r$r%�"spatio_temporal_cluster_1samp_testas,H��r.cCs`|	durtt�|djdd��|djd|	d�}nd}t|||||||||||461|||
|d�S)uGNon-parametric cluster-level test for spatio-temporal data.462 463    This function provides a convenient wrapper for464    :func:`mne.stats.permutation_cluster_test`, for use with data465    organized in the form (observations × time × space),466    (observations × time × space), or optionally467    (observations × time × frequencies × space). For more information,468    see :footcite:p:`MarisOostenveld2007,Sassenhagen2019`.469 470    Parameters471    ----------472    X : list of array, shape (n_observations, p[, q], n_vertices)473        The data to be clustered. Each array in ``X`` should contain the474        observations for one group. The first dimension of each array is the475        number of observations from that group (and may vary between groups).476        The second, and optionally third, dimensions correspond to the477        time or time-frequency data. And, the last dimension should be spatial.478        All dimensions except the first should match across all groups.479    %(threshold_clust_f)s480    %(n_permutations_clust_int)s481    %(tail_clust)s482    %(stat_fun_clust_f)s483    %(adjacency_clust_stn)s484    %(n_jobs)s485    %(seed)s486    %(max_step_clust)s487    spatial_exclude : list of int or None488        List of spatial indices to exclude from clustering.489    %(step_down_p_clust)s490    %(f_power_clust)s491    %(out_type_clust)s492    %(check_disjoint_clust)s493    %(buffer_size_clust)s494    %(verbose)s495 496    Returns497    -------498    F_obs : array, shape (p[, q], n_vertices)499        Statistic (F by default) observed for all variables.500    clusters : list501        List type defined by out_type above.502    cluster_pv: array503        P-value for each cluster.504    H0 : array, shape (n_permutations,)505        Max cluster level stats observed under permutation.506 507    Notes508    -----509    %(threshold_clust_f_notes)s510 511    References512    ----------513    .. footbibliography::514    Nrrr3Tr+)r,rrrOr)r-r$r$r%�spatio_temporal_cluster_test�s,J&��r/cCs@tj||ftd�}d|dd�|f<|��}|durt�|�}|S)a�Compute mask to apply to a spatio-temporal adjacency matrix.515 516    This can be used to include (or exclude) certain spatial coordinates.517    This is useful for excluding certain regions from analysis (e.g.,518    medial wall vertices).519 520    Parameters521    ----------522    n_times : int523        Number of time points.524    n_vertices : int525        Number of spatial points.526    vertices : list or array of int527        Vertex numbers to set.528    set_as : bool529        If True, all points except "vertices" are set to False (inclusion).530        If False, all points except "vertices" are set to True (exclusion).531 532    Returns533    -------534    mask : array of bool535        A (n_times * n_vertices) array of boolean values for masking536    rWTNF)rrirPr�rJ)rn�537n_vertices�verticesZset_asr�r$r$r%r,%s538r,c539Cst|t�r2t�t|��}tjt|�t|�fdd�}tt|��D]540}d||||f<qtj|dd�}n541t�|j	d�}|}t542|dd|�d}t|�dkrxt�t|��d��tjt|�dd�}t
|�D]\}}	|||	<qbt|t�rvt�||�}|St�d	�d543}|S)z@Specify disjoint subsets (e.g., hemispheres) based on adjacency.rPrWTr�rrz disjoint adjacency sets foundr4z No disjoint adjacency sets foundN)ryr9rrNr*rir:rr�rOr�rr�rjZtile)544r�rnr�testZtest_adj�viZpart_clustsr�r<Zpcr$r$r%rEs(545546547548�rcs\t|�dkr,t|dtj�r,|djt�t�kr#�fdd�|D�}|S�fdd�|D�}|S)z<Reshape cluster masks or indices to be of the correct shape.rcsg|]}|����qSr$)r�r��r�r$r%rdfr�z%_reshape_clusters.<locals>.<listcomp>csg|]}t�|���qSr$)rrxr�r4r$r%rdhre)r*ryrr�rXrP)rSr�r$r4r%ras�rrr�	fsaveragecCs�t|dttfd�|durt�d�t�d�g}t}n"t|t�r1tttt	d�|j549}dd�|D�}nttd��t|�t	�}t
dd	�|D��}|\}}	}550}|j\}}
|
|kr_td551|
�d|�d���t�|552|k�d
}t|�d
krrtd��t�|
|f�}t�|
t|�df�}t|�D]+\}}|�d
�|	|\}}|||f|||f<tj
t||�dd�|dd�|df<q�tj
|dd�|dd�d
f<||||||�S)aSAssemble summary SourceEstimate from spatiotemporal cluster results.553 554    This helps visualizing results from spatio-temporal-clustering555    permutation tests.556 557    Parameters558    ----------559    clu : tuple560        The output from clustering permutation tests.561    p_thresh : float562        The significance threshold for inclusion of clusters.563    tstep : float564        The time step between samples of the original :class:`STC565        <mne.SourceEstimate>`, in seconds (i.e., ``1 / stc.sfreq``). Defaults566        to ``1``, which will yield a colormap indicating cluster duration567        measured in *samples* rather than *seconds*.568    tmin : float | int569        The time of the first sample.570    subject : str571        The name of the subject.572    vertices : list of array | instance of SourceSpaces | None573        The vertex numbers associated with the source space locations. Defaults574        to None. If None, equals ``[np.arange(10242), np.arange(10242)]``.575        Can also be an instance of SourceSpaces to get vertex numbers from.576 577        .. versionchanged:: 0.21578           Added support for SourceSpaces.579 580    Returns581    -------582    out : instance of SourceEstimate583        A summary of the clusters. The first time point in this SourceEstimate584        object is the summation of all the clusters. Subsequent time points585        contain each individual cluster. The magnitude of the activity586        corresponds to the duration spanned by the cluster (duration units are587        determined by ``tstep``).588 589        .. versionchanged:: 0.21590           Added support for volume and mixed source estimates.591    Nr1i()Zsurface�volume�mixedcSsg|]}|d�qS)Zvertnor$r�r$r$r%rd�r�z*summarize_clusters_stc.<locals>.<listcomp>)rrcss�|]}t|�VqdSrBrg)r`rvr$r$r%r��r�z)summarize_clusters_stc.<locals>.<genexpr>zNumber of cluster vertices (z') did not match the provided vertices (�)rzcNo significant clusters available. Please adjust your threshold or check your statistical analysis.rr)rr9rrrurryr�r
rr r�r*rCrOr�rr�rirjr�rM)Zclur!rLZtmin�subjectr1�klassZn_vertices_needrrSZ	clu_pvalsr�rnr0Zgood_cluster_indsrKZdata_summaryr<Zcluster_indrTZv_indsr$r$r%�summarize_clusters_stclsJ+592��593�594���595&r;rBr�)T)rNrNNrFr�)rr)Nr$rNNNNrNrrr"Fr%N)rrrr5N)H�numpyrZscipyrrZscipy.sparse.csgraphrZscipy.statsrrrr�fixesrr	rr596Zsource_estimaterrr
Zsource_spacer�utilsrrrrrrrrrZ597parametricrrr&r2r8r?r@rArErIrMrVr^rsrwr�r�r�r�r�r�r�r�r�r�r�r�rrr#r)r*r.r/r,rrr;r$r$r$r%�<module>s,598599600601602603604605	60606070608!609�.5
R610Y$*611u#�a�m�`�612b �