Loading Module#
Module: pyradise.fileio.loading
General#
The loading module provides extensible functionality to load a
Subject (including all selected and associated Image)
specified by a list of SeriesInfo entries. This module provides the
SubjectLoader class to load a single subject and an iterative loader variant called
IterableSubjectLoader to load multiple subjects iteratively. Furthermore, the module provides the
Loader and the ExplicitLoader base classes from which new loader can be derived.
Class Overview#
The following Loader classes are provided by the loading module:
Class |
Description |
|---|---|
Base class for all |
|
Base class for |
|
Details#
- class Loader[source]#
Bases:
ABCAn abstract base class for all
Loaderclasses. ALoaderclass typically takes a sequence ofSeriesInfoentries and loads the data based on the information provided by theSeriesInfoentries. The data is then returned as aSubjectsuch that it can be used directly for further processing with for example theprocesspackage or thewritingmodule.
- class ExplicitLoader[source]#
Bases:
Loader,ABCAn abstract
Loaderclass that implements aload()method such that multiple sets ofSeriesInfoentries can be loaded with the sameLoaderinstance.- abstract load(info)[source]#
Load the
Subject.- Parameters:
info (Tuple[SeriesInfo, ...]) – The
SeriesInfoentries to be loaded.- Returns:
The loaded
Subject.- Return type:
- class SubjectLoader(intensity_pixel_value_type=8, segmentation_pixel_value_type=1, fill_hole_search_distance=0)[source]#
Bases:
ExplicitLoaderAn
ExplicitLoaderfor loading aSubjectbased on itsSeriesInfoentries. This loader can load both DICOM data (i.e.DicomSeriesInfo) and discrete image data (i.e.FileSeriesInfo). The loader validates the providedSeriesInfoentries before loading and raises appropriate errors if the information is not valid.Examples
Load and normalize NIFTI files and save the subject as NRRD files:
>>> from argparse import ArgumentParser >>> from pyradise.fileio import (SubjectFileCrawler, SubjectLoader, >>> SubjectWriter, ImageFileFormat) >>> from pyradise.process import (ZScoreNormFilter, >>> ZScoreNormFilterParams) >>> >>> >>> def main(input_path: str, output_path: str, subject_name: str) -> None: >>> # Crawl the input directory for compressed NIFTI files >>> info = SubjectFileCrawler(input_path, subject_name, 'nii.gz').execute() >>> >>> # Load the subject >>> subject = SubjectLoader().load(info) >>> >>> # Perform the normalization >>> normalization_params = ZScoreNormFilterParams(loop_axis=1) >>> normalization_filter = ZScoreNormFilter(normalization_params) >>> subject = normalization_filter.execute(subject) >>> >>> # Write the subject to the output directory >>> writer = SubjectWriter(ImageFileFormat.NRRD) >>> writer.write(output_path, subject, write_transforms=False) >>> >>> >>> if __name__ == '__main__': >>> parser = ArgumentParser() >>> parser.add_argument('input_path', type=str, help='The input directory.') >>> parser.add_argument('output_path', type=str, help='The output directory.') >>> parser.add_argument('subject_name', type=str, help='The name of the subject.') >>> args = parser.parse_args() >>> >>> main(args.input_path, args.output_path, args.subject_name)
Load DICOM data and save the converted data as NIFTI files:
>>> from argparse import ArgumentParser >>> from pyradise.fileio import SubjectDicomCrawler, SubjectLoader, SubjectWriter >>> >>> >>> def main(input_path: str, output_path: str) -> None: >>> # Crawl the input directory for DICOM data >>> # Note: We assume that the modality configuration file (modality_config.json) >>> # is existing. >>> info = SubjectDicomCrawler(input_path).execute() >>> >>> # Load the subject >>> subject = SubjectLoader().load(info) >>> >>> # Write the subject to the output directory >>> writer = SubjectWriter() >>> writer.write(output_path, subject, write_transforms=False) >>> >>> >>> if __name__ == '__main__': >>> parser = ArgumentParser() >>> parser.add_argument('input_path', type=str, help='The input directory.') >>> parser.add_argument('output_path', type=str, help='The output directory.') >>> args = parser.parse_args() >>> >>> main(args.input_path, args.output_path)
- Parameters:
intensity_pixel_value_type (int) – The pixel value type of the intensity imagesm when loading discrete image files (default: sitk.sitkFloat32).
segmentation_pixel_value_type (int) – The pixel value type of the segmentation images when loading discrete files (default: sitk.sitkUInt8).
fill_hole_search_distance (int) – The search distance for the hole filling algorithm. If the search distance is set to zero the hole filling algorithm is omitted. The search distance must be an odd number larger than 1 (default: 0).
- load(info)[source]#
Load a
Subjectfrom the providedSeriesInfoentries.- Parameters:
info (Tuple[SeriesInfo, ...]) – The
SeriesInfoentries containing the necessary information for loading the subject.- Raises:
ValueError – If
infois an empty tuple.ValueError – If
infois not a tuple ofSeriesInfoentries.ValueError – If the patient name and patient id of the provided
SeriesInfoentries are not equal.ValueError – If not all referenced
DicomSeriesImageInfoentries are provided for registration.ValueError – If not all referenced
DicomSeriesImageInfoentries are provided for RTSS loading.
- Returns:
The loaded subject.
- Return type:
- class IterableSubjectLoader(info, intensity_pixel_value_type=8, segmentation_pixel_value_type=1, fill_hole_search_distance=0)[source]#
Bases:
LoaderAn
Loaderfor loading a sequence ofSubjects based on theirSeriesInfoentries. This loader can load both DICOM data (i.e.DicomSeriesInfo) and discrete image data (i.e.FileSeriesInfo). The loader validates the providedSeriesInfoentries before loading and raises appropriate errors if the information is not valid.Notes
For loading large DICOM dataset we recommend to use the
SubjectLoaderinstead because the antecedent crawling process can require a lot of computation time and memory.- Raises:
ValueError – If
infois an empty tuple.ValueError – If
infois not a tuple of tuples ofSeriesInfoentries.
- Parameters:
info (Tuple[Tuple[SeriesInfo, ...], ...]) – The
SeriesInfoentries for all subjects to load.intensity_pixel_value_type (int) – The pixel value type of the intensity imagesm when loading discrete image files (default: sitk.sitkFloat32).
segmentation_pixel_value_type (int) – The pixel value type of the segmentation images when loading discrete files (default: sitk.sitkUInt8).
fill_hole_search_distance (int) – The search distance for the hole filling algorithm. If the search distance is set to zero the hole filling algorithm is omitted. The search distance must be an odd number larger than 1 (default: 0).
Examples
Load, normalize and save a NIFTI dataset with multiple subjects:
>>> from argparse import ArgumentParser >>> from pyradise.fileio import (DatasetFileCrawler, IterableSubjectLoader, >>> SubjectWriter) >>> from pyradise.process import (ZScoreNormFilter, >>> ZScoreNormFilterParams) >>> >>> >>> def main(input_path: str, output_path: str) -> None: >>> # Crawl the dataset info >>> info = DatasetFileCrawler(input_path, '.nii.gz').execute() >>> >>> # Construct the loader >>> loader = IterableSubjectLoader(info) >>> >>> # Construct the normalization filter >>> normalization_params = ZScoreNormFilterParams(loop_axis=1) >>> normalization_filter = ZScoreNormFilter(normalization_params) >>> >>> # Construct the writer >>> writer = SubjectWriter() >>> >>> # Iteratively load the subjects >>> for subject in loader: >>> # Normalize the images >>> subject = normalization_filter.execute(subject) >>> >>> # Save the subject >>> writer.write_to_subject_folder(output_path, subject, write_transforms=False) >>> >>> >>> if __name__ == '__main__': >>> parser = ArgumentParser() >>> parser.add_argument('--input_path', type=str, >>> help='The dataset input directory.') >>> parser.add_argument('--output_path', type=str, >>> help='The dataset output directory.') >>> args = parser.parse_args() >>> >>> main(args.input_path, args.output_path)