Example #1
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)
Example #2
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)
Example #3
0
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()