def test_odfdeconv(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (90, 0)] S, sticks = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric362') odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) e1 = 15.0 e2 = 3.0 ratio = e2 / e1 csd = ConstrainedSDTModel(gtab, ratio, None) csd_fit = csd.fit(S) fodf = csd_fit.odf(sphere) directions, _, _ = peak_directions(odf_gt, sphere) directions2, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions, directions2) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) assert_equal(directions2.shape[0], 2) with warnings.catch_warnings(record=True) as w: ConstrainedSDTModel(gtab, ratio, sh_order=10) assert_equal(len(w) > 0, True) with warnings.catch_warnings(record=True) as w: ConstrainedSDTModel(gtab, ratio, sh_order=8) assert_equal(len(w) > 0, False) csd_fit = csd.fit(np.zeros_like(S)) fodf = csd_fit.odf(sphere) assert_array_equal(fodf, np.zeros_like(fodf)) odf_sh = np.zeros_like(fodf) odf_sh[1] = np.nan fodf, it = odf_deconv(odf_sh, csd.R, csd.B_reg) assert_array_equal(fodf, np.zeros_like(fodf))
def test_mapmri_odf(radial_order=6): gtab = get_gtab_taiwan_dsi() # load repulsion 724 sphere sphere = default_sphere # load icosahedron sphere l1, l2, l3 = [0.0015, 0.0003, 0.0003] data, golden_directions = generate_signal_crossing(gtab, l1, l2, l3, angle2=90) mapmod = MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=0.01) # repulsion724 sphere2 = create_unit_sphere(5) mapfit = mapmod.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] asmfit = mapmod.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) # for the isotropic implementation check if the odf spherical harmonics # actually represent the discrete sphere function. mapmod = MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=0.01, anisotropic_scaling=False) mapfit = mapmod.fit(data) odf = mapfit.odf(sphere) odf_sh = mapfit.odf_sh() odf_from_sh = sh_to_sf(odf_sh, sphere, radial_order, basis_type=None) assert_almost_equal(odf, odf_from_sh, 10)
def test_forecast_odf(): # check FORECAST fODF at different SH order fm = ForecastModel(data.gtab, sh_order=4, dec_alg='CSD', sphere=data.sphere) f_fit = fm.fit(data.S) sphere = default_sphere fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, data.sticks), 2, 1) fm = ForecastModel(data.gtab, sh_order=6, dec_alg='CSD', sphere=data.sphere) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, data.sticks), 2, 1) fm = ForecastModel(data.gtab, sh_order=8, dec_alg='CSD', sphere=data.sphere) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, data.sticks), 2, 1) # stronger regularization is required for high order SH fm = ForecastModel(data.gtab, sh_order=10, dec_alg='CSD', sphere=sphere.vertices) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, data.sticks), 2, 1) fm = ForecastModel(data.gtab, sh_order=12, dec_alg='CSD', sphere=sphere.vertices) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, data.sticks), 2, 1)
def test_r2_term_odf_sharp(): SNR = None S0 = 1 angle = 45 # 45 degrees is a very tight angle to disentangle _, fbvals, fbvecs = get_data('small_64D') # get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) sphere = get_sphere('symmetric724') gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (angle, 0)] S, sticks = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) odfs_sh = sf_to_sh(odf_gt, sphere, sh_order=8, basis_type=None) fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=0.1, r2_term=True) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions_gt, _, _ = peak_directions(odf_gt, sphere) directions, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions_gt, directions) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) # This should pass as well sdt_model = ConstrainedSDTModel(gtab, ratio=3 / 15., sh_order=8) sdt_fit = sdt_model.fit(S) fodf = sdt_fit.odf(sphere) directions_gt, _, _ = peak_directions(odf_gt, sphere) directions, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions_gt, directions) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2)
def test_mapmri_odf(radial_order=6): gtab = get_gtab_taiwan_dsi() # load symmetric 724 sphere sphere = get_sphere('symmetric724') # load icosahedron sphere l1, l2, l3 = [0.0015, 0.0003, 0.0003] data, golden_directions = generate_signal_crossing(gtab, l1, l2, l3, angle2=90) mapmod = MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=0.01) # symmetric724 sphere2 = create_unit_sphere(5) mapfit = mapmod.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] asmfit = mapmod.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) # for the isotropic implementation check if the odf spherical harmonics # actually represent the discrete sphere function. mapmod = MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=0.01, anisotropic_scaling=False) mapfit = mapmod.fit(data) odf = mapfit.odf(sphere) odf_sh = mapfit.odf_sh() odf_from_sh = sh_to_sf(odf_sh, sphere, radial_order, basis_type=None) assert_almost_equal(odf, odf_from_sh, 10)
def test_shore_odf(): gtab = get_isbi2013_2shell_gtab() # load repulsion 724 sphere sphere = default_sphere # load icosahedron sphere sphere2 = create_unit_sphere(5) data, golden_directions = sticks_and_ball(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) # repulsion724 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, legacy=True) npt.assert_almost_equal(odf, odf_from_sh, 10) expected_phi = shore_matrix(radial_order=6, zeta=700, gtab=gtab) npt.assert_array_almost_equal(np.dot(expected_phi, asmfit.shore_coeff), asmfit.fitted_signal()) directions, _, _ = peak_directions(odf, sphere, .35, 25) npt.assert_equal(len(directions), 2) npt.assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) # 5 subdivisions odf = asmfit.odf(sphere2) directions, _, _ = peak_directions(odf, sphere2, .35, 25) npt.assert_equal(len(directions), 2) npt.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: npt.assert_equal(len(directions), len(golden_directions)) if len(directions) > 3: npt.assert_equal(gfa(odf) < 0.1, True)
def test_difference_with_minmax(): # Show difference with and without minmax normalization # we create an odf here with 3 main peaks, 1 small sharp unwanted peak # (noise) and an isotropic compartment. mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.00005, 0.00005], [0.0015, 0.0015, 0.0015]]) angles = [(0, 0), (45, 0), (90, 0), (90, 90), (0, 0)] fractions = [20, 20, 10, 1, 100 - 20 - 20 - 10 - 1] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) # We will show that when the minmax normalization is used we can remove # the noisy peak using a lower threshold. odf_gt_minmax = (odf_gt - odf_gt.min()) / (odf_gt.max() - odf_gt.min()) _, values_1, _ = peak_directions(odf_gt, sphere, .30, 25.) assert_equal(len(values_1), 3) _, values_2, _ = peak_directions(odf_gt_minmax, sphere, .30, 25.) assert_equal(len(values_2), 3) # Setting the smallest value of the odf to zero is like running # peak_directions without the odf_min correction. odf_gt[odf_gt.argmin()] = 0. _, values_3, _ = peak_directions( odf_gt, sphere, .30, 25., ) assert_equal(len(values_3), 4) # we show here that to actually get that noisy peak out we need to # increase the peak threshold considerably directions, values_4, indices = peak_directions( odf_gt, sphere, .60, 25., ) assert_equal(len(values_4), 3) assert_almost_equal(values_1, values_4)
def test_mvoxel_gqi(): data, gtab = dsi_voxels() sphere = get_sphere('symmetric724') gq = GeneralizedQSamplingModel(gtab, 'standard') gqfit = gq.fit(data) all_odfs = gqfit.odf(sphere) # Check that the first and last voxels each have 2 peaks odf = all_odfs[0, 0, 0] directions, values, indices = peak_directions(odf, sphere, .35, 25) assert_equal(directions.shape[0], 2) odf = all_odfs[-1, -1, -1] directions, values, indices = peak_directions(odf, sphere, .35, 25) assert_equal(directions.shape[0], 2)
def test_forecast_odf(): # check FORECAST fODF at different SH order fm = ForecastModel(data.gtab, sh_order=4, dec_alg='CSD', sphere=data.sphere) f_fit = fm.fit(data.S) sphere = get_sphere('repulsion724') fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal( angular_similarity(directions, data.sticks), 2, 1) fm = ForecastModel(data.gtab, sh_order=6, dec_alg='CSD', sphere=data.sphere) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal( angular_similarity(directions, data.sticks), 2, 1) fm = ForecastModel(data.gtab, sh_order=8, dec_alg='CSD', sphere=data.sphere) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal( angular_similarity(directions, data.sticks), 2, 1) # stronger regularization is required for high order SH fm = ForecastModel(data.gtab, sh_order=10, dec_alg='CSD', sphere=sphere.vertices) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal( angular_similarity(directions, data.sticks), 2, 1) fm = ForecastModel(data.gtab, sh_order=12, dec_alg='CSD', sphere=sphere.vertices) f_fit = fm.fit(data.S) fodf = f_fit.odf(sphere) directions, _, _ = peak_directions(fodf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal( angular_similarity(directions, data.sticks), 2, 1)
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)
def test_csd_superres(): """ Check the quality of csdfit with high SH order. """ _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs) # img, gtab = read_stanford_hardi() evals = np.array([[1.5, .3, .3]]) * [[1.], [1.]] / 1000. S, sticks = multi_tensor(gtab, evals, snr=None, fractions=[55., 45.]) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings(action="always", message="Number of parameters required.*", category=UserWarning) model16 = ConstrainedSphericalDeconvModel(gtab, (evals[0], 3.), sh_order=16) assert_greater_equal(len(w), 1) npt.assert_(issubclass(w[-1].category, UserWarning)) fit16 = model16.fit(S) sphere = HemiSphere.from_sphere(get_sphere('symmetric724')) # print local_maxima(fit16.odf(default_sphere), default_sphere.edges) d, v, ind = peak_directions(fit16.odf(sphere), sphere, relative_peak_threshold=.2, min_separation_angle=0) # Check that there are two peaks assert_equal(len(d), 2) # Check that peaks line up with sticks cos_sim = abs((d * sticks).sum(1)) ** .5 assert_(all(cos_sim > .99))
def test_odf_sh_to_sharp(): SNR = None S0 = 1 _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, _ = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = default_sphere with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:, :, :] = odf_gt[:] with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=0.1) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
def _peak_directions(self, blob): """Gets directions using parameters provided at init. Blob can be any function defined on ``self.sphere``, ie an ODF, PMF, FOD. """ return peak_directions(blob, self.sphere, **self._pf_kwargs)[0]
def test_csd_superres(): """ Check the quality of csdfit with high SH order. """ _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) # img, gtab = read_stanford_hardi() evals = np.array([[1.5, .3, .3]]) * [[1.], [1.]] / 1000. S, sticks = multi_tensor(gtab, evals, snr=None, fractions=[55., 45.]) model16 = ConstrainedSphericalDeconvModel(gtab, (evals[0], 3.), sh_order=16) fit16 = model16.fit(S) # print local_maxima(fit16.odf(default_sphere), default_sphere.edges) d, v, ind = peak_directions(fit16.odf(default_sphere), default_sphere, relative_peak_threshold=.2, min_separation_angle=0) # Check that there are two peaks assert_equal(len(d), 2) # Check that peaks line up with sticks cos_sim = abs((d * sticks).sum(1))**.5 assert_(all(cos_sim > .99))
def test_single_voxel_fit(self): signal, gtab, expected = make_fake_signal() sphere = hemi_icosahedron.subdivide(4) model = self.model(gtab, sh_order=4, min_signal=1e-5, assume_normed=True) fit = model.fit(signal) odf = fit.odf(sphere) assert_equal(odf.shape, sphere.phi.shape) directions, _, _ = peak_directions(odf, sphere) # Check the same number of directions n = len(expected) assert_equal(len(directions), n) # Check directions are unit vectors cos_similarity = (directions * directions).sum(-1) assert_array_almost_equal(cos_similarity, np.ones(n)) # Check the directions == expected or -expected cos_similarity = (directions * expected).sum(-1) assert_array_almost_equal(abs(cos_similarity), np.ones(n)) # Test normalize data model = self.model(gtab, sh_order=4, min_signal=1e-5, assume_normed=False) fit = model.fit(signal * 5) odf_with_norm = fit.odf(sphere) assert_array_almost_equal(odf, odf_with_norm)
def test_csd_superres(): """ Check the quality of csdfit with high SH order. """ _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) # img, gtab = read_stanford_hardi() evals = np.array([[1.5, .3, .3]]) * [[1.], [1.]] / 1000. S, sticks = multi_tensor(gtab, evals, snr=None, fractions=[55., 45.]) model16 = ConstrainedSphericalDeconvModel(gtab, (evals[0], 3.), sh_order=16) fit16 = model16.fit(S) # print local_maxima(fit16.odf(default_sphere), default_sphere.edges) d, v, ind = peak_directions(fit16.odf(default_sphere), default_sphere, relative_peak_threshold=.2, min_separation_angle=0) # Check that there are two peaks assert_equal(len(d), 2) # Check that peaks line up with sticks cos_sim = abs((d * sticks).sum(1)) ** .5 assert_(all(cos_sim > .99))
def test_dsi(): # load repulsion 724 sphere sphere = default_sphere # load icosahedron sphere sphere2 = create_unit_sphere(5) btable = np.loadtxt(get_fnames('dsi515btable')) gtab = gradient_table(btable[:, 0], btable[:, 1:]) data, golden_directions = sticks_and_ball(gtab, d=0.0015, S0=100, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=None) ds = DiffusionSpectrumDeconvModel(gtab) # repulsion724 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)
def test_single_voxel_fit(self): signal, gtab, expected = make_fake_signal() sphere = hemi_icosahedron.subdivide(4) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) model = self.model(gtab, sh_order=4, min_signal=1e-5, assume_normed=True) fit = model.fit(signal) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) odf = fit.odf(sphere) assert_equal(odf.shape, sphere.phi.shape) directions, _, _ = peak_directions(odf, sphere) # Check the same number of directions n = len(expected) assert_equal(len(directions), n) # Check directions are unit vectors cos_similarity = (directions * directions).sum(-1) assert_array_almost_equal(cos_similarity, np.ones(n)) # Check the directions == expected or -expected cos_similarity = (directions * expected).sum(-1) assert_array_almost_equal(abs(cos_similarity), np.ones(n)) # Test normalize data with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) model = self.model(gtab, sh_order=4, min_signal=1e-5, assume_normed=False) fit = model.fit(signal * 5) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) odf_with_norm = fit.odf(sphere) assert_array_almost_equal(odf, odf_with_norm)
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)
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 = DiffusionSpectrumModel(gtab) # symmetric724 dsfit = ds.fit(data) odf = dsfit.odf(sphere) directions, _, _ = peak_directions(odf, sphere) 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) 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) 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, DiffusionSpectrumModel, gtab, qgrid_size=16)
def get_maximas(data, sphere, b_matrix, threshold, absolute_threshold, min_separation_angle=25): spherical_func = np.dot(data, b_matrix.T) spherical_func[np.nonzero(spherical_func < absolute_threshold)] = 0. return peak_directions(spherical_func, sphere, threshold, min_separation_angle)
def test_r2_term_odf_sharp(): SNR = None S0 = 1 angle = 45 # 45 degrees is a very tight angle to disentangle _, fbvals, fbvecs = get_data('small_64D') # get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) sphere = get_sphere('symmetric724') gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (angle, 0)] S, sticks = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) odfs_sh = sf_to_sh(odf_gt, sphere, sh_order=8, basis_type=None) fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=0.1, r2_term=True) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions_gt, _, _ = peak_directions(odf_gt, sphere) directions, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions_gt, directions) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) # This should pass as well sdt_model = ConstrainedSDTModel(gtab, ratio=3/15., sh_order=8) sdt_fit = sdt_model.fit(S) fodf = sdt_fit.odf(sphere) directions_gt, _, _ = peak_directions(odf_gt, sphere) directions, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions_gt, directions) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2)
def test_gqi(): # load symmetric 724 sphere sphere = get_sphere('symmetric724') # load icosahedron sphere sphere2 = create_unit_sphere(5) btable = np.loadtxt(get_fnames('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)
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_difference_with_minmax(): # Show difference with and without minmax normalization # we create an odf here with 3 main peaks, 1 small sharp unwanted peak # (noise) and an isotropic compartment. mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.00005, 0.00005], [0.0015, 0.0015, 0.0015]]) angles = [(0, 0), (45, 0), (90, 0), (90, 90), (0, 0)] fractions = [20, 20, 10, 1, 100 - 20 - 20 - 10 - 1] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) # We will show that when the minmax normalization is used we can remove # the noisy peak using a lower threshold. odf_gt_minmax = (odf_gt - odf_gt.min()) / (odf_gt.max() - odf_gt.min()) _, values_1, _ = peak_directions(odf_gt, sphere, .30, 25.) assert_equal(len(values_1), 3) _, values_2, _ = peak_directions(odf_gt_minmax, sphere, .30, 25.) assert_equal(len(values_2), 3) # Setting the smallest value of the odf to zero is like running # peak_directions without the odf_min correction. odf_gt[odf_gt.argmin()] = 0. _, values_3, _ = peak_directions(odf_gt, sphere, .30, 25.,) assert_equal(len(values_3), 4) # we show here that to actually get that noisy peak out we need to # increase the peak threshold considerably directions, values_4, indices = peak_directions(odf_gt, sphere, .60, 25.,) assert_equal(len(values_4), 3) assert_almost_equal(values_1, values_4)
def test_peak_directions(): model = SimpleOdfModel(_gtab) fit = model.fit(None) odf = fit.odf() argmax = odf.argmax() mx = odf.max() sphere = fit.model.sphere # Only one peak dir, val, ind = peak_directions(odf, sphere, .5, 45) dir_e = sphere.vertices[[argmax]] assert_array_equal(ind, [argmax]) assert_array_equal(val, odf[ind]) assert_array_equal(dir, dir_e) odf[0] = mx * .9 # Two peaks, relative_threshold dir, val, ind = peak_directions(odf, sphere, 1., 0) dir_e = sphere.vertices[[argmax]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax]) assert_array_equal(val, odf[ind]) dir, val, ind = peak_directions(odf, sphere, .8, 0) dir_e = sphere.vertices[[argmax, 0]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax, 0]) assert_array_equal(val, odf[ind]) # Two peaks, angle_sep dir, val, ind = peak_directions(odf, sphere, 0., 90) dir_e = sphere.vertices[[argmax]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax]) assert_array_equal(val, odf[ind]) dir, val, ind = peak_directions(odf, sphere, 0., 0) dir_e = sphere.vertices[[argmax, 0]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax, 0]) assert_array_equal(val, odf[ind])
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_odf_sh_to_sharp(): SNR = None S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, sticks = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric724') qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:, :, :] = odf_gt[:] odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=0.1) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
def test_degenerative_cases(): sphere = get_sphere('symmetric724') # completely isotropic and degencase odf = np.zeros(sphere.vertices.shape[0]) directions, values, indices = peak_directions(odf, sphere, .5, 25) print(directions, values, indices) assert_equal(len(values), 0) assert_equal(len(directions), 0) assert_equal(len(indices), 0) odf = np.zeros(sphere.vertices.shape[0]) odf[0] = 0.020 odf[1] = 0.018 directions, values, indices = peak_directions(odf, sphere, .5, 25) print(directions, values, indices) assert_equal(values[0], 0.02) odf = -np.ones(sphere.vertices.shape[0]) directions, values, indices = peak_directions(odf, sphere, .5, 25) print(directions, values, indices) assert_equal(len(values), 0) odf = np.zeros(sphere.vertices.shape[0]) odf[0] = 0.020 odf[1] = 0.018 odf[2] = -0.018 directions, values, indices = peak_directions(odf, sphere, .5, 25) assert_equal(values[0], 0.02) odf = np.ones(sphere.vertices.shape[0]) odf += 0.1 * np.random.rand(odf.shape[0]) directions, values, indices = peak_directions(odf, sphere, .5, 25) assert_(all(values > values[0] * .5)) assert_array_equal(values, odf[indices]) odf = np.ones(sphere.vertices.shape[0]) odf[1:] = np.finfo(np.float).eps * np.random.rand(odf.shape[0] - 1) directions, values, indices = peak_directions(odf, sphere, .5, 25) assert_equal(values[0], 1) assert_equal(len(values), 1)
def test_degenerative_cases(): sphere = get_sphere('symmetric724') # completely isotropic and degencase odf = np.zeros(sphere.vertices.shape[0]) directions, values, indices = peak_directions(odf, sphere, .5, 25) print(directions, values, indices) assert_equal(len(values), 0) assert_equal(len(directions), 0) assert_equal(len(indices), 0) odf = np.zeros(sphere.vertices.shape[0]) odf[0] = 0.020 odf[1] = 0.018 directions, values, indices = peak_directions(odf, sphere, .5, 25) print(directions, values, indices) assert_equal(values[0], 0.02) odf = - np.ones(sphere.vertices.shape[0]) directions, values, indices = peak_directions(odf, sphere, .5, 25) print(directions, values, indices) assert_equal(len(values), 0) odf = np.zeros(sphere.vertices.shape[0]) odf[0] = 0.020 odf[1] = 0.018 odf[2] = - 0.018 directions, values, indices = peak_directions(odf, sphere, .5, 25) assert_equal(values[0], 0.02) odf = np.ones(sphere.vertices.shape[0]) odf += 0.1 * np.random.rand(odf.shape[0]) directions, values, indices = peak_directions(odf, sphere, .5, 25) assert_(all(values > values[0] * .5)) assert_array_equal(values, odf[indices]) odf = np.ones(sphere.vertices.shape[0]) odf[1:] = np.finfo(np.float).eps * np.random.rand(odf.shape[0] - 1) directions, values, indices = peak_directions(odf, sphere, .5, 25) assert_equal(values[0], 1) assert_equal(len(values), 1)
def compute_peaks(odfs, sphere, relative_peak_threshold=.5, peak_normalize=1, min_separation_angle=45, max_peak_number=5): if odfs.shape[0] == sphere.vertices.shape[0]: odfs = np.array(odfs.reshape(odfs.shape[0], -1).T, order='C') num_peak_coeffs = max_peak_number * 3 peaks = np.zeros(odfs.shape[:-1] + (num_peak_coeffs, )) for index in ndindex(odfs.shape[:-1]): vox_peaks, values, _ = peak_directions(odfs[index], sphere, float(relative_peak_threshold), float(min_separation_angle)) if peak_normalize == 1: values /= values[0] vox_peaks = vox_peaks * values[:, None] vox_peaks = vox_peaks.ravel() m = vox_peaks.shape[0] if m > num_peak_coeffs: m = num_peak_coeffs peaks[index][:m] = vox_peaks[:m] return peaks
def peaks_from_sh_parallel(args): shm_coeff = args[0] B = args[1] sphere = args[2] relative_peak_threshold = args[3] absolute_threshold = args[4] min_separation_angle = args[5] npeaks = args[6] normalize_peaks = args[7] chunk_id = args[8] data_shape = shm_coeff.shape[0] peak_dirs = np.zeros((data_shape, npeaks, 3)) peak_values = np.zeros((data_shape, npeaks)) peak_indices = np.zeros((data_shape, npeaks), dtype='int') peak_indices.fill(-1) for idx in range(len(shm_coeff)): if shm_coeff[idx].any(): odf = np.dot(shm_coeff[idx], B) odf[odf < absolute_threshold] = 0. dirs, peaks, ind = peak_directions(odf, sphere, relative_peak_threshold, min_separation_angle) if peaks.shape[0] != 0: n = min(npeaks, peaks.shape[0]) peak_dirs[idx][:n] = dirs[:n] peak_indices[idx][:n] = ind[:n] peak_values[idx][:n] = peaks[:n] if normalize_peaks: peak_values[idx][:n] /= peaks[0] peak_dirs[idx] *= peak_values[idx][:, None] return chunk_id, peak_dirs, peak_values, peak_indices
def test_csdeconv(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (60, 0)] S, sticks = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric362') odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) response = (np.array([0.0015, 0.0003, 0.0003]), S0) csd = ConstrainedSphericalDeconvModel(gtab, response) csd_fit = csd.fit(S) assert_equal(csd_fit.shm_coeff[0] > 0, True) fodf = csd_fit.odf(sphere) directions, _, _ = peak_directions(odf_gt, sphere) directions2, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions, directions2) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) assert_equal(directions2.shape[0], 2) with warnings.catch_warnings(record=True) as w: ConstrainedSphericalDeconvModel(gtab, response, sh_order=10) assert_equal(len(w) > 0, True) with warnings.catch_warnings(record=True) as w: ConstrainedSphericalDeconvModel(gtab, response, sh_order=8) assert_equal(len(w) > 0, False) mevecs = [] for s in sticks: mevecs += [all_tensor_evecs(s).T] S2 = single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None) big_S = np.zeros((10, 10, 10, len(S2))) big_S[:] = S2 aresponse, aratio = auto_response(gtab, big_S, roi_center=(5, 5, 4), roi_radius=3, fa_thr=0.5) assert_array_almost_equal(aresponse[0], response[0]) assert_almost_equal(aresponse[1], 100) assert_almost_equal(aratio, response[0][1] / response[0][0]) aresponse2, aratio2 = auto_response(gtab, big_S, roi_radius=3, fa_thr=0.5) assert_array_almost_equal(aresponse[0], response[0]) _, _, nvoxels = auto_response(gtab, big_S, roi_center=(5, 5, 4), roi_radius=30, fa_thr=0.5, return_number_of_voxels=True) assert_equal(nvoxels, 1000) _, _, nvoxels = auto_response(gtab, big_S, roi_center=(5, 5, 4), roi_radius=30, fa_thr=1, return_number_of_voxels=True) assert_equal(nvoxels, 0)
def test_peak_directions_thorough(): # two equal fibers (creating a very sharp odf) mevals = np.array([[0.0025, 0.0003, 0.0003], [0.0025, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0)] fractions = [50, 50] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 2, 2) # two unequal fibers fractions = [75, 25] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 1, 2) directions, values, indices = peak_directions(odf_gt, sphere, .20, 25.) assert_almost_equal(angular_similarity(directions, sticks), 2, 2) # two equal fibers short angle (simulating very sharp ODF) mevals = np.array(([0.0045, 0.0003, 0.0003], [0.0045, 0.0003, 0.0003])) fractions = [50, 50] angles = [(0, 0), (20, 0)] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 1, 2) directions, values, indices = peak_directions(odf_gt, sphere, .5, 15.) assert_almost_equal(angular_similarity(directions, sticks), 2, 2) # 1 fiber mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) fractions = [50, 50] angles = [(15, 0), (15, 0)] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 15.) assert_almost_equal(angular_similarity(directions, sticks), 1, 2) AE = np.rad2deg(np.arccos(np.dot(directions[0], sticks[0]))) assert_(abs(AE) < 2. or abs(AE - 180) < 2.) # two equal fibers and one small noisy one mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0), (90, 0)] fractions = [45, 45, 10] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 2, 2) # two equal fibers and one faulty mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0), (60, 0)] fractions = [45, 45, 10] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 2, 2) # two equal fibers and one very very annoying one mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0), (60, 0)] fractions = [40, 40, 20] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 2, 2) # three peaks and one faulty mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0), (90, 0), (90, 45)] fractions = [35, 35, 20, 10] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_almost_equal(angular_similarity(directions, sticks), 3, 2) # four peaks mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0), (90, 0), (90, 45)] fractions = [25, 25, 25, 25] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .15, 5.) assert_almost_equal(angular_similarity(directions, sticks), 4, 2) # four difficult peaks mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (45, 0), (90, 0), (90, 45)] fractions = [30, 30, 20, 20] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, 0, 0) assert_almost_equal(angular_similarity(directions, sticks), 4, 1) odf_gt, sticks, hsphere = _create_mt_sim(mevals, angles, fractions, 100, None, half_sphere=True) directions, values, indices = peak_directions(odf_gt, hsphere, 0, 0) assert_equal(angular_similarity(directions, sticks) < 4, True) # four peaks and one them quite small fractions = [35, 35, 20, 10] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, 0, 0) assert_equal(angular_similarity(directions, sticks) < 4, True) odf_gt, sticks, hsphere = _create_mt_sim(mevals, angles, fractions, 100, None, half_sphere=True) directions, values, indices = peak_directions(odf_gt, hsphere, 0, 0) assert_equal(angular_similarity(directions, sticks) < 4, True) # isotropic case mevals = np.array([[0.0015, 0.0015, 0.0015]]) angles = [(0, 0)] fractions = [100.] odf_gt, sticks, sphere = _create_mt_sim(mevals, angles, fractions, 100, None) directions, values, indices = peak_directions(odf_gt, sphere, .5, 25.) assert_equal(len(values) > 10, True)
def test_csdeconv(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs, b0_threshold=0) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (60, 0)] S, sticks = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric362') odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) response = (np.array([0.0015, 0.0003, 0.0003]), S0) csd = ConstrainedSphericalDeconvModel(gtab, response) csd_fit = csd.fit(S) assert_equal(csd_fit.shm_coeff[0] > 0, True) fodf = csd_fit.odf(sphere) directions, _, _ = peak_directions(odf_gt, sphere) directions2, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions, directions2) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) assert_equal(directions2.shape[0], 2) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", category=UserWarning) _ = ConstrainedSphericalDeconvModel(gtab, response, sh_order=10) assert_greater(len([lw for lw in w if issubclass(lw.category, UserWarning)]), 0) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", category=UserWarning) ConstrainedSphericalDeconvModel(gtab, response, sh_order=8) assert_equal(len([lw for lw in w if issubclass(lw.category, UserWarning)]), 0) mevecs = [] for s in sticks: mevecs += [all_tensor_evecs(s).T] S2 = single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None) big_S = np.zeros((10, 10, 10, len(S2))) big_S[:] = S2 aresponse, aratio = auto_response_ssst(gtab, big_S, roi_center=(5, 5, 4), roi_radii=3, fa_thr=0.5) assert_array_almost_equal(aresponse[0], response[0]) assert_almost_equal(aresponse[1], 100) assert_almost_equal(aratio, response[0][1] / response[0][0]) auto_response_ssst(gtab, big_S, roi_radii=3, fa_thr=0.5) assert_array_almost_equal(aresponse[0], response[0])
def test_recursive_response_calibration(): """ Test the recursive response calibration method. """ SNR = 100 S0 = 1 _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) sphere = default_sphere gtab = gradient_table(bvals, bvecs) evals = np.array([0.0015, 0.0003, 0.0003]) evecs = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]).T mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (90, 0)] where_dwi = lazy_index(~gtab.b0s_mask) S_cross, _ = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) S_single = single_tensor(gtab, S0, evals, evecs, snr=SNR) data = np.concatenate((np.tile(S_cross, (8, 1)), np.tile(S_single, (2, 1))), axis=0) odf_gt_cross = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) odf_gt_single = single_tensor_odf(sphere.vertices, evals, evecs) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) response = recursive_response(gtab, data, mask=None, sh_order=8, peak_thr=0.01, init_fa=0.05, init_trace=0.0021, iter=8, convergence=0.001, parallel=False) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) csd = ConstrainedSphericalDeconvModel(gtab, response) csd_fit = csd.fit(data) assert_equal(np.all(csd_fit.shm_coeff[:, 0] >= 0), True) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) fodf = csd_fit.odf(sphere) directions_gt_single, _, _ = peak_directions(odf_gt_single, sphere) directions_gt_cross, _, _ = peak_directions(odf_gt_cross, sphere) directions_single, _, _ = peak_directions(fodf[8, :], sphere) directions_cross, _, _ = peak_directions(fodf[0, :], sphere) ang_sim = angular_similarity(directions_cross, directions_gt_cross) assert_equal(ang_sim > 1.9, True) assert_equal(directions_cross.shape[0], 2) assert_equal(directions_gt_cross.shape[0], 2) ang_sim = angular_similarity(directions_single, directions_gt_single) assert_equal(ang_sim > 0.9, True) assert_equal(directions_single.shape[0], 1) assert_equal(directions_gt_single.shape[0], 1) with warnings.catch_warnings(record=True) as w: sphere = Sphere(xyz=gtab.gradients[where_dwi]) npt.assert_equal(len(w), 1) npt.assert_(issubclass(w[0].category, UserWarning)) npt.assert_("Vertices are not on the unit sphere" in str(w[0].message)) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) sf = response.on_sphere(sphere) S = np.concatenate(([response.S0], sf)) tenmodel = TensorModel(gtab, min_signal=0.001) tenfit = tenmodel.fit(S) FA = fractional_anisotropy(tenfit.evals) FA_gt = fractional_anisotropy(evals) assert_almost_equal(FA, FA_gt, 1)
def test_odfdeconv(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (90, 0)] S, _ = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric362') odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) e1 = 15.0 e2 = 3.0 ratio = e2 / e1 csd = ConstrainedSDTModel(gtab, ratio, None) csd_fit = csd.fit(S) fodf = csd_fit.odf(sphere) directions, _, _ = peak_directions(odf_gt, sphere) directions2, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions, directions2) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) assert_equal(directions2.shape[0], 2) with warnings.catch_warnings(record=True) as w: ConstrainedSDTModel(gtab, ratio, sh_order=10) w_count = len(w) # A warning is expected from the ConstrainedSDTModel constructor # and additionnal warnings should be raised where legacy SH bases # are used assert_equal(w_count > 1, True) with warnings.catch_warnings(record=True) as w: ConstrainedSDTModel(gtab, ratio, sh_order=8) # Test that the warning from ConstrainedSDTModel # constructor is no more raised assert_equal(len(w) == w_count - 1, True) csd_fit = csd.fit(np.zeros_like(S)) fodf = csd_fit.odf(sphere) assert_array_equal(fodf, np.zeros_like(fodf)) odf_sh = np.zeros_like(fodf) odf_sh[1] = np.nan fodf, _ = odf_deconv(odf_sh, csd.R, csd.B_reg) assert_array_equal(fodf, np.zeros_like(fodf))
def __call__(self, fit): discrete_odf = fit.odf(self.sphere) directions, _, _ = peak_directions(discrete_odf, self.sphere, self.relative_peak_threshold, self.min_seperation_angle) return directions
def test_recursive_response_calibration(): """ Test the recursive response calibration method. """ SNR = 100 S0 = 1 sh_order = 8 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) sphere = get_sphere('symmetric724') gtab = gradient_table(bvals, bvecs) evals = np.array([0.0015, 0.0003, 0.0003]) evecs = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]).T mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) angles = [(0, 0), (90, 0)] where_dwi = lazy_index(~gtab.b0s_mask) S_cross, sticks_cross = multi_tensor(gtab, mevals, S0, angles=angles, fractions=[50, 50], snr=SNR) S_single = single_tensor(gtab, S0, evals, evecs, snr=SNR) data = np.concatenate((np.tile(S_cross, (8, 1)), np.tile(S_single, (2, 1))), axis=0) odf_gt_cross = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50]) odf_gt_single = single_tensor_odf(sphere.vertices, evals, evecs) response = recursive_response(gtab, data, mask=None, sh_order=8, peak_thr=0.01, init_fa=0.05, init_trace=0.0021, iter=8, convergence=0.001, parallel=False) csd = ConstrainedSphericalDeconvModel(gtab, response) csd_fit = csd.fit(data) assert_equal(np.all(csd_fit.shm_coeff[:, 0] >= 0), True) fodf = csd_fit.odf(sphere) directions_gt_single, _, _ = peak_directions(odf_gt_single, sphere) directions_gt_cross, _, _ = peak_directions(odf_gt_cross, sphere) directions_single, _, _ = peak_directions(fodf[8, :], sphere) directions_cross, _, _ = peak_directions(fodf[0, :], sphere) ang_sim = angular_similarity(directions_cross, directions_gt_cross) assert_equal(ang_sim > 1.9, True) assert_equal(directions_cross.shape[0], 2) assert_equal(directions_gt_cross.shape[0], 2) ang_sim = angular_similarity(directions_single, directions_gt_single) assert_equal(ang_sim > 0.9, True) assert_equal(directions_single.shape[0], 1) assert_equal(directions_gt_single.shape[0], 1) sphere = Sphere(xyz=gtab.gradients[where_dwi]) sf = response.on_sphere(sphere) S = np.concatenate(([response.S0], sf)) tenmodel = dti.TensorModel(gtab, min_signal=0.001) tenfit = tenmodel.fit(S) FA = fractional_anisotropy(tenfit.evals) FA_gt = fractional_anisotropy(evals) assert_almost_equal(FA, FA_gt, 1)