Exemplo n.º 1
0
def looping((sub, hemi)):

    highres_file = '/scr/ilz3/myelinconnect/struct/surf_%s/prep_t1/profiles/%s_%s_mid_proflies.vtk'%(hemi, sub, hemi)
    label_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.npy'%(sub, hemi)
    old_lowres_file = '/scr/ilz3/myelinconnect/groupavg/indv_space/%s/lowres_%s_d_def.vtk'%(sub, hemi)

    
    new_lowres_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/%s_%s_profiles.vtk'%(sub, hemi)
    data_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/%s_%s_profiles.npy'%(sub, hemi)


    # load data
    labels = np.load(label_file)[:,1]
    highres_v, highres_f, highres_d = read_vtk(highres_file)
    lowres_v, lowres_f, lowres_d = read_vtk(old_lowres_file)

    # call to sampling function
    new_lowres_d = sample_simple(highres_d, labels)

    # save lowres vtk and txt
    #write_vtk(new_lowres_file, lowres_v, lowres_f, data=new_lowres_d)
    np.save(data_file, new_lowres_d)
    
    if os.path.isfile(data_file):
        print sub+' '+hemi+' finished'

    return
Exemplo n.º 2
0
def looping((sub, hemi)):

    highres_file = '/scr/ilz3/myelinconnect/struct/surf_%s/prep_t1/profiles/%s_%s_mid_proflies.vtk'%(hemi, sub, hemi)
    label_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.npy'%(sub, hemi)
    old_lowres_file = '/scr/ilz3/myelinconnect/groupavg/indv_space/%s/lowres_%s_d_def.vtk'%(sub, hemi)

    
    new_lowres_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/raw/%s_%s_profiles_raw.vtk'%(sub, hemi)
    data_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/raw/%s_%s_profiles_raw.npy'%(sub, hemi)
    cheb_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/raw/%s_%s_coeff_raw.npy'%(sub, hemi)
    cheb_vtk_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/raw/%s_%s_coeff_raw.vtk'%(sub, hemi)


    # load data
    labels = np.load(label_file)[:,1]
    highres_v, highres_f, highres_d = read_vtk(highres_file)
    lowres_v, lowres_f, lowres_d = read_vtk(old_lowres_file)

    # call to sampling function
    new_lowres_d = sample_simple(highres_d, labels)

    # save lowres vtk and txt
    write_vtk(new_lowres_file, lowres_v, lowres_f, data=new_lowres_d)
    np.save(data_file, new_lowres_d)
    
    # calculate chebychev coefficients
    t1_3_7 = new_lowres_d[:,3:8]
    coeff, poly = chebapprox(t1_3_7, degree=4)
    np.save(cheb_file, coeff)
    write_vtk(cheb_vtk_file, lowres_v, lowres_f, data=coeff)
    
    if os.path.isfile(data_file):
        print sub+' '+hemi+' finished'

    return
Exemplo n.º 3
0
def create_mapping((sub, hemi)):
    
    print sub, hemi

    complex_file = '/scr/ilz3/myelinconnect/struct/surf_%s/orig/mid_surface/%s_%s_mid.vtk'
    simple_file = '/scr/ilz3/myelinconnect/groupavg/indv_space/%s/lowres_%s_d_def.vtk'# version d
    
    log_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/logs/log_worker_%s.txt'%(str(os.getpid()))
    seed_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/seeds/%s_%s_highres2lowres_seeds.npy'
    label_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.npy'
    surf_label_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.vtk'

    # load the meshes
    log(log_file, 'Processing %s %s'%(sub, hemi))
    log(log_file, '...loading data', logtime=False)
    complex_v, complex_f, complex_d = read_vtk(complex_file%(hemi, sub, hemi))
    simple_v, simple_f, simple_d = read_vtk(simple_file%(sub, hemi))

    # find those points on the individuals complex mesh that correspond best
    # to the simplified group mesh in subject space
    log(log_file, '...finding unique voronoi seeds')
    
    try:
        voronoi_seed_idx = np.load(seed_file%(sub, hemi))
        voronoi_seed_coord = complex_v[voronoi_seed_idx]
    
    except IOError:
    
        voronoi_seed_idx, inaccuracy  = find_voronoi_seeds(simple_v, complex_v)
        np.save(seed_file%(sub, hemi), voronoi_seed_idx)
        
        # find coordinates of those points in the highres mesh
        voronoi_seed_coord = complex_v[voronoi_seed_idx]
        
        # double check differences
        log(log_file, '...checking unique vs nearest mapping')
        dist = np.linalg.norm((voronoi_seed_coord - simple_v), axis=1)
        if ((np.mean(dist)-np.mean(inaccuracy[:,0])>0.1)):
            log(log_file, 'Unique seeds very far from nearest seeds!')
            return dist
            sys.exit("Unique seeds very far from nearest seeds %s %s"%(sub,hemi))


    # convert highres mesh into graph containing edge length
    log(log_file, '...creating graph')
    complex_graph = graph_from_mesh(complex_v, complex_f, edge_length=True)

    # find the actual labels
    log(log_file, '...competetive fast marching')
    labels = competetive_fast_marching(complex_v, complex_graph, voronoi_seed_idx)

    # write out labelling file and surface with labels
    log(log_file, '...saving data')
    np.save(label_file%(sub, hemi), labels)
    write_vtk(surf_label_file%(sub, hemi), complex_v, complex_f,
                data=labels[:,1, np.newaxis])

    log(log_file, 'Finished %s %s'%(sub, hemi))

    return log_file
