Datasets:
Tasks:
Image Segmentation
Formats:
parquet
Sub-tasks:
instance-segmentation
Languages:
English
Size:
< 1K
License:
File size: 5,290 Bytes
72affa1 |
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
from collections.abc import Iterable
from pathlib import Path
from typing import Any
from xml.etree import ElementTree as ET
import datasets
import numpy as np
import pyvips
from datasets import Dataset
from datasets.splits import NamedSplit
from PIL import Image, ImageDraw
from tqdm import tqdm
# https://drive.google.com/file/d/1kdOl3s6uQBRv0nToSIf1dPuceZunzL4N/view
patient_data = {
"TCGA-55-1594": "Lung",
"TCGA-69-7760": "Lung",
"TCGA-69-A59K": "Lung",
"TCGA-73-4668": "Lung",
"TCGA-78-7220": "Lung",
"TCGA-86-7713": "Lung",
"TCGA-86-8672": "Lung",
"TCGA-L4-A4E5": "Lung",
"TCGA-MP-A4SY": "Lung",
"TCGA-MP-A4T7": "Lung",
"TCGA-5P-A9K0": "Kidney",
"TCGA-B9-A44B": "Kidney",
"TCGA-B9-A8YI": "Kidney",
"TCGA-DW-7841": "Kidney",
"TCGA-EV-5903": "Kidney",
"TCGA-F9-A97G": "Kidney",
"TCGA-G7-A8LD": "Kidney",
"TCGA-MH-A560": "Kidney",
"TCGA-P4-AAVK": "Kidney",
"TCGA-SX-A7SR": "Kidney",
"TCGA-UZ-A9PO": "Kidney",
"TCGA-UZ-A9PU": "Kidney",
"TCGA-A2-A0CV": "Breast",
"TCGA-A2-A0ES": "Breast",
"TCGA-B6-A0WZ": "Breast",
"TCGA-BH-A18T": "Breast",
"TCGA-D8-A1X5": "Breast",
"TCGA-E2-A154": "Breast",
"TCGA-E9-A22B": "Breast",
"TCGA-E9-A22G": "Breast",
"TCGA-EW-A6SD": "Breast",
"TCGA-S3-AA11": "Breast",
"TCGA-EJ-5495": "Prostate",
"TCGA-EJ-5505": "Prostate",
"TCGA-EJ-5517": "Prostate",
"TCGA-G9-6342": "Prostate",
"TCGA-G9-6499": "Prostate",
"TCGA-J4-A67Q": "Prostate",
"TCGA-J4-A67T": "Prostate",
"TCGA-KK-A59X": "Prostate",
"TCGA-KK-A6E0": "Prostate",
"TCGA-KK-A7AW": "Prostate",
"TCGA-V1-A8WL": "Prostate",
"TCGA-V1-A9O9": "Prostate",
"TCGA-X4-A8KQ": "Prostate",
"TCGA-YL-A9WY": "Prostate",
"TCGA-49-6743": "Lung",
"TCGA-50-6591": "Lung",
"TCGA-55-7570": "Lung",
"TCGA-55-7573": "Lung",
"TCGA-73-4662": "Lung",
"TCGA-78-7152": "Lung",
"TCGA-2Z-A9JG": "Kidney",
"TCGA-2Z-A9JN": "Kidney",
"TCGA-DW-7838": "Kidney",
"TCGA-DW-7963": "Kidney",
"TCGA-F9-A8NY": "Kidney",
"TCGA-IZ-A6M9": "Kidney",
"TCGA-MH-A55W": "Kidney",
"TCGA-A2-A04X": "Breast",
"TCGA-D8-A3Z6": "Breast",
"TCGA-E2-A108": "Breast",
"TCGA-EW-A6SB": "Breast",
"TCGA-G9-6356": "Prostate",
"TCGA-G9-6367": "Prostate",
"TCGA-VP-A87E": "Prostate",
"TCGA-VP-A87H": "Prostate",
"TCGA-X4-A8KS": "Prostate",
"TCGA-YL-A9WL": "Prostate",
}
features = datasets.Features(
{
"patient": datasets.Value("string"),
"image": datasets.Image(mode="RGB"),
"instances": datasets.Sequence(datasets.Image(mode="1")),
"categories": datasets.Sequence(
datasets.ClassLabel(
names=[
"Ambiguous",
"Epithelial",
"Lymphocyte",
"Macrophage",
"Neutrophil",
],
)
),
"tissue": datasets.ClassLabel(
names=[
"Breast",
"Kidney",
"Lung",
"Prostate",
]
),
}
)
def get_masks(
path: Path, mask_size: tuple[int, int]
) -> tuple[list[Image.Image], list[str]]:
masks = []
categories = []
root = ET.parse(path).getroot()
for annotation in root.findall("Annotation"):
for region in annotation.findall("Regions/Region"):
polygon = [
(float(vertex.attrib["X"]), float(vertex.attrib["Y"]))
for vertex in region.findall("Vertices/Vertex")
]
if len(polygon) < 2:
continue
mask = Image.new("1", size=mask_size)
canvas = ImageDraw.Draw(mask)
canvas.polygon(xy=polygon, outline=True, fill=True)
masks.append(mask)
categories.append(annotation.find("Attributes/Attribute").attrib["Name"])
return masks, categories
def process(src: str) -> Iterable[dict[str, Any]]:
files = list(Path(src).rglob("*.xml"))
for file in tqdm(files):
try:
image = np.asarray(Image.open(file.with_suffix(".tif")))
except FileNotFoundError:
image = pyvips.Image.new_from_file(file.with_suffix(".svs"))
image = image.numpy()
masks, categories = get_masks(file, mask_size=(image.shape[1], image.shape[0]))
patient_id = file.parent.stem[:12]
yield {
"patient": patient_id,
"image": Image.fromarray(image.astype(np.uint8)),
"instances": masks,
"categories": categories,
"tissue": patient_data[patient_id],
}
if __name__ == "__main__":
train = Dataset.from_generator(
process,
gen_kwargs={"src": "data/raw/MoNuSAC/MoNuSAC_images_and_annotations"},
features=features,
split=NamedSplit("train"),
keep_in_memory=True,
)
train.push_to_hub("RationAI/MoNuSAC")
test = Dataset.from_generator(
process,
gen_kwargs={"src": "data/raw/MoNuSAC/MoNuSAC Testing Data and Annotations"},
features=features,
split=NamedSplit("test"),
keep_in_memory=True,
)
test.push_to_hub("RationAI/MoNuSAC")
|