|
1 | | -import numpy as np |
2 | | - |
3 | | -from monai.transforms import ScaleIntensityRange, NormalizeIntensity |
| 1 | +import pytest |
| 2 | +from monai.transforms import LoadImage, EnsureChannelFirst, ScaleIntensityRange, NormalizeIntensity |
4 | 3 | from monai.transforms.clinical_preprocessing import ( |
5 | 4 | get_ct_preprocessing_pipeline, |
6 | 5 | get_mri_preprocessing_pipeline, |
7 | 6 | preprocess_dicom_series, |
| 7 | + UnsupportedModalityError, |
| 8 | + ModalityTypeError, |
8 | 9 | ) |
9 | | -from unittest.mock import patch, MagicMock |
10 | | - |
11 | | - |
12 | | -def test_ct_windowing_range_and_shape(): |
13 | | - """Test CT windowing transform parameters.""" |
14 | | - rng = np.random.default_rng(0) |
15 | | - |
16 | | - sample_ct = rng.integers( |
17 | | - -1024, 2048, size=(64, 64, 64), dtype=np.int16 |
18 | | - ) |
19 | | - |
20 | | - transform = ScaleIntensityRange( |
21 | | - a_min=-1000, |
22 | | - a_max=400, |
23 | | - b_min=0.0, |
24 | | - b_max=1.0, |
25 | | - clip=True, |
26 | | - ) |
27 | | - |
28 | | - output = transform(sample_ct) |
29 | | - output = np.asarray(output) |
30 | | - |
31 | | - assert output.shape == sample_ct.shape |
32 | | - assert np.isfinite(output).all() |
33 | | - assert output.min() >= -1e-6 |
34 | | - assert output.max() <= 1.0 + 1e-6 |
35 | | - |
36 | | - |
37 | | -def test_mri_normalization_mean_std(): |
38 | | - """Test MRI normalization transform.""" |
39 | | - rng = np.random.default_rng(0) |
40 | | - |
41 | | - sample_mri = rng.random((64, 64, 64), dtype=np.float32) |
42 | | - |
43 | | - transform = NormalizeIntensity(nonzero=True) |
44 | | - |
45 | | - output = transform(sample_mri) |
46 | | - output = np.asarray(output) |
47 | | - |
48 | | - mean_val = float(output.mean()) |
49 | | - std_val = float(output.std()) |
50 | | - |
51 | | - assert output.shape == sample_mri.shape |
52 | | - assert np.isclose(mean_val, 0.0, atol=0.1) |
53 | | - assert np.isclose(std_val, 1.0, atol=0.1) |
54 | 10 |
|
55 | 11 |
|
56 | 12 | def test_ct_preprocessing_pipeline(): |
57 | 13 | """Test CT preprocessing pipeline returns expected transform composition.""" |
58 | 14 | pipeline = get_ct_preprocessing_pipeline() |
59 | | - |
60 | 15 | assert hasattr(pipeline, 'transforms') |
61 | 16 | assert len(pipeline.transforms) == 3 |
62 | | - assert pipeline.transforms[0].__class__.__name__ == 'LoadImage' |
63 | | - assert pipeline.transforms[1].__class__.__name__ == 'EnsureChannelFirst' |
64 | | - assert pipeline.transforms[2].__class__.__name__ == 'ScaleIntensityRange' |
| 17 | + assert isinstance(pipeline.transforms[0], LoadImage) |
| 18 | + assert isinstance(pipeline.transforms[1], EnsureChannelFirst) |
| 19 | + assert isinstance(pipeline.transforms[2], ScaleIntensityRange) |
65 | 20 |
|
66 | 21 |
|
67 | 22 | def test_mri_preprocessing_pipeline(): |
68 | 23 | """Test MRI preprocessing pipeline returns expected transform composition.""" |
69 | 24 | pipeline = get_mri_preprocessing_pipeline() |
70 | | - |
71 | 25 | assert hasattr(pipeline, 'transforms') |
72 | 26 | assert len(pipeline.transforms) == 3 |
73 | | - assert pipeline.transforms[0].__class__.__name__ == 'LoadImage' |
74 | | - assert pipeline.transforms[1].__class__.__name__ == 'EnsureChannelFirst' |
75 | | - assert pipeline.transforms[2].__class__.__name__ == 'NormalizeIntensity' |
76 | | - |
77 | | - |
78 | | -@patch('monai.transforms.clinical_preprocessing.get_ct_preprocessing_pipeline') |
79 | | -def test_preprocess_dicom_series_ct(mock_pipeline): |
80 | | - """Test preprocess_dicom_series with CT modality.""" |
81 | | - mock_transform = MagicMock() |
82 | | - mock_pipeline.return_value = mock_transform |
83 | | - |
84 | | - preprocess_dicom_series("dummy_path.dcm", "CT") |
85 | | - |
86 | | - mock_pipeline.assert_called_once() |
87 | | - mock_transform.assert_called_once_with("dummy_path.dcm") |
88 | | - |
89 | | - |
90 | | -@patch('monai.transforms.clinical_preprocessing.get_ct_preprocessing_pipeline') |
91 | | -def test_preprocess_dicom_series_ct_lowercase(mock_pipeline): |
92 | | - """Test preprocess_dicom_series with CT modality in lowercase.""" |
93 | | - mock_transform = MagicMock() |
94 | | - mock_pipeline.return_value = mock_transform |
95 | | - |
96 | | - preprocess_dicom_series("dummy_path.dcm", "ct") |
97 | | - |
98 | | - mock_pipeline.assert_called_once() |
99 | | - mock_transform.assert_called_once_with("dummy_path.dcm") |
100 | | - |
101 | | - |
102 | | -@patch('monai.transforms.clinical_preprocessing.get_mri_preprocessing_pipeline') |
103 | | -def test_preprocess_dicom_series_mri(mock_pipeline): |
104 | | - """Test preprocess_dicom_series with MRI modality.""" |
105 | | - mock_transform = MagicMock() |
106 | | - mock_pipeline.return_value = mock_transform |
107 | | - |
108 | | - preprocess_dicom_series("dummy_path.dcm", "MRI") |
109 | | - |
110 | | - mock_pipeline.assert_called_once() |
111 | | - mock_transform.assert_called_once_with("dummy_path.dcm") |
112 | | - |
113 | | - |
114 | | -@patch('monai.transforms.clinical_preprocessing.get_mri_preprocessing_pipeline') |
115 | | -def test_preprocess_dicom_series_mr(mock_pipeline): |
116 | | - """Test preprocess_dicom_series with MR modality.""" |
117 | | - mock_transform = MagicMock() |
118 | | - mock_pipeline.return_value = mock_transform |
119 | | - |
120 | | - preprocess_dicom_series("dummy_path.dcm", "MR") |
121 | | - |
122 | | - mock_pipeline.assert_called_once() |
123 | | - mock_transform.assert_called_once_with("dummy_path.dcm") |
| 27 | + assert isinstance(pipeline.transforms[0], LoadImage) |
| 28 | + assert isinstance(pipeline.transforms[1], EnsureChannelFirst) |
| 29 | + assert isinstance(pipeline.transforms[2], NormalizeIntensity) |
124 | 30 |
|
125 | 31 |
|
126 | 32 | def test_preprocess_dicom_series_invalid_modality(): |
127 | | - """Test preprocess_dicom_series raises ValueError for unsupported modality.""" |
128 | | - try: |
| 33 | + """Test preprocess_dicom_series raises UnsupportedModalityError for unsupported modality.""" |
| 34 | + with pytest.raises(UnsupportedModalityError, match=r"Unsupported modality.*PET.*CT, MR, MRI"): |
129 | 35 | preprocess_dicom_series("dummy_path.dcm", "PET") |
130 | | - assert False, "Should have raised ValueError" |
131 | | - except ValueError as e: |
132 | | - error_message = str(e) |
133 | | - assert "Unsupported modality" in error_message |
134 | | - assert "PET" in error_message |
135 | | - assert "CT, MR, MRI" in error_message |
136 | 36 |
|
137 | 37 |
|
138 | 38 | def test_preprocess_dicom_series_invalid_type(): |
139 | | - """Test preprocess_dicom_series raises TypeError for non-string modality.""" |
140 | | - try: |
| 39 | + """Test preprocess_dicom_series raises ModalityTypeError for non-string modality.""" |
| 40 | + with pytest.raises(ModalityTypeError, match=r"modality must be a string, got int"): |
141 | 41 | preprocess_dicom_series("dummy_path.dcm", 123) |
142 | | - assert False, "Should have raised TypeError" |
143 | | - except TypeError as e: |
144 | | - error_message = str(e) |
0 commit comments