-
Notifications
You must be signed in to change notification settings - Fork 30
/
sphere_models.py
503 lines (435 loc) · 17.3 KB
/
sphere_models.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
from ..core.modeling_framework import ModelProperties
from ..core.constants import CONSTANTS
from scipy import special
import numpy as np
DIAMETER_SCALING = 1e-6
__all__ = [
'S1Dot',
'S2SphereStejskalTannerApproximation'
]
class S1Dot(ModelProperties):
r"""
The Dot model [1]_ - an non-diffusing compartment.
It has no parameters and returns 1 no matter the input.
References
----------
.. [1] Panagiotaki et al.
"Compartment models of the diffusion MR signal in brain white
matter: a taxonomy and comparison". NeuroImage (2012)
"""
_parameter_ranges = {
}
_parameter_scales = {
}
_parameter_types = {
}
_model_type = 'CompartmentModel'
def __call__(self, acquisition_scheme, **kwargs):
r'''
Calculates the signal attenation.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
attenuation : float or array, shape(N),
signal attenuation
'''
E_dot = np.ones(acquisition_scheme.number_of_measurements)
return E_dot
def rotational_harmonics_representation(
self, acquisition_scheme, **kwargs):
r""" The rotational harmonics of the model, such that Y_lm = Yl0.
Axis aligned with z-axis to be used as kernel for spherical
convolution. Returns an array with rotational harmonics for each shell.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
rh_array : array, shape(Nshells, N_rh_coef),
Rotational harmonics coefficients for each shell.
"""
rh_scheme = acquisition_scheme.rotational_harmonics_scheme
kwargs.update({'mu': [0., 0.]})
E_kernel_sf = self(rh_scheme, **kwargs)
E_reshaped = E_kernel_sf.reshape([-1, rh_scheme.Nsamples])
rh_array = np.zeros((len(E_reshaped), 1))
for i, sh_order in enumerate(rh_scheme.shell_sh_orders):
rh_array[i, :sh_order // 2 + 1] = (
np.dot(
rh_scheme.inverse_rh_matrix[0],
E_reshaped[i])
)
return rh_array
def spherical_mean(self, acquisition_scheme, **kwargs):
"""
Estimates spherical mean for every shell in acquisition scheme.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
E_mean : float,
spherical mean of the model for every acquisition shell.
"""
return self(acquisition_scheme.spherical_mean_scheme, **kwargs)
class S2SphereStejskalTannerApproximation(ModelProperties):
r"""
The Stejskal Tanner signal approximation of a sphere model. It assumes
that pulse length is infinitessimally small and diffusion time large enough
so that the diffusion is completely restricted. Only depends on q-value.
Parameters
----------
diameter : float,
sphere diameter in meters.
References
----------
.. [1] Balinov, Balin, et al. "The NMR self-diffusion method applied to
restricted diffusion. Simulation of echo attenuation from molecules in
spheres and between planes." Journal of Magnetic Resonance, Series A
104.1 (1993): 17-25.
"""
_parameter_ranges = {
'diameter': (1e-2, 20)
}
_parameter_scales = {
'diameter': DIAMETER_SCALING
}
_parameter_types = {
'diameter': 'sphere',
}
_model_type = 'CompartmentModel'
def __init__(self, diameter=None):
self.diameter = diameter
def sphere_attenuation(self, q, diameter):
"The signal attenuation for the sphere model."
radius = diameter / 2
factor = 2 * np.pi * q * radius
E = (
3 / (factor ** 2) *
(
np.sin(factor) / factor -
np.cos(factor)
)
) ** 2
return E
def __call__(self, acquisition_scheme, **kwargs):
r'''
Calculates the signal attenation.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
attenuation : float or array, shape(N),
signal attenuation
'''
q = acquisition_scheme.qvalues
diameter = kwargs.get('diameter', self.diameter)
E_sphere = np.ones_like(q)
q_nonzero = q > 0 # only q>0 attenuate
E_sphere[q_nonzero] = self.sphere_attenuation(
q[q_nonzero], diameter)
return E_sphere
def rotational_harmonics_representation(
self, acquisition_scheme, **kwargs):
r""" The rotational harmonics of the model, such that Y_lm = Yl0.
Axis aligned with z-axis to be used as kernel for spherical
convolution. Returns an array with rotational harmonics for each shell.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
rh_array : array, shape(Nshells, N_rh_coef),
Rotational harmonics coefficients for each shell.
"""
rh_scheme = acquisition_scheme.rotational_harmonics_scheme
kwargs.update({'mu': [0., 0.]})
E_kernel_sf = self(rh_scheme, **kwargs)
E_reshaped = E_kernel_sf.reshape([-1, rh_scheme.Nsamples])
rh_array = np.zeros((len(E_reshaped), 1))
for i, sh_order in enumerate(rh_scheme.shell_sh_orders):
rh_array[i, :sh_order // 2 + 1] = (
np.dot(
rh_scheme.inverse_rh_matrix[0],
E_reshaped[i])
)
return rh_array
def spherical_mean(self, acquisition_scheme, **kwargs):
"""
Estimates spherical mean for every shell in acquisition scheme.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
E_mean : float,
spherical mean of the model for every acquisition shell.
"""
return self(acquisition_scheme.spherical_mean_scheme, **kwargs)
class _S3SphereCallaghanApproximation(ModelProperties):
r"""
The Callaghan model [1]_ of diffusion inside a sphere.
Parameters
----------
diameter : float
Distance between the two plates in meters.
diffusion_constant : float,
The diffusion constant of the water particles between the two planes.
The default value is the approximate diffusivity of water inside axons
as 1.7e-9 m^2/s.
number_of_roots : integer,
The number of roots for the Callaghan approximation.
References
----------
[1] Callaghan, "Pulsed-Gradient Spin-Echo NMR for Planar, Cylindrical,
and Spherical Pores under Conditions of Wall Relaxation", JMR 1995
"""
_parameter_ranges = {
'diameter': (1e-2, 20)
}
_parameter_scales = {
'diameter': DIAMETER_SCALING
}
_parameter_types = {
'diameter': 'sphere'
}
_model_type = 'CompartmentModel'
def __init__(
self,
diameter=None,
diffusion_constant=CONSTANTS['water_in_axons_diffusion_constant'],
number_of_roots=20,
number_of_functions=50,
):
self.diameter = diameter
self.Dintra = diffusion_constant
self.alpha = np.empty((number_of_roots, number_of_functions))
self.alpha[0, 0] = 0
if number_of_roots > 1:
self.alpha[1:, 0] = special.jnp_zeros(0, number_of_roots - 1)
for m in range(1, number_of_functions):
self.alpha[:, m] = special.jnp_zeros(m, number_of_roots)
def sphere_attenuation(self, q, tau, diameter):
"""Implements the finite time Callaghan model for planes."""
radius = diameter / 2.0
q_argument = 2 * np.pi * q * radius
q_argument_2 = q_argument ** 2
res = np.zeros_like(q)
# J = special.spherical_jn(q_argument)
Jder = special.spherical_jn(q_argument, derivative=True)
for k in range(0, self.alpha.shape[0]):
for n in range(0, self.alpha.shape[1]):
a_nk2 = self.alpha[k, n] ** 2
update = np.exp(-a_nk2 * self.Dintra * tau / radius ** 2)
update *= ((2 * n + 1) * a_nk2) / \
(a_nk2 - (n - 0.5) ** 2 + 0.25)
update *= q_argument * Jder
update /= (q_argument_2 - a_nk2) ** 2
res += update
return res
def __call__(self, acquisition_scheme, **kwargs):
r'''
Calculates the signal attenuation.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
attenuation : float or array, shape(N),
signal attenuation
'''
q = acquisition_scheme.qvalues
tau = acquisition_scheme.tau
diameter = kwargs.get('diameter', self.diameter)
E_sphere = np.ones_like(q)
q_nonzero = q > 0
E_sphere[q_nonzero] = self.sphere_attenuation(
q[q_nonzero], tau[q_nonzero], diameter
)
return E_sphere
class S4SphereGaussianPhaseApproximation(ModelProperties):
r"""
The gaussian phase approximation for diffusion inside a sphere according
to [1]_. It is dependent on gradient strength, pulse separation and pulse
length.
References
----------
.. [1] Balinov, Balin, et al. "The NMR self-diffusion method applied to
restricted diffusion. Simulation of echo attenuation from molecules in
spheres and between planes." Journal of Magnetic Resonance, Series A
104.1 (1993): 17-25.
"""
_parameter_ranges = {
'diameter': (1e-2, 20)
}
_parameter_scales = {
'diameter': DIAMETER_SCALING
}
_parameter_types = {
'diameter': 'sphere'
}
_model_type = 'CompartmentModel'
# According to Balinov et al., solutions of
# 1/(alpha * R) * J(3/2,alpha * R) = J(5/2, alpha * R)
# with R = 1 with alpha * R < 100 * pi
SPHERE_TRASCENDENTAL_ROOTS = np.r_[
# 0.,
2.081575978, 5.940369990, 9.205840145,
12.40444502, 15.57923641, 18.74264558, 21.89969648,
25.05282528, 28.20336100, 31.35209173, 34.49951492,
37.64596032, 40.79165523, 43.93676147, 47.08139741,
50.22565165, 53.36959180, 56.51327045, 59.65672900,
62.80000055, 65.94311190, 69.08608495, 72.22893775,
75.37168540, 78.51434055, 81.65691380, 84.79941440,
87.94185005, 91.08422750, 94.22655255, 97.36883035,
100.5110653, 103.6532613, 106.7954217, 109.9375497,
113.0796480, 116.2217188, 119.3637645, 122.5057870,
125.6477880, 128.7897690, 131.9317315, 135.0736768,
138.2156061, 141.3575204, 144.4994207, 147.6413080,
150.7831829, 153.9250463, 157.0668989, 160.2087413,
163.3505741, 166.4923978, 169.6342129, 172.7760200,
175.9178194, 179.0596116, 182.2013968, 185.3431756,
188.4849481, 191.6267147, 194.7684757, 197.9102314,
201.0519820, 204.1937277, 207.3354688, 210.4772054,
213.6189378, 216.7606662, 219.9023907, 223.0441114,
226.1858287, 229.3275425, 232.4692530, 235.6109603,
238.7526647, 241.8943662, 245.0360648, 248.1777608,
251.3194542, 254.4611451, 257.6028336, 260.7445198,
263.8862038, 267.0278856, 270.1695654, 273.3112431,
276.4529189, 279.5945929, 282.7362650, 285.8779354,
289.0196041, 292.1612712, 295.3029367, 298.4446006,
301.5862631, 304.7279241, 307.8695837, 311.0112420,
314.1528990
]
def __init__(
self, diameter=None,
diffusion_constant=CONSTANTS['water_in_axons_diffusion_constant'],
):
self.diffusion_constant = diffusion_constant
self.gyromagnetic_ratio = CONSTANTS['water_gyromagnetic_ratio']
self.diameter = diameter
def sphere_attenuation(
self, gradient_strength, delta, Delta, diameter
):
"Calculates the sphere signal attenuation."
D = self.diffusion_constant
gamma = self.gyromagnetic_ratio
radius = diameter / 2
alpha = self.SPHERE_TRASCENDENTAL_ROOTS / radius
alpha2 = alpha ** 2
alpha2D = alpha2 * D
first_factor = -2 * (gamma * gradient_strength) ** 2 / D
summands = (
alpha ** (-4) / (alpha2 * radius ** 2 - 2) *
(
2 * delta - (
2 +
np.exp(-alpha2D * (Delta - delta)) -
2 * np.exp(-alpha2D * delta) -
2 * np.exp(-alpha2D * Delta) +
np.exp(-alpha2D * (Delta + delta))
) / (alpha2D)
)
)
E = np.exp(
first_factor *
summands.sum()
)
return E
def __call__(self, acquisition_scheme, **kwargs):
r'''
Calculates the signal attenation.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
attenuation : float or array, shape(N),
signal attenuation
'''
g = acquisition_scheme.gradient_strengths
delta = acquisition_scheme.delta
Delta = acquisition_scheme.Delta
diameter = kwargs.get('diameter', self.diameter)
E_sphere = np.ones_like(g)
g_nonzero = g > 0
# for every unique combination get the perpendicular attenuation
unique_deltas = np.unique([acquisition_scheme.shell_delta,
acquisition_scheme.shell_Delta], axis=1)
for delta_, Delta_ in zip(*unique_deltas):
mask = np.all([g_nonzero, delta == delta_, Delta == Delta_],
axis=0)
E_sphere[mask] = self.sphere_attenuation(
g[mask], delta_, Delta_, diameter
)
return E_sphere
def rotational_harmonics_representation(
self, acquisition_scheme, **kwargs):
r""" The rotational harmonics of the model, such that Y_lm = Yl0.
Axis aligned with z-axis to be used as kernel for spherical
convolution. Returns an array with rotational harmonics for each shell.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
rh_array : array, shape(Nshells, N_rh_coef),
Rotational harmonics coefficients for each shell.
"""
rh_scheme = acquisition_scheme.rotational_harmonics_scheme
kwargs.update({'mu': [0., 0.]})
E_kernel_sf = self(rh_scheme, **kwargs)
E_reshaped = E_kernel_sf.reshape([-1, rh_scheme.Nsamples])
rh_array = np.zeros((len(E_reshaped), 1))
for i, sh_order in enumerate(rh_scheme.shell_sh_orders):
rh_array[i, :sh_order // 2 + 1] = (
np.dot(
rh_scheme.inverse_rh_matrix[0],
E_reshaped[i])
)
return rh_array
def spherical_mean(self, acquisition_scheme, **kwargs):
"""
Estimates spherical mean for every shell in acquisition scheme.
Parameters
----------
acquisition_scheme : DmipyAcquisitionScheme instance,
An acquisition scheme that has been instantiated using dMipy.
kwargs: keyword arguments to the model parameter values,
Is internally given as **parameter_dictionary.
Returns
-------
E_mean : float,
spherical mean of the model for every acquisition shell.
"""
return self(acquisition_scheme.spherical_mean_scheme, **kwargs)