# A stat_fun must deal with a variable number of input arguments. def stat_fun(*args): # Inside the clustering function each condition will be passed as # flattened array, necessitated by the clustering procedure. # The ANOVA however expects an input array of dimensions: # subjects X conditions X observations (optional). # The following expression catches the list input and swaps the first and # the second dimension and finally calls the ANOVA function. return f_twoway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels, effects=effects, return_pvals=False)[0] # The ANOVA returns a tuple f-values and p-values, we will pick the former. pthresh = 0.00001 # set threshold rather high to save some time f_thresh = f_threshold_twoway_rm(n_replications, factor_levels, effects, pthresh) tail = 1 # f-test, so tail > 0 n_permutations = 256 # Save some time (the test won't be too sensitive ...) T_obs, clusters, cluster_p_values, h0 = mne.stats.permutation_cluster_test( epochs_power, stat_fun=stat_fun, threshold=f_thresh, tail=tail, n_jobs=1, n_permutations=n_permutations, buffer_size=None) # Create new stats image with only significant clusters good_clusers = np.where(cluster_p_values < .05)[0] T_obs_plot = np.ma.masked_array(T_obs, np.invert(clusters[np.squeeze(good_clusers)])) plt.figure() for f_image, cmap in zip([T_obs, T_obs_plot], [plt.cm.gray, 'RdBu_r']): plt.imshow(f_image, cmap=cmap, extent=[times[0], times[-1], frequencies[0], frequencies[-1]], aspect='auto',
############################################################################### # Compute clustering statistic # To use an algorithm optimized for spatio-temporal clustering, we # just pass the spatial connectivity matrix (instead of spatio-temporal) source_space = grade_to_tris(5) # as we only have one hemisphere we need only need half the connectivity lh_source_space = source_space[source_space[:, 0] < 10242] print('Computing connectivity.') connectivity = spatial_tris_connectivity(lh_source_space) # Now let's actually do the clustering. Please relax, on a small # notebook and one single thread only this will take a couple of minutes ... pthresh = 0.0005 f_thresh = f_threshold_twoway_rm(n_subjects, factor_levels, effects, pthresh) # To speed things up a bit we will ... n_permutations = 128 # ... run fewer permutations (reduces sensitivity) print('Clustering.') T_obs, clusters, cluster_p_values, H0 = clu = \ spatio_temporal_cluster_test(X, connectivity=connectivity, n_jobs=1, threshold=f_thresh, stat_fun=stat_fun, n_permutations=n_permutations, buffer_size=None) # Now select the clusters that are sig. at p < 0.05 (note that this value # is multiple-comparisons corrected). good_cluster_inds = np.where(cluster_p_values < 0.05)[0] ###############################################################################
############################################################################### # Compute clustering statistic # To use an algorithm optimized for spatio-temporal clustering, we # just pass the spatial connectivity matrix (instead of spatio-temporal) source_space = grade_to_tris(5) # as we only have one hemisphere we need only need half the connectivity lh_source_space = source_space[source_space[:, 0] < 10242] print('Computing connectivity.') connectivity = spatial_tris_connectivity(lh_source_space) # Now let's actually do the clustering. Please relax, on a small # notebook and one single thread only this will take a couple of minutes ... pthresh = 0.0005 f_thresh = f_threshold_twoway_rm(n_subjects, factor_levels, effects, pthresh) # To speed things up a bit we will ... n_permutations = 100 # ... run fewer permutations (reduces sensitivity) print('Clustering.') T_obs, clusters, cluster_p_values, H0 = clu = \ spatio_temporal_cluster_test(X, connectivity=connectivity, n_jobs=1, threshold=f_thresh, stat_fun=stat_fun, n_permutations=n_permutations, buffer_size=None) # Now select the clusters that are sig. at p < 0.05 (note that this value # is multiple-comparisons corrected). good_cluster_inds = np.where(cluster_p_values < 0.05)[0] ###############################################################################