def test_odfdeconv(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, sticks = multi_tensor(gtab, mevals, S0, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric724') mevecs = [all_tensor_evecs(sticks[0]).T, all_tensor_evecs(sticks[1]).T] odf_gt = multi_tensor_odf(sphere.vertices, [0.5, 0.5], mevals, mevecs) e1 = 15.0 e2 = 3.0 ratio = e2 / e1 csd = ConstrainedSDTModel(gtab, ratio, None) csd_fit = csd.fit(S) fodf = csd_fit.odf(sphere) directions, _, _ = peak_directions(odf_gt, sphere) directions2, _, _ = peak_directions(fodf, sphere) ang_sim = angular_similarity(directions, directions2) assert_equal(ang_sim > 1.9, True) assert_equal(directions.shape[0], 2) assert_equal(directions2.shape[0], 2) with warnings.catch_warnings(record=True) as w: ConstrainedSDTModel(gtab, ratio, sh_order=10) assert_equal(len(w) > 0, True) with warnings.catch_warnings(record=True) as w: ConstrainedSDTModel(gtab, ratio, sh_order=8) assert_equal(len(w) > 0, False)
def test_mvoxel_gqi(): data, gtab = dsi_voxels() sphere = get_sphere('symmetric724') gq = GeneralizedQSamplingModel(gtab, 'standard') gqfit = gq.fit(data) all_odfs = gqfit.odf(sphere) # Check that the first and last voxels each have 2 peaks odf = all_odfs[0, 0, 0] directions, values, indices = peak_directions(odf, sphere, .35, 25) assert_equal(directions.shape[0], 2) odf = all_odfs[-1, -1, -1] directions, values, indices = peak_directions(odf, sphere, .35, 25) assert_equal(directions.shape[0], 2)
def dirs_from_odf(odfs, sphere, relative_peak_threshold=.35, min_separation_angle=25., peak_normalize=True, max_peak_number=5): # or directions from odf num_peak_coeffs = max_peak_number * 3 peaks = np.zeros(odfs.shape[:-1] + (num_peak_coeffs,)) for index in ndindex(odfs.shape[:-1]): vox_peaks, values, _ = peak_directions(odfs[index], sphere, float(relative_peak_threshold), float(min_separation_angle)) if peak_normalize is True: values /= values[0] vox_peaks = vox_peaks * values[:, None] vox_peaks = vox_peaks.ravel() m = vox_peaks.shape[0] if m > num_peak_coeffs: m = num_peak_coeffs peaks[index][:m] = vox_peaks[:m] peaks = peaks.reshape(odfs.shape[:3] + (5, 3)) return peaks
def peak_extraction(odfs_file, sphere_vertices_file, out_file, relative_peak_threshold=.5, peak_normalize=1, min_separation_angle=45, max_peak_number=5): in_nifti = nib.load(odfs_file) refaff = in_nifti.get_affine() odfs = in_nifti.get_data() vertices = np.loadtxt(sphere_vertices_file) sphere = Sphere(xyz=vertices) num_peak_coeffs = max_peak_number * 3 peaks = np.zeros(odfs.shape[:-1] + (num_peak_coeffs,)) for index in ndindex(odfs.shape[:-1]): vox_peaks, values, _ = peak_directions(odfs[index], sphere, float(relative_peak_threshold), float(min_separation_angle)) if peak_normalize == 1: values /= values[0] vox_peaks = vox_peaks * values[:, None] vox_peaks = vox_peaks.ravel() m = vox_peaks.shape[0] if m > num_peak_coeffs: m = num_peak_coeffs peaks[index][:m] = vox_peaks[:m] peaks_img = nib.Nifti1Image(peaks.astype(np.float32), refaff) nib.save(peaks_img, out_file)
def peaks_extract(out_file, odf, affine, sphere, relative_peak_threshold=.5, peak_normalize=1, min_separation_angle=45, max_peak_number=5): num_peak_coeffs = max_peak_number * 3 peaks = np.zeros(odf.shape[:-1] + (num_peak_coeffs,)) for index in ndindex(odf.shape[:-1]): vox_peaks, values, _ = peak_directions(odf[index], sphere, float(relative_peak_threshold), float(min_separation_angle)) if peak_normalize == 1: values /= values[0] vox_peaks = vox_peaks * values[:, None] vox_peaks = vox_peaks.ravel() m = vox_peaks.shape[0] if m > num_peak_coeffs: m = num_peak_coeffs peaks[index][:m] = vox_peaks[:m] peaks_img = nib.Nifti1Image(peaks.astype(np.float32), affine) nib.save(peaks_img, out_file)
def test_dsi(): # load symmetric 724 sphere sphere = get_sphere('symmetric724') # load icosahedron sphere sphere2 = create_unit_sphere(5) btable = np.loadtxt(get_data('dsi515btable')) gtab = gradient_table(btable[:, 0], btable[:, 1:]) data, golden_directions = SticksAndBall(gtab, d=0.0015, S0=100, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=None) ds = DiffusionSpectrumDeconvModel(gtab) # symmetric724 dsfit = ds.fit(data) odf = dsfit.odf(sphere) directions, _, _ = peak_directions(odf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) # 5 subdivisions dsfit = ds.fit(data) odf2 = dsfit.odf(sphere2) directions, _, _ = peak_directions(odf2, sphere2, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) assert_equal(dsfit.pdf().shape, 3 * (ds.qgrid_size, )) sb_dummies = sticks_and_ball_dummies(gtab) for sbd in sb_dummies: data, golden_directions = sb_dummies[sbd] odf = ds.fit(data).odf(sphere2) directions, _, _ = peak_directions(odf, sphere2, .35, 25) if len(directions) <= 3: assert_equal(len(directions), len(golden_directions)) if len(directions) > 3: assert_equal(gfa(odf) < 0.1, True) assert_raises(ValueError, DiffusionSpectrumDeconvModel, gtab, qgrid_size=16)
def test_gqi(): #load symmetric 724 sphere sphere = get_sphere('symmetric724') #load icosahedron sphere sphere2 = create_unit_sphere(5) btable = np.loadtxt(get_data('dsi515btable')) bvals = btable[:, 0] bvecs = btable[:, 1:] gtab = gradient_table(bvals, bvecs) data, golden_directions = SticksAndBall(gtab, d=0.0015, S0=100, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=None) gq = GeneralizedQSamplingModel(gtab, method='gqi2', sampling_length=1.4) #symmetric724 gqfit = gq.fit(data) odf = gqfit.odf(sphere) directions, values, indices = peak_directions(odf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) #5 subdivisions gqfit = gq.fit(data) odf2 = gqfit.odf(sphere2) directions, values, indices = peak_directions(odf2, sphere2, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) sb_dummies = sticks_and_ball_dummies(gtab) for sbd in sb_dummies: data, golden_directions = sb_dummies[sbd] odf = gq.fit(data).odf(sphere2) directions, values, indices = peak_directions(odf, sphere2, .35, 25) if len(directions) <= 3: assert_equal(len(directions), len(golden_directions)) if len(directions) > 3: assert_equal(gfa(odf) < 0.1, True)
def test_dsi(): #load symmetric 724 sphere sphere = get_sphere('symmetric724') #load icosahedron sphere sphere2 = create_unit_sphere(5) btable = np.loadtxt(get_data('dsi515btable')) gtab = gradient_table(btable[:,0], btable[:,1:]) data, golden_directions = SticksAndBall(gtab, d=0.0015, S0=100, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=None) ds = DiffusionSpectrumModel(gtab) #symmetric724 dsfit = ds.fit(data) odf = dsfit.odf(sphere) directions, _, _ = peak_directions(odf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) #5 subdivisions dsfit = ds.fit(data) odf2 = dsfit.odf(sphere2) directions, _, _ = peak_directions(odf2, sphere2, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) assert_equal(dsfit.pdf().shape, 3 * (ds.qgrid_size, )) sb_dummies=sticks_and_ball_dummies(gtab) for sbd in sb_dummies: data, golden_directions = sb_dummies[sbd] odf = ds.fit(data).odf(sphere2) directions, _, _ = peak_directions(odf, sphere2, .35, 25) if len(directions) <= 3: assert_equal(len(directions), len(golden_directions)) if len(directions) > 3: assert_equal(gfa(odf) < 0.1, True) assert_raises(ValueError, DiffusionSpectrumModel, gtab, qgrid_size=16)
def test_peak_directions(): model = SimpleOdfModel() fit = model.fit(None) odf = fit.odf() argmax = odf.argmax() mx = odf.max() sphere = fit.model.sphere # Only one peak dir, val, ind = peak_directions(odf, sphere, .5, 45) dir_e = sphere.vertices[[argmax]] assert_array_equal(ind, [argmax]) assert_array_equal(val, odf[ind]) assert_array_equal(dir, dir_e) odf[0] = mx * .9 # Two peaks, relative_threshold dir, val, ind = peak_directions(odf, sphere, 1., 0) dir_e = sphere.vertices[[argmax]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax]) assert_array_equal(val, odf[ind]) dir, val, ind = peak_directions(odf, sphere, .8, 0) dir_e = sphere.vertices[[argmax, 0]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax, 0]) assert_array_equal(val, odf[ind]) # Two peaks, angle_sep dir, val, ind = peak_directions(odf, sphere, 0., 90) dir_e = sphere.vertices[[argmax]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax]) assert_array_equal(val, odf[ind]) dir, val, ind = peak_directions(odf, sphere, 0., 0) dir_e = sphere.vertices[[argmax, 0]] assert_array_equal(dir, dir_e) assert_array_equal(ind, [argmax, 0]) assert_array_equal(val, odf[ind])
def test_gqi(): #load symmetric 724 sphere sphere = get_sphere('symmetric724') #load icosahedron sphere sphere2 = create_unit_sphere(5) btable = np.loadtxt(get_data('dsi515btable')) bvals = btable[:,0] bvecs = btable[:,1:] gtab = gradient_table(bvals, bvecs) data, golden_directions = SticksAndBall(gtab, d=0.0015, S0=100, angles=[(0, 0), (90, 0)], fractions=[50, 50], snr=None) gq = GeneralizedQSamplingModel(gtab, method='gqi2', sampling_length=1.4) #symmetric724 gqfit = gq.fit(data) odf = gqfit.odf(sphere) directions, values, indices = peak_directions(odf, sphere, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) #5 subdivisions gqfit = gq.fit(data) odf2 = gqfit.odf(sphere2) directions, values, indices = peak_directions(odf2, sphere2, .35, 25) assert_equal(len(directions), 2) assert_almost_equal(angular_similarity(directions, golden_directions), 2, 1) sb_dummies=sticks_and_ball_dummies(gtab) for sbd in sb_dummies: data, golden_directions = sb_dummies[sbd] odf = gq.fit(data).odf(sphere2) directions, values, indices = peak_directions(odf, sphere2, .35, 25) if len(directions) <= 3: assert_equal(len(directions), len(golden_directions)) if len(directions) > 3: assert_equal(gfa(odf) < 0.1, True)
def test_odf_sh_to_sharp(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, sticks = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric724') qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:, :, :] = odf_gt[:] odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=1.) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
def test_odf_sh_to_sharp(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, sticks = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric724') qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:,:,:] = odf_gt[:] odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=1.) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
# from pylab import plot, show # plot(S, 'b') # plot(S2, 'r') # show() # # plot(np.abs(S - S2)) # plot((S - S2)) # show() gq = GeneralizedQSamplingModel(gtab_full, sampling_length=3.5) gqfit = gq.fit(SS) gqodf = gqfit.odf(sphere) gqdir, _, _ = peak_directions(gqodf, sphere, .35, 15) print angular_similarity(sticks, gqdir) grid_size = 35 dds = DiffusionSpectrumDeconvModel(gtab_full, qgrid_size=grid_size, r_start=0.2 * (grid_size // 2), r_end=0.7 * (grid_size // 2), r_step=0.02 * (grid_size // 2), filter_width=np.inf, normalize_peaks=False) ddsfit = dds.fit(SS) ddsodf = ddsfit.odf(sphere)