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)
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
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
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)))
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)
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
def test(): # img=nib.load('/home/eg309/Data/project01_dsi/connectome_0001/tp1/RAWDATA/OUT/mr000001.nii.gz') btable = np.loadtxt(get_data("dsi515btable")) # volume size sz = 16 # shifting origin = 8 # hanning width filter_width = 32.0 # number of signal sampling points n = 515 # odf radius radius = np.arange(2.1, 6, 0.2) # create q-table bv = btable[:, 0] bmin = np.sort(bv)[1] bv = np.sqrt(bv / bmin) qtable = np.vstack((bv, bv, bv)).T * btable[:, 1:] qtable = np.floor(qtable + 0.5) # copy bvals and bvecs bvals = btable[:, 0] bvecs = btable[:, 1:] # S=img.get_data()[38,50,20]#[96/2,96/2,20] S, stics = SticksAndBall( bvals, bvecs, d=0.0015, S0=100, angles=[(0, 0), (60, 0), (90, 90)], fractions=[0, 0, 0], snr=None ) S2 = S.copy() S2 = S2.reshape(1, len(S)) dn = DiffusionNabla(S2, bvals, bvecs, auto=False) pR = dn.equators odf = dn.odf(S) # Xs=dn.precompute_interp_coords() peaks, inds = peak_finding(odf.astype("f8"), dn.odf_faces.astype("uint16")) print peaks print peaks / peaks.min() # print dn.PK dn.fit() print dn.PK # """ ren = fvtk.ren() colors = fvtk.colors(odf, "jet") fvtk.add(ren, fvtk.point(dn.odf_vertices, colors, point_radius=0.05, theta=8, phi=8)) fvtk.show(ren) # """ stop # ds=DiffusionSpectrum(S2,bvals,bvecs) # tpr=ds.pdf(S) # todf=ds.odf(tpr) """ #show projected signal Bvecs=np.concatenate([bvecs[1:],-bvecs[1:]]) X0=np.dot(np.diag(np.concatenate([S[1:],S[1:]])),Bvecs) ren=fvtk.ren() fvtk.add(ren,fvtk.point(X0,fvtk.yellow,1,2,16,16)) fvtk.show(ren) """ # qtable=5*matrix[:,1:] # calculate radius for the hanning filter r = np.sqrt(qtable[:, 0] ** 2 + qtable[:, 1] ** 2 + qtable[:, 2] ** 2) # setting hanning filter width and hanning hanning = 0.5 * np.cos(2 * np.pi * r / filter_width) # center and index in q space volume q = qtable + origin q = q.astype("i8") # apply the hanning filter values = S * hanning """ #plot q-table ren=fvtk.ren() colors=fvtk.colors(values,'jet') fvtk.add(ren,fvtk.point(q,colors,1,0.1,6,6)) fvtk.show(ren) """ # create the signal volume Sq = np.zeros((sz, sz, sz)) for i in range(n): Sq[q[i][0], q[i][1], q[i][2]] += values[i] # apply fourier transform Pr = fftshift(np.abs(np.real(fftn(fftshift(Sq), (sz, sz, sz))))) # """ ren = fvtk.ren() vol = fvtk.volume(Pr) fvtk.add(ren, vol) fvtk.show(ren) # """ """ from enthought.mayavi import mlab mlab.pipeline.volume(mlab.pipeline.scalar_field(Sq)) mlab.show() """ # vertices, edges, faces = create_unit_sphere(5) vertices, faces = sphere_vf_from("symmetric362") odf = np.zeros(len(vertices)) for m in range(len(vertices)): xi = origin + radius * vertices[m, 0] yi = origin + radius * vertices[m, 1] zi = origin + radius * vertices[m, 2] PrI = map_coordinates(Pr, np.vstack((xi, yi, zi)), order=1) for i in range(len(radius)): odf[m] = odf[m] + PrI[i] * radius[i] ** 2 """ ren=fvtk.ren() colors=fvtk.colors(odf,'jet') fvtk.add(ren,fvtk.point(vertices,colors,point_radius=.05,theta=8,phi=8)) fvtk.show(ren) """ """ #Pr[Pr<500]=0 ren=fvtk.ren() #ren.SetBackground(1,1,1) fvtk.add(ren,fvtk.volume(Pr)) fvtk.show(ren) """ peaks, inds = peak_finding(odf.astype("f8"), faces.astype("uint16")) Eq = np.zeros((sz, sz, sz)) for i in range(n): Eq[q[i][0], q[i][1], q[i][2]] += S[i] / S[0] LEq = laplace(Eq) # Pr[Pr<500]=0 ren = fvtk.ren() # ren.SetBackground(1,1,1) fvtk.add(ren, fvtk.volume(Eq)) fvtk.show(ren) phis = np.linspace(0, 2 * np.pi, 100) planars = [] for phi in phis: planars.append(sphere2cart(1, np.pi / 2, phi)) planars = np.array(planars) planarsR = [] for v in vertices: R = vec2vec_rotmat(np.array([0, 0, 1]), v) planarsR.append(np.dot(R, planars.T).T) """ ren=fvtk.ren() fvtk.add(ren,fvtk.point(planarsR[0],fvtk.green,1,0.1,8,8)) fvtk.add(ren,fvtk.point(2*planarsR[1],fvtk.red,1,0.1,8,8)) fvtk.show(ren) """ azimsums = [] for disk in planarsR: diskshift = 4 * disk + origin # Sq0=map_coordinates(Sq,diskshift.T,order=1) # azimsums.append(np.sum(Sq0)) # Eq0=map_coordinates(Eq,diskshift.T,order=1) # azimsums.append(np.sum(Eq0)) LEq0 = map_coordinates(LEq, diskshift.T, order=1) azimsums.append(np.sum(LEq0)) azimsums = np.array(azimsums) # """ ren = fvtk.ren() colors = fvtk.colors(azimsums, "jet") fvtk.add(ren, fvtk.point(vertices, colors, point_radius=0.05, theta=8, phi=8)) fvtk.show(ren) # """ # for p in planarsR[0]: """
def 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
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)
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
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
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
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)))
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)