def test_sticks_and_ball(): d = 0.0015 S, sticks = sticks_and_ball(gtab, d=d, S0=1, angles=[(0, 0), ], fractions=[100], snr=None) assert_array_equal(sticks, [[0, 0, 1]]) S_st = SingleTensor(gtab, 1, evals=[d, 0, 0], evecs=[[0, 0, 0], [0, 0, 0], [1, 0, 0]]) assert_array_almost_equal(S, S_st)
def test_single_tensor(): evals = np.array([1.4, .35, .35]) * 10**(-3) evecs = np.eye(3) S = SingleTensor(gtab, 100, evals, evecs, snr=None) assert_array_almost_equal(S[gtab.b0s_mask], 100) assert_(np.mean(S[~gtab.b0s_mask]) < 100) from dipy.reconst.dti import TensorModel m = TensorModel(gtab) t = m.fit(S) assert_array_almost_equal(t.fa, 0.707, decimal=3)
def test_single_tensor(): fimg, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) #bvals=np.loadtxt(fbvals) #bvecs=np.loadtxt(fbvecs).T img = nib.load(fimg) data = img.get_data() evals = np.array([1.4, .35, .35]) * 10**(-3) evecs = np.eye(3) S = SingleTensor(bvals, bvecs, 100, evals, evecs, snr=None) """
def orbital_phantom(gtab=None, evals=diffusion_evals, func=None, t=np.linspace(0, 2 * np.pi, 1000), datashape=(64, 64, 64, 65), origin=(32, 32, 32), scale=(25, 25, 25), angles=np.linspace(0, 2 * np.pi, 32), radii=np.linspace(0.2, 2, 6), S0=100., snr=None): """Create a phantom based on a 3-D orbit ``f(t) -> (x,y,z)``. Parameters ----------- gtab : GradientTable Gradient table of measurement directions. evals : array, shape (3,) Tensor eigenvalues. func : user defined function f(t)->(x,y,z) It could be desirable for ``-1=<x,y,z <=1``. If None creates a circular orbit. t : array, shape (K,) Represents time for the orbit. Default is ``np.linspace(0, 2 * np.pi, 1000)``. datashape : array, shape (X,Y,Z,W) Size of the output simulated data origin : tuple, shape (3,) Define the center for the volume scale : tuple, shape (3,) Scale the function before applying to the grid angles : array, shape (L,) Density angle points, always perpendicular to the first eigen vector Default np.linspace(0, 2 * np.pi, 32). radii : array, shape (M,) Thickness radii. Default ``np.linspace(0.2, 2, 6)``. angles and radii define the total thickness options S0 : double, optional Maximum simulated signal. Default 100. snr : float, optional The signal to noise ratio set to apply Rician noise to the data. Default is to not add noise at all. Returns ------- data : array, shape (datashape) See Also -------- add_noise Examples --------- >>> def f(t): ... x = np.sin(t) ... y = np.cos(t) ... z = np.linspace(-1, 1, len(x)) ... return x, y, z >>> data = orbital_phantom(func=f) """ if gtab is None: fimg, fbvals, fbvecs = get_data('small_64D') gtab = gradient_table(fbvals, fbvecs) if func is None: x = np.sin(t) y = np.cos(t) z = np.zeros(t.shape) else: x, y, z = func(t) dx = np.diff(x) dy = np.diff(y) dz = np.diff(z) x = scale[0] * x + origin[0] y = scale[1] * y + origin[1] z = scale[2] * z + origin[2] bx = np.zeros(len(angles)) by = np.sin(angles) bz = np.cos(angles) # The entire volume is considered to be inside the brain. # Voxels without a fiber crossing through them are taken # to be isotropic with signal = S0. vol = np.zeros(datashape) + S0 for i in range(len(dx)): evecs, R = diff2eigenvectors(dx[i], dy[i], dz[i]) S = SingleTensor(gtab, S0, evals, evecs, snr=None) vol[int(x[i]), int(y[i]), int(z[i]), :] += S for r in radii: for j in range(len(angles)): rb = np.dot(R, np.array([bx[j], by[j], bz[j]])) ix = int(x[i] + r * rb[0]) iy = int(y[i] + r * rb[1]) iz = int(z[i] + r * rb[2]) vol[ix, iy, iz] = vol[ix, iy, iz] + S vol = vol / np.max(vol, axis=-1)[..., np.newaxis] vol *= S0 if snr is not None: vol = add_noise(vol, snr, S0=S0, noise_type='rician') return vol
def orbital_phantom(bvals=None, bvecs=None, evals=np.array([1.4, .35, .35]) * 10**(-3), func=None, t=np.linspace(0, 2 * np.pi, 1000), datashape=(64, 64, 64, 65), origin=(32, 32, 32), scale=(25, 25, 25), angles=np.linspace(0, 2 * np.pi, 32), radii=np.linspace(0.2, 2, 6), S0=100.): """ Create a phantom based on a 3d orbit f(t)->(x,y,z) Parameters ----------- bvals : array, shape (N,) bvecs : array, shape (N,3) evals : array, shape (3,) tensor eigenvalues func : user defined function f(t)->(x,y,z) It could be desirable for -1=<x,y,z <=1 if None creates a circular orbit t : array, shape (K,) represents time for the orbit Default is np.linspace(0,2*np.pi,1000) datashape : array, shape (X,Y,Z,W) size of the output simulated data origin : tuple, shape (3,) define the center for the volume scale : tuple, shape (3,) scale the function before applying to the grid angles : array, shape (L,) density angle points, always perpendicular to the first eigen vector Default np.linspace(0,2*np.pi,32), radii : array, shape (M,) thickness radii Default np.linspace(0.2,2,6) angles and radii define the total thickness options S0 : double, simulated signal without diffusion gradients applied Default 100. snr : signal to noise ratio Used for applying rician noise to the data. Default 200. Common is 20. background_noise : boolean, Default False Returns --------- data : array, shape (datashape) Notes -------- Crossings can be created by adding multiple orbitual_phantom outputs. Examples --------- def f(t): x=np.sin(t) y=np.cos(t) z=np.linspace(-1,1,len(x)) return x,y,z data=orbitual_phantom(func=f) """ if bvals == None: fimg, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) bvecs[np.isnan(bvecs)] = 0 if func == None: x = np.sin(t) y = np.cos(t) z = np.zeros(t.shape) else: x, y, z = func(t) #stop dx = np.diff(x) dy = np.diff(y) dz = np.diff(z) x = scale[0] * x + origin[0] y = scale[1] * y + origin[1] z = scale[2] * z + origin[2] bx = np.zeros(len(angles)) by = np.sin(angles) bz = np.cos(angles) vol = np.zeros(datashape) for i in range(len(dx)): evecs, R = diff2eigenvectors(dx[i], dy[i], dz[i]) S = SingleTensor(bvals, bvecs, S0, evals, evecs, snr=None) #print sigma, S0/snr, S0, snr vol[x[i], y[i], z[i], :] += S for r in radii: for j in range(len(angles)): rb = np.dot(R, np.array([bx[j], by[j], bz[j]])) vol[x[i] + r * rb[0], y[i] + r * rb[1], z[i] + r * rb[2]] += S #ten=Tensor(vol,bvals,bvecs) #FA=ten.fa() #FA[np.isnan(FA)]=0 #vol[np.isnan(vol)]=0 return vol