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
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
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: no_labelling = np.zeros_like(labelling[:,1]) for n in list(np.where(labelling[:,1]==-1)[0]): no_labelling[n] = 1 write_vtk(no_file%(sub, hemi), complex_v, complex_f, data=no_labelling[:,np.newaxis]) # get actual and ideal seed distances actual_seed_dist = np.linalg.norm((complex_v[seeds] - simple_v), axis=1) ideal_seed_dist, mapping = spatial.KDTree(complex_v).query(simple_v) # count how many of each label
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 avg_mean += mean_3_7 count += 1 print 'Finished '+sub avg_pro = avg_pro / count avg_mean = avg_mean / count np.save(avg_pro_file%(hemi), avg_pro) np.save(avg_mean_pro_file%(hemi), avg_mean) #pdb.set_trace() write_vtk(avg_mean_surf%(hemi), v, f, data=avg_mean[:,np.newaxis]) for p in range(profiles.shape[1]): write_vtk(avg_pro_surf%(hemi, str(p)), v, f, data=avg_pro[:,p, np.newaxis])
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': print 'vtk to npy' for hemi in hemis: for sub in subjects: #print hemi, sub #v,f,d = read_vtk(vtk_data_file%(sub, hemi)) #np.save(data_file%(sub, hemi), d) for sess in sessions:
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])
import numpy as np from vtk_rw import read_vtk, write_vtk iterations = 100 hemis = ['rh', 'lh'] mesh_file="/scr/ilz3/myelinconnect/new_groupavg/surfs/lowres/%s_lowres_new.vtk" 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' for hemi in hemis: print hemi v,f,d = read_vtk(mesh_file%hemi) data_shape = v.shape[0] for r in range(iterations): print r random_data = np.random.randn(data_shape) 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])
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': print 'vtk to npy' for hemi in hemis: for sub in subjects: #print hemi, sub #v,f,d = read_vtk(vtk_data_file%(sub, hemi)) #np.save(data_file%(sub, hemi), d)
from ply_rw import read_ply from vtk_rw import write_vtk ''' Meta script to convert ply to vtk file ''' in_ply = raw_input("input ply file: ") out_vtk = raw_input("output vtk file: ") comment = raw_input("comment (optional): ") if comment == "": comment = 'vtk format converted from ply' v, f = read_ply(in_ply) write_vtk(out_vtk, v, f, comment)