Passed
Pull Request — master (#204)
by Fernando
57s
created

TestImagesDataset.iterate_dataset()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
"""Tests for ImagesDataset."""
4
5
import nibabel as nib
6
from torchio import DATA, ImagesDataset
7
from ..utils import TorchioTestCase
8
9
10
class TestImagesDataset(TorchioTestCase):
11
    """Tests for `ImagesDataset`."""
12
13
    def test_images(self):
14
        self.iterate_dataset(self.subjects_list)
15
16
    def test_empty_subjects_list(self):
17
        with self.assertRaises(ValueError):
18
            self.iterate_dataset([])
19
20
    def test_empty_subjects_tuple(self):
21
        with self.assertRaises(ValueError):
22
            self.iterate_dataset(())
23
24
    def test_wrong_subjects_type(self):
25
        with self.assertRaises(TypeError):
26
            self.iterate_dataset(0)
27
28
    def test_wrong_subject_type_int(self):
29
        with self.assertRaises(TypeError):
30
            self.iterate_dataset([0])
31
32
    def test_wrong_subject_type_dict(self):
33
        with self.assertRaises(TypeError):
34
            self.iterate_dataset([{}])
35
36
    def test_wrong_index(self):
37
        with self.assertRaises(ValueError):
38
            self.dataset[:3]
39
40
    def test_save_sample(self):
41
        dataset = ImagesDataset(
42
            self.subjects_list, transform=lambda x: x)
43
        _ = len(dataset)  # for coverage
44
        sample = dataset[0]
45
        output_path = self.dir / 'test.nii.gz'
46
        paths_dict = {'t1': output_path}
47
        with self.assertWarns(DeprecationWarning):
48
            dataset.save_sample(sample, paths_dict)
49
        nii = nib.load(str(output_path))
50
        ndims_output = len(nii.shape)
51
        ndims_sample = len(sample['t1'].shape)
52
        assert ndims_sample == ndims_output + 1
53
54
    def test_wrong_transform_init(self):
55
        with self.assertRaises(ValueError):
56
            ImagesDataset(
57
                self.subjects_list,
58
                transform=dict(),
59
            )
60
61
    def test_wrong_transform_arg(self):
62
        with self.assertRaises(ValueError):
63
            self.dataset.set_transform(1)
64
65
    @staticmethod
66
    def iterate_dataset(subjects_list):
67
        dataset = ImagesDataset(subjects_list)
68
        for _ in dataset:
69
            pass
70