Passed
Push — master ( 178654...c7af60 )
by Fernando
01:30
created

Colin27.get_subject_dict()   A

Complexity

Conditions 3

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 23
rs 9.45
c 0
b 0
f 0
cc 3
nop 2
1
import urllib.parse
2
3
from ...utils import compress
4
from ...data import ScalarImage, LabelMap
5
from ...download import download_and_extract_archive
6
from .mni import SubjectMNI
7
8
9
TISSUES_2008 = {
10
    1: 'Cerebro-spinal fluid',
11
    2: 'Gray Matter',
12
    3: 'White Matter',
13
    4: 'Fat',
14
    5: 'Muscles',
15
    6: 'Skin and Muscles',
16
    7: 'Skull',
17
    9: 'Fat 2',
18
    10: 'Dura',
19
    11: 'Marrow',
20
    12: 'Vessels',
21
}
22
23
24
class Colin27(SubjectMNI):
25
    r"""Colin27 MNI template.
26
27
    More information can be found in the website of the
28
    `1998 <http://nist.mni.mcgill.ca/?p=935>`_ and
29
    `2008 <http://www.bic.mni.mcgill.ca/ServicesAtlases/Colin27Highres>`_
30
    versions.
31
32
    .. image:: http://www.bic.mni.mcgill.ca/uploads/ServicesAtlases/mni_colin27_2008.jpg
33
        :alt: MNI Colin 27 2008 version
34
35
    Arguments:
36
        version: Template year. It can be ``1998`` or ``2008``.
37
38
    .. warning:: The resolution of the ``2008`` version is quite high. The
39
        subject instance will contain four images of size
40
        :math:`362 \times 434 \times 362`, therefore applying a transform to
41
        it might take longer than expected.
42
43
    Example:
44
        >>> import torchio as tio
45
        >>> colin_1998 = tio.datasets.Colin27(version=1998)
46
        >>> colin_1998
47
        Colin27(Keys: ('t1', 'head', 'brain'); images: 3)
48
        >>> colin_1998.load()
49
        >>> colin_1998.t1
50
        ScalarImage(shape: (1, 181, 217, 181); spacing: (1.00, 1.00, 1.00); orientation: RAS+; memory: 27.1 MiB; type: intensity)
51
        >>>
52
        >>> colin_2008 = tio.datasets.Colin27(version=2008)
53
        >>> colin_2008
54
        Colin27(Keys: ('t1', 't2', 'pd', 'cls'); images: 4)
55
        >>> colin_2008.load()
56
        >>> colin_2008.t1
57
        ScalarImage(shape: (1, 362, 434, 362); spacing: (0.50, 0.50, 0.50); orientation: RAS+; memory: 217.0 MiB; type: intensity)
58
59
    """  # noqa: E501
60
    def __init__(self, version=1998):
61
        if version not in (1998, 2008):
62
            raise ValueError(f'Version must be 1998 or 2008, not "{version}"')
63
        self.version = version
64
        self.name = f'mni_colin27_{version}_nifti'
65
        self.url_dir = urllib.parse.urljoin(self.url_base, 'colin27/')
66
        self.filename = f'{self.name}.zip'
67
        self.url = urllib.parse.urljoin(self.url_dir, self.filename)
68
        if not self.download_root.is_dir():
69
            download_and_extract_archive(
70
                self.url,
71
                download_root=self.download_root,
72
                filename=self.filename,
73
            )
74
75
            # Fix label map (https://github.com/fepegar/torchio/issues/220)
76
            if version == 2008:
77
                path = self.download_root / 'colin27_cls_tal_hires.nii'
78
                cls_image = LabelMap(path)
79
                cls_image.set_data(cls_image.data.round().byte())
80
                cls_image.save(path)
81
82
            (self.download_root / self.filename).unlink()
83
            for path in self.download_root.glob('*.nii'):
84
                compress(path)
85
                path.unlink()
86
87
        try:
88
            subject_dict = self.get_subject_dict(extension='.nii.gz')
89
        except FileNotFoundError:  # for backward compatibility
90
            subject_dict = self.get_subject_dict(extension='.nii')
91
        super().__init__(subject_dict)
92
93
    def get_subject_dict(self, extension):
94
        if self.version == 1998:
95
            t1, head, mask = [
96
                self.download_root / f'colin27_t1_tal_lin{suffix}{extension}'
97
                for suffix in ('', '_headmask', '_mask')
98
            ]
99
            subject_dict = {
100
                't1': ScalarImage(t1),
101
                'head': LabelMap(head),
102
                'brain': LabelMap(mask),
103
            }
104
        elif self.version == 2008:
105
            t1, t2, pd, label = [
106
                self.download_root / f'colin27_{name}_tal_hires{extension}'
107
                for name in ('t1', 't2', 'pd', 'cls')
108
            ]
109
            subject_dict = {
110
                't1': ScalarImage(t1),
111
                't2': ScalarImage(t2),
112
                'pd': ScalarImage(pd),
113
                'cls': LabelMap(label, labels=TISSUES_2008),
114
            }
115
        return subject_dict
0 ignored issues
show
introduced by
The variable subject_dict does not seem to be defined for all execution paths.
Loading history...
116