|
| 1 | +"""Round-trip I/O tests for NII.load, NII.save, and NII.from_numpy.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from TPTBox import NII |
| 12 | + |
| 13 | + |
| 14 | +def _make_nii(arr: np.ndarray, seg: bool = True, zoom=(1.0, 1.0, 1.0)) -> NII: |
| 15 | + affine = np.diag([*zoom, 1.0]) |
| 16 | + return NII.from_numpy(arr, affine=affine, seg=seg) |
| 17 | + |
| 18 | + |
| 19 | +class Test_NII_IO(unittest.TestCase): |
| 20 | + """Verify that NII survives a save→load round-trip with consistent shape and metadata.""" |
| 21 | + |
| 22 | + def test_save_load_roundtrip_shape(self): |
| 23 | + arr = np.zeros((10, 12, 14), dtype=np.uint8) |
| 24 | + arr[3:7, 3:9, 3:11] = 1 |
| 25 | + nii = _make_nii(arr, zoom=(1.0, 2.0, 3.0)) |
| 26 | + with tempfile.NamedTemporaryFile(suffix=".nii.gz", delete=False) as f: |
| 27 | + path = Path(f.name) |
| 28 | + try: |
| 29 | + nii.save(path, verbose=False) |
| 30 | + loaded = NII.load(path, seg=True) |
| 31 | + self.assertEqual(loaded.shape, nii.shape) |
| 32 | + finally: |
| 33 | + path.unlink(missing_ok=True) |
| 34 | + |
| 35 | + def test_save_load_roundtrip_data(self): |
| 36 | + arr = np.arange(27, dtype=np.uint8).reshape(3, 3, 3) |
| 37 | + nii = _make_nii(arr, seg=True) |
| 38 | + with tempfile.NamedTemporaryFile(suffix=".nii.gz", delete=False) as f: |
| 39 | + path = Path(f.name) |
| 40 | + try: |
| 41 | + nii.save(path, verbose=False) |
| 42 | + loaded = NII.load(path, seg=True) |
| 43 | + np.testing.assert_array_equal(loaded.get_array(), arr) |
| 44 | + finally: |
| 45 | + path.unlink(missing_ok=True) |
| 46 | + |
| 47 | + def test_save_load_preserves_zoom(self): |
| 48 | + arr = np.zeros((5, 6, 7), dtype=np.uint8) |
| 49 | + nii = _make_nii(arr, zoom=(1.5, 2.5, 3.5)) |
| 50 | + with tempfile.NamedTemporaryFile(suffix=".nii.gz", delete=False) as f: |
| 51 | + path = Path(f.name) |
| 52 | + try: |
| 53 | + nii.save(path, verbose=False) |
| 54 | + loaded = NII.load(path, seg=True) |
| 55 | + for orig, restored in zip(nii.zoom, loaded.zoom): |
| 56 | + self.assertAlmostEqual(orig, restored, places=4) |
| 57 | + finally: |
| 58 | + path.unlink(missing_ok=True) |
| 59 | + |
| 60 | + def test_save_load_seg_flag(self): |
| 61 | + arr = np.zeros((4, 4, 4), dtype=np.uint8) |
| 62 | + arr[1:3, 1:3, 1:3] = 1 |
| 63 | + nii = _make_nii(arr, seg=True) |
| 64 | + with tempfile.NamedTemporaryFile(suffix=".nii.gz", delete=False) as f: |
| 65 | + path = Path(f.name) |
| 66 | + try: |
| 67 | + nii.save(path, verbose=False) |
| 68 | + loaded = NII.load(path, seg=True) |
| 69 | + self.assertTrue(loaded.seg) |
| 70 | + finally: |
| 71 | + path.unlink(missing_ok=True) |
| 72 | + |
| 73 | + |
| 74 | +class Test_NII_FromNumpy(unittest.TestCase): |
| 75 | + """Tests for NII.from_numpy factory method.""" |
| 76 | + |
| 77 | + def test_shape(self): |
| 78 | + arr = np.zeros((8, 9, 10), dtype=np.uint8) |
| 79 | + nii = NII.from_numpy(arr, affine=np.eye(4), seg=True) |
| 80 | + self.assertEqual(nii.shape, (8, 9, 10)) |
| 81 | + |
| 82 | + def test_seg_flag_true(self): |
| 83 | + arr = np.zeros((4, 4, 4), dtype=np.uint8) |
| 84 | + self.assertTrue(NII.from_numpy(arr, affine=np.eye(4), seg=True).seg) |
| 85 | + |
| 86 | + def test_seg_flag_false(self): |
| 87 | + arr = np.zeros((4, 4, 4), dtype=np.uint8) |
| 88 | + self.assertFalse(NII.from_numpy(arr, affine=np.eye(4), seg=False).seg) |
| 89 | + |
| 90 | + def test_affine_zoom_extracted(self): |
| 91 | + arr = np.zeros((5, 5, 5), dtype=np.uint8) |
| 92 | + affine = np.diag([2.0, 3.0, 4.0, 1.0]) |
| 93 | + nii = NII.from_numpy(arr, affine=affine, seg=True) |
| 94 | + self.assertAlmostEqual(nii.zoom[0], 2.0) |
| 95 | + self.assertAlmostEqual(nii.zoom[1], 3.0) |
| 96 | + self.assertAlmostEqual(nii.zoom[2], 4.0) |
| 97 | + |
| 98 | + def test_data_round_trip(self): |
| 99 | + arr = np.arange(24, dtype=np.int16).reshape(2, 3, 4) |
| 100 | + nii = NII.from_numpy(arr, affine=np.eye(4), seg=False) |
| 101 | + np.testing.assert_array_equal(nii.get_array(), arr) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments