|
1 | 1 | import copy |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import patch |
2 | 6 |
|
3 | 7 | import pytest |
4 | 8 | import torch |
@@ -85,3 +89,139 @@ def test_add_images_from_batch(self): |
85 | 89 | def test_empty_batch(self): |
86 | 90 | with pytest.raises(RuntimeError): |
87 | 91 | tio.utils.get_batch_images_and_size({}) |
| 92 | + |
| 93 | + def test_compress_default_output(self): |
| 94 | + """compress() creates .nii.gz from input with default output.""" |
| 95 | + with tempfile.TemporaryDirectory() as tmp: |
| 96 | + input_path = Path(tmp) / 'test.nii' |
| 97 | + input_path.write_bytes(b'fake nifti data') |
| 98 | + result = tio.utils.compress(input_path) |
| 99 | + assert result.exists() |
| 100 | + assert result.suffix == '.gz' |
| 101 | + |
| 102 | + def test_compress_explicit_output(self): |
| 103 | + """compress() writes to the specified output path.""" |
| 104 | + with tempfile.TemporaryDirectory() as tmp: |
| 105 | + input_path = Path(tmp) / 'test.nii' |
| 106 | + input_path.write_bytes(b'fake nifti data') |
| 107 | + output_path = Path(tmp) / 'compressed.nii.gz' |
| 108 | + result = tio.utils.compress(input_path, output_path) |
| 109 | + assert result == output_path |
| 110 | + assert result.exists() |
| 111 | + |
| 112 | + def test_history_collate_non_subject(self): |
| 113 | + """history_collate returns empty dict for non-Subject batches.""" |
| 114 | + result = tio.utils.history_collate([{'a': 1}]) |
| 115 | + assert result == {} |
| 116 | + |
| 117 | + def test_create_dummy_dataset_force(self): |
| 118 | + """create_dummy_dataset with force=True recreates directories.""" |
| 119 | + with tempfile.TemporaryDirectory() as tmp: |
| 120 | + subjects = tio.utils.create_dummy_dataset( |
| 121 | + num_images=2, |
| 122 | + size_range=(5, 10), |
| 123 | + directory=tmp, |
| 124 | + ) |
| 125 | + assert len(subjects) == 2 |
| 126 | + # Call again with force to recreate |
| 127 | + subjects = tio.utils.create_dummy_dataset( |
| 128 | + num_images=2, |
| 129 | + size_range=(5, 10), |
| 130 | + directory=tmp, |
| 131 | + force=True, |
| 132 | + ) |
| 133 | + assert len(subjects) == 2 |
| 134 | + |
| 135 | + def test_create_dummy_dataset_existing(self): |
| 136 | + """create_dummy_dataset loads from existing dirs without force.""" |
| 137 | + with tempfile.TemporaryDirectory() as tmp: |
| 138 | + tio.utils.create_dummy_dataset( |
| 139 | + num_images=2, |
| 140 | + size_range=(5, 10), |
| 141 | + directory=tmp, |
| 142 | + ) |
| 143 | + # Call again without force — loads from existing paths |
| 144 | + subjects = tio.utils.create_dummy_dataset( |
| 145 | + num_images=2, |
| 146 | + size_range=(5, 10), |
| 147 | + directory=tmp, |
| 148 | + ) |
| 149 | + assert len(subjects) == 2 |
| 150 | + |
| 151 | + def test_create_dummy_dataset_verbose(self): |
| 152 | + """create_dummy_dataset with verbose=True prints a message.""" |
| 153 | + with tempfile.TemporaryDirectory() as tmp: |
| 154 | + subjects = tio.utils.create_dummy_dataset( |
| 155 | + num_images=1, |
| 156 | + size_range=(5, 10), |
| 157 | + directory=tmp, |
| 158 | + verbose=True, |
| 159 | + ) |
| 160 | + assert len(subjects) == 1 |
| 161 | + |
| 162 | + def test_parse_spatial_shape_wrong_length(self): |
| 163 | + """parse_spatial_shape raises ValueError for non-3-element shapes.""" |
| 164 | + with pytest.raises(ValueError, match='3 elements'): |
| 165 | + tio.utils.parse_spatial_shape((10, 20)) |
| 166 | + |
| 167 | + def test_normalize_path(self): |
| 168 | + """normalize_path expands ~ and resolves to absolute path.""" |
| 169 | + result = tio.utils.normalize_path('~/test') |
| 170 | + assert result.is_absolute() |
| 171 | + assert '~' not in str(result) |
| 172 | + |
| 173 | + def test_guess_external_viewer_env_var(self): |
| 174 | + """guess_external_viewer returns SITK_SHOW_COMMAND if set.""" |
| 175 | + with patch.dict(os.environ, {'SITK_SHOW_COMMAND': '/usr/bin/viewer'}): |
| 176 | + result = tio.utils.guess_external_viewer() |
| 177 | + assert result == Path('/usr/bin/viewer') |
| 178 | + |
| 179 | + def test_guess_external_viewer_linux_itksnap(self): |
| 180 | + """guess_external_viewer finds itksnap on Linux.""" |
| 181 | + with ( |
| 182 | + patch('torchio.utils.sys') as mock_sys, |
| 183 | + patch('torchio.utils.shutil') as mock_shutil, |
| 184 | + patch.dict(os.environ, {}, clear=False), |
| 185 | + ): |
| 186 | + # Remove SITK_SHOW_COMMAND if present |
| 187 | + os.environ.pop('SITK_SHOW_COMMAND', None) |
| 188 | + mock_sys.platform = 'linux' |
| 189 | + mock_shutil.which = lambda x: '/usr/bin/itksnap' if x == 'itksnap' else None |
| 190 | + result = tio.utils.guess_external_viewer() |
| 191 | + assert result == Path('/usr/bin/itksnap') |
| 192 | + |
| 193 | + def test_guess_external_viewer_none(self): |
| 194 | + """guess_external_viewer returns None when no viewer found.""" |
| 195 | + with ( |
| 196 | + patch('torchio.utils.sys') as mock_sys, |
| 197 | + patch('torchio.utils.shutil') as mock_shutil, |
| 198 | + patch.dict(os.environ, {}, clear=False), |
| 199 | + ): |
| 200 | + os.environ.pop('SITK_SHOW_COMMAND', None) |
| 201 | + mock_sys.platform = 'unknown_platform' |
| 202 | + mock_shutil.which = lambda x: None |
| 203 | + result = tio.utils.guess_external_viewer() |
| 204 | + assert result is None |
| 205 | + |
| 206 | + def test_guess_external_viewer_linux_slicer(self): |
| 207 | + """guess_external_viewer finds Slicer on Linux when itksnap absent.""" |
| 208 | + with ( |
| 209 | + patch('torchio.utils.sys') as mock_sys, |
| 210 | + patch('torchio.utils.shutil') as mock_shutil, |
| 211 | + patch.dict(os.environ, {}, clear=False), |
| 212 | + ): |
| 213 | + os.environ.pop('SITK_SHOW_COMMAND', None) |
| 214 | + mock_sys.platform = 'linux' |
| 215 | + mock_shutil.which = lambda x: '/usr/bin/Slicer' if x == 'Slicer' else None |
| 216 | + result = tio.utils.guess_external_viewer() |
| 217 | + assert result == Path('/usr/bin/Slicer') |
| 218 | + |
| 219 | + def test_apply_transform_verbose_history(self): |
| 220 | + """apply_transform_to_file with verbose prints history.""" |
| 221 | + transform = tio.RandomFlip() |
| 222 | + tio.utils.apply_transform_to_file( |
| 223 | + self.get_image_path('input_v'), |
| 224 | + transform, |
| 225 | + self.get_image_path('output_v'), |
| 226 | + verbose=True, |
| 227 | + ) |
0 commit comments