path_to_file2 = download_file(file2, astype='example_data') # Define a brain object : b_obj = BrainObj('inflated', hemisphere='both', translucent=False, cblabel='Parcellates example', cbtxtsz=4.) """Parcellize the left hemisphere using the Destrieux Atlas. By default, no parcellates are selected """ b_obj.parcellize(path_to_file1, hemisphere='left') """If you want to get the list of all predefined parcellates, use the `get_parcellates` method which returns a pandas DataFrame with the index, the name and the color associated to each parcellates """ df = b_obj.get_parcellates(path_to_file2) # print(df) """Select only some parcellates. Note that this parcellization is using an other atlas (Desikan-Killiany atlas) """ select = ['insula', 'paracentral', 'precentral', 'precuneus', 'frontalpole', 'temporalpole', 'fusiform', 'cuneus', 'inferiorparietal', 'inferiortemporal', 'precentral', 'superiorfrontal', 'superiortemporal'] """Instead of using predefined colors inside the annot file, we use some data """ data = np.arange(len(select)) b_obj.parcellize(path_to_file2, hemisphere='right', select=select, data=data, cmap='Spectral_r')
def vis_brainSurface_by_padj(padj_parcellates, gci_parcellates, tt, ptype='significant', cmap='jet', use_log=False, use_1_p=False, use_p=True): # '''visualize the brain surface based on the FDR-adjusted GCI values or FDR-adjusted p values''' # Define the default camera state used for each subplot CAM_STATE = dict( azimuth=0, # azimuth angle elevation=90, # elevation angle scale_factor=180 # distance to the camera ) S_KW = dict(camera_state=CAM_STATE) # Create the scene CBAR_STATE = dict(cbtxtsz=12, txtsz=13., width=.5, cbtxtsh=2., rect=(1., -2., 1., 4.)) sc = SceneObj(bgcolor='black', size=(1300, 1000)) # n_sources = all_coord.shape[0] #sig_roi_coord.shape[0] # data = np.random.rand(n_sources) # 1. significant regions b_obj_1 = BrainObj('white', translucent=False) # parcellize brain based on desikan atlas path_to_file1 = download_file('lh.aparc.annot', astype='example_data') path_to_file2 = download_file('rh.aparc.annot', astype='example_data') # dataframe type varaible inlcuding index, label, color for each brain region in DKT atlas lh_df = b_obj_1.get_parcellates(path_to_file1) rh_df = b_obj_1.get_parcellates(path_to_file2) # lh_df.to_excel('output/lh.aparc.xlsx') def select_rois(df, row=1, col=1): select_val = list(np.array(df.iloc[row:, col])) select_val.pop(3) return select_val select_roi1 = select_rois(lh_df) select_roi2 = select_rois(rh_df) # get log FDR-adjusted p values (optional) # log_p_parce, l_p, s_p = get_log_pval(padj_parcellates, basis=2) l_p = np.max(gci_parcellates) s_p = np.min(gci_parcellates) # print('-----#####',l_p,s_p) if ptype == 'significant' or ptype == 'unsignificant': def get_hemisphere_rois(lh_padj, lh_gci, lh_rois, select='significant', cal_log=True): if select == 'significant': lh_sig_ind = np.where(np.array(lh_padj) <= 0.05)[0] else: lh_sig_ind = np.where(np.array(lh_padj) > 0.05)[0] lh_sig_rois = [lh_rois[i] for i in lh_sig_ind] if cal_log: log_p, _, _ = get_log_pval(lh_padj, basis=2) # calculate "log2(padj)" lh_sig_padj = list(np.array(log_p)[lh_sig_ind]) else: # lh_sig_padj = list(np.array(lh_padj)[lh_sig_ind]) lh_sig_gci = list(np.array(lh_gci)[lh_sig_ind]) # max_p= np.max(np.array(lh_sig_padj)) # min_p= np.min(np.array(lh_sig_padj)) # return lh_sig_rois,lh_sig_padj,max_p,min_p max_gci = np.max(np.array(lh_sig_gci)) min_gci = np.min(np.array(lh_sig_gci)) return lh_sig_rois, lh_sig_gci, max_gci, min_gci # (1). set (log-padj) as values for color mapping # select_regions_L,lh_padj,_,_ = get_hemisphere_rois(padj_parcellates[:34], gci_parcellates[:34], select_roi1, select = ptype, cal_log=use_log) # select_regions_R,rh_padj,_,_ = get_hemisphere_rois(padj_parcellates[34:], gci_parcellates[34:], select_roi2, select = ptype, cal_log=use_log) # clab = 'Log FDR-adjusted p value' # b_obj_1.parcellize(path_to_file1, select= select_regions_L,data=lh_padj,cmap=cmap,clim=[s_p,l_p]) # b_obj_1.parcellize(path_to_file2, select= select_regions_R,data=rh_padj,cmap=cmap,clim=[s_p,l_p]) # cb_1 = ColorbarObj(b_obj_1, clim= [s_p,l_p], cblabel=clab, border=False, **CBAR_STATE) # plot GCI value-4/23/2020 select_regions_L, lh_gci, _, _ = get_hemisphere_rois( padj_parcellates[:34], gci_parcellates[:34], select_roi1, select=ptype, cal_log=use_log) select_regions_R, rh_gci, _, _ = get_hemisphere_rois( padj_parcellates[34:], gci_parcellates[34:], select_roi2, select=ptype, cal_log=use_log) clab = 'GCI value' b_obj_1.parcellize(path_to_file1, select=select_regions_L, data=lh_gci) #clim=[s_p,l_p] b_obj_1.parcellize(path_to_file2, select=select_regions_R, data=rh_gci, cmap=cmap) #, clim=[s_p,l_p]) cb_1 = ColorbarObj(b_obj_1, clim=[1.76, 1.80], cblabel=clab, border=False, **CBAR_STATE) elif ptype == 'together': select_regions_L = select_roi1 select_regions_R = select_roi2 if use_log: # (1). set (log-padj) as values for color mapping clab = 'Log FDR-adjusted p value' lh_padj = log_p_parce[:34] rh_padj = log_p_parce[34:] b_obj_1.parcellize(path_to_file1, select=select_regions_L, data=lh_padj, cmap=cmap, clim=[s_p, l_p]) b_obj_1.parcellize(path_to_file2, select=select_regions_R, data=rh_padj, cmap=cmap, clim=[s_p, l_p]) cb_1 = ColorbarObj(b_obj_1, clim=[s_p, l_p], cblabel=clab, border=False, **CBAR_STATE) if use_1_p: # (2). set (1-padj) as values for color mapping clab = tt #'1-FDR-adjusted p value' # clab = '1-FDR-adjusted p value' padj_0 = [1 - i for i in padj_parcellates] b_obj_1.parcellize(path_to_file1, select=select_regions_L, data=padj_0[:34], cmap=cmap) b_obj_1.parcellize(path_to_file2, select=select_regions_R, data=padj_0[34:], cmap=cmap) cb_1 = ColorbarObj(b_obj_1, cblabel=clab, border=False, **CBAR_STATE) #Log FDR-adjusted p value if use_p: # (2). set (1-padj) as values for color mapping print('--------use p-------') clab = tt #'1-FDR-adjusted p value' mx = np.array(gci_parcellates).max() mi = np.array(gci_parcellates).min() b_obj_1.parcellize(path_to_file1, select=select_regions_L, data=gci_parcellates[:34], cmap=cmap, clim=[mi, mx]) b_obj_1.parcellize(path_to_file2, select=select_regions_R, data=gci_parcellates[34:], cmap=cmap, clim=[mi, mx]) cb_1 = ColorbarObj(b_obj_1, cblabel=clab, border=False, **CBAR_STATE) #Log FDR-adjusted p value b_obj_1.animate(iterations=10, step=30, interval=1.2) sc.add_to_subplot(b_obj_1, row=0, col=0, rotate='front') sc.add_to_subplot(cb_1, row=0, col=1, width_max=90) # sc.record_animation('output/%s_pvalue_projection.gif'%ptype, n_pic=10) # sc.record_animation('output/pmci_degree.gif', n_pic=8) sc.preview()
# Define a brain object : b_obj = BrainObj('inflated', hemisphere='both', translucent=False, cblabel='Parcellates example', cbtxtsz=4.) """Parcellize the left hemisphere using the Destrieux Atlas. By default, no parcellates are selected """ b_obj.parcellize(path_to_file1, hemisphere='left') """If you want to get the list of all predefined parcellates, use the `get_parcellates` method which returns a pandas DataFrame with the index, the name and the color associated to each parcellates """ df = b_obj.get_parcellates(path_to_file2) # print(df) """Select only some parcellates. Note that this parcellization is using an other atlas (Desikan-Killiany atlas) """ select = [ 'insula', 'paracentral', 'precentral', 'precuneus', 'frontalpole', 'temporalpole', 'fusiform', 'cuneus', 'inferiorparietal', 'inferiortemporal', 'precentral', 'superiorfrontal', 'superiortemporal' ] """Instead of using predefined colors inside the annot file, we use some data """ data = np.arange(len(select)) b_obj.parcellize(path_to_file2, hemisphere='right', select=select,
def create_brain_obj(l_file, r_file, activated_areas, brain_name, hemisphere='both', cmap='copper', vmin=0., vmax=0.01): """Create BrainObj, add areas, and return object Input: -------- l_file: str r_file: str brain_areas: pd.DataFrame dataframe of selected areas, shape ['index', 'values'] brain_name: str parameter for the BrainObj, in ('white', 'inflated') hemisphere: str parameter for the BrainObj, in ('both', 'left', 'right) cmap: str maplotlib colomap to use. For pvalues, 'copper' should be used; for estimates, 'coolwarm'. clim: tuple colorbar limits. For pvalues, (0,pmax) should be used; for estimates, a symetrical interval. Output: -------- b_obj: visbrain.objects.BrainObj """ b_obj = BrainObj(brain_name, translucent=False, hemisphere=hemisphere) annot_data = b_obj.get_parcellates(l_file) # errors if missing labels - removing labels not in annot_data annot_select = pd.merge(annot_data.reset_index()[['index', 'Labels']], activated_areas, on=['index'], validate="one_to_one") annot_select['is_left'] = annot_select['Labels'].apply( lambda x: x[-1] == 'L') if annot_select[annot_select.is_left].shape[0] > 0: b_obj.parcellize( l_file, hemisphere='left', select=annot_select[annot_select.is_left]['Labels'].tolist(), data=annot_select[annot_select.is_left]['values'].tolist(), cmap=cmap, vmin=vmin, vmax=vmax, clim=(vmin, vmax)) if annot_select[~annot_select.is_left].shape[0] > 0: b_obj.parcellize( r_file, hemisphere='right', select=annot_select[~annot_select.is_left]['Labels'].tolist(), data=annot_select[~annot_select.is_left]['values'].tolist(), cmap=cmap, vmin=vmin, vmax=vmax, clim=(vmin, vmax)) return b_obj