-
Notifications
You must be signed in to change notification settings - Fork 429
/
canal.py
548 lines (437 loc) · 17.9 KB
/
canal.py
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import numpy as np
from dipy.reconst.cache import Cache
from dipy.reconst.multi_voxel import multi_voxel_fit
from dipy.reconst.shm import real_sph_harm
from dipy.core.gradients import gradient_table
from scipy.special import genlaguerre, gamma, hyp2f1
from dipy.core.geometry import cart2sphere
from math import factorial
class AnalyticalModel(Cache):
def __init__(self, gtab):
r""" Analytical and continuous modeling of the diffusion signal
The main idea is to model the diffusion signal as a linear combination of continuous
functions $\phi_i$,
..math::
:nowrap:
\begin{equation}
S(\mathbf{q})= \sum_{i=0}^I c_{i} \phi_{i}(\mathbf{q}).
\end{equation}
where $\mathbf{q}$ is the wavector which corresponds to different gradient directions.
Numerous continuous functions $\phi_i$ can be used to model $S$. Some are presented in
[1,2,3]_.
From the $c_i$ coefficients, there exist analytical formulae to estimate the ODF.
This is an abstract class, which is used as a template for the implementation
of specific continuous functions classe.
Parameters
----------
gtab : GradientTable,
Gradient directions and bvalues container class
References
----------
.. [1] Merlet S. et. al, "Continuous diffusion signal, EAP and ODF estimation via
Compressive Sensing in diffusion MRI", Medical Image Analysis, 2013.
.. [2] Rathi Y. et. al, "Sparse multi-shell diffusion imaging", MICCAI, 2011.
.. [3] Cheng J. et. al, "Theoretical Analysis and Practical Insights on EAP
Estimation via a Unified HARDI Framework", MICCAI workshop on Computational
Diffusion MRI, 2011.
"""
self.bvals = gtab.bvals
self.bvecs = gtab.bvecs
self.gtab = gtab
@multi_voxel_fit
def fit(self, data):
coef = None
return AnalyticalFit(self, coef)
class AnalyticalFit():
def __init__(self, model, coef):
""" Calculates diffusion properties for a single voxel. This is an abstract class.
Parameters
----------
model : object,
AnalyticalModel
coef : 1d ndarray,
fit coefficients
"""
self.model = model
self.coef = coef
class ShoreModel(AnalyticalModel):
def __init__(self,
gtab,
radialOrder=6,
zeta=700,
lambdaN=1e-8,
lambdaL=1e-8,
tau=1 / (4 * np.pi ** 2)):
r""" Analytical and continuous modeling of the diffusion signal with respect to the SHORE
basis [1,2]_.
The main idea is to model the diffusion signal as a linear combination of continuous
functions $\phi_i$,
..math::
:nowrap:
\begin{equation}
S(\mathbf{q})= \sum_{i=0}^I c_{i} \phi_{i}(\mathbf{q}).
\end{equation}
where $\mathbf{q}$ is the wavector which corresponds to different gradient directions.
From the $c_i$ coefficients, there exists an analytical formula to estimate the ODF.
Parameters
----------
gtab : GradientTable,
Gradient directions and bvalues container class
radialOrder : unsigned int,
Radial Order
zeta : unsigned int,
scale factor
lambdaN : float,
radial regularisation constant
lambdaL : float,
angular regularisation constant
tau : float,
diffusion time. By default the value that makes q=sqrt(b).
References
----------
.. [1] Merlet S. et. al, "Continuous diffusion signal, EAP and ODF estimation via
Compressive Sensing in diffusion MRI", Medical Image Analysis, 2013.
.. [2] Cheng J. et. al, "Theoretical Analysis and Practical Insights on EAP
Estimation via a Unified HARDI Framework", MICCAI workshop on Computational
Diffusion MRI, 2011.
Examples
--------
In this example where we provide the data, a gradient table
and a reconstruction sphere, we model the diffusion signal with respect
to the SHORE basis and compute the real and analytical ODF in terms of
Spherical Harmonics.
from dipy.data import get_data,get_sphere
sphere = get_sphere('symmetric724')
fimg, fbvals, fbvecs = get_data('ISBI_testing_2shells_table')
bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs)
gtab = gradient_table(bvals[1:], bvecs[1:,:])
from dipy.sims.voxel import SticksAndBall
data, golden_directions = SticksAndBall(gtab, d=0.0015,
S0=1, angles=[(0, 0), (90, 0)],
fractions=[50, 50], snr=None)
from dipy.reconst.canal import ShoreModel
radialOrder = 4
zeta = 700
asm = ShoreModel(gtab, radialOrder=radialOrder, zeta=zeta, lambdaN=1e-8, lambdaL=1e-8)
asmfit = asm.fit(data)
odf_sh = asmfit.odf_sh()
"""
self.bvals = gtab.bvals
self.bvecs = gtab.bvecs
self.gtab = gtab
self.radialOrder = radialOrder
self.zeta = zeta
self.lambdaL = lambdaL
self.lambdaN = lambdaN
self.tau = tau
@multi_voxel_fit
def fit(self, data):
Lshore = L_SHORE(self.radialOrder)
Nshore = N_SHORE(self.radialOrder)
# Generate the SHORE basis
M = self.cache_get('shore_matrix', key=self.gtab)
if M is None:
M = SHOREmatrix(self.radialOrder, self.zeta, self.gtab, self.tau)
self.cache_set('shore_matrix', self.gtab, M)
# Compute the signal coefficients in SHORE basis
pseudoInv = np.dot(
np.linalg.inv(np.dot(M.T, M) + self.lambdaN * Nshore + self.lambdaL * Lshore), M.T)
coef = np.dot(pseudoInv, data)
return ShoreFit(self, coef)
class ShoreFit(AnalyticalFit):
def __init__(self, model, shore_coef):
""" Calculates diffusion properties for a single voxel
Parameters
----------
model : object,
AnalyticalModel
shore_coef : 1d ndarray,
shore coefficients
"""
self.model = model
self._shore_coef = shore_coef
self.gtab = model.gtab
self.radialOrder = model.radialOrder
self.zeta = model.zeta
def pdf(self, gridsize, radius_max):
r""" Applies the analytical FFT on $S$ to generate the diffusion propagator.
Parameters
----------
gridsize : unsigned int
dimension of the propagator grid
radius_max : float
maximal radius in which compute the propagator
Returns
-------
Pr : ndarray
the propagator in the 3D grid
psi : ndarray
shore propagator matrix $psi$
"""
Pr = np.zeros((gridsize, gridsize, gridsize))
# Create the grid in wich compute the pdf
rgrid, rtab = create_rspace(gridsize, radius_max)
psi = self.model.cache_get('shore_matrix_pdf', key=gridsize)
if psi is None:
psi = SHOREmatrix_pdf(self.radialOrder, self.zeta, rtab)
self.model.cache_set('shore_matrix_pdf', gridsize, psi)
propagator = np.dot(psi, self._shore_coef)
# fill r-space
for i in range(len(rgrid)):
qx, qy, qz = rgrid[i]
Pr[qx, qy, qz] += propagator[i]
# normalize by the area of the propagator
Pr = Pr * (2 * radius_max / (gridsize - 1)) ** 3
return Pr, psi
def pdf_iso(self, r_points):
""" Diffusion propagator on a given set of r-points.
"""
psi = SHOREmatrix_pdf(self.radialOrder, self.zeta, r_points)
Pr = np.dot(psi, self._shore_coef)
return Pr
def odf_sh(self):
r""" Calculates the real analytical odf in terms of Spherical Harmonics.
"""
# Number of Spherical Harmonics involved in the estimation
J = (self.radialOrder + 1) * (self.radialOrder + 2) / 2
# Compute the spherical Harmonic Coefficients
c_sh = np.zeros(J)
counter = 0
for n in range(self.radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
j = int(l + m + (2 * np.array(range(0, l, 2)) + 1).sum())
Cnl = ((-1) ** (n - l / 2)) / (2.0 * (4.0 * np.pi ** 2 * self.zeta) ** (3.0 / 2.0)) * ((2.0 * (
4.0 * np.pi ** 2 * self.zeta) ** (3.0 / 2.0) * factorial(n - l)) / (gamma(n + 3.0 / 2.0))) ** (1.0 / 2.0)
Gnl = (gamma(l / 2 + 3.0 / 2.0) * gamma(3.0 / 2.0 + n)) / (gamma(
l + 3.0 / 2.0) * factorial(n - l)) * (1.0 / 2.0) ** (-l / 2 - 3.0 / 2.0)
Fnl = hyp2f1(-n + l, l / 2 + 3.0 / 2.0, l + 3.0 / 2.0, 2.0)
c_sh[j] += self._shore_coef[counter] * Cnl * Gnl * Fnl
counter += 1
return c_sh
def odf(self, sphere):
r""" Calculates the real analytical odf for a given discrete sphere.
"""
upsilon = self.model.cache_get('shore_matrix_odf', key=sphere)
if upsilon is None:
upsilon = SHOREmatrix_odf(
self.radialOrder, self.zeta, sphere.vertices)
self.model.cache_set('shore_matrix_odf', sphere, upsilon)
odf = np.dot(upsilon, self._shore_coef)
return odf
def rtop_signal(self):
r""" Calculates the analytical return to origin probability from the signal.
"""
rtop = 0
c = self._shore_coef
counter = 0
for n in range(self.radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
if l == 0:
rtop += c[counter] * (-1) ** n * \
((16 * np.pi * self.zeta ** 1.5 * gamma(n + 1.5)) / (
factorial(n))) ** 0.5
counter += 1
return rtop
def rtop_pdf(self):
r""" Calculates the analytical return to origin probability from the pdf.
"""
rtop = 0
c = self._shore_coef
counter = 0
for n in range(self.radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
if l == 0:
rtop += c[counter] * (-1) ** n * \
((4 * np.pi ** 2 * self.zeta ** 1.5 * factorial(n)) / (gamma(n + 1.5))) ** 0.5 * \
genlaguerre(n, 0.5)(0)
counter += 1
return rtop
def msd(self):
r""" Calculates the analytical mean squared displacement
..math::
:nowrap:
\begin{equation}
MSD:{DSI}=\int_{-\infty}^{\infty}\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} P(\hat{\mathbf{r}}) \cdot \hat{\mathbf{r}}^{2} \ dr_x \ dr_y \ dr_z
\end{equation}
where $\hat{\mathbf{r}}$ is a point in the 3D Propagator space (see Wu et. al [1]_).
References
----------
.. [1] Wu Y. et. al, "Hybrid diffusion imaging", NeuroImage, vol 36,
p. 617-629, 2007.
"""
msd = 0
c = self._shore_coef
counter = 0
for n in range(self.radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
if l == 0:
msd += c[counter] * (-1) ** n *\
(9 * (gamma(n + 1.5)) / (8 * np.pi ** 6 * self.zeta ** 3.5 * factorial(n))) ** 0.5 *\
hyp2f1(-n, 2.5, 1.5, 2)
counter += 1
return msd
@property
def shore_coeff(self):
"""The SHORE coefficients
"""
return self._shore_coef
def SHOREmatrix(radialOrder, zeta, gtab, tau=1 / (4 * np.pi ** 2)):
"""Compute the SHORE matrix"
Parameters
----------
radialOrder : unsigned int,
Radial Order
zeta : unsigned int,
scale factor
gtab : GradientTable,
Gradient directions and bvalues container class
tau : float,
diffusion time. By default the value that makes q=sqrt(b).
"""
qvals = np.sqrt(gtab.bvals / (4 * np.pi ** 2 * tau))
bvecs = gtab.bvecs
qgradients = qvals[:, None] * bvecs
r, theta, phi = cart2sphere(
qgradients[:, 0], qgradients[:, 1], qgradients[:, 2])
theta[np.isnan(theta)] = 0
M = np.zeros(
(r.shape[0], (radialOrder + 1) * ((radialOrder + 1) / 2) * (2 * radialOrder + 1)))
counter = 0
for n in range(radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
M[:, counter] = real_sph_harm(m, l, theta, phi) * \
genlaguerre(n - l, l + 0.5)(r ** 2 / float(zeta)) * \
np.exp(- r ** 2 / (2.0 * zeta)) * \
__kappa(zeta, n, l) * \
(r ** 2 / float(zeta)) ** (l / 2)
counter += 1
return M[:, 0:counter]
def __kappa(zeta, n, l):
if n - l < 0:
return np.sqrt((2 * 1) / (zeta ** 1.5 * gamma(n + 1.5)))
else:
return np.sqrt((2 * factorial(n - l)) / (zeta ** 1.5 * gamma(n + 1.5)))
def SHOREmatrix_pdf(radialOrder, zeta, rtab):
"""Compute the SHORE matrix"
Parameters
----------
radialOrder : unsigned int,
Radial Order
zeta : unsigned int,
scale factor
rtab : array, shape (N,3)
r-space points in which calculates the pdf
"""
r, theta, phi = cart2sphere(
rtab[:, 0], rtab[:, 1], rtab[:, 2])
theta[np.isnan(theta)] = 0
psi = np.zeros(
(r.shape[0], (radialOrder + 1) * ((radialOrder + 1) / 2) * (2 * radialOrder + 1)))
counter = 0
for n in range(radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
psi[:, counter] = real_sph_harm(m, l, theta, phi) * \
genlaguerre(n - l, l + 0.5)(4 * np.pi ** 2 * zeta * r ** 2 ) *\
np.exp(-2 * np.pi ** 2 * zeta * r ** 2) *\
__kappa_pdf(zeta, n, l) *\
(4 * np.pi ** 2 * zeta * r ** 2) ** (l / 2) * \
(-1) ** (n - l / 2)
counter += 1
return psi[:, 0:counter]
def __kappa_pdf(zeta, n, l):
if n - l < 0:
return np.sqrt((16 * np.pi ** 3 * zeta ** 1.5) / gamma(n + 1.5))
else:
return np.sqrt((16 * np.pi ** 3 * zeta ** 1.5 * factorial(n - l)) / gamma(n + 1.5))
def SHOREmatrix_odf(radialOrder, zeta, sphere_vertices):
"""Compute the SHORE matrix"
Parameters
----------
radialOrder : unsigned int,
Radial Order
zeta : unsigned int,
scale factor
sphere_vertices : array, shape (N,3)
vertices of the odf sphere
"""
r, theta, phi = cart2sphere(sphere_vertices[:, 0], sphere_vertices[:, 1], sphere_vertices[:, 2])
theta[np.isnan(theta)] = 0
counter = 0
upsilon = np.zeros(
(len(sphere_vertices), (radialOrder + 1) * ((radialOrder + 1) / 2) * (2 * radialOrder + 1)))
for n in range(radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
upsilon[:, counter] = (-1) ** (n - l / 2.0) * __kappa_odf(zeta, n, l) * \
hyp2f1(l - n, l / 2.0 + 1.5, l + 1.5, 2.0) * \
real_sph_harm(m, l, theta, phi)
counter += 1
return upsilon[:, 0:counter]
def __kappa_odf(zeta, n, l):
if n - l < 0:
return np.sqrt((gamma(l / 2.0 + 1.5) ** 2 * gamma(n + 1.5) * 2 ** (l + 3)) /
(16 * np.pi ** 3 * (zeta) ** 1.5 * gamma(l + 1.5) ** 2))
else:
return np.sqrt((gamma(l / 2.0 + 1.5) ** 2 * gamma(n + 1.5) * 2 ** (l + 3)) /
(16 * np.pi ** 3 * (zeta) ** 1.5 * factorial(n - l) * gamma(l + 1.5) ** 2))
def L_SHORE(radialOrder):
"Returns the angular regularisation matrix for SHORE basis"
diagL = np.zeros(
(radialOrder + 1) * ((radialOrder + 1) / 2) * (2 * radialOrder + 1))
counter = 0
for n in range(radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
# print(counter)
# print "(n,l,m) = (%d,%d,%d)" % (n,l,m)
# print(counter)
diagL[counter] = (l * (l + 1)) ** 2
counter += 1
return np.diag(diagL[0:counter])
def N_SHORE(radialOrder):
"Returns the angular regularisation matrix for SHORE basis"
diagN = np.zeros(
(radialOrder + 1) * ((radialOrder + 1) / 2) * (2 * radialOrder + 1))
counter = 0
for n in range(radialOrder + 1):
for l in range(0, n + 1, 2):
for m in range(-l, l + 1):
# print(counter)
# print "(n,l,m) = (%d,%d,%d)" % (n,l,m)
# print(counter)
diagN[counter] = (n * (n + 1)) ** 2
counter += 1
return np.diag(diagN[0:counter])
def create_rspace(gridsize, radius_max):
""" create the r-space table, that contains the points in which
compute the pdf.
Parameters
----------
gridsize : unsigned int
dimension of the propagator grid
radius_max : float
maximal radius in which compute the propagator
Returns
-------
vecs : array, shape (N,3)
positions of the pdf points in a 3D matrix
tab : array, shape (N,3)
r-space points in which calculates the pdf
"""
radius = gridsize // 2
vecs = []
for i in range(-radius, radius + 1):
for j in range(-radius, radius + 1):
for k in range(-radius, radius + 1):
vecs.append([i, j, k])
vecs = np.array(vecs, dtype=np.float32)
tab = vecs / float(radius)
tab = tab * radius_max
vecs = vecs + radius
return vecs, tab