def make_fake_signal(): v, e, f = create_half_unit_sphere(4) vecs_xy = v[np.flatnonzero(v[:, 2] == 0)] 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)) assert sig.max() <= 1 assert sig.min() > 0 return v, e, vecs_xy, bval, bvec, sig
def make_fake_signal(): v, e, f = create_half_unit_sphere(4) vecs_xy = v[np.flatnonzero(v[:, 2] == 0)] evals = np.array([1.8, 0.2, 0.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 < 0.001).all() assert ((evecs_moveing * evecs_moveing).sum(2) - 1 < 0.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 = 0.45 * np.exp(np.dot(D_moveing, B.T)) + 0.55 * np.exp(np.dot(B, D_fixed)) assert sig.max() <= 1 assert sig.min() > 0 return v, e, vecs_xy, bval, bvec, sig
def test_set_odf_vertices(self): model = OdfModel() v, e, f = create_half_unit_sphere(4) model.set_odf_vertices(v, e) assert_array_equal(v, model.odf_vertices) assert_array_equal(e, model.odf_edges) assert_array_equal(abs(v.dot(v.T)), model._distance_matrix) assert_raises(ValueError, model.set_odf_vertices, v[:, :2], e) assert_raises(ValueError, model.set_odf_vertices, v / 2, e)
def simple_tracking_function(data, fa, bval, bvec, seed_mask, start_steps, voxel_size, density): """An example of a simple traking function using the tools in dipy This tracking function uses the SlowAdcOpdfModel to fit diffusion data. By using the ClosestPeakSelector, the function tracks along the peak of Opdf closest to the incoming direction. It also uses the BoundryIntegrator to integrate the streamlines and NearestNeighborInterpolator to interpolate the data. The ResidualBootstrap means the tracks are probabilistic, not deterministic. """ #the interpolator allows us to index the dwi data in continous space data_mask = fa > .2 normalized_data = normalize_data(data, bval) interpolator = NearestNeighborInterpolator(normalized_data, voxel_size, data_mask) #the model fits the dwi data, this model can resolve crossing fibers #see documentation of SlowAdcOpdfModel for more info model = SlowAdcOpdfModel(6, bval, bvec, .006) vert, edges, faces = create_half_unit_sphere(4) model.set_sampling_points(vert, edges) #this residual bootstrap wrapper returns a sample from the bootstrap #distribution istead of returning the raw data min_signal = normalized_data.min() B = model.B wrapped_interp = ResidualBootstrapWrapper(interpolator, B, min_signal) #the peakselector returns the closest peak to the incoming direction when #in voxels with multiple peaks peak_finder = ClosestPeakSelector(model, wrapped_interp) peak_finder.angle_limit = 60 seeds = seeds_from_mask(seed_mask, density, voxel_size) #the propagator is used to integrate the streamlines propogator = BoundryIntegrator(voxel_size) tracks = generate_streamlines(peak_finder, propogator, seeds, start_steps) return tracks
def needlebook(bvals,bvecs,d=0.0015,steps=21,subdiv=3,fractions=[1.]): """ Single needles book """ sticks,e,t=create_half_unit_sphere(subdiv) CBK=[]; STK=[]; FRA=[]; REG=[] def single_needles(CBK,STK,FRA,bvals,bvecs,sticks,fraction): for s in sticks: S=np.zeros(len(bvecs)-1) for (i,g) in enumerate(bvecs[1:]): S[i]=(1-fraction)*np.exp(-bvals[i+1]*d)+fraction*np.exp(-bvals[i+1]*d*np.dot(s,g)**2) CBK.append(S) STK.append(s) FRA.append(fraction) #fractions=np.linspace(0,1,steps) for f in fractions: single_needles(CBK,STK,FRA,bvals,bvecs,sticks,fraction=f) CBK=np.array(CBK) STK=np.array(STK) FRA=np.array(FRA) return CBK,STK,FRA
def test_hat_and_lcr(): v, e, f = create_half_unit_sphere(6) m, n = sph_harm_ind_list(8) r, pol, azi = cart2sphere(*v.T) B = real_sph_harm(m, n, azi[:, None], pol[:, 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(azi)) 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(): v, e, f = create_half_unit_sphere(3) m, n = sph_harm_ind_list(4) r, pol, azi = cart2sphere(*v.T) B = real_sph_harm(m, n, azi[:, None], pol[:, 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) * 0.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)) * 0.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)
def test_smooth_pinv(): v, e, f = create_half_unit_sphere(3) m, n = sph_harm_ind_list(4) r, pol, azi = cart2sphere(*v.T) B = real_sph_harm(m, n, azi[:, None], pol[:, 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)
def codebook(bvals,bvecs,S0=100,d=0.0015,fractions=False): sticks,e,t=create_half_unit_sphere(3) CB=[] STK=[] FRA=[] REG=[] def single_needles(CB,STK,FRA,bvals,bvecs,sticks,fraction): for s in sticks: S=np.zeros(len(bvecs)) for (i,g) in enumerate(bvecs[1:]): S[i+1]=fraction*np.exp(-bvals[i+1]*d*np.dot(s,g)**2) S[i+1]=S0*S[i+1] S[0]=S0 CB.append(S) STK.append([s]) FRA.append([1.]) return #initial codebook only with single needles single_needles(CB,STK,FRA,bvals,bvecs,sticks,fraction=1) IN=np.array(CB) REG.append(len(CB)) #two fibers 50 50 for c in combinations(range(len(sticks)),2): CB.append((IN[c[0]]+IN[c[1]])/2.) STK.append([sticks[c[0]],sticks[c[1]]]) FRA.append([.5,.5]) #REG.append(len(CB)) if fractions: #two fibers 75 25 for c in combinations(range(len(sticks)),2): CB.append((.75*IN[c[0]]+.25*IN[c[1]])) STK.append([sticks[c[0]],sticks[c[1]]]) FRA.append([.75,.25]) #REG.append(len(CB)) #two fibers 25 75 for c in combinations(range(len(sticks)),2): CB.append((.25*IN[c[0]]+.75*IN[c[1]])) STK.append([sticks[c[0]],sticks[c[1]]]) FRA.append([.25,.75]) #REG.append(len(CB)) REG.append(len(CB)) #three fibers 33 33 33 for c in combinations(range(len(sticks)),3): CB.append((IN[c[0]]+IN[c[1]]+IN[c[2]])/3.) STK.append([sticks[c[0]],sticks[c[1]],sticks[c[2]]]) FRA.append([1/3.,1/3.,1/3.]) #REG.append(len(CB)) if fractions: #three fibers 60 20 20 for c in combinations(range(len(sticks)),3): CB.append((0.6*IN[c[0]]+0.2*IN[c[1]]+0.2*IN[c[2]])) STK.append([sticks[c[0]],sticks[c[1]],sticks[c[2]]]) FRA.append([.6,.2,.2]) #REG.append(len(CB)) #three fibers 20 60 20 for c in combinations(range(len(sticks)),3): CB.append((0.2*IN[c[0]]+0.6*IN[c[1]]+0.2*IN[c[2]])) STK.append([sticks[c[0]],sticks[c[1]],sticks[c[2]]]) FRA.append([.2,.6,.2]) #REG.append(len(CB)) #three fibers 20 20 60 for c in combinations(range(len(sticks)),3): CB.append((0.2*IN[c[0]]+0.2*IN[c[1]]+0.6*IN[c[2]])) STK.append([sticks[c[0]],sticks[c[1]],sticks[c[2]]]) FRA.append([.2,.2,.6]) #REG.append(len(CB)) REG.append(len(CB)) #isotropic CB.append(S0*np.ones(len(bvecs))) STK.append([]) FRA.append([0.,0.,0.]) REG.append(len(CB)) return np.array(CB),STK,FRA,REG
return 0 w = np.dot(verts[i[0]], verts[i[1]]) return np.rad2deg(np.arccos(np.abs(w))) 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] * 3 from dipy.core.triangle_subdivide import create_half_unit_sphere verts, edges, sides = create_half_unit_sphere(5) faces = edges[sides, 0] sk = SparseKernelModel(bvals, bvecs, sh_order=8) angles = [25, 30, 35, 40, 45, 50, 55, 60] recovered_angle = [] SNR = None bvals = np.ones_like(bvals) * 3000 cache = None odf_verts = verts for angle in angles: print "Analyzing angle", angle
def __init__(self): v, e, f = create_half_unit_sphere(4) self.set_odf_vertices(v, e) self.odf = (v * [1, 2, 3]).sum(-1)