def anova_test(event_vals, param_grid, prob_diff): n_val1 = len(param_grid[0]) n_val2 = len(param_grid[1]) table = [] for param0 in param_grid[0]: sel0 = (event_vals[0]==param0) for param1 in param_grid[1]: sel = sel0 & (event_vals[1]==param1) table.append(prob_diff[sel]) min_len = min([len(p) for p in table]) print 'ANOVA test with', min_len, 'samples' if min_len < 3: return None table = [p[:min_len] for p in table] table = np.stack(table, axis=1) f_vals, p_vals = f_mway_rm(table, [n_val1,n_val2]) return (f_vals, p_vals)
def anova_test(ps_table, param1_name, param2_name): param1_vals = sorted(ps_table[param1_name].unique()) n1 = len(param1_vals) param2_vals = sorted(ps_table[param2_name].unique()) n2 = len(param2_vals) table = [] for val1 in param1_vals: ps_table_val1 = ps_table[ps_table[param1_name]==val1] for val2 in param2_vals: ps_table_val1_val2 = ps_table_val1[ps_table_val1[param2_name]==val2] table.append(list(ps_table_val1_val2['prob_diff'].values)) min_len = min([len(p) for p in table]) print 'ANOVA test with', min_len, 'samples' if min_len < 3: return None table = [p[:min_len] for p in table] table = np.stack(table, axis=1) f_vals, p_vals = f_mway_rm(table, [n1,n2]) return (f_vals, p_vals)
def two_way_rmANOVA(df, model, factor_levels, effect_labels): # TODO use spatio_temporal clust to pool in function to stat_fun f, p = f_mway_rm(data, factor_levels, effects="A*B") # TODO check wether names of results can be overwritten with effect_labels return f, p
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 ANOVA. return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels, effects=effects, return_pvals=return_pvals)[0]
def rm_anova_stat_fun(*args): '''Stat fun that does one-way repeated measures ANOVA.''' from mne.stats import f_mway_rm data = np.stack(args, axis=1) n_factors = data.shape[1] fval, _ = f_mway_rm(data, factor_levels=[n_factors], return_pvals=False) if data.ndim > 3: fval = fval.reshape(data.shape[2:]) return fval
def stat_fun(*args): # get f-values only. return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels, effects=effects, return_pvals=return_pvals)[0]
# ===== ==== ==== ==== ==== # trial A1B1 A1B2 A2B1 B2B2 # ===== ==== ==== ==== ==== # 1 1.34 2.53 0.97 1.74 # ... ... ... ... ... # 56 2.45 7.90 3.09 4.76 # ===== ==== ==== ==== ==== # # Now we're ready to run our repeated measures ANOVA. # # Note. As we treat trials as subjects, the test only accounts for # time locked responses despite the 'induced' approach. # For analysis for induced power at the group level averaged TRFs # are required. fvals, pvals = f_mway_rm(data, factor_levels, effects=effects) effect_labels = ['modality', 'location', 'modality by location'] # let's visualize our effects by computing f-images for effect, sig, effect_label in zip(fvals, pvals, effect_labels): plt.figure() # show naive F-values in gray plt.imshow(effect.reshape(8, 211), cmap=plt.cm.gray, extent=[times[0], times[-1], freqs[0], freqs[-1]], aspect='auto', origin='lower') # create mask for significant Time-frequency locations effect = np.ma.masked_array(effect, [sig > .05]) plt.imshow(effect.reshape(8, 211),
# so we have replications * conditions * observations: print(data.shape) # while the iteration scheme used above for assembling the data matrix # makes sure the first two dimensions are organized as expected (with A = # modality and B = location): # # A1B1 A1B2 A2B1 B2B2 # trial 1 1.34 2.53 0.97 1.74 # trial ... .... .... .... .... # trial 56 2.45 7.90 3.09 4.76 # # Now we're ready to run our repeated measures ANOVA. fvals, pvals = f_mway_rm(data, factor_levels, effects=effects) effect_labels = ['modality', 'location', 'modality by location'] # let's visualize our effects by computing f-images for effect, sig, effect_label in zip(fvals, pvals, effect_labels): plt.figure() # show naive F-values in gray plt.imshow(effect.reshape(8, 211), cmap=plt.cm.gray, extent=[times[0], times[-1], frequencies[0], frequencies[-1]], aspect='auto', origin='lower') # create mask for significant Time-frequency locations effect = np.ma.masked_array(effect, [sig > .05]) plt.imshow(effect.reshape(8, 211), cmap='RdBu_r', extent=[times[0], times[-1], frequencies[0], frequencies[-1]], aspect='auto', origin='lower')
# hf = plt.plot(times, T_obs, 'g') # plt.xlabel("time (ms)") # plt.ylabel("t-values") # plt.show() # plt.savefig(C.pictures_path_Source_estimate+ 't-test_timeseries_abs_'+lb[j]+'_.png') # # # # plt.close('all') ############################################################################### # ## 2way repeated measure fvals, pvals = f_mway_rm(S, factor_levels=[2,2], effects='all') thresh = mne.stats.f_threshold_mway_rm(18,factor_levels=[2,2], effects='all') # T = np.arange(0,550) my_colors = ['darkblue', 'darkgreen','darkred'] rect_colors = ['lightsteelblue', 'paleturquoise','lightcoral'] y_label =['SD/LD', 'ATLs', 'SD/LD by ATLs'] p_thresh = np.repeat(0.05,len(T),axis=0) plt.figure() for k in np.arange(1,4):
def stat_fun(*args): return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels, effects=effects, return_pvals=False)[0]
c=wav, f0=fr[0], f1=fr[-1], d=spacing)) stc_temp = morph.apply(stc_temp) for lab_idx, lab in enumerate(labels): X_temp = mne.extract_label_time_course(stc_temp, lab, fs_src, mode="pca_flip") X[lab_idx][idx].append(X_temp.mean(axis=-1).squeeze()) idx += 1 X = [[(np.array(x) * 1e+26).astype(np.float32) for x in xx] for xx in X] del X_temp, morph, src X = np.array(X).swapaxes(0, 2) result = f_mway_rm(X, factor_levels=factor_levels, effects=effects)[0][0] sig_areas = np.where(result[1] < 0.05)[0] if sig_areas.size == 0: continue for sa in np.nditer(sig_areas): title = "{}_{}".format(k, labels[sa].name) figures.append(mlab.figure(title)) brains.append( Brain('fsaverage', 'both', 'inflated', alpha=0.7, subjects_dir=subjects_dir, figure=figures[-1])) brains[-1].add_annotation(parc, color="black") brains[-1].add_label(labels[sa]) plt.figure()
def stat_fun_Interaction(*args): return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels, effects='A:B', correction=correction, return_pvals=False)[0]
X_LD = matrix_mirror(np.swapaxes(C.im_coh_ld.copy(), 2, 0)) X = np.zeros([18, 8, 6, 6]) X[:, 0, :, :] = X_SD[:, 0, 0, :, :] X[:, 1, :, :] = X_SD[:, 0, 1, :, :] X[:, 2, :, :] = X_SD[:, 1, 0, :, :] X[:, 3, :, :] = X_SD[:, 1, 1, :, :] X[:, 4, :, :] = X_LD[:, 0, 0, :, :] X[:, 5, :, :] = X_LD[:, 0, 1, :, :] X[:, 6, :, :] = X_LD[:, 1, 0, :, :] X[:, 7, :, :] = X_LD[:, 1, 1, :, :] factor_levels = [2, 2, 2] effects = 'all' Y = X.copy().reshape(18, 8, 6 * 6) fvalss, pvalss = f_mway_rm(Y, factor_levels, effects=effects) for m in np.arange(0, 35, 7): pvalss[:, m] = 1 fvalss[:, m] = 0 thresh = mne.stats.f_threshold_mway_rm(18, factor_levels, effects=effects) effect_labels = ['SD/LD', 'Freq', 'SD/LD by freq','time','SD/LD by time',\ 'freq by time','all'] # let's visualize our effects by computing f-images for effect, sig, effect_label in zip(fvalss, pvalss, effect_labels): plt.figure() # show naive F-values in gray plt.imshow(effect.reshape(6, 6), cmap=plt.cm.RdBu_r, extent=[1, 6, 1, 6],
factor_levels = [2, 2] # number of levels in each factor effects = 'A*B' n_freqs = len(freqs) times = 1e3 * this_epoch.times[::decim] n_times = len(times) epochs_power = epochs_power.reshape(n_replications, n_conditions, n_freqs * n_times) epochs_itc = epochs_itc.reshape(n_replications, n_conditions, n_freqs * n_times) print 'epochs_power', epochs_power.shape print 'epochs_itc', epochs_itc.shape ###f-map for induced power fvals, pvals = f_mway_rm(epochs_power, factor_levels, effects=effects) effect_labels = ['word', 'session', 'word by session'] for effect, sig, effect_label in zip(fvals, pvals, effect_labels): print effect_label, effect, sig plt.figure() # show naive F-values in gray plt.imshow(effect.reshape(len(freqs), 601), cmap=plt.cm.gray, extent=[times[0], times[-1], freqs[0], freqs[-1]], aspect='auto', origin='lower') # create mask for significant Time-frequency locations effect = np.ma.masked_array(effect, [sig > .05]) plt.imshow(effect.reshape(len(freqs), 601), cmap='RdBu_r',
def stat_fun_rm1wayANOVA(*args): return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=[len(levellist)], effects='A', correction=True, return_pvals=False)[0]