Esempio n. 1
0
def sticks_and_ball_dummies(gtab):
    sb_dummies = {}
    S, sticks = SticksAndBall(gtab,
                              d=0.0015,
                              S0=100,
                              angles=[(0, 0)],
                              fractions=[100],
                              snr=None)
    sb_dummies['1fiber'] = (S, sticks)
    S, sticks = SticksAndBall(gtab,
                              d=0.0015,
                              S0=100,
                              angles=[(0, 0), (90, 0)],
                              fractions=[50, 50],
                              snr=None)
    sb_dummies['2fiber'] = (S, sticks)
    S, sticks = SticksAndBall(gtab,
                              d=0.0015,
                              S0=100,
                              angles=[(0, 0), (90, 0), (90, 90)],
                              fractions=[33, 33, 33],
                              snr=None)
    sb_dummies['3fiber'] = (S, sticks)
    S, sticks = SticksAndBall(gtab,
                              d=0.0015,
                              S0=100,
                              angles=[(0, 0), (90, 0), (90, 90)],
                              fractions=[0, 0, 0],
                              snr=None)
    sb_dummies['isotropic'] = (S, sticks)
    return sb_dummies
Esempio n. 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)
Esempio n. 3
0
def create_data_2fibers(bvals,bvecs,d=0.0015,S0=100,angles=np.arange(0,92,5),snr=None):
    
    data=np.zeros((len(angles),len(bvals)))
        
    for (i,a) in enumerate(angles):
        #2 two fibers
        S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(a,0),(0,0)], 
                          fractions=[50,50,0], snr=snr)
        data[i]=S.copy()
        
    return data
Esempio n. 4
0
def multi_d_isotropic_data(bvals,bvecs,dvals=[0.0015],S0=100,snr=None):
    print dvals, len(dvals)
    #data=np.zeros((19,len(bvals)))
    data=np.zeros((len(dvals),len(bvals)))
    
    for i, d in enumerate(dvals):
        #0 isotropic
        S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[0,0,0], snr=snr)
        data[i]=S.copy()
    
    return data
Esempio n. 5
0
def test_dsi_metrics():
    btable = np.loadtxt(get_fnames('dsi4169btable'))
    gtab = gradient_table(btable[:, 0], btable[:, 1:])
    data, golden_directions = SticksAndBall(gtab,
                                            d=0.0015,
                                            S0=100,
                                            angles=[(0, 0), (60, 0)],
                                            fractions=[50, 50],
                                            snr=None)

    dsmodel = DiffusionSpectrumModel(gtab, qgrid_size=21, filter_width=4500)
    rtop_signal_norm = dsmodel.fit(data).rtop_signal()
    dsmodel.fit(data).rtop_pdf()
    rtop_pdf = dsmodel.fit(data).rtop_pdf(normalized=False)
    assert_almost_equal(rtop_signal_norm, rtop_pdf, 6)
    dsmodel = DiffusionSpectrumModel(gtab, qgrid_size=21, filter_width=4500)
    mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]))
    S_0, sticks_0 = MultiTensor(gtab,
                                mevals,
                                S0=100,
                                angles=[(0, 0), (60, 0)],
                                fractions=[50, 50],
                                snr=None)
    S_1, sticks_0 = MultiTensor(gtab,
                                mevals * 2.0,
                                S0=100,
                                angles=[(0, 0), (60, 0)],
                                fractions=[50, 50],
                                snr=None)
    MSD_norm_0 = dsmodel.fit(S_0).msd_discrete(normalized=True)
    MSD_norm_1 = dsmodel.fit(S_1).msd_discrete(normalized=True)
    assert_almost_equal(MSD_norm_0, 0.5 * MSD_norm_1, 4)
