def test_hemisphere_subdivide(): def flip(vertices): x, y, z = vertices.T f = (z < 0) | ((z == 0) & (y < 0)) | ((z == 0) & (y == 0) & (x < 0)) return 1 - 2*f[:, None] decimals = 6 # Test HemiSphere.subdivide # Create a hemisphere by dividing a hemi-icosahedron hemi1 = HemiSphere.from_sphere(unit_icosahedron).subdivide(4) vertices1 = np.round(hemi1.vertices, decimals) vertices1 *= flip(vertices1) order = np.lexsort(vertices1.T) vertices1 = vertices1[order] # Create a hemisphere from a subdivided sphere sphere = unit_icosahedron.subdivide(4) hemi2 = HemiSphere.from_sphere(sphere) vertices2 = np.round(hemi2.vertices, decimals) vertices2 *= flip(vertices2) order = np.lexsort(vertices2.T) vertices2 = vertices2[order] # The two hemispheres should have the same vertices up to their order nt.assert_array_equal(vertices1, vertices2) # Create a hemisphere from vertices hemi3 = HemiSphere(xyz=hemi1.vertices) nt.assert_array_equal(hemi1.faces, hemi3.faces) nt.assert_array_equal(hemi1.edges, hemi3.edges)
def test_hemisphere_subdivide(): def flip(vertices): x, y, z = vertices.T f = (z < 0) | ((z == 0) & (y < 0)) | ((z == 0) & (y == 0) & (x < 0)) return 1 - 2 * f[:, None] decimals = 6 # Test HemiSphere.subdivide # Create a hemisphere by dividing a hemi-icosahedron hemi1 = HemiSphere.from_sphere(unit_icosahedron).subdivide(4) vertices1 = np.round(hemi1.vertices, decimals) vertices1 *= flip(vertices1) order = np.lexsort(vertices1.T) vertices1 = vertices1[order] # Create a hemisphere from a subdivided sphere sphere = unit_icosahedron.subdivide(4) hemi2 = HemiSphere.from_sphere(sphere) vertices2 = np.round(hemi2.vertices, decimals) vertices2 *= flip(vertices2) order = np.lexsort(vertices2.T) vertices2 = vertices2[order] # The two hemispheres should have the same vertices up to their order nt.assert_array_equal(vertices1, vertices2) # Create a hemisphere from vertices hemi3 = HemiSphere(xyz=hemi1.vertices) nt.assert_array_equal(hemi1.faces, hemi3.faces) nt.assert_array_equal(hemi1.edges, hemi3.edges)
def test_sphere_subdivide(): sphere1 = unit_octahedron.subdivide(4) sphere2 = Sphere(xyz=sphere1.vertices) nt.assert_equal(sphere1.faces.shape, sphere2.faces.shape) nt.assert_equal(array_to_set(sphere1.faces), array_to_set(sphere2.faces)) sphere1 = unit_icosahedron.subdivide(4) sphere2 = Sphere(xyz=sphere1.vertices) nt.assert_equal(sphere1.faces.shape, sphere2.faces.shape) nt.assert_equal(array_to_set(sphere1.faces), array_to_set(sphere2.faces))
def test_peak_directions_nl(): def discrete_eval(sphere): return abs(sphere.vertices).sum(-1) directions, values = peak_directions_nl(discrete_eval) assert_equal(directions.shape, (4, 3)) assert_array_almost_equal(abs(directions), 1 / np.sqrt(3)) assert_array_equal(values, abs(directions).sum(-1)) # Test using a different sphere sphere = unit_icosahedron.subdivide(4) directions, values = peak_directions_nl(discrete_eval, sphere=sphere) assert_equal(directions.shape, (4, 3)) assert_array_almost_equal(abs(directions), 1 / np.sqrt(3)) assert_array_equal(values, abs(directions).sum(-1)) # Test the relative_peak_threshold def discrete_eval(sphere): A = abs(sphere.vertices).sum(-1) x, y, z = sphere.vertices.T B = 1 + (x * z > 0) + 2 * (y * z > 0) return A * B directions, values = peak_directions_nl(discrete_eval, .01) assert_equal(directions.shape, (4, 3)) directions, values = peak_directions_nl(discrete_eval, .3) assert_equal(directions.shape, (3, 3)) directions, values = peak_directions_nl(discrete_eval, .6) assert_equal(directions.shape, (2, 3)) directions, values = peak_directions_nl(discrete_eval, .8) assert_equal(directions.shape, (1, 3)) assert_almost_equal(values, 4 * 3 / np.sqrt(3)) # Test odfs with large areas of zero def discrete_eval(sphere): A = abs(sphere.vertices).sum(-1) x, y, z = sphere.vertices.T B = (x * z > 0) + 2 * (y * z > 0) return A * B directions, values = peak_directions_nl(discrete_eval, 0.) assert_equal(directions.shape, (3, 3)) directions, values = peak_directions_nl(discrete_eval, .6) assert_equal(directions.shape, (2, 3)) directions, values = peak_directions_nl(discrete_eval, .8) assert_equal(directions.shape, (1, 3)) assert_almost_equal(values, 3 * 3 / np.sqrt(3))
def test_bdg_get_direction(): """This tests the direction found by the bootstrap direction getter. """ sphere = HemiSphere.from_sphere(unit_icosahedron.subdivide()) two_neighbors = sphere.edges[0] direction1 = sphere.vertices[two_neighbors[0]] direction2 = sphere.vertices[two_neighbors[1]] angle = np.rad2deg(direction1.dot(direction2)) point = np.zeros(3) prev_direction = direction2.copy() pmf = np.zeros([1, 1, 1, len(sphere.vertices)]) pmf[:, :, :, two_neighbors[0]] = 1 pmf_gen = SimplePmfGen(pmf) # test case in which no valid direction is found with default maxdir boot_dg = BootDirectionGetter(pmf_gen, angle / 2., sphere=sphere) npt.assert_equal(boot_dg.get_direction(point, prev_direction), 1) npt.assert_equal(direction2, prev_direction) # test case in which no valid direction is found with new max attempts boot_dg = BootDirectionGetter(pmf_gen, angle / 2., sphere=sphere, max_attempts=3) npt.assert_equal(boot_dg.get_direction(point, prev_direction), 1) npt.assert_equal(direction2, prev_direction) # test case in which a valid direction is found boot_dg = BootDirectionGetter(pmf_gen, angle * 2., sphere=sphere, max_attempts=1) npt.assert_equal(boot_dg.get_direction(point, prev_direction), 0) npt.assert_equal(direction1, prev_direction) # test invalid max_attempts parameters npt.assert_raises( ValueError, lambda: BootDirectionGetter( pmf_gen, angle * 2., sphere=sphere, max_attempts=0))
def test_bdg_get_direction(): """This tests the direction found by the bootstrap direction getter. """ sphere = HemiSphere.from_sphere(unit_icosahedron.subdivide()) two_neighbors = sphere.edges[0] direction1 = sphere.vertices[two_neighbors[0]] direction2 = sphere.vertices[two_neighbors[1]] angle = np.rad2deg(direction1.dot(direction2)) point = np.zeros(3) prev_direction = direction2.copy() pmf = np.zeros([1, 1, 1, len(sphere.vertices)]) pmf[:, :, :, two_neighbors[0]] = 1 pmf_gen = SimplePmfGen(pmf) # test case in which no valid direction is found with default maxdir boot_dg = BootDirectionGetter(pmf_gen, angle / 2., sphere=sphere) npt.assert_equal(boot_dg.get_direction(point, prev_direction), 1) npt.assert_equal(direction2, prev_direction) # test case in which no valid direction is found with new max attempts boot_dg = BootDirectionGetter(pmf_gen, angle / 2., sphere=sphere, max_attempts=3) npt.assert_equal(boot_dg.get_direction(point, prev_direction), 1) npt.assert_equal(direction2, prev_direction) # test case in which a valid direction is found boot_dg = BootDirectionGetter(pmf_gen, angle * 2., sphere=sphere, max_attempts=1) npt.assert_equal(boot_dg.get_direction(point, prev_direction), 0) npt.assert_equal(direction1, prev_direction) # test invalid max_attempts parameters npt.assert_raises( ValueError, lambda: BootDirectionGetter(pmf_gen, angle * 2., sphere=sphere, max_attempts=0))
from dipy.data import get_data img, bvals, bvecs = get_data('small_64D') bvals = np.load(bvals) bvecs = np.load(bvecs) where_dwi = bvals > 0 bvecs = bvecs[where_dwi] bvals = bvals[where_dwi] bvals = np.ones_like(bvals) * B gtab = GradientTable(bvals[:, None] * bvecs) error = np.zeros_like(angles) mean_means = np.zeros_like(angles) mean_stds = np.zeros_like(angles) from dipy.core.sphere import unit_icosahedron sphere = unit_icosahedron.subdivide(5) angles_mean = [] angles_std = [] mask = np.zeros_like(bvals, dtype=bool) mask[:discarded] = True #------------------------------------------------------------------------------ recovered_angle = [] SNR = None sk = SparseKernelModel(gtab, sh_order=8, l1_ratio=0.5, alpha=0.0001)
from __future__ import division from warnings import warn import numpy as np from .recspeed import local_maxima, remove_similar_vertices from ..core.onetime import auto_attr from dipy.core.sphere import unique_edges, unit_icosahedron, HemiSphere #Classes OdfModel and OdfFit are using API ReconstModel and ReconstFit from .base default_sphere = HemiSphere.from_sphere(unit_icosahedron.subdivide(3)) class DirectionFinder(object): """Abstract class for direction finding""" def __init__(self): self._config = {} def __call__(self, sphere_eval): """To be impemented by subclasses""" raise NotImplementedError() def config(self, **kwargs): """Update direction finding parameters""" for i in kwargs: if i not in self._config: warn("{} is not a known parameter".format(i)) self._config.update(kwargs) class DiscreteDirectionFinder(DirectionFinder): """Discrete Direction Finder