def looping((sub, hemi, sess)):
    
    print 'running', sub, hemi, sess
    label_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.npy'%(sub, hemi)
    rest_file = '/scr/ilz3/myelinconnect/resting/final/%s_rest%s_denoised.nii.gz'%(sub, sess)
    highres_file = '/scr/ilz3/myelinconnect/struct/surf_%s/orig2func/rest%s/%s_%s_mid_groupavgsurf.vtk'%(hemi, sess, sub, hemi)
        
    data_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/rest/%s_%s_rest%s_smooth_3.npy'%(sub, hemi, sess)

    # load data
    labels = np.load(label_file)[:,1]
    highres_v, highres_f, highres_d = read_vtk(highres_file)
    
    # sample resting state time series on highres mesh
    rest_highres = sample_volume(rest_file, highres_v)
    
    # average across highres vertices that map to the same lowres vertex
    rest_lowres = sample_simple(rest_highres, labels)

    # save data
    np.save(data_file, rest_lowres)
    
    if os.path.isfile(data_file):
        print sub+' '+hemi+' ' +sess+' finished'

    return data_file
def fit_iteration((smooth, iteration)):
    
    print smooth, iteration, hemi

    _,_,t1 = read_vtk(random_file%(smooth, hemi, str(iteration)))
    masked_t1 = np.delete(t1, mask)
    masked_orig = np.delete(t1_orig, mask)
    
    # histogramm matching to original t1
    masked_t1 = hist_match(masked_t1, masked_orig)
    
    clf = linear_model.LinearRegression()
    clf.fit(masked_embed[:,maps], masked_t1)
    
    modelled_fit = clf.predict(masked_embed[:,maps])
    residuals = masked_t1 - clf.predict(masked_embed[:,maps])
    
    # write data back into cortex dimensions to avoid confusion
    modelled_fit_cortex = np.zeros((t1.shape[0])) 
    modelled_fit_cortex[nonmask_small]=modelled_fit
    residuals_cortex = np.zeros((t1.shape[0]))  
    residuals_cortex[nonmask_small]=residuals
    t1_cortex = np.zeros((t1.shape[0]))
    t1_cortex[nonmask_small]=masked_t1
    
    model_dict = dict()
    model_dict['maps']=maps
    model_dict['coef']=clf.coef_
    model_dict['modelled_fit']=modelled_fit_cortex
    model_dict['residuals']=residuals_cortex
    model_dict['t1']=t1_cortex
    model_dict['score'] = clf.score(masked_embed[:,maps], masked_t1)
    model_dict['corr'] = stats.pearsonr(modelled_fit, masked_t1)
    
    pkl_out = open(model_file%(smooth, hemi, iteration, maps_str), 'wb')
    pickle.dump(model_dict, pkl_out)
    pkl_out.close()
    
    return