Esempio n. 6
0
def dsi_deconv_voxels():
    gtab = gradient_table(np.loadtxt(get_data('dsi515btable')))
    data = np.zeros((2, 2, 2, 515))
    for ix in range(2):
        for iy in range(2):
            for iz in range(2):
                data[ix, iy, iz], dirs = SticksAndBall(gtab,
                                                       d=0.0015,
                                                       S0=100,
                                                       angles=[(0, 0), (90, 0)],
                                                       fractions=[50, 50],
                                                       snr=None)
    return data, gtab
Esempio n. 7
0
def test_shore_odf():
    gtab = get_isbi2013_2shell_gtab()

    # load symmetric 724 sphere
    sphere = get_sphere('symmetric724')

    # load icosahedron sphere
    sphere2 = create_unit_sphere(5)
    data, golden_directions = SticksAndBall(gtab,
                                            d=0.0015,
                                            S0=100,
                                            angles=[(0, 0), (90, 0)],
                                            fractions=[50, 50],
                                            snr=None)
    asm = ShoreModel(gtab,
                     radial_order=6,
                     zeta=700,
                     lambdaN=1e-8,
                     lambdaL=1e-8)
    # symmetric724
    asmfit = asm.fit(data)
    odf = asmfit.odf(sphere)
    odf_sh = asmfit.odf_sh()
    odf_from_sh = sh_to_sf(odf_sh, sphere, 6, basis_type=None)
    assert_almost_equal(odf, odf_from_sh, 10)

    directions, _, _ = peak_directions(odf, sphere, .35, 25)
    assert_equal(len(directions), 2)
    assert_almost_equal(angular_similarity(directions, golden_directions), 2,
                        1)

    # 5 subdivisions
    odf = asmfit.odf(sphere2)
    directions, _, _ = peak_directions(odf, sphere2, .35, 25)
    assert_equal(len(directions), 2)
    assert_almost_equal(angular_similarity(directions, golden_directions), 2,
                        1)

    sb_dummies = sticks_and_ball_dummies(gtab)
    for sbd in sb_dummies:
        data, golden_directions = sb_dummies[sbd]
        asmfit = asm.fit(data)
        odf = asmfit.odf(sphere2)
        directions, _, _ = peak_directions(odf, sphere2, .35, 25)
        if len(directions) <= 3:
            assert_equal(len(directions), len(golden_directions))
        if len(directions) > 3:
            assert_equal(gfa(odf) < 0.1, True)
Esempio n. 8
0
def test_dsi():
    # load symmetric 724 sphere
    sphere = get_sphere('symmetric724')
    # load icosahedron sphere
    sphere2 = create_unit_sphere(5)
    btable = np.loadtxt(get_data('dsi515btable'))
    gtab = gradient_table(btable[:, 0], btable[:, 1:])
    data, golden_directions = SticksAndBall(gtab,
                                            d=0.0015,
                                            S0=100,
                                            angles=[(0, 0), (90, 0)],
                                            fractions=[50, 50],
                                            snr=None)

    ds = DiffusionSpectrumDeconvModel(gtab)

    # symmetric724
    dsfit = ds.fit(data)
    odf = dsfit.odf(sphere)
    directions, _, _ = peak_directions(odf, sphere, .35, 25)
    assert_equal(len(directions), 2)
    assert_almost_equal(angular_similarity(directions, golden_directions), 2,
                        1)

    # 5 subdivisions
    dsfit = ds.fit(data)
    odf2 = dsfit.odf(sphere2)
    directions, _, _ = peak_directions(odf2, sphere2, .35, 25)
    assert_equal(len(directions), 2)
    assert_almost_equal(angular_similarity(directions, golden_directions), 2,
                        1)

    assert_equal(dsfit.pdf().shape, 3 * (ds.qgrid_size, ))
    sb_dummies = sticks_and_ball_dummies(gtab)
    for sbd in sb_dummies:
        data, golden_directions = sb_dummies[sbd]
        odf = ds.fit(data).odf(sphere2)
        directions, _, _ = peak_directions(odf, sphere2, .35, 25)
        if len(directions) <= 3:
            assert_equal(len(directions), len(golden_directions))
        if len(directions) > 3:
            assert_equal(gfa(odf) < 0.1, True)

    assert_raises(ValueError,
                  DiffusionSpectrumDeconvModel,
                  gtab,
                  qgrid_size=16)
