Passed
Push — master ( 0b0a34...8a30f9 )
by Fernando
01:06
created

torchio.datasets.mni.pediatric   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B Pediatric.__init__() 0 25 5

1 Function

Rating   Name   Duplication   Size   Complexity  
A format_age() 0 4 1
1
import urllib.parse
2
from torchvision.datasets.utils import download_and_extract_archive
3
from ...utils import get_torchio_cache_dir
4
from ... import Image, LABEL
5
from .mni import SubjectMNI
6
7
8
SUPPORTED_YEARS = (
9
    (4.5, 18.5),
10
    (4.5, 8.5),
11
    (7, 11),
12
    (7.5, 13.5),
13
    (10, 14),
14
    (13, 18.5),
15
)
16
17
def format_age(n):
18
    integer = int(n)
19
    decimal = int(10 * (n - integer))
20
    return f'{integer:02d}.{decimal}'
21
22
23
class Pediatric(SubjectMNI):
24
    """MNI pediatric atlases
25
26
    Arguments:
27
        years: Tuple of 2 years. Possible values are: ``[(4.5, 18.5),
28
            (4.5, 8.5),
29
            (7, 11),
30
            (7.5, 13.5),
31
            (10, 14),
32
            (13, 18.5)]``.
33
    """
34
    def __init__(self, years, symmetric=False):
35
        self.url_dir = 'http://www.bic.mni.mcgill.ca/~vfonov/nihpd/obj1/'
36
        sym_string = 'sym' if symmetric else 'asym'
37
        if not isinstance(years, tuple) or years not in SUPPORTED_YEARS:
38
            message = f'Years must be a tuple in {SUPPORTED_YEARS}'
39
            raise ValueError(message)
40
        a, b = years
41
        file_id = f'{sym_string}_{format_age(a)}-{format_age(b)}'
42
        self.name = f'nihpd_{file_id}_nifti'
43
        self.filename = f'{self.name}.zip'
44
        self.url = urllib.parse.urljoin(self.url_dir, self.filename)
45
        download_root = get_torchio_cache_dir() / self.name
46
        if download_root.is_dir():
47
            print(f'Using cache found in {download_root}')
48
        else:
49
            download_and_extract_archive(
50
                self.url,
51
                download_root=download_root,
52
                filename=self.filename,
53
            )
54
        super().__init__(
55
            t1=Image(download_root / f'nihpd_{file_id}_t1w.nii'),
56
            t2=Image(download_root / f'nihpd_{file_id}_t2w.nii'),
57
            pd=Image(download_root / f'nihpd_{file_id}_pdw.nii'),
58
            mask=Image(download_root / f'nihpd_{file_id}_pdw.nii', type=LABEL),
59
        )
60