File size: 5,673 Bytes
b01bc48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d49e2e9
b01bc48
 
 
 
 
aa362df
4120121
 
 
b01bc48
 
3dd87ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b01bc48
 
 
 
 
 
 
 
8745815
 
b01bc48
8745815
b01bc48
 
 
 
 
 
 
 
 
 
 
 
 
 
0a92020
 
 
 
 
d77fe25
b01bc48
 
 
 
0a92020
b01bc48
b926b05
9c5fa49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e53775a
693682c
b01bc48
b926b05
 
 
b01bc48
 
9c5fa49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b01bc48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import datasets
import pandas as pd

_CITATION = """\
@InProceedings{huggingface:dataset,
title = {RSNA-ATD2023},
author = {Yeow Zi Qin},
year = {2023}
}
"""

_DESCRIPTION = """\
The dataset is the processed version of Kaggle Competition: RSNA 2023 Abdominal Trauma Detection.
It comprises of segmentation of 205 series of CT scans with 5 classes (liver, spleen, right_kidney, 
left_kidney, bowel).
"""

_NAME = "RSNA-ATD2023"

_HOMEPAGE = f"https://huggingface.co/datasets/ziq/{_NAME}"

_LICENSE = "MIT"

_DATA = f"https://huggingface.co/datasets/ziq/{_NAME}/resolve/main/data/"


class RSNAATD(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    # "image_path": datasets.Value("string"),
                    "patient_id": datasets.Value("int64"),
                    "series_id": datasets.Value("int64"),
                    "frame_id": datasets.Value("int64"),
                    "image": datasets.Image(),
                    "mask": datasets.Image(),
                    "liver": datasets.Value("int16"),
                    "spleen": datasets.Value("int16"),
                    "right_kidney": datasets.Value("int16"),
                    "left_kidney": datasets.Value("int16"),
                    "bowel": datasets.Value("int16"),
                    "aortic_hu": datasets.Value("int16"),
                    "incomplete_organ": datasets.Value("int16"),
                    "bowel_healthy": datasets.Value("int16"),
                    "bowel_injury": datasets.Value("int16"),
                    "extravasation_healthy": datasets.Value("int16"),
                    "extravasation_injury": datasets.Value("int16"),
                    "kidney_healthy": datasets.Value("int16"),
                    "kidney_low": datasets.Value("int16"),
                    "kidney_high": datasets.Value("int16"),
                    "liver_healthy": datasets.Value("int16"),
                    "liver_low": datasets.Value("int16"),
                    "liver_high": datasets.Value("int16"),
                    "spleen_healthy": datasets.Value("int16"),
                    "spleen_low": datasets.Value("int16"),
                    "spleen_high": datasets.Value("int16"),
                    "any_injury": datasets.Value("int16"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        train_images = dl_manager.download(f"{_DATA}images.tar.gz")
        train_masks = dl_manager.download(f"{_DATA}masks.tar.gz")

        metadata = dl_manager.download(f"{_DATA}metadata.csv")
        train_images = dl_manager.iter_archive(train_images)
        train_masks = dl_manager.iter_archive(train_masks)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": train_images,
                    "masks": train_masks,
                    "metadata": metadata,
                },
            ),
        ]

    # def sort_key(self, x):
    #     patient_id, series_id, frame_id = (
    #         x[0][0].replace("images/", "").replace(".png", "").split("_")
    #     )
    #     return int(patient_id), int(series_id), int(frame_id)

    def _generate_examples(self, images, masks, metadata):
        df = pd.read_csv(metadata)

        for idx, ((image_path, image), (mask_path, mask)) in enumerate(
            zip(images, masks)
        ):
            row = df.loc[df["path"] == image_path.lower().replace("images/", "")]
            (
                liver,
                spleen,
                right_kidney,
                left_kidney,
                bowel,
                aortic_hu,
                incomplete_organ,
                bowel_healthy,
                bowel_injury,
                extravasation_healthy,
                extravasation_injury,
                kidney_healthy,
                kidney_low,
                kidney_high,
                liver_healthy,
                liver_low,
                liver_high,
                spleen_healthy,
                spleen_low,
                spleen_high,
                any_injury,
            ) = row.to_numpy()[0][4:]

            yield idx, {
                "patient_id": row["patient_id"].values[0],
                "series_id": row["series_id"].values[0],
                "frame_id": row["frame_id"].values[0],
                "image": {"path": image_path, "bytes": image.read()},
                "mask": {"path": mask_path, "bytes": mask.read()},
                "liver": liver,
                "spleen": spleen,
                "right_kidney": right_kidney,
                "left_kidney": left_kidney,
                "bowel": bowel,
                "aortic_hu": aortic_hu,
                "incomplete_organ": incomplete_organ,
                "bowel_healthy": bowel_healthy,
                "bowel_injury": bowel_injury,
                "extravasation_healthy": extravasation_healthy,
                "extravasation_injury": extravasation_injury,
                "kidney_healthy": kidney_healthy,
                "kidney_low": kidney_low,
                "kidney_high": kidney_high,
                "liver_healthy": liver_healthy,
                "liver_low": liver_low,
                "liver_high": liver_high,
                "spleen_healthy": spleen_healthy,
                "spleen_low": spleen_low,
                "spleen_high": spleen_high,
                "any_injury": any_injury,
            }