def show(): #brains=[(1,1),(1,2),(2,1),(3,1),(3,2)] brains=[(1,1),(2,1),(3,1)] ids=tl.emi_atlas() #print ids.keys() for (b,s) in brains: ids2 = pbc.load_pickle(path+'/Relabelling_8_sc1_'+str(b)+'_'+str(s)+'.pkl') #'/Relabelling_8_sc1_'+str(b)+'_'+str(s)+'.pkl' print b,s, ids2.keys() tracks = pbc.load_approximate_tracks(path,b,s) for i in ids: if i >0: r=fos.ren() color=np.array(ids[i]['color']) indices=ids2[i]['indices'] bundle=[tracks[ind] for ind in indices] fos.add(r,fos.line(bundle,color,opacity=0.9)) print 'Bundle_name',i,ids[i]['bundle_name'] fos.show(r,title=ids[i]['bundle_name'][0])
def show_rep3(C,r=None,color=fos.white): if r==None: r=fos.ren() for c in C: fos.add(r,fos.line(C[c]['rep3']/C[c]['N'],color)) fos.show(r) return r
def show_rep3(C, r=None, color=fos.white): if r == None: r = fos.ren() for c in C: fos.add(r, fos.line(C[c]['rep3'] / C[c]['N'], color)) fos.show(r) return r
def skeletonize_both(): from dipy.viz import fos from dipy.core.track_metrics import downsample from dipy.core.track_performance import local_skeleton_clustering, most_similar_track_mam froi='/home/eg309/Data/ICBM_Wmpm/ICBM_WMPM.nii' wI=get_roi(froi,9,0) #4 is genu fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy' #fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy' #fname='/home/eg309/Data/PROC_MR10032/subj_06/101/13122110752323511930000010092916083910900000227ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy' fname2='/home/eg309/Data/PROC_MR10032/subj_06/101/13122110752323511930000010092916083910900000227ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy' r=fos.ren() #''' dpr=Dpy(fname,'r') T=dpr.read_indexed(range(2*10**4)) dpr.close() print len(T) Td=[downsample(t,3) for t in T if length(t)>40] C=local_skeleton_clustering(Td,d_thr=20.) for c in C: #color=np.random.rand(3) color=fos.red if C[c]['N']>0: Ttmp=[] for i in C[c]['indices']: Ttmp.append(T[i]) si,s=most_similar_track_mam(Ttmp,'avg') print si,C[c]['N'] fos.add(r,fos.line(Ttmp[si],color)) dpr=Dpy(fname2,'r') T=dpr.read_indexed(range(2*10**4)) dpr.close() print len(T) Td=[downsample(t,3) for t in T if length(t)>40] C=local_skeleton_clustering(Td,d_thr=20.) #r=fos.ren() for c in C: #color=np.random.rand(3) color=fos.yellow if C[c]['N']>0: Ttmp=[] for i in C[c]['indices']: Ttmp.append(T[i]) si,s=most_similar_track_mam(Ttmp,'avg') print si,C[c]['N'] fos.add(r,fos.line(Ttmp[si],color)) #''' fos.add(r,fos.point(wI,fos.green)) fos.show(r)
def tracks_in_roi(): froi='/home/eg309/Data/ICBM_Wmpm/ICBM_WMPM.nii' wI=get_roi(froi,35,1) #4 is genu fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy' dpr=Dpy(fname,'r') T=dpr.read_indexed(range(2*10**4)) print len(T) Troi=[] for t in T: if track_roi_intersection_check(t,wI,.5): Troi.append(t) print(len(Troi)) dpr.close() from dipy.viz import fos r=fos.ren() fos.add(r,fos.line(Troi,fos.red)) fos.add(r,fos.point(wI,fos.green)) fos.show(r)
def skeletonize(): froi='/home/eg309/Data/ICBM_Wmpm/ICBM_WMPM.nii' wI=get_roi(froi,35,1) #4 is genu #fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy' #fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy' #fname='/home/eg309/Data/PROC_MR10032/subj_06/101/13122110752323511930000010092916083910900000227ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy' fname='/home/eg309/Data/PROC_MR10032/subj_06/101/13122110752323511930000010092916083910900000227ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy' dpr=Dpy(fname,'r') T=dpr.read_indexed(range(2*10**4)) dpr.close() print len(T) from dipy.core.track_metrics import downsample from dipy.core.track_performance import local_skeleton_clustering, most_similar_track_mam Td=[downsample(t,3) for t in T] C=local_skeleton_clustering(Td,d_thr=20.) #Tobject=np.array(T,dtype=np.object) from dipy.viz import fos r=fos.ren() #skeleton=[] for c in C: color=np.random.rand(3) if C[c]['N']>0: Ttmp=[] for i in C[c]['indices']: Ttmp.append(T[i]) si,s=most_similar_track_mam(Ttmp,'avg') print si,C[c]['N'] fos.add(r,fos.line(Ttmp[si],color)) #print len(skeleton) #fos.add(r,fos.line(skeleton,color)) #fos.add(r,fos.line(T,fos.red)) fos.show(r)
r=fos.ren() #fos.add(r,fos.line(tracks1,fos.red,opacity=0.01)) #fos.add(r,fos.line(tracks2,fos.cyan,opacity=0.01)) tracks1zshift=[t+np.array([-70,0,0]) for t in tracks1z] tracks2zshift=[t+np.array([70,0,0]) for t in tracks2z] tracks3zshift=[t+np.array([210,0,0]) for t in tracks3z] fos.add(r,fos.line(tracks1zshift,fos.red,opacity=0.02)) fos.add(r,fos.line(tracks2zshift,fos.cyan,opacity=0.02)) fos.add(r,fos.line(tracks3zshift,fos.blue,opacity=0.02)) print 'Show track to track correspondence br1 br2' for i in track2track: fos.add(r,fos.line(tracks1zshift[i[1]],fos.yellow,opacity=0.5,linewidth=3)) fos.label(r,str(i[0]),tracks1zshift[i[1]][0],(4,4,4),fos.white) fos.add(r,fos.line(tracks2zshift[i[2]],fos.yellow,opacity=0.5,linewidth=3)) fos.label(r,str(i[0]),tracks2zshift[i[2]][0],(4,4,4),fos.white) print 'Show track to track correspondence br1_FACT and br2_RK2' for i in track2track2: fos.add(r,fos.line(tracks3zshift[i[2]],fos.yellow,opacity=0.5,linewidth=3)) fos.label(r,str(i[0]),tracks3zshift[i[2]][0],(4,4,4),fos.white) fos.show(r,size=(1024,768))
r = fos.ren() fos.add(r, fos.line(tracks, fos.red)) #fos.show(r) for c in C: color = np.random.rand(3) for i in C[c]['indices']: fos.add(r, fos.line(tracks[i] + np.array([8., 0., 0.]), color)) fos.add(r, fos.line(tracks[i] + np.array([16., 0., 0.]), color)) fos.add( r, fos.line(C[c]['rep3'] / C[c]['N'] + np.array([16., 0., 0.]), fos.white)) fos.show(r) ''' print len(C) C=pf.larch_3merge(C,0.5) print len(C) for c in C: color=np.random.rand(3) for i in C[c]['indices']: fos.add(r,fos.line(tracks[i]+np.array([14.,0.,0.]),color)) #fos.show(r) for c in C:
def plot_sphere(v, key): r = fos.ren() fos.add(r, fos.point(v, fos.green, point_radius=0.01)) fos.show(r, title=key, size=(1000, 1000))
def plot_sphere(v,key): r = fos.ren() fos.add(r,fos.point(v,fos.green, point_radius= 0.01)) fos.show(r, title=key, size=(1000,1000))
def warp_tracks(): dn='/home/eg309/Data/TEST_MR10032/subj_03/101/' ffa=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_bet_FA.nii.gz' finvw=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_warps_in_bet_FA.nii.gz' fqadpy=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy' flaff=dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_affine_transf.mat' fref ='/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' fdis =dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_nonlin_displacements.nii.gz' fdis2 =dn+'1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_nonlin_displacements_withaff.nii.gz' #read some tracks dpr=Dpy(fqadpy,'r') T=dpr.read_indexed(range(150)) dpr.close() #from fa index to ref index res=flirt2aff_files(flaff,ffa,fref) #load the reference img imgref=ni.load(fref) refaff=imgref.get_affine() #load the invwarp displacements imginvw=ni.load(finvw) invwdata=imginvw.get_data() invwaff = imginvw.get_affine() #load the forward displacements imgdis=ni.load(fdis) disdata=imgdis.get_data() #load the forward displacements + affine imgdis2=ni.load(fdis2) disdata2=imgdis2.get_data() #from their difference create the affine disaff=imgdis2.get_data()-disdata shift=np.array([disaff[...,0].mean(),disaff[...,1].mean(),disaff[...,2].mean()]) shape=ni.load(ffa).get_data().shape disaff0=affine_transform(disaff[...,0],res[:3,:3],res[:3,3],shape,order=1) disaff1=affine_transform(disaff[...,1],res[:3,:3],res[:3,3],shape,order=1) disaff2=affine_transform(disaff[...,2],res[:3,:3],res[:3,3],shape,order=1) disdata0=affine_transform(disdata[...,0],res[:3,:3],res[:3,3],shape,order=1) disdata1=affine_transform(disdata[...,1],res[:3,:3],res[:3,3],shape,order=1) disdata2=affine_transform(disdata[...,2],res[:3,:3],res[:3,3],shape,order=1) #print disgrad0.shape,disgrad1.shape,disgrad2.shape #disdiff=np.empty(invwdata.shape) #disdiff[...,0]=disgrad0 #disdiff[...,1]=disgrad1 #disdiff[...,2]=disgrad2 #ni.save(ni.Nifti1Image(disdiff,invwaff),'/tmp/disdiff.nii.gz') di=disdata0 dj=disdata1 dk=disdata2 d2i=invwdata[:,:,:,0] + disaff0 d2j=invwdata[:,:,:,1] + disaff1 d2k=invwdata[:,:,:,2] + disaff2 #di=disgrad0 #dj=disgrad1 #dk=disgrad2 imgfa=ni.load(ffa) fadata=imgfa.get_data() faaff =imgfa.get_affine() Tw=[] Tw2=[] Tw3=[] froi='/home/eg309/Data/ICBM_Wmpm/ICBM_WMPM.nii' roiI=get_roi(froi,3,1) #3 is GCC roiI2=get_roi(froi,4,1) #4 is BCC roiI3=get_roi(froi,5,1) #4 is SCC roiI=np.vstack((roiI,roiI2,roiI3)) for t in T: if np.min(t[:,2])>=0:#to be removed mci=mc(di,t.T,order=1) #interpolations for i displacement mcj=mc(dj,t.T,order=1) #interpolations for j displacement mck=mc(dk,t.T,order=1) #interpolations for k displacement D=np.vstack((mci,mcj,mck)).T WI=np.dot(t,res[:3,:3].T)+res[:3,3]+D#+ shift W=np.dot(WI,refaff[:3,:3].T)+refaff[:3,3] mc2i=mc(d2i,t.T,order=1) #interpolations for i displacement mc2j=mc(d2j,t.T,order=1) #interpolations for j displacement mc2k=mc(d2k,t.T,order=1) #interpolations for k displacement D2=np.vstack((mc2i,mc2j,mc2k)).T WI2=np.dot(t,res[:3,:3].T)+res[:3,3]+D2 #+ shift W2=np.dot(WI2,refaff[:3,:3].T)+refaff[:3,3] WI3=np.dot(t,res[:3,:3].T)+res[:3,3] W3=np.dot(WI3,refaff[:3,:3].T)+refaff[:3,3] Tw.append(W) Tw2.append(W2) Tw3.append(W3) from dipy.viz import fos r=fos.ren() fos.add(r,fos.line(Tw,fos.red)) fos.add(r,fos.line(Tw2,fos.green)) fos.add(r,fos.line(Tw3,fos.yellow)) fos.add(r,fos.sphere((0,0,0),10,color=fos.blue)) fos.add(r,fos.point(roiI,fos.blue)) fos.show(r)
r = fos.ren() fos.add(r, fos.line(tracks, fos.red)) #fos.show(r) for c in C: color = np.random.rand(3) for i in C[c]['indices']: fos.add(r, fos.line(tracks[i] + np.array([8., 0., 0.]), color)) fos.add(r, fos.line(tracks[i] + np.array([16., 0., 0.]), color)) fos.add( r, fos.line(C[c]['rep3'] / C[c]['N'] + np.array([16., 0., 0.]), fos.white)) fos.show(r) """ print len(C) C=pf.larch_3merge(C,0.5) print len(C) for c in C: color=np.random.rand(3) for i in C[c]['indices']: fos.add(r,fos.line(tracks[i]+np.array([14.,0.,0.]),color)) #fos.show(r) for c in C:
along = reference[index+1]-reference[index] import numpy as np normal=along/np.sqrt(np.inner(along,along)) crossings = list([]) hit_div = list([]) for k in range(len(b1)): t = b1[k] cross= -1 for i in range(len(t))[:-1]: q = t[i] r = t[i+1] if np.inner(normal,q-p)*np.inner(normal,r-p) <= 0: # print "Segment %d of track %d crosses the normal plane" % (i,k) cross = i crossings.append([k,cross]) if np.inner((r-q),normal) != 0: alpha = np.inner((p-q),normal)/np.inner((r-q),normal) hit = q+alpha*(r-q) divergence = (r-q)-np.inner(r-q,normal)*normal hit_div.append([hit,divergence]) else: hit_div.append([hit,0]) break # if cross<0: # print "No crossing segment" # if cross >= 0: print "%d tracks cross the plane" % (len(crossings)) r = fos.ren() fos.add(r,fos.points(np.array([h[0] for h in hit_div]))) fos.show()
C=pf.larch_3split(tracks,None,0.5) r=fos.ren() fos.add(r,fos.line(tracks,fos.red)) #fos.show(r) for c in C: color=np.random.rand(3) for i in C[c]['indices']: fos.add(r,fos.line(tracks[i]+np.array([8.,0.,0.]),color)) fos.add(r,fos.line(tracks[i]+np.array([16.,0.,0.]),color)) fos.add(r,fos.line(C[c]['rep3']/C[c]['N']+np.array([16.,0.,0.]),fos.white)) fos.show(r) """ print len(C) C=pf.larch_3merge(C,0.5) print len(C) for c in C: color=np.random.rand(3) for i in C[c]['indices']: fos.add(r,fos.line(tracks[i]+np.array([14.,0.,0.]),color)) #fos.show(r)
C=pf.larch_3split(tracks,None,0.5) r=fos.ren() fos.add(r,fos.line(tracks,fos.red)) #fos.show(r) for c in C: color=np.random.rand(3) for i in C[c]['indices']: fos.add(r,fos.line(tracks[i]+np.array([8.,0.,0.]),color)) fos.add(r,fos.line(tracks[i]+np.array([16.,0.,0.]),color)) fos.add(r,fos.line(C[c]['rep3']/C[c]['N']+np.array([16.,0.,0.]),fos.white)) fos.show(r) ''' print len(C) C=pf.larch_3merge(C,0.5) print len(C) for c in C: color=np.random.rand(3) for i in C[c]['indices']: fos.add(r,fos.line(tracks[i]+np.array([14.,0.,0.]),color)) #fos.show(r)
print 'Deleting unnecessary data...' del streams,hdr print 'Hidden Structure Clustering...' now=time.clock() C=pf.local_skeleton_clustering(tracks,d_thr=20) print 'Done in', time.clock()-now,'s.' print 'Reducing the number of points...' T=[pf.approximate_ei_trajectory(t) for t in T] print 'Showing initial dataset.' r=fos.ren() fos.add(r,fos.line(T,fos.white,opacity=0.1)) fos.show(r) print 'Showing dataset after clustering.' fos.clear(r) colors=np.zeros((len(T),3)) for c in C: color=np.random.rand(1,3) for i in C[c]['indices']: colors[i]=color fos.add(r,fos.line(T,colors,opacity=1)) fos.show(r) print 'Some statistics about the clusters' lens=[len(C[c]['indices']) for c in C] print 'max ',max(lens), 'min ',min(lens) print 'singletons ',lens.count(1)
#C=pf.local_skeleton_clustering(tracks,20.) print 'Done in total of ', time.clock() - tim, 'seconds.' print 'Saving result...' pkl.save_pickle(C_fname, C) streams = [(i, None, None) for i in atracks] tv.write(appr_fname, streams, hdr) else: print 'Loading result...' C = pkl.load_pickle(C_fname) skel = [] for c in C: skel.append(C[c]['repz']) print 'Showing dataset after clustering...' r = fos.ren() fos.clear(r) colors = np.zeros((len(skel), 3)) for (i, s) in enumerate(skel): color = np.random.rand(1, 3) colors[i] = color fos.add(r, fos.line(skel, colors, opacity=1)) fos.show(r)
def showline(myline): from dipy.viz import fos r = fos.ren() fos.add(r,fos.line(myline,fos.blue,opacity=0.5)) fos.show(r)