def clu2STC(fn_list, p_thre=0.05): ''' Generate STCs from significant clusters Parameters ------- fn_list: string The paths of significant clusters. p_thre: float The corrected p_values. ''' for fn_cluster in fn_list: fn_stc_out = fn_cluster[:fn_cluster.rfind('.npz')] + ',pv_%.3f' % ( p_thre) npz = np.load(fn_cluster) clu = npz['clu'] good_cluster_inds = np.where(clu[2] < p_thre)[0] print 'the amount of significant clusters are: %d' % good_cluster_inds.shape fsave_vertices = list(npz['fsave_vertices']) tstep = npz['tstep'].flatten()[0] stc_all_cluster_vis = summarize_clusters_stc(clu, p_thre, tstep=tstep, vertices=fsave_vertices, subject='fsaverage') stc_all_cluster_vis.save(fn_stc_out)
def plot_clusters(subject, CondComb): ############################################################################### def load_object(filename): import pickle with open(filename, 'rb') as output: obj = pickle.load(output) return obj ############################################################################### T_obs, clusters, cluster_p_values, H0 = load_object( '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/' + subject + '/mne_python/CLUST/' + '-'.join([cond for cond in CondComb]) + '/spatio_temporal_cluster_1samp_test') clu = T_obs, clusters, cluster_p_values, H0 stc = mne.read_source_estimate( '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/' + subject + '/mne_python/CLUST/' + '-'.join([cond for cond in CondComb]) + '/spatio_temporal_cluster_1samp_test-forplot-lh.stc') good_cluster_inds = np.where(cluster_p_values < 0.05)[0] if good_cluster_inds.any(): # Now let's build a convenient representation of each cluster, where each # cluster becomes a "time point" in the SourceEstimate stc_all_cluster_vis = summarize_clusters_stc(clu, p_thresh=0.05, tstep=stc.tstep, vertices=stc.vertices, subject=subject) # Let's actually plot the first "time point" in the SourceEstimate, which # shows all the clusters, weighted by duration # blue blobs are for condition A < condition B, red for A > B subjects_dir = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/mri' for hemi in ['lh', 'rh']: brain = stc_all_cluster_vis.plot( hemi=hemi, subjects_dir=subjects_dir, time_label='Duration significant (ms)', smoothing_steps=5) PlotDir = ('/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/' + subject + '/mne_python/BrainMaps/SingleSubjStat/' + '-'.join([cond for cond in CondComb])) if not os.path.exists(PlotDir): os.makedirs(PlotDir) brain.set_data_time_index(0) brain.show_view('lateral') brain.save_image( (PlotDir + '/' + '-'.join([cond for cond in CondComb]) + '_clusters_' + 'lateralview' + hemi + '.png')) brain.show_view('medial') brain.save_image( (PlotDir + '/' + '-'.join([cond for cond in CondComb]) + '_clusters_' + 'medialview' + hemi + '.png'))
def clu2STC(fn_cluster, p_thre=0.05, tstep=None): ''' Generate STCs from significant clusters Parameters ------- fn_cluster: string The filename of significant clusters. p_thre: float The corrected p_values. tstep: float The interval between timepoints. ''' fn_stc_out = fn_cluster[:fn_cluster.rfind('.npz')] + ',p_corrected%.3f' % (p_thre) npz = np.load(fn_cluster) if tstep is None: tstep = npz['tstep'].flatten()[0] clu = npz['clu'] fsave_vertices = list(npz['fsave_vertices']) stc_all_cluster_vis = summarize_clusters_stc(clu, p_thre, tstep=tstep, vertices=fsave_vertices, subject='fsaverage') if fn_stc_out is None: return stc_all_cluster_vis stc_all_cluster_vis.save(fn_stc_out)
def clu2STC(fn_cluster, p_thre=0.05, tmin=0.): # Now let's build a convenient representation of each cluster, where each # cluster becomes a "time point" in the SourceEstimate fn_stc_out = fn_cluster[:fn_cluster.rfind('.npz')] + ',temp_%.3f,tmin_%.3f' %(p_thre, tmin) npz = np.load(fn_cluster) tstep = npz['tstep'].flatten()[0] clu = npz['clu'] fsave_vertices = list(npz['fsave_vertices']) stc_all_cluster_vis = summarize_clusters_stc(clu, p_thre, tstep=tstep, tmin=tmin, vertices=fsave_vertices, subject='fsaverage') if fn_stc_out == None: return stc_all_cluster_vis stc_all_cluster_vis.save(fn_stc_out)
def plot_ttest_results_using_pysurfer(patient, cond_name, res, subjects_dir): clu = (res['T_obs'], res['clusters'], res['cluster_p_values'], res['H0']) fsave_vertices = [np.arange(10242), np.arange(10242)] stc_all_cluster_vis = summarize_clusters_stc(clu, tstep=tstep, vertices=fsave_vertices, subject='fsaverage') # Let's actually plot the first "time point" in the SourceEstimate, which # shows all the clusters, weighted by duration # blue blobs are for condition A < condition B, red for A > B brain = stc_all_cluster_vis.plot('fsaverage', 'inflated', 'split', 'mne', views=['lat', 'med'], subjects_dir=subjects_dir, clim='auto',time_label='Duration significant (ms)') brain.set_data_time_index(0) brain.show_view('lateral') print('tada!') brain.save_image(op.join(LOCAL_ROOT_DIR, 'permutation_ttest_results', '{}_{}_clusters.jpg'.format(patient, cond_name))) brain.close()
def permutation_test_on_source_data_with_spatio_temporal_clustering(controls_data, patient_data, patient, cond_name, tstep, n_permutations, inverse_method='dSPM', n_jobs=6): try: print('permutation_test: patient {}, cond {}'.format(patient, cond_name)) connectivity = spatial_tris_connectivity(grade_to_tris(5)) # Note that X needs to be a list of multi-dimensional array of shape # samples (subjects_k) x time x space, so we permute dimensions print(controls_data.shape, patient_data.shape) X = [controls_data, patient_data] p_threshold = 0.05 f_threshold = stats.distributions.f.ppf(1. - p_threshold / 2., controls_data.shape[0] - 1, 1) print('Clustering. thtreshold = {}'.format(f_threshold)) T_obs, clusters, cluster_p_values, H0 = clu =\ spatio_temporal_cluster_test(X, connectivity=connectivity, n_jobs=n_jobs, threshold=10, n_permutations=n_permutations) results_file_name = op.join(LOCAL_ROOT_DIR, 'clusters_results', '{}_{}_{}'.format(patient, cond_name, inverse_method)) np.savez(results_file_name, T_obs=T_obs, clusters=clusters, cluster_p_values=cluster_p_values, H0=H0) # 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] ############################################################################### # Visualize the clusters print('Visualizing clusters.') # Now let's build a convenient representation of each cluster, where each # cluster becomes a "time point" in the SourceEstimate fsave_vertices = [np.arange(10242), np.arange(10242)] stc_all_cluster_vis = summarize_clusters_stc(clu, tstep=tstep, vertices=fsave_vertices, subject='fsaverage') stc_all_cluster_vis.save(op.join(LOCAL_ROOT_DIR, 'stc_clusters', '{}_{}_{}'.format(patient, cond_name, inverse_method)), ftype='h5') # # Let's actually plot the first "time point" in the SourceEstimate, which # # shows all the clusters, weighted by duration # # blue blobs are for condition A != condition B # brain = stc_all_cluster_vis.plot('fsaverage', 'inflated', 'both', # subjects_dir=subjects_dir, clim='auto', # time_label='Duration significant (ms)') # brain.set_data_time_index(0) # brain.show_view('lateral') # brain.save_image('clusters.png') except: print('bummer! {}, {}'.format(patient, cond_name)) print(traceback.format_exc())
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] ############################################################################### # Visualize the clusters print('Visualizing clusters.') # Now let's build a convenient representation of each cluster, where each # cluster becomes a "time point" in the SourceEstimate stc_all_cluster_vis = summarize_clusters_stc(clu, tstep=tstep, vertices=fsave_vertices, subject='fsaverage') # Let's actually plot the first "time point" in the SourceEstimate, which # shows all the clusters, weighted by duration subjects_dir = op.join(data_path, 'subjects') # The brighter the color, the stronger the interaction between # stimulus modality and stimulus location brain = stc_all_cluster_vis.plot('fsaverage', 'inflated', 'lh', subjects_dir=subjects_dir, time_label='Duration significant (ms)') brain.set_data_time_index(0) brain.scale_data_colormap(fmin=5, fmid=10, fmax=30, transparent=True)
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] ############################################################################### # Visualize the clusters # ---------------------- print('Visualizing clusters.') # Now let's build a convenient representation of each cluster, where each # cluster becomes a "time point" in the SourceEstimate stc_all_cluster_vis = summarize_clusters_stc(clu, tstep=tstep, vertices=fsave_vertices, subject='fsaverage') # Let's actually plot the first "time point" in the SourceEstimate, which # shows all the clusters, weighted by duration subjects_dir = op.join(data_path, 'subjects') # The brighter the color, the stronger the interaction between # stimulus modality and stimulus location brain = stc_all_cluster_vis.plot(subjects_dir=subjects_dir, colormap='mne', views='lateral', time_label='Duration significant (ms)') brain.save_image('cluster-lh.png') brain.show_view('medial')
p_threshold = 0.05 t_threshold = -stats.distributions.t.ppf(p_threshold / 2., n_subjects - 1) # print('Clustering.') T_obs, clusters, cluster_p_values, H0 = clu = \ spatio_temporal_cluster_1samp_test(Y, connectivity= connectivity,\ n_jobs=10,threshold=t_threshold,n_permutations=5000,spatial_exclude =\ spatial_exclude,step_down_p=0.05,t_power=1) # print('Visualizing clusters.') fsave_vertices = [np.arange(10242),np.arange(10242)] # # Build a convenient representation of each cluster, where each # # cluster becomes a "time point" in the SourceEstimate stc_all_cluster_vis = summarize_clusters_stc(clu,tstep=tstep*1000,\ vertices = fsave_vertices) idx = stc_all_cluster_vis.time_as_index(times=stc_all_cluster_vis.times) data = stc_all_cluster_vis.data[:, idx] thresh = max(([abs(data.min()) , abs(data.max())])) brain = stc_all_cluster_vis.plot(surface='inflated', hemi='split',subject =\ 'fsaverage', subjects_dir=data_path, clim=dict(kind='value', pos_lims=\ [0,1,101]),size=(800,400),colormap='mne') # brain.save_image(C.pictures_path_Source_estimate+'Clusters_100_'+\ # f'{t_min_crop:.3f}' +'_'+f'{t_max_crop:.3f}_unequalized.png')
T_obs, clusters, cluster_p_values, H0 = clu = \ spatio_temporal_cluster_1samp_test(X, connectivity=connectivity, n_jobs=-1,stat_fun=stat_fun_hat, threshold=t_threshold,n_permutations=1000) print('summary stats') good_cluster_inds = np.where(cluster_p_values < 0.05)[0] for ind in good_cluster_inds: inds_t, inds_v = clusters[ind] inds_t=inds_t*tstep inds_p=cluster_p_values[ind] print(' cluster %d \n p value: %f \n time: %s \n clusters: %s '%(ind,inds_p,inds_t,inds_v)) print('Visualizing clusters.') fsave_vertices = [np.arange(10242), np.arange(10242)] stc_all_cluster_vis = summarize_clusters_stc(clu,tstep=0.005,tmin=stat_tmin, vertices=fsave_vertices,subject='fsaverage',p_thresh=0.05) Folder_name='/nashome1/wexu/Results/MNE_Results/AVLearn/' File_Name=cond_A.replace('/Learning/','_')+cond_B.replace('/Learning/','_')+'Day_' filename= Folder_name+File_Name+str(day)+'.pickle' import pickle with open(filename, 'wb') as f: pickle.dump(stc_all_cluster_vis, f) hemi='lh' for view in ['lat', 'med', 'ros', 'cau', 'dor', 'ven', 'fro', 'par']: brain = stc_all_cluster_vis.plot(hemi=hemi, views=view,smoothing_steps=5,
op.join(results_path, analysis_name + '_sources_clusters.pickle')) is False: print('No cluster file found for ' + analysis_name + ' (not significant?)') else: print('Loading sources clusters for ' + analysis_name + '...') with open( op.join(results_path, analysis_name + '_sources_clusters.pickle'), 'rb') as f: clu = pickle.load(f) # Plot all clusters as a "cluster duration map" stc_all_clusters = summarize_clusters_stc( clu, p_thresh=0.05, tstep=tstep, vertices=fsaverage_vertices, subject='fsaverage') brain = stc_all_clusters.plot( surface='inflated', hemi='split', views=['lat', 'med'], subjects_dir=fsMRI_dir, time_label='Temporal extent (ms)', size=(800, 800), smoothing_steps=5, clim=dict(kind='value', pos_lims=[0, 1, 50]), time_viewer=False) # , show_traces=True) print( op.join(results_path, 'Sources_' + analysis_name + '_allclusters.png'))
from mne.stats import spatio_temporal_cluster_test good_cluster_inds = np.where(cluster_p_values < 0.1)[0] a = spatio_temporal_cluster_test( X, threshold=None, n_permutations=1024, tail=0, stat_fun=None, connectivity=None, verbose=None, n_jobs=1, seed=None, max_step=1, spatial_exclude=None, step_down_p=0, t_power=1, out_type='indices', check_disjoint=False) #%%############################################################################ # Visualize the clusters # ---------------------- print('Visualizing clusters.') # Now let's build a convenient representation of each cluster, where each # cluster becomes a "time point" in the SourceEstimate stc_all_cluster_vis = summarize_clusters_stc(clu, p_thresh=0.1, tstep=tstep, vertices=fsave_vertices, subject=subject) #%% Let's actually plot the first "time point" in the SourceEstimate, which # shows all the clusters, weighted by duration subjects_dir = os.path.join(mainDir) # blue blobs are for condition A < condition B, red for A > B brain = stc_all_cluster_vis.plot(surface='inflated', hemi='both', views='lateral', subjects_dir=subjects_dir, time_label='Duration significant (ms)', size=(800, 800), smoothing_steps=10) # brain.save_image('clusters.png') #%% Statistics """
thresh_pv = 0.02 F_obs, clusters, cluster_pv, H0 = clu = spatio_temporal_cluster_test( [X_high, X_low], # threshold=8, n_permutations=100, adjacency=adjacency, out_type="indices", check_disjoint=True, stat_fun=ttest_ind_no_p, ) stc_all_cluster_vis = summarize_clusters_stc( clu, p_thresh=thresh_pv, # p_thresh=0.05, vertices=src, subject="fsaverage", ) stc_all_cluster_vis stc_all_cluster_vis.plot(subjects_dir=dirs.fsf_subjects, hemi="both") # good_ids = np.where(cluster_pv < thresh_pv)[0] # for good_id in good_ids: # print(freqs[np.unique(clusters[good_id][0])])