Esempio n. 1
0
def save2surfgord(lsurf,
                  rsurf,
                  out_dir,
                  surf_name,
                  bfp_path='.',
                  save_png=True):
    # if label is zero, black out surface, attribute should be nan
    num_vert = lsurf.vertices.shape[0]
    lab = spio.loadmat(
        os.path.join(bfp_path, 'supp_data/USCBrain_grayordinate_labels.mat'))
    labs = lab['labels'].squeeze()
    labs = sp.float64(labs)
    lsurf.attributes[labs[:num_vert] == 0] = sp.nan
    rsurf.attributes[labs[num_vert:2 * num_vert] == 0] = sp.nan
    lsurf.vColor[sp.isnan(lsurf.attributes), :] = 0
    rsurf.vColor[sp.isnan(lsurf.attributes), :] = 0

    writedfs(out_dir + '/Right_' + surf_name + '.dfs', rsurf)
    writedfs(out_dir + '/Left_' + surf_name + '.dfs', lsurf)

    if VTK_INSTALLED == 0:
        print('VTK is not installed, screenshots will not be saved.')
        save_png = False

    if save_png == True:
        # Visualize left hemisphere
        view_patch_vtk(lsurf,
                       azimuth=100,
                       elevation=180,
                       roll=90,
                       outfile=out_dir + '/LeftLateral_' + surf_name + '.png',
                       show=0)
        view_patch_vtk(lsurf,
                       azimuth=-100,
                       elevation=180,
                       roll=-90,
                       outfile=out_dir + '/LeftMedial_' + surf_name + '.png',
                       show=0)
        # Visualize right hemisphere
        view_patch_vtk(rsurf,
                       azimuth=-100,
                       elevation=180,
                       roll=-90,
                       outfile=out_dir + '/RightLateral_' + surf_name + '.png',
                       show=0)
        view_patch_vtk(rsurf,
                       azimuth=100,
                       elevation=180,
                       roll=90,
                       outfile=out_dir + '/RightMedial_' + surf_name + '.png',
                       show=0)
Esempio n. 2
0
#
# 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]
pval = 0 * rho_all1

for jj in range(rho_all1.shape[1]):
    pval[:, jj] = sp.sum(rho_null > rho_all1[:, jj][:, None], axis=1)
Esempio n. 3
0
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]

vrest = scipy.io.loadmat('/big_disk/ajoshi/with_andrew/100307/100307.\
Esempio n. 4
0
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]):
    lsurf.attributes = (diffafter[ind, nVert:])**2  #diffafter[ind, nVert:]
    fname1 = 'rest2motor_right_%d_d.png' % ind
    fname2 = 'rest2motor_right_%d_m.png' % ind
    #    lsurf.attributes = smooth_surf_function(lsurf, lsurf.attributes, a1=1.1, a2=1.1)
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')
Esempio n. 6
0
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):])
    fname1 = 'rest_after_rot_right_%d_d.png' % ind
    fname2 = 'rest_after_rot_right_%d_m.png' % ind
    dfs_right_sm = patch_color_attrib(dfs_right_sm, clim=[0.8, 1])
    view_patch_vtk(dfs_right_sm,
Esempio n. 7
0
    outfile = 'BCI-DNI_Perirhinal' + '.' + 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] + '.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)
Esempio n. 8
0
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 16 15:51:16 2016

