/usr/lib/python2.7/dist-packages/dipy/reconst/tests/test_csdeconv.py is in python-dipy 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | import warnings
import nibabel as nib
import numpy as np
import numpy.testing as npt
from numpy.testing import (assert_, assert_equal, assert_almost_equal,
assert_array_almost_equal, run_module_suite,
assert_array_equal)
from dipy.data import get_sphere, get_data, default_sphere, small_sphere
from dipy.sims.voxel import (multi_tensor,
single_tensor,
multi_tensor_odf,
all_tensor_evecs, single_tensor_odf)
from dipy.core.gradients import gradient_table
from dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel,
ConstrainedSDTModel,
forward_sdeconv_mat,
odf_deconv,
odf_sh_to_sharp,
auto_response,
recursive_response,
response_from_mask)
from dipy.direction.peaks import peak_directions
from dipy.core.sphere_stats import angular_similarity
from dipy.reconst.dti import TensorModel, fractional_anisotropy
from dipy.reconst.shm import (CsaOdfModel, QballModel, sf_to_sh, sh_to_sf,
real_sym_sh_basis, sph_harm_ind_list)
from dipy.reconst.shm import lazy_index
from dipy.core.geometry import cart2sphere
import dipy.reconst.dti as dti
from dipy.reconst.dti import fractional_anisotropy
from dipy.core.sphere import Sphere
def test_recursive_response_calibration():
"""
Test the recursive response calibration method.
"""
SNR = 100
S0 = 1
sh_order = 8
_, fbvals, fbvecs = get_data('small_64D')
bvals = np.load(fbvals)
bvecs = np.load(fbvecs)
sphere = get_sphere('symmetric724')
gtab = gradient_table(bvals, bvecs)
evals = np.array([0.0015, 0.0003, 0.0003])
evecs = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]).T
mevals = np.array(([0.0015, 0.0003, 0.0003],
[0.0015, 0.0003, 0.0003]))
angles = [(0, 0), (90, 0)]
where_dwi = lazy_index(~gtab.b0s_mask)
S_cross, sticks_cross = multi_tensor(gtab, mevals, S0, angles=angles,
fractions=[50, 50], snr=SNR)
S_single = single_tensor(gtab, S0, evals, evecs, snr=SNR)
data = np.concatenate((np.tile(S_cross, (8, 1)), np.tile(S_single, (2, 1))),
axis=0)
odf_gt_cross = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50])
odf_gt_single = single_tensor_odf(sphere.vertices, evals, evecs)
response = recursive_response(gtab, data, mask=None, sh_order=8,
peak_thr=0.01, init_fa=0.05,
init_trace=0.0021, iter=8, convergence=0.001,
parallel=False)
csd = ConstrainedSphericalDeconvModel(gtab, response)
csd_fit = csd.fit(data)
assert_equal(np.all(csd_fit.shm_coeff[:, 0] >= 0), True)
fodf = csd_fit.odf(sphere)
directions_gt_single, _, _ = peak_directions(odf_gt_single, sphere)
directions_gt_cross, _, _ = peak_directions(odf_gt_cross, sphere)
directions_single, _, _ = peak_directions(fodf[8, :], sphere)
directions_cross, _, _ = peak_directions(fodf[0, :], sphere)
ang_sim = angular_similarity(directions_cross, directions_gt_cross)
assert_equal(ang_sim > 1.9, True)
assert_equal(directions_cross.shape[0], 2)
assert_equal(directions_gt_cross.shape[0], 2)
ang_sim = angular_similarity(directions_single, directions_gt_single)
assert_equal(ang_sim > 0.9, True)
assert_equal(directions_single.shape[0], 1)
assert_equal(directions_gt_single.shape[0], 1)
sphere = Sphere(xyz=gtab.gradients[where_dwi])
sf = response.on_sphere(sphere)
S = np.concatenate(([response.S0], sf))
tenmodel = dti.TensorModel(gtab, min_signal=0.001)
tenfit = tenmodel.fit(S)
FA = fractional_anisotropy(tenfit.evals)
FA_gt = fractional_anisotropy(evals)
assert_almost_equal(FA, FA_gt, 1)
def test_response_from_mask():
fdata, fbvals, fbvecs = get_data('small_64D')
bvals = np.load(fbvals)
bvecs = np.load(fbvecs)
data = nib.load(fdata).get_data()
gtab = gradient_table(bvals, bvecs)
ten = TensorModel(gtab)
tenfit = ten.fit(data)
FA = fractional_anisotropy(tenfit.evals)
FA[np.isnan(FA)] = 0
radius = 3
for fa_thr in np.arange(0, 1, 0.1):
response_auto, ratio_auto, nvoxels = auto_response(gtab,
data,
roi_center=None,
roi_radius=radius,
fa_thr=fa_thr,
return_number_of_voxels=True)
ci, cj, ck = np.array(data.shape[:3]) / 2
mask = np.zeros(data.shape[:3])
mask[ci - radius: ci + radius,
cj - radius: cj + radius,
ck - radius: ck + radius] = 1
mask[FA <= fa_thr] = 0
response_mask, ratio_mask = response_from_mask(gtab, data, mask)
assert_equal(int(np.sum(mask)), nvoxels)
assert_array_almost_equal(response_mask[0], response_auto[0])
assert_almost_equal(response_mask[1], response_auto[1])
assert_almost_equal(ratio_mask, ratio_auto)
def test_csdeconv():
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]))
angles = [(0, 0), (60, 0)]
S, sticks = multi_tensor(gtab, mevals, S0, angles=angles,
fractions=[50, 50], snr=SNR)
sphere = get_sphere('symmetric362')
odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50])
response = (np.array([0.0015, 0.0003, 0.0003]), S0)
csd = ConstrainedSphericalDeconvModel(gtab, response)
csd_fit = csd.fit(S)
assert_equal(csd_fit.shm_coeff[0] > 0, True)
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:
ConstrainedSphericalDeconvModel(gtab, response, sh_order=10)
assert_equal(len(w) > 0, True)
with warnings.catch_warnings(record=True) as w:
ConstrainedSphericalDeconvModel(gtab, response, sh_order=8)
assert_equal(len(w) > 0, False)
mevecs = []
for s in sticks:
mevecs += [all_tensor_evecs(s).T]
S2 = single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None)
big_S = np.zeros((10, 10, 10, len(S2)))
big_S[:] = S2
aresponse, aratio = auto_response(gtab, big_S, roi_center=(5, 5, 4),
roi_radius=3, fa_thr=0.5)
assert_array_almost_equal(aresponse[0], response[0])
assert_almost_equal(aresponse[1], 100)
assert_almost_equal(aratio, response[0][1] / response[0][0])
aresponse2, aratio2 = auto_response(gtab, big_S, roi_radius=3, fa_thr=0.5)
assert_array_almost_equal(aresponse[0], response[0])
_, _, nvoxels = auto_response(gtab, big_S, roi_center=(5, 5, 4),
roi_radius=30, fa_thr=0.5,
return_number_of_voxels=True)
assert_equal(nvoxels, 1000)
_, _, nvoxels = auto_response(gtab, big_S, roi_center=(5, 5, 4),
roi_radius=30, fa_thr=1,
return_number_of_voxels=True)
assert_equal(nvoxels, 0)
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]))
angles = [(0, 0), (90, 0)]
S, sticks = multi_tensor(gtab, mevals, S0, angles=angles,
fractions=[50, 50], snr=SNR)
sphere = get_sphere('symmetric362')
odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50])
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)
csd_fit = csd.fit(np.zeros_like(S))
fodf = csd_fit.odf(sphere)
assert_array_equal(fodf, np.zeros_like(fodf))
odf_sh = np.zeros_like(fodf)
odf_sh[1] = np.nan
fodf, it = odf_deconv(odf_sh, csd.R, csd.B_reg)
assert_array_equal(fodf, np.zeros_like(fodf))
def test_odf_sh_to_sharp():
SNR = None
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=0.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_forward_sdeconv_mat():
m, n = sph_harm_ind_list(4)
mat = forward_sdeconv_mat(np.array([0, 2, 4]), n)
expected = np.diag([0, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4])
npt.assert_array_equal(mat, expected)
sh_order = 8
expected_size = (sh_order + 1) * (sh_order + 2) / 2
r_rh = np.arange(0, sh_order + 1, 2)
m, n = sph_harm_ind_list(sh_order)
mat = forward_sdeconv_mat(r_rh, n)
npt.assert_equal(mat.shape, (expected_size, expected_size))
npt.assert_array_equal(mat.diagonal(), n)
# Odd spherical harmonic degrees should raise a ValueError
n[2] = 3
npt.assert_raises(ValueError, forward_sdeconv_mat, r_rh, n)
def test_r2_term_odf_sharp():
SNR = None
S0 = 1
angle = 45 #45 degrees is a very tight angle to disentangle
_, fbvals, fbvecs = get_data('small_64D') #get_data('small_64D')
bvals = np.load(fbvals)
bvecs = np.load(fbvecs)
sphere = get_sphere('symmetric724')
gtab = gradient_table(bvals, bvecs)
mevals = np.array(([0.0015, 0.0003, 0.0003],
[0.0015, 0.0003, 0.0003]))
angles = [(0, 0), (angle, 0)]
S, sticks = multi_tensor(gtab, mevals, S0, angles=angles,
fractions=[50, 50], snr=SNR)
odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50])
odfs_sh = sf_to_sh(odf_gt, sphere, sh_order=8, basis_type=None)
fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15.,
sh_order=8, lambda_=1., tau=0.1, r2_term=True)
fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None)
directions_gt, _, _ = peak_directions(odf_gt, sphere)
directions, _, _ = peak_directions(fodf, sphere)
ang_sim = angular_similarity(directions_gt, directions)
assert_equal(ang_sim > 1.9, True)
assert_equal(directions.shape[0], 2)
# This should pass as well
sdt_model = ConstrainedSDTModel(gtab, ratio=3/15., sh_order=8)
sdt_fit = sdt_model.fit(S)
fodf = sdt_fit.odf(sphere)
directions_gt, _, _ = peak_directions(odf_gt, sphere)
directions, _, _ = peak_directions(fodf, sphere)
ang_sim = angular_similarity(directions_gt, directions)
assert_equal(ang_sim > 1.9, True)
assert_equal(directions.shape[0], 2)
def test_csd_predict():
"""
Test prediction API
"""
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]))
angles = [(0, 0), (60, 0)]
S, sticks = multi_tensor(gtab, mevals, S0, angles=angles,
fractions=[50, 50], snr=SNR)
sphere = small_sphere
odf_gt = multi_tensor_odf(sphere.vertices, mevals, angles, [50, 50])
response = (np.array([0.0015, 0.0003, 0.0003]), S0)
csd = ConstrainedSphericalDeconvModel(gtab, response)
csd_fit = csd.fit(S)
# Predicting from a fit should give the same result as predicting from a
# model, S0 is 1 by default
prediction1 = csd_fit.predict()
prediction2 = csd.predict(csd_fit.shm_coeff)
npt.assert_array_equal(prediction1, prediction2)
npt.assert_array_equal(prediction1[..., gtab.b0s_mask], 1.)
# Same with a different S0
prediction1 = csd_fit.predict(S0=123.)
prediction2 = csd.predict(csd_fit.shm_coeff, S0=123.)
npt.assert_array_equal(prediction1, prediction2)
npt.assert_array_equal(prediction1[..., gtab.b0s_mask], 123.)
# For "well behaved" coefficients, the model should be able to find the
# coefficients from the predicted signal.
coeff = np.random.random(csd_fit.shm_coeff.shape) - .5
coeff[..., 0] = 10.
S = csd.predict(coeff)
csd_fit = csd.fit(S)
npt.assert_array_almost_equal(coeff, csd_fit.shm_coeff)
def test_csd_predict_multi():
"""
Check that we can predict reasonably from multi-voxel fits:
"""
SNR = 100
S0 = 123.
_, fbvals, fbvecs = get_data('small_64D')
bvals = np.load(fbvals)
bvecs = np.load(fbvecs)
gtab = gradient_table(bvals, bvecs)
response = (np.array([0.0015, 0.0003, 0.0003]), S0)
csd = ConstrainedSphericalDeconvModel(gtab, response)
coeff = np.random.random(45) - .5
coeff[..., 0] = 10.
S = csd.predict(coeff, S0=123.)
multi_S = np.array([[S, S], [S, S]])
csd_fit_multi = csd.fit(multi_S)
S0_multi = np.mean(multi_S[..., gtab.b0s_mask], -1)
pred_multi = csd_fit_multi.predict(S0=S0_multi)
npt.assert_array_almost_equal(pred_multi, multi_S)
def test_sphere_scaling_csdmodel():
"""Check that mirroring regularization sphere does not change the result of
the model"""
_, 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]))
angles = [(0, 0), (60, 0)]
S, sticks = multi_tensor(gtab, mevals, 100., angles=angles,
fractions=[50, 50], snr=None)
hemi = small_sphere
sphere = hemi.mirror()
response = (np.array([0.0015, 0.0003, 0.0003]), 100)
model_full = ConstrainedSphericalDeconvModel(gtab, response,
reg_sphere=sphere)
model_hemi = ConstrainedSphericalDeconvModel(gtab, response,
reg_sphere=hemi)
csd_fit_full = model_full.fit(S)
csd_fit_hemi = model_hemi.fit(S)
assert_array_almost_equal(csd_fit_full.shm_coeff, csd_fit_hemi.shm_coeff)
expected_lambda = {4:27.5230088, 8:82.5713865, 16:216.0843135}
def test_default_lambda_csdmodel():
"""We check that the default value of lambda is the expected value with
the symmetric362 sphere. This value has empirically been found to work well
and changes to this default value should be discussed with the dipy team.
"""
sphere = default_sphere
# Create gradient table
_, fbvals, fbvecs = get_data('small_64D')
bvals = np.load(fbvals)
bvecs = np.load(fbvecs)
gtab = gradient_table(bvals, bvecs)
# Some response function
response = (np.array([0.0015, 0.0003, 0.0003]), 100)
for sh_order, expected in expected_lambda.items():
model_full = ConstrainedSphericalDeconvModel(gtab, response,
sh_order=sh_order,
reg_sphere=sphere)
B_reg, _, _ = real_sym_sh_basis(sh_order, sphere.theta, sphere.phi)
npt.assert_array_almost_equal(model_full.B_reg, expected * B_reg)
def test_csd_superres():
""" Check the quality of csdfit with high SH order. """
_, fbvals, fbvecs = get_data('small_64D')
bvals = np.load(fbvals)
bvecs = np.load(fbvecs)
gtab = gradient_table(bvals, bvecs)
# img, gtab = read_stanford_hardi()
evals = np.array([[1.5, .3, .3]]) * [[1.], [1.]] / 1000.
S, sticks = multi_tensor(gtab, evals, snr=None, fractions=[55., 45.])
model16 = ConstrainedSphericalDeconvModel(gtab, (evals[0], 3.), sh_order=16)
fit16 = model16.fit(S)
# print local_maxima(fit16.odf(default_sphere), default_sphere.edges)
d, v, ind = peak_directions(fit16.odf(default_sphere), default_sphere,
relative_peak_threshold=.2,
min_separation_angle=0)
# Check that there are two peaks
assert_equal(len(d), 2)
# Check that peaks line up with sticks
cos_sim = abs((d * sticks).sum(1)) ** .5
assert_(all(cos_sim > .99))
if __name__ == '__main__':
run_module_suite()
|