1515This module provides modality-specific preprocessing pipelines for common medical imaging modalities.
1616"""
1717
18- from typing import Any
18+ from typing import Union
19+
20+ import numpy as np
21+ import torch
1922
2023from monai .transforms import (
2124 Compose ,
@@ -42,6 +45,10 @@ def get_ct_preprocessing_pipeline() -> Compose:
4245 Compose: Transform composition for CT preprocessing.
4346 Applies Hounsfield Unit (HU) windowing [-1000, 400] scaled to [0, 1].
4447 This range captures lung (-1000 to -400 HU) and soft tissue (0 to 100 HU) contrast.
48+
49+ Note:
50+ Output will be a single-channel tensor with shape (1, H, W, D)
51+ and values in range [0, 1].
4552 """
4653 return Compose (
4754 [
@@ -66,6 +73,10 @@ def get_mri_preprocessing_pipeline() -> Compose:
6673 Compose: Transform composition for MRI preprocessing.
6774 Normalizes intensities using nonzero voxels only, excluding background regions
6875 typical in MRI acquisitions.
76+
77+ Note:
78+ Output will be a single-channel tensor with shape (1, H, W, D)
79+ normalized based on nonzero voxel statistics.
6980 """
7081 return Compose (
7182 [
@@ -76,23 +87,24 @@ def get_mri_preprocessing_pipeline() -> Compose:
7687 )
7788
7889
79- def preprocess_dicom_series (path : str , modality : str ) -> Any :
80- """Preprocess a DICOM series or file based on imaging modality.
90+ def preprocess_medical_image (path : str , modality : str ) -> Union [torch .Tensor , np .ndarray ]:
91+ """
92+ Preprocess a medical image based on imaging modality.
8193
8294 Args:
83- path: Path to the DICOM file or directory containing a DICOM series.
95+ path: Path to the medical image file. Supports various formats including
96+ DICOM, NIfTI, and others supported by MONAI's LoadImage transform.
8497 modality: Imaging modality. Supported values are "CT", "MR", and "MRI" (case-insensitive).
8598
8699 Returns:
87- Any: Preprocessed image data.
100+ Preprocessed image data as a tensor or numpy array .
88101
89102 Raises:
90103 ModalityTypeError: If modality is not a string.
91104 UnsupportedModalityError: If the provided modality is not supported.
92105 """
93106 if not isinstance (modality , str ):
94- error_msg = "modality must be a string"
95- raise ModalityTypeError (error_msg )
107+ raise ModalityTypeError ("modality must be a string" )
96108
97109 modality_clean = modality .strip ().upper ()
98110
@@ -101,19 +113,40 @@ def preprocess_dicom_series(path: str, modality: str) -> Any:
101113 elif modality_clean == "CT" :
102114 pipeline = get_ct_preprocessing_pipeline ()
103115 else :
104- error_msg = (
105- f"Unsupported modality '{ modality } '. "
106- f"Supported modalities: CT, MR, MRI"
116+ raise UnsupportedModalityError (
117+ f"Unsupported modality '{ modality } '. Supported modalities: CT, MR, MRI"
107118 )
108- raise UnsupportedModalityError (error_msg )
109119
110120 return pipeline (path )
111121
112122
123+ # Keep the old function name for backward compatibility
124+ def preprocess_dicom_series (path : str , modality : str ) -> Union [torch .Tensor , np .ndarray ]:
125+ """
126+ Preprocess a DICOM series or file based on imaging modality.
127+
128+ Note: This function also supports other medical image formats
129+ (NIfTI, etc.) through MONAI's LoadImage transform.
130+
131+ Args:
132+ path: Path to the DICOM file or directory containing a DICOM series.
133+ modality: Imaging modality. Supported values are "CT", "MR", and "MRI" (case-insensitive).
134+
135+ Returns:
136+ Preprocessed image data.
137+
138+ Raises:
139+ ModalityTypeError: If modality is not a string.
140+ UnsupportedModalityError: If the provided modality is not supported.
141+ """
142+ return preprocess_medical_image (path , modality )
143+
144+
113145__all__ = [
114146 "ModalityTypeError" ,
115147 "UnsupportedModalityError" ,
116148 "get_ct_preprocessing_pipeline" ,
117149 "get_mri_preprocessing_pipeline" ,
118150 "preprocess_dicom_series" ,
119- ]
151+ "preprocess_medical_image" ,
152+ ]
0 commit comments