@author: ajoshi
"""
from dfsio import readdfs
from surfproc import view_patch_vtk

subbasename = '/big_disk/ajoshi/fs_dir/co20050723_090747MPRAGET1Coronals002a001'
hemi = 'left'

s = readdfs(subbasename + '/' + hemi + '.mid.dfs')

view_patch_vtk(s,
               outfile=subbasename + '/mri/BST/fs_' + hemi + '1.png',
               show=0)
view_patch_vtk(s,
               outfile=subbasename + '/mri/BST/fs_' + hemi + '2.png',
               azimuth=-90,
               roll=90,
               show=0)

s = readdfs(subbasename + '/mri/BST/orig.' + hemi + '.mid.cortex.svreg.dfs')

view_patch_vtk(s,
               outfile=subbasename + '/mri/BST/bst_' + hemi + '1.png',
               show=0)
view_patch_vtk(s,
               outfile=subbasename + '/mri/BST/bst_' + hemi + '2.png',
               azimuth=-90,
Esempio n. 9
0
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')
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
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)

#%%
lsurf.attributes = np.sqrt(np.sum((diffAdhdInatt), axis=0))
lsurf.attributes = lsurf.attributes[nVert:2 * nVert] / 15
#lsurf.attributes = smooth_surf_function(lsurf, lsurf.attributes)#, a1=1.1, a2=1.1)
lsurf = patch_color_attrib(lsurf, clim=[0.1, .3])
view_patch_vtk(lsurf,
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)

rho_after = sp.sum(sub1seg1rot * sub1seg2, axis=1) / sub2seg2.shape[1]
dfs_ref = patch_color_attrib(dfs_ref, rho_after, clim=[0, .7])
writedfs('temp.dfs', dfs_ref)
_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]
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)
    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])
ind = (dist_all_rot < 1e-6)
dfs_right_sm.vColor[ind, :] = 0.5
view_patch_vtk(dfs_right_sm,
               azimuth=-90,
        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)

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')
Esempio n. 16
0
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]

vrest1 = scipy.io.loadmat('/big_disk/ajoshi/epilepsy/\
Esempio n. 17
0
plt.figure()
plt.imshow(np.abs(B))
plt.show()

for nClusters in [2, 3, 4, 5, 6]:
    SC = SpectralClustering(n_clusters=nClusters, affinity='precomputed')
    #SC=SpectralClustering(n_clusters=nClusters,assign_labels='discretize')
    labs = SC.fit_predict(B)

    r = dfs_left_sm
    r.labels = r.labels * 0
    r.labels[msk_small_region] = labs + 1

    dfs_left_sm = patch_color_labels(dfs_left_sm)
    #dfs_left_sm.vColor[sp.absolute(rho) < 1e-116, :] = 0.5
    filename = 'c' + str(nClusters) + 'labels_1.png'
    view_patch_vtk(dfs_left_sm,
                   azimuth=90,
                   elevation=180,
                   roll=90,
                   outfile=filename,
                   show=1)

    filename = 'c' + str(nClusters) + 'labels_2.png'
    view_patch_vtk(dfs_left_sm,
                   azimuth=-90,
                   elevation=-180,
                   roll=-90,
                   outfile=filename,
                   show=1)
Esempio n. 18
0
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)

bs1 = readdfs(
Esempio n. 19
0
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

dfs_left_sm.attributes = rho
dfs_left_sm = patch_color_attrib(dfs_left_sm, rho, clim=[-1, 1])
view_patch_vtk(dfs_left_sm,
    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)
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)
ind_subsample = sp.arange(start=0, stop=dfs_left.labels.shape[0], step=100)
ind_rois_orig = sp.in1d(dfs_left.labels, [46, 3, 4, 28, 29, 68, 69, 70])
ind_rois = sp.full(ind_rois_orig.shape[0], False, dtype=bool)
ind_rois = ind_rois_orig.copy()
#ind_rois[ind_subsample] = True

ind_rois = sp.nonzero(ind_rois)[0]

surf1 = dfs_left_sm

surf1.attributes = sp.zeros(surf1.vertices.shape[0])
surf1.attributes[ind_rois] = 1
surf1 = patch_color_attrib(surf1)
view_patch_vtk(surf1,
               show=1,
               azimuth=90,
               elevation=180,
               roll=90,
               outfile='motor_region.png')

# 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]
s = r
s.vColor = sp.zeros(s.vertices.shape)
label_vert, lab_count = sp.stats.mode(labs_all.T)
colr = get_cmap(nClusters + 1)
lab_count = sp.float32(lab_count.squeeze())
s.vColor = s.vColor + 1

for i in range(len(s.vertices)):
    #        print i, (lab_count[i]/sp.amax(lab_count)), colr(label_vert[0,i])[:3], (lab_count[i]/sp.amax(lab_count)), 0.55*sp.array(colr(label_vert[0,i])[:3])
    if label_vert[0, i] > 0:
        freq = ((lab_count[i] / sp.amax(lab_count)) -
                1.0 / nClusters) * (sp.float32(nClusters) / (nClusters - 1.0))
        s.vColor[i, ] = (1 -
                         freq) + freq * sp.array(colr(label_vert[0, i])[:3])

view_patch(s)
view_patch_vtk(s)
writedfs('outclustering_pc.dfs', s)
#    view_patch(r,r.labels)
#mesh = mlab.triangular_mesh(r.vertices[:,0], r.vertices[:,1], r.vertices[:,2], r.faces, representation='surface',
#                            opacity=1,scalars=np.float64(r.labels))
#    #mlab.pipeline.surface(mesh)
#mlab.gcf().scene.parallel_projection = True
#mlab.view(azimuth=0, elevation=-90)
#mlab.show()
#mlab.savefig(filename = 'dict_learning_1.png')
#mlab.view(azimuth=0, elevation=90)
#mlab.savefig(filename = 'dict_learning_2.png')
#mlab.close()
Esempio n. 24
0
    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])
view_patch_vtk(dfs_left_sm,
               azimuth=-90,
               elevation=-180,
               roll=-90,
Esempio n. 25
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)

#%% Visualization of ADHD diff from the atlas
lsurf.attributes = np.sqrt(np.sum((diffAdhdInatt), axis=1))
lsurf.attributes = lsurf.attributes[:nVert] / 50
rsurf.attributes = np.sqrt(np.sum((diffAdhdInatt), axis=1))
rsurf.attributes = rsurf.attributes[nVert:2 * nVert] / 50
lsurf = patch_color_attrib(lsurf, clim=[0, .2])
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]

vrest = scipy.io.loadmat('/big_disk/ajoshi/with_andrew/100307/100307.\
Esempio n. 27
0
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)
Esempio n. 28
0
rho_rho = []
rho_all = []

s1 = sp.load('labs_concat_data_100_clusters.npz')

catlab = s1['labs_cat']

tlab = sp.zeros((catlab.shape[0], 2))

tlab[:, 0] = catlab

dfs_left_sm.labels = catlab
dfs_left_sm = patch_color_labels(dfs_left_sm, shuffle=False)
view_patch_vtk(dfs_left_sm,
               azimuth=90,
               elevation=180,
               roll=90,
               outfile='conc_joint1' + str(nClusters) + '_clusters_new2.png',
               show=0)

view_patch_vtk(dfs_left_sm,
               azimuth=-90,
               elevation=-180,
               roll=-90,
               outfile='conc_joint2' + str(nClusters) + '_clusters_new2.png',
               show=0)

s1 = sp.load('labs_all_data_bothsessions_' + str(nClusters) + '_clusters.npz')
l = s1['lab_sub']
l1 = sp.reshape(l[:, :40], (l.shape[0] * 40), order='F')

l2 = sp.reshape(l[:, 40:80], (l.shape[0] * 40), order='F')
Esempio n. 29
0
nClusters = 30

ref = '100307'
print(ref + '.reduce' + str(r_factor) + '.LR_mask.mat')
fn1 = ref + '.reduce' + str(r_factor) + '.LR_mask.mat'
fname1 = os.path.join(ref_dir, fn1)
msk = scipy.io.loadmat(fname1)  # h5py.File(fname1);
dfs_left = readdfs(
    os.path.join(p_dir_ref, 'reference', ref + '.aparc.\
a2009s.32k_fs.reduce3.left.dfs'))
dfs_left_sm = readdfs(
    os.path.join(p_dir_ref, 'reference', ref + '.aparc.\
a2009s.32k_fs.reduce3.very_smooth.left.dfs'))
view_patch_vtk(dfs_left_sm,
               azimuth=90,
               elevation=180,
               roll=90,
               outfile='sub.png',
               show=1)

count1 = 0
rho_rho = []
rho_all = []
cc_msk = (dfs_left.labels > 0)

sub = lst[0]
data = scipy.io.loadmat(
    os.path.join(p_dir, sub, sub + '.rfMRI_REST1_LR.\
reduce3.ftdata.NLM_11N_hvar_25.mat'))
LR_flag = msk['LR_flag']
LR_flag = np.squeeze(LR_flag) != 0
data = data['ftdata_NLM']
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)
rh32k.vertices = gR.darrays[0].data