def brain_plot(r_name, viewtimes, brain): views = {'rh': ['medial', 'parietal'], 'lh': ['parietal', 'medial']} if type(brain) is str: hemi = brain brain = Brain('fsaverage', hemi, 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') else: hemi = brain.geo.keys()[0] sv.show_labels_as_data(src_df_masked.loc[r_name], show_measure, brain, **colorinfo) filepat = os.path.join( figdir, filepat_base + '_{}_{}_%02d.png'.format(hemi, r_name)) brain.save_image_sequence([times.get_loc(t) for t in viewtimes], filepat, montage=views[hemi]) return brain
def make_brain_figures(srcdf, measure, cinfo, filepat_base, top=5): views = {'rh': ['medial', 'lateral'], 'lh': ['lateral', 'medial']} toplabels = {} for hemi in ['lh', 'rh']: brain = Brain('fsaverage', hemi, 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') sv.show_labels_as_data(srcdf, measure, brain, time_label=None, parc=parc, **cinfo) if top: ind = srcdf.index.get_level_values('label').map( lambda x: x.startswith(hemi[0].upper())) topl = srcdf[ind].abs().sort_values( measure, ascending=False).head(top) topl = ( topl[(topl[measure] != 0) & topl[measure].notna()] .index.droplevel('time')) toplabels[hemi] = topl alllabels = mne.read_labels_from_annot( 'fsaverage', parc=parc, hemi=hemi) brain.remove_labels() for label in alllabels: if label.name in topl: brain.add_label(label, borders=1, hemi=hemi, alpha=0.8, color='k') # increase font size of colorbar - this only works by increasing the # colorbar itself and setting the ratio of colorbar to text brain.data['colorbar'].scalar_bar.bar_ratio = 0.35 brain.data['colorbar'].scalar_bar_representation.position = [0.075, 0.01] brain.data['colorbar'].scalar_bar_representation.position2 = [0.85, 0.12] filepat = os.path.join(figdir, filepat_base + '_{}.png'.format(hemi)) brain.save_montage(filepat, views[hemi], colorbar=0) brain.close() infiles = [ os.path.join(figdir, filepat_base + '_{}.png'.format(hemi)) for hemi in ['lh', 'rh']] outfile = os.path.join(figdir, filepat_base + '.png') os.system("montage -tile 2x1 -geometry +0+0 {} {}".format( ' '.join(infiles), outfile)) return toplabels
def plot(r_name, measure, mask=None, clim=[0, 3., 5], perm=0): src_df = second_level.loc[perm].xs(r_name, axis=1, level='regressor') if mask: insig = pd.np.logical_not(second_level.loc[perm, ('significant', r_name)]) src_df[insig.values] = 0 src_df['time'] = 0 src_df.set_index('time', append=True, inplace=True) if measure in ['tval', 'mean']: labels = sv.show_labels_as_data(src_df, measure, brain, colormap='divtrans', clim=clim) else: labels = sv.show_labels_as_data(src_df, measure, brain) return labels
#%% 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(), # src_df_masked[show_measure].median(), # src_df_masked[show_measure].max(), True) brain.scale_data_colormap(0.01, 0.025, 0.06, True) #labels = sv.show_labels_as_data(src_df_masked, 'mu_mean', brain, # transparent=True, # region_aggfun=lambda a: np.max(a, axis=0)) #tv = TimeViewer(brain)
srcdf_normed = srcdf.copy() if normalise: for wname in winnames: normed, _, _ = helpers.normalise_magnitudes( srcdf.loc[(slice(None), wname), :].dropna()) srcdf_normed.loc[normed.index] = normed if interactive: hemi = 'lh' wname = 'build-up' brain = Brain('fsaverage', hemi, 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') sv.show_labels_as_data(srcdf_normed.loc[(slice(None), wname), :], show_measure, brain, time_label=None, parc=parc, **get_colorinfo(srcdf, show_measure)) else: toplstrs = [] for wname in winnames: filepat_base = 'win_brain_{}_{}_{}'.format(r_name, show_measure, wname) df = srcdf_normed.loc[(slice(None), wname), :] if normalise: cinfo = get_colorinfo(srcdf_normed, show_measure) else: cinfo = get_colorinfo(df, show_measure) toplabels = make_brain_figures(df, show_measure, cinfo, filepat_base) toplstrs.append('{}: left - {}; right - {}'.format(
def brain_plot( wname, brain, toplabels=None, save=False, ): views = { 'rh': ['medial', 'lateral'], #{'azimuth': -20, 'elevation': 62, 'roll': -68}], 'lh': ['lateral', 'medial'] } if type(brain) is str: hemi = brain brain = Brain('fsaverage', hemi, 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') else: hemi = brain.geo.keys()[0] srcdf = pd.DataFrame(xma_win[wname].values.copy(), index=pd.MultiIndex.from_product( [labels, [0]], names=['label', 'time']), columns=[measure]) # get colorinfo based on distribution of differences before nulling cinfo = get_colorinfo(srcdf) # set all areas to 0 for which there is insufficient evidence that they # represent evidence; for positive differences supporting dot_x only # consider significant dot_x areas and equally for accev and negative for reg in regressors: if reg == 'accev': siglabels = srcdf[srcdf[measure] < 0].xs(0, level='time').index else: siglabels = srcdf[srcdf[measure] > 0].xs(0, level='time').index siglabels = is_significant.xs( reg, level='regressor').loc[list(siglabels), wname] siglabels = siglabels[~siglabels].index srcdf.loc[(list(siglabels), 0), measure] = 0 sv.show_labels_as_data(srcdf, measure, brain, time_label=None, parc=parc, **cinfo) # if toplabels is not None: # alllabels = mne.read_labels_from_annot( # 'fsaverage', parc=parc, hemi=hemi) # for label in alllabels: # if label.name in toplabels[hemi]: # brain.add_label(label, borders=1, hemi=hemi, alpha=0.8, # color='k') # increase font size of colorbar - this only works by increasing the # colorbar itself and setting the ratio of colorbar to text brain.data['colorbar'].scalar_bar.bar_ratio = 0.35 brain.data['colorbar'].scalar_bar_representation.position = [ 0.075, 0.01 ] brain.data['colorbar'].scalar_bar_representation.position2 = [ 0.85, 0.12 ] if save: filepat = os.path.join( figdir, filepat_base + '_{}_{}.png'.format(hemi, wname)) brain.save_montage(filepat, views[hemi], colorbar=0) return brain, srcdf[srcdf[measure].abs() > cinfo['fmid']].xs( 0, level='time')
def brain_plot(brain, wname, toplabels=False, save=False, backface_culling=True): if r_name in ['dot_x', 'dot_y', 'dot_x_sign']: views = { 'rh': ['medial', 'parietal'], #{'azimuth': -20, 'elevation': 62, 'roll': -68}], 'lh': ['parietal', 'medial'] } elif r_name in ['sum_dot_x', 'response']: views = {'rh': ['medial', 'lateral'], 'lh': ['lateral', 'medial']} if type(brain) is str: hemi = brain brain = Brain('fsaverage', hemi, 'inflated', cortex='low_contrast', subjects_dir=sv.subjects_dir, background='w', foreground='k') else: hemi = brain.geo.keys()[0] if common_color_scale: cinfo = get_colorinfo(avsrcdf) else: cinfo = get_colorinfo(results[wname]['avsrcdf']) sv.show_labels_as_data(results[wname]['avsrcdf'], show_measure, brain, time_label=None, parc=parc, **cinfo) if backface_culling: brain.data['surfaces'][0].actor.property.backface_culling = True if toplabels: brain.remove_labels() alllabels = mne.read_labels_from_annot('fsaverage', parc=parc, hemi=hemi) for label in alllabels: if label.name in results[wname]['toplabels'][hemi]: brain.add_label(label, borders=1, hemi=hemi, alpha=0.8, color='k') if backface_culling: for ldict in brain._label_dicts.values(): ldict['surfaces'][0].actor.property.backface_culling = True # increase font size of colorbar - this only works by increasing the # colorbar itself and setting the ratio of colorbar to text brain.data['colorbar'].scalar_bar.bar_ratio = 0.35 brain.data['colorbar'].scalar_bar_representation.position = [0.075, 0.01] brain.data['colorbar'].scalar_bar_representation.position2 = [0.85, 0.12] if save: filepat = os.path.join(figdir, filepat_base + '_{}_{}.png'.format(wname, hemi)) brain.save_montage(filepat, views[hemi], colorbar=0) return brain