def test_bdg_residual(): """This tests the bootstrapping residual. """ hsph_updated = HemiSphere.from_sphere(unit_icosahedron).subdivide(2) vertices = hsph_updated.vertices bvecs = vertices bvals = np.ones(len(vertices)) * 1000 bvecs = np.insert(bvecs, 0, np.array([0, 0, 0]), axis=0) bvals = np.insert(bvals, 0, 0) gtab = gradient_table(bvals, bvecs) r, theta, phi = cart2sphere(*vertices.T) with warnings.catch_warnings(): warnings.filterwarnings( "ignore", message=shm.descoteaux07_legacy_msg, category=PendingDeprecationWarning) B, m, n = shm.real_sh_descoteaux(6, theta, phi) shm_coeff = np.random.random(B.shape[1]) # sphere_func is sampled of the spherical function for each point of # the sphere sphere_func = np.dot(shm_coeff, B.T) voxel = np.concatenate((np.zeros(1), sphere_func)) data = np.tile(voxel, (3, 3, 3, 1)) with warnings.catch_warnings(): warnings.filterwarnings( "ignore", message=shm.descoteaux07_legacy_msg, category=PendingDeprecationWarning) csd_model = ConstrainedSphericalDeconvModel(gtab, response, sh_order=6) boot_pmf_gen = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=6) # Two boot samples should be the same odf1 = boot_pmf_gen.get_pmf(np.array([1.5, 1.5, 1.5])) odf2 = boot_pmf_gen.get_pmf(np.array([1.5, 1.5, 1.5])) npt.assert_array_almost_equal(odf1, odf2) # A boot sample with less sh coeffs should have residuals, thus the two # should be different with warnings.catch_warnings(): warnings.filterwarnings( "ignore", message=shm.descoteaux07_legacy_msg, category=PendingDeprecationWarning) boot_pmf_gen2 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=4) odf1 = boot_pmf_gen2.get_pmf(np.array([1.5, 1.5, 1.5])) odf2 = boot_pmf_gen2.get_pmf(np.array([1.5, 1.5, 1.5])) npt.assert_(np.any(odf1 != odf2)) # test with a gtab with two shells and assert you get an error bvals[-1] = 2000 gtab = gradient_table(bvals, bvecs) with warnings.catch_warnings(): warnings.filterwarnings( "ignore", message=shm.descoteaux07_legacy_msg, category=PendingDeprecationWarning) csd_model = ConstrainedSphericalDeconvModel(gtab, response, sh_order=6) npt.assert_raises(ValueError, BootPmfGen, data, csd_model, hsph_updated, 6)
def test_bdg_residual(): """This tests the bootstrapping residual. """ hsph_updated = HemiSphere.from_sphere(unit_icosahedron).subdivide(2) vertices = hsph_updated.vertices bvecs = vertices bvals = np.ones(len(vertices)) * 1000 bvecs = np.insert(bvecs, 0, np.array([0, 0, 0]), axis=0) bvals = np.insert(bvals, 0, 0) gtab = gradient_table(bvals, bvecs) r, theta, phi = cart2sphere(*vertices.T) B, m, n = shm.real_sym_sh_basis(6, theta, phi) shm_coeff = np.random.random(B.shape[1]) # sphere_func is sampled of the spherical function for each point of # the sphere sphere_func = np.dot(shm_coeff, B.T) voxel = np.concatenate((np.zeros(1), sphere_func)) data = np.tile(voxel, (3, 3, 3, 1)) csd_model = ConstrainedSphericalDeconvModel(gtab, None, sh_order=6) boot_pmf_gen = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=6) # Two boot samples should be the same odf1 = boot_pmf_gen.get_pmf(np.array([1.5, 1.5, 1.5])) odf2 = boot_pmf_gen.get_pmf(np.array([1.5, 1.5, 1.5])) npt.assert_array_almost_equal(odf1, odf2) # A boot sample with less sh coeffs should have residuals, thus the two # should be different boot_pmf_gen2 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=4) odf1 = boot_pmf_gen2.get_pmf(np.array([1.5, 1.5, 1.5])) odf2 = boot_pmf_gen2.get_pmf(np.array([1.5, 1.5, 1.5])) npt.assert_(np.any(odf1 != odf2)) # test with a gtab with two shells and assert you get an error bvals[-1] = 2000 gtab = gradient_table(bvals, bvecs) csd_model = ConstrainedSphericalDeconvModel(gtab, None, sh_order=6) npt.assert_raises(ValueError, BootPmfGen, data, csd_model, hsph_updated, 6)
def test_boot_pmf(): # This tests the local model used for the bootstrapping. hsph_updated = HemiSphere.from_sphere(unit_octahedron) vertices = hsph_updated.vertices bvecs = vertices bvals = np.ones(len(vertices)) * 1000 bvecs = np.insert(bvecs, 0, np.array([0, 0, 0]), axis=0) bvals = np.insert(bvals, 0, 0) gtab = gradient_table(bvals, bvecs) voxel = single_tensor(gtab) data = np.tile(voxel, (3, 3, 3, 1)) point = np.array([1., 1., 1.]) tensor_model = TensorModel(gtab) boot_pmf_gen = BootPmfGen(data, model=tensor_model, sphere=hsph_updated) no_boot_pmf = boot_pmf_gen.get_pmf_no_boot(point) model_pmf = tensor_model.fit(voxel).odf(hsph_updated) npt.assert_equal(len(hsph_updated.vertices), no_boot_pmf.shape[0]) npt.assert_array_almost_equal(no_boot_pmf, model_pmf) # test model spherical harmonic order different than bootstrap order with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", category=UserWarning) csd_model = ConstrainedSphericalDeconvModel(gtab, response, sh_order=6) # Tests that the first catched warning comes from # the CSD model constructor npt.assert_(issubclass(w[0].category, UserWarning)) npt.assert_("Number of parameters required " in str(w[0].message)) # Tests that additionnal warnings are raised for outdated SH basis npt.assert_(len(w) > 1) boot_pmf_gen_sh4 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=4) pmf_sh4 = boot_pmf_gen_sh4.get_pmf(point) npt.assert_equal(len(hsph_updated.vertices), pmf_sh4.shape[0]) npt.assert_(np.sum(pmf_sh4.shape) > 0) boot_pmf_gen_sh8 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=8) pmf_sh8 = boot_pmf_gen_sh8.get_pmf(point) npt.assert_equal(len(hsph_updated.vertices), pmf_sh8.shape[0]) npt.assert_(np.sum(pmf_sh8.shape) > 0)
def test_boot_pmf(): """This tests the local model used for the bootstrapping. """ hsph_updated = HemiSphere.from_sphere(unit_octahedron) vertices = hsph_updated.vertices bvecs = vertices bvals = np.ones(len(vertices)) * 1000 bvecs = np.insert(bvecs, 0, np.array([0, 0, 0]), axis=0) bvals = np.insert(bvals, 0, 0) gtab = gradient_table(bvals, bvecs) voxel = single_tensor(gtab) data = np.tile(voxel, (3, 3, 3, 1)) point = np.array([1., 1., 1.]) tensor_model = TensorModel(gtab) boot_pmf_gen = BootPmfGen(data, model=tensor_model, sphere=hsph_updated) no_boot_pmf = boot_pmf_gen.get_pmf_no_boot(point) model_pmf = tensor_model.fit(voxel).odf(hsph_updated) npt.assert_equal(len(hsph_updated.vertices), no_boot_pmf.shape[0]) npt.assert_array_almost_equal(no_boot_pmf, model_pmf) # test model spherical harmonic order different than bootstrap order with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", category=UserWarning) csd_model = ConstrainedSphericalDeconvModel(gtab, None, sh_order=6) assert_greater( len([lw for lw in w if issubclass(lw.category, UserWarning)]), 0) boot_pmf_gen_sh4 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=4) pmf_sh4 = boot_pmf_gen_sh4.get_pmf(point) npt.assert_equal(len(hsph_updated.vertices), pmf_sh4.shape[0]) npt.assert_(np.sum(pmf_sh4.shape) > 0) boot_pmf_gen_sh8 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=8) pmf_sh8 = boot_pmf_gen_sh8.get_pmf(point) npt.assert_equal(len(hsph_updated.vertices), pmf_sh8.shape[0]) npt.assert_(np.sum(pmf_sh8.shape) > 0)
def test_boot_pmf(): """This tests the local model used for the bootstrapping. """ hsph_updated = HemiSphere.from_sphere(unit_octahedron) vertices = hsph_updated.vertices bvecs = vertices bvals = np.ones(len(vertices)) * 1000 bvecs = np.insert(bvecs, 0, np.array([0, 0, 0]), axis=0) bvals = np.insert(bvals, 0, 0) gtab = gradient_table(bvals, bvecs) voxel = single_tensor(gtab) data = np.tile(voxel, (3, 3, 3, 1)) point = np.array([1., 1., 1.]) tensor_model = TensorModel(gtab) boot_pmf_gen = BootPmfGen(data, model=tensor_model, sphere=hsph_updated) no_boot_pmf = boot_pmf_gen.get_pmf_no_boot(point) model_pmf = tensor_model.fit(voxel).odf(hsph_updated) npt.assert_equal(len(hsph_updated.vertices), no_boot_pmf.shape[0]) npt.assert_array_almost_equal(no_boot_pmf, model_pmf) # test model sherical harminic order different than bootstrap order with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always", category=UserWarning) csd_model = ConstrainedSphericalDeconvModel(gtab, None, sh_order=6) assert_greater(len([lw for lw in w if issubclass(lw.category, UserWarning)]), 0) boot_pmf_gen_sh4 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=4) pmf_sh4 = boot_pmf_gen_sh4.get_pmf(point) npt.assert_equal(len(hsph_updated.vertices), pmf_sh4.shape[0]) npt.assert_(np.sum(pmf_sh4.shape) > 0) boot_pmf_gen_sh8 = BootPmfGen(data, model=csd_model, sphere=hsph_updated, sh_order=8) pmf_sh8 = boot_pmf_gen_sh8.get_pmf(point) npt.assert_equal(len(hsph_updated.vertices), pmf_sh8.shape[0]) npt.assert_(np.sum(pmf_sh8.shape) > 0)