Skip to content

Commit 821fc9a

Browse files
Complete fix for all critical code review issues
1 parent 81b7f5b commit 821fc9a

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

monai/tests/test_clinical_preprocessing.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_ct_preprocessing_pipeline():
2626
assert scale_transform.b_min == 0.0
2727
assert scale_transform.b_max == 1.0
2828
assert scale_transform.clip is True
29-
29+
3030
# Verify LoadImage configuration (as suggested in review)
3131
load_transform = pipeline.transforms[0]
3232
assert load_transform.image_only is True
@@ -44,7 +44,7 @@ def test_mri_preprocessing_pipeline():
4444
# Verify MRI-specific normalization parameter
4545
normalize_transform = pipeline.transforms[2]
4646
assert normalize_transform.nonzero is True
47-
47+
4848
# Verify LoadImage configuration (as suggested in review)
4949
load_transform = pipeline.transforms[0]
5050
assert load_transform.image_only is True
@@ -55,13 +55,14 @@ def test_preprocess_dicom_series_invalid_modality():
5555
# More robust error matching (as suggested in review)
5656
with pytest.raises(UnsupportedModalityError) as exc_info:
5757
preprocess_dicom_series("dummy_path.dcm", "PET")
58-
58+
5959
error_message = str(exc_info.value)
60-
# Check that all supported modalities are mentioned (order doesn't matter)
60+
# Check that all required strings are present (NO OR OPERATOR - separate assertions)
6161
assert "CT" in error_message
6262
assert "MR" in error_message
6363
assert "MRI" in error_message
64-
assert "PET" in error_message or "Unsupported modality" in error_message
64+
assert "Unsupported modality" in error_message
65+
assert "PET" in error_message
6566

6667

6768
def test_preprocess_dicom_series_invalid_type():
@@ -99,4 +100,4 @@ def test_preprocess_dicom_series_mr(mock_pipeline):
99100

100101
# Test lowercase and "MRI" variant
101102
result2 = preprocess_dicom_series("dummy_path.dcm", "mri")
102-
assert result2 == dummy_output
103+
assert result2 == dummy_output

monai/transforms/clinical_preprocessing.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
This module provides preprocessing pipelines for different medical imaging modalities.
55
"""
66

7-
from typing import Union
87
from monai.transforms import Compose, LoadImage, EnsureChannelFirst, ScaleIntensityRange, NormalizeIntensity
8+
from monai.data import MetaTensor
99

1010

1111
class ModalityTypeError(TypeError):
@@ -21,15 +21,15 @@ class UnsupportedModalityError(ValueError):
2121
def 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:
4343
def 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

Comments
 (0)