Ejemplo n.º 1
0
def test_eudx():

    # read bvals,gradients and data
    fimg, fbvals, fbvecs = get_data("small_64D")
    bvals = np.load(fbvals)
    gradients = np.load(fbvecs)
    img = ni.load(fimg)
    data = img.get_data()

    print(data.shape)
    gqs = GeneralizedQSampling(data, bvals, gradients)
    ten = Tensor(data, bvals, gradients, thresh=50)
    seed_list = np.dot(np.diag(np.arange(10)), np.ones((10, 3)))
    iT = iter(EuDX(gqs.qa(), gqs.ind(), seeds=seed_list))
    T = []
    for t in iT:
        T.append(t)
    iT2 = iter(EuDX(ten.fa(), ten.ind(), seeds=seed_list))
    T2 = []
    for t in iT2:
        T2.append(t)

    print("length T ", sum([length(t) for t in T]))
    print("length T2", sum([length(t) for t in T2]))

    print(gqs.QA[1, 4, 8, 0])
    print(gqs.QA.ravel()[ndarray_offset(np.array([1, 4, 8, 0]), np.array(gqs.QA.strides), 4, 8)])

    assert_almost_equal(
        gqs.QA[1, 4, 8, 0], gqs.QA.ravel()[ndarray_offset(np.array([1, 4, 8, 0]), np.array(gqs.QA.strides), 4, 8)]
    )

    assert_almost_equal(sum([length(t) for t in T]), 70.999996185302734, places=3)
    assert_almost_equal(sum([length(t) for t in T2]), 56.999997615814209, places=3)
