|
1
|
|
|
import tempfile |
|
2
|
|
|
import unittest |
|
3
|
|
|
from pathlib import Path |
|
4
|
|
|
import torch |
|
5
|
|
|
import numpy as np |
|
6
|
|
|
import nibabel as nib |
|
7
|
|
|
import SimpleITK as sitk |
|
8
|
|
|
from ..utils import TorchioTestCase |
|
9
|
|
|
from torchio.data import io, Image |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestIO(TorchioTestCase): |
|
13
|
|
|
"""Tests for `io` module.""" |
|
14
|
|
|
def setUp(self): |
|
15
|
|
|
super().setUp() |
|
16
|
|
|
self.write_dicom() |
|
17
|
|
|
string = ( |
|
18
|
|
|
'1.5 0.18088 -0.124887 0.65072 ' |
|
19
|
|
|
'-0.20025 0.965639 -0.165653 -11.6452 ' |
|
20
|
|
|
'0.0906326 0.18661 0.978245 11.4002 ' |
|
21
|
|
|
'0 0 0 1 ' |
|
22
|
|
|
) |
|
23
|
|
|
tensor = torch.from_numpy(np.fromstring(string, sep=' ').reshape(4, 4)) |
|
24
|
|
|
self.matrix = tensor |
|
25
|
|
|
|
|
26
|
|
|
def write_dicom(self): |
|
27
|
|
|
self.dicom_dir = self.dir / 'dicom' |
|
28
|
|
|
self.dicom_dir.mkdir(exist_ok=True) |
|
29
|
|
|
self.dicom_path = self.dicom_dir / 'dicom.dcm' |
|
30
|
|
|
self.nii_path = self.get_image_path('read_image') |
|
31
|
|
|
writer = sitk.ImageFileWriter() |
|
32
|
|
|
writer.SetFileName(str(self.dicom_path)) |
|
33
|
|
|
image = sitk.ReadImage(str(self.nii_path)) |
|
34
|
|
|
image = sitk.Cast(image, sitk.sitkUInt16) |
|
35
|
|
|
image = image[0] # dicom reader supports 2D only |
|
36
|
|
|
writer.Execute(image) |
|
37
|
|
|
|
|
38
|
|
|
def test_read_image(self): |
|
39
|
|
|
# I need to find something readable by nib but not sitk |
|
40
|
|
|
io.read_image(self.nii_path) |
|
41
|
|
|
io.read_image(self.nii_path, itk_first=True) |
|
42
|
|
|
|
|
43
|
|
|
def test_read_dicom_file(self): |
|
44
|
|
|
io.read_image(self.dicom_path) |
|
45
|
|
|
|
|
46
|
|
|
def test_read_dicom_dir(self): |
|
47
|
|
|
io.read_image(self.dicom_dir) |
|
48
|
|
|
|
|
49
|
|
|
def test_dicom_dir_missing(self): |
|
50
|
|
|
with self.assertRaises(FileNotFoundError): |
|
51
|
|
|
io._read_dicom('missing') |
|
52
|
|
|
|
|
53
|
|
|
def test_dicom_dir_no_files(self): |
|
54
|
|
|
empty = self.dir / 'empty' |
|
55
|
|
|
empty.mkdir() |
|
56
|
|
|
with self.assertRaises(FileNotFoundError): |
|
57
|
|
|
io._read_dicom(empty) |
|
58
|
|
|
|
|
59
|
|
|
def write_read_matrix(self, suffix): |
|
60
|
|
|
out_path = self.dir / f'matrix{suffix}' |
|
61
|
|
|
io.write_matrix(self.matrix, out_path) |
|
62
|
|
|
matrix = io.read_matrix(out_path) |
|
63
|
|
|
assert torch.allclose(matrix, self.matrix) |
|
64
|
|
|
|
|
65
|
|
|
def test_matrix_itk(self): |
|
66
|
|
|
self.write_read_matrix('.tfm') |
|
67
|
|
|
self.write_read_matrix('.h5') |
|
68
|
|
|
|
|
69
|
|
|
def test_matrix_txt(self): |
|
70
|
|
|
self.write_read_matrix('.txt') |
|
71
|
|
|
|
|
72
|
|
|
def save_load_save_load(self, dimensions): |
|
73
|
|
|
path = self.dir / f'img{dimensions}d.nii' |
|
74
|
|
|
shape = [4] |
|
75
|
|
|
for _ in range(dimensions - 1): |
|
76
|
|
|
shape.append(shape[-1] + 1) |
|
77
|
|
|
nii = nib.Nifti1Image(np.random.rand(*shape), np.eye(4)) |
|
78
|
|
|
nii.to_filename(path) |
|
79
|
|
|
tensor, _ = io.read_image(path) |
|
80
|
|
|
assert tensor.ndim == dimensions |
|
81
|
|
|
image = Image(path) |
|
82
|
|
|
image.save(path) |
|
83
|
|
|
tensor, _ = io.read_image(path) |
|
84
|
|
|
assert tensor.ndim == dimensions |
|
85
|
|
|
|
|
86
|
|
|
def test_nd(self): |
|
87
|
|
|
self.save_load_save_load(2) |
|
88
|
|
|
self.save_load_save_load(3) |
|
89
|
|
|
self.save_load_save_load(4) |
|
90
|
|
|
|