mri_dataloader
ðŸ§
Backend functions to load MRI and target data of different datasets.
This is more or less the backend of the data loading process in xai4mri.dataloader.datasets
.
Authors: Simon M. Hofmann | Hannah S. Heinrichs
Years: 2023-2024
get_metadata_path
ðŸ§
get_metadata_path(
project_id: str,
mri_seq: str,
regis_mni: int | None,
path_brain_mask: str | None,
norm: bool,
prune_mode: str | None,
path_to_dataset: str | Path | None,
) -> Path
Get the path to the metadata table of a project's dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_id |
str
|
project ID |
required |
mri_seq |
str
|
MRI sequence (e.g., 't1w') |
required |
regis_mni |
int | None
|
set when data was transformed to MNI space (1 or 2 mm) or None |
required |
path_brain_mask |
str | None
|
if used, path to the applied brain mask, else None |
required |
norm |
bool
|
if images were normalized |
required |
prune_mode |
str | None
|
if not used: None; else pruning mode: "cube" or "max". |
required |
path_to_dataset |
str | Path | None
|
Optional path to folder containing project data (if not in globally set |
required |
Returns:
Type | Description |
---|---|
Path
|
path to the metadata table of the project dataset |
Source code in src/xai4mri/dataloader/mri_dataloader.py
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 |
|
get_mri_set_name
ðŸ§
get_mri_set_name(
project_id: str,
mri_seq: str,
regis_mni: int | None,
brain_masked: bool,
norm: bool,
prune_mode: str | None,
) -> str
Construct a name for the MRI set which is/will be saved as *.pkl
object.
The full name describes different pre-processing steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_id |
str
|
name of the project containing the data set, e.g., lemon, hcp, or other projects |
required |
mri_seq |
str
|
MRI sequence |
required |
regis_mni |
int | None
|
registered to MNI space in 1 or 2 mm resolution [int], or None for no registration |
required |
brain_masked |
bool
|
brain mask has been applied |
required |
norm |
bool
|
if data is normalized |
required |
prune_mode |
str | None
|
if data is pruned: None OR "cube" OR "max" |
required |
Returns:
Type | Description |
---|---|
str
|
final name of MRI set |
Source code in src/xai4mri/dataloader/mri_dataloader.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
|
get_mri_set_path
ðŸ§
get_mri_set_path(
mri_set_name: str,
path_to_folder: str | Path | None = None,
as_npy: bool = True,
as_zip: bool = False,
) -> str
Get the absolute path to the MRI set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mri_set_name |
str
|
Name of MRI set (constructed by |
required |
path_to_folder |
str | Path | None
|
The path where the MRI set is supposed to be located. |
None
|
as_npy |
bool
|
True: Save as a numpy ( |
True
|
as_zip |
bool
|
zipped file ( |
False
|
Returns:
Type | Description |
---|---|
str
|
absolute path to the MRI set |
Source code in src/xai4mri/dataloader/mri_dataloader.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 |
|
get_nifti
ðŸ§
Get NIfTI image from its file path.
This works for both NIfTI [*.nii
| *.nii.gz
] and MGH [*.mgh
| *.mgz
] files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mri_path |
str | Path
|
path to an MRI file |
required |
reorient |
bool
|
reorient the image to the global project orientation space |
required |
Returns:
Type | Description |
---|---|
Nifti1Image
|
nibabel Nifti1Image object |
Source code in src/xai4mri/dataloader/mri_dataloader.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
load_file_paths_from_metadata
ðŸ§
load_file_paths_from_metadata(
sid_list: list[str] | ndarray[str],
path_to_metadata: str | Path,
exist_check: bool = True,
) -> tuple[
ndarray[Any, dtype[str | Path]],
ndarray[Any, dtype[str]],
]
Load file paths to MRI data from a project's metadata table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sid_list |
list[str] | ndarray[str]
|
List of subject ID's. |
required |
path_to_metadata |
str | Path
|
Path to the metadata table of a project dataset. |
required |
exist_check |
bool
|
Check if image files exist. |
True
|
Returns:
Type | Description |
---|---|
tuple[ndarray[Any, dtype[str | Path]], ndarray[Any, dtype[str]]]
|
Array of MRI file paths and ordered list of corresponding subject ID's. |
Source code in src/xai4mri/dataloader/mri_dataloader.py
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 |
|
load_files_from_metadata
ðŸ§
load_files_from_metadata(
sid_list: list[str] | ndarray[str],
path_to_metadata: str | Path,
) -> tuple[
ndarray[Any, dtype[uint8 | float32]],
ndarray[Any, dtype[str]],
]
Load MRI data from a project's metadata table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sid_list |
list[str] | ndarray[str]
|
List of subject ID's. |
required |
path_to_metadata |
str | Path
|
Path to the metadata table of a project dataset. |
required |
Returns:
Type | Description |
---|---|
tuple[ndarray[Any, dtype[uint8 | float32]], ndarray[Any, dtype[str]]]
|
Array of MRI files with the shape |
Source code in src/xai4mri/dataloader/mri_dataloader.py
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 |
|
mgz2nifti
ðŸ§
mgz2nifti(nib_mgh: MGHImage) -> Nifti1Image
Convert Freesurfer's MGH-NMR [*.mgh
| *.mgz
] file to NIfTI [*.nii
].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nib_mgh |
MGHImage
|
|
required |
Returns:
Type | Description |
---|---|
Nifti1Image
|
|
Source code in src/xai4mri/dataloader/mri_dataloader.py
448 449 450 451 452 453 454 455 |
|
process_single_mri
ðŸ§
process_single_mri(
mri_path: str | Path,
dtype: type = np.float32,
prune_mode: str | None = "max",
path_brain_mask: str | Path | None = None,
regis_mni: int | None = None,
path_cached_mni: str | Path | None = None,
verbose: bool = False,
) -> ndarray
Load an individual MRI of an individual subject as a numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mri_path |
str | Path
|
path to the original NIfTI MRI file |
required |
dtype |
type
|
data type of returned MRI (default: |
float32
|
prune_mode |
str | None
|
if not use: |
'max'
|
path_brain_mask |
str | Path | None
|
path to the brain mask; if no mask should be applied use |
None
|
regis_mni |
int | None
|
transform MRI to MNI space in 1 or 2 mm resolution [int], or |
None
|
verbose |
bool
|
be verbose about the process or not |
False
|
path_cached_mni |
str | Path | None
|
if a path is provided, save interim file in MNI space to this cache path |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
4D numpy array (MRI) of shape |
Source code in src/xai4mri/dataloader/mri_dataloader.py
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 |
|