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)
def test(): # img=nib.load('/home/eg309/Data/project01_dsi/connectome_0001/tp1/RAWDATA/OUT/mr000001.nii.gz') btable = np.loadtxt(get_data("dsi515btable")) # volume size sz = 16 # shifting origin = 8 # hanning width filter_width = 32.0 # number of signal sampling points n = 515 # odf radius radius = np.arange(2.1, 6, 0.2) # create q-table bv = btable[:, 0] bmin = np.sort(bv)[1] bv = np.sqrt(bv / bmin) qtable = np.vstack((bv, bv, bv)).T * btable[:, 1:] qtable = np.floor(qtable + 0.5) # copy bvals and bvecs bvals = btable[:, 0] bvecs = btable[:, 1:] # S=img.get_data()[38,50,20]#[96/2,96/2,20] S, stics = SticksAndBall( bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (60, 0), (90, 90)], fractions=[0, 0, 0], snr=None ) S2 = S.copy() S2 = S2.reshape(1, len(S)) dn = DiffusionNabla(S2, bvals, bvecs, auto=False) pR = dn.equators odf = dn.odf(S) # Xs=dn.precompute_interp_coords() peaks, inds = peak_finding(odf.astype("f8"), dn.odf_faces.astype("uint16")) print peaks print peaks / peaks.min() # print dn.PK dn.fit() print dn.PK # """ ren = fvtk.ren() colors = fvtk.colors(odf, "jet") fvtk.add(ren, fvtk.point(dn.odf_vertices, colors, point_radius=0.05, theta=8, phi=8)) fvtk.show(ren) # """ stop # ds=DiffusionSpectrum(S2,bvals,bvecs) # tpr=ds.pdf(S) # todf=ds.odf(tpr) """ #show projected signal Bvecs=np.concatenate([bvecs[1:],-bvecs[1:]]) X0=np.dot(np.diag(np.concatenate([S[1:],S[1:]])),Bvecs) ren=fvtk.ren() fvtk.add(ren,fvtk.point(X0,fvtk.yellow,1,2,16,16)) fvtk.show(ren) """ # qtable=5*matrix[:,1:] # calculate radius for the hanning filter r = np.sqrt(qtable[:, 0] ** 2 + qtable[:, 1] ** 2 + qtable[:, 2] ** 2) # setting hanning filter width and hanning hanning = 0.5 * np.cos(2 * np.pi * r / filter_width) # center and index in q space volume q = qtable + origin q = q.astype("i8") # apply the hanning filter values = S * hanning """ #plot q-table ren=fvtk.ren() colors=fvtk.colors(values,'jet') fvtk.add(ren,fvtk.point(q,colors,1,0.1,6,6)) fvtk.show(ren) """ # create the signal volume Sq = np.zeros((sz, sz, sz)) for i in range(n): Sq[q[i][0], q[i][1], q[i][2]] += values[i] # apply fourier transform Pr = fftshift(np.abs(np.real(fftn(fftshift(Sq), (sz, sz, sz))))) # """ ren = fvtk.ren() vol = fvtk.volume(Pr) fvtk.add(ren, vol) fvtk.show(ren) # """ """ from enthought.mayavi import mlab mlab.pipeline.volume(mlab.pipeline.scalar_field(Sq)) mlab.show() """ # vertices, edges, faces = create_unit_sphere(5) vertices, faces = sphere_vf_from("symmetric362") odf = np.zeros(len(vertices)) for m in range(len(vertices)): xi = origin + radius * vertices[m, 0] yi = origin + radius * vertices[m, 1] zi = origin + radius * vertices[m, 2] PrI = map_coordinates(Pr, np.vstack((xi, yi, zi)), order=1) for i in range(len(radius)): odf[m] = odf[m] + PrI[i] * radius[i] ** 2 """ ren=fvtk.ren() colors=fvtk.colors(odf,'jet') fvtk.add(ren,fvtk.point(vertices,colors,point_radius=.05,theta=8,phi=8)) fvtk.show(ren) """ """ #Pr[Pr<500]=0 ren=fvtk.ren() #ren.SetBackground(1,1,1) fvtk.add(ren,fvtk.volume(Pr)) fvtk.show(ren) """ peaks, inds = peak_finding(odf.astype("f8"), faces.astype("uint16")) Eq = np.zeros((sz, sz, sz)) for i in range(n): Eq[q[i][0], q[i][1], q[i][2]] += S[i] / S[0] LEq = laplace(Eq) # Pr[Pr<500]=0 ren = fvtk.ren() # ren.SetBackground(1,1,1) fvtk.add(ren, fvtk.volume(Eq)) fvtk.show(ren) phis = np.linspace(0, 2 * np.pi, 100) planars = [] for phi in phis: planars.append(sphere2cart(1, np.pi / 2, phi)) planars = np.array(planars) planarsR = [] for v in vertices: R = vec2vec_rotmat(np.array([0, 0, 1]), v) planarsR.append(np.dot(R, planars.T).T) """ ren=fvtk.ren() fvtk.add(ren,fvtk.point(planarsR[0],fvtk.green,1,0.1,8,8)) fvtk.add(ren,fvtk.point(2*planarsR[1],fvtk.red,1,0.1,8,8)) fvtk.show(ren) """ azimsums = [] for disk in planarsR: diskshift = 4 * disk + origin # Sq0=map_coordinates(Sq,diskshift.T,order=1) # azimsums.append(np.sum(Sq0)) # Eq0=map_coordinates(Eq,diskshift.T,order=1) # azimsums.append(np.sum(Eq0)) LEq0 = map_coordinates(LEq, diskshift.T, order=1) azimsums.append(np.sum(LEq0)) azimsums = np.array(azimsums) # """ ren = fvtk.ren() colors = fvtk.colors(azimsums, "jet") fvtk.add(ren, fvtk.point(vertices, colors, point_radius=0.05, theta=8, phi=8)) fvtk.show(ren) # """ # for p in planarsR[0]: """
def show_simulated_multiple_crossings(): ''' S=np.array([[-1,0,0],[0,1,0],[0,0,1]]) #T=np.array([[1,0,0],[0,0,]]) T=np.array([[1,0,0],[-0.76672114, 0.26105963, -0.58650369]]) print 'yo ',compare_orientation_sets(S,T) ''' btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] """ path='/home/eg309/Data/PROC_MR10032/subj_03/101_32/1312211075232351192010121313490254679236085ep2dadvdiffDSI10125x25x25STs004a001' #path='/home/eg309/Data/101_32/1312211075232351192010121313490254679236085ep2dadvdiffDSI10125x25x25STs004a001' bvals=np.loadtxt(path+'.bval') bvecs=np.loadtxt(path+'.bvec').T bvals=np.append(bvals.copy(),bvals[1:].copy()) bvecs=np.append(bvecs.copy(),-bvecs[1:].copy(),axis=0) """ #bvals=np.load('/tmp/bvals.npy') #bvecs=np.load('/tmp/bvecs.npy') #data=create_data(bvals,bvecs,d=0.00075,S0=100,snr=None) #dvals=[0.0003, 0.0006, 0.0012, 0.0024, 0.0048] dvals=[0.0003, 0.0005, 0.0010, 0.0015, 0.0020, 0.0025, 0.0030] data = multi_d_isotropic_data(bvals,bvecs,dvals=dvals,S0=100,snr=None) dn=DiffusionNabla(data,bvals,bvecs,odf_sphere='symmetric362', auto=False,save_odfs=True,fast=False,rotation=None) #rotation=np.array([[0,0,1],[np.sqrt(0.5),np.sqrt(0.5),0.],[np.sqrt(0.5),-np.sqrt(0.5),0.]]) dn.peak_thr=.4 dn.iso_thr=.05 dn.radius=np.arange(0,6,0.2) 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(0.01) dn.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.peak_thr=.6 gq.iso_thr=.7 gq.fit() gq2=GeneralizedQSampling(data,bvals,bvecs,2.,odf_sphere='symmetric642',squared=True,auto=False,save_odfs=True) gq2.peak_thr=.6 gq2.iso_thr=.7 gq2.fit() ''' dn_odfs=dn.odfs() #blobs=np.zeros((1,19,4,dn.odfn)) blobs=np.zeros((1,len(dvals),1,dn.odfn)) #blobs=np.zeros((1,19,1,dn.odfn)) #blobs[0,:,0]=dn.odfs() for i,d in enumerate(dvals): blobs[0,i,0]=dn.ODF[i]/dn.ODF[i].mean() ''' blobs[0,:,1]=ds.odfs() blobs[0,:,2]=gq.odfs() gq2odfs=gq2.odfs() gq2odfs[gq2odfs<0]=0 print 'gq2',gq2odfs.min(),gq2odfs.max() blobs[0,:,3]=gq2odfs ''' ''' peaknos=np.array([0,1,2,3,3,3,3,3,3,2,2,1,1,2,3,0,3,3,3]) print peaknos print np.sum(dn.pk()>0,axis=1),np.sum(np.sum(dn.pk()>0,axis=1)-peaknos),len(peaknos) ''' ''' print np.sum(ds.qa()>0,axis=1),np.sum(np.sum(ds.qa()>0,axis=1)-peaknos),len(peaknos) print np.sum(gq.qa()>0,axis=1),np.sum(np.sum(gq.qa()>0,axis=1)-peaknos),len(peaknos) print np.sum(gq2.qa()>0,axis=1),np.sum(np.sum(gq2.qa()>0,axis=1)-peaknos),len(peaknos) ''' show_blobs(blobs,dn.odf_vertices,dn.odf_faces,colormap='jet') for i, d in enumerate(dvals): m = dn.ODF[i].min() M = dn.ODF[i].max() vM = np.argmax(dn.ODF[i]) print i, d, 50*(M-m)/(M+m), dn.odf_vertices[vM] #odf=dn.fast_odf(data[0]) #r=fvtk.ren() #fvtk.add(r,fvtk.volume(dn.Eq)) #fvtk.show(r) """ blobs2=np.zeros((1,1,3,dn.odfn)) blobs2[0,:,0]=dn.log_fast_odf(data[15]) blobs2[0,:,1]=dn.fast_odf(data[15]) blobs2[0,:,2]=dn.log_slow_odf(data[15]) show_blobs(blobs2,dn.odf_vertices,dn.odf_faces) e=data[15]/data[15,0] ne=np.sqrt(-np.log(e)) norm=np.sum(dn.qtable**2,axis=1) """ """
def compare_sdni_eitl(): 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) 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 ei.set_operator('laplacian') #{'reflect','constant','nearest','mirror', 'wrap'} ei.set_mode(order=1,zoom=1,mode='constant') ei.update() ei.fit() dn=DiffusionNabla(data,bvals,bvecs,odf_sphere='symmetric642', auto=False,save_odfs=True,fast=False) dn.radius=np.arange(0,5,0.1) dn.gaussian_weight=None dn.update() dn.fit() vts=ei.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 print "test0" thresh=0.5 PK,IN=simple_peaks(ei.ODF,ei.odf_faces,thresh) res,me,st =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) #PKG,ING=simple_peaks(eig.ODF,ei.odf_faces,thresh) #resg,meg,stg =do_compare(gold,vts,PKG,ING,0,anglesn,axesn,type) PK,IN=simple_peaks(dn.ODF,dn.odf_faces,thresh) res2,me2,st2 =do_compare(gold,vts,PK,IN,0,anglesn,axesn,type) 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 print "test1" plt.figure(1) plt.plot(angles,me,'k:',linewidth=3.,label='EITL') plt.plot(angles,me2,'k--',linewidth=3.,label='sDNI') #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()
S,stics=SticksAndBall(bvals, bvecs, d, S0, angles=[(30, 0),(60,0),(90,90)], fractions=[0,0,0], snr=snr) data[13]=S.copy() return data if __name__ == '__main__': #def test_dni(): btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] data=sim_data(bvals,bvecs) dn=DiffusionNabla(data,bvals,bvecs,save_odfs=True) pks=dn.pk() #assert_array_equal(np.sum(pks>0,axis=1), # np.array([0, 1, 2, 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, 0])) odfs=dn.odfs() peaks,inds=peak_finding(odfs[10],dn.odf_faces)
""" path='/home/eg309/Data/PROC_MR10032/subj_03/101_32/1312211075232351192010121313490254679236085ep2dadvdiffDSI10125x25x25STs004a001' #path='/home/eg309/Data/101_32/1312211075232351192010121313490254679236085ep2dadvdiffDSI10125x25x25STs004a001' bvals=np.loadtxt(path+'.bval') bvecs=np.loadtxt(path+'.bvec').T bvals=np.append(bvals.copy(),bvals[1:].copy()) bvecs=np.append(bvecs.copy(),-bvecs[1:].copy(),axis=0) """ #bvals=np.load('/tmp/bvals.npy') #bvecs=np.load('/tmp/bvecs.npy') #data=create_data(bvals,bvecs,d=0.00075,S0=100,snr=None) #dvals=[0.0003, 0.0006, 0.0012, 0.0024, 0.0048] dvals=[0.0003, 0.0005, 0.0010, 0.0015, 0.0020, 0.0025, 0.0030] data = multi_d_isotropic_data(bvals,bvecs,dvals=dvals,S0=100,snr=None) dn=DiffusionNabla(data,bvals,bvecs,odf_sphere='symmetric362', auto=False,save_odfs=True,fast=False,rotation=None) #rotation=np.array([[0,0,1],[np.sqrt(0.5),np.sqrt(0.5),0.],[np.sqrt(0.5),-np.sqrt(0.5),0.]]) dn.peak_thr=.4 dn.iso_thr=.05 dn.radius=np.arange(0,6,0.2) 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(0.01) dn.fit() '''
np.save('/tmp/test.npy',fvol) #stop """ fvol=np.load('/tmp/test.npy') #data=np.load('/tmp/test.npy') dat=fvol[22:42,22:42,32:34] del fvol t1=time() #""" dn=DiffusionNabla(dat,bvals,bvecs,odf_sphere='symmetric362', auto=True,save_odfs=True,fast=True) """ dn.radius=np.arange(0,6,.2) dn.radiusn=len(dn.radius) dn.create_qspace(bvals,bvecs,16,8) dn.peak_thr=.4 dn.iso_thr=.7 dn.radon_params(64) dn.precompute_interp_coords() dn.fit() """ #from dipy.reconst.dsi import DiffusionSpectrum #mask=dat[...,0]>0 #dn=DiffusionSpectrum(dat,bvals,bvecs,save_odfs=True)