def test_fos_functions(): # Create a renderer r=fos.ren() # Create 2 lines with 2 different colors lines=[np.random.rand(10,3),np.random.rand(20,3)] colors=np.random.rand(2,3) c=fos.line(lines,colors) fos.add(r,c) # Create a volume and return a volumetric actor using volumetric rendering vol=100*np.random.rand(100,100,100) vol=vol.astype('uint8') r = fos.ren() v = fos.volume(vol) fos.add(r,v) # Remove all objects fos.rm_all(r) # Put some text l=fos.label(r,text='Yes Men') fos.add(r,l) # Show everything #fos.show(r)
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)
#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)
print 'Representing tracks using only 3 pts...' tracks=[tm.downsample(t,3) for t in T] 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]
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)
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)