qmri.diffusion¶
Diffusion-weighted imaging (DWI) signal models and fitting.
ADC Fitting¶
adc ¶
Apparent Diffusion Coefficient (ADC) fitting.
This module provides functions for fitting ADC from diffusion-weighted MRI data using various least squares methods.
Example
References
.. [1] Veraart, J., et al. (2013). "Weighted linear least squares estimation of diffusion MRI parameters: Strengths, limitations, and pitfalls." NeuroImage 81:335-346. .. [2] Basser, P.J., Mattiello, J., Le Bihan, D. (1994). "Estimation of the effective self-diffusion tensor from the NMR spin echo." J Magn Reson B 103(3):247-254.
ADCResult
dataclass
¶
Result of ADC fitting for a single voxel or signal.
Attributes:
| Name | Type | Description |
|---|---|---|
adc |
float
|
Apparent Diffusion Coefficient in mm²/s. |
s0 |
float
|
Baseline signal intensity (b=0). |
r_squared |
float
|
Coefficient of determination (0 to 1). Higher values indicate better fit quality. |
iterations |
int | None
|
Number of iterations performed (only for IWLLS method). |
ADCMapResult
dataclass
¶
fit ¶
fit(
signal: NDArray[floating],
b_values: NDArray[floating],
*,
method: FittingMethod = "iwlls",
mask: NDArray[bool_] | None = None,
max_iterations: int = 10,
tolerance: float = 1e-06,
) -> ADCResult | ADCMapResult
Fit ADC from diffusion-weighted signal data.
This is the main entry point for ADC fitting. It automatically handles both single-voxel signals (1D) and multi-dimensional volumes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
NDArray[floating]
|
Signal intensities. For single voxel: 1D array of length n_bvalues. For volumes: ND array where the last dimension is n_bvalues. |
required |
b_values
|
NDArray[floating]
|
Diffusion weighting values in s/mm². Length must match last dimension of signal. |
required |
method
|
FittingMethod
|
Fitting method. Default is "iwlls" (recommended). - "lls": Linear Least Squares (fastest, least accurate) - "wlls": Weighted Linear Least Squares - "iwlls": Iterative Weighted Linear Least Squares (recommended) |
'iwlls'
|
mask
|
NDArray[bool_] | None
|
Binary mask for volume processing. Only voxels where mask is True will be fitted. Shape must match signal shape excluding last dimension. |
None
|
max_iterations
|
int
|
Maximum iterations for IWLLS. Default is 10. |
10
|
tolerance
|
float
|
Convergence tolerance for IWLLS. Default is 1e-6. |
1e-06
|
Returns:
| Type | Description |
|---|---|
ADCResult | ADCMapResult
|
For 1D input: ADCResult with scalar values. |
ADCResult | ADCMapResult
|
For ND input: ADCMapResult with arrays matching input spatial dims. |
Example
Single voxel fitting:
import numpy as np
from qmri.diffusion import adc
b_values = np.array([0, 500, 1000, 2000])
signal = np.array([1000, 606, 368, 135])
result = adc.fit(signal, b_values)
print(f"ADC: {result.adc:.2e} mm²/s, R²: {result.r_squared:.3f}")
Volume fitting:
Source code in packages/qmri/src/qmri/diffusion/adc.py
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 | |
fit_lls ¶
Fit ADC using Linear Least Squares (LLS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
NDArray[floating]
|
Signal intensities at each b-value. Must be positive. |
required |
b_values
|
NDArray[floating]
|
Diffusion weighting values in s/mm². |
required |
Returns:
| Type | Description |
|---|---|
ADCResult
|
Fitted ADC, S0, and R² quality metric. |
Notes
Implements the standard LLS estimator [1]_:
where \(\mathbf{y} = \ln(\mathbf{S})\) and design matrix \(\mathbf{X} = [\mathbf{1}, -\mathbf{b}]\).
The method assumes SNR > 2 for unbiased estimation [2]_.
References
.. [1] Basser, P.J., et al. (1994). J Magn Reson B 103(3):247-254. .. [2] Salvador, R., et al. (2005). Hum Brain Mapp 24(2):144-155.
Source code in packages/qmri/src/qmri/diffusion/adc.py
fit_wlls ¶
Fit ADC using Weighted Linear Least Squares (WLLS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
NDArray[floating]
|
Signal intensities at each b-value. Must be positive. |
required |
b_values
|
NDArray[floating]
|
Diffusion weighting values in s/mm². |
required |
Returns:
| Type | Description |
|---|---|
ADCResult
|
Fitted ADC, S0, and R² quality metric. |
Notes
Implements the WLLS2 estimator from Veraart et al. (2013) [1]_:
where \(\mathbf{W} = \text{diag}(\exp(2\mathbf{X}\hat{\beta}_{LLS}))\).
This approach uses predicted signals from an initial LLS fit for weights, which provides better accuracy than using noisy measured signals.
References
.. [1] Veraart, J., et al. (2013). NeuroImage 81:335-346.
Source code in packages/qmri/src/qmri/diffusion/adc.py
fit_iwlls ¶
fit_iwlls(
signal: NDArray[floating],
b_values: NDArray[floating],
*,
max_iterations: int = 10,
tolerance: float = 1e-06,
) -> ADCResult
Fit ADC using Iterative Weighted Linear Least Squares (IWLLS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
NDArray[floating]
|
Signal intensities at each b-value. Must be positive. |
required |
b_values
|
NDArray[floating]
|
Diffusion weighting values in s/mm². |
required |
max_iterations
|
int
|
Maximum number of iterations. Default is 10. |
10
|
tolerance
|
float
|
Convergence tolerance for ADC change. Default is 1e-6. |
1e-06
|
Returns:
| Type | Description |
|---|---|
ADCResult
|
Fitted ADC, S0, R² quality metric, and iteration count. |
Notes
Implements the iterative WLLS from Veraart et al. (2013) [1]_:
The algorithm iteratively refines weights until convergence. Typically converges in 2-3 iterations.
References
.. [1] Veraart, J., et al. (2013). NeuroImage 81:335-346.
Source code in packages/qmri/src/qmri/diffusion/adc.py
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 | |
signal_model ¶
signal_model(
s0: NDArray[floating] | float,
adc: NDArray[floating] | float,
b_values: NDArray[floating],
) -> NDArray[floating]
Generate DWI signal using the mono-exponential diffusion model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s0
|
NDArray[floating] | float
|
Baseline signal intensity (at b=0). |
required |
adc
|
NDArray[floating] | float
|
Apparent Diffusion Coefficient in mm²/s. |
required |
b_values
|
NDArray[floating]
|
Diffusion weighting values in s/mm². |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Predicted signal at each b-value. |
Notes
Implements the Stejskal-Tanner equation: