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"], }