def label_surf(pval, colorbar_lim, smooth_iter, colormap, bfp_path='.'): lsurf = readdfs(os.path.join(bfp_path, 'supp_data/bci32kleft.dfs')) rsurf = readdfs(os.path.join(bfp_path, 'supp_data/bci32kright.dfs')) num_vert = lsurf.vertices.shape[0] lsurf.attributes = sp.zeros((lsurf.vertices.shape[0])) rsurf.attributes = sp.zeros((rsurf.vertices.shape[0])) if VTK_INSTALLED: #smooth surfaces lsurf = smooth_patch(lsurf, iterations=smooth_iter) rsurf = smooth_patch(rsurf, iterations=smooth_iter) else: print('VTK is not installed, surface will not be smoothed') # write on surface attributes lsurf.attributes = pval.squeeze() lsurf.attributes = lsurf.attributes[:num_vert] rsurf.attributes = pval.squeeze() rsurf.attributes = rsurf.attributes[num_vert:2 * num_vert] lsurf = patch_color_attrib(lsurf, clim=colorbar_lim, cmap=colormap) rsurf = patch_color_attrib(rsurf, clim=colorbar_lim, cmap=colormap) return lsurf, rsurf
plt.colorbar() plt.savefig('dist_motor_before.pdf', dpi=300) plt.show() plt.figure() plt.imshow(sp.absolute(diffafter), aspect='auto', clim=(0, .1)) plt.colorbar() plt.savefig('dist_motor_after.pdf', dpi=300) plt.show() #diffafter = gaussian_filter(diffafter, [2, 0]) nV = len(dfs_right_sm.vertices) dfs_right_sm.attributes = np.sum(frest * fmotor, axis=0) dfs_right_sm.attributes = dfs_right_sm.attributes[nV:] fname1 = 'rest_after_1.png' fname2 = 'rest_after_2.png' dfs_right_sm = patch_color_attrib(dfs_right_sm, clim=[0.8, 1]) view_patch_vtk(dfs_right_sm, azimuth=90, elevation=180, roll=90, outfile=fname1, show=0) view_patch_vtk(dfs_right_sm, azimuth=-90, elevation=180, roll=-90, outfile=fname2, show=0) for ind in sp.arange(frest.shape[0]): dfs_right_sm.attributes = sp.absolute(diffafter[ind, (nV):])
sub_conn_0 = sub_conn_0 / (np.std(sub_conn_0, axis=1) + 1e-16)[:, None] for ind in range(1, nSub): sub_conn = sp.corrcoef(sub_data[:, :, ind] + 1e-16) sub_conn = sub_conn - sp.mean(sub_conn, axis=1)[:, None] sub_conn = sub_conn / (np.std(sub_conn, axis=1) + 1e-16)[:, None] dist_all_conn[cc_msk] += sp.mean((sub_conn_0 - sub_conn)**2.0, axis=(1)) print ind, dist_all_conn = dist_all_conn / nSub var_all = sp.zeros((sub_data.shape[0], sub_data.shape[1])) avg_sub_data = sp.mean(sub_data, axis=2) # azimuth=-90,elevation=-180, roll=-90, dfs_left_sm = patch_color_attrib(dfs_left_sm, dist_all_conn, clim=[0, 1]) view_patch_vtk(dfs_left_sm, azimuth=-90, elevation=-180, roll=-90, outfile='dist_conn_view1_1sub_left.png', show=0) view_patch_vtk(dfs_left_sm, azimuth=90, elevation=180, roll=90, outfile='dist_conn_view2_1sub_left.png', show=0) sp.savez('conn_pairwise_dist.npz', dist_all_conn)
sub_mean_pc = sp.mean(d, axis=1) sub_mean_pc = sub_mean_pc - sp.mean(sub_mean_pc) sub_mean_pc = sub_mean_pc / (sp.std(sub_mean_pc)) rho = np.dot(ref_mean_pc, sub1) / ref_mean_pc.shape[0] rho[~np.isfinite(rho)] = 0 simil_mtx = sp.pi / 2.0 + sp.arcsin(rho) # simil_mtx=0.3*sp.ones(rho.shape) #SC = SpectralClustering(n_clusters=nClusters, affinity='precomputed') #labs_all = SC.fit_predict(simil_mtx)+1 #rho = smooth_surf_function(dfs_left_sm, rho) dfs_left_sm.attributes = rho dfs_left_sm = patch_color_attrib(dfs_left_sm, rho, clim=[-1, 1]) view_patch_vtk(dfs_left_sm, azimuth=90, elevation=180, roll=90, outfile='sub1to1_view1_pc.png', show=1) view_patch_vtk(dfs_left_sm, azimuth=-90, elevation=-180, roll=-90, outfile='sub1to1_view2_pc.png', show=1) rho = np.dot(sub_mean_pc, sub2) / sub_mean_pc.shape[0] rho[~np.isfinite(rho)] = 0
p_dir, sub, sub + '.rfMRI_REST2_LR.\ reduce3.ftdata.NLM_11N_hvar_25.mat')) data = data['ftdata_NLM'] sub2L, _, _ = normalizeData(data[~LR_flag, :].T) sub2R, _, _ = normalizeData(data[LR_flag, :].T) _, R = brainSync(X=sub1L, Y=sub2L) avgCorrL += sp.sum(sub1L * sp.dot(R, sub2L), axis=0) avgCorrR += sp.sum(sub1R * sp.dot(R, sub2R), axis=0) nSub += 1 print nSub, avgCorrL = avgCorrL / nSub avgCorrR = avgCorrR / nSub # plot correlations in right hemisphere dfs_right_sm = patch_color_attrib(dfs_right_sm, avgCorrR, clim=[0, 1]) view_patch_vtk(dfs_right_sm, azimuth=-90, elevation=-180, roll=-90, outfile='corrLR_right1.png', show=0) view_patch_vtk(dfs_right_sm, azimuth=90, elevation=180, roll=90, outfile='corr_LR_right2.png', show=0) # Plot correlations in left hemisphere dfs_left_sm = patch_color_attrib(dfs_left_sm, avgCorrL, clim=[0, 1])
ls = readdfs( os.path.join(p_dir_ref, 'reference', ref + '.aparc.\ a2009s.32k_fs.reduce3.very_smooth.right.dfs')) lsurf = ls #lind = np.where(ls.labels > -10)[0] lsurf.attributes = np.zeros((lsurf.vertices.shape[0])) #lsurf.attributes = X[150,:lsurf.vertices.shape[0]] # nVert = lsurf.vertices.shape[0] diffafter = Xtsk - Xnew lsurf.attributes = np.sum((diffafter)**2, axis=0) lsurf.attributes = lsurf.attributes[nVert:] #lsurf.attributes = smooth_surf_function(lsurf, lsurf.attributes)#, a1=1.1, a2=1.1) lsurf = patch_color_attrib(lsurf, clim=[1, 2]) view_patch_vtk(lsurf, azimuth=90, elevation=180, roll=90, outfile=fname1, show=0) view_patch_vtk(lsurf, azimuth=-90, elevation=180, roll=-90, outfile=fname2, show=0) #%% for ind in np.arange(Xtsk.shape[0]):
for iter1 in sp.arange(1000): perm1 = np.random.permutation(temp.shape[1]) d2 = temp[:, perm1] d2, R = brainSync(X=d1, Y=d2) null_corr[:, iter1] = sp.sum(d1 * d2, axis=0) print iter1, d2, R = brainSync(X=d1, Y=temp) scorr = sp.sum(d1 * d2, axis=0) c = scorr[:, None] < null_corr pval = sp.mean(c, axis=1) dfs_left_sm = patch_color_attrib(dfs_left_sm, 1 - pval, clim=[0.95, 1]) view_patch_vtk(dfs_left_sm, azimuth=-90, elevation=-180, roll=-90, outfile='pval_perm1.png', show=0) view_patch_vtk(dfs_left_sm, azimuth=90, elevation=180, roll=90, outfile='pval_perm2.png', show=0)
fmri_sub22 = loadmat('/deneb_disk/studyforrest/sub-02-run2\ /fmri_tnlm_5_reduce3_v2.mat') # h5py.File(fname1); dfs_ref = readdfs( os.path.join( p_dir_ref, 'reference', ref + '.aparc\ .a2009s.32k_fs.reduce3.smooth.' + hemi + '.dfs')) sub1seg1 = normdata(fmri_sub11['func_' + hemi][:, :420]) sub1seg2 = normdata(fmri_sub12['func_' + hemi][:, :420]) sub2seg1 = normdata(fmri_sub21['func_' + hemi][:, :420]) sub2seg2 = normdata(fmri_sub22['func_' + hemi][:, :420]) rho_before = sp.sum(sub1seg1 * sub1seg2, axis=1) / sub1seg1.shape[1] dfs_ref = patch_color_attrib(dfs_ref, rho_before, clim=[0, .7]) view_patch_vtk(dfs_ref, azimuth=90, elevation=180, roll=90, outfile='before2_seg1to2_1.png') view_patch_vtk(dfs_ref, azimuth=-90, elevation=180, roll=-90, outfile='before2_seg1to2_2.png') _, Rot12 = rot_sub_data(ref=sub2seg2, sub=sub1seg1) sub1seg1rot = sp.dot(sub1seg1, Rot12.T)
#%% Read surfaces for visualization lsurf = readdfs('/home/ajoshi/coding_ground/bfp/supp_data/bci32kleft.dfs') rsurf = readdfs('/home/ajoshi/coding_ground/bfp/supp_data/bci32kright.dfs') lsurf.attributes = np.zeros((lsurf.vertices.shape[0])) rsurf.attributes = np.zeros((rsurf.vertices.shape[0])) smooth_surf_ nVert = lsurf.vertices.shape[0] #%% Visualization of normal diff from the atlas lsurf.attributes = np.sqrt(np.sum((diff), axis=0)) lsurf.attributes = lsurf.attributes[:nVert] / 15 rsurf.attributes = np.sqrt(np.sum((diff), axis=0)) rsurf.attributes = rsurf.attributes[nVert:2 * nVert] / 15 lsurf = patch_color_attrib(lsurf, clim=[0.1, .3]) rsurf = patch_color_attrib(rsurf, clim=[0.1, .3]) view_patch_vtk(lsurf, azimuth=-90, elevation=180, roll=-90, outfile='l1normal.png', show=1) view_patch_vtk(rsurf, azimuth=-90, elevation=180, roll=-90, outfile='r1normal.png', show=1)
freq1[(bci_labs > 0) & (freq1 == 0)] = \ sp.amin(freq1[(bci_labs_orig > 0) & (freq1 != 0)]) freq[(bci_bst.labels == roino * 10)] += freq1[(bci_bst.labels == roino * 10)] bci_bst.labels += sp.uint16(bci_labs) freq[freq == 0] = 1 bci_bst.attributes = freq bci_bst = patch_color_labels(bci_bst, freq=freq, cmap='Paired') # bci_bst = smooth_patch(bci_bst, iterations=90, relaxation=10.8) view_patch_vtk(bci_bst, show=1) ####writedfs('/home/ajoshi/data/BCI-DNI_brain_atlas/BCI-DNI_brain.left.\ ####mid.cortex_refined_labs_uncorr.dfs', bci_bst) bci_bst = patch_color_attrib(bci_bst, bci_bst.attributes) view_patch_vtk(bci_bst, show=1) bci_bst = patch_color_labels(bci_bst, freq=freq, cmap='Paired') view_patch_vtk(bci_bst, show=1) ####writedfs('/home/ajoshi/data/BCI-DNI_brain_atlas/BCI-DNI_brain.left.\ ####mid.cortex_refined_labs_mod_freq_uncorr.dfs', bci_bst) bci_labs = reduce3_to_bci_lh(bci_bst.labels) bci_freq = reduce3_to_bci_lh(bci_bst.attributes) bci_bst = readdfs('/big_disk/ajoshi/coding_ground/hbci_atlas/BCI-DNI_brain_\ atlas_refined_4_11_2017/BCI-DNI_brain.left.mid.cortex.dfs') bci_bst_sm = readdfs('/big_disk/ajoshi/data/BCI-DNI_brain_atlas/BCI-DNI_brain.\ left.mid.cortex_smooth10.dfs')
dist_all_orig += sp.mean( (sub_data_orig1[:, :, ind] - sub_data_orig2[:, :, ind])**2.0, axis=(1)) sub_data2[:, :, ind], _ = rot_sub_data(ref=sub_data1[:, :, ind], sub=sub_data2[:, :, ind]) dist_all_rot += sp.mean((sub_data1[:, :, ind] - sub_data2[:, :, ind])**2.0, axis=(1)) print ind, dist_all_rot = dist_all_rot / (nSub) dist_all_orig = dist_all_orig / (nSub) var_all = sp.zeros((sub_data1.shape[0], sub_data2.shape[1])) avg_sub_data = sp.mean(sub_data1, axis=2) dfs_right_sm = patch_color_attrib(dfs_right_sm, (2 - dist_all_orig) / 2.0, clim=[0, 1]) view_patch_vtk(dfs_right_sm, azimuth=-90, elevation=-180, roll=-90, outfile='dist_sess_orig_view1_1sub_right.png', show=0) view_patch_vtk(dfs_right_sm, azimuth=90, elevation=180, roll=90, outfile='dist_sess_orig_view2_1sub_right.png', show=0) dfs_right_sm = patch_color_attrib(dfs_right_sm, (2 - dist_all_rot) / 2.0, clim=[0.75, 1])
# # pval=sp.zeros((rho_all.shape[0],1)) # # for jj in range(rho_all.shape[0]): # _, pval[jj] = sp.stats.mannwhitneyu(rho_null[jj,:], rho_all[jj,:]) #, # # alternative='greater') # print jj # sns.distplot(pval) dfs_hemi_sm = readdfs( os.path.join( p_dir_ref, 'reference', ref + '.aparc\ .a2009s.32k_fs.reduce3.smooth.' + hemi + '.dfs')) dfs_hemi_sm.attributes = sp.squeeze(rho_all.mean(axis=1)) dfs_hemi_sm = patch_color_attrib(dfs_hemi_sm, clim=[0, 1]) view_patch_vtk(dfs_hemi_sm, azimuth=90, elevation=180, roll=90, outfile='rest_rot1_fcon1000_subject1_' + hemi + '.png', show=1) view_patch_vtk(dfs_hemi_sm, azimuth=-90, elevation=180, roll=-90, outfile='rest_rot2_fcon1000_subject1_' + hemi + '.png', show=1) rho_null = rho_null.T rho_all1 = sp.mean(rho_all, axis=1)[:, None]
vrest = data m = np.mean(vrest, 1) vrest = vrest - m[:, None] s = np.std(vrest, 1) + 1e-116 vrest1 = vrest / s[:, None] rho1 = 0 rho1rot = 0 diffafter = 0 diffbefore = 0 a = sp.load('fcon1000_null_all_left.npz') rho_null = sp.mean(a['rho_null'], axis=0) dfs_left_sm.attributes = sp.mean(rho_null, axis=0) dfs_left_sm = patch_color_attrib(dfs_left_sm, clim=[0, 1]) view_patch_vtk(dfs_left_sm, azimuth=90, elevation=180, roll=90, outfile='rest_mean1_left.png', show=1) view_patch_vtk(dfs_left_sm, azimuth=-90, elevation=180, roll=-90, outfile='rest_mean2_left.png', show=1) dfs_left_sm.attributes = sp.std(rho_null, axis=0) dfs_left_sm = patch_color_attrib(dfs_left_sm, clim=[0, .1])
freq1[(bci_labs > 0) & (freq1 == 0)] = \ sp.amin(freq1[(bci_labs_orig > 0) & (freq1 != 0)]) freq[(bci_bst.labels == roino * 10)] += freq1[(bci_bst.labels == roino * 10)] bci_bst.labels += sp.uint16(bci_labs) freq[freq == 0] = 1 bci_bst.attributes = freq bci_bst = patch_color_labels(bci_bst, cmap='Paired') # bci_bst = smooth_patch(bci_bst, iterations=90, relaxation=10.8) view_patch_vtk(bci_bst, show=1) ####writedfs('/home/ajoshi/data/BCI-DNI_brain_atlas/BCI-DNI_brain.right.\ ####mid.cortex_refined_labs_uncorr.dfs', bci_bst) bci_bst = patch_color_attrib(bci_bst, bci_bst.attributes) view_patch_vtk(bci_bst, show=1) bci_bst = patch_color_labels(bci_bst, freq=freq, cmap='Paired') # bci_bst = smooth_patch(bci_bst, iterations=90, relaxation=10.8) view_patch_vtk(bci_bst, show=1) ####writedfs('/home/ajoshi/data/BCI-DNI_brain_atlas/BCI-DNI_brain.right.\ ####mid.cortex_refined_labs_mod_freq_uncorr.dfs', bci_bst) bci_bst = patch_color_attrib(bci_bst, bci_bst.labels) view_patch_vtk(bci_bst, show=1) bci_labs = reduce3_to_bci_rh(bci_bst.labels) bci_freq = reduce3_to_bci_rh(bci_bst.attributes) bci_bst = readdfs( '/big_disk/ajoshi/data/BCI-DNI_brain_atlas/BCI-DNI_brain.right.\
faceseg1 = faceseg1[:180] faceseg2 = faceseg2[:180] faceseg1_2 = faceseg1_2[:180] print(sp.linalg.norm(fseg1 - fseg2), sp.linalg.norm(fseg1_2 - fseg2), sp.linalg.norm(fseg1_2 - fseg1)) print( sp.dot(faceseg1, faceseg2) / len(faceseg2), sp.dot(faceseg1_2, faceseg2) / len(faceseg2), sp.dot(faceseg1_2, faceseg1) / len(faceseg2)) plt.plot(faceseg1, 'b') plt.plot(faceseg2, 'r') plt.plot(faceseg1_2, 'k') plt.savefig('face_annotation_sync.png') rho_21 = sp.sum(fseg1_2 * fseg2, axis=1) / fseg2.shape[1] dfs_ref = patch_color_attrib(dfs_ref, rho_21, clim=[0.5, 1]) view_patch_vtk(dfs_ref, azimuth=90, elevation=180, roll=90, outfile='sync1_2_1_right.png') view_patch_vtk(dfs_ref, azimuth=-90, elevation=180, roll=-90, outfile='sync1_2_2_right.png')
y3 = np.einsum('ij,ij->i', yunit, (V3 - V1)) / np.linalg.norm(yunit, axis=1) sqrt_DT = (np.abs((x1 * y2 - y1 * x2) + (x2 * y3 - y2 * x3) + (x3 * y1 - y3 * x1))) Ar = 0.5 * (np.abs((x1 * y2 - y1 * x2) + (x2 * y3 - y2 * x3) + (x3 * y1 - y3 * x1))) TC = face_v_conn(surf1) Wt = (1.0 / 3.0) * (TC) # Wt = sp.sparse.spdiags(Wt*Ar, (0), NumTri, NumTri) surf_weight = Wt * Ar surf1.attributes = surf_weight surf_weight = surf_weight[:, None] # smooth_surf_function(dfs_right_sm, Wt*Ar*0.1, a1=0, a2=1) surf1.attributes = ind_rois surf1 = patch_color_attrib(surf1) view_patch_vtk(surf1, show=1) # sub = '110411' # p_dir = '/home/ajoshi/data/HCP_data' lst = os.listdir('/big_disk/ajoshi/HCP5') rho1 = 0 rho1rot = 0 rho2 = 0 rho2rot = 0 # lst = [lst[0]] diffbefore = 0 diffafter = 0 sub = lst[0]
lsurf.attributes = np.zeros((lsurf.vertices.shape[0])) rsurf.attributes = np.zeros((rsurf.vertices.shape[0])) lsurf = smooth_patch(lsurf, iterations=1500) rsurf = smooth_patch(rsurf, iterations=1500) labs[sp.isnan(labs)] = 0 diff = diff * (labs.T > 0) diffAdhdInatt = diffAdhdInatt * (labs.T > 0) nVert = lsurf.vertices.shape[0] #%% Visualization of normal diff from the atlas lsurf.attributes = np.sqrt(np.sum((diff), axis=1)) lsurf.attributes = lsurf.attributes[:nVert] / 50 rsurf.attributes = np.sqrt(np.sum((diff), axis=1)) rsurf.attributes = rsurf.attributes[nVert:2 * nVert] / 50 lsurf = patch_color_attrib(lsurf, clim=[0, .2]) rsurf = patch_color_attrib(rsurf, clim=[0, .2]) view_patch_vtk(lsurf, azimuth=100, elevation=180, roll=90, outfile='l1normal.png', show=1) view_patch_vtk(rsurf, azimuth=-100, elevation=180, roll=-90, outfile='r1normal.png', show=1)
vrest, _, _ = normalizeData(vrest) vrest, Rot = brainSync(X=vsub, Y=vrest) t = sp.sum(vrest*vsub, axis=0) # print('rho(%d)=%g' % (ind1, sp.mean(t)), end=' ') print 'rho(%d)=%g' % (ind1, sp.mean(t)) rho_sub[ind1, :] = t # %% # Hypothesis test rho_sub1 = sp.mean(rho_sub, axis=0) pval = sp.mean(rho_sub1 > rho_null, axis=0) r, corrPval,_,_ = multipletests(pvals=pval, alpha=0.05, method='fdr_bh') sl = readdfs(os.path.join(BFPPATH, 'bci32kleft.dfs')) sl.attributes = corrPval[:sl.vertices.shape[0]] sl = patch_color_attrib(sl, clim=[0, 1]) sr = readdfs(os.path.join(BFPPATH, 'bci32kright.dfs')) sr.attributes = corrPval[sl.vertices.shape[0]:2*sl.vertices.shape[0]] sr = patch_color_attrib(sr, clim=[0, 1]) writedfs('right_pval_sn7915.dfs',sr); writedfs('left_pval_sn7915.dfs',sl); view_patch_vtk(sl, azimuth=90, elevation=180, roll=90, show=1) view_patch_vtk(sl, azimuth=-90, elevation=180, roll=-90, show=1)
y3 = np.einsum('ij,ij->i', yunit, (V3 - V1)) / np.linalg.norm(yunit, axis=1) sqrt_DT = (np.abs((x1 * y2 - y1 * x2) + (x2 * y3 - y2 * x3) + (x3 * y1 - y3 * x1))) Ar = 0.5 * (np.abs((x1 * y2 - y1 * x2) + (x2 * y3 - y2 * x3) + (x3 * y1 - y3 * x1))) TC = face_v_conn(surf1) Wt = (1.0 / 3.0) * (TC) # Wt = sp.sparse.spdiags(Wt*Ar, (0), NumTri, NumTri) surf_weight = Wt * Ar surf1.attributes = surf_weight surf_weight = surf_weight[:, None] # smooth_surf_function(dfs_left_sm, Wt*Ar*0.1, a1=0, a2=1) surf1.attributes = ind_rois surf1 = patch_color_attrib(surf1) view_patch_vtk(surf1, show=1) # sub = '110411' # p_dir = '/home/ajoshi/data/HCP_data' lst = os.listdir('/big_disk/ajoshi/HCP5') rho1 = 0 rho1rot = 0 rho2 = 0 rho2rot = 0 # lst = [lst[0]] diffbefore = 0 diffafter = 0 sub = lst[0]
all_centroid = [] centroid = [] label_count = 0 # for n in range(nClusters.shape[0]): # print n n = 0 roiregion = left_hemisphere[n] s_a = readdfs('100307.reduce3.very_smooth.left.refined.dfs') seeds = sp.zeros(nClusters) # seeds_roi = sp.zeros(nClusters) col = sp.zeros(s_a.vertices.shape[0]) col[sp.int16(seeds)] = sp.arange(len(seeds)) + 1 s_a = patch_color_attrib(s_a, col) ref_dir = os.path.join(p_dir, 'reference') ref = '100307' fn1 = ref + '.reduce' + str(3) + '.LR_mask.mat' fname1 = os.path.join(ref_dir, fn1) msk = scipy.io.loadmat(fname1) data = scipy.io.loadmat( os.path.join( p_dir, sub, sub + '.rfMRI_REST' + str(1) + '_LR' + '.reduce3.ftdata.NLM_11N_hvar_25.mat')) LR_flag = 0 data = data['ftdata_NLM'] LR_flag = msk['LR_flag'].squeeze() LR_flag = np.squeeze(LR_flag) == 1