Esempio n. 9
0
def test_gqi():
    #load symmetric 724 sphere
    sphere = get_sphere('symmetric724')
    #load icosahedron sphere
    sphere2 = create_unit_sphere(5)
    btable = np.loadtxt(get_data('dsi515btable'))
    bvals = btable[:, 0]
    bvecs = btable[:, 1:]
    gtab = gradient_table(bvals, bvecs)
    data, golden_directions = SticksAndBall(gtab,
                                            d=0.0015,
                                            S0=100,
                                            angles=[(0, 0), (90, 0)],
                                            fractions=[50, 50],
                                            snr=None)
    gq = GeneralizedQSamplingModel(gtab, method='gqi2', sampling_length=1.4)

    #symmetric724
    gqfit = gq.fit(data)
    odf = gqfit.odf(sphere)
    directions, values, indices = peak_directions(odf, sphere, .35, 25)
    assert_equal(len(directions), 2)
    assert_almost_equal(angular_similarity(directions, golden_directions), 2,
                        1)

    #5 subdivisions
    gqfit = gq.fit(data)
    odf2 = gqfit.odf(sphere2)
    directions, values, indices = peak_directions(odf2, sphere2, .35, 25)
    assert_equal(len(directions), 2)
    assert_almost_equal(angular_similarity(directions, golden_directions), 2,
                        1)

    sb_dummies = sticks_and_ball_dummies(gtab)
    for sbd in sb_dummies:
        data, golden_directions = sb_dummies[sbd]
        odf = gq.fit(data).odf(sphere2)
        directions, values, indices = peak_directions(odf, sphere2, .35, 25)
        if len(directions) <= 3:
            assert_equal(len(directions), len(golden_directions))
        if len(directions) > 3:
            assert_equal(gfa(odf) < 0.1, True)
Esempio n. 10
0
def test_gqi():

    #load odf sphere
    vertices, faces = sphere_vf_from('symmetric724')
    edges = unique_edges(faces)
    half_vertices, half_edges, half_faces = reduce_antipodal(vertices, faces)

    #load bvals and gradients
    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))

    odf_sphere = (vertices, faces)
    ds = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values > 0).sum(), 3)

    #change thresholds
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 30
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values > 0).sum(), 2)

    #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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 20
    dsfit = ds.fit(S)
    QA = dsfit.qa
    #1/0
    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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 20
    dsfit = ds.fit(S)
    QA = dsfit.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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    dsfit = ds.fit(S)
    QA = dsfit.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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    QA = dsfit.qa
    assert_equal(np.sum(QA > 0), 0)

    #3 fibers DSI2
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d=0.0015,
                             S0=100,
                             angles=[(0, 0), (90, 0), (90, 90)],
                             fractions=[33, 33, 33],
                             snr=None)
    ds = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere, squared=True)
    ds.relative_peak_threshold = 0.5
    dsfit = ds.fit(S, gfa_thr=0.05)
    QA = dsfit.qa

    #3 fibers DSI2 with a 3D volume
    data = np.zeros((3, 3, 3, len(S)))
    data[..., :] = S.copy()
    dsfit = ds.fit(data, gfa_thr=0.05)
    #1/0
    assert_array_almost_equal(np.sum(dsfit.peak_values > 0, axis=-1),
                              3 * np.ones((3, 3, 3)))
