def normal_registration(): target = read_ply('../data/132118/m_ex_atr-left_shore.ply') subject = read_ply('../data/150019/m_ex_atr-left_shore.ply') subject_after, _ = register(target, subject) draw_bundles([target, subject, subject_after], [[1, 0, 0], [0, 0, 1], [0, 0, .7]])
def left_to_right(): target = read_ply('../data/132118/m_ex_atr-left_shore.ply') subject = read_ply('../data/132118/m_ex_atr-right_shore.ply') subject_after, _ = register(target, subject) draw_bundles([target, subject, subject_after], [[1, 0, 0], [0, 0, 1], [0, 0, .7]])
def fake_registration(): mat = compose_matrix44([50, 20, 20, 180, 90, 90]) target = read_ply('../data/132118/m_ex_atr-left_shore.ply') subject = transform_streamlines(moving, mat) subject_after_registration, _ = register(target, subject) draw_bundles([target, subject, subject_after_registration], [[1, 0, 0], [0, 0, 1], [0, 0, .7]])
def icp_registration(): mat = compose_matrix44([50, 20, 20, 180, 90, 90, 5]) subject = read_ply('../data/164939/m_ex_atr-left_shore.ply') #subject = read_ply('../data/150019/m_ex_atr-left_shore.ply') target = transform_streamlines(subject, mat) subject_T = pca_transform(target, subject) #subject_T=registration_icp(static=target,moving=subject,pca=True) draw_bundles([target, subject_T, subject], [[1, 0, 0], [0, 0, 1], [0, 0, .7]])
from sklearn.neighbors import KDTree import matplotlib.pyplot as plt from dipy.tracking.streamline import transform_streamlines from src.tractography.io import read_ply from src.tractography.Utils import pca_transform_norm, flip from src.tractography.registration import register from src.tractography.viz import draw_bundles static = read_ply('data/197348/m_ex_atr-left_shore.ply') moving = read_ply('data/197348/m_ex_atr-right_shore.ply') ''' Apply PCA ''' pre_moving = pca_transform_norm(static, moving, best=True) draw_bundles([pre_moving, static], [[0, 0, 1], [1, 0, 0]]) ''' Flip ''' ''' pre_moving = flip(moving,x=-1) draw_bundles([pre_moving,static],[[0,0,1],[1,0,0]]) ''' con_static = np.concatenate(static) con_moving = np.concatenate(pre_moving) start = time() new_moving = register(static, pre_moving) end = time() kdtree = KDTree(con_static) distances = kdtree.query(np.concatenate(new_moving), k=1)[0]
} #,'maxiter': 10000000}#,'maxfun':200} start = time() m = Optimizer(cost_fun, x0, args=(static_clusters.cluster_centers_, moving_clusters.cluster_centers_, 7), method='L-BFGS-B', options=options) end = time() m.print_summary() #np.save('out/new_x0_00.npy',m.xopt) x1 = np.reshape(m.xopt, (num, 7)) new_moving = transform(x1, moving, moving_clusters.cluster_centers_) draw_bundles([static, moving], [[1, 0, 0], [0, 0, 1]]) #draw_bundles([new_moving]) hours = int((end - start) / 3600) minutes = int(((end - start) % 3600) / 60) seconds = int(((end - start) % 3600) % 60) plt.plot(costs) plt.title("Cost Function - Duration: {:02}:{}:{}\nMax dist: 7".format( hours, minutes, seconds)) plt.legend(['Distance', 'Link']) plt.gray() plt.savefig("pics/clust_cost_plot_000.png", dpi=600)
for track in moving: end = len(track) + i new_moving.append(new_con_mov[i:end]) #new_moving.append([i,end]) i = end # bins='auto' ''' Get the threshold ''' distances = np.linalg.norm(con_static - new_con_mov, axis=1) plt.hist(distances, bins='auto') #plt.title("After ICP | Duration: {:02}:{:02}:{:02}, Total Distance: {:}" # .format(hours,minutes,seconds,round(distances.sum(),2))+ # "\nMax distance: "+str(threshold)+"mm, alpha: "+str(alpha)) plt.ylabel("Frequency") plt.xlabel("Distance") plt.savefig('new_plan/1{:02d}_hist_ICP.png'.format(num), dpi=600) draw_bundles([new_moving, static], [[0, 0, 1], [1, 0, 0]]) dipy_moving = register(moving, new_moving) draw_bundles([dipy_moving, moving], [[0, 0, 1], [1, 0, 0]]) '''Dipy''' ''' Plot the distance ''' distances = np.linalg.norm(con_moving - np.concatenate(dipy_moving), axis=1) plt.hist(distances, bins='auto') #plt.title("dypi | Duration: {:02}:{:02}:{:02}, Total Distance: {:}" # .format(hours,minutes,seconds,round(distances.sum(),2))+ # "\nMax distance: "+str(threshold)+"mm, alpha: "+str(alpha)) plt.ylabel("Frequency") plt.xlabel("Distance") plt.savefig('new_plan/1{:02d}_hist_dipy.png'.format(num), dpi=600)
from src.tractography.viz import draw_bundles from os import listdir # , mkdir from os.path import isfile # , isdir from src.tractography.io import read_ply import argparse from dipy.align.streamlinear import compose_matrix44 from dipy.tracking.streamline import transform_streamlines parser = argparse.ArgumentParser(description='Input argument parser.') parser.add_argument('-f', type=str, help='location of files') args = parser.parse_args() data_path = '../data/132118/' #data_path = args.f files = [ data_path + f for f in listdir(data_path) if isfile(data_path + f) and f.endswith('.ply') ] mat = compose_matrix44([0, 0, 0, 0, 90, 90]) brain = [] for name in files: brain.append(transform_streamlines(read_ply(name), mat)) draw_bundles(brain, rotate=True) """ data1 = read_ply('../data/132118/m_ex_atr-left_shore.ply') data2 = read_ply('../data/132118/m_ex_atr-right_shore.ply') draw_bundles([data1,data2]) """
lsqr(D,new_static_centers[:,1])[0], lsqr(D,new_static_centers[:,2])[0]]).T ''' I get the same new_static_centers ''' print(D.dot(x)) ''' Apply soft membership''' def transform(affine,bundle,clusters_centers): num = len(clusters_centers) kdtree = KDTree(clusters_centers) distances,ids = kdtree.query(np.concatenate(bundle),k=num) distances[distances==0]=1 #centroid distance must be 1 distances=1/distances #heigh value for close verteces distances = np.divide(distances,distances.sum(axis=1).reshape((distances.shape[0],1))) # weights = distances[:,0]/distances.sum(axis=1) #The weigh is affine = [compose_matrix44(np.multiply(np.reshape(dis,(num,1)),affine[id]).sum(axis=0)) for dis,id in zip(distances,ids)] #Z = [apply_affine(compose_matrix44(np.multiply(np.reshape(dis,(num,1)),x0[id]).sum(axis=0)),vec) for dis,id,vec in zip(distances,ids,con_moving)] count = 0 trans_bundle=[] for tract in bundle: temp = [] for vec in tract: temp.append(apply_affine(affine[count],vec)) count+=1 trans_bundle.append(np.array(temp)) return trans_bundle new_moving = transform(x,moving,moving_centers) draw_bundles([static,moving])