Ejemplo n.º 2
0
def test_eudx_further():
    """ Cause we love testin.. ;-)
    """

    fimg, fbvals, fbvecs = get_data("small_101D")
    img = ni.load(fimg)
    affine = img.get_affine()
    bvals = np.loadtxt(fbvals)
    gradients = np.loadtxt(fbvecs).T
    data = img.get_data()
    ten = Tensor(data, bvals, gradients, thresh=50)
    x, y, z = data.shape[:3]
    seeds = np.zeros((10 ** 4, 3))
    for i in range(10 ** 4):
        rx = (x - 1) * np.random.rand()
        ry = (y - 1) * np.random.rand()
        rz = (z - 1) * np.random.rand()
        seeds[i] = np.ascontiguousarray(np.array([rx, ry, rz]), dtype=np.float64)

    # print seeds
    # """
    eu = EuDX(a=ten.fa(), ind=ten.ind(), seeds=seeds, a_low=0.2)
    T = [e for e in eu]

    # check that there are no negative elements
    for t in T:
        assert_equal(np.sum(t.ravel() < 0), 0)

    """
Ejemplo n.º 3
0
def test_eudx():
    
    #read bvals,gradients and data
    fimg,fbvals, fbvecs = get_data('small_64D')    
    bvals=np.load(fbvals)
    gradients=np.load(fbvecs)
    img =ni.load(fimg)    
    data=img.get_data()
    
    print(data.shape)    
    gqs = GeneralizedQSampling(data,bvals,gradients)       
    ten = Tensor(data,bvals,gradients,thresh=50)
    seed_list=np.dot(np.diag(np.arange(10)),np.ones((10,3)))    
    iT=iter(EuDX(gqs.qa(),gqs.ind(),seed_list=seed_list))
    T=[]
    for t in iT: 
        T.append(t)    
    iT2=iter(EuDX(ten.fa(),ten.ind(),seed_list=seed_list))
    T2=[]
    for t in iT2: 
        T2.append(t)
    
    print('length T ',sum([length(t) for t in T]))  
    print('length T2',sum([length(t) for t in T2]))  

    print(gqs.QA[1,4,8,0])
    print(gqs.QA.ravel()[ndarray_offset(np.array([1,4,8,0]),np.array(gqs.QA.strides),4,8)])

    assert_equal(gqs.QA[1,4,8,0], gqs.QA.ravel()[ndarray_offset(np.array([1,4,8,0]),np.array(gqs.QA.strides),4,8)])

    #assert_equal, sum([length(t) for t in T ]) , 77.999996662139893
    #assert_equal, sum([length(t) for t in T2]) , 63.499998092651367
    assert_equal(sum([length(t) for t in T ]) , 75.214988201856613)
    assert_equal(sum([length(t) for t in T2]) , 60.202986091375351)
Ejemplo n.º 4
0
def test_eudx_further():
    """ Cause we love testin.. ;-)
    """

    fimg, fbvals, fbvecs = get_data('small_101D')
    img = ni.load(fimg)
    affine = img.get_affine()
    bvals = np.loadtxt(fbvals)
    gradients = np.loadtxt(fbvecs).T
    data = img.get_data()
    ten = Tensor(data, bvals, gradients, thresh=50)
    x, y, z = data.shape[:3]
    seeds = np.zeros((10**4, 3))
    for i in range(10**4):
        rx = (x - 1) * np.random.rand()
        ry = (y - 1) * np.random.rand()
        rz = (z - 1) * np.random.rand()
        seeds[i] = np.ascontiguousarray(np.array([rx, ry, rz]),
                                        dtype=np.float64)

    #print seeds
    #"""
    eu = EuDX(a=ten.fa(), ind=ten.ind(), seeds=seeds, a_low=.2)
    T = [e for e in eu]

    #check that there are no negative elements
    for t in T:
        assert_equal(np.sum(t.ravel() < 0), 0)
    """
Ejemplo n.º 5
0
def test_eudx():

    #read bvals,gradients and data
    fimg, fbvals, fbvecs = get_data('small_64D')
    bvals = np.load(fbvals)
    gradients = np.load(fbvecs)
    img = ni.load(fimg)
    data = img.get_data()

    print(data.shape)
    gqs = GeneralizedQSampling(data, bvals, gradients)
    ten = Tensor(data, bvals, gradients, thresh=50)
    seed_list = np.dot(np.diag(np.arange(10)), np.ones((10, 3)))
    iT = iter(EuDX(gqs.qa(), gqs.ind(), seeds=seed_list))
    T = []
    for t in iT:
        T.append(t)
    iT2 = iter(EuDX(ten.fa(), ten.ind(), seeds=seed_list))
    T2 = []
    for t in iT2:
        T2.append(t)

    print('length T ', sum([length(t) for t in T]))
    print('length T2', sum([length(t) for t in T2]))

    print(gqs.QA[1, 4, 8, 0])
    print(gqs.QA.ravel()[ndarray_offset(np.array([1, 4, 8, 0]),
                                        np.array(gqs.QA.strides), 4, 8)])

    assert_almost_equal(
        gqs.QA[1, 4, 8, 0],
        gqs.QA.ravel()[ndarray_offset(np.array([1, 4, 8, 0]),
                                      np.array(gqs.QA.strides), 4, 8)])

    assert_almost_equal(sum([length(t) for t in T]),
                        70.999996185302734,
                        places=3)
    assert_almost_equal(sum([length(t) for t in T2]),
                        56.999997615814209,
                        places=3)
Ejemplo n.º 6
0
        nii_filename = base_filename + '_bet.nii.gz'
        bvec_filename = base_filename + '.bvec'
        bval_filename = base_filename + '.bval'        

        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

        base_dir2 = base_dir+ 'DTI/'   
        
        tensors = Tensor(data, bvals, gradients, thresh=50)
        create_save_tracks(tensors.fa(), tensors.ind(), 10**4, .2, base_dir2+'tracks_dti_10K_new.dpy')
      #  create_save_tracks(tensors.fa(), tensors.ind(), 10**6, .2, base_dir2+'tracks_dti_1M.dpy')
      #  create_save_tracks(tensors.fa(), tensors.ind(), 3*10**6, .2, base_dir2+'tracks_dti_3M.dpy')
#        
#        gqs=GeneralizedQSampling(data,bvals,gradients)
#        create_save_tracks(gqs.qa(), gqs.ind(), 10**4, .0239, base_dir2+'tracks_gqi_10K.dpy')
#        create_save_tracks(gqs.qa(), gqs.ind(), 10**6, .0239, base_dir2+'tracks_gqi_1M.dpy')
#        create_save_tracks(gqs.qa(), gqs.ind(), 3*10**6, .0239, base_dir2+'tracks_gqi_3M.dpy')
#        
             
        flirt_filename =base_dir2+'flirt.mat'
        fa_filename= base_dir2 + 'fa.nii.gz' 
        
        tracks_filename =  base_dir2+'tracks_dti_10K_new.dpy'
        linear_filename =  base_dir2+'tracks_dti_10K_linear_new.dpy'        
        warp_tracks_linearly(flirt_filename,fa_filename, tracks_filename,linear_filename)
        nii_filename = base_dir + "DTI/data_resample.nii.gz"
        bvec_filename = base_filename + ".bvec"
        bval_filename = base_filename + ".bval"

        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

        base_dir2 = base_dir + "DTI/"

        tensors = Tensor(data, bvals, gradients, thresh=50)
        create_save_tracks(tensors.fa(), tensors.ind(), 10 ** 4, 0.2, base_dir2 + "tracks_dti_10K.dpy")
        create_save_tracks(tensors.fa(), tensors.ind(), 10 ** 6, 0.2, base_dir2 + "tracks_dti_1M.dpy")
        create_save_tracks(tensors.fa(), tensors.ind(), 3 * 10 ** 6, 0.2, base_dir2 + "tracks_dti_3M.dpy")

        # =====================  gqi ========================================
        #        gqs=GeneralizedQSampling(data,bvals,gradients)
        #        create_save_tracks(gqs.qa(), gqs.ind(), 10**4, .0239, base_dir2+'tracks_gqi_10K.dpy')
        #        create_save_tracks(gqs.qa(), gqs.ind(), 10**6, .0239, base_dir2+'tracks_gqi_1M.dpy')
        #        create_save_tracks(gqs.qa(), gqs.ind(), 3*10**6, .0239, base_dir2+'tracks_gqi_3M.dpy')
        # =====================  gqi ========================================

        print "Done warped"

        flirt_filename = base_dir2 + "flirt.mat"
        fa_filename = base_dir2 + "fa_resample.nii.gz"
Ejemplo n.º 8
0
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))
Ejemplo n.º 9
0
    all,allo=fvtk.crossing(A,IND,VERTS,scale,True)
    colors=np.zeros((len(all),3))
    for (i,a) in enumerate(all):
        if allo[i][0]==0 and allo[i][1]==0 and allo[i][2]==1:
            pass
        else:            
            colors[i]=cm.boys2rgb(allo[i])
    
    fvtk.add(r,fvtk.line(all,colors))    
    fvtk.show(r)


ten=Tensor(data,bvals,bvecs,mask)
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]
Ejemplo n.º 10
0
for i in range(w):
    if i not in ind[1:]:        
        data2[:,:,:,cnt]=data[:,:,:,i]
        bvals2[cnt]=bvals[i]
        bvecs2[cnt,:]=bvecs[i,:]
        cnt+=1

print 'after',bvals2.shape,bvecs2.shape


#always check the threshold but 50 should be okay
ten=Tensor(data2,bvals2,bvecs2,thresh=50)

#ten.ind is indices of the eigen directions projected on a sphere
#stopping criteria is FA of .2
eu=EuDX(a=ten.fa(),ind=ten.ind(),seeds=5000,a_low=0.2)

#generate tracks
ten_tracks=[track for track in eu]
print 'No tracks ',len(ten_tracks)

#remove short tracks smaller than 40mm i.e. 20 in native units
ten_tracks=[t for t in ten_tracks if length(t)>20]
print 'No reduced tracks ',len(ten_tracks)

raw_input('Press enter...')


#load the rois
imsk1=nib.load(dname+'/'+froi1)
roi1=imsk1.get_data()
Ejemplo n.º 11
0
    ei.fit()

    print 'EITL', time()-t    
    t=time()
    #DSI
    ds=DiffusionSpectrum(data,bvals,gradients,\
                odf_sphere='symmetric642',\
                mask=famask,\
                half_sphere_grads=True,\
                auto=True,\
                save_odfs=False)
    print 'DSI', time()-t
    #ds.PK[FA<.2]=np.zeros(5)
    
    t=time()
    euler=EuDX(a=FA,ind=tensors.ind(),seeds=seeds,a_low=.2)
    T=[track for track in euler] 
    print 'Eudx ten',time()-t,len(T)

    dpr_linear = Dpy(ftracks[0], 'w')
    dpr_linear.write_tracks(T)
    dpr_linear.close()
    del T


    for i,qgrid in enumerate([ds,gqs,ei]):
        t=time()
        euler=EuDX(a=qgrid.PK,ind=qgrid.IN,seeds=seeds,odf_vertices=qgrid.odf_vertices,a_low=.2)
        T=[track for track in euler]     
        print 'Eudx ',time()-t, len(T)
        dpr_linear = Dpy(ftracks[i+1], 'w')