Esempio n. 11
0
def sim_data(bvals, bvecs, d=0.0015, S0=100, snr=None):

    descr = np.zeros(13).tolist()
    data = np.zeros((13, len(bvals)))

    descr[0] = ('isotropic', 0)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (90, 0), (90, 90)],
                             fractions=[0, 0, 0],
                             snr=snr)
    data[0] = S.copy()
    descr[1] = ('one fiber', 1)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(30, 0), (90, 0), (90, 90)],
                             fractions=[100, 0, 0],
                             snr=snr)
    data[1] = S.copy()
    descr[2] = ('two fibers', 2)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (90, 0), (90, 90)],
                             fractions=[50, 50, 0],
                             snr=snr)
    data[2] = S.copy()
    descr[3] = ('three fibers', 3)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (90, 0), (90, 90)],
                             fractions=[33, 33, 33],
                             snr=snr)
    data[3] = S.copy()
    descr[4] = ('three fibers iso', 3)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (90, 0), (90, 90)],
                             fractions=[23, 23, 23],
                             snr=snr)
    data[4] = S.copy()
    descr[5] = ('three fibers more iso', 3)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (90, 0), (90, 90)],
                             fractions=[13, 13, 13],
                             snr=snr)
    data[5] = S.copy()
    descr[6] = ('three fibers one at 60', 3)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (60, 0), (90, 90)],
                             fractions=[33, 33, 33],
                             snr=snr)
    data[6] = S.copy()
    descr[7] = ('three fibers one at 90,90 one smaller', 3)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (60, 0), (90, 90)],
                             fractions=[33, 33, 23],
                             snr=snr)
    data[7] = S.copy()
    descr[8] = ('three fibers one at 60, one at 90 and one smaller', 3)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (60, 0), (90, 90)],
                             fractions=[33, 33, 13],
                             snr=snr)
    data[8] = S.copy()
    descr[9] = ('two fibers at 60 one smaller', 2)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (60, 0), (90, 90)],
                             fractions=[50, 30, 0],
                             snr=snr)
    data[9] = S.copy()
    descr[10] = ('two fibers one at 30', 2)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (45, 0)],
                             fractions=[50, 50],
                             snr=snr)
    data[10] = S.copy()
    descr[11] = ('one fiber one at 30 but small iso', 1)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(30, 0), (60, 0), (90, 90)],
                             fractions=[60, 0, 0],
                             snr=snr)
    data[11] = S.copy()
    descr[12] = ('one fiber one at 30 but even smaller iso', 1)
    S, stics = SticksAndBall(bvals,
                             bvecs,
                             d,
                             S0,
                             angles=[(0, 0), (60, 0), (90, 90)],
                             fractions=[30, 0, 0],
                             snr=snr)
    data[12] = S.copy()

    return data, descr
Esempio n. 12
0
def sim_data(bvals,bvecs,d=0.0015,S0=100,snr=None):
    
    data=np.zeros((14,len(bvals)))
    #isotropic
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[0,0,0], snr=snr)
    data[0]=S.copy()
    #one fiber    
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(30, 0),(90,0),(90,90)], 
                          fractions=[100,0,0], snr=snr)
    data[1]=S.copy()
    #two fibers
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[50,50,0], snr=snr)
    data[2]=S.copy()
    #three fibers
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[33,33,33], snr=snr)
    data[3]=S.copy()
    #three fibers iso
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[23,23,23], snr=snr)
    data[4]=S.copy()
    #three fibers more iso
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[13,13,13], snr=snr)
    data[5]=S.copy()
    #three fibers one at 60
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,33], snr=snr)
    data[6]=S.copy()    
    
    #three fibers one at 90,90 one smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,23], snr=snr)
    data[7]=S.copy()
    
    #three fibers one at 90,90 one even smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,13], snr=snr)
    data[8]=S.copy()
    
    #two fibers one at 60 one even smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[50,30,0], snr=snr)
    data[9]=S.copy()
    
    #two fibers one at 30 one at 60 even smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(20,0),(90,90)], 
                          fractions=[50,50,0], snr=snr)
    data[10]=S.copy()
    
    #one fiber one at 30 but small
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(30, 0),(60,0),(90,90)], 
                          fractions=[60,0,0], snr=snr)
    data[11]=S.copy()
    
    #one fiber one at 30 but even smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[30,0,0], snr=snr)
    data[12]=S.copy()
    
    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
