def test_mapmri_compare_fitted_pdf_with_multi_tensor(radial_order=6): gtab = get_gtab_taiwan_dsi() l1, l2, l3 = [0.0015, 0.0003, 0.0003] S, _ = generate_signal_crossing(gtab, l1, l2, l3) radius_max = 0.02 # 40 microns gridsize = 10 r_points = mapmri.create_rspace(gridsize, radius_max) # test MAPMRI fitting mapm = MapmriModel(gtab, radial_order=radial_order, laplacian_weighting=0.0001) mapfit = mapm.fit(S) # compare the mapmri pdf with the ground truth multi_tensor pdf mevals = np.array(([l1, l2, l3], [l1, l2, l3])) angl = [(0, 0), (60, 0)] pdf_mt = multi_tensor_pdf(r_points, mevals=mevals, angles=angl, fractions=[50, 50]) pdf_map = mapfit.pdf(r_points) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_map) ** 2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2)
def test_mapmri_compare_fitted_pdf_with_multi_tensor(radial_order=6): gtab = get_gtab_taiwan_dsi() l1, l2, l3 = [0.0015, 0.0003, 0.0003] S, _ = generate_signal_crossing(gtab, l1, l2, l3) radius_max = 0.02 # 40 microns gridsize = 10 r_points = mapmri.create_rspace(gridsize, radius_max) # test MAPMRI fitting mapm = MapmriModel(gtab, radial_order=radial_order, laplacian_weighting=0.0001) mapfit = mapm.fit(S) # compare the mapmri pdf with the ground truth multi_tensor pdf mevals = np.array(([l1, l2, l3], [l1, l2, l3])) angl = [(0, 0), (60, 0)] pdf_mt = multi_tensor_pdf(r_points, mevals=mevals, angles=angl, fractions=[50, 50]) pdf_map = mapfit.pdf(r_points) nmse_pdf = np.sqrt(np.sum((pdf_mt - pdf_map)**2)) / (pdf_mt.sum()) assert_almost_equal(nmse_pdf, 0.0, 2)
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_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_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, _ = multi_tensor(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) npt.assert_equal(n, n2) npt.assert_equal(l, l2) npt.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) npt.assert_equal(radial_order, radial_order2) npt.assert_equal(c, c2) npt.assert_raises(ValueError, shore_indices, 6, 100) npt.assert_raises(ValueError, shore_order, m, n, l) # 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()) npt.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 npt.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() npt.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()) npt.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() npt.assert_almost_equal(rtop_shore_signal, rtop_shore_pdf, 9) rtop_mt = multi_tensor_rtop([.5, .5], mevals=mevals) npt.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() npt.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)