if __name__ == "__main__":

    ### load data
    print 'loading'
    rh_mesh_file = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/rh_lowres_new.vtk'
    lh_mesh_file = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/lh_lowres_new.vtk'
    full_mask_file = '/scr/ilz3/myelinconnect/new_groupavg/masks/fullmask_lh_rh_new.npy'
    rh_t1_file = '/scr/ilz3/myelinconnect/new_groupavg/t1/smooth_1.5/rh_t1_avg_smooth_1.5.npy'
    lh_t1_file = '/scr/ilz3/myelinconnect/new_groupavg/t1/smooth_1.5/lh_t1_avg_smooth_1.5.npy'
    embed_file = '/scr/ilz3/myelinconnect/new_groupavg/embed/both_smooth_3_embed.npy'
    embed_dict_file = '/scr/ilz3/myelinconnect/new_groupavg/embed/both_smooth_3_embed_dict.pkl'
    model_file_template = '/scr/ilz3/myelinconnect/new_groupavg/model/linear_combination/t1avg/smooth_1.5/model_comparison/model_%s.pkl'

    lv, lf, _ = read_vtk(lh_mesh_file)
    lh_t1 = np.load(lh_t1_file)
    rv, rf, _ = read_vtk(rh_mesh_file)
    rh_t1 = np.load(rh_t1_file)

    mask = np.load(full_mask_file)

    # prepare embedding (normalized from entry in dict)
    pkl_in = open(embed_dict_file, 'r')
    embed_dict = pickle.load(pkl_in)
    pkl_in.close()

    embed_masked = np.zeros(
        (embed_dict['vectors'].shape[0], embed_dict['vectors'].shape[1] - 1))
    for comp in range(100):
        embed_masked[:, comp] = (embed_dict['vectors'][:, comp + 1] /
Exemplo n.º 7
0
from vtk_rw import read_vtk
import numpy as np
import gdist


mesh_file="/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/%s_lowres_new.vtk"
hemis=['rh', 'lh']

for hemi in hemis:
    v, f, _ = read_vtk(mesh_file %hemi)
    
    v = v.astype(np.float64)
    f = f.astype(np.int32)
    dist_matrix=gdist.local_gdist_matrix(v, f)
Exemplo n.º 8
0
hemis = ['rh', 'lh']
sessions = ['1_1', '1_2', '2_1', '2_2']

mesh_file = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/%s_lowres_new.vtk'
data_file = '/scr/ilz3/myelinconnect/new_groupavg/rest/smooth_3/%s_%s_rest%s_smooth_3.npy'
vtk_data_file = '/scr/ilz3/myelinconnect/new_groupavg/rest/smooth_3/%s_%s_rest%s_smoothdata.vtk'

mode = 'vtk2npy'

if mode == 'npy2vtk':

    print 'npy to vtk'

    for hemi in hemis:

        v, f, d = read_vtk(mesh_file % (hemi))

        for sub in subjects:

            #            print hemi, sub
            #            data = np.load(data_file%(sub, hemi))
            #            write_vtk(vtk_data_file%(sub, hemi), v,f,data=data)

            for sess in sessions:

                print hemi, sub, sess

                data = np.load(data_file % (sub, hemi, sess))
                write_vtk(vtk_data_file % (sub, hemi, sess), v, f, data=data)

elif mode == 'vtk2npy':
Exemplo n.º 9
0
from vtk_rw import read_vtk
from ply_rw import write_ply
'''
Meta script to convert vtk to ply file
'''

in_vtk = raw_input("input vtk file: ")
out_ply = raw_input("output ply file: ")
comment = raw_input("comment (optional): ")

if comment == "":
    comment = 'ply format converted from vtk'

v, f, d = read_vtk(in_vtk)
write_ply(out_ply, v, f, comment)
Exemplo n.º 10
0
inv2prob_full_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/masks/%s_inv2prob_full.npy'
inv2prob_min_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/masks/%s_inv2prob_min.npy'


for hemi in hemis:
    
    n_vertices = np.load(label_file%(subjects[0], hemi))[:,1].max()+1
    tsnr = np.zeros((n_vertices, len(subjects)*len(sessions)))
    inv2prob = np.zeros((n_vertices, len(subjects)))

    inv2prob_count = 0
    tsnr_count = 0
    for sub in subjects: 
        
        labels = np.load(label_file%(sub, hemi))[:,1]
        inv2prob_highres_v, inv2prob_highres_f, inv2prob_highres_d = read_vtk(inv2prob_highres_file%(sub, hemi))
        
        # average across highres vertices that map to the same lowres vertex
        inv2prob[:,inv2prob_count] = np.squeeze(sample_simple(inv2prob_highres_d, labels))
        
        inv2prob_count += 1
        
        for sess in sessions:
             
            print sub, sess
             
            highres_func_v, highres_func_f, highres_func_d = read_vtk(highres_func_file%(hemi, sess, sub, hemi))
              
            # sample resting state time series on highres mesh
            tsnr_highres = sample_volume(tsnr_file%(sub, sess, sub, sess), highres_func_v)
                 
subjects = pd.read_csv('/scr/ilz3/myelinconnect/subjects.csv')
subjects=list(subjects['DB'])
subjects.remove('KSMT')

hemis = ['rh', 'lh']
sessions = ['1_1', '1_2', '2_1', '2_2']

for hemi in hemis:
    for sess in sessions:
    
        mapping_x='/scr/ilz3/myelinconnect/mappings/rest2groupavg_surf/rest%s/x/%s_lowres_new_avgsurf_groupdata.vtk'%(sess, hemi)
        mapping_y='/scr/ilz3/myelinconnect/mappings/rest2groupavg_surf/rest%s/y/%s_lowres_new_avgsurf_groupdata.vtk'%(sess, hemi)
        mapping_z='/scr/ilz3/myelinconnect/mappings/rest2groupavg_surf/rest%s/z/%s_lowres_new_avgsurf_groupdata.vtk'%(sess, hemi)
        
        v, f, dx = read_vtk(mapping_x)
        _, _, dy = read_vtk(mapping_y)
        _, _, dz = read_vtk(mapping_z) 

        for sub in range(len(subjects)):

            tsnr_file='/scr/ilz3/myelinconnect/resting/preprocessed/%s/rest%s/realignment/corr_%s_rest%s_roi_tsnr.nii.gz'%(subjects[sub], sess, subjects[sub], sess)
            out_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_%s_rest%s.npy'%(subjects[sub], hemi, sess)
            #out_vtk =  '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_%s_rest%s.vtk'%(subjects[sub], hemi, sess)
            
            tsnr_vol = nb.load(tsnr_file).get_data()
            tsnr_mesh = np.zeros(dx.shape[0],)
            
            coords = np.vstack((dx[:,sub],dy[:,sub],dz[:,sub]))
            coords = coords.T
            
from vtk_rw import read_vtk
import numpy as np

filename = '/Users/ghfc/Desktop/P8_F10_test.vtk'

#filename = '/Users/ghfc/Desktop/P8_F10_DTI.vtk'

vertex_array, face_array, data_array = read_vtk(filename)

#print vertex_array
Exemplo n.º 13
0
subjects.remove('KSMT')

hemis = ['rh', 'lh']
sessions = ['1_1', '1_2', '2_1', '2_2']

for hemi in hemis:
    for sess in sessions:

        mapping_x = '/scr/ilz3/myelinconnect/mappings/rest2groupavg_surf/rest%s/x/%s_lowres_new_avgsurf_groupdata.vtk' % (
            sess, hemi)
        mapping_y = '/scr/ilz3/myelinconnect/mappings/rest2groupavg_surf/rest%s/y/%s_lowres_new_avgsurf_groupdata.vtk' % (
            sess, hemi)
        mapping_z = '/scr/ilz3/myelinconnect/mappings/rest2groupavg_surf/rest%s/z/%s_lowres_new_avgsurf_groupdata.vtk' % (
            sess, hemi)

        v, f, dx = read_vtk(mapping_x)
        _, _, dy = read_vtk(mapping_y)
        _, _, dz = read_vtk(mapping_z)

        for sub in range(len(subjects)):

            tsnr_file = '/scr/ilz3/myelinconnect/resting/preprocessed/%s/rest%s/realignment/corr_%s_rest%s_roi_tsnr.nii.gz' % (
                subjects[sub], sess, subjects[sub], sess)
            out_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_%s_rest%s.npy' % (
                subjects[sub], hemi, sess)
            #out_vtk =  '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_%s_rest%s.vtk'%(subjects[sub], hemi, sess)

            tsnr_vol = nb.load(tsnr_file).get_data()
            tsnr_mesh = np.zeros(dx.shape[0], )

            coords = np.vstack((dx[:, sub], dy[:, sub], dz[:, sub]))
