|
|
import datasets |
|
|
from typing import List |
|
|
|
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
from pathlib import Path |
|
|
import os |
|
|
|
|
|
_DESCRIPTION = "Prostate dataset." |
|
|
|
|
|
class Prostate158Dataset(datasets.GeneratorBasedBuilder): |
|
|
VERSION = datasets.Version("1.1.0") |
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
|
datasets.BuilderConfig( |
|
|
name="2d", |
|
|
version=VERSION, |
|
|
description="Return all the dataset in a 2d image format", |
|
|
), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datasets.BuilderConfig( |
|
|
name="3d_path", |
|
|
version=VERSION, |
|
|
description="Return all the dataset in a 3d path format", |
|
|
), |
|
|
|
|
|
] |
|
|
|
|
|
DEFAULT_CONFIG_NAME = "3d_path" |
|
|
|
|
|
def _info(self): |
|
|
if self.config.name == "2d": |
|
|
features = datasets.Features( |
|
|
{ |
|
|
"t2": datasets.Image(), |
|
|
"adc": datasets.Image(), |
|
|
"dwi": datasets.Image(), |
|
|
"t2_anatomy_reader1": datasets.Image(), |
|
|
"adc_tumor_reader1": datasets.Image(), |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif self.config.name == "3d_path": |
|
|
features = datasets.Features( |
|
|
{ |
|
|
"t2_path": datasets.Value(dtype="string"), |
|
|
"adc_path": datasets.Value(dtype="string"), |
|
|
"dwi_path": datasets.Value(dtype="string"), |
|
|
"t2_anatomy_reader1_path": datasets.Value(dtype="string"), |
|
|
"adc_tumor_reader1_path": datasets.Value(dtype="string"), |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return datasets.DatasetInfo( |
|
|
description=_DESCRIPTION, |
|
|
features=features, |
|
|
) |
|
|
|
|
|
def _split_generators( |
|
|
self, |
|
|
dl_manager: datasets.DownloadManager |
|
|
) -> List[datasets.SplitGenerator]: |
|
|
downloaded_files = dl_manager.download_and_extract("data.zip") |
|
|
|
|
|
return [ |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.TRAIN, |
|
|
gen_kwargs={ |
|
|
"split": "train", |
|
|
"downloaded_files": Path(downloaded_files), |
|
|
}, |
|
|
), |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.VALIDATION, |
|
|
gen_kwargs={ |
|
|
"split": "valid", |
|
|
"downloaded_files": Path(downloaded_files), |
|
|
}, |
|
|
), |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.TEST, |
|
|
gen_kwargs={ |
|
|
"split": "test", |
|
|
"downloaded_files": Path(downloaded_files), |
|
|
}, |
|
|
), |
|
|
] |
|
|
|
|
|
def _generate_examples(self, split, downloaded_files): |
|
|
images_list = ["t2", "adc", "dwi", "t2_anatomy_reader1", "adc_tumor_reader1"] |
|
|
df = pd.read_csv(downloaded_files / f"{split}.csv") |
|
|
if self.config.name == "2d": |
|
|
import nibabel as nib |
|
|
from PIL import Image |
|
|
|
|
|
yield_index = -1 |
|
|
for row in df.to_dict(orient="records"): |
|
|
images_data = {image_name: nib.load(downloaded_files / row[image_name]).get_fdata() for image_name in images_list} |
|
|
for i in range(images_data["t2"].shape[2]): |
|
|
yield_index += 1 |
|
|
yield yield_index, {image_name: Image.fromarray(image[:, :, i]) for image_name, image in images_data.items()} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif self.config.name == "3d_path": |
|
|
for idx, row in enumerate(df.to_dict(orient="records")): |
|
|
yield idx, {image_name+"_path": downloaded_files / row[image_name] for image_name in images_list} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|