bvals=btable[:,0] bvecs=btable[:,1:] img=nib.load(fname) data=img.get_data() print data.shape mask=data[:,:,:,0]>50 #D=data[20:90,20:90,18:22] #D=data[40:44,40:44,18:22] #del data D=data from time import time t0=time() ds=DiffusionSpectrum(D,bvals,bvecs,mask=mask) t1=time() print t1-t0,' secs' GFA=ds.gfa() t2=time() ten=Tensor(D,bvals,bvecs,mask=mask) t3=time() print t3-t2,' secs' FA=ten.fa() from dipy.tracking.propagation import EuDX IN=ds.ind()
def test_dsi(): btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] S,stics=SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0),(90,0),(90,90)], fractions=[50,50,0], snr=None) pdf0,odf0,peaks0=standard_dsi_algorithm(S,bvals,bvecs) S2=S.copy() S2=S2.reshape(1,len(S)) ds=DiffusionSpectrum(S2,bvals,bvecs) assert_almost_equal(np.sum(ds.pdf(S)-pdf0),0) assert_almost_equal(np.sum(ds.odf(ds.pdf(S))-odf0),0) #compare gfa psi=odf0/odf0.max() numer=len(psi)*np.sum((psi-np.mean(psi))**2) denom=(len(psi)-1)*np.sum(psi**2) GFA=np.sqrt(numer/denom) assert_almost_equal(ds.gfa()[0],GFA) #compare indices #print ds.ind() #print peak_finding(odf0,odf_faces) #print peaks0 data=np.zeros((3,3,3,515)) data[:,:,:]=S ds=DiffusionSpectrum(data,bvals,bvecs) ds2=DiffusionSpectrum(data,bvals,bvecs,auto=False) r = np.sqrt(ds2.qtable[:,0]**2+ds2.qtable[:,1]**2+ds2.qtable[:,2]**2) ds2.filter=.5*np.cos(2*np.pi*r/32) ds2.fit() assert_almost_equal(np.sum(ds2.qa()-ds.qa()),0) #1 fiber S,stics=SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0),(90,0),(90,90)], fractions=[100,0,0], snr=None) ds=DiffusionSpectrum(S.reshape(1,len(S)),bvals,bvecs) QA=ds.qa() assert_equal(np.sum(QA>0),1) #2 fibers S,stics=SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0),(90,0),(90,90)], fractions=[50,50,0], snr=None) ds=DiffusionSpectrum(S.reshape(1,len(S)),bvals,bvecs) QA=ds.qa() assert_equal(np.sum(QA>0),2) #3 fibers S,stics=SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0),(90,0),(90,90)], fractions=[33,33,33], snr=None) ds=DiffusionSpectrum(S.reshape(1,len(S)),bvals,bvecs) QA=ds.qa() assert_equal(np.sum(QA>0),3) #isotropic S,stics=SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0),(90,0),(90,90)], fractions=[0,0,0], snr=None) ds=DiffusionSpectrum(S.reshape(1,len(S)),bvals,bvecs) QA=ds.qa() assert_equal(np.sum(QA>0),0)
def test_dsi(): btable = np.loadtxt(get_data('dsi515btable')) bvals = btable[:, 0] bvecs = btable[:, 1:] S, stics = SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (90, 0), (90, 90)], fractions=[50, 50, 0], snr=None) pdf0, odf0, peaks0 = standard_dsi_algorithm(S, bvals, bvecs) S2 = S.copy() S2 = S2.reshape(1, len(S)) ds = DiffusionSpectrum(S2, bvals, bvecs) assert_almost_equal(np.sum(ds.pdf(S) - pdf0), 0) assert_almost_equal(np.sum(ds.odf(ds.pdf(S)) - odf0), 0) #compare gfa psi = odf0 / odf0.max() numer = len(psi) * np.sum((psi - np.mean(psi))**2) denom = (len(psi) - 1) * np.sum(psi**2) GFA = np.sqrt(numer / denom) assert_almost_equal(ds.gfa()[0], GFA) #compare indices #print ds.ind() #print peak_finding(odf0,odf_faces) #print peaks0 data = np.zeros((3, 3, 3, 515)) data[:, :, :] = S ds = DiffusionSpectrum(data, bvals, bvecs) ds2 = DiffusionSpectrum(data, bvals, bvecs, auto=False) r = np.sqrt(ds2.qtable[:, 0]**2 + ds2.qtable[:, 1]**2 + ds2.qtable[:, 2]**2) ds2.filter = .5 * np.cos(2 * np.pi * r / 32) ds2.fit() assert_almost_equal(np.sum(ds2.qa() - ds.qa()), 0) #1 fiber S, stics = SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (90, 0), (90, 90)], fractions=[100, 0, 0], snr=None) ds = DiffusionSpectrum(S.reshape(1, len(S)), bvals, bvecs) QA = ds.qa() assert_equal(np.sum(QA > 0), 1) #2 fibers S, stics = SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (90, 0), (90, 90)], fractions=[50, 50, 0], snr=None) ds = DiffusionSpectrum(S.reshape(1, len(S)), bvals, bvecs) QA = ds.qa() assert_equal(np.sum(QA > 0), 2) #3 fibers S, stics = SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (90, 0), (90, 90)], fractions=[33, 33, 33], snr=None) ds = DiffusionSpectrum(S.reshape(1, len(S)), bvals, bvecs) QA = ds.qa() assert_equal(np.sum(QA > 0), 3) #isotropic S, stics = SticksAndBall(bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (90, 0), (90, 90)], fractions=[0, 0, 0], snr=None) ds = DiffusionSpectrum(S.reshape(1, len(S)), bvals, bvecs) QA = ds.qa() assert_equal(np.sum(QA > 0), 0)
bvals = btable[:, 0] bvecs = btable[:, 1:] img = nib.load(fname) data = img.get_data() print data.shape mask = data[:, :, :, 0] > 50 #D=data[20:90,20:90,18:22] #D=data[40:44,40:44,18:22] #del data D = data from time import time t0 = time() ds = DiffusionSpectrum(D, bvals, bvecs, mask=mask) t1 = time() print t1 - t0, ' secs' GFA = ds.gfa() t2 = time() ten = Tensor(D, bvals, bvecs, mask=mask) t3 = time() print t3 - t2, ' secs' FA = ten.fa() from dipy.tracking.propagation import EuDX IN = ds.ind()
def compare_dni_dsi_gqi_gqi2_eit(): #stop btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] type=3 axesn=200 SNR=20 data,gold,angs,anglesn,axesn,angles=generate_gold_data(bvals,bvecs,fibno=type,axesn=axesn,SNR=SNR) gq=GeneralizedQSampling(data,bvals,bvecs,1.2,odf_sphere='symmetric642',squared=False,save_odfs=True) gq2=GeneralizedQSampling(data,bvals,bvecs,3.,odf_sphere='symmetric642',squared=True,save_odfs=True) ds=DiffusionSpectrum(data,bvals,bvecs,odf_sphere='symmetric642',auto=False,save_odfs=True) ds.filter_width=32. ds.update() ds.fit() ei=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642',auto=False,save_odfs=True,fast=True) ei.radius=np.arange(0,5,0.1) ei.gaussian_weight=None#0.01 ei.set_operator('laplacian') ei.update() ei.fit() ei2=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642',auto=False,save_odfs=True,fast=True) ei2.radius=np.arange(0,5,0.1) ei2.gaussian_weight=None ei2.set_operator('laplap') ei2.update() ei2.fit() ei3=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642',auto=False,save_odfs=True,fast=True) ei3.radius=np.arange(0,5,0.1) ei3.gaussian_weight=None ei3.set_operator('signal') ei3.update() ei3.fit() """ blobs=np.zeros((2,4,642)) no=200 blobs[0,0,:]=gq.ODF[no] blobs[0,1,:]=gq2.ODF[no] blobs[0,2,:]=ds.ODF[no] blobs[0,3,:]=ei.ODF[no] no=399 blobs[1,0,:]=gq.ODF[no] blobs[1,1,:]=gq2.ODF[no] blobs[1,2,:]=ds.ODF[no] blobs[1,3,:]=ei.ODF[no] show_blobs(blobs[None,:,:,:],ei.odf_vertices,ei.odf_faces,1.2) """ #stop vts=gq.odf_vertices def simple_peaks(ODF,faces,thr): x,g=ODF.shape PK=np.zeros((x,5)) IN=np.zeros((x,5)) for (i,odf) in enumerate(ODF): peaks,inds=peak_finding(odf,faces) ibigp=np.where(peaks>thr*peaks[0])[0] l=len(ibigp) if l>3: l=3 PK[i,:l]=peaks[:l] IN[i,:l]=inds[:l] return PK,IN thresh=0.5 PK,IN=simple_peaks(ds.ODF,ds.odf_faces,thresh) res,me,st =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) PK,IN=simple_peaks(gq.ODF,ds.odf_faces,thresh) res2,me2,st2 =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) PK,IN=simple_peaks(gq2.ODF,ds.odf_faces,thresh) res3,me3,st3 =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) PK,IN=simple_peaks(ei.ODF,ds.odf_faces,thresh) res4,me4,st4 =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) PK,IN=simple_peaks(ei2.ODF,ds.odf_faces,thresh) res5,me5,st5 =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) PK,IN=simple_peaks(ei3.ODF,ei3.odf_faces,thresh) res6,me6,st6 =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) #res7,me7,st7 =do_compare(ei2.PK,ei2.IN,0,anglesn,axesn,type) if type==1: plt.figure(1) plt.plot(res,'r',label='DSI') plt.plot(res2,'g',label='GQI') plt.plot(res3,'b',label='GQI2') plt.plot(res4,'k',label='EITL') plt.plot(res5,'k--',label='EITL2') plt.plot(res6,'k-.',label='EITS') #plt.xlabel('angle') #plt.ylabel('resolution') #plt.title('Angular accuracy') plt.legend() plt.show() else: if type==3: x,y,z=sphere2cart(np.ones(len(angles)),np.deg2rad(angles),np.zeros(len(angles))) x2,y2,z2=sphere2cart(np.ones(len(angles)),np.deg2rad(angles),np.deg2rad(120*np.ones(len(angles)))) angles2=[] for i in range(len(x)): angles2.append(np.rad2deg(np.arccos(np.dot([x[i],y[i],z[i]],[x2[i],y2[i],z2[i]])))) angles=angles2 plt.figure(1) plt.plot(angles,me,'r',linewidth=3.,label='DSI') plt.plot(angles,me2,'g',linewidth=3.,label='GQI') plt.plot(angles,me3,'b',linewidth=3.,label='GQI2') plt.plot(angles,me4,'k',linewidth=3.,label='EITL') plt.plot(angles,me5,'k--',linewidth=3.,label='EITL2') plt.plot(angles,me6,'k-.',linewidth=3.,label='EITS') #plt.plot(angles,me7,'r--',linewidth=3.,label='EITL2') plt.xlabel('angle') plt.ylabel('similarity') title='Angular similarity of ' + str(type) + '-fibres crossing with SNR ' + str(SNR) plt.title(title) plt.legend(loc='center right') plt.savefig('/tmp/test.png',dpi=300) plt.show()
def show_simulated_2fiber_crossings(): btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] data=create_data_2fibers(bvals,bvecs,d=0.0015,S0=100,angles=np.arange(0,47,5),snr=None) #data=create_data_2fibers(bvals,bvecs,d=0.0015,S0=100,angles=np.arange(0,92,5),snr=None) print data.shape #stop #""" dn=DiffusionNabla(data,bvals,bvecs,odf_sphere='symmetric642', auto=False,save_odfs=True,fast=True) dn.peak_thr=.4 dn.iso_thr=.05 dn.radius=np.arange(0,5,0.1) dn.radiusn=len(dn.radius) dn.create_qspace(bvals,bvecs,16,8) dn.radon_params(64) dn.precompute_interp_coords() dn.precompute_fast_coords() dn.precompute_equator_indices(5.) dn.precompute_angular(None)#0.01) dn.fit() dn2=DiffusionNabla(data,bvals,bvecs,odf_sphere='symmetric642', auto=False,save_odfs=True,fast=False) dn2.peak_thr=.4 dn2.iso_thr=.05 dn2.radius=np.arange(0,5,0.1) dn2.radiusn=len(dn.radius) dn2.create_qspace(bvals,bvecs,16,8) dn2.radon_params(64) dn2.precompute_interp_coords() dn2.precompute_fast_coords() dn2.precompute_equator_indices(5.) dn2.precompute_angular(None)#0.01) dn2.fit() #""" eis=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642', auto=False,save_odfs=True,fast=True) #eis.radius=np.arange(0,6,0.2) eis.radius=np.arange(0,5,0.1) eis.gaussian_weight=None eis.set_operator('signal')#'laplap') eis.update() eis.fit() eil=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642', auto=False,save_odfs=True,fast=True) #eil.radius=np.arange(0,6,0.2) eil.radius=np.arange(0,5,0.1) eil.gaussian_weight=None eil.set_operator('laplacian')#'laplap') eil.update() eil.fit() eil2=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642', auto=False,save_odfs=True,fast=True) #eil2.radius=np.arange(0,6,0.2) eil2.radius=np.arange(0,5,0.1) eil2.gaussian_weight=None eil2.set_operator('laplap') eil2.update() eil2.fit() ds=DiffusionSpectrum(data,bvals,bvecs,odf_sphere='symmetric642',auto=True,save_odfs=True) gq=GeneralizedQSampling(data,bvals,bvecs,1.2,odf_sphere='symmetric642',squared=False,auto=False,save_odfs=True) gq.fit() gq2=GeneralizedQSampling(data,bvals,bvecs,3.,odf_sphere='symmetric642',squared=True,auto=False,save_odfs=True) gq2.fit() #blobs=np.zeros((19,1,8,dn.odfn)) blobs=np.zeros((10,1,6,dn.odfn)) """ blobs[:,0,0]=dn.odfs() blobs[:,0,1]=dn2.odfs() blobs[:,0,2]=eis.odfs() blobs[:,0,3]=eil.odfs() blobs[:,0,4]=eil2.odfs() blobs[:,0,5]=ds.odfs() blobs[:,0,6]=gq.odfs() blobs[:,0,7]=gq2.odfs() """ blobs[:,0,0]=dn2.odfs() blobs[:,0,1]=eil.odfs() eo=eil2.odfs() #eo[eo<0.05*eo.max()]=0 blobs[:,0,2]=eo blobs[:,0,3]=ds.odfs() blobs[:,0,4]=gq.odfs() blobs[:,0,5]=gq2.odfs() show_blobs(blobs,dn.odf_vertices,dn.odf_faces,scale=0.5)
FA=ten.fa() FA[np.isnan(FA)]=0 eu=EuDX(FA,ten.ind(),seeds=10**4,a_low=fa_thr,length_thr=length_thr) T =[e for e in eu] #show(T,FA,ten.ind(),eu.odf_vertices,scale=1) #r=fvtk.ren() #fvtk.add(r,fvtk.point(eu.odf_vertices,cm.orient2rgb(eu.odf_vertices),point_radius=.5,theta=30,phi=30)) #fvtk.show(r) gqs=GeneralizedQSampling(data,bvals,bvecs,Lambda=1.,mask=mask,squared=False) eu2=EuDX(gqs.qa(),gqs.ind(),seeds=10**4,a_low=0,length_thr=length_thr) T2=[e for e in eu2] show(T2,gqs.qa(),gqs.ind(),eu2.odf_vertices,scale=1) ds=DiffusionSpectrum(data,bvals,bvecs,mask=mask) eu3=EuDX(ds.gfa(),ds.ind()[...,0],seeds=10**4,a_low=0,length_thr=length_thr) T3=[e for e in eu3] #show(T3,ds.gfa(),ds.ind()[...,0],eu3.odf_vertices,scale=1) eu4=EuDX(ds.nfa(),ds.ind(),seeds=10**4,a_low=0,length_thr=length_thr) T4=[e for e in eu4] #show(T4,ds.nfa(),ds.ind(),eu4.odf_vertices,scale=1) eu5=EuDX(ds.qa(),ds.ind(),seeds=10**4,a_low=0,length_thr=length_thr) T5=[e for e in eu5] #show(T5,ds.qa(),ds.ind(),eu5.odf_vertices,scale=1) """ #data[mask == False]=0