File size: 4,091 Bytes
db08881
 
 
 
 
4d0199a
429652c
db08881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0953b46
 
 
 
db08881
72de623
db08881
 
 
dbbdc21
db08881
 
 
 
 
 
 
 
 
 
dca1d91
db08881
 
 
 
 
 
70891f4
b3356b8
43e8394
db08881
 
 
 
 
 
105172e
db08881
 
 
 
 
4c55b42
dbbdc21
 
 
db08881
fba824b
 
db08881
 
 
 
 
bbb7264
7309162
db08881
 
 
 
 
bbb7264
7309162
db08881
 
 
 
0b4c22c
db08881
b3356b8
 
db08881
 
f659003
bba4e00
92cfc82
064ce07
db08881
429652c
 
a952f1c
7c5ae34
 
 
 
 
 
81a6582
db08881
81a6582
b3356b8
43e8394
db08881
 
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
import datasets
import numpy as np
import pandas as pd
from pathlib import Path
import os
import json
import SimpleITK as sitk

_CITATION = """\
Alves, N., & Boulogne, L. (2023). Kidney CT Abnormality [Data set]. Zenodo. https://doi.org/10.5281/zenodo.8043408
"""

_DESCRIPTION = """\
This dataset in total is comprised of 986 .mha (medical high-resolution image) files. Each of these files contains multiple layers of CT scans of the kidney. 
The dataset has been speparated into train and test set by initial processing.

"""

_HOMEPAGE = "https://zenodo.org/records/8043408"

_LICENSE = "CC BY-NC-SA 4.0"

_URL = "https://huggingface.co/datasets/Euniceyeee/kidney-ct-abnormality"

# _URLS = {
#     'train': "https://huggingface.co/datasets/Euniceyeee/kidney-ct-abnormality/resolve/main/train.zip",
#     'test': "https://huggingface.co/datasets/Euniceyeee/kidney-ct-abnormality/resolve/main/test.zip"
# }
_URLS = {
    'kidney_CT': 'https://huggingface.co/datasets/Euniceyeee/kidney-ct-abnormality/resolve/main/kidney_ct/kidney_CT.zip'
}

_METADATA_URL = {
    "metadata": "https://huggingface.co/datasets/Euniceyeee/kidney-ct-abnormality/resolve/main/dataset_m.json"
}

LABELS = [
    'Normal',
    'Abnormal'
]


class KidneyCTAbnormality(datasets.GeneratorBasedBuilder):
    """Collection of brain xray images for fine-grain classification."""
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "images": datasets.Sequence(datasets.Image()),
                    # "img_f64_array": datasets.Array3D(dtype = 'float64'),
                    "img_path": datasets.Value(dtype = 'string'),
                    "label": datasets.ClassLabel(num_classes=2, names=LABELS),
                }
            ),
            supervised_keys=("image", "label"),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE
        )

    def _split_generators(self, dl_manager):
        metadata_url = dl_manager.download(_METADATA_URL)
        files_metadata = {}
        with open(metadata_url["metadata"], encoding="utf-8") as f:
            for lines in f.read().splitlines():
                file_json_metdata = json.loads(lines)
                files_metadata.setdefault(file_json_metdata["split"], []).append(file_json_metdata)
        
        # data_dir = dl_manager.download(_URLS)
        downloaded_files = dl_manager.download_and_extract(_URLS)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": downloaded_files['kidney_CT'],
                    "metadata": files_metadata["train"]
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": downloaded_files['kidney_CT'],
                    "metadata": files_metadata["test"]
                },
            ),
        ]

    def _generate_examples(self, filepath, metadata):
        """Generate images and labels for splits."""
        h = 0
        w = 0
        for i, meta in enumerate(metadata):
            img_path = os.path.join(
                filepath,
                'kidney_CT',
                meta['split'],
                meta["image"]
            )
            img = sitk.ReadImage(img_path)
            img_array = sitk.GetArrayFromImage(img)
            img_array_c = img_array.astype('uint8')
            # if img_array_c.shape[0]>h:
            #     h = img_array_c.shape[0]
            #     print(h)
            # if img_array_c.shape[1]>w:
            #     w = img_array_c.shape[1]
            #     print(w)
            # img_array_cr = img_array_c.transpose(2, 0, 1)[..., np.newaxis]
            yield i, {
                "images": img_array_c,
                # "img_f64_array": img_array,
                "img_path": img_path,
                "label": meta["abnormality"],
            }