csttool.metrics¶
CST metric computation and PDF/HTML reporting. The CLI counterpart is metrics.
from csttool.metrics import compute_metrics
compute_metrics(
cst_left="extract/cst_left.trk",
cst_right="extract/cst_right.trk",
fa="tracking/dti_FA.nii.gz",
out="./metrics",
generate_pdf=True,
)
csttool.metrics
¶
CST Metrics Module
Modular metrics computation for bilateral CST analysis.
Structure: - modules/unilateral_analysis: Analyze individual hemispheres - modules/bilateral_analysis: Compare left vs right - modules/visualizations: Generate all plots
Functions¶
analyze_cst_hemisphere(streamlines, fa_map=None, md_map=None, rd_map=None, ad_map=None, affine=None, hemisphere='unknown')
¶
Complete analysis of a single CST hemisphere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streamlines
|
Streamlines
|
Streamlines for one hemisphere (left OR right) |
required |
fa_map
|
ndarray
|
3D fractional anisotropy map |
None
|
md_map
|
ndarray
|
3D mean diffusivity map |
None
|
rd_map
|
ndarray
|
3D radial diffusivity map |
None
|
ad_map
|
ndarray
|
3D axial diffusivity map |
None
|
affine
|
ndarray
|
4x4 affine transformation matrix |
None
|
hemisphere
|
str
|
'left' or 'right' for identification |
'unknown'
|
Returns:
| Name | Type | Description |
|---|---|---|
metrics |
dict
|
Comprehensive metrics dictionary containing: - morphology: streamline count, length stats, volume - fa: mean, std, median, profile, all sampled values - md: mean, std, median, profile, all sampled values - rd: mean, std, median, profile, all sampled values - ad: mean, std, median, profile, all sampled values - hemisphere: identification string |
Source code in src/csttool/metrics/modules/unilateral_analysis.py
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 | |
compute_morphology(streamlines, affine)
¶
Compute morphological properties of a streamline bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streamlines
|
Streamlines
|
Input streamlines |
required |
affine
|
ndarray
|
4x4 affine transformation matrix |
required |
Returns:
| Name | Type | Description |
|---|---|---|
morphology |
dict
|
Dictionary containing: - n_streamlines: number of streamlines - mean_length: average streamline length in mm - std_length: standard deviation of lengths - min_length: minimum streamline length - max_length: maximum streamline length - tract_volume: volume in mm³ |
Source code in src/csttool/metrics/modules/unilateral_analysis.py
sample_scalar_along_tract(streamlines, scalar_map, affine)
¶
Sample scalar values at every point along all streamlines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streamlines
|
Streamlines
|
Input streamlines in world coordinates (mm) |
required |
scalar_map
|
ndarray
|
3D scalar map (e.g., FA or MD) |
required |
affine
|
ndarray
|
4x4 affine transformation matrix |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scalar_values |
ndarray
|
Array of all sampled scalar values (flattened across all streamlines) |
Source code in src/csttool/metrics/modules/unilateral_analysis.py
compute_tract_profile(streamlines, scalar_map, affine, n_points=20)
¶
Compute normalized tract profile (average scalar along tract).
This function samples scalar values along each streamline, normalizes each streamline to the same number of points, and averages across all streamlines to create a representative profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streamlines
|
Streamlines
|
Input streamlines |
required |
scalar_map
|
ndarray
|
3D scalar map (FA or MD) |
required |
affine
|
ndarray
|
4x4 affine transformation matrix |
required |
n_points
|
int
|
Number of points in output profile (default: 20) |
20
|
Returns:
| Name | Type | Description |
|---|---|---|
profile |
ndarray
|
Average scalar values at n_points positions along the tract |
Source code in src/csttool/metrics/modules/unilateral_analysis.py
print_hemisphere_summary(metrics)
¶
Print human-readable summary of hemisphere metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
dict
|
Metrics dictionary from analyze_cst_hemisphere() |
required |
Source code in src/csttool/metrics/modules/unilateral_analysis.py
compare_bilateral_cst(left_metrics, right_metrics)
¶
Compare left and right CST metrics and compute asymmetry measures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_metrics
|
dict
|
Metrics from analyze_cst_hemisphere() for left CST |
required |
right_metrics
|
dict
|
Metrics from analyze_cst_hemisphere() for right CST |
required |
Returns:
| Name | Type | Description |
|---|---|---|
comparison |
dict
|
Comprehensive bilateral comparison containing: - left: complete left hemisphere metrics - right: complete right hemisphere metrics - asymmetry: laterality indices and differences |
Source code in src/csttool/metrics/modules/bilateral_analysis.py
compute_laterality_indices(left_metrics, right_metrics)
¶
Compute laterality indices for all available metrics.
Laterality Index (LI) = (L - R) / (L + R)
LI interpretation:
- LI > 0: Left hemisphere larger/higher
- LI < 0: Right hemisphere larger/higher
- LI ≈ 0: Symmetric
- |LI| > 0.1: Potentially significant asymmetry
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_metrics
|
dict
|
Left hemisphere metrics |
required |
right_metrics
|
dict
|
Right hemisphere metrics |
required |
Returns:
| Name | Type | Description |
|---|---|---|
asymmetry |
dict
|
Laterality indices and absolute differences for: - volume - streamline count - mean length - mean FA (if available) - mean MD (if available) - mean RD (if available) - mean AD (if available) |
Source code in src/csttool/metrics/modules/bilateral_analysis.py
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 | |
compute_li(left_value, right_value)
¶
Compute laterality index and absolute difference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_value
|
float
|
Metric value from left hemisphere |
required |
right_value
|
float
|
Metric value from right hemisphere |
required |
Returns:
| Name | Type | Description |
|---|---|---|
li_info |
dict
|
Dictionary containing: - laterality_index: (L-R)/(L+R) - absolute_difference: |L-R| - percent_difference: 100 * |L-R| / mean(L,R) - interpretation: text description |
Source code in src/csttool/metrics/modules/bilateral_analysis.py
print_bilateral_summary(comparison)
¶
Print human-readable bilateral comparison summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Output from compare_bilateral_cst() |
required |
Source code in src/csttool/metrics/modules/bilateral_analysis.py
assess_clinical_significance(asymmetry, thresholds=None)
¶
Assess clinical significance of asymmetry based on thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asymmetry
|
dict
|
Asymmetry metrics from compute_laterality_indices() |
required |
thresholds
|
dict
|
Custom thresholds for each metric Default thresholds are based on literature (if available) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
assessment |
dict
|
Clinical assessment with flags for significant asymmetries |
Source code in src/csttool/metrics/modules/bilateral_analysis.py
plot_tract_profiles(left_metrics, right_metrics, output_dir, subject_id, scalar='fa', anatomical_labels=True)
¶
Plot along-tract profiles for bilateral comparison.
Creates a figure showing FA or MD profiles along normalized tract length for both left and right CST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_metrics
|
dict
|
Left hemisphere metrics with 'fa' or 'md' profile |
required |
right_metrics
|
dict
|
Right hemisphere metrics with 'fa' or 'md' profile |
required |
output_dir
|
str or Path
|
Output directory for saving figure |
required |
subject_id
|
str
|
Subject identifier for filename |
required |
scalar
|
str
|
'fa' or 'md' - which scalar to plot |
'fa'
|
anatomical_labels
|
bool
|
If True, add anatomical labels to x-axis (Pontine Level, PLIC, Precentral Gyrus) |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure |
Source code in src/csttool/metrics/modules/visualizations.py
plot_bilateral_comparison(comparison, output_dir, subject_id)
¶
Create bar charts comparing left vs right CST metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Output from compare_bilateral_cst() |
required |
output_dir
|
str or Path
|
Output directory for saving figure |
required |
subject_id
|
str
|
Subject identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure |
Source code in src/csttool/metrics/modules/visualizations.py
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 | |
plot_3d_streamlines(streamlines_left, streamlines_right, fa_map, affine, output_dir, subject_id)
¶
Create 3D visualization of CST streamlines colored by FA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streamlines_left
|
Streamlines
|
Left CST streamlines |
required |
streamlines_right
|
Streamlines
|
Right CST streamlines |
required |
fa_map
|
ndarray
|
3D FA map for coloring |
required |
affine
|
ndarray
|
4x4 affine transformation matrix |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure |
Source code in src/csttool/metrics/modules/visualizations.py
create_summary_figure(comparison, streamlines_left, streamlines_right, fa_map, affine, output_dir, subject_id)
¶
Create multi-panel summary figure with all key visualizations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Bilateral comparison metrics |
required |
streamlines_left
|
Streamlines
|
Left CST streamlines |
required |
streamlines_right
|
Streamlines
|
Right CST streamlines |
required |
fa_map
|
ndarray
|
3D FA map |
required |
affine
|
ndarray
|
4x4 affine transformation matrix |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved summary figure |
Source code in src/csttool/metrics/modules/visualizations.py
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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 | |
plot_asymmetry_radar(asymmetry, output_dir, subject_id)
¶
Create radar plot showing asymmetry across multiple metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asymmetry
|
dict
|
Asymmetry metrics from bilateral comparison |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure |
Source code in src/csttool/metrics/modules/visualizations.py
plot_stacked_profiles(left_metrics, right_metrics, output_dir, subject_id)
¶
Create stacked FA, MD, RD, and AD profile plots for PDF report.
Creates a vertically stacked figure with 4 subplots (if data available): - FA profile - MD profile - RD profile - AD profile
All have shared x-axis (anatomical labels only on bottom).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_metrics
|
dict
|
Left hemisphere metrics |
required |
right_metrics
|
dict
|
Right hemisphere metrics |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure |
Source code in src/csttool/metrics/modules/visualizations.py
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 | |
plot_tractogram_qc_preview(streamlines_left, streamlines_right, background_image, affine, output_dir, subject_id, slice_type='axial', max_streamlines=500, set_title=True)
¶
Create compact 3D tractogram QC preview for PDF report.
Renders left (blue) and right (red) CST streamlines overlaid on a brain slice at the internal capsule level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streamlines_left
|
Streamlines
|
Left CST streamlines |
required |
streamlines_right
|
Streamlines
|
Right CST streamlines |
required |
background_image
|
ndarray
|
3D T1 or FA image for background |
required |
affine
|
ndarray
|
4x4 affine transformation matrix |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
slice_type
|
str
|
'axial', 'sagittal', or 'coronal' |
'axial'
|
max_streamlines
|
int
|
Maximum streamlines to render per hemisphere (for performance) |
500
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure |
Source code in src/csttool/metrics/modules/visualizations.py
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 | |
save_json_report(comparison, output_dir, subject_id, metadata=None)
¶
Save comprehensive metrics report as JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Output from compare_bilateral_cst() |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
metadata
|
dict
|
Additional metadata including: - acquisition: dict with protocol, b_values, n_directions, resolution - processing: dict with denoising_method, tracking_method, etc. - qc_thresholds: dict with fa_threshold, min_length, max_length |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
json_path |
Path
|
Path to saved JSON file |
Source code in src/csttool/metrics/modules/reports.py
save_csv_summary(comparison, output_dir, subject_id)
¶
Save metrics summary as CSV table.
Creates a flat CSV file with key metrics suitable for group analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Output from compare_bilateral_cst() |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
Returns:
| Name | Type | Description |
|---|---|---|
csv_path |
Path
|
Path to saved CSV file |
Source code in src/csttool/metrics/modules/reports.py
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 | |
save_html_report(comparison, visualization_paths, output_dir, subject_id, version=None, space='Native Space', metadata=None)
¶
Generate HTML report using Jinja2 template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Output from compare_bilateral_cst() |
required |
visualization_paths
|
dict
|
Paths to generated visualizations |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
version
|
str
|
csttool version (defaults to package version) |
None
|
space
|
str
|
Space declaration (e.g., "Native Space") |
'Native Space'
|
metadata
|
dict
|
Acquisition and processing metadata |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
html_path |
Path
|
Path to saved HTML file |
Source code in src/csttool/metrics/modules/reports.py
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 | |
save_pdf_report(comparison, visualization_paths, output_dir, subject_id, version=None, space='Native Space', html_path=None)
¶
Generate PDF report by converting HTML report.
Source code in src/csttool/metrics/modules/reports.py
generate_complete_report(comparison, streamlines_left, streamlines_right, fa_map, affine, output_dir, subject_id, background_image=None, version=None, space='Native Space', metadata=None)
¶
Generate all report formats: JSON, CSV, and PDF with visualizations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
comparison
|
dict
|
Bilateral comparison metrics |
required |
streamlines_left
|
Streamlines
|
Left CST streamlines |
required |
streamlines_right
|
Streamlines
|
Right CST streamlines |
required |
fa_map
|
ndarray
|
3D FA map |
required |
affine
|
ndarray
|
4x4 affine transformation |
required |
output_dir
|
str or Path
|
Output directory |
required |
subject_id
|
str
|
Subject identifier |
required |
background_image
|
ndarray
|
3D T1 or FA image for tractogram QC background (defaults to fa_map) |
None
|
version
|
str
|
csttool version string |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
report_paths |
dict
|
Dictionary of all generated report file paths |
Source code in src/csttool/metrics/modules/reports.py
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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |