def test_interp_rbf(): from dipy.core.sphere import Sphere, interp_rbf from dipy.core.subdivide_octahedron import create_unit_hemisphere import numpy as np s0 = create_unit_hemisphere(2) s1 = create_unit_hemisphere(3) data = np.cos(s0.theta) + np.sin(s0.phi) expected = np.cos(s1.theta) + np.sin(s1.phi) interp_data = interp_rbf(data, s0, s1) nt.assert_(np.mean(np.abs(interp_data - expected)) < 0.1)
def odf_verts(self): """ The vertices on which to estimate the odf """ verts, edges, sides = create_unit_hemisphere(self.verts_level) return verts, edges
def make_fake_signal(): hemisphere = create_unit_hemisphere(4) v, e = hemisphere.vertices, hemisphere.edges vecs_xy = v[np.flatnonzero(v[:, 2] < .001)] evals = np.array([1.8, .2, .2])*10**-3*1.5 evecs_moveing = np.empty((len(vecs_xy), 3, 3)) evecs_moveing[:, :, 0] = vecs_xy evecs_moveing[:, :, 1] = [0, 0, 1] evecs_moveing[:, :, 2] = np.cross(evecs_moveing[:, :, 0], evecs_moveing[:, :, 1]) assert ((evecs_moveing * evecs_moveing).sum(1) - 1 < .001).all() assert ((evecs_moveing * evecs_moveing).sum(2) - 1 < .001).all() gtab = np.empty((len(v) + 1, 3)) bval = np.empty(len(v) + 1) bval[0] = 0 bval[1:] = 2000 gtab[0] = [0, 0, 0] gtab[1:] = v bvec = gtab.T B = design_matrix(bvec, bval) tensor_moveing = np.empty_like(evecs_moveing) for ii in xrange(len(vecs_xy)): tensor_moveing[ii] = np.dot(evecs_moveing[ii]*evals, evecs_moveing[ii].T) D_moveing = lower_triangular(tensor_moveing, 1) tensor_fixed = np.diag(evals) D_fixed = lower_triangular(tensor_fixed, 1) sig = .45*np.exp(np.dot(D_moveing, B.T)) + .55*np.exp(np.dot(B, D_fixed)) print sig assert sig.max() <= 1 assert sig.min() > 0 return hemisphere, vecs_xy, bval, bvec, sig
def test_DiscreteDirectionFinder(): def discrete_eval(sphere): X = np.zeros(len(sphere.phi)) X[0] = 1. X[1] = .3 return X sphere = create_unit_hemisphere(3) ddf = DiscreteDirectionFinder() ddf.config(sphere=sphere, relative_peak_threshold=.5, min_separation_angle=45) direction = ddf(discrete_eval) assert_array_almost_equal(direction, sphere.vertices[:1]) ddf.config(relative_peak_threshold=.2) direction = ddf(discrete_eval) assert_array_almost_equal(direction, sphere.vertices[:2])
def test_hat_and_lcr(): hemi = create_unit_hemisphere(6) m, n = sph_harm_ind_list(8) B = real_sph_harm(m, n, hemi.phi[:, None], hemi.theta[:, None]) H = hat(B) B_hat = np.dot(H, B) assert_array_almost_equal(B, B_hat) R = lcr_matrix(H) d = np.arange(len(hemi.theta)) r = d - np.dot(H, d) lev = np.sqrt(1-H.diagonal()) r /= lev r -= r.mean() r2 = np.dot(R, d) assert_array_almost_equal(r, r2) r3 = np.dot(d, R.T) assert_array_almost_equal(r, r3)
def test_smooth_pinv(): hemi = create_unit_hemisphere(3) m, n = sph_harm_ind_list(4) B = real_sph_harm(m, n, hemi.phi[:, None], hemi.theta[:, None]) L = np.zeros(len(m)) C = smooth_pinv(B, L) D = np.dot(npl.inv(np.dot(B.T, B)), B.T) assert_array_almost_equal(C, D) L = n*(n+1)*.05 C = smooth_pinv(B, L) L = np.diag(L) D = np.dot(npl.inv(np.dot(B.T, B) + L*L), B.T) assert_array_almost_equal(C, D) L = np.arange(len(n))*.05 C = smooth_pinv(B, L) L = np.diag(L) D = np.dot(npl.inv(np.dot(B.T, B) + L*L), B.T) assert_array_almost_equal(C, D)
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)) _sphere = create_unit_hemisphere(4) _odf = (_sphere.vertices * [1, 2, 3]).sum(-1) _gtab = GradientTable(np.ones((64, 3))) class SimpleOdfModel(OdfModel): sphere = _sphere def fit(self, data): fit = SimpleOdfFit(self, data) fit.model = self return fit class SimpleOdfFit(OdfFit):
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)) _sphere = create_unit_hemisphere(4) _odf = (_sphere.vertices * [1, 2, 3]).sum(-1) _gtab = GradientTable(np.ones((64, 3))) class SimpleOdfModel(OdfModel): sphere = _sphere def fit(self, data): fit = SimpleOdfFit(self, data) fit.model = self return fit class SimpleOdfFit(OdfFit): def odf(self, sphere=None):