Datasets:
Tasks:
Image Segmentation
Formats:
parquet
Sub-tasks:
instance-segmentation
Languages:
English
Size:
< 1K
License:
Create gen_script.py
Browse files- gen_script.py +138 -0
gen_script.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Iterable
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import Any
|
| 4 |
+
from xml.etree import ElementTree as ET
|
| 5 |
+
|
| 6 |
+
import datasets
|
| 7 |
+
import numpy as np
|
| 8 |
+
from datasets import Dataset
|
| 9 |
+
from datasets.splits import NamedSplit
|
| 10 |
+
from PIL import Image, ImageDraw
|
| 11 |
+
from tqdm import tqdm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# https://drive.google.com/file/d/1xYyQ31CHFRnvTCTuuHdconlJCMk2SK7Z/view?usp=sharing
|
| 15 |
+
patient_data = {
|
| 16 |
+
"TCGA-A7-A13E-01Z-00-DX1": "Breast",
|
| 17 |
+
"TCGA-A7-A13F-01Z-00-DX1": "Breast",
|
| 18 |
+
"TCGA-AR-A1AK-01Z-00-DX1": "Breast",
|
| 19 |
+
"TCGA-AR-A1AS-01Z-00-DX1": "Breast",
|
| 20 |
+
"TCGA-E2-A1B5-01Z-00-DX1": "Breast",
|
| 21 |
+
"TCGA-E2-A14V-01Z-00-DX1": "Breast",
|
| 22 |
+
"TCGA-B0-5711-01Z-00-DX1": "Kidney",
|
| 23 |
+
"TCGA-HE-7128-01Z-00-DX1": "Kidney",
|
| 24 |
+
"TCGA-HE-7129-01Z-00-DX1": "Kidney",
|
| 25 |
+
"TCGA-HE-7130-01Z-00-DX1": "Kidney",
|
| 26 |
+
"TCGA-B0-5710-01Z-00-DX1": "Kidney",
|
| 27 |
+
"TCGA-B0-5698-01Z-00-DX1": "Kidney",
|
| 28 |
+
"TCGA-18-5592-01Z-00-DX1": "Liver",
|
| 29 |
+
"TCGA-38-6178-01Z-00-DX1": "Liver",
|
| 30 |
+
"TCGA-49-4488-01Z-00-DX1": "Liver",
|
| 31 |
+
"TCGA-50-5931-01Z-00-DX1": "Liver",
|
| 32 |
+
"TCGA-21-5784-01Z-00-DX1": "Liver",
|
| 33 |
+
"TCGA-21-5786-01Z-00-DX1": "Liver",
|
| 34 |
+
"TCGA-G9-6336-01Z-00-DX1": "Prostate",
|
| 35 |
+
"TCGA-G9-6348-01Z-00-DX1": "Prostate",
|
| 36 |
+
"TCGA-G9-6356-01Z-00-DX1": "Prostate",
|
| 37 |
+
"TCGA-G9-6363-01Z-00-DX1": "Prostate",
|
| 38 |
+
"TCGA-CH-5767-01Z-00-DX1": "Prostate",
|
| 39 |
+
"TCGA-G9-6362-01Z-00-DX1": "Prostate",
|
| 40 |
+
"TCGA-DK-A2I6-01A-01-TS1": "Bladder",
|
| 41 |
+
"TCGA-G2-A2EK-01A-02-TSB": "Bladder",
|
| 42 |
+
"TCGA-AY-A8YK-01A-01-TS1": "Colon",
|
| 43 |
+
"TCGA-NH-A8F7-01A-01-TS1": "Colon",
|
| 44 |
+
"TCGA-KB-A93J-01A-01-TS1": "Stomach",
|
| 45 |
+
"TCGA-RD-A8N9-01A-01-TS1": "Stomach",
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_masks(path: Path, mask_size: tuple[int, int]) -> list[Image.Image]:
|
| 50 |
+
masks = []
|
| 51 |
+
|
| 52 |
+
for region in ET.parse(path).getroot().findall("Annotation/Regions/Region"):
|
| 53 |
+
polygon = [
|
| 54 |
+
(float(vertex.attrib["X"]), float(vertex.attrib["Y"]))
|
| 55 |
+
for vertex in region.findall("Vertices/Vertex")
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
if len(polygon) < 2:
|
| 59 |
+
continue
|
| 60 |
+
|
| 61 |
+
mask = Image.new("1", size=mask_size)
|
| 62 |
+
canvas = ImageDraw.Draw(mask)
|
| 63 |
+
canvas.polygon(xy=polygon, outline=True, fill=True)
|
| 64 |
+
masks.append(mask)
|
| 65 |
+
|
| 66 |
+
return masks
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def process_train(src: str) -> Iterable[dict[str, Any]]:
|
| 70 |
+
files = list(Path(src).rglob("*.xml"))
|
| 71 |
+
|
| 72 |
+
for file in tqdm(files):
|
| 73 |
+
masks = get_masks(file, mask_size=(1000, 1000))
|
| 74 |
+
tissue_path = Path(str(file).replace("Annotations", "Tissue Images"))
|
| 75 |
+
image = np.asarray(Image.open(tissue_path.with_suffix(".tif")))
|
| 76 |
+
|
| 77 |
+
yield {
|
| 78 |
+
"patient": file.stem,
|
| 79 |
+
"image": Image.fromarray(image.astype(np.uint8)),
|
| 80 |
+
"instances": masks,
|
| 81 |
+
"tissue": patient_data.get(file.stem, "Unknown"),
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def process_test(src: str) -> Iterable[dict[str, Any]]:
|
| 86 |
+
files = list(Path(src).rglob("*.xml"))
|
| 87 |
+
|
| 88 |
+
for file in tqdm(files):
|
| 89 |
+
masks = get_masks(file, mask_size=(1000, 1000))
|
| 90 |
+
image = np.asarray(Image.open(file.with_suffix(".tif")))
|
| 91 |
+
|
| 92 |
+
yield {
|
| 93 |
+
"patient": file.stem,
|
| 94 |
+
"image": Image.fromarray(image.astype(np.uint8)),
|
| 95 |
+
"instances": masks,
|
| 96 |
+
"tissue": patient_data.get(file.stem, "Unknown"),
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
features = datasets.Features(
|
| 101 |
+
{
|
| 102 |
+
"patient": datasets.Value("string"),
|
| 103 |
+
"image": datasets.Image(mode="RGB"),
|
| 104 |
+
"instances": datasets.Sequence(datasets.Image(mode="1")),
|
| 105 |
+
"tissue": datasets.ClassLabel(
|
| 106 |
+
names=[
|
| 107 |
+
"Unknown",
|
| 108 |
+
"Breast",
|
| 109 |
+
"Kidney",
|
| 110 |
+
"Liver",
|
| 111 |
+
"Prostate",
|
| 112 |
+
"Bladder",
|
| 113 |
+
"Colon",
|
| 114 |
+
"Stomach",
|
| 115 |
+
]
|
| 116 |
+
),
|
| 117 |
+
}
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
train = Dataset.from_generator(
|
| 123 |
+
process_train,
|
| 124 |
+
gen_kwargs={"src": "data/raw/MoNuSeg/MoNuSeg 2018 Training Data/Annotations"},
|
| 125 |
+
features=features,
|
| 126 |
+
split=NamedSplit("train"),
|
| 127 |
+
keep_in_memory=True,
|
| 128 |
+
)
|
| 129 |
+
train.push_to_hub("RationAI/MoNuSeg")
|
| 130 |
+
|
| 131 |
+
test = Dataset.from_generator(
|
| 132 |
+
process_test,
|
| 133 |
+
gen_kwargs={"src": "data/raw/MoNuSeg/MoNuSegTestData"},
|
| 134 |
+
features=features,
|
| 135 |
+
split=NamedSplit("test"),
|
| 136 |
+
keep_in_memory=True,
|
| 137 |
+
)
|
| 138 |
+
test.push_to_hub("RationAI/MoNuSeg")
|