def test_dni_eit(): btable = np.loadtxt(get_data('dsi515btable')) bvals = btable[:, 0] bvecs = btable[:, 1:] data, descr = sim_data(bvals, bvecs) #load odf sphere vertices, faces = sphere_vf_from('symmetric724') edges = unique_edges(faces) half_vertices, half_edges, half_faces = reduce_antipodal(vertices, faces) #create the sphere odf_sphere = (vertices, faces) dn = DiffusionNablaModel(bvals, bvecs, odf_sphere) dn.relative_peak_threshold = 0.5 dn.angular_distance_threshold = 20 dnfit = dn.fit(data) print('DiffusionNablaModel') for i, d in enumerate(data): print descr[i], np.sum(dnfit.peak_values[i] > 0) ei = EquatorialInversionModel(bvals, bvecs, odf_sphere) ei.relative_peak_threshold = 0.3 ei.angular_distance_threshold = 15 ei.set_operator('laplacian') eifit = ei.fit(data, return_odf=True) print('EquatorialInversionModel') for i, d in enumerate(data): print descr[i], np.sum(eifit.peak_values[i] > 0) assert_equal(descr[i][1], np.sum(eifit.peak_values[i] > 0)) from dipy.viz import show_odfs show_odfs(eifit.odf[None, None, :, :], (vertices, faces), scale=2)
def test_peak_finding(): vertices, faces=get_sphere('symmetric724') odf=np.zeros(len(vertices)) odf = np.abs(vertices.sum(-1)) odf[1] = 10. odf[505] = 505. odf[143] = 143. peaks, inds=peak_finding(odf.astype('f8'), faces.astype('uint16')) print peaks, inds edges = unique_edges(faces) peaks, inds = local_maxima(odf, edges) print peaks, inds vertices_half, edges_half, faces_half = reduce_antipodal(vertices, faces) n = len(vertices_half) peaks, inds = local_maxima(odf[:n], edges_half) print peaks, inds mevals=np.array(([0.0015,0.0003,0.0003], [0.0015,0.0003,0.0003])) e0=np.array([1,0,0.]) e1=np.array([0.,1,0]) mevecs=[all_tensor_evecs(e0),all_tensor_evecs(e1)] odf = multi_tensor_odf(vertices, [0.5,0.5], mevals, mevecs) peaks, inds=peak_finding(odf, faces) print peaks, inds peaks2, inds2 = local_maxima(odf[:n], edges_half) print peaks2, inds2 assert_equal(len(peaks), 2) assert_equal(len(peaks2), 2)
def test_dni_eit(): btable=np.loadtxt(get_data('dsi515btable')) bvals=btable[:,0] bvecs=btable[:,1:] data,descr=sim_data(bvals,bvecs) #load odf sphere vertices,faces = sphere_vf_from('symmetric724') edges = unique_edges(faces) half_vertices,half_edges,half_faces=reduce_antipodal(vertices,faces) #create the sphere odf_sphere=(vertices,faces) dn=DiffusionNablaModel(bvals,bvecs,odf_sphere) dn.relative_peak_threshold = 0.5 dn.angular_distance_threshold = 20 dnfit=dn.fit(data) print('DiffusionNablaModel') for i,d in enumerate(data): print descr[i], np.sum(dnfit.peak_values[i]>0) ei=EquatorialInversionModel(bvals,bvecs,odf_sphere) ei.relative_peak_threshold = 0.3 ei.angular_distance_threshold = 15 ei.set_operator('laplacian') eifit = ei.fit(data,return_odf=True) print('EquatorialInversionModel') for i,d in enumerate(data): print descr[i], np.sum(eifit.peak_values[i]>0) assert_equal(descr[i][1], np.sum(eifit.peak_values[i]>0)) from dipy.viz import show_odfs show_odfs(eifit.odf[None,None,:,:], (vertices,faces), scale=2)
def set_odf_vertices(self, vertices, edges=None, faces=None): """Sets the vertices used to evaluate the odf or get odf peaks Parameters ---------- vertices : ndarray (n, 3), dtype=float The x, y, z coordinates of n points on a unit sphere. edges : ndarray (m, 2), dtype=int16, optional A list neighboring vertices, for example if (1, 2) is in edges then vertex 1 and 2 are treated as neighbors. """ on_unit_sphere = np.allclose((vertices * vertices).sum(-1), 1) if vertices.ndim != 2 or vertices.shape[1] != 3 or not on_unit_sphere: raise ValueError("vertices should be an (n, 3) array of points on " "the unit sphere") if (edges is not None) and (edges.ndim != 2 or edges.shape[1] != 2): raise ValueError("Edges should be an (m, 2) array with a list of " "neighboring vertices") self._odf_vertices = vertices if edges is None and faces is not None: self._odf_edges = unique_edges(faces) else: self._odf_edges = edges self._odf_faces = faces self._distance_matrix = abs(vertices.dot(vertices.T))
def set_odf_vertices(self, vertices, edges=None, faces=None): """Sets the vertices used to evaluate the odf or get odf peaks Parameters ---------- vertices : ndarray (n, 3), dtype=float The x, y, z coordinates of n points on a unit sphere. edges : ndarray (m, 2), dtype=int16, optional A list neighboring vertices, for example if (1, 2) is in edges then vertex 1 and 2 are treated as neighbors. """ on_unit_sphere = np.allclose((vertices*vertices).sum(-1), 1) if vertices.ndim != 2 or vertices.shape[1] != 3 or not on_unit_sphere: raise ValueError("vertices should be an (n, 3) array of points on " "the unit sphere") if (edges is not None) and (edges.ndim != 2 or edges.shape[1] != 2): raise ValueError("Edges should be an (m, 2) array with a list of " "neighboring vertices") self._odf_vertices = vertices if edges is None and faces is not None: self._odf_edges = unique_edges(faces) else: self._odf_edges = edges self._odf_faces = faces self._distance_matrix = abs(vertices.dot(vertices.T))
def test_local_maxima(): vertices, faces=get_sphere('symmetric724') edges = unique_edges(faces) odf = abs(vertices.sum(-1)) odf[1] = 10. odf[143] = 143. odf[505] = 505 peak_values, peak_index = local_maxima(odf, edges) assert_array_equal(peak_values, [505, 143, 10]) assert_array_equal(peak_index, [505, 143, 1]) vertices_half, edges_half, faces_half = reduce_antipodal(vertices, faces) odf = abs(vertices_half.sum(-1)) odf[1] = 10. odf[143] = 143. peak_value, peak_index = local_maxima(odf, edges_half) assert_array_equal(peak_value, [143, 10]) assert_array_equal(peak_index, [143, 1]) odf[20] = np.nan assert_raises(ValueError, local_maxima, odf, edges_half)
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 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)