def show_gaussian_smoothing_effects(): SNR=20 type='laplacian' print 'Loading data' 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=[33,33,33], snr=SNR) data=S[None,:] 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(type) 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=0.01 ei2.set_operator(type) 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=0.05 ei3.set_operator(type) ei3.update() ei3.fit() ei4=EquatorialInversion(data,bvals,bvecs,odf_sphere='symmetric642',auto=False,save_odfs=True,fast=True) ei4.radius=np.arange(0,5,0.1) ei4.gaussian_weight=0.1 ei4.set_operator(type) ei4.update() ei4.fit() blobs=np.zeros((4,len(ei.odf_vertices))) blobs[0]=ei.ODF.ravel() blobs[1]=ei2.ODF.ravel() blobs[2]=ei3.ODF.ravel() blobs[3]=ei4.ODF.ravel() show_blobs(blobs[None,None,:,:],ei.odf_vertices,ei.odf_faces,size=1.5,scale=1.)
def show_phantom_crossing(fname,type='dsi',weight=None): btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] #fname='/home/eg309/Desktop/t5' #fname='/home/eg309/Data/orbital_phantoms/20.0' #fname='/tmp/t5' fvol = np.memmap(fname, dtype='f8', mode='r', shape=(64,64,64,len(bvals))) data=fvol[32-10:32+10,32-10:32+10,31:34,:] #data=fvol[32,32,32,:][None,None,None,:] ten=Tensor(data,bvals,bvecs,thresh=50) FA=ten.fa() def H(x): res=(2*x*np.cos(x) + (x**2-2)*np.sin(x))/x**3 res[np.isnan(res)]=1/3. return res if type=='dsi': mod=DiffusionSpectrum(data,bvals,bvecs,odf_sphere='symmetric642',auto=True,save_odfs=True) if type=='gqi': mod=GeneralizedQSampling(data,bvals,bvecs,1.2,odf_sphere='symmetric642',squared=False,save_odfs=True) if type=='gqi2': mod=GeneralizedQSampling(data,bvals,bvecs,3.,odf_sphere='symmetric642',squared=True,save_odfs=True) if type=='eitl': 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=weight ei.set_operator('laplacian') ei.update() ei.fit() mod=ei if type=='eitl2': 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=weight ei.set_operator('laplap') ei.update() ei.fit() mod=ei if type=='eits': 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=weight ei.set_operator('signal') ei.update() ei.fit() mod=ei show_blobs(mod.ODF[:,:,0,:][:,:,None,:],mod.odf_vertices,mod.odf_faces,fa_slice=FA[:,:,0],size=1.5,scale=1.)
#dn.radius=np.arange(1.,6,0.2) #dn.radius=np.arange(0,1.,0.2) dn.radiusn=len(dn.radius) dn.origin=8#16 dn.precompute_fast_coords() dn.precompute_equator_indices(5.) dn.precompute_angular(0.1) dn.fit() """ ei=EquatorialInversion(dat,bvals,bvecs,odf_sphere='symmetric642', half_sphere_grads=True,auto=False,save_odfs=True,fast=True) ei.radius=np.arange(0,5,0.1) ei.gaussian_weight=0.05 ei.set_operator('laplacian') ei.update() ei.fit() ds=DiffusionSpectrum(dat,bvals,bvecs, odf_sphere='symmetric642', mask=None, half_sphere_grads=True, auto=True, save_odfs=True) ten=Tensor(dat,bvals,bvecs) FA=ten.fa() #gq=GeneralizedQSampling(dat,bvals,bvecs,1.2,odf_sphere='symmetric642',squared=False,save_odfs=True) #gq2=GeneralizedQSampling(dat,bvals,bvecs,3.,odf_sphere='symmetric642',squared=True,save_odfs=True) #show_blobs(ds.ODF,ds.odf_vertices,ds.odf_faces,FA.squeeze(),size=1.5,scale=1.)
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)
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()
def humans(): no_seeds=10**6 visualize = False save_odfs = False dirname = "data/" for root, dirs, files in os.walk(dirname): if root.endswith('101_32'): base_dir = root+'/' filename = 'raw' base_filename = base_dir + filename nii_filename = base_filename + 'bet.nii.gz' bvec_filename = base_filename + '.bvec' bval_filename = base_filename + '.bval' flirt_mat = base_dir + 'DTI/flirt.mat' fa_filename = base_dir + 'DTI/fa.nii.gz' fsl_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' dpy_filename = base_dir + 'DTI/res_tracks_dti.dpy' print bvec_filename img = nib.load(nii_filename) data = img.get_data() affine = img.get_affine() bvals = np.loadtxt(bval_filename) gradients = np.loadtxt(bvec_filename).T # this is the unitary direction of the gradient tensors = Tensor(data, bvals, gradients, thresh=50) FA = tensors.fa() FA = tensors.fa() famask=FA>=.2 ds=DiffusionSpectrum(data,bvals,gradients,odf_sphere='symmetric642',mask=famask,half_sphere_grads=True,auto=True,save_odfs=save_odfs) gq=GeneralizedQSampling(data,bvals,gradients,1.2,odf_sphere='symmetric642',mask=famask,squared=False,save_odfs=save_odfs) ei=EquatorialInversion(data,bvals,gradients,odf_sphere='symmetric642',mask=famask,half_sphere_grads=True,auto=False,save_odfs=save_odfs,fast=True) ei.radius=np.arange(0,5,0.4) ei.gaussian_weight=0.05 ei.set_operator('laplacian') ei.update() ei.fit() ds.PK[FA<.2]=np.zeros(5) ei.PK[FA<.2]=np.zeros(5) gq.PK[FA<.2]=np.zeros(5) print 'create seeds' x,y,z,g=ei.PK.shape seeds=np.zeros((no_seeds,3)) sid=0 while sid<no_seeds: rx=(x-1)*np.random.rand() ry=(y-1)*np.random.rand() rz=(z-1)*np.random.rand() seed=np.ascontiguousarray(np.array([rx,ry,rz]),dtype=np.float64) seeds[sid]=seed sid+=1 euler = EuDX(a=FA, ind=tensors.ind(), seeds=seeds, a_low=.2) dt_tracks = [track for track in euler] euler2 = EuDX(a=ds.PK, ind=ds.IN, seeds=seeds, odf_vertices=ds.odf_vertices, a_low=.2) ds_tracks = [track for track in euler2] euler3 = EuDX(a=gq.PK, ind=gq.IN, seeds=seeds, odf_vertices=gq.odf_vertices, a_low=.2) gq_tracks = [track for track in euler3] euler4 = EuDX(a=ei.PK, ind=ei.IN, seeds=seeds, odf_vertices=ei.odf_vertices, a_low=.2) ei_tracks = [track for track in euler4] if visualize: renderer = fvtk.ren() fvtk.add(renderer, fvtk.line(tensor_tracks, fvtk.red, opacity=1.0)) fvtk.show(renderer) print 'Load images to be used for registration' img_fa =nib.load(fa_filename) img_ref =nib.load(fsl_ref) mat=flirt2aff(np.loadtxt(flirt_mat),img_fa,img_ref) del img_fa del img_ref print 'transform the tracks' dt_linear = transform_tracks(dt_tracks,mat) ds_linear = transform_tracks(ds_tracks,mat) gq_linear = transform_tracks(gq_tracks,mat) ei_linear = transform_tracks(ei_tracks,mat) print 'save tensor tracks' dpy_filename = base_dir + 'DTI/dt_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(dt_linear) dpr_linear.close() print 'save ei tracks' dpy_filename = base_dir + 'DTI/ei_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(ei_linear) dpr_linear.close() print 'save ds tracks' dpy_filename = base_dir + 'DTI/ds_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(ds_linear) dpr_linear.close() print 'save gq tracks' dpy_filename = base_dir + 'DTI/gq_linear.dpy' print dpy_filename dpr_linear = Dpy(dpy_filename, 'w') dpr_linear.write_tracks(gq_linear) dpr_linear.close() print 'save lengths' pkl_filename = base_dir + 'DTI/dt_lengths.pkl' save_pickle(pkl_filename,lengths(dt_linear)) pkl_filename = base_dir + 'DTI/ei_lengths.pkl' save_pickle(pkl_filename,lengths(ei_linear)) pkl_filename = base_dir + 'DTI/gq_lengths.pkl' save_pickle(pkl_filename,lengths(gq_linear)) pkl_filename = base_dir + 'DTI/ds_lengths.pkl' save_pickle(pkl_filename,lengths(ds_linear))