Exemplo n.º 14
0
sessions = ['1_1', '1_2', '2_1', '2_2']

tsnr_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_%s_rest%s.npy'
inv2prob_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/inv2prob/%s_lowres_new_groupavgsurf.vtk'

tsnr_full_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_tsnr_full.npy'
tsnr_min_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_tsnr_min.npy'
inv2prob_full_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/inv2prob/%s_inv2prob_full.npy'
inv2prob_min_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/inv2prob/%s_inv2prob_min.npy'

for hemi in hemis:

    n_vertices = np.load(tsnr_file % (subjects[0], hemi, sessions[0])).shape[0]
    tsnr = np.zeros((n_vertices, len(subjects) * len(sessions)))

    _, _, inv2prob = read_vtk(inv2prob_file % (hemi))

    tsnr_count = 0
    for sub in range(len(subjects)):

        for sess in sessions:

            print subjects[sub], sess

            tsnr_d = np.load(tsnr_file % (subjects[sub], hemi, sess))

            tsnr[:, tsnr_count] = tsnr_d

            tsnr_count += 1

    print 'saving'
Exemplo n.º 15
0
import numpy as np
from vtk_rw import read_vtk

vtk_file = '/nobackup/ilz3/myelinconnect/new_groupavg/profiles/smooth_1.5/%s/%s_lowres_new_avgsurf_groupdata.vtk'
pro_file = '/nobackup/ilz3/myelinconnect/new_groupavg/profiles/smooth_1.5/%s_lowres_new_avgsurf_groupdata.npy'
pro_mean_file = '/nobackup/ilz3/myelinconnect/new_groupavg/profiles/smooth_1.5/%s_lowres_new_avgsurf_groupdata_mean.npy'

for hemi in ['lh', 'rh']:

    _, _, d = read_vtk(vtk_file % (0, hemi))
    pro = np.zeros((d.shape[0], d.shape[1], 11))

    for layer in range(11):

        _, _, d = read_vtk(vtk_file % (layer, hemi))
        pro[:, :, layer] = d

    pro_mean = np.mean(pro, axis=1)

    np.save(pro_file % (hemi), pro)
    np.save(pro_mean_file % (hemi), pro_mean)
