Passed
Push — master ( 091b4a...ebbd14 )
by Fernando
01:12
created

ICBM2009CNonlinearSymmetric.__init__()   B

Complexity

Conditions 5

Size

Total Lines 50
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 40
nop 2
dl 0
loc 50
rs 8.4533
c 0
b 0
f 0
1
import urllib.parse
2
import torch
3
from torchvision.datasets.utils import download_and_extract_archive
4
from ...utils import get_torchio_cache_dir, compress
5
from ... import ScalarImage, LabelMap, DATA
6
from .mni import SubjectMNI
7
8
9
class ICBM2009CNonlinearSymmetric(SubjectMNI):
10
    r"""ICBM template.
11
12
    More information can be found in the `website
13
    <http://www.bic.mni.mcgill.ca/ServicesAtlases/ICBM152NLin2009>`_.
14
15
    .. image:: http://www.bic.mni.mcgill.ca/uploads/ServicesAtlases/mni_icbm152_sym_09c_small.jpg
16
        :alt: ICBM 2009c Nonlinear Symmetric
17
18
    Args:
19
        load_4d_tissues: If ``True``, the tissue probability maps will be loaded
20
            together into a 4D image. Otherwise, they will be loaded into
21
            independent images.
22
23
    Example:
24
        >>> import torchio
25
        >>> icbm = torchio.datasets.ICBM2009CNonlinearSymmetric()
26
        >>> icbm
27
        ICBM2009CNonlinearSymmetric(Keys: ('t1', 'eyes', 'face', 'brain', 't2', 'pd', 'tissues'); images: 7)
28
        >>> icbm = torchio.datasets.ICBM2009CNonlinearSymmetric(load_4d_tissues=False)
29
        >>> icbm
30
        ICBM2009CNonlinearSymmetric(Keys: ('t1', 'eyes', 'face', 'brain', 't2', 'pd', 'gm', 'wm', 'csf'); images: 9)
31
32
    """
33
    def __init__(self, load_4d_tissues: bool = True):
34
        self.name = 'mni_icbm152_nlin_sym_09c_nifti'
35
        self.url_base = 'http://www.bic.mni.mcgill.ca/~vfonov/icbm/2009/'
36
        self.filename = f'{self.name}.zip'
37
        self.url = urllib.parse.urljoin(self.url_base, self.filename)
38
        download_root = get_torchio_cache_dir() / self.name
39
        if download_root.is_dir():
40
            print(f'Using cache found in {download_root}')  # noqa: T001
41
        else:
42
            download_and_extract_archive(
43
                self.url,
44
                download_root=download_root,
45
                filename=self.filename,
46
                remove_finished=True,
47
            )
48
49
        files_dir = download_root / 'mni_icbm152_nlin_sym_09c'
50
51
        p = files_dir / 'mni_icbm152'
52
        m = 'tal_nlin_sym_09c'
53
        s = '.nii.gz'
54
55
        tissues_path = files_dir / f'{p}_tissues_{m}.nii.gz'
56
        if not tissues_path.is_file():
57
            gm = LabelMap(f'{p}_gm_{m}.nii')
58
            wm = LabelMap(f'{p}_wm_{m}.nii')
59
            csf = LabelMap(f'{p}_csf_{m}.nii')
60
            gm[DATA] = torch.cat((gm[DATA], wm[DATA], csf[DATA]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DATA does not seem to be defined.
Loading history...
61
            gm.save(tissues_path)
62
63
        for fp in files_dir.glob('*.nii'):
64
            compress(fp, fp.with_suffix('.nii.gz'))
65
            fp.unlink()
66
67
        subject_dict = dict(
68
            t1=ScalarImage(f'{p}_t1_{m}{s}'),
69
            eyes=LabelMap(f'{p}_t1_{m}_eye_mask{s}'),
70
            face=LabelMap(f'{p}_t1_{m}_face_mask{s}'),
71
            brain=LabelMap(f'{p}_t1_{m}_mask{s}'),
72
            t2=ScalarImage(f'{p}_t2_{m}{s}'),
73
            pd=ScalarImage(f'{p}_csf_{m}{s}'),
74
        )
75
        if load_4d_tissues:
76
            subject_dict['tissues'] = LabelMap(tissues_path)
77
        else:
78
            subject_dict['gm'] = LabelMap(f'{p}_gm_{m}{s}')
79
            subject_dict['wm'] = LabelMap(f'{p}_wm_{m}{s}')
80
            subject_dict['csf'] = LabelMap(f'{p}_csf_{m}{s}')
81
82
        super().__init__(subject_dict)
83