-
Notifications
You must be signed in to change notification settings - Fork 1.4k
This PR adds clinical DICOM preprocessing pipelines for CT and MRI modalities, along with unit tests. #8659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hitendrasinhdata7
wants to merge
21
commits into
Project-MONAI:dev
Choose a base branch
from
Hitendrasinhdata7:clinical-dicom-preprocessing
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5394658
Final commit: add clinical DICOM preprocessing files, workflow PDF, a…
Hitendrasinhdata7 aeabbbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24665aa
Add clinical DICOM preprocessing Python module, test module, PDF; rem…
Hitendrasinhdata7 798f8af
Remove old notebook files after converting to .py modules
Hitendrasinhdata7 b3a6ac2
Add clinical DICOM preprocessing utilities for CT/MRI with unit tests
Hitendrasinhdata7 a446448
Update clinical preprocessing utilities and tests per CodeRabbit revi…
Hitendrasinhdata7 d7f134c
Refactor clinical preprocessing: add custom exceptions, use isinstanc…
Hitendrasinhdata7 ce7850f
Update clinical preprocessing: add Google-style Returns, parameter ch…
Hitendrasinhdata7 01b88fa
Fix clinical preprocessing module based on code review feedback
Hitendrasinhdata7 81b7f5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 821fc9a
Complete fix for all critical code review issues
Hitendrasinhdata7 1a15437
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d12bd51
Hitendrasinh Rathod <Hitendrasinh.data7@gmail.com>
Hitendrasinhdata7 634136a
Merge branch 'clinical-dicom-preprocessing' of https://github.com/Hit…
Hitendrasinhdata7 34a7aa2
Complete fix for all CI and code review issues
Hitendrasinhdata7 3a493d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2b0f6a7
Add MetaTensor import and return type hint
Hitendrasinhdata7 f0261b8
Hitendrasinh Rathod <Hitendrasinh.data7@gmail.com>
Hitendrasinhdata7 1418dcc
Merge branch 'clinical-dicom-preprocessing' of https://github.com/Hit…
Hitendrasinhdata7 1e68a5a
Fix docstring: add Returns section and correct Raises section formatting
Hitendrasinhdata7 7030e7d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import pytest | ||
| from unittest.mock import patch, Mock | ||
| from monai.transforms import LoadImage, EnsureChannelFirst, ScaleIntensityRange, NormalizeIntensity | ||
| from monai.transforms.clinical_preprocessing import ( | ||
| get_ct_preprocessing_pipeline, | ||
| get_mri_preprocessing_pipeline, | ||
| preprocess_dicom_series, | ||
| UnsupportedModalityError, | ||
| ModalityTypeError, | ||
| ) | ||
|
|
||
|
|
||
| def test_ct_preprocessing_pipeline(): | ||
| """Test CT preprocessing pipeline returns expected transform composition and parameters.""" | ||
| pipeline = get_ct_preprocessing_pipeline() | ||
| assert hasattr(pipeline, 'transforms') | ||
| assert len(pipeline.transforms) == 3 | ||
| assert isinstance(pipeline.transforms[0], LoadImage) | ||
| assert isinstance(pipeline.transforms[1], EnsureChannelFirst) | ||
| assert isinstance(pipeline.transforms[2], ScaleIntensityRange) | ||
|
|
||
| # Verify CT-specific HU window parameters | ||
| scale_transform = pipeline.transforms[2] | ||
| assert scale_transform.a_min == -1000 | ||
| assert scale_transform.a_max == 400 | ||
| assert scale_transform.b_min == 0.0 | ||
| assert scale_transform.b_max == 1.0 | ||
| assert scale_transform.clip is True | ||
|
|
||
| # Verify LoadImage configuration | ||
| load_transform = pipeline.transforms[0] | ||
| assert load_transform.image_only is True | ||
|
|
||
|
|
||
| def test_mri_preprocessing_pipeline(): | ||
| """Test MRI preprocessing pipeline returns expected transform composition and parameters.""" | ||
| pipeline = get_mri_preprocessing_pipeline() | ||
| assert hasattr(pipeline, 'transforms') | ||
| assert len(pipeline.transforms) == 3 | ||
| assert isinstance(pipeline.transforms[0], LoadImage) | ||
| assert isinstance(pipeline.transforms[1], EnsureChannelFirst) | ||
| assert isinstance(pipeline.transforms[2], NormalizeIntensity) | ||
|
|
||
| # Verify MRI-specific normalization parameter | ||
| normalize_transform = pipeline.transforms[2] | ||
| assert normalize_transform.nonzero is True | ||
|
|
||
| # Verify LoadImage configuration | ||
| load_transform = pipeline.transforms[0] | ||
| assert load_transform.image_only is True | ||
|
|
||
|
|
||
| def test_preprocess_dicom_series_invalid_modality(): | ||
| """Test preprocess_dicom_series raises UnsupportedModalityError for unsupported modality.""" | ||
| with pytest.raises(UnsupportedModalityError) as exc_info: | ||
| preprocess_dicom_series("dummy_path.dcm", "PET") | ||
|
|
||
| error_message = str(exc_info.value) | ||
| # Check that all required strings are present (separate assertions, no OR operator) | ||
| assert "CT" in error_message | ||
| assert "MR" in error_message | ||
| assert "MRI" in error_message | ||
| assert "Unsupported modality" in error_message | ||
| assert "PET" in error_message | ||
|
|
||
|
|
||
| def test_preprocess_dicom_series_invalid_type(): | ||
| """Test preprocess_dicom_series raises ModalityTypeError for non-string modality.""" | ||
| with pytest.raises(ModalityTypeError, match=r"modality must be a string, got int"): | ||
| preprocess_dicom_series("dummy_path.dcm", 123) | ||
|
|
||
|
|
||
| @patch("monai.transforms.clinical_preprocessing.get_ct_preprocessing_pipeline") | ||
| def test_preprocess_dicom_series_ct(mock_pipeline): | ||
| """Test preprocess_dicom_series successfully runs for CT modality.""" | ||
| dummy_output = "ct_processed" | ||
| mock_pipeline.return_value = Mock(return_value=dummy_output) | ||
| result = preprocess_dicom_series("dummy_path.dcm", "CT") | ||
| assert result == dummy_output | ||
|
|
||
| # Test lowercase and whitespace variants | ||
| result2 = preprocess_dicom_series("dummy_path.dcm", " ct ") | ||
| assert result2 == dummy_output | ||
|
|
||
|
|
||
| @patch("monai.transforms.clinical_preprocessing.get_mri_preprocessing_pipeline") | ||
| def test_preprocess_dicom_series_mr(mock_pipeline): | ||
| """Test preprocess_dicom_series successfully runs for MR modality.""" | ||
| dummy_output = "mr_processed" | ||
| mock_pipeline.return_value = Mock(return_value=dummy_output) | ||
| result = preprocess_dicom_series("dummy_path.dcm", "MR") | ||
| assert result == dummy_output | ||
|
|
||
| # Test lowercase and "MRI" variant | ||
| result2 = preprocess_dicom_series("dummy_path.dcm", "mri") | ||
| assert result2 == dummy_output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """ | ||
| Clinical preprocessing transforms for medical imaging data. | ||
|
|
||
| This module provides preprocessing pipelines for different medical imaging modalities. | ||
| """ | ||
|
|
||
| from monai.data import MetaTensor | ||
| from monai.transforms import Compose, LoadImage, EnsureChannelFirst, ScaleIntensityRange, NormalizeIntensity | ||
|
|
||
|
|
||
| class ModalityTypeError(TypeError): | ||
| """Exception raised when modality parameter is not a string.""" | ||
| pass | ||
|
|
||
|
|
||
| class UnsupportedModalityError(ValueError): | ||
| """Exception raised when an unsupported modality is requested.""" | ||
| pass | ||
|
|
||
|
|
||
| def get_ct_preprocessing_pipeline() -> Compose: | ||
| """ | ||
| Create a preprocessing pipeline for CT (Computed Tomography) images. | ||
|
|
||
| Returns: | ||
| Compose: A transform composition for CT preprocessing. | ||
|
|
||
| The pipeline consists of: | ||
| 1. LoadImage - Load DICOM series | ||
| 2. EnsureChannelFirst - Add channel dimension | ||
| 3. ScaleIntensityRange - Scale Hounsfield Units (HU) from [-1000, 400] to [0, 1] | ||
|
|
||
| Note: | ||
| The HU window [-1000, 400] is a common soft tissue window. | ||
| """ | ||
| return Compose([ | ||
| LoadImage(image_only=True), | ||
| EnsureChannelFirst(), | ||
| ScaleIntensityRange(a_min=-1000, a_max=400, b_min=0.0, b_max=1.0, clip=True) | ||
| ]) | ||
|
|
||
|
|
||
| def get_mri_preprocessing_pipeline() -> Compose: | ||
| """ | ||
| Create a preprocessing pipeline for MRI (Magnetic Resonance Imaging) images. | ||
|
|
||
| Returns: | ||
| Compose: A transform composition for MRI preprocessing. | ||
|
|
||
| The pipeline consists of: | ||
| 1. LoadImage - Load DICOM series | ||
| 2. EnsureChannelFirst - Add channel dimension | ||
| 3. NormalizeIntensity - Normalize non-zero voxels | ||
|
|
||
| Note: | ||
| Normalization is applied only to non-zero voxels to avoid bias from background. | ||
| """ | ||
| return Compose([ | ||
| LoadImage(image_only=True), | ||
| EnsureChannelFirst(), | ||
| NormalizeIntensity(nonzero=True) | ||
| ]) | ||
|
|
||
|
|
||
| def preprocess_dicom_series(path: str, modality: str) -> MetaTensor: | ||
| """ | ||
| Preprocess a DICOM series based on the imaging modality. | ||
|
|
||
| Args: | ||
| path: Path to the DICOM series directory or file. | ||
| modality: Imaging modality (case-insensitive). Supported values: | ||
| "CT", "MR", "MRI" (MRI is treated as synonym for MR). | ||
|
|
||
| Returns: | ||
| MetaTensor: The preprocessed image data with metadata. | ||
|
|
||
| Raises: | ||
| ModalityTypeError: If modality is not a string. | ||
| UnsupportedModalityError: If modality is not supported. | ||
| """ | ||
| # Validate input type | ||
| if not isinstance(modality, str): | ||
| raise ModalityTypeError(f"modality must be a string, got {type(modality).__name__}") | ||
|
|
||
| # Normalize modality string (strip whitespace, convert to uppercase) | ||
| modality_clean = modality.strip().upper() | ||
|
|
||
| # Map MRI to MR (treat as synonyms) | ||
| if modality_clean == "MRI": | ||
| modality_clean = "MR" | ||
|
|
||
| # Select appropriate preprocessing pipeline | ||
| if modality_clean == "CT": | ||
| pipeline = get_ct_preprocessing_pipeline() | ||
| elif modality_clean == "MR": | ||
| pipeline = get_mri_preprocessing_pipeline() | ||
| else: | ||
| supported = ["CT", "MR", "MRI"] | ||
| raise UnsupportedModalityError( | ||
| f"Unsupported modality '{modality}'. Supported modalities: {', '.join(supported)}" | ||
| ) | ||
|
|
||
| # Apply preprocessing pipeline | ||
| return pipeline(path) | ||
|
|
||
|
|
||
| # Export the public API | ||
| __all__ = [ | ||
| "ModalityTypeError", | ||
| "UnsupportedModalityError", | ||
| "get_ct_preprocessing_pipeline", | ||
| "get_mri_preprocessing_pipeline", | ||
| "preprocess_dicom_series", | ||
| ] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add return type hint and missing import.
Line 64 lacks a return type annotation. Per past review feedback and coding guidelines,
preprocess_dicom_seriesshould returnMetaTensor, but the import is missing.Apply this diff:
+from monai.data import MetaTensor from monai.transforms import Compose, LoadImage, EnsureChannelFirst, ScaleIntensityRange, NormalizeIntensityThen update line 64:
And update the docstring Returns section (lines 73-74):
Also applies to: 64-64
🤖 Prompt for AI Agents