Exemplo n.º 16
0
            

        '''clustering'''
        if calc_cluster:
            if not calc_embed:
                embedding_recort = np.load(embed_file%(smooth, masktype, hemi, str(n_embedding)))
                mask = np.load(mask_file%(hemi, masktype))
            for nk in n_kmeans:
                print 'clustering %s'%str(nk)
                kmeans_recort = kmeans(embedding_recort, nk, mask)
                np.save(kmeans_file%(smooth, masktype, hemi, str(n_embedding), str(nk)),
                        kmeans_recort)
                
                if calc_subcluster:
                    print 'subclustering %s'%str(nk)
                    v, f, d = read_vtk(mesh_file%hemi)
                    subclust_arr=subcluster(kmeans_recort, f)
                    np.save(subclust_file%(smooth, masktype, hemi, str(n_embedding), str(nk)), subclust_arr)  
                     
                        

        '''subclustering'''
        if not calc_cluster:
            if calc_subcluster:
                
                v, f, d = read_vtk(mesh_file%hemi)
                
                for nk in n_kmeans:
                    print 'subclustering %s'%str(nk)
                    kmeans_recort = np.load(kmeans_file%(smooth, masktype, hemi, str(n_embedding), str(nk)))
                    subclust_arr=subcluster(kmeans_recort, f)
Exemplo n.º 17
0
hemis = ['rh', 'lh']
sessions = ['1_1', '1_2', '2_1', '2_2']

mesh_file = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/%s_lowres_new.vtk'
data_file = '/scr/ilz3/myelinconnect/new_groupavg/rest/smooth_3/%s_%s_rest%s_smooth_3.npy'
vtk_data_file = '/scr/ilz3/myelinconnect/new_groupavg/rest/smooth_3/%s_%s_rest%s_smoothdata.vtk'

mode = 'vtk2npy'

if mode == 'npy2vtk' :
    
    print 'npy to vtk'
    
    for hemi in hemis: 
        
        v,f,d = read_vtk(mesh_file%(hemi))
        
        for sub in subjects: 
            
#            print hemi, sub
#            data = np.load(data_file%(sub, hemi))
#            write_vtk(vtk_data_file%(sub, hemi), v,f,data=data)
            
             for sess in sessions:
                 
                 print hemi, sub, sess
             
                 data = np.load(data_file%(sub, hemi, sess))
                 write_vtk(vtk_data_file%(sub, hemi, sess), v,f,data=data)

                
(FC1 and FC1,5,6) for each hemisphere separately for comparison to random data.
'''

mesh_file = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/%s_lowres_new.vtk'
mask_file="/scr/ilz3/myelinconnect/new_groupavg/masks/%s_fullmask_new.npy"
fullmask_file = '/scr/ilz3/myelinconnect/new_groupavg/masks/fullmask_lh_rh_new.npy'
t1_file = '/scr/ilz3/myelinconnect/new_groupavg/t1/smooth_1.5/%s_t1_avg_smooth_1.5.npy'
embed_dict_file="/scr/ilz3/myelinconnect/new_groupavg/embed/both_smooth_3_embed_dict.pkl"
model_file = '/scr/ilz3/myelinconnect/new_groupavg/model/linear_combination/t1avg/smooth_1.5/%s_t1avg_by_fc_maps_%s.pkl'


all_maps = [[0],[0,4,5]]
all_maps_str = ['0', 'best']

# load one file for each hemishpere once to get dimensions
lv,_,_ = read_vtk(mesh_file%('lh'))
rv,_,_ = read_vtk(mesh_file%('rh'))

# prepare embedding (normalized from entry in dict)
pkl_in = open(embed_dict_file, 'r')
embed_dict=pickle.load(pkl_in)
pkl_in.close()

embed_masked = np.zeros((embed_dict['vectors'].shape[0], embed_dict['vectors'].shape[1]-1))
for comp in range(100):
    embed_masked[:,comp]=(embed_dict['vectors'][:,comp+1]/embed_dict['vectors'][:,0])

fullmask = np.load(fullmask_file)
# unmask embed which has been saved in masked form
idcs_full=np.arange(0,(lv.shape[0]+rv.shape[0]))
nonmask_full=np.delete(idcs_full, fullmask)
Exemplo n.º 19
0
import numpy as np
import gdist
from vtk_rw import read_vtk
import time

sub = 'BP4T'
hemi = 'lh'

complex_file = '/scr/ilz3/myelinconnect/struct/surf_%s/orig/mid_surface/%s_%s_mid.vtk'%(hemi, sub, hemi)
simple_file = '/scr/ilz3/myelinconnect/groupavg/indv_space/%s/lowres_%s_d_def.vtk'%(sub, hemi)# version d

complex_v, complex_f, complex_d = read_vtk(complex_file)
complex_vertices = complex_v.astype(np.float64)
complex_faces = complex_f.astype(np.int32)

simple_v, simple_f, simple_d = read_vtk(simple_file)
simple_vertices = simple_v.astype(np.float64)
simple_faces = simple_f.astype(np.int32)

complex_radius = 2 

print time.ctime()
complex_matrix=gdist.local_gdist_matrix(complex_vertices, complex_faces, max_distance=complex_radius)

for sv in range(len(simple_v.shape[0])):
    dist = 1000000
    for cv in range(len(complex_v.shape[0])):
        if sv in complex_matrix[:,cv].indices:
            dist_now = complex_matrix[:,cv][sv].data
            if dist_now < dist:
                dist = dist_now
# TO DO: allow transformation input, find a better way to get pixdims?

vol_file = raw_input('volume file: ')
surf = raw_input('surface file: ') 
out = raw_input('output file: ')  


# load information from volume
vol = nb.load(vol_file)
vol_data = vol.get_data()
vol_hdr = vol.get_header()
vol_affine = vol.get_affine()

# read vertices from surface
if surf[-3:] == 'vtk':
    v, f = read_vtk(surf)
elif surf[-3:] == 'ply':
    v, f = read_ply(surf)
else:
    print "only vtk and ply format supported yet"


# scale and translate surface into volume space
pixdims = list(vol_hdr['pixdim'][1:4])

v_scaled = np.empty_like(v)
v_scaled[:, 0] = ((1. / pixdims[0]) * v[:, 0])
v_scaled[:, 1] = (-(1. / pixdims[1]) * v[:, 1])
v_scaled[:, 2] = (-(1. / pixdims[2]) * v[:, 2])

trans = [0, (vol.shape[1] - 1), (vol.shape[2] - 1)]
'''
# TO DO: allow transformation input, find a better way to get pixdims?

