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