def get_colorinfo(r_name, clusters): Nsig = ((clusters.loc[r_name].end_t - clusters.loc[r_name].start_t) / 10 + 1).sum() # if sufficiently many significant effects if Nsig >= 12: # set non-significant effects to NaN src_df_masked = ss.load_src_df(basefile, r_name, clusters, use_basefile) else: # there are not sufficiently many significant effects after FDR, # so don't mask src_df_masked = ss.load_src_df(basefile, r_name, None, use_basefile) if show_measure not in src_df_masked.columns: ss.add_measure(src_df_masked, show_measure) if Nsig >= 12: # make colormap based on distribution of significant effects colorinfo = { 'fmin': src_df_masked[show_measure].abs().min(), 'fmid': src_df_masked[show_measure].abs().median(), 'fmax': src_df_masked[show_measure].abs().max(), 'transparent': True, 'colormap': 'auto' } else: # make colormap based on distribution of all effects colorinfo = { 'fmin': src_df_masked[show_measure].abs().quantile(0.95), 'fmid': src_df_masked[show_measure].abs().quantile(0.99), 'fmax': src_df_masked[show_measure].abs().quantile(0.999), 'transparent': True, 'colormap': 'auto' } return colorinfo
elif r_name == 'accev': x_times = [20, 150, 230, 300] # colorinfo['fmin'] = 0 colorinfo['center'] = 0 elif r_name == 'dot_y': x_times = [120, 180, 330, 410] elif r_name == 'response': if response_aligned: x_times = [-30, 0, 30, 50] else: x_times = [780, 820, 890] # load the selected regressors and mask as desired src_df_masked = pd.concat( [ss.load_src_df(basefile, reg, mask, use_basefile) for reg in regressors], keys=regressors, names=['regressor', 'label', 'time']) times = src_df_masked.index.levels[2] if show_measure not in src_df_masked.columns: ss.add_measure(src_df_masked, show_measure) if basefile.startswith('source_sequential'): # flip sign of all non-nan values in accev for which there is a nan value # in dot_x 100 ms later - these are the effects that are only present in # accumulated evidence, but not in dot_x fliptimes = times[times <= times[-1] - 100] accevvals = src_df_masked.loc[('accev', slice(None), fliptimes), show_measure]
clusters = ss.get_fdrcorr_clusters(basefile, regressors, measure, threshold, measure_cdf, fdr_alpha=0.001, use_basefile=True, perm=perm) print('cluster counts:') print(clusters.label.groupby(level='regressor').count()) #%% load specific regressor r_name = 'response' show_measure = 'mlog10p' src_df_masked = ss.load_src_df(basefile, r_name, clusters, use_basefile=True) brain = Brain('fsaverage', 'both', 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') labels = sv.show_labels_as_data(src_df_masked, show_measure, brain, transparent=True) brain.scale_data_colormap(src_df_masked[show_measure].min(),
#threshold = 0.5 #%% identify significant clusters clusters = ss.get_fdrcorr_clusters(basefile, regressors, measure, threshold, measure_cdf, fdr_alpha=0.001) #%% load specific regressor r_name = 'dot_x' show_measure = 'mu_mean' src_df_masked = ss.load_src_df(basefile, r_name, clusters) brain = Brain('fsaverage', 'both', 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') labels = sv.show_labels_as_data(src_df_masked, show_measure, brain, transparent=True) #brain.scale_data_colormap(src_df_masked[show_measure].min(),
figdir = os.path.expanduser('~/ZIH/texts/BeeMEG/figures') #%% load data r_names = ['dot_x', 'dot_y'] measure = 'tval' make_figures = True # vertices of pre-motor and motor areas, baseline (-0.3, 0), first 5 dots, # trialregs_dot=0, source GLM, sum_dot_y, constregs=0 for 1st dot, # subject-specific normalisation of DM without centering and scaling by std # label_tc normalised across trials, times and subjects basefile = 'source_sequential_201711271306.h5' src_df = pd.concat([ ss.load_src_df(basefile, r_name, use_basefile=True) for r_name in r_names ], keys=r_names, names=['regressor', 'label', 'time']) # this performs FDR-correction across all regressors, vertices and times ss.add_measure(src_df, 'p_fdr') #%% prepare plotting def get_colorinfo(measure, src_df, fdr_alpha=0.01): # find measure value that is the first one with p-value equal or smaller # than fdr_alpha pdiff = src_df.p_fdr - fdr_alpha try: fmid = src_df[pdiff <= 0].sort_values('p_fdr')[measure].abs().iloc[-1]
'second_level')[measure][regressors] second_level = second_level_perms.loc[0] P = second_level_perms.index.get_level_values('permnr').unique().size times = second_level.index.get_level_values('time').unique() labels = second_level.index.get_level_values('label').unique() #%% find brain areas with significant effects in selected time windows clusters = ss.get_fdrcorr_clusters(basefile, regressors, fdr_alpha, use_basefile=True) srcdf_masked = pd.concat([ ss.load_src_df(basefile, reg, clusters, use_basefile=True) for reg in regressors ], keys=regressors, names=['regressor', 'label', 'time']) srcdf_masked.dropna(inplace=True) is_significant = pd.DataFrame( np.zeros((len(regressors) * len(labels), len(wnames)), dtype=bool), index=pd.MultiIndex.from_product([regressors, labels], names=['regressor', 'label']), columns=wnames) for name in wnames: for win in twin[name]: for reg in regressors:
#basefile = 'source_sequential_201807031144.h5' # loose source orientations, but cortex normal currents # label mode = mean_flip, baseline (-0.3, 0), all dots with toolate=-200, # time window [0, 690], exclude time-outs, local normalisation of DM # trialregs_dot=0, accev, sum_dot_y_prev, percupt, constregs=0 for 1st dot, # label_tc normalised across trials but within times and subjects basefile = 'source_sequential_201807041930.h5' regressors = ['dot_x', 'dot_y', 'accev'] measure = 'mean' if measure in ['mu_mean', 'mu_p_large']: second_level = pd.concat( [ss.load_src_df(basefile, reg) for reg in regressors], axis=1, keys=regressors, names=['regressor', 'measure']) second_level = second_level.swaplevel(axis=1).sort_index(axis=1) else: second_level_perms = pd.read_hdf( os.path.join(helpers.resultsdir, basefile), 'second_level') second_level = second_level_perms.loc[0] P = second_level_perms.index.get_level_values('permnr').unique().size first_level = pd.read_hdf(os.path.join(helpers.resultsdir, basefile), 'first_level')
# label_tc normalised across trials, times and subjects basefile = 'source_sequential_201710231824.h5' elif datatype == 'eog': # eog event regression basefile = 'eog_source_201802091232.h5' elif datatype == 'evoked': # evoked sources 300 to 500 ms after response # basefile = 'evoked_source_201802091710.h5' # evoked sources -1000 to 1000 after response basefile = 'evoked_source_201802271701.h5' if len(datatype) > 0 and not datatype.startswith('_'): datatype = '_' + datatype src_df = ss.load_src_df(basefile, r_name, use_basefile=True) ss.add_measure(src_df, 'p_fdr') if measure not in src_df.columns: ss.add_measure(src_df, measure) if make_figures == 'max': make_figures = [src_df[measure].abs().groupby('time').max().idxmax()] elif make_figures == 'mean': make_figures = [src_df[measure].abs().groupby('time').mean().idxmax()] #flips = sv.get_label_sign_flips( # src_df.index.get_level_values('label').map(lambda s: s[:-7]).unique(), # combine=True) #for time in src_df.index.get_level_values('time').unique(): # src_df.loc[(slice(None), time), 'tval'] = ( # src_df.xs(time, level='time').tval * flips).values
use_basefile=True) #%% save all significant r_name clusters to csv-file def to_csv(areas, fname): areas['label'] = areas['label'].map(lambda x: x[:-7]) areas.to_csv(fname) to_csv( clusters.loc[r_name].copy().sort_values('start_t')[[ 'label', 'region', 'start_t', 'end_t', 'log10p' ]], os.path.join(figdir, 'significant_clusters_{}.csv'.format(r_name))) #%% src_df = ss.load_src_df(basefile, r_name, None, use_basefile) if show_measure not in src_df.columns: ss.add_measure(src_df, show_measure) labels = src_df.index.levels[0] def get_average_effects(twin, wname, top=5): winclusters = clusters.loc[r_name] winclusters = winclusters[((winclusters.start_t >= twin[0]) & (winclusters.start_t <= twin[1])) | ((winclusters.end_t >= twin[0]) & (winclusters.end_t <= twin[1]))] areas = winclusters.label.unique()