Esempio n. 13
0
def test_gqi():

    # load odf sphere
    vertices, faces = sphere_vf_from("symmetric724")
    edges = unique_edges(faces)
    half_vertices, half_edges, half_faces = reduce_antipodal(vertices, faces)

    # load bvals and gradients
    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))

    odf_sphere = (vertices, faces)
    ds = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values > 0).sum(), 3)

    # change thresholds
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 30
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values > 0).sum(), 2)

    # 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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 20
    dsfit = ds.fit(S)
    QA = dsfit.qa
    # 1/0
    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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 20
    dsfit = ds.fit(S)
    QA = dsfit.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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    dsfit = ds.fit(S)
    QA = dsfit.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 = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    QA = dsfit.qa
    assert_equal(np.sum(QA > 0), 0)

    # 3 fibers DSI2
    S, stics = SticksAndBall(
        bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (90, 0), (90, 90)], fractions=[33, 33, 33], snr=None
    )
    ds = GeneralizedQSamplingModel(bvals, bvecs, odf_sphere, squared=True)
    ds.relative_peak_threshold = 0.5
    dsfit = ds.fit(S, gfa_thr=0.05)
    QA = dsfit.qa

    # 3 fibers DSI2 with a 3D volume
    data = np.zeros((3, 3, 3, len(S)))
    data[..., :] = S.copy()
    dsfit = ds.fit(data, gfa_thr=0.05)
    # 1/0
    assert_array_almost_equal(np.sum(dsfit.peak_values > 0, axis=-1), 3 * np.ones((3, 3, 3)))
Esempio n. 14
0
def sim_data(bvals,bvecs,d=0.0015,S0=100,snr=None):

    descr=np.zeros(13).tolist()
    data=np.zeros((13,len(bvals)))

    descr[0]=('isotropic',0)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[0,0,0], snr=snr)
    data[0]=S.copy()
    descr[1]=('one fiber',1)    
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(30, 0),(90,0),(90,90)], 
                          fractions=[100,0,0], snr=snr)
    data[1]=S.copy()
    descr[2]=('two fibers',2)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[50,50,0], snr=snr)
    data[2]=S.copy()
    descr[3]=('three fibers',3)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[33,33,33], snr=snr)
    data[3]=S.copy()
    descr[4]=('three fibers iso',3)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[23,23,23], snr=snr)
    data[4]=S.copy()
    descr[5]=('three fibers more iso',3)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[13,13,13], snr=snr)
    data[5]=S.copy()
    descr[6]=('three fibers one at 60',3)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,33], snr=snr)
    data[6]=S.copy()        
    descr[7]=('three fibers one at 90,90 one smaller',3)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,23], snr=snr)
    data[7]=S.copy()    
    descr[8]=('three fibers one at 60, one at 90 and one smaller',3)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,13], snr=snr)
    data[8]=S.copy()    
    descr[9]=('two fibers at 60 one smaller',2)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[50,30,0], snr=snr)
    data[9]=S.copy()    
    descr[10]=('two fibers one at 30',2)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(45,0)], 
                          fractions=[50,50], snr=snr)
    data[10]=S.copy()    
    descr[11]=('one fiber one at 30 but small iso',1)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(30, 0),(60,0),(90,90)], 
                          fractions=[60,0,0], snr=snr)
    data[11]=S.copy()    
    descr[12]=('one fiber one at 30 but even smaller iso',1)
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[30,0,0], snr=snr)
    data[12]=S.copy()
        
    return data, descr
