Passed
Push — master ( 83d594...05eb75 )
by Fernando
02:10
created

torchio.datasets.fpg.FPG.__init__()   B

Complexity

Conditions 4

Size

Total Lines 45
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 45
rs 8.9919
c 0
b 0
f 0
cc 4
nop 2
1
import urllib.parse
2
from .. import Subject, ScalarImage, LabelMap, DATA_REPO
3
from ..data.io import read_matrix
4
from ..download import download_url
5
from ..utils import get_torchio_cache_dir
6
7
8
class FPG(Subject):
9
    """3T :math:`T_1`-weighted brain MRI and corresponding parcellation.
10
11
    Args:
12
        load_all: If ``True``, three more images will be loaded: a
13
            :math:`T_2`-weighted MRI, a diffusion MRI and a functional MRI.
14
    """
15
    def __init__(self, load_all: bool = False):
16
        repo_dir = urllib.parse.urljoin(DATA_REPO, 'fernando/')
17
18
        self.filenames = {
19
            't1': 't1.nii.gz',
20
            'seg': 't1_seg_gif.nii.gz',
21
            'rigid': 't1_to_mni.tfm',
22
            'affine': 't1_to_mni_affine.h5',
23
        }
24
        if load_all:
25
            self.filenames['t2'] = 't2.nii.gz'
26
            self.filenames['fmri'] = 'fmri.nrrd'
27
            self.filenames['dmri'] = 'dmri.nrrd'
28
29
        download_root = get_torchio_cache_dir() / 'fpg'
30
        for filename in self.filenames.values():
31
            download_url(
32
                urllib.parse.urljoin(repo_dir, filename),
33
                download_root,
34
                filename=filename,
35
            )
36
37
        rigid = read_matrix(download_root / self.filenames['rigid'])
38
        affine = read_matrix(download_root / self.filenames['affine'])
39
        subject_dict = {
40
            't1': ScalarImage(
41
                download_root / self.filenames['t1'],
42
                rigid_matrix=rigid,
43
                affine_matrix=affine,
44
            ),
45
            'seg': LabelMap(
46
                download_root / self.filenames['seg'],
47
                rigid_matrix=rigid,
48
                affine_matrix=affine,
49
            ),
50
        }
51
        if load_all:
52
            subject_dict['t2'] = ScalarImage(
53
                download_root / self.filenames['t2'])
54
            subject_dict['fmri'] = ScalarImage(
55
                download_root / self.filenames['fmri'])
56
            subject_dict['dmri'] = ScalarImage(
57
                download_root / self.filenames['dmri'])
58
        super().__init__(subject_dict)
59
        self.gif_colors = GIF_COLORS
60
61
    def plot(self, *args, **kwargs):
62
        super().plot(*args, **kwargs, cmap_dict={'seg': self.gif_colors})
