dataloader
🧠
Init dataloader
submodule of xai4mri
.
BaseDataSet
🧠
BaseDataSet(
study_table_or_path: DataFrame | str | Path,
project_id: str,
mri_sequence: str,
register_to_mni: int | None = None,
cache_dir: str | Path = CACHE_DIR,
load_mode: str = "full_array",
**kwargs
)
Bases: ABC
Base class for an MRI dataset.
In the case of a research project with multiple MRI sequences, each sequence must have its own dataset class that inherits from the BaseDataSet class.
Initialize BaseDataSet.
Usage
# Create a study-specific dataset class
class MyStudyData(BaseDataSet):
def __init__(self):
super().__init__(
study_table_or_path="PATH/TO/STUDY_TABLE.csv", # one column must be 'sid' (subject ID)
project_id="MyProjectID",
mri_sequence="t1w", # this is of descriptive nature, for projects with multiple MRI sequences
load_mode="full_array", # or 'file_paths' for very large datasets
)
# Define mri_path_constructor
def mri_path_constructor(sid: str) -> str | Path:
return f"/path/to/mri/{sid}.nii.gz"
# Instantiate the dataset class
my_study_data = MyStudyData()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
study_table_or_path |
DataFrame | str | Path
|
The study table, OR the absolute or relative path to the table [ |
required |
project_id |
str
|
The project ID. |
required |
mri_sequence |
str
|
MRI sequence ('t1_mni_1mm', 't2', 'dwi', or similar). This is of a descriptive nature for projects with multiple MRI sequences, hence multiple offsprings of |
required |
register_to_mni |
int | None
|
Register MRIs to the MNI space (1 mm, 2 mm) using |
None
|
cache_dir |
str | Path
|
Path to the cache directory, where intermediate and processed data is stored. |
CACHE_DIR
|
load_mode |
str
|
Load mode for the dataset: 'file_paths': Load the MRI data from file paths (recommended for very large datasets). 'full_array': Load the MRI data as a full array (default). |
'full_array'
|
kwargs |
Additional keyword arguments for MRI processing. Find details to |
{}
|
Source code in src/xai4mri/dataloader/datasets.py
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 |
|
current_split_dict
property
🧠
load_mode
property
writable
🧠
load_mode: str
Return the load mode for the dataset.
The load mode can be either 'file_paths' or 'full_array'.
- 'file_paths': Load the MRI data from file paths. That is, individual files are stored separately.
- 'full_array': Load the MRI data as a full array. Data is saved as a single large file.
sid_list
property
writable
🧠
sid_list: ndarray[str]
Return the list of subject IDs.
Returns:
Type | Description |
---|---|
np.ndarray[str]
|
list of subject IDs |
study_table
property
writable
🧠
study_table: DataFrame
Get the study table.
Ideally, each BaseDataSet has its own study table, except if for all participants of a research project all MRI sequences are available. In this case, the study table can be the same for all MRI sequences and derivatives.
Returns:
Type | Description |
---|---|
pd.DataFrame
|
study table |
study_table_path
property
writable
🧠
study_table_path: str | None
Return the path to the study table if it has been provided.
Can be provided as study_table_or_path
at initialization, or it can be set later.
Returns:
Type | Description |
---|---|
str | None
|
path to the study table |
create_data_split
🧠
create_data_split(
target: str,
batch_size: int = 1,
split_ratio: tuple[float, float, float] | None = (
0.8,
0.1,
0.1,
),
split_dict: dict[str, str] | None = None,
**get_data_kwargs
) -> tuple[
dict[str, ndarray],
_DataSetGenerator,
_DataSetGenerator,
_DataSetGenerator,
]
Create data split with a training, validation, and test set.
The data subsets are provided as generator objects, and can be used for model training and evaluation.
Usage
# Create a data split for model training and evaluation
split_dict, train_gen, val_gen, test_gen = mydata.create_data_split(target="age")
# Train a model
model.fit(train_gen, validation_data=val_gen, ...)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
Prediction target. |
required |
batch_size |
int
|
Batch size in the returned data generators per data split. MRIs are arther large files; hence, it is recommended to keep batches rather small. |
1
|
split_ratio |
tuple[float, float, float] | None
|
Ratio of the data split (train, validation, test). Must add up to 1. |
(0.8, 0.1, 0.1)
|
split_dict |
dict[str, str] | None
|
Dictionary with 'train', 'validation', & 'test' as keys, and subject IDs as values. If a |
None
|
Returns:
Type | Description |
---|---|
tuple[dict[str, ndarray], _DataSetGenerator, _DataSetGenerator, _DataSetGenerator]
|
split_dict, and the data generators for the training, validation, and test set |
Source code in src/xai4mri/dataloader/datasets.py
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 |
|
get_data
🧠
Load dataset into workspace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
Additional keyword arguments for MRI processing. Find details to |
{}
|
Returns:
Type | Description |
---|---|
tuple[ndarray, ndarray[str]]
|
Processed MRI data: either 5D data array of shape |
Source code in src/xai4mri/dataloader/datasets.py
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 |
|
get_metadata_table
🧠
get_metadata_table()
Get the metadata table for the MRIs of the project dataset.
Source code in src/xai4mri/dataloader/datasets.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
get_size_of_prospective_mri_set
🧠
get_size_of_prospective_mri_set(
estimate_with_n: int = 3,
estimate_processing_time: bool = True,
**process_mri_kwargs
) -> None
Estimate the prospective storage sized, which is necessary to save the pre-processed project data.
Additionally, estimate the time needed to process the entire dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
estimate_with_n |
int
|
use n samples to approximate the size of the whole processed dataset. If approx_with >= N, the entire dataset will be taken. |
3
|
estimate_processing_time |
bool
|
estimate the time needed to process the entire dataset. |
True
|
process_mri_kwargs |
|
{}
|
Source code in src/xai4mri/dataloader/datasets.py
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 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 |
|
get_unpruned_mri
🧠
get_unpruned_mri(sid: str) -> Nifti1Image
Get the processed MRI data for a given subject ID, in the state before the image is pruned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sid |
str
|
subject ID |
required |
Returns:
Type | Description |
---|---|
Nifti1Image
|
Non-pruned MRI data as a NIfTI image |
Source code in src/xai4mri/dataloader/datasets.py
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
|
load_split_dict
staticmethod
🧠
Load the split dictionary from the given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
split_dict_path |
str | Path
|
path to split dictionary file |
required |
Returns:
Type | Description |
---|---|
dict[str, ndarray[str]]
|
split dictionary |
Source code in src/xai4mri/dataloader/datasets.py
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
|
mri_path_constructor
abstractmethod
staticmethod
🧠
Construct the path to the original MRI file of a subject given its ID (sid).
Define this function in the dataset class which inherits from BaseDataSet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sid |
str
|
subject ID |
required |
Returns:
Type | Description |
---|---|
str | Path
|
path to the original MRI file of the subject |
Source code in src/xai4mri/dataloader/datasets.py
188 189 190 191 192 193 194 195 196 197 198 199 |
|
save_split_dict
🧠
save_split_dict(
split_dict: dict[str, ndarray[str]] | None = None,
save_path: str | Path | None = None,
) -> str
Save a split dictionary to a file.
If no split dictionary is given, the self.current_split_dict
is saved.
self.current_split_dict
is set after calling self.create_data_split()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
split_dict |
dict[str, ndarray[str]] | None
|
data split dictionary: {'train': ['sub-42', ...], 'validation': [...], 'test': [...]} |
None
|
save_path |
str | Path | None
|
path to file |
None
|
Returns:
Type | Description |
---|---|
str
|
the path to the file |
Source code in src/xai4mri/dataloader/datasets.py
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 |
|
reverse_pruning
🧠
reverse_pruning(
original_mri: ndarray | Nifti1Image,
pruned_mri: ndarray,
pruned_stats_map: ndarray | None = None,
) -> ndarray | Nifti1Image
Reverse the pruning of an MRI or its corresponding statistical map.
If a statistical map is given, both the original MRI and the pruned MRI are necessary to find the edges of
the cut-off during pruning.
If no statistical map is given, only the original MRI and the pruned MRI are required.
Note, in this case reverse_pruning()
is applied to a processed and pruned version of the original MRI.
Make sure that the original MRI and the pruned MRI have the same orientation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_mri |
ndarray | Nifti1Image
|
Original (i.e., non-pruned) MRI. |
required |
pruned_mri |
ndarray
|
Pruned MRI. |
required |
pruned_stats_map |
ndarray | None
|
[Optional] pruned statistical map. |
None
|
Returns:
Type | Description |
---|---|
ndarray | Nifti1Image
|
MRI with original size (if original_mri is given as Nifti1Image, returns Nifti1Image). |
Source code in src/xai4mri/dataloader/prune_image.py
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 |
|