# Generate random data and random connectivity data = np.random.uniform(low=-1., high=1., size=(n_sources, )) conn = np.triu(np.random.uniform(-1., 1., (n_sources, n_sources))) conn_select = (-.005 < conn) & (conn < .005) # Scene creation sc = SceneObj() ############################################################################### # Animate a single brain object ############################################################################### # Here we set an animation for a single brain object. b_obj_1 = BrainObj('inflated', translucent=False) b_obj_1.animate() sc.add_to_subplot(b_obj_1, rotate='left', title='Animate a single object') ############################################################################### # Animate multiple objects ############################################################################### # Here we animate multiple objects inside a subplot s_obj_1 = SourceObj('s1', xyz, data=data) b_obj_2 = BrainObj('white') b_obj_2.animate() sc.add_to_subplot(s_obj_1, row=1, title='Animate multiple objects') sc.add_to_subplot(b_obj_2, row=1, rotate='right', use_this_cam=True) ###############################################################################
def vis_sources_by_pval(padj, all_coord, short_label): ''' 1. visualize the sources (brain regions) based on the FDR-adjusted p values display both the source objects and the brain object ''' # 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=(1600, 1000)) # mask = padj<=0.05 # significant regions with true label data0, max_p, min_p = get_log_pval(padj) b_obj = BrainObj('white', hemisphere='both', translucent=True) b_obj.animate(iterations=10, step=30, interval=1) s_obj = SourceObj( 's1', all_coord, #all_coord, # mask = mask, # mask_color='white', # mask_radius = 15, color=padj * 100, data=data0, text=short_label, text_size=10, text_bold=True, text_color='yellow', symbol='disc', visible=True, radius_min=10, radius_max=40, alpha=0.65) s_obj_1.set_visible_sources('left') s_obj.color_sources(data=data0, cmap='jet', clim=(min_p, max_p)) cb_data = ColorbarObj(s_obj, cblabel='Log FDR-adjusted p value', border=False, **CBAR_STATE) sc.add_to_subplot(b_obj, row=0, col=0, rotate='front') sc.add_to_subplot(s_obj, row=0, col=0) sc.add_to_subplot(cb_data, row=0, col=1, width_max=90) # recording animation and save it sc.record_animation('output/animate_pvalue_projection.gif', n_pic=10) sc.preview()
# Generate random data and random connectivity data = np.random.uniform(low=-1., high=1., size=(n_sources,)) conn = np.triu(np.random.uniform(-1., 1., (n_sources, n_sources))) conn_select = (-.005 < conn) & (conn < .005) # Scene creation sc = SceneObj() ############################################################################### # Animate a single brain object ############################################################################### # Here we set an animation for a single brain object. b_obj_1 = BrainObj('inflated', translucent=False) b_obj_1.animate() sc.add_to_subplot(b_obj_1, rotate='left', title='Animate a single object') ############################################################################### # Animate multiple objects ############################################################################### # Here we animate multiple objects inside a subplot s_obj_1 = SourceObj('s1', xyz, data=data) b_obj_2 = BrainObj('white') b_obj_2.animate() sc.add_to_subplot(s_obj_1, row=1, title='Animate multiple objects') sc.add_to_subplot(b_obj_2, row=1, rotate='right', use_this_cam=True)
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()