mrtrix
lacuna.utils.mrtrix
¶
MRtrix3 command wrappers for tractography-based network mapping.
Provides Python wrappers around MRtrix3 commands (tckedit, tckmap, mrcalc) for filtering tractograms by masks and computing disconnection maps.
MRtrixError
¶
check_mrtrix_available()
¶
Check if MRtrix3 commands are available in the system PATH.
Returns:
| Type | Description |
|---|---|
bool
|
True if all required MRtrix3 commands are available |
Raises:
| Type | Description |
|---|---|
MRtrixError
|
If MRtrix3 is not installed or commands are not in PATH |
Examples:
>>> from lacuna.utils.mrtrix import check_mrtrix_available
>>> try:
... check_mrtrix_available()
... print("MRtrix3 is available")
... except MRtrixError as e:
... print(f"MRtrix3 not available: {e}")
Source code in src/lacuna/utils/mrtrix.py
compute_disconnection_map(mask_tdi, whole_brain_tdi, output_path=None, force=False, verbose=False)
¶
Compute disconnection map as ratio of mask TDI to whole-brain TDI.
Uses MRtrix3's mrcalc to divide mask TDI by whole-brain TDI, producing a voxel-wise disconnection probability map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_tdi
|
str, Path, or nibabel.Nifti1Image
|
TDI from mask-filtered tractogram |
required |
whole_brain_tdi
|
str, Path, or nibabel.Nifti1Image
|
TDI from whole-brain tractogram (reference) |
required |
output_path
|
str, Path, or None
|
Output path for disconnection map. If None, creates temp file. |
None
|
force
|
bool
|
Overwrite existing output file |
False
|
verbose
|
bool
|
If True, prints MRtrix3 commands being executed |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to disconnection map file |
Raises:
| Type | Description |
|---|---|
MRtrixError
|
If MRtrix3 command fails |
FileNotFoundError
|
If input files don't exist |
Examples:
>>> from lacuna.utils.mrtrix import compute_disconnection_map
>>> disconn_map = compute_disconnection_map(
... mask_tdi="mask_tdi.nii.gz",
... whole_brain_tdi="whole_brain_tdi.nii.gz",
... output_path="disconnection_map.nii.gz"
... )
Notes
- Values range from 0 (no disconnection) to 1 (complete disconnection)
- The output is limited to white matter regions where the tractogram has streamlines
- Voxels with zero whole-brain TDI (outside white matter) are set to 0
Source code in src/lacuna/utils/mrtrix.py
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 | |
compute_tdi_map(tractogram_path, template, output_path=None, n_jobs=1, force=False, verbose=False)
¶
Compute Track Density Image (TDI) from a tractogram.
Uses MRtrix3's tckmap to create a voxel-wise count of streamline endpoints or trajectories passing through each voxel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tractogram_path
|
str or Path
|
Path to tractogram (.tck file) |
required |
template
|
str, Path, or nibabel.Nifti1Image
|
Template image defining output grid, or path to NIfTI file |
required |
output_path
|
str, Path, or None
|
Output path for TDI map. If None, creates temp file. |
None
|
n_jobs
|
int
|
Number of threads for MRtrix3 to use |
1
|
force
|
bool
|
Overwrite existing output file |
False
|
verbose
|
bool
|
If True, prints MRtrix3 commands being executed |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to TDI map file |
Raises:
| Type | Description |
|---|---|
MRtrixError
|
If MRtrix3 command fails |
FileNotFoundError
|
If input files don't exist |
Examples:
>>> from lacuna.utils.mrtrix import compute_tdi_map
>>> tdi = compute_tdi_map(
... tractogram_path="filtered_streamlines.tck",
... template="MNI152_T1_2mm.nii.gz",
... output_path="tdi_map.nii.gz",
... n_jobs=8
... )
Notes
- Template defines the output grid resolution and FOV
- Uses -contrast tdi for standard track density imaging
Source code in src/lacuna/utils/mrtrix.py
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 | |
compute_whole_brain_tdi(tractogram_path, output_1mm=None, output_2mm=None, n_jobs=1, force=False)
¶
Compute whole-brain Track Density Images (TDI) at 1mm and/or 2mm MNI resolution.
Convenience function that computes TDI maps from a whole-brain tractogram using bundled MNI152 templates at different resolutions. This generates the reference connectivity maps needed for disconnection analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tractogram_path
|
str or Path
|
Path to whole-brain tractogram (.tck file) |
required |
output_1mm
|
str, Path, or None
|
Output path for 1mm TDI map. If None, 1mm TDI is not computed. |
None
|
output_2mm
|
str, Path, or None
|
Output path for 2mm TDI map. If None, 2mm TDI is not computed. |
None
|
n_jobs
|
int
|
Number of threads for MRtrix3 to use |
1
|
force
|
bool
|
Overwrite existing output files |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with keys "tdi_1mm" and "tdi_2mm" containing Path objects to the computed TDI maps, or None if not computed. |
Raises:
| Type | Description |
|---|---|
MRtrixError
|
If MRtrix3 command fails |
FileNotFoundError
|
If tractogram file doesn't exist |
ValueError
|
If neither output_1mm nor output_2mm is specified |
Examples:
>>> from lacuna.utils.mrtrix import compute_whole_brain_tdi
>>>
>>> # Compute both resolutions
>>> tdis = compute_whole_brain_tdi(
... tractogram_path="whole_brain.tck",
... output_1mm="tdi_1mm.nii.gz",
... output_2mm="tdi_2mm.nii.gz",
... n_jobs=8
... )
>>> print(f"1mm TDI: {tdis['tdi_1mm']}")
>>> print(f"2mm TDI: {tdis['tdi_2mm']}")
>>>
>>> # Compute only 2mm resolution
>>> tdis = compute_whole_brain_tdi(
... tractogram_path="whole_brain.tck",
... output_2mm="tdi_2mm.nii.gz",
... n_jobs=8
... )
Notes
- Uses bundled FSL MNI152 T1 templates (1mm: 182×218×182, 2mm: 91×109×91)
- Processing time scales with tractogram size (typically 5-30 minutes)
- Output files can be large (hundreds of MB for dense tractograms)
- These TDI maps serve as reference connectivity for disconnection analysis
See Also
compute_tdi_map : Lower-level function for custom TDI computation StructuralNetworkMapping : Analysis that uses whole-brain TDI as reference
Source code in src/lacuna/utils/mrtrix.py
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 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
filter_tractogram_by_mask(tractogram_path, mask, output_path=None, n_jobs=1, force=False, verbose=False)
¶
Filter a whole-brain tractogram to streamlines passing through a mask.
Uses MRtrix3's tckedit with -include to extract only streamlines that intersect with the mask region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tractogram_path
|
str or Path
|
Path to whole-brain tractogram (.tck file) |
required |
mask
|
str, Path, or nibabel.Nifti1Image
|
Binary mask image or path to NIfTI file |
required |
output_path
|
str, Path, or None
|
Output path for filtered tractogram. If None, creates temp file. |
None
|
n_jobs
|
int
|
Number of threads for MRtrix3 to use |
1
|
force
|
bool
|
Overwrite existing output file |
False
|
verbose
|
bool
|
If True, prints MRtrix3 commands being executed |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to filtered tractogram file |
Raises:
| Type | Description |
|---|---|
MRtrixError
|
If MRtrix3 command fails |
FileNotFoundError
|
If input files don't exist |
Examples:
>>> from lacuna.utils.mrtrix import filter_tractogram_by_mask
>>> filtered_tck = filter_tractogram_by_mask(
... tractogram_path="whole_brain.tck",
... mask="roi_mask.nii.gz",
... output_path="filtered_streamlines.tck",
... n_jobs=8
... )
Notes
- Input tractogram must be in same coordinate space as mask (typically MNI152)
- Output is a .tck file containing only streamlines passing through mask
- For large tractograms, this operation can be slow (minutes to hours)
Source code in src/lacuna/utils/mrtrix.py
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 | |
run_mrtrix_command(command, check=True, capture_output=False, verbose=False)
¶
Execute an MRtrix3 command with proper error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
list of str
|
Command and arguments to execute |
required |
check
|
bool
|
If True, raises subprocess.CalledProcessError on non-zero exit |
True
|
capture_output
|
bool
|
If True, captures stdout and stderr |
False
|
verbose
|
bool
|
If True, prints the command being executed and allows output to display. If False, suppresses both command printing and MRtrix3 output. |
True
|
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
Result of command execution |
Raises:
| Type | Description |
|---|---|
MRtrixError
|
If command execution fails |