1
|
|
|
#!/usr/bin/env python |
2
|
|
|
import torch |
3
|
|
|
|
4
|
|
|
from torchio import DATA, SubjectsDataset |
5
|
|
|
from ..utils import TorchioTestCase |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestSubjectsDataset(TorchioTestCase): |
9
|
|
|
|
10
|
|
|
def test_indexing_nonint(self): |
11
|
|
|
dset = SubjectsDataset(self.subjects_list) |
12
|
|
|
dset[torch.tensor(0)] |
13
|
|
|
|
14
|
|
|
def test_images(self): |
15
|
|
|
self.iterate_dataset(self.subjects_list) |
16
|
|
|
|
17
|
|
|
def test_empty_subjects_list(self): |
18
|
|
|
with self.assertRaises(ValueError): |
19
|
|
|
self.iterate_dataset([]) |
20
|
|
|
|
21
|
|
|
def test_empty_subjects_tuple(self): |
22
|
|
|
with self.assertRaises(ValueError): |
23
|
|
|
self.iterate_dataset(()) |
24
|
|
|
|
25
|
|
|
def test_wrong_subjects_type(self): |
26
|
|
|
with self.assertRaises(TypeError): |
27
|
|
|
self.iterate_dataset(0) |
28
|
|
|
|
29
|
|
|
def test_wrong_subject_type_int(self): |
30
|
|
|
with self.assertRaises(TypeError): |
31
|
|
|
self.iterate_dataset([0]) |
32
|
|
|
|
33
|
|
|
def test_wrong_subject_type_dict(self): |
34
|
|
|
with self.assertRaises(TypeError): |
35
|
|
|
self.iterate_dataset([{}]) |
36
|
|
|
|
37
|
|
|
def test_wrong_index(self): |
38
|
|
|
with self.assertRaises(ValueError): |
39
|
|
|
self.dataset[:3] |
40
|
|
|
|
41
|
|
|
def test_wrong_transform_init(self): |
42
|
|
|
with self.assertRaises(ValueError): |
43
|
|
|
SubjectsDataset( |
44
|
|
|
self.subjects_list, |
45
|
|
|
transform={}, |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
def test_wrong_transform_arg(self): |
49
|
|
|
with self.assertRaises(ValueError): |
50
|
|
|
self.dataset.set_transform(1) |
51
|
|
|
|
52
|
|
|
@staticmethod |
53
|
|
|
def iterate_dataset(subjects_list): |
54
|
|
|
dataset = SubjectsDataset(subjects_list) |
55
|
|
|
for _ in dataset: |
56
|
|
|
pass |
57
|
|
|
|
58
|
|
|
def test_data_loader(self): |
59
|
|
|
from torch.utils.data import DataLoader |
60
|
|
|
subj_list = [self.sample_subject] |
61
|
|
|
dataset = SubjectsDataset(subj_list) |
62
|
|
|
loader = DataLoader(dataset, batch_size=1, shuffle=True) |
63
|
|
|
for batch in loader: |
64
|
|
|
batch['t1'][DATA] |
65
|
|
|
batch['label'][DATA] |
66
|
|
|
|