Esempio n. 15
0
def test_dsi():

    #load odf sphere
    vertices,faces = sphere_vf_from('symmetric724')
    edges = unique_edges(faces)
    half_vertices,half_edges,half_faces=reduce_antipodal(vertices,faces)

    #load bvals and gradients
    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)) 
    
    odf_sphere=(vertices,faces)
    ds=DiffusionSpectrumModel( bvals, bvecs, odf_sphere)    
    dsfit=ds.fit(S)
    assert_equal((dsfit.peak_values>0).sum(),3)

    #change thresholds
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 30
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values>0).sum(),2)

    #assert_almost_equal(np.sum(ds.pdf(S)-pdf0),0)
    #assert_almost_equal(np.sum(ds.odf(ds.pdf(S))-odf0),0)

    assert_almost_equal(dsfit.gfa,np.array([0.5749720469955439]))
    
    #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=DiffusionSpectrumModel(bvals,bvecs,odf_sphere)
    dsfit=ds.fit(S)
    QA=dsfit.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=DiffusionSpectrumModel(bvals,bvecs,odf_sphere)
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 20
    dsfit=ds.fit(S)
    QA=dsfit.qa
    assert_equal(np.sum(QA>0),2)
     
    #Give me 2 directions
    assert_equal(len(dsfit.get_directions()),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=DiffusionSpectrumModel(bvals,bvecs,odf_sphere)
    ds.relative_peak_threshold = 0.5
    dsfit=ds.fit(S,return_odf=True)
    QA=dsfit.qa
    assert_equal(np.sum(QA>0),3)

    #Give me 3 directions
    assert_equal(len(dsfit.get_directions()),3)

    #Recalculate the odf with a different sphere.

    vertices, faces = sphere_vf_from('symmetric724') 
    
    odf1=dsfit.odf()
    print len(odf1)
    
    odf2=dsfit.odf((vertices,faces))
    print len(odf2)

    assert_array_almost_equal(odf1,odf2)

    #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=DiffusionSpectrumModel(bvals,bvecs,odf_sphere)
    dsfit=ds.fit(S)
    QA=dsfit.qa
    assert_equal(np.sum(QA>0),0)
Esempio n. 16
0
def create_data(d=0.0015, S0=100, snr=None):

    data = np.zeros((16, len(bvals)))
    # isotropic
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (90, 0), (90, 90)], fractions=[0, 0, 0], snr=snr)
    data[0] = S.copy()
    # one fiber
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(30, 0), (90, 0), (90, 90)], fractions=[100, 0, 0], snr=snr)
    data[1] = S.copy()
    # two fibers
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (90, 0), (90, 90)], fractions=[50, 50, 0], snr=snr)
    data[2] = S.copy()
    # three fibers
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (90, 0), (90, 90)], fractions=[33, 33, 33], snr=snr)
    data[3] = S.copy()
    # three fibers iso
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (90, 0), (90, 90)], fractions=[23, 23, 23], snr=snr)
    data[4] = S.copy()
    # three fibers more iso
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (90, 0), (90, 90)], fractions=[13, 13, 13], snr=snr)
    data[5] = S.copy()
    # three fibers one at 60
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (60, 0), (90, 90)], fractions=[33, 33, 33], snr=snr)
    data[6] = S.copy()

    # three fibers one at 90,90 one smaller
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (60, 0), (90, 90)], fractions=[33, 33, 23], snr=snr)
    data[7] = S.copy()

    # three fibers one at 90,90 one even smaller
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (60, 0), (90, 90)], fractions=[33, 33, 13], snr=snr)
    data[8] = S.copy()

    # two fibers one at 0, second 30
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (30, 0), (90, 90)], fractions=[50, 50, 0], snr=snr)
    data[9] = S.copy()

    # two fibers one at 0, second 30
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (30, 0), (90, 90)], fractions=[50, 30, 0], snr=snr)
    data[10] = S.copy()

    # 1 fiber with 80% isotropic
    S, stics = SticksAndBall(bvals, bvecs, d, S0, angles=[(0, 0), (30, 0), (90, 90)], fractions=[20, 0, 0], snr=snr)
    data[11] = S.copy()

    # 1 fiber with tensor
    evals = np.array([1.4, 0.35, 0.35]) * 10 ** (-3)
    # evals=np.array([1.4,.2,.2])*10**(-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=snr)
    data[12] = S.copy()

    # 2 fibers with two tensors
    evals = np.array([1.4, 0.35, 0.35]) * 10 ** (-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=None)
    evals = np.array([0.35, 1.4, 0.35]) * 10 ** (-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=None) + S
    # std=2*S0/snr
    # S=S+np.random.randn(len(S))*std
    data[13] = S.copy()

    # 3 fibers with two tensors
    evals = np.array([1.4, 0.35, 0.35]) * 10 ** (-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=None)
    evals = np.array([0.35, 1.4, 0.35]) * 10 ** (-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=None) + S
    evals = np.array([0.35, 0.35, 1.4]) * 10 ** (-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=None) + S

    # std=2*S0/snr
    # S=S+np.random.randn(len(S))*std
    data[14] = S.copy()

    evals = np.array([0.35, 0.35, 0.35]) * 10 ** (-3)
    S = SingleTensor(bvals, bvecs, S0, evals=evals, evecs=np.eye(3), snr=snr)
    data[15] = S.copy()

    return data
