csttool.preprocess¶
The preprocessing module exposes the high-level functions that the CLI preprocess command orchestrates. Use it directly when you want to script preprocessing from Python.
from csttool.preprocess import preprocess
preprocess(
nifti="raw_dwi.nii.gz",
out="./preproc",
denoise_method="patch2self",
perform_motion_correction=True,
)
csttool.preprocess
¶
Preprocessing package for csttool.
Exports the main preprocessing orchestrator and module functions.
Functions¶
run_preprocessing(input_dir, output_dir, filename, *, denoise_method='patch2self', coil_count=4, apply_gibbs_correction=False, apply_motion_correction=False, target_voxel_size=None, save_visualizations=False, verbose=False)
¶
Run the complete DWI preprocessing pipeline.
Steps: 1. Load dataset (NIfTI/DICOM + gradient table) 2. Reslice to target voxel size (optional) 3. Denoise (Patch2Self or NLMeans) 4. Brain masking (median Otsu on b0 volumes) 5. Gibbs unringing (optional) 6. Motion correction (optional) 7. Save outputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dir
|
str or Path
|
Directory containing input NIfTI/DICOM and gradient files. |
required |
output_dir
|
str or Path
|
Output directory for preprocessed files. |
required |
filename
|
str
|
Base filename without extension (e.g., "sub01_dwi"). |
required |
denoise_method
|
str
|
Denoising method: "patch2self" or "nlmeans". |
"patch2self"
|
coil_count
|
int
|
Number of scanner coils (for NLMeans noise estimation). |
4
|
apply_gibbs_correction
|
bool
|
Apply Gibbs ringing correction. |
False
|
apply_motion_correction
|
bool
|
Apply between-volume motion correction. |
False
|
target_voxel_size
|
tuple[float, float, float] or None
|
Target voxel size in mm (x, y, z). If provided, data will be resliced to this voxel size. If None, no reslicing is performed. |
None
|
save_visualizations
|
bool
|
Save QC visualizations. |
False
|
verbose
|
bool
|
Print detailed processing information. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing: - 'output_paths': Paths to saved files - 'brain_mask': The computed brain mask array - 'motion_correction_applied': Whether motion correction was applied - 'gtab': The gradient table |
Source code in src/csttool/preprocess/preprocess.py
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 | |
load_dataset(dir_path, fname)
¶
Load dataset from DICOM directory or NIfTI file and build gradient table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
Path to the directory containing the dataset. |
required |
fname
|
str
|
Name of the file to load. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
nii |
Nifti1Image
|
NIfTI image. |
bval |
str
|
Path to the bval file. |
bvec |
str
|
Path to the bvec file. |
gtab |
GradientTable
|
Gradient table. |
Source code in src/csttool/preprocess/modules/load_dataset.py
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 | |
denoise(data, bvals=None, brain_mask=None, denoise_method='patch2self', N=4)
¶
Denoise DWI data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
4D DWI data array. |
required |
bvals
|
ndarray
|
1D array of b values. |
None
|
brain_mask
|
ndarray
|
3D brain mask array. |
None
|
denoise_method
|
str
|
Denoising method to use. Can be "nlmeans" or "patch2self". |
'patch2self'
|
N
|
int
|
Number of scanner head coils used for acquisition, needed for NLM. |
4
|
Returns:
| Name | Type | Description |
|---|---|---|
denoised_data |
ndarray
|
4D denoised DWI data array. |
Source code in src/csttool/preprocess/modules/denoise.py
gibbs_unringing(data, slice_axis=2, n_points=3)
¶
Remove Gibbs' ringing artifacts from DWI data.
Gibbs ringing artifacts appear as spurious oscillations near sharp edges in MR images due to truncation of k-space data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
3D or 4D DWI data array. |
required |
slice_axis
|
int
|
Axis along which slices were acquired (0, 1, or 2). Default is 2. |
2
|
n_points
|
int
|
Number of neighbor points to access local TV. Default is 3. |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
data_corrected |
ndarray
|
3D or 4D DWI data array with Gibbs ringing artifacts removed. |
Source code in src/csttool/preprocess/modules/gibbs_unringing.py
background_segmentation(data, gtab=None, median_radius=2, numpass=1, autocrop=False)
¶
Estimate brain mask with median Otsu.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
4D DWI data array. |
required |
gtab
|
GradientTable
|
Gradient table to identify b0 volumes. If provided, only b0 volumes are used for mask computation. If None, all volumes are used. |
None
|
median_radius
|
int
|
Radius of the median filter. Default is 2. |
2
|
numpass
|
int
|
Number of passes for the median filter. Default is 1. |
1
|
autocrop
|
bool
|
Whether to autocrop the data. Default is True. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
masked_data |
ndarray
|
4D masked DWI data array. |
mask |
ndarray
|
3D binary brain mask array. |
Source code in src/csttool/preprocess/modules/background_segmentation.py
perform_motion_correction(data, gtab, affine, brain_mask=None)
¶
Perform between-volume motion correction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
4D DWI data array. |
required |
gtab
|
GradientTable
|
Gradient table containing b-values and b-vectors. |
required |
affine
|
ndarray
|
4x4 affine transformation matrix. |
required |
brain_mask
|
ndarray or None
|
Binary brain mask to constrain registration. If provided, will be converted to uint8 and passed as static_mask. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
data_corrected |
ndarray
|
4D DWI data array with motion correction applied. |
reg_affines |
list
|
List of 4x4 registration affine matrices for each volume. |
Source code in src/csttool/preprocess/modules/perform_motion_correction.py
save_preprocessed(data, affine, output_dir, filename_stem, *, gradient_files=None, brain_mask=None, metadata=None, processing_params=None, create_report=True)
¶
Save preprocessed DWI data with auxiliary files and metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Preprocessed 4D DWI data array (X, Y, Z, volumes). |
required |
affine
|
ndarray
|
4x4 affine transformation matrix from NIfTI header. |
required |
output_dir
|
str or Path
|
Output directory for all files (flat structure). |
required |
filename_stem
|
str
|
Base filename without extension (e.g., "sub01_dwi_preproc"). |
required |
gradient_files
|
dict or None
|
Dictionary with keys 'bval' and 'bvec' pointing to source files. If provided, these will be copied to the output directory. |
None
|
brain_mask
|
ndarray or None
|
3D binary brain mask to save alongside data. |
None
|
metadata
|
dict or None
|
Custom metadata to include in report (e.g., subject ID, session). |
None
|
processing_params
|
dict or None
|
Processing parameters used (e.g., denoising method, motion correction). |
None
|
create_report
|
bool
|
Whether to generate a JSON processing report. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
output_paths |
dict[str, Path]
|
Dictionary mapping output types to their absolute paths: - 'data': Path to saved preprocessed data - 'bval': Path to copied bval file (if provided) - 'bvec': Path to copied bvec file (if provided) - 'mask': Path to saved brain mask (if provided) - 'report': Path to processing report (if created) |
Examples:
>>> paths = save_preprocessed(
... data=preprocessed_data,
... affine=affine,
... output_dir="/data/preprocessed",
... filename_stem="sub01_dwi_preproc",
... gradient_files={"bval": "original.bval", "bvec": "original.bvec"},
... brain_mask=mask,
... processing_params={"denoise_method": "patch2self", "motion_correction": True}
... )
Source code in src/csttool/preprocess/modules/save_preprocessed.py
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 | |
plot_denoising_comparison(data_before, data_after, brain_mask, output_dir, stem, denoise_method, vol_idx=None, verbose=True)
¶
Create before/after denoising comparison figure.
Shows three orthogonal views comparing original and denoised data, plus RMS residuals highlighting removed noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_before
|
ndarray
|
4D DWI data before denoising. |
required |
data_after
|
ndarray
|
4D DWI data after denoising. |
required |
brain_mask
|
ndarray
|
3D binary brain mask. |
required |
output_dir
|
str or Path
|
Output directory for saving figure. |
required |
stem
|
str
|
Subject/scan identifier for filename. |
required |
denoise_method
|
str
|
Denoising method used. |
required |
vol_idx
|
int
|
Volume index to visualize. Default picks a DWI volume (middle of 4th dim). |
None
|
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure. |
Source code in src/csttool/preprocess/modules/visualizations.py
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 | |
plot_gibbs_unringing_comparison(data_before, data_after, brain_mask, output_dir, stem, vol_idx=None, verbose=True)
¶
Create before/after Gibbs unringing comparison figure.
Shows three orthogonal views comparing data before and after unringing, plus RMS residuals highlighting removed ringing artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_before
|
ndarray
|
4D DWI data before Gibbs unringing. |
required |
data_after
|
ndarray
|
4D DWI data after Gibbs unringing. |
required |
brain_mask
|
ndarray
|
3D binary brain mask. |
required |
output_dir
|
str or Path
|
Output directory for saving figure. |
required |
stem
|
str
|
Subject/scan identifier for filename. |
required |
vol_idx
|
int
|
Volume index to visualize. Default picks a DWI volume (middle of 4th dim). |
None
|
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure. |
Source code in src/csttool/preprocess/modules/visualizations.py
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 | |
plot_brain_mask_overlay(data, brain_mask, gtab, output_dir, stem, verbose=True)
¶
Create brain mask overlay visualization.
Shows brain mask overlaid on b0 image in three orthogonal views, plus mask coverage statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
4D DWI data (masked or unmasked). |
required |
brain_mask
|
ndarray
|
3D binary brain mask. |
required |
gtab
|
GradientTable
|
Gradient table to identify b0 volumes. |
required |
output_dir
|
str or Path
|
Output directory for saving figure. |
required |
stem
|
str
|
Subject/scan identifier for filename. |
required |
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure. |
Source code in src/csttool/preprocess/modules/visualizations.py
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 | |
plot_motion_correction_summary(reg_affines, output_dir, stem, verbose=True)
¶
Create motion correction summary visualization.
Shows translation and rotation parameters across volumes, highlighting any large motion events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reg_affines
|
list of ndarray
|
List of 4x4 registration affine matrices (one per volume). |
required |
output_dir
|
str or Path
|
Output directory for saving figure. |
required |
stem
|
str
|
Subject/scan identifier for filename. |
required |
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure. |
Source code in src/csttool/preprocess/modules/visualizations.py
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 | |
create_preprocessing_summary(data_original, data_preprocessed, brain_mask, gtab, output_dir, stem, motion_correction_applied=False, verbose=True)
¶
Create multi-panel preprocessing summary figure.
Combines key QC visualizations into a single summary figure for quick assessment of preprocessing quality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_original
|
ndarray
|
4D DWI data before preprocessing. |
required |
data_preprocessed
|
ndarray
|
4D DWI data after preprocessing. |
required |
brain_mask
|
ndarray
|
3D binary brain mask. |
required |
gtab
|
GradientTable
|
Gradient table. |
required |
output_dir
|
str or Path
|
Output directory for saving figure. |
required |
stem
|
str
|
Subject/scan identifier for filename. |
required |
motion_correction_applied
|
bool
|
Whether motion correction was applied. |
False
|
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
fig_path |
Path
|
Path to saved figure. |
Source code in src/csttool/preprocess/modules/visualizations.py
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 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 | |
save_all_preprocessing_visualizations(data_original, data_denoised, data_masked, data_unringed, data_preprocessed, brain_mask, gtab, output_dir, stem, denoise_method, reg_affines=None, motion_correction_applied=False, verbose=True)
¶
Generate and save all preprocessing visualizations.
Convenience function that calls all visualization functions and returns paths to all generated figures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_original
|
ndarray
|
4D DWI data before any preprocessing. |
required |
data_denoised
|
ndarray
|
4D DWI data after denoising (before masking). |
required |
data_masked
|
ndarray
|
4D DWI data after brain masking (cropped). |
required |
data_unringed
|
ndarray
|
4D DWI data after Gibbs unringing (cropped). |
required |
data_preprocessed
|
ndarray
|
4D DWI data after full preprocessing. |
required |
brain_mask
|
ndarray
|
3D binary brain mask. |
required |
gtab
|
GradientTable
|
Gradient table. |
required |
output_dir
|
str or Path
|
Output directory for saving figures. |
required |
stem
|
str
|
Subject/scan identifier for filenames. |
required |
denoise_method
|
str
|
Denoising method used. |
required |
reg_affines
|
list of ndarray
|
Registration affines from motion correction. |
None
|
motion_correction_applied
|
bool
|
Whether motion correction was applied. |
False
|
verbose
|
bool
|
Print progress information. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
viz_paths |
dict
|
Dictionary mapping visualization names to file paths. |
Source code in src/csttool/preprocess/modules/visualizations.py
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 | |