matejpekar commited on
Commit
72affa1
·
verified ·
1 Parent(s): 2d74f88

Create gen_script.py

Browse files
Files changed (1) hide show
  1. gen_script.py +183 -0
gen_script.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import pyvips
9
+ from datasets import Dataset
10
+ from datasets.splits import NamedSplit
11
+ from PIL import Image, ImageDraw
12
+ from tqdm import tqdm
13
+
14
+
15
+ # https://drive.google.com/file/d/1kdOl3s6uQBRv0nToSIf1dPuceZunzL4N/view
16
+ patient_data = {
17
+ "TCGA-55-1594": "Lung",
18
+ "TCGA-69-7760": "Lung",
19
+ "TCGA-69-A59K": "Lung",
20
+ "TCGA-73-4668": "Lung",
21
+ "TCGA-78-7220": "Lung",
22
+ "TCGA-86-7713": "Lung",
23
+ "TCGA-86-8672": "Lung",
24
+ "TCGA-L4-A4E5": "Lung",
25
+ "TCGA-MP-A4SY": "Lung",
26
+ "TCGA-MP-A4T7": "Lung",
27
+ "TCGA-5P-A9K0": "Kidney",
28
+ "TCGA-B9-A44B": "Kidney",
29
+ "TCGA-B9-A8YI": "Kidney",
30
+ "TCGA-DW-7841": "Kidney",
31
+ "TCGA-EV-5903": "Kidney",
32
+ "TCGA-F9-A97G": "Kidney",
33
+ "TCGA-G7-A8LD": "Kidney",
34
+ "TCGA-MH-A560": "Kidney",
35
+ "TCGA-P4-AAVK": "Kidney",
36
+ "TCGA-SX-A7SR": "Kidney",
37
+ "TCGA-UZ-A9PO": "Kidney",
38
+ "TCGA-UZ-A9PU": "Kidney",
39
+ "TCGA-A2-A0CV": "Breast",
40
+ "TCGA-A2-A0ES": "Breast",
41
+ "TCGA-B6-A0WZ": "Breast",
42
+ "TCGA-BH-A18T": "Breast",
43
+ "TCGA-D8-A1X5": "Breast",
44
+ "TCGA-E2-A154": "Breast",
45
+ "TCGA-E9-A22B": "Breast",
46
+ "TCGA-E9-A22G": "Breast",
47
+ "TCGA-EW-A6SD": "Breast",
48
+ "TCGA-S3-AA11": "Breast",
49
+ "TCGA-EJ-5495": "Prostate",
50
+ "TCGA-EJ-5505": "Prostate",
51
+ "TCGA-EJ-5517": "Prostate",
52
+ "TCGA-G9-6342": "Prostate",
53
+ "TCGA-G9-6499": "Prostate",
54
+ "TCGA-J4-A67Q": "Prostate",
55
+ "TCGA-J4-A67T": "Prostate",
56
+ "TCGA-KK-A59X": "Prostate",
57
+ "TCGA-KK-A6E0": "Prostate",
58
+ "TCGA-KK-A7AW": "Prostate",
59
+ "TCGA-V1-A8WL": "Prostate",
60
+ "TCGA-V1-A9O9": "Prostate",
61
+ "TCGA-X4-A8KQ": "Prostate",
62
+ "TCGA-YL-A9WY": "Prostate",
63
+ "TCGA-49-6743": "Lung",
64
+ "TCGA-50-6591": "Lung",
65
+ "TCGA-55-7570": "Lung",
66
+ "TCGA-55-7573": "Lung",
67
+ "TCGA-73-4662": "Lung",
68
+ "TCGA-78-7152": "Lung",
69
+ "TCGA-2Z-A9JG": "Kidney",
70
+ "TCGA-2Z-A9JN": "Kidney",
71
+ "TCGA-DW-7838": "Kidney",
72
+ "TCGA-DW-7963": "Kidney",
73
+ "TCGA-F9-A8NY": "Kidney",
74
+ "TCGA-IZ-A6M9": "Kidney",
75
+ "TCGA-MH-A55W": "Kidney",
76
+ "TCGA-A2-A04X": "Breast",
77
+ "TCGA-D8-A3Z6": "Breast",
78
+ "TCGA-E2-A108": "Breast",
79
+ "TCGA-EW-A6SB": "Breast",
80
+ "TCGA-G9-6356": "Prostate",
81
+ "TCGA-G9-6367": "Prostate",
82
+ "TCGA-VP-A87E": "Prostate",
83
+ "TCGA-VP-A87H": "Prostate",
84
+ "TCGA-X4-A8KS": "Prostate",
85
+ "TCGA-YL-A9WL": "Prostate",
86
+ }
87
+
88
+
89
+ features = datasets.Features(
90
+ {
91
+ "patient": datasets.Value("string"),
92
+ "image": datasets.Image(mode="RGB"),
93
+ "instances": datasets.Sequence(datasets.Image(mode="1")),
94
+ "categories": datasets.Sequence(
95
+ datasets.ClassLabel(
96
+ names=[
97
+ "Ambiguous",
98
+ "Epithelial",
99
+ "Lymphocyte",
100
+ "Macrophage",
101
+ "Neutrophil",
102
+ ],
103
+ )
104
+ ),
105
+ "tissue": datasets.ClassLabel(
106
+ names=[
107
+ "Breast",
108
+ "Kidney",
109
+ "Lung",
110
+ "Prostate",
111
+ ]
112
+ ),
113
+ }
114
+ )
115
+
116
+
117
+ def get_masks(
118
+ path: Path, mask_size: tuple[int, int]
119
+ ) -> tuple[list[Image.Image], list[str]]:
120
+ masks = []
121
+ categories = []
122
+
123
+ root = ET.parse(path).getroot()
124
+
125
+ for annotation in root.findall("Annotation"):
126
+ for region in annotation.findall("Regions/Region"):
127
+ polygon = [
128
+ (float(vertex.attrib["X"]), float(vertex.attrib["Y"]))
129
+ for vertex in region.findall("Vertices/Vertex")
130
+ ]
131
+
132
+ if len(polygon) < 2:
133
+ continue
134
+
135
+ mask = Image.new("1", size=mask_size)
136
+ canvas = ImageDraw.Draw(mask)
137
+ canvas.polygon(xy=polygon, outline=True, fill=True)
138
+ masks.append(mask)
139
+ categories.append(annotation.find("Attributes/Attribute").attrib["Name"])
140
+
141
+ return masks, categories
142
+
143
+
144
+ def process(src: str) -> Iterable[dict[str, Any]]:
145
+ files = list(Path(src).rglob("*.xml"))
146
+
147
+ for file in tqdm(files):
148
+ try:
149
+ image = np.asarray(Image.open(file.with_suffix(".tif")))
150
+ except FileNotFoundError:
151
+ image = pyvips.Image.new_from_file(file.with_suffix(".svs"))
152
+ image = image.numpy()
153
+
154
+ masks, categories = get_masks(file, mask_size=(image.shape[1], image.shape[0]))
155
+ patient_id = file.parent.stem[:12]
156
+
157
+ yield {
158
+ "patient": patient_id,
159
+ "image": Image.fromarray(image.astype(np.uint8)),
160
+ "instances": masks,
161
+ "categories": categories,
162
+ "tissue": patient_data[patient_id],
163
+ }
164
+
165
+
166
+ if __name__ == "__main__":
167
+ train = Dataset.from_generator(
168
+ process,
169
+ gen_kwargs={"src": "data/raw/MoNuSAC/MoNuSAC_images_and_annotations"},
170
+ features=features,
171
+ split=NamedSplit("train"),
172
+ keep_in_memory=True,
173
+ )
174
+ train.push_to_hub("RationAI/MoNuSAC")
175
+
176
+ test = Dataset.from_generator(
177
+ process,
178
+ gen_kwargs={"src": "data/raw/MoNuSAC/MoNuSAC Testing Data and Annotations"},
179
+ features=features,
180
+ split=NamedSplit("test"),
181
+ keep_in_memory=True,
182
+ )
183
+ test.push_to_hub("RationAI/MoNuSAC")