63
64
65
GIF_COLORS = {
66
    0: (0, 0, 0),
67
    1: (0, 0, 0),
68
    5: (127, 255, 212),
69
    12: (240, 230, 140),
70
    16: (176, 48, 96),
71
    24: (48, 176, 96),
72
    31: (48, 176, 96),
73
    32: (103, 255, 255),
74
    33: (103, 255, 255),
75
    35: (238, 186, 243),
76
    36: (119, 159, 176),
77
    37: (122, 186, 220),
78
    38: (122, 186, 220),
79
    39: (96, 204, 96),
80
    40: (96, 204, 96),
81
    41: (220, 247, 164),
82
    42: (220, 247, 164),
83
    43: (205, 62, 78),
84
    44: (205, 62, 78),
85
    45: (225, 225, 225),
86
    46: (225, 225, 225),
87
    47: (60, 60, 60),
88
    48: (220, 216, 20),
89
    49: (220, 216, 20),
90
    50: (196, 58, 250),
91
    51: (196, 58, 250),
92
    52: (120, 18, 134),
93
    53: (120, 18, 134),
94
    54: (255, 165, 0),
95
    55: (255, 165, 0),
96
    56: (12, 48, 255),
97
    57: (12, 48, 225),
98
    58: (236, 13, 176),
99
    59: (236, 13, 176),
100
    60: (0, 118, 14),
101
    61: (0, 118, 14),
102
    62: (165, 42, 42),
103
    63: (165, 42, 42),
104
    64: (160, 32, 240),
105
    65: (160, 32, 240),
106
    66: (56, 192, 255),
107
    67: (56, 192, 255),
108
    70: (255, 225, 225),
109
    72: (184, 237, 194),
110
    73: (180, 231, 250),
111
    74: (225, 183, 231),
112
    76: (180, 180, 180),
113
    77: (180, 180, 180),
114
    81: (245, 255, 200),
115
    82: (255, 230, 255),
116
    83: (245, 245, 245),
117
    84: (220, 255, 220),
118
    85: (220, 220, 220),
119
    86: (200, 255, 255),
120
    87: (250, 220, 200),
121
    89: (245, 255, 200),
122
    90: (255, 230, 255),
123
    91: (245, 245, 245),
124
    92: (220, 255, 220),
125
    93: (220, 220, 220),
126
    94: (200, 255, 255),
127
    96: (140, 125, 255),
128
    97: (140, 125, 255),
129
    101: (255, 62, 150),
130
    102: (255, 62, 150),
131
    103: (160, 82, 45),
132
    104: (160, 82, 45),
133
    105: (165, 42, 42),
134
    106: (165, 42, 42),
135
    107: (205, 91, 69),
136
    108: (205, 91, 69),
137
    109: (100, 149, 237),
138
    110: (100, 149, 237),
139
    113: (135, 206, 235),
140
    114: (135, 206, 235),
141
    115: (250, 128, 114),
142
    116: (250, 128, 114),
143
    117: (255, 255, 0),
144
    118: (255, 255, 0),
145
    119: (221, 160, 221),
146
    120: (221, 160, 221),
147
    121: (0, 238, 0),
148
    122: (0, 238, 0),
149
    123: (205, 92, 92),
150
    124: (205, 92, 92),
151
    125: (176, 48, 96),
152
    126: (176, 48, 96),
153
    129: (152, 251, 152),
154
    130: (152, 251, 152),
155
    133: (50, 205, 50),
156
    134: (50, 205, 50),
157
    135: (0, 100, 0),
158
    136: (0, 100, 0),
159
    137: (173, 216, 230),
160
    138: (173, 216, 230),
161
    139: (153, 50, 204),
162
    140: (153, 50, 204),
163
    141: (160, 32, 240),
164
    142: (160, 32, 240),
165
    143: (0, 206, 208),
166
    144: (0, 206, 208),
167
    145: (51, 50, 135),
168
    146: (51, 50, 135),
169
    147: (135, 50, 74),
170
    148: (135, 50, 74),
171
    149: (218, 112, 214),
172
    150: (218, 112, 214),
173
    151: (240, 230, 140),
174
    152: (240, 230, 140),
175
    153: (255, 255, 0),
176
    154: (255, 255, 0),
177
    155: (255, 110, 180),
178
    156: (255, 110, 180),
179
    157: (0, 255, 255),
180
    158: (0, 255, 255),
181
    161: (100, 50, 100),
182
    162: (100, 50, 100),
183
    163: (178, 34, 34),
184
    164: (178, 34, 34),
185
    165: (255, 0, 255),
186
    166: (255, 0, 255),
187
    167: (39, 64, 139),
188
    168: (39, 64, 139),
189
    169: (255, 99, 71),
190
    170: (255, 99, 71),
191
    171: (255, 69, 0),
192
    172: (255, 69, 0),
193
    173: (210, 180, 140),
194
    174: (210, 180, 140),
195
    175: (0, 255, 127),
196
    176: (0, 255, 127),
197
    177: (74, 155, 60),
198
    178: (74, 155, 60),
199
    179: (255, 215, 0),
200
    180: (255, 215, 0),
201
    181: (238, 0, 0),
202
    182: (238, 0, 0),
203
    183: (46, 139, 87),
204
    184: (46, 139, 87),
205
    185: (238, 201, 0),
206
    186: (238, 201, 0),
207
    187: (102, 205, 170),
208
    188: (102, 205, 170),
209
    191: (255, 218, 185),
210
    192: (255, 218, 185),
211
    193: (238, 130, 238),
212
    194: (238, 130, 238),
213
    195: (255, 165, 0),
214
    196: (255, 165, 0),
215
    197: (255, 192, 203),
216
    198: (255, 192, 203),
217
    199: (244, 222, 179),
218
    200: (244, 222, 179),
219
    201: (208, 32, 144),
220
    202: (208, 32, 144),
221
    203: (34, 139, 34),
222
    204: (34, 139, 34),
223
    205: (125, 255, 212),
224
    206: (127, 255, 212),
225
    207: (0, 0, 128),
226
    208: (0, 0, 128),
227
}
228