#######looking at cluster-level significance
###for induced power
epochs_power = np.swapaxes(np.asarray(epochs_power), 1, 0)
effects = 'A:B'


def stat_fun(*args):
    return f_mway_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_mway_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)

mask, _ = fdr_correction(pvals[2])
T_obs_plot2 = np.ma.masked_array(T_obs, np.invert(mask))

plt.figure()
###############################################################################
# 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_mway_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).

# as we only have one hemisphere we need only need half the connectivity
print('Computing connectivity.')
connectivity = mne.spatial_src_connectivity(src[:1])

#    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_mway_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]

###############################################################################
# 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_mway_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_mway_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',
    plt.title(r"Time-locked response for '%s' " % (effect_label))
    plt.show()

effects = 'A:B'


def stat_fun(*args):
    return f_mway_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.001  # set threshold rather high to save some time
f_thresh = f_threshold_mway_rm(13, factor_levels, effects, pthresh)
tail = 1  # f-test, so tail > 0
n_permutations = 10000  # Save some time (the test won't be too sensitive ...)
T_obs, clusters, cluster_p_values, h0 = mne.stats.permutation_cluster_test(
    cond,
    stat_fun=stat_fun,
    threshold=f_thresh,
    tail=tail,
    n_jobs=1,
    n_permutations=n_permutations,
    buffer_size=None)

good_clusters = np.where(cluster_p_values < .05)[0]
T_obs_plot = np.ma.masked_array(T_obs,
                                np.invert(clusters[np.squeeze(good_clusters)]))
Example #6
0
spacing = 4
n_jobs = 4
parc = "RegionGrowing_70"

with open("{}{}_apriori".format(proc_dir, parc), "rb") as f:
    labels = pickle.load(f)

conds = ["audio", "visual", "visselten"]
conds = ["audio", "visual"]
wavs = ["4000fftf", "4000Hz", "7000Hz", "4000cheby"]
effect_idx = 0
factor_levels = [len(conds), len(wavs)]
effects = ["A", "B", "A:B"]
return_pvals = False
p_thresh = 0.001
threshold = f_threshold_mway_rm(len(subjs), [3, 4], effects[effect_idx],
                                p_thresh)
threshold = dict(start=0, step=0.1)
cond_str = conds[0]
for c in conds[1:]:
    cond_str += "_" + c
thresh_str = "tfce" if isinstance(threshold, dict) else p_thresh

# get connectivity
fs_src = mne.read_source_spaces("{}{}_ico{}-src.fif".format(
    proc_dir, "fsaverage", spacing))
cnx = mne.spatial_src_connectivity(fs_src)
#del fs_src
exclude = np.load("{}fsaverage_ico{}_exclude.npy".format(proc_dir, spacing))
results = []
figures = []
brains = []
Example #7
0
def grouplevel_spatial_stats(ListSubj,condition,method,mod,twin,clust_p):
    
    wdir = "/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG"
        
    # load a specific STC morphed on fsaverage to get shape info
    stc0_path = (wdir + '/' + ListSubj[0] + '/mne_python/STCS/IcaCorr_' + mod + 
               '_' + ListSubj[0] + '_' + condition[0] + '_pick_oriNone_' + 
                method + '_ico-5-fwd-fsaverage.fif-rh.stc')
    stc0      = mne.read_source_estimate(stc0_path)
    stc0.crop(-0.2,2.5)
    ncond     = len(condition)
    nsub      = len(ListSubj)
    ntimes    = len(stc0.times)    
    nvertices = stc0.data.shape[0]        
            
    # average individual STCs morphed on fsaverage for each cond
    AVG_STC_cond  = np.empty([nvertices, ntimes,  nsub, ncond])
    
    for s,subj in enumerate(ListSubj):            
        for c,cond in enumerate(condition):
            stc_path = (wdir + '/' + subj + '/mne_python/STCS/IcaCorr_' + mod +
                        '_' + subj + '_' + cond + '_pick_oriNone_' + 
                        method + '_ico-5-fwd-fsaverage.fif-rh.stc')
            stc = mne.read_source_estimate(stc_path) 
            stc.crop(-0.2,2.5)
            AVG_STC_cond[:,:,s,c] = stc.data

    # optional: restrict computation to temporal window of interest
    lower_bound = np.where(stc0.times >= twin[0])[0][0]
    upper_bound = np.where(stc0.times >= twin[1])[0][0]
    
    con = mne.spatial_tris_connectivity(grade_to_tris(5))
    # array of shapes (obs, time, vertices)
    X = []
    for c,cond in enumerate(condition):
        X.append(np.mean(np.transpose(AVG_STC_cond[:,lower_bound: upper_bound,:,c], [2, 1, 0]),1))
 
    effects = 'A'
    factor_levels = [3]
   
    # get f-values only.
    def mystat_fun(*args):
        return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels,
                     effects=effects, return_pvals=False)[0]

    p_threshold = clust_p
    f_threshold = f_threshold_mway_rm(nsub,factor_levels = factor_levels, effects = effects,pvalue= p_threshold)
    
    F_obs, clu, clu_p_val, H0  = mne.stats.permutation_cluster_test(X, threshold=f_threshold,stat_fun=mystat_fun,
                                                                        connectivity=con, n_jobs=1,
                                                                    verbose=True, seed = 666)                                       
    
    wdir = "/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/"
    save_path = (wdir+'GROUP/mne_python/Plot_STATS/' + "_vs_".join(condition))  
    
    if not os.path.exists(save_path):
        os.makedirs(save_path)  
    
    # save cluster stats
    spatial_clust_F = np.array((F_obs, clu, clu_p_val, H0))
    np.save((save_path+'/' + mod + '_' +'cluster_stats_f_'+ "_vs_".join(condition)),
            spatial_clust_F)        
    
    # save F-Map                                                
    tmp = F_obs
    tmp = tmp[:,np.newaxis]
    fsave_vertices = [np.arange(10242), np.arange(10242)]
    stc_Ftest = mne.SourceEstimate(tmp,fsave_vertices,0,stc.tstep) 
    stc_Ftest.save((save_path + '/fmap'  + mod + '_' + "_vs_".join(condition)))
    
    return F_obs, clu, clu_p_val, H0,stc0   
Example #8
0
# y_label=['SD/LD' , 'SD/LD by ROIs']
y_label = ['SD/LD', 'ATLs', 'SD/LD by ATLs']
# y_label = ['SD/LD', 'ATLs']

# my_colors = ['mediumblue', 'darkgreen','darkred']
my_colors = ['b', 'g', 'purple']


###############################################################################

# 2-way repeated measure : F-test and cluster-based correction
plt.figure(figsize=(9, 9))
plt.rcParams['font.size'] = '18'

for e, effect in enumerate(effects):
    f_thresh = f_threshold_mway_rm(n_subjects, factor_levels, effects=effect,
                                   pvalue=p_threshold)
    p = 0

    def stat_fun(*args):
        return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels,
                         effects=effect, return_pvals=False)[0]

    # The ANOVA returns a tuple f-values and p-values, we will pick the former.
    tail = 0  # f-test, so tail > 0
    T_obs, clusters, cluster_p_values, h0 = mne.stats.permutation_cluster_test(
        X_SDLD_ATLs, stat_fun=stat_fun, threshold=f_thresh, tail=tail,
        n_jobs=4, n_permutations=n_permutations, buffer_size=None, out_type='mask')

    plt.subplot(len(effects), 1, e+1)
    # plt.legend(loc='upper left')