Passed
Push — master ( 6a63bf...106cb5 )
by Fernando
01:25
created

torchio.datasets.rsna_miccai   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 44
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B RSNAMICCAI._get_subjects() 0 32 8
A RSNAMICCAI.__init__() 0 5 1
1
import csv
2
from typing import List
3
from pathlib import Path
4
5
from ..typing import TypePath
6
from .. import SubjectsDataset, Subject, ScalarImage
7
8
9
class RSNAMICCAI(SubjectsDataset):
10
    """RSNA-MICCAI Brain Tumor Radiogenomic Classification challenge dataset.
11
12
    This is a helper class for the dataset used in the
13
    `RSNA-MICCAI Brain Tumor Radiogenomic Classification challenge`_ hosted on
14
    `kaggle <https://www.kaggle.com/>`_. The dataset must be downloaded before
15
    instantiating this class (as oposed to, e.g., :class:`torchio.datasets.IXI`).
16
17
    If you reference or use the dataset in any form, include the following
18
    citation:
19
20
    U.Baid, et al., "The RSNA-ASNR-MICCAI BraTS 2021 Benchmark on Brain Tumor
21
    Segmentation and Radiogenomic Classification", arXiv:2107.02314, 2021.
22
23
    .. _RSNA-MICCAI Brain Tumor Radiogenomic Classification challenge: https://www.kaggle.com/c/rsna-miccai-brain-tumor-radiogenomic-classification
24
    """
25
    id_key = 'BraTS21ID'
26
    label_key = 'MGMT_value'
27
    modalities = 'T1w', 'T1wCE', 'T2w', 'FLAIR'
28
29
    def __init__(self, root_dir: TypePath, train: bool = True, **kwargs):
30
        self.root_dir = Path(root_dir).expanduser().resolve()
31
        subjects = self._get_subjects(self.root_dir, train)
32
        super().__init__(subjects, **kwargs)
33
        self.train = train
34
35
    def _get_subjects(self, root_dir: Path, train: bool) -> List[Subject]:
36
        subjects = []
37
        if train:
38
            csv_path = root_dir / 'train_labels.csv'
39
            with open(csv_path) as csvfile:
40
                reader = csv.DictReader(csvfile)
41
                labels_dict = {
42
                    row[self.id_key]: int(row[self.label_key])
43
                    for row in reader
44
                }
45
            subjects_dir = root_dir / 'train'
46
        else:
47
            subjects_dir = root_dir / 'test'
48
49
        for subject_dir in sorted(subjects_dir.iterdir()):
50
            subject_id = subject_dir.name
51
            try:
52
                int(subject_id)
53
            except ValueError:
54
                continue
55
            images_dict = {self.id_key: subject_dir.name}
56
            if train:
57
                images_dict[self.label_key] = labels_dict[subject_id]
0 ignored issues
show
introduced by
The variable labels_dict does not seem to be defined for all execution paths.
Loading history...
58
            for modality in self.modalities:
59
                image_dir = subject_dir / modality
60
                filepaths = list(image_dir.iterdir())
61
                num_files = len(filepaths)
62
                path = filepaths[0] if num_files == 1 else image_dir
63
                images_dict[modality] = ScalarImage(path)
64
            subject = Subject(images_dict)
65
            subjects.append(subject)
66
        return subjects
67