vol_file = raw_input('volume file: ')
surf = raw_input('surface file: ')
out = raw_input('output file: ')

# load information from volume
vol = nb.load(vol_file)
vol_data = vol.get_data()
vol_hdr = vol.get_header()
vol_affine = vol.get_affine()

# read vertices from surface
if surf[-3:] == 'vtk':
    v, f = read_vtk(surf)
elif surf[-3:] == 'ply':
    v, f = read_ply(surf)
else:
    print "only vtk and ply format supported yet"

# scale and translate surface into volume space
pixdims = list(vol_hdr['pixdim'][1:4])

v_scaled = np.empty_like(v)
v_scaled[:, 0] = ((1. / pixdims[0]) * v[:, 0])
v_scaled[:, 1] = (-(1. / pixdims[1]) * v[:, 1])
v_scaled[:, 2] = (-(1. / pixdims[2]) * v[:, 2])

trans = [0, (vol.shape[1] - 1), (vol.shape[2] - 1)]


if __name__ == "__main__":
    
    ### load data
    print 'loading'
    rh_mesh_file='/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/rh_lowres_new.vtk'
    lh_mesh_file='/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/lh_lowres_new.vtk'
    full_mask_file='/scr/ilz3/myelinconnect/new_groupavg/masks/fullmask_lh_rh_new.npy'
    rh_t1_file='/scr/ilz3/myelinconnect/new_groupavg/t1/smooth_1.5/rh_t1_avg_smooth_1.5.npy'
    lh_t1_file='/scr/ilz3/myelinconnect/new_groupavg/t1/smooth_1.5/lh_t1_avg_smooth_1.5.npy'
    embed_file='/scr/ilz3/myelinconnect/new_groupavg/embed/both_smooth_3_embed.npy'
    embed_dict_file='/scr/ilz3/myelinconnect/new_groupavg/embed/both_smooth_3_embed_dict.pkl'
    
    lv,lf,_ = read_vtk(lh_mesh_file)
    lh_t1 = np.load(lh_t1_file)
    rv,rf,_ = read_vtk(rh_mesh_file)
    rh_t1 = np.load(rh_t1_file)
    
    mask = np.load(full_mask_file)
    
    # prepare embedding (normalized from entry in dict)
    pkl_in = open(embed_dict_file, 'r')
    embed_dict=pickle.load(pkl_in)
    pkl_in.close()
    
    embed_masked = np.zeros((embed_dict['vectors'].shape[0], embed_dict['vectors'].shape[1]-1))
    for comp in range(100):
        embed_masked[:,comp]=(embed_dict['vectors'][:,comp+1]/embed_dict['vectors'][:,0])
    
Exemplo n.º 23
0
tsnr_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_%s_rest%s.npy'
inv2prob_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/inv2prob/%s_lowres_new_groupavgsurf.vtk'

