from nibabel.gifti.giftiio import read as gread labels, _, _ = fsio.read_annot( '/big_disk/ajoshi/freesurfer/subjects/sub06880/label/rh.HCP-MMP1.annot') vert, faces = fsaverage_surf = fsio.read_geometry( '/big_disk/ajoshi/freesurfer/subjects/sub06880/surf/rh.pial') class fs: pass fs.vertices = vert fs.faces = faces fs.labels = labels fs = patch_color_labels(fs) view_patch_vtk(fs, outfile='fs1.png') #, azimuth=-90, roll=90) class bs: pass bs1 = readdfs('/home/ajoshi/Desktop/test/T1.right.pial.cortex.svreg.dfs') bs = readdfs( '/home/ajoshi/Desktop/test/multiparc/T1.right.mid.cortex.svreg.HCP-MMP1.dfs' ) bs.vertices = bs1.vertices bs = patch_color_labels(bs) view_patch_vtk(bs, outfile='bst1.png') #, azimuth=-90, roll=90)
outfile = 'BCI-DNI_Broadmann' + '.' + hemi + '.mid.cortex.dfs' ''' BCI to FS processed BCI ''' bci_bsti = readdfs( '/home/ajoshi/BrainSuite19b/svreg/BCI-DNI_brain_atlas/BCI-DNI_brain.' + hemi + '.inner.cortex.dfs') bci_bst_mid = readdfs( '/home/ajoshi/BrainSuite19b/svreg/BCI-DNI_brain_atlas/BCI-DNI_brain.' + hemi + '.mid.cortex.dfs') bci_bsti.vertices[:, 0] -= 96 * 0.8 bci_bsti.vertices[:, 1] -= 192 * 0.546875 bci_bsti.vertices[:, 2] -= 192 * 0.546875 bci.vertices, bci.faces = fsio.read_geometry( '/big_disk/ajoshi/data/BCI_DNI_Atlas/surf/' + fshemi + '.white') bci.labels = np.zeros(bci.vertices.shape[0]) for i in range(len(broadmann)): labind = fsio.read_label('/big_disk/ajoshi/data/BCI_DNI_Atlas/label/' + fshemi + '.' + broadmann[i] + '.thresh.label') bci.labels[labind] = i + 1 bci = patch_color_labels(bci) view_patch_vtk(bci) bci_bsti = interpolate_labels(bci, bci_bsti) bci_bst_mid.labels = bci_bsti.labels bci_bst_mid = smooth_patch(bci_bst_mid, iterations=3000, relaxation=.5) bci_bst_labels = patch_color_labels(bci_bst_mid) view_patch_vtk(bci_bst_labels) writedfs(outfile, bci_bst_labels)
vol_lab = image.load_img(outvol) vol_lab = image.new_img_like(vol_lab, np.int16(vol_lab.get_fdata())) vol_lab.to_filename(outvol) vol_img = vol_lab.get_fdata() xres = vol_lab.header['pixdim'][1] yres = vol_lab.header['pixdim'][2] zres = vol_lab.header['pixdim'][3] sl = readdfs(lmid) sr = readdfs(rmid) xx = np.arange(vol_lab.shape[0]) * xres yy = np.arange(vol_lab.shape[1]) * yres zz = np.arange(vol_lab.shape[2]) * zres sl.labels = interpn((xx, yy, zz), vol_img, sl.vertices, method='nearest') sr.labels = interpn((xx, yy, zz), vol_img, sr.vertices, method='nearest') sl = smooth_patch(sl, iterations=3000, relaxation=.5) sr = smooth_patch(sr, iterations=3000, relaxation=.5) patch_color_labels(sl) view_patch_vtk(sl) patch_color_labels(sr) view_patch_vtk(sr) writedfs(outmidl, sl) writedfs(outmidr, sr)
aa = labs.nifti_header.extensions[0].get_content() idxs = aa.get_axis(1).vertex labels = np.squeeze(labs.get_fdata()) g_surf = nib.load('/data_disk/HCP5-fMRI-NLM/reference/100307/MNINonL\ inear/fsaverage_LR32k/100307.L.very_inflated.32k_fs_LR.surf.gii') s.vertices = g_surf.darrays[0].data s.faces = g_surf.darrays[1].data s.labels = np.zeros(s.vertices.shape[0]) s.labels[idxs] = labels '''h32k to full res FS''' g_surf = nib.load('/data_disk/HCP5-fMRI-NLM/reference/100307/MNINonLinear/N\ ative/100307.L.very_inflated.native.surf.gii') h.vertices = g_surf.darrays[0].data h.faces = g_surf.darrays[1].data h = interpolate_labels(s, h) h = patch_color_labels(h) s = patch_color_labels(s) view_patch_vtk(s) view_patch_vtk(h) ''' native FS ref to native FS BCI''' g_surf = nib.load('/data_disk/HCP5-fMRI-NLM/reference/100307/MNINon\ Linear/Native/100307.L.sphere.reg.native.surf.gii') s.vertices = g_surf.darrays[0].data s.faces = g_surf.darrays[1].data s.labels = h.labels ''' map to bc sphere''' bs.vertices, bs.faces = fsio.read_geometry( '/big_disk/ajoshi/fs_sub/BCI_DNI_Atlas/surf/lh.sphere.reg')
from sklearn.utils.linear_assignment_ import linear_assignment from sklearn.metrics import adjusted_rand_score s = readdfs( '/big_disk/ajoshi/coding_ground/svreg-matlab/BCI-DNI_brain_atlas_refined/BCI-DNI_brain.left.mid.cortex.mod.dfs' ) sl = readdfs( '/big_disk/ajoshi/coding_ground/svreg-matlab/BCI-DNI_brain_atlas_refined/BCI-DNI_brain.left.mid.cortex.dfs' ) sm = readdfs( '/big_disk/ajoshi/coding_ground/svreg-matlab/BCI-DNI_brain_atlas_refined/BCI-DNI_brain.left.mid.cortex_smooth10.dfs' ) s.labels = sl.labels.copy() flg = (s.labels != 187) & (s.labels != 189) & (s.labels != 191) #131 middle frontal s.attributes[flg] = 0 s.labels[flg] = 0 s.attributes = 2.0 * sp.maximum(s.attributes - 0.5, 0) s = patch_color_labels(s, freq=s.attributes, cmap='hsv') #s = patch_color_attrib(s,cmap='gray',clim=[0,1]) s.vertices = sm.vertices view_patch_vtk(s, outfile='mod_map_cingulate2.png', show=0, azimuth=90, elevation=0, roll=-90)
g32ktmp = gread('/big_disk/ajoshi/data/standard_mesh_atlases/resample\ _fsaverage/fs_LR-deformed_to-fsaverage.R.sphere.32k_fs_LR.surf.gii') g32k.vertices = g32ktmp.get_arrays_from_intent('NIFTI_INTENT_POINTSET')[0].data g32k.faces = g32ktmp.get_arrays_from_intent('NIFTI_INTENT_TRIANGLE')[0].data g32k.vColor = sp.ones(g32k.vertices.shape) bci.vertices, bci.faces = fsio.read_geometry('/big_disk/ajoshi/data/BCI_\ DNI_Atlas/surf/rh.sphere.reg') bci.labels = fsio.read_annot('/big_disk/ajoshi/data/BCI_DNI_Atlas/label/rh\ .BA.thresh.annot')[0] g32k = interpolate_labels(fromsurf=bci, tosurf=g32k) g32ktmp = gread('/big_disk/ajoshi/HCP_data/32k_ConteAtlas_v2/Conte69.R\ .very_inflated.32k_fs_LR.surf.gii') g32k.vertices = g32ktmp.get_arrays_from_intent('NIFTI_INTENT_POINTSET')[0].data g32k = patch_color_labels(g32k) view_patch_vtk(g32k) writedfs('Boradmann_32k_right.dfs', g32k) ''' Left Hemisphere ''' g32ktmp = gread('/big_disk/ajoshi/data/standard_mesh_atlases/resample\ _fsaverage/fs_LR-deformed_to-fsaverage.L.sphere.32k_fs_LR.surf.gii') g32k.vertices = g32ktmp.get_arrays_from_intent('NIFTI_INTENT_POINTSET')[0].data g32k.faces = g32ktmp.get_arrays_from_intent('NIFTI_INTENT_TRIANGLE')[0].data g32k.vColor = sp.ones(g32k.vertices.shape) bci.vertices, bci.faces = fsio.read_geometry('/big_disk/ajoshi/data/BCI_\ DNI_Atlas/surf/lh.sphere.reg') bci.labels = fsio.read_annot('/big_disk/ajoshi/data/BCI_DNI_Atlas/label/lh\ .BA.thresh.annot')[0]
# Process left hemisphere yeomapL, _, _ = fsio.read_label(inputfile_L) vert, faces = fsio.read_geometry(fsAve_sph_L) gL = nib.load(fsAve_sph_32k_L) vert32k = gL.darrays[0].data faces32k = gL.darrays[1].data lh_sph.vertices = vert lh_sph.faces = faces lh_sph.labels = yeomapL lh32k.vertices = vert32k lh32k.faces = faces32k lh32k = interpolate_labels(lh_sph, lh32k) gL = nib.load(fsAve_32k_L) lh32k.vertices = gL.darrays[0].data lh32k.faces = gL.darrays[1].data lh32k = patch_color_labels(lh32k) view_patch_vtk(lh32k) # process right hemisphere yeomapR, _, _ = fsio.read_annot(inputfile_R) vert, faces = fsio.read_geometry(fsAve_sph_R) gR = nib.load(fsAve_sph_32k_R) vert32k = gR.darrays[0].data faces32k = gR.darrays[1].data rh_sph.vertices = vert rh_sph.faces = faces rh_sph.labels = yeomapR rh32k.vertices = vert32k rh32k.faces = faces32k rh32k = interpolate_labels(rh_sph, rh32k) gR = nib.load(fsAve_32k_R)
ind = (bci_bst.labels == roino * 10) & (bci_labs != 0) fs.vertices = bci_bst.vertices[ind, :] fs.labels = bci_labs[ind] ts = interpolate_labels(fs, ts) bci_labs[indt] = ts.labels 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)
l2 = sp.reshape(l[:, 40:80], (l.shape[0]*40), order='F') l12 = sp.concatenate((l1[:, None], l2[:, None]), axis=1) print sp.sum(sp.absolute(l12[:, 1]-l12[:, 0])) print sp.sum(sp.absolute(l12[:, 1]-l12[:, 0])) l1 = sp.reshape(l12[:, 0], (l.shape[0], 40), order='F') l2 = sp.reshape(l12[:, 1], (l.shape[0], 40), order='F') # for ind in range(l12.shape[1]): lab1 = sp.int32(l12[:, ind]) dfs_left_sm.labels = lab1 s = silhouette_samples(cat_data[nVert*(ind):nVert*(ind+1), :], dfs_left_sm.labels) s[s < 0] = 0 s = s/sp.median(s) s[s > 1.0] = 1.0 s1 = patch_color_labels(dfs_left_sm, freq=s) # view_patch(s1) view_patch(s1, elevation=90, colorbar=0, show=0, outfile=lst[ind]+'_\ individual_view1_nclusters30_sil_modulated.png', colormap='Paired') view_patch(s1, elevation=-90, colorbar=0, show=0, outfile=lst[ind]+'_\ individual_view2_nclusters30_sil_modulated.png', colormap='Paired')
class bci: pass ''' map to bc sphere''' bs.vertices, bs.faces = fsio.read_geometry( '/big_disk/ajoshi/fs_sub/BCI_DNI_Atlas/surf/lh.sphere.reg') bs = interpolate_labels(s, bs) bci.vertices, bci.faces = fsio.read_geometry( '/big_disk/ajoshi/fs_sub/BCI_DNI_Atlas/surf/lh.white') bci.labels = bs.labels writedfs('BCI_orig_lh.dfs', bci) bci.vertices, bci.faces = fsio.read_geometry( '/big_disk/ajoshi/fs_sub/BCI_DNI_Atlas/surf/lh.inflated') bci = patch_color_labels(bci) view_patch_vtk(bci) writedfs('BCI_pial_lh.dfs.', bci) bci.vertices, bci.faces = fsio.read_geometry( '/big_disk/ajoshi/fs_sub/BCI_DNI_Atlas/surf/lh.white') writedfs('BCI_white_lh.dfs.', bci) bci.vertices[:, 0] += 96 * 0.8 bci.vertices[:, 1] += 192 * 0.546875 bci.vertices[:, 2] += 192 * 0.546875 bci_bst = readdfs( '/home/ajoshi/BrainSuite19b/svreg/BCI-DNI_brain_atlas/BCI-DNI_brain.left.inner.cortex.dfs' ) bci_bst = interpolate_labels(bci, bci_bst)
hemi = 'left' fshemi = 'lh' ''' BCI to FS processed BCI ''' bci_bsti = readdfs('/home/ajoshi/BrainSuite19b/svreg/BCI-DNI\ _brain_atlas/BCI-DNI_brain.' + hemi + '.mid.cortex.dfs') bci_bst = readdfs('/home/ajoshi/BrainSuite19b/svreg/BCI-DNI\ _brain_atlas/BCI-DNI_brain.' + hemi + '.inner.cortex.dfs') bci_bst.labels = bci_bsti.labels bci_bst.vertices[:, 0] -= 96 * 0.8 bci_bst.vertices[:, 1] -= 192 * 0.546875 bci_bst.vertices[:, 2] -= 192 * 0.546875 bci_fs.vertices, bci_fs.faces = fsio.read_geometry('/big_disk/ajoshi/data/BCI_\ DNI_Atlas/surf/' + fshemi + '.white') fslabels, _, _ = fsio.read_annot( '/big_disk/ajoshi/freesurfer/subjects/BCI_DNI_Atlas/label/' + fshemi + '.economo.annot') bci_fs.labels = fslabels bci_bst = interpolate_labels(bci_fs, bci_bst) bci_bsti.labels = bci_bst.labels bci_bsti = patch_color_labels(bci_bsti) view_patch_vtk(bci_bsti) bci_bsti = smooth_patch(bci_bsti, iterations=10000) view_patch_vtk(bci_bsti) writedfs('BCI_DNI_economo_' + hemi + '.dfs', bci_bsti)
f_rho = np.arctanh(rho_1) f_rho[~np.isfinite(f_rho)] = 0 B = np.corrcoef(f_rho) B[~np.isfinite(B)] = 0 affinity_matrix = np.arcsin(rho) # conn = sp.sparse.lil_matrix((dfs_left.vertices.shape[0], # dfs_left.vertices.shape[0])) conn = sp.eye(dfs_left.vertices.shape[0]) conn[dfs_left.faces[:, 0], dfs_left.faces[:, 1]] = 1 conn[dfs_left.faces[:, 1], dfs_left.faces[:, 2]] = 1 conn[dfs_left.faces[:, 0], dfs_left.faces[:, 2]] = 1 conn = conn + conn.T conn = conn > 0 conn = conn[msk_small_region, ] conn = conn[:, msk_small_region] labs = region_growing_fmri(seeds, affinity_matrix, conn) col = 0 * col col[msk_small_region] = labs + 1 ms = sp.where(msk_small_region) ms = sp.array(ms).squeeze() col[ms[sp.int16(seeds)]] = 10 s_a = patch_color_attrib(s_a, col) view_patch_vtk(s_a) s_a.labels[~msk_small_region] = 0 s_a = patch_color_labels(s_a) view_patch_vtk(s_a) print sp.sum(labs < 0)
bci = interpolate_labels(bci_bst, bci) ''' FS_BCI to FS BCI Sphere''' bci.vertices, bci.faces = fsio.read_geometry('/big_disk/ajoshi/data/BCI_\ DNI_Atlas/surf/' + fshemi + '.sphere.reg') ''' FS BCI Sphere to SUB FS Sphere''' s.vertices, s.faces = fsio.read_geometry(subbasename + '/surf/' + fshemi + '.sphere.reg') s = interpolate_labels(bci, s) fslabels, _, _ = fsio.read_annot(subbasename + '/label/' + fshemi + '.aparc.annot') s.labels = s.labels * sp.int16(fslabels > 0) s = patch_color_labels(s) view_patch_vtk(s) writedfs('fs_sphere.dfs', s) s.vertices, _ = fsio.read_geometry(subbasename + '/surf/' + fshemi + '.pial') so, _ = fsio.read_geometry(subbasename + '/surf/' + fshemi + '.white') s.vertices = (s.vertices + so) / 2.0 view_patch_vtk(s) s.faces = s.faces[:, (0, 2, 1)] writedfs(subbasename + '/' + hemi + '.mid.dfs', s) #eng = meng.start_matlab() #eng.addpath(eng.genpath('/big_disk/ajoshi/coding_ground/svreg-matlab/MEX_Files')) #eng.addpath(eng.genpath('/big_disk/ajoshi/coding_ground/svreg-matlab/3rdParty')) #eng.addpath(eng.genpath('/big_disk/ajoshi/coding_ground/svreg-matlab/src')) #xmlf='/big_disk/ajoshi/coding_ground/svreg/USCBrain/brainsuite_labeldescription.xml' #eng.recolor_by_label('../precentral_corr/BCI-DNI_brain.left.mid.cortex.dfs','',xmlf,nargout=0)