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)
def test_shore_metrics(): gtab = get_gtab_taiwan_dsi() mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angl = [(0, 0), (60, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100.0, angles=angl, fractions=[50, 50], snr=None) # since we are testing without noise we can use higher order and lower lambdas, with respect to the default. radial_order = 6 lambd = 1e-8 # test mapmri_indices indices = mapmri_index_matrix(radial_order) n_c = indices.shape[0] F = radial_order / 2 n_gt = np.round(1 / 6.0 * (F + 1) * (F + 2) * (4 * F + 3)) assert_equal(n_c, n_gt) # test MAPMRI fitting mapm= MapmriModel(gtab, radial_order=radial_order, lambd=lambd) mapfit = mapm.fit(S) c_map=mapfit.mapmri_coeff R = mapfit.mapmri_R mu = mapfit.mapmri_mu S_reconst = mapfit.predict(gtab, 1.0) # test the signal reconstruction S = S / S[0] nmse_signal = np.sqrt(np.sum((S - S_reconst) ** 2)) / (S.sum()) assert_almost_equal(nmse_signal, 0.0, 3) # test if the analytical integral of the pdf is equal to one integral = 0 for i in range(indices.shape[0]): n1,n2,n3 = indices[i] integral += c_map[i] * int_func(n1) * int_func(n2) * int_func(n3) assert_almost_equal(integral, 1.0, 3) # compare the shore pdf with the ground truth multi_tensor pdf sphere = get_sphere('symmetric724') v = sphere.vertices radius = 10e-3 r_points = v * radius pdf_mt = multi_tensor_pdf(r_points, mevals=mevals, angles=angl, fractions= [50, 50]) pdf_map = mapmri_EAP(r_points, radial_order, c_map, mu, R) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_map) ** 2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2)
def test_multi_tensor(): sphere = get_sphere('symmetric724') vertices = sphere.vertices mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) e0 = np.array([np.sqrt(2) / 2., np.sqrt(2) / 2., 0]) e1 = np.array([0, np.sqrt(2) / 2., np.sqrt(2) / 2.]) mevecs = [all_tensor_evecs(e0), all_tensor_evecs(e1)] # odf = multi_tensor_odf(vertices, [0.5, 0.5], mevals, mevecs) # assert_(odf.shape == (len(vertices),)) # assert_(np.all(odf <= 1) & np.all(odf >= 0)) fimg, fbvals, fbvecs = get_data('small_101D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs) s1 = single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None) s2 = single_tensor(gtab, 100, mevals[1], mevecs[1], snr=None) Ssingle = 0.5 * s1 + 0.5 * s2 S, sticks = MultiTensor(gtab, mevals, S0=100, angles=[(90, 45), (45, 90)], fractions=[50, 50], snr=None) assert_array_almost_equal(S, Ssingle)
def generate_signal_crossing(gtab, lambda1, lambda2, lambda3, angle2=60): mevals = np.array(([lambda1, lambda2, lambda3], [lambda1, lambda2, lambda3])) angl = [(0, 0), (angle2, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100.0, angles=angl, fractions=[50, 50], snr=None) return S, sticks
def test_shore_metrics(): fetch_taiwan_ntu_dsi() img, gtab = read_taiwan_ntu_dsi() mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angl = [(0, 0), (60, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100, angles=angl, fractions=[50, 50], snr=None) S = S / S[0, None].astype(np.float) radial_order = 8 zeta = 800 lambdaN = 1e-12 lambdaL = 1e-12 asm = ShoreModel(gtab, radial_order=radial_order, zeta=zeta, lambdaN=lambdaN, lambdaL=lambdaL) asmfit = asm.fit(S) c_shore= asmfit.shore_coeff cmat = SHOREmatrix(radial_order, zeta, gtab) S_reconst = np.dot(cmat, c_shore) nmse_signal = np.sqrt(np.sum((S - S_reconst) ** 2)) / (S.sum()) assert_almost_equal(nmse_signal, 0.0, 4) mevecs2 = np.zeros((2, 3, 3)) angl = np.array(angl) for i in range(2): mevecs2[i] = all_tensor_evecs(sticks[i]).T sphere = get_sphere('symmetric724') v = sphere.vertices radius = 10e-3 pdf_shore = asmfit.pdf(v * radius) pdf_mt = multi_tensor_pdf(v * radius, [.5, .5], mevals=mevals, mevecs=mevecs2) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_shore) ** 2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2) rtop_shore_signal = asmfit.rtop_signal() rtop_shore_pdf = asmfit.rtop_pdf() assert_almost_equal(rtop_shore_signal, rtop_shore_pdf, 9) rtop_mt = multi_tensor_rtop([.5, .5], mevals=mevals) assert_equal(rtop_mt/rtop_shore_signal < 1.12 and rtop_mt/rtop_shore_signal > 0.9 , True) msd_mt = multi_tensor_msd([.5, .5], mevals=mevals) msd_shore = asmfit.msd() assert_equal(msd_mt/msd_shore < 1.05 and msd_mt/msd_shore > 0.95 , True)
def test_multivox_forecast(): gtab = get_3shell_gtab() mevals = np.array(([0.0017, 0.0003, 0.0003], [0.0017, 0.0003, 0.0003])) angl1 = [(0, 0), (60, 0)] angl2 = [(90, 0), (45, 90)] angl3 = [(0, 0), (90, 0)] S = np.zeros((3, 1, 1, len(gtab.bvals))) S[0, 0, 0], sticks = MultiTensor(gtab, mevals, S0=1.0, angles=angl1, fractions=[50, 50], snr=None) S[1, 0, 0], sticks = MultiTensor(gtab, mevals, S0=1.0, angles=angl2, fractions=[50, 50], snr=None) S[2, 0, 0], sticks = MultiTensor(gtab, mevals, S0=1.0, angles=angl3, fractions=[50, 50], snr=None) fm = ForecastModel(gtab, sh_order=8, dec_alg='CSD') f_fit = fm.fit(S) S_predict = f_fit.predict() assert_equal(S_predict.shape, S.shape) mse1 = np.sum((S_predict[0, 0, 0] - S[0, 0, 0])**2) / len(gtab.bvals) assert_almost_equal(mse1, 0.0, 3) mse2 = np.sum((S_predict[1, 0, 0] - S[1, 0, 0])**2) / len(gtab.bvals) assert_almost_equal(mse2, 0.0, 3) mse3 = np.sum((S_predict[2, 0, 0] - S[2, 0, 0])**2) / len(gtab.bvals) assert_almost_equal(mse3, 0.0, 3)
def setup(): data.gtab = get_3shell_gtab() data.mevals = np.array(([0.0017, 0.0003, 0.0003], [0.0017, 0.0003, 0.0003])) data.angl = [(0, 0), (60, 0)] data.S, data.sticks = MultiTensor( data.gtab, data.mevals, S0=100.0, angles=data.angl, fractions=[50, 50], snr=None) data.sh_order = 6 data.lambda_lb = 1e-8 data.lambda_csd = 1.0 sphere = get_sphere('repulsion100') data.sphere = sphere.vertices[0:int(sphere.vertices.shape[0]/2), :]
def setup(): data.gtab = get_gtab_taiwan_dsi() data.mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) data.angl = [(0, 0), (60, 0)] data.S, sticks = MultiTensor(data.gtab, data.mevals, S0=100.0, angles=data.angl, fractions=[50, 50], snr=None) data.radial_order = 6 data.zeta = 700 data.lambdaN = 1e-12 data.lambdaL = 1e-12
def test_forecast_indices(): # check anisotropic tensor fm = ForecastModel(data.gtab, sh_order=2, lambda_lb=data.lambda_lb, dec_alg='WLS') f_fit = fm.fit(data.S) d_par = f_fit.dpar d_perp = f_fit.dperp assert_almost_equal(d_par, data.mevals[0, 0], 5) assert_almost_equal(d_perp, data.mevals[0, 1], 5) gt_fa = np.sqrt(0.5 * (2 * (data.mevals[0, 0] - data.mevals[0, 1])**2) / (data.mevals[0, 0]**2 + 2 * data.mevals[0, 1]**2)) gt_md = (data.mevals[0, 0] + 2 * data.mevals[0, 1]) / 3.0 assert_almost_equal(f_fit.fractional_anisotropy(), gt_fa, 2) assert_almost_equal(f_fit.mean_diffusivity(), gt_md, 5) # check isotropic tensor mevals = np.array(([0.003, 0.003, 0.003], [0.003, 0.003, 0.003])) data.angl = [(0, 0), (60, 0)] S, sticks = MultiTensor(data.gtab, mevals, S0=100.0, angles=data.angl, fractions=[50, 50], snr=None) fm = ForecastModel(data.gtab, sh_order=data.sh_order, lambda_lb=data.lambda_lb, dec_alg='WLS') f_fit = fm.fit(S) d_par = f_fit.dpar d_perp = f_fit.dperp assert_almost_equal(d_par, 3e-03, 5) assert_almost_equal(d_perp, 3e-03, 5) assert_almost_equal(f_fit.fractional_anisotropy(), 0.0, 5) assert_almost_equal(f_fit.mean_diffusivity(), 3e-03, 10)
def test_mapmri_odf(): gtab = get_3shell_gtab() # load symmetric 724 sphere sphere = get_sphere('symmetric724') # load icosahedron sphere sphere2 = create_unit_sphere(5) evals = np.array(([0.0017, 0.0003, 0.0003], [0.0017, 0.0003, 0.0003])) data, golden_directions = MultiTensor(gtab, evals, S0=1.0, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=None) map_model = MapmriModel(gtab, radial_order=4) # symmetric724 mapfit = map_model.fit(data) odf = mapfit.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 odf = mapfit.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] mapfit = map_model.fit(data) odf = mapfit.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)
def test_shore_metrics(): gtab = get_gtab_taiwan_dsi() mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angl = [(0, 0), (60, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100.0, angles=angl, fractions=[50, 50], snr=None) # test shore_indices n = 7 l = 6 m = -4 radial_order, c = shore_order(n, l, m) n2, l2, m2 = shore_indices(radial_order, c) assert_equal(n, n2) assert_equal(l, l2) assert_equal(m, m2) radial_order = 6 c = 41 n, l, m = shore_indices(radial_order, c) radial_order2, c2 = shore_order(n, l, m) assert_equal(radial_order, radial_order2) assert_equal(c, c2) # since we are testing without noise we can use higher order and lower lambdas, with respect to the default. radial_order = 8 zeta = 700 lambdaN = 1e-12 lambdaL = 1e-12 asm = ShoreModel(gtab, radial_order=radial_order, zeta=zeta, lambdaN=lambdaN, lambdaL=lambdaL) asmfit = asm.fit(S) c_shore = asmfit.shore_coeff cmat = shore_matrix(radial_order, zeta, gtab) S_reconst = np.dot(cmat, c_shore) # test the signal reconstruction S = S / S[0] nmse_signal = np.sqrt(np.sum((S - S_reconst) ** 2)) / (S.sum()) assert_almost_equal(nmse_signal, 0.0, 4) # test if the analytical integral of the pdf is equal to one integral = 0 for n in range(int((radial_order)/2 +1)): integral += c_shore[n] * (np.pi**(-1.5) * zeta **(-1.5) * genlaguerre(n,0.5)(0)) ** 0.5 assert_almost_equal(integral, 1.0, 10) # test if the integral of the pdf calculated on a discrete grid is equal to one pdf_discrete = asmfit.pdf_grid(17, 40e-3) integral = pdf_discrete.sum() assert_almost_equal(integral, 1.0, 1) # compare the shore pdf with the ground truth multi_tensor pdf sphere = get_sphere('symmetric724') v = sphere.vertices radius = 10e-3 pdf_shore = asmfit.pdf(v * radius) pdf_mt = multi_tensor_pdf(v * radius, mevals=mevals, angles=angl, fractions= [50, 50]) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_shore) ** 2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2) # compare the shore rtop with the ground truth multi_tensor rtop rtop_shore_signal = asmfit.rtop_signal() rtop_shore_pdf = asmfit.rtop_pdf() assert_almost_equal(rtop_shore_signal, rtop_shore_pdf, 9) rtop_mt = multi_tensor_rtop([.5, .5], mevals=mevals) assert_equal(rtop_mt / rtop_shore_signal <1.10 and rtop_mt / rtop_shore_signal > 0.95, True) # compare the shore msd with the ground truth multi_tensor msd msd_mt = multi_tensor_msd([.5, .5], mevals=mevals) msd_shore = asmfit.msd() assert_equal(msd_mt / msd_shore < 1.05 and msd_mt / msd_shore > 0.95, True)
def test_shore_metrics(): gtab = get_gtab_taiwan_dsi() mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angl = [(0, 0), (60, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100.0, angles=angl, fractions=[50, 50], snr=None) # test shore_indices n = 7 l = 6 m = -4 radial_order, c = shore_order(n, l, m) n2, l2, m2 = shore_indices(radial_order, c) assert_equal(n, n2) assert_equal(l, l2) assert_equal(m, m2) radial_order = 6 c = 41 n, l, m = shore_indices(radial_order, c) radial_order2, c2 = shore_order(n, l, m) assert_equal(radial_order, radial_order2) assert_equal(c, c2) # since we are testing without noise we can use higher order and lower lambdas, with respect to the default. radial_order = 8 zeta = 700 lambdaN = 1e-12 lambdaL = 1e-12 asm = ShoreModel(gtab, radial_order=radial_order, zeta=zeta, lambdaN=lambdaN, lambdaL=lambdaL) asmfit = asm.fit(S) c_shore = asmfit.shore_coeff cmat = shore_matrix(radial_order, zeta, gtab) S_reconst = np.dot(cmat, c_shore) # test the signal reconstruction S = S / S[0] nmse_signal = np.sqrt(np.sum((S - S_reconst)**2)) / (S.sum()) assert_almost_equal(nmse_signal, 0.0, 4) # test if the analytical integral of the pdf is equal to one integral = 0 for n in range(int((radial_order) / 2 + 1)): integral += c_shore[n] * (np.pi**(-1.5) * zeta**(-1.5) * genlaguerre(n, 0.5)(0))**0.5 assert_almost_equal(integral, 1.0, 10) # test if the integral of the pdf calculated on a discrete grid is equal to one pdf_discrete = asmfit.pdf_grid(17, 40e-3) integral = pdf_discrete.sum() assert_almost_equal(integral, 1.0, 1) # compare the shore pdf with the ground truth multi_tensor pdf sphere = get_sphere('symmetric724') v = sphere.vertices radius = 10e-3 pdf_shore = asmfit.pdf(v * radius) pdf_mt = multi_tensor_pdf(v * radius, mevals=mevals, angles=angl, fractions=[50, 50]) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_shore)**2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2) # compare the shore rtop with the ground truth multi_tensor rtop rtop_shore_signal = asmfit.rtop_signal() rtop_shore_pdf = asmfit.rtop_pdf() assert_almost_equal(rtop_shore_signal, rtop_shore_pdf, 9) rtop_mt = multi_tensor_rtop([.5, .5], mevals=mevals) assert_equal( rtop_mt / rtop_shore_signal < 1.10 and rtop_mt / rtop_shore_signal > 0.95, True) # compare the shore msd with the ground truth multi_tensor msd msd_mt = multi_tensor_msd([.5, .5], mevals=mevals) msd_shore = asmfit.msd() assert_equal(msd_mt / msd_shore < 1.05 and msd_mt / msd_shore > 0.95, True)
def test_mapmri_metrics(): gtab = get_gtab_taiwan_dsi() mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angl = [(0, 0), (60, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100.0, angles=angl, fractions=[50, 50], snr=None) # since we are testing without noise we can use higher order and lower # lambdas, with respect to the default. radial_order = 6 lambd = 1e-8 # test mapmri_indices indices = mapmri_index_matrix(radial_order) n_c = indices.shape[0] F = radial_order / 2 n_gt = np.round(1 / 6.0 * (F + 1) * (F + 2) * (4 * F + 3)) assert_equal(n_c, n_gt) # test MAPMRI fitting mapm = MapmriModel(gtab, radial_order=radial_order, lambd=lambd) mapfit = mapm.fit(S) c_map = mapfit.mapmri_coeff R = mapfit.mapmri_R mu = mapfit.mapmri_mu S_reconst = mapfit.predict(gtab, 1.0) # test the signal reconstruction S = S / S[0] nmse_signal = np.sqrt(np.sum((S - S_reconst)**2)) / (S.sum()) assert_almost_equal(nmse_signal, 0.0, 3) # test if the analytical integral of the pdf is equal to one integral = 0 for i in range(indices.shape[0]): n1, n2, n3 = indices[i] integral += c_map[i] * int_func(n1) * int_func(n2) * int_func(n3) assert_almost_equal(integral, 1.0, 3) # compare the shore pdf with the ground truth multi_tensor pdf sphere = get_sphere('symmetric724') v = sphere.vertices radius = 10e-3 r_points = v * radius pdf_mt = multi_tensor_pdf(r_points, mevals=mevals, angles=angl, fractions=[50, 50]) pdf_map = mapmri_EAP(r_points, radial_order, c_map, mu, R) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_map)**2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2) # test MAPMRI metrics tau = 1 / (4 * np.pi**2) angl = [(0, 0), (0, 0)] S, sticks = MultiTensor(gtab, mevals, S0=100.0, angles=angl, fractions=[50, 50], snr=None) mapm = MapmriModel(gtab, radial_order=radial_order, lambd=lambd) mapfit = mapm.fit(S) # RTOP gt_rtop = 1.0 / np.sqrt( (4 * np.pi * tau)**3 * mevals[0, 0] * mevals[0, 1] * mevals[0, 2]) rtop = mapfit.rtop() assert_almost_equal(rtop, gt_rtop, 4) # RTAP gt_rtap = 1.0 / np.sqrt((4 * np.pi * tau)**2 * mevals[0, 1] * mevals[0, 2]) rtap = mapfit.rtap() assert_almost_equal(rtap, gt_rtap, 4) # RTPP gt_rtpp = 1.0 / np.sqrt((4 * np.pi * tau) * mevals[0, 0]) rtpp = mapfit.rtpp() assert_almost_equal(rtpp, gt_rtpp, 4)