tsnr_full_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_tsnr_full.npy'
tsnr_min_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/tsnr/%s_tsnr_min.npy'
inv2prob_full_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/inv2prob/%s_inv2prob_full.npy'
inv2prob_min_file = '/scr/ilz3/myelinconnect/new_groupavg/snr/inv2prob/%s_inv2prob_min.npy'


for hemi in hemis:
    
    n_vertices = np.load(tsnr_file%(subjects[0], hemi, sessions[0])).shape[0]
    tsnr = np.zeros((n_vertices, len(subjects)*len(sessions)))
    
    _, _, inv2prob = read_vtk(inv2prob_file%(hemi))

    tsnr_count = 0
    for sub in range(len(subjects)): 
         
        for sess in sessions:
              
            print subjects[sub], sess
              
            tsnr_d = np.load(tsnr_file%(subjects[sub], hemi, sess))
                  
            tsnr[:,tsnr_count] = tsnr_d
                          
            tsnr_count += 1
            
    print 'saving'
Exemplo n.º 24
0
from __future__ import division
import numpy as np
import h5py
from scipy.spatial.distance import pdist
from vtk_rw import read_vtk

lh_mesh = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/lh_lowres_new.vtk'
rh_mesh = '/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/rh_lowres_new.vtk'
mat_file = '/scr/ilz3/myelinconnect/new_groupavg/corr/both_euclid_dist.hdf5'

# load t1 data
print 'load data'
v_left, _, _ = read_vtk(lh_mesh)
v_right, _, _ = read_vtk(rh_mesh)
v = np.concatenate((v_left, v_right))
size = v.shape[0]

# calculate t1 distances for each subject and average
print 'calc mat'
dist_upper = pdist(v,'euclidean')

# save
print 'saving matrix'
f = h5py.File(mat_file, 'w')
f.create_dataset('upper', data=dist_upper)
f.create_dataset('shape', data=(size, size))
f.close()
Exemplo n.º 25
0
mean_pro_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/%s_%s_profiles_mean_3_7.npy'
avg_pro_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/avg_%s_profiles.npy'
avg_mean_pro_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/avg_%s_mean_3_7.npy'

avg_pro_surf = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/avg_%s_profiles_%s.vtk'
avg_mean_surf = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/avg_%s_mean_3_7.vtk'

surf_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/surfs/lowres_%s_d.vtk'

for hemi in hemis:
    
    get_size = np.load('/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/%s_%s_profiles.npy'%(subjects[0], hemi)).shape
    avg_pro = np.zeros((get_size[0], get_size[1]))
    avg_mean = np.zeros((get_size[0]))
    
    v,f,d = read_vtk(surf_file%(hemi))
    
    count = 0
    for sub in subjects:
        
        profiles = np.load(pro_file%(sub, hemi))
        
        
        try:
            mean_3_7 = np.load(mean_pro_file%(sub, hemi))
        
        except IOError:
            mean_3_7= np.mean(profiles[:,layer_low:layer_high], axis=1)
            np.save(mean_pro_file%(sub, hemi), mean_3_7)
        
        avg_pro += profiles
from vtk_rw import read_vtk
from ply_rw import write_ply

'''
Meta script to convert vtk to ply file
'''

in_vtk = raw_input("input vtk file: ")
out_ply = raw_input("output ply file: ")
comment = raw_input("comment (optional): ")

if comment == "":
    comment = 'ply format converted from vtk'

v, f, d= read_vtk(in_vtk)
write_ply(out_ply, v, f, comment)
Exemplo n.º 27
0
                                     'labels min',
                                     'labels max',
                                     'labels mean',
                                     'labels sdv',
                                     'vertex no label',
                                     'label no vertex'])

for sub in subjects:
    for hemi in hemis:

        if os.path.isfile('/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.npy'%(sub, hemi)):
            
            print 'processing '+sub+' '+hemi
            
            # load data
            complex_v,complex_f, complex_d = read_vtk('/scr/ilz3/myelinconnect/struct/surf_%s/orig/mid_surface/%s_%s_mid.vtk'%(hemi, sub, hemi))
            simple_v, simple_f, simple_d = read_vtk('/scr/ilz3/myelinconnect/groupavg/indv_space/%s/lowres_%s_d_def.vtk'%(sub, hemi))
            labelling=np.load('/scr/ilz3/myelinconnect/all_data_on_simple_surf/labels/%s_%s_highres2lowres_labels.npy'%(sub, hemi))
            seeds=np.load('/scr/ilz3/myelinconnect/all_data_on_simple_surf/seeds/%s_%s_highres2lowres_seeds.npy'%(sub, hemi))

            # make an edge label surface and save it
            G = graph_from_mesh(complex_v, complex_f)
            edge_labelling = np.zeros_like(labelling[:,1])
            for v in range(edge_labelling.shape[0]):
                if any(labelling[G.neighbors(v),1] != labelling[v,1]):
                    edge_labelling[v] = 1
            write_vtk(edge_file%(sub, hemi), complex_v, complex_f, 
                      data=edge_labelling[:,np.newaxis])
            
            # make a no label surface and save it
            if np.where(labelling[:,1]==-1)[0].shape[0] >0:
