44This module provides preprocessing pipelines for different medical imaging modalities.
55"""
66
7- from typing import Union
87from monai .transforms import Compose , LoadImage , EnsureChannelFirst , ScaleIntensityRange , NormalizeIntensity
8+ from monai .data import MetaTensor
99
1010
1111class ModalityTypeError (TypeError ):
@@ -21,15 +21,15 @@ class UnsupportedModalityError(ValueError):
2121def get_ct_preprocessing_pipeline () -> Compose :
2222 """
2323 Create a preprocessing pipeline for CT (Computed Tomography) images.
24-
24+
2525 Returns:
2626 Compose: A transform composition for CT preprocessing.
27-
27+
2828 The pipeline consists of:
2929 1. LoadImage - Load DICOM series
3030 2. EnsureChannelFirst - Add channel dimension
3131 3. ScaleIntensityRange - Scale Hounsfield Units (HU) from [-1000, 400] to [0, 1]
32-
32+
3333 Note:
3434 The HU window [-1000, 400] is a common soft tissue window.
3535 """
@@ -43,15 +43,15 @@ def get_ct_preprocessing_pipeline() -> Compose:
4343def get_mri_preprocessing_pipeline () -> Compose :
4444 """
4545 Create a preprocessing pipeline for MRI (Magnetic Resonance Imaging) images.
46-
46+
4747 Returns:
4848 Compose: A transform composition for MRI preprocessing.
49-
49+
5050 The pipeline consists of:
5151 1. LoadImage - Load DICOM series
5252 2. EnsureChannelFirst - Add channel dimension
5353 3. NormalizeIntensity - Normalize non-zero voxels
54-
54+
5555 Note:
5656 Normalization is applied only to non-zero voxels to avoid bias from background.
5757 """
@@ -62,33 +62,33 @@ def get_mri_preprocessing_pipeline() -> Compose:
6262 ])
6363
6464
65- def preprocess_dicom_series (path : str , modality : str ) -> Union [ dict , None ] :
65+ def preprocess_dicom_series (path : str , modality : str ) -> MetaTensor :
6666 """
6767 Preprocess a DICOM series based on the imaging modality.
68-
68+
6969 Args:
7070 path: Path to the DICOM series directory or file.
7171 modality: Imaging modality (case-insensitive). Supported values:
7272 "CT", "MR", "MRI" (MRI is treated as synonym for MR).
73-
73+
7474 Returns:
75- The preprocessed image data.
76-
75+ MetaTensor: The preprocessed image data with metadata .
76+
7777 Raises:
7878 ModalityTypeError: If modality is not a string.
7979 UnsupportedModalityError: If modality is not supported.
8080 """
8181 # Validate input type
8282 if not isinstance (modality , str ):
8383 raise ModalityTypeError (f"modality must be a string, got { type (modality ).__name__ } " )
84-
84+
8585 # Normalize modality string (strip whitespace, convert to uppercase)
8686 modality_clean = modality .strip ().upper ()
87-
87+
8888 # Map MRI to MR (treat as synonyms)
8989 if modality_clean == "MRI" :
9090 modality_clean = "MR"
91-
91+
9292 # Select appropriate preprocessing pipeline
9393 if modality_clean == "CT" :
9494 pipeline = get_ct_preprocessing_pipeline ()
@@ -99,16 +99,16 @@ def preprocess_dicom_series(path: str, modality: str) -> Union[dict, None]:
9999 raise UnsupportedModalityError (
100100 f"Unsupported modality '{ modality } '. Supported modalities: { ', ' .join (supported )} "
101101 )
102-
102+
103103 # Apply preprocessing pipeline
104104 return pipeline (path )
105105
106106
107107# Export the public API
108108__all__ = [
109109 "ModalityTypeError" ,
110- "UnsupportedModalityError" ,
110+ "UnsupportedModalityError" ,
111111 "get_ct_preprocessing_pipeline" ,
112112 "get_mri_preprocessing_pipeline" ,
113113 "preprocess_dicom_series" ,
114- ]
114+ ]
0 commit comments