Esempio n. 17
0
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]:
    """
Esempio n. 18
0
def create_data(bvals,bvecs,d=0.0015,S0=100,snr=None):
    
    #data=np.zeros((19,len(bvals)))
    data=np.zeros((2,len(bvals)))
    #0 isotropic
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[0,0,0], snr=snr)
    data[0]=S.copy()
    '''
    #1 one fiber    
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(30, 0),(90,0),(90,90)], 
                          fractions=[100,0,0], snr=snr)
    data[1]=S.copy()
    #2 two fibers
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[50,50,0], snr=snr)
    data[2]=S.copy()
    #3 three fibers
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[33,33,33], snr=snr)
    data[3]=S.copy()
    #4 three fibers iso
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[23,23,23], snr=snr)
    data[4]=S.copy()
    #5 three fibers more iso
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(90,0),(90,90)], 
                          fractions=[13,13,13], snr=snr)
    data[5]=S.copy()
    #6 three fibers one at 60
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,33], snr=snr)
    data[6]=S.copy()    
    
    #7 three fibers one at 90,90 one smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,23], snr=snr)
    data[7]=S.copy()
    
    #8 three fibers one at 90,90 one even smaller
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(60,0),(90,90)], 
                          fractions=[33,33,13], snr=snr)
    data[8]=S.copy()
    
    #9 two fibers one at 0, second 30 
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(45,0),(90,90)],
                          fractions=[50,50,0], snr=snr)
    data[9]=S.copy()
    
    #10 two fibers one at 0, second 30 
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(45,0),(90,90)], 
                          fractions=[50,30,0], snr=snr)
    data[10]=S.copy()
    
        
    #11 one fiber with 80% isotropic
    S,stics=SticksAndBall(bvals, bvecs, d, S0, 
                          angles=[(0, 0),(30,0),(90,90)], 
                          fractions=[20,0,0], snr=snr)    
    data[11]=S.copy()
    
    #12 one fiber with tensor
    evals=np.array([1.4,.35,.35])*10**(-3)
    #evals=np.array([1.4,.2,.2])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=snr)
    data[12]=S.copy()
    
    #13 three fibers with two tensors
    evals=np.array([1.4,.35,.35])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=None)
    evals=np.array([.35,1.4,.35])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=None)+S    
    #std=2*S0/snr
    #S=S+np.random.randn(len(S))*std    
    data[13]=S.copy()
    
    #14 three fibers with three tensors
    evals=np.array([1.4,.35,.35])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=None)
    evals=np.array([.35,1.4,.35])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=None)+S
    evals=np.array([.35,.35,1.4])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=None)+S
        
    #std=2*S0/snr
    #S=S+np.random.randn(len(S))*std    
    data[14]=S.copy()
    '''
    
    #15 isotropic with spherical tensor
    #evals=np.array([.15,.15,.15])*10**(-2)
    evals=np.array([d,d,d])
    #evals=np.array([.35,.35,.35])*10**(-3)
    #evals=np.array([.15,.15,.15])*10**(-2)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=np.eye(3),snr=snr)
    #data[15]=S.copy()  
    data[1]=S.copy()  
    
    '''
    #16 angles
    
    angles=[(0,0),(60,0),(90,90)]       
    R=[sphere2cart(1,np.deg2rad(pair[0]),np.deg2rad(pair[1])) for pair in angles]
    R=np.array(R)
    R=R.T
    #print R
    
    evals=np.array([1.4,.35,.35])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)
    evals=np.array([.35,1.4,.35])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)+S
    evals=np.array([.35,.35,1.4])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)+S
    
    data[16]=S.copy()
    
    #17 angles
    
    angles=[(0,0),(30,0),(90,90)]       
    R=[sphere2cart(1,np.deg2rad(pair[0]),np.deg2rad(pair[1])) for pair in angles]
    R=np.array(R)
    R=R.T
    #print R
    
    evals=np.array([1.4,.05,.05])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)
    evals=np.array([.05,1.4,.05])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)+S
    evals=np.array([.05,.05,1.4])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)+S
    
    data[17]=S.copy()
        
    #18 angles
    
    angles=[(0,0),(20,0),(90,90)]       
    R=[sphere2cart(1,np.deg2rad(pair[0]),np.deg2rad(pair[1])) for pair in angles]
    R=np.array(R)
    R=R.T
    #print R
    
    evals=np.array([1.4,.05,.05])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)
    evals=np.array([.05,1.4,.05])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)+S
    evals=np.array([.05,.05,1.4])*10**(-3)
    S=SingleTensor(bvals,bvecs,S0,evals=evals,evecs=R,snr=None)+S
    
    data[18]=S.copy()
    '''
    
    return data
Esempio n. 19
0
def test_dsi():

    #load odf sphere
    vertices, faces = sphere_vf_from('symmetric724')
    edges = unique_edges(faces)
    half_vertices, half_edges, half_faces = reduce_antipodal(vertices, faces)

    #load bvals and gradients
    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))

    odf_sphere = (vertices, faces)
    ds = DiffusionSpectrumModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values > 0).sum(), 3)

    #change thresholds
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 30
    dsfit = ds.fit(S)
    assert_equal((dsfit.peak_values > 0).sum(), 2)

    #assert_almost_equal(np.sum(ds.pdf(S)-pdf0),0)
    #assert_almost_equal(np.sum(ds.odf(ds.pdf(S))-odf0),0)

    assert_almost_equal(dsfit.gfa, np.array([0.5749720469955439]))

    #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 = DiffusionSpectrumModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    QA = dsfit.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 = DiffusionSpectrumModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    ds.angular_distance_threshold = 20
    dsfit = ds.fit(S)
    QA = dsfit.qa
    assert_equal(np.sum(QA > 0), 2)

    #Give me 2 directions
    assert_equal(len(dsfit.get_directions()), 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 = DiffusionSpectrumModel(bvals, bvecs, odf_sphere)
    ds.relative_peak_threshold = 0.5
    dsfit = ds.fit(S, return_odf=True)
    QA = dsfit.qa
    assert_equal(np.sum(QA > 0), 3)

    #Give me 3 directions
    assert_equal(len(dsfit.get_directions()), 3)

    #Recalculate the odf with a different sphere.

    vertices, faces = sphere_vf_from('symmetric724')

    odf1 = dsfit.odf()
    print len(odf1)

    odf2 = dsfit.odf((vertices, faces))
    print len(odf2)

    assert_array_almost_equal(odf1, odf2)

    #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 = DiffusionSpectrumModel(bvals, bvecs, odf_sphere)
    dsfit = ds.fit(S)
    QA = dsfit.qa
    assert_equal(np.sum(QA > 0), 0)
Esempio n. 20
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)