-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add clinical preprocessing utilities for CT and MRI modalities. #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
Closed
Hitendrasinhdata7
wants to merge
37
commits into
Project-MONAI:dev
from
Hitendrasinhdata7:clinical-dicom-preprocessing
Closed
Changes from 10 commits
Commits
Show all changes
37 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 <[email protected]>
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 <[email protected]>
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] 441d48d
Add clinical preprocessing transforms
Hitendrasinhdata7 11dce12
Resolve merge conflict - keep clinical preprocessing module
Hitendrasinhdata7 f513c16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2986d2e
Fix CodeRabbit review issues
Hitendrasinhdata7 b3e8f87
Address CodeRabbit review suggestions
Hitendrasinhdata7 5a4fbb5
Final fixes per CodeRabbit review
Hitendrasinhdata7 c273cf7
Fix CI compliance: formatting, type hints, line length
Hitendrasinhdata7 d0961e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9753c34
Fix CI compliance for clinical preprocessing
Hitendrasinhdata7 0daee66
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 885ae4b
Fix CodeRabbit issues: return type and exception classes
Hitendrasinhdata7 e50560d
Merge remote changes and fix CodeRabbit review issues
Hitendrasinhdata7 68038cb
Improve tests per CodeRabbit suggestions
Hitendrasinhdata7 b241ee6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c4c2303
Improve mock setup and add MRI validation
Hitendrasinhdata7 01b60f0
Merge formatting and apply test improvements
Hitendrasinhdata7 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
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,102 @@ | ||
| 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 (as suggested in review) | ||
| 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 (as suggested in review) | ||
| 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.""" | ||
| # More robust error matching (as suggested in review) | ||
| with pytest.raises(UnsupportedModalityError) as exc_info: | ||
| preprocess_dicom_series("dummy_path.dcm", "PET") | ||
|
|
||
| error_message = str(exc_info.value) | ||
| # Check that all supported modalities are mentioned (order doesn't matter) | ||
| assert "CT" in error_message | ||
| assert "MR" in error_message | ||
| assert "MRI" in error_message | ||
| assert "PET" in error_message or "Unsupported modality" 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) | ||
|
|
||
|
|
||
| # ------------------------ | ||
| # Tests for valid modalities | ||
| # ------------------------ | ||
|
|
||
| @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" | ||
| # Fixed: Use Mock instead of lambda with unused argument (as suggested in review) | ||
| 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" | ||
| # Fixed: Use Mock instead of lambda with unused argument (as suggested in review) | ||
| 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 typing import Union | ||
| 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) -> Union[dict, None]: | ||
| """ | ||
| 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: | ||
| The preprocessed image data. | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.