def detect_corresponding_tracks_plus(indices,tracks1,indices2,tracks2): ''' Detect corresponding tracks from 1 to 2 where tracks1 & tracks2 are sequences of tracks Parameters ------------ indices : sequence of indices of tracks1 that are to be detected in tracks2 tracks1 : sequence of tracks as arrays, shape (N1,3) .. (Nm,3) indices2 : sequence of indices of tracks2 in the initial brain tracks2 : sequence of tracks as arrays, shape (M1,3) .. (Mm,3) Returns --------- track2track : array (N,2) where N is len(indices) of int showing the correspondance in th following way the first colum is the current index of tracks1 the second column is the corresponding index in tracks2 Examples ---------- >>> import numpy as np >>> import dipy.tracking.learning as tl >>> A=np.array([[0,0,0],[1,1,1],[2,2,2]]) >>> B=np.array([[1,0,0],[2,0,0],[3,0,0]]) >>> C=np.array([[0,0,-1],[0,0,-2],[0,0,-3]]) >>> bundle1=[A,B,C] >>> bundle2=[B,A] >>> indices=[0,1] >>> indices2=indices >>> arr=tl.detect_corresponding_tracks_plus(indices,bundle1,indices2,bundle2) Notes ------- To find the corresponding tracks we use mam_distances with 'avg' option. Then we calculate the argmin of all the calculated distances and return it for every index. (See 3rd column of arr in the example given below. See also ---------- distances.mam_distances ''' li=len(indices) track2track=np.zeros((li,2)) cnt=0 for i in indices: rt=[pf.mam_distances(tracks1[i],t,'avg') for t in tracks2] rt=np.array(rt) track2track[cnt]=np.array([i,indices2[rt.argmin()]]) cnt+=1 return track2track.astype(int)
def detect_corresponding_tracks_plus(indices, tracks1, indices2, tracks2): """ Detect corresponding tracks from 1 to 2 where tracks1 & tracks2 are sequences of tracks Parameters ---------- indices : sequence of indices of tracks1 that are to be detected in tracks2 tracks1 : sequence of tracks as arrays, shape (N1,3) .. (Nm,3) indices2 : sequence of indices of tracks2 in the initial brain tracks2 : sequence of tracks as arrays, shape (M1,3) .. (Mm,3) Returns ------- track2track : array (N,2) where N is len(indices) of int showing the correspondance in th following way the first colum is the current index of tracks1 the second column is the corresponding index in tracks2 Examples -------- >>> import numpy as np >>> import dipy.tracking.learning as tl >>> A = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]]) >>> B = np.array([[1, 0, 0], [2, 0, 0], [3, 0, 0]]) >>> C = np.array([[0, 0, -1], [0, 0, -2], [0, 0, -3]]) >>> bundle1 = [A, B, C] >>> bundle2 = [B, A] >>> indices = [0, 1] >>> indices2 = indices >>> arr = tl.detect_corresponding_tracks_plus(indices, bundle1, indices2, bundle2) Notes ----- To find the corresponding tracks we use mam_distances with 'avg' option. Then we calculate the argmin of all the calculated distances and return it for every index. (See 3rd column of arr in the example given below.) See Also -------- distances.mam_distances """ li = len(indices) track2track = np.zeros((li, 2)) cnt = 0 for i in indices: rt = [pf.mam_distances(tracks1[i], t, 'avg') for t in tracks2] rt = np.array(rt) track2track[cnt] = np.array([i, indices2[rt.argmin()]]) cnt += 1 return track2track.astype(int)
def test_mam_distances(): xyz1 = np.array([[0, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0]]) xyz2 = np.array([[0, 1, 1], [1, 0, 1], [2, 3, -2]]) # dm=array([[ 2, 2, 17], [ 3, 1, 14], [6, 2, 13], [11, 5, 14]]) # this is the distance matrix between points of xyz1 # and points of xyz2 xyz1 = xyz1.astype('float32') xyz2 = xyz2.astype('float32') zd2 = pf.mam_distances(xyz1, xyz2) assert_almost_equal(zd2[0], 1.76135602742)
def test_mam_distances(): xyz1 = np.array([[0,0,0],[1,0,0],[2,0,0],[3,0,0]]) xyz2 = np.array([[0,1,1],[1,0,1],[2,3,-2]]) # dm=array([[ 2, 2, 17], [ 3, 1, 14], [6, 2, 13], [11, 5, 14]]) # this is the distance matrix between points of xyz1 # and points of xyz2 xyz1=xyz1.astype('float32') xyz2=xyz2.astype('float32') zd2 = pf.mam_distances(xyz1,xyz2) assert_almost_equal( zd2[0], 1.76135602742)
idx2_best = None loss_best = 100000000 mapping12_best = None for i1 in range(size1): idx1 = np.argsort(dm1[i1]) for i2 in range(size2): idx2 = np.argsort(dm2[i2]) mapping12 = np.argsort(idx2[np.argsort(idx1)]) # this line is tricky and create the mapping as desiderd. It works correctly because if tracks2 is just a reshuffling of tracks1 then it leads to loss=0, as expected. loss = np.linalg.norm(dm2 - dm1[mapping12][:,mapping12]) # print i2, loss if loss < loss_best: idx2_best = idx2 loss_best = loss mapping12_best = mapping12 actual_distance = np.sum([mam_distances(tracks1[mapping12_best][i], tracks2[i]) for i in range(size1)]) print i1, i2, loss, actual_distance tracks3 = tracks1[mapping12_best].copy() for i in range(size1): tracks3[i] = tracks3[i] - tracks3[i].mean(0) tracks3[i] = tracks3[i] + tracks2[i].mean(0) r = fvtk.ren() # s = fvtk.streamtube(tracks2, fvtk.colors.carrot) # fvtk.add(r, s) # s = fvtk.streamtube(tracks3, fvtk.colors.blue) # fvtk.add(r, s) # s = fvtk.streamtube(tracks, fvtk.colors.red) # fvtk.add(r, s)
def avg_mam_distance_dipy(s1, s2): """Average streamline distance provided by DiPy (Zhang (2008)) """ return mam_distances(s1, s2, metric='avg')
fvtk.add(r,fvtk.line(T3s,fvtk.gray)) #fvtk.show(r) bout """ For each track in T1 find the minimum average distance to all the tracks in T3 and put information about it in ``track2track``. """ indices=range(len(T1)) track2track=[] mam_threshold=6. for i in indices: rt=[mam_distances(T1[i],t,'avg') for t in T3] rt=np.array(rt) if rt.min()< mam_threshold: track2track.append(np.array([i,rt.argmin(),rt.min()])) track2track=np.array(track2track) np.set_printoptions(2) """ When a track in T3 is simultaneously the nearest track to more than one track in T1 we identify the track in T1 that has the best correspondence and remove the other. """ good_correspondence=[] for i in track2track[:,1]:
fvtk.clear(ren) if __name__ == '__main__': T_A_filename = 'F:\Thesis\data\\100307\\100307_af.left.trk' T_A, hdr = loadtrkfile(T_A_filename, threshold_short_streamlines=10.0) #T_A.tolist() T_A_filename2 = 'F:\Thesis\data\\124422\\124422_af.left.trk' #T_A_filename2 = 'F:\Thesis\data\\100307\\100307_af.right.trk' T_A2, hdr = loadtrkfile(T_A_filename2, threshold_short_streamlines=10.0) #T_A2.tolist() #dis=[mam_distances(T_A[0], T_A2[0], metric='avg' ) for i in enumerate(0,len(T_A))] dis = [mam_distances(i, j, metric='avg') for i in T_A for j in T_A2] dis = np.array(dis) dis = dis.reshape(len(T_A), len(T_A2)) match = [] for row_idx, row in enumerate(dis): for col_idx, element in enumerate(row): if element == min(dis[row_idx]): match.append([element, row_idx, col_idx]) #np.append(match,row_idx) #np.append(match,col_idx) dis[:, col_idx] = 'inf' #print(element) #print() break
fvtk.add(r,fvtk.line(T3s,fvtk.gray)) # To show now use: #fvtk.show(r) """ For each track in T1 find the minimum average distance to all the tracks in T3 and put information about it in ``track2track``. """ indices=range(len(T1)) track2track=[] mam_threshold=6. for i in indices: rt=[mam_distances(T1[i],t,'avg') for t in T3] rt=np.array(rt) if rt.min()< mam_threshold: track2track.append(np.array([i,rt.argmin(),rt.min()])) track2track=np.array(track2track) np.set_printoptions(2) """ When a track in T3 is simultaneously the nearest track to more than one track in T1 we identify the track in T1 that has the best correspondence and remove the other. """ good_correspondence=[] for i in track2track[:,1]:
return T, hdr if __name__ == '__main__': T_A_filename = 'F:\Thesis\data\\100307\\100307_af.left.trk' T_A,hdr= loadtrkfile(T_A_filename, threshold_short_streamlines=10.0) T_A.tolist() T_A_filename2 = 'F:\Thesis\data\\124422\\124422_af.left.trk' #T_A_filename2 = 'F:\Thesis\data\\100307\\100307_af.right.trk' T_A2,hdr= loadtrkfile(T_A_filename2, threshold_short_streamlines=10.0) T_A2.tolist() dis=mam_distances(T_A[0], T_A2[0], metric='avg' ) print("Track Distance .. ") lengths = list(length(T_A)) fig_hist, ax = plt.subplots() ax.plot(dis) plt.show() print(dis)