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

Pediatric.get_subject_dict()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import urllib.parse
2
3
from ...utils import compress
4
from ... import ScalarImage, LabelMap
5
from ...download import download_and_extract_archive
6
from .mni import SubjectMNI
7
8
9
SUPPORTED_YEARS = (
10
    (4.5, 18.5),
11
    (4.5, 8.5),
12
    (7, 11),
13
    (7.5, 13.5),
14
    (10, 14),
15
    (13, 18.5),
16
)
17
18
19
def format_age(n):
20
    integer = int(n)
21
    decimal = int(10 * (n - integer))
22
    return f'{integer:02d}.{decimal}'
23
24
25
class Pediatric(SubjectMNI):
26
    """MNI pediatric atlases.
27
28
    See `the MNI website <http://nist.mni.mcgill.ca/?p=974>`_ for more
29
    information.
30
31
    .. image:: http://nist.mni.mcgill.ca/wp-content/uploads/2016/04/nihpd_asym_all_sm.jpg
32
        :alt: Pediatric MNI template
33
34
    Arguments:
35
        years: Tuple of 2 ages. Possible values are: ``(4.5, 18.5)``,
36
            ``(4.5, 8.5)``,
37
            ``(7, 11)``,
38
            ``(7.5, 13.5)``,
39
            ``(10, 14)`` and
40
            ``(13, 18.5)``.
41
        symmetric: If ``True``, the left-right symmetric templates will be
42
            used. Else, the asymmetric (natural) templates will be used.
43
    """  # noqa: E501
44
    def __init__(self, years, symmetric=False):
45
        self.url_dir = 'http://www.bic.mni.mcgill.ca/~vfonov/nihpd/obj1/'
46
        sym_string = 'sym' if symmetric else 'asym'
47
        if not isinstance(years, tuple) or years not in SUPPORTED_YEARS:
48
            message = f'Years must be a tuple in {SUPPORTED_YEARS}'
49
            raise ValueError(message)
50
        a, b = years
51
        self.file_id = f'{sym_string}_{format_age(a)}-{format_age(b)}'
52
        self.name = f'nihpd_{self.file_id}_nifti'
53
        self.filename = f'{self.name}.zip'
54
        self.url = urllib.parse.urljoin(self.url_dir, self.filename)
55
        if not self.download_root.is_dir():
56
            download_and_extract_archive(
57
                self.url,
58
                download_root=self.download_root,
59
                filename=self.filename,
60
            )
61
            (self.download_root / self.filename).unlink()
62
            for path in self.download_root.glob('*.nii'):
63
                compress(path)
64
                path.unlink()
65
66
        try:
67
            subject_dict = self.get_subject_dict('.nii.gz')
68
        except FileNotFoundError:  # for backward compatibility
69
            subject_dict = self.get_subject_dict('.nii')
70
        super().__init__(subject_dict)
71
72
    def get_subject_dict(self, extension):
73
        root = self.download_root
74
        subject_dict = {
75
            't1': ScalarImage(root / f'nihpd_{self.file_id}_t1w{extension}'),
76
            't2': ScalarImage(root / f'nihpd_{self.file_id}_t2w{extension}'),
77
            'pd': ScalarImage(root / f'nihpd_{self.file_id}_pdw{extension}'),
78
            'mask': LabelMap(root / f'nihpd_{self.file_id}_mask{extension}'),
79
        }
80
        return subject_dict
81