Exemplo n.º 28
0
import numpy as np
from vtk_rw import read_vtk, write_vtk

iterations = 1000

hemis = ['rh', 'lh']
mesh_file = "/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/%s_lowres_new.vtk"
t1_file = '/scr/ilz3/myelinconnect/new_groupavg/t1/smooth_3/%s_t1_avg_smooth_3.npy'
mask_file = '/scr/ilz3/myelinconnect/new_groupavg/masks/%s_fullmask_new.npy'
random_npy_file = '/scr/ilz3/myelinconnect/new_groupavg/model/random_data/raw/%s_random_normal_%s.npy'
random_vtk_file = '/scr/ilz3/myelinconnect/new_groupavg/model/random_data/raw/%s_random_normal_%s.vtk'
'''
Create random datasets by drawing from a normal distribution and write them to 
group average surface for subsequent smoothing with cbstools.
'''

for hemi in hemis:
    print hemi

    v, f, d = read_vtk(mesh_file % hemi)

    for r in range(iterations):
        print r
        random_data = np.random.randn(v.shape[0])
        np.save(random_npy_file % (hemi, str(r)), random_data)
        write_vtk(random_vtk_file % (hemi, str(r)),
                  v,
                  f,
                  data=random_data[:, np.newaxis])
Exemplo n.º 29
0
        mask_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/masks/%s_fullmask.npy'%hemi
        t1_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/%s/avg_%s_profiles_%s.npy'%(smooth, hemi, smooth)
        #t1_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/%s/avg_%s_coeffs_%s.npy'%(smooth, hemi, smooth)
        #euclid_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/profile_embedding/%s_euclidian_dist_%s.hdf5'
        #corr_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/t1/profile_embedding/%s_profile_corr_%s.hdf5'
       
        affinity_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/embed/profiles/%s_%s_cheb_affinity_%s.hdf5'%(hemi, smooth, affine_method)
        embed_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/embed/profiles/%s_%s_embedding_%s_%s.npy'%(hemi, smooth, str(n_embedding), affine_method)
        embed_dict_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/embed/profiles/%s_%s_embedding_dict_%s_%s.pkl'%(hemi, smooth, str(n_embedding), affine_method)

        #affinity_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/embed/coefficients/%s_%s_cheb_affinity_%s.hdf5'%(hemi, smooth, affine_method)
        #embed_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/embed/coefficients/%s_%s_embedding_%s_%s.npy'%(hemi, smooth, str(n_embedding), affine_method)
        #embed_dict_file = '/scr/ilz3/myelinconnect/all_data_on_simple_surf/embed/coefficients/%s_%s_embedding_dict_%s_%s.pkl'%(hemi, smooth, str(n_embedding), affine_method)
 
        print 'loading data'
        v, f, d = read_vtk(mesh_file)
        t1=np.load(t1_file)
        full_shape=tuple((t1.shape[0], t1.shape[0]))


        if euclid:
            print 'euclid'
            t1_3_7_diff = sp.spatial.distance.pdist(t1[:,3:8], 'euclidean')
            f = h5py.File(euclid_file%('rh', '3_7'), 'w')
            f.create_dataset('upper', data=t1_3_7_diff)
            f.create_dataset('shape', data=full_shape)
            f.close()
            del t1_3_7_diff
            
        if corr:
            print 'corr'
Exemplo n.º 30
0
import numpy as np
from vtk_rw import read_vtk

vtk_file = '/nobackup/ilz3/myelinconnect/new_groupavg/profiles/smooth_1.5/%s/%s_lowres_new_avgsurf_groupdata.vtk'
pro_file = '/nobackup/ilz3/myelinconnect/new_groupavg/profiles/smooth_1.5/%s_lowres_new_avgsurf_groupdata.npy'
pro_mean_file = '/nobackup/ilz3/myelinconnect/new_groupavg/profiles/smooth_1.5/%s_lowres_new_avgsurf_groupdata_mean.npy'

for hemi in ['lh', 'rh']:
    
    _, _, d = read_vtk(vtk_file%(0, hemi))
    pro = np.zeros((d.shape[0], d.shape[1], 11))
    
    for layer in range(11):
        
        _, _, d = read_vtk(vtk_file%(layer, hemi))
        pro[:,:,layer] = d
        
    pro_mean = np.mean(pro, axis=1)
    
    np.save(pro_file%(hemi), pro)
    np.save(pro_mean_file%(hemi), pro_mean)