Passed
Push — master ( 68ba61...357cca )
by Fernando
01:15
created

ICBM2009CNonlinearSymmetryc.__init__()   B

Complexity

Conditions 5

Size

Total Lines 51
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 41
nop 2
dl 0
loc 51
rs 8.4293
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ICBM2009CNonlinearSymmetryc(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.ICBM2009CNonlinearSymmetryc()
26
        >>> icbm
27
        ICBM2009CNonlinearSymmetryc(Keys: ('t1', 'eyes', 'face', 'brain', 't2', 'pd', 'tissues'); images: 7)
28
        >>> icbm = torchio.datasets.ICBM2009CNonlinearSymmetryc(load_4d_tissues=False)
29
        >>> icbm
30
        ICBM2009CNonlinearSymmetryc(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 = f'mni_icbm152_nlin_sym_09c_nifti'
35
        self.url_base = 'http://www.bic.mni.mcgill.ca/~vfonov/icbm/2009/'
36
        dir_name = 'icbm_2009c_nonlinear_symmetric/'
37
        self.filename = f'{self.name}.zip'
38
        self.url = urllib.parse.urljoin(self.url_base, self.filename)
39
        download_root = get_torchio_cache_dir() / self.name
40
        if download_root.is_dir():
41
            print(f'Using cache found in {download_root}')
42
        else:
43
            download_and_extract_archive(
44
                self.url,
45
                download_root=download_root,
46
                filename=self.filename,
47
                remove_finished=True,
48
            )
49
50
        files_dir = download_root / 'mni_icbm152_nlin_sym_09c'
51
52
        p = files_dir / 'mni_icbm152'
53
        m = 'tal_nlin_sym_09c'
54
        s = '.nii.gz'
55
56
        tissues_path = files_dir / f'{p}_tissues_{m}.nii.gz'
57
        if not tissues_path.is_file():
58
            gm = LabelMap(f'{p}_gm_{m}.nii')
59
            wm = LabelMap(f'{p}_wm_{m}.nii')
60
            csf = LabelMap(f'{p}_csf_{m}.nii')
61
            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...
62
            gm.save(tissues_path)
63
64
        for fp in files_dir.glob('*.nii'):
65
            compress(fp, fp.with_suffix('.nii.gz'))
66
            fp.unlink()
67
68
        subject_dict = dict(
69
            t1=ScalarImage(f'{p}_t1_{m}{s}'),
70
            eyes=LabelMap(f'{p}_t1_{m}_eye_mask{s}'),
71
            face=LabelMap(f'{p}_t1_{m}_face_mask{s}'),
72
            brain=LabelMap(f'{p}_t1_{m}_mask{s}'),
73
            t2=ScalarImage(f'{p}_t2_{m}{s}'),
74
            pd=ScalarImage(f'{p}_csf_{m}{s}'),
75
        )
76
        if load_4d_tissues:
77
            subject_dict['tissues'] = LabelMap(tissues_path)
78
        else:
79
            subject_dict['gm'] = LabelMap(f'{p}_gm_{m}{s}')
80
            subject_dict['wm'] = LabelMap(f'{p}_wm_{m}{s}')
81
            subject_dict['csf'] = LabelMap(f'{p}_csf_{m}{s}')
82
83
        super().__init__(subject_dict)
84