[7823dd]: / pathaia / util / convert.py

Download this file

402 lines (335 with data), 11.2 kB

  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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
A Module to translate pathaia csv to json annotation for micromap.
It uses networkx to handle graph structures.
"""
import json
import pandas as pd
import networkx as nx
import openslide
import os
from .types import PathLike
from typing import Dict, Iterable, Tuple
import warnings
class Error(Exception):
"""
Base of custom errors.
**********************
"""
pass
class BottomLeftNotFound(Error):
"""
Raise when trying to access unknown level.
*********************************************
"""
pass
class NextPointNotFound(Error):
"""
Raise when trying to access unknown level.
*********************************************
"""
pass
class NoPathFound(Error):
"""
Raise when trying to access unknown level.
*********************************************
"""
pass
class OutOfBound(Error):
"""
Raise when trying to access unknown level.
*********************************************
"""
pass
colorCycle = [
"#f44336", "#8bc34a", "#ffeb3b", "#673ab7", "#e91e63", "#cddc39", "#9c27b0",
"#ffc107", "#3f51b5", "#ff9800", "#2196f3", "#ff5722", "#03a9f4", "#795548",
"#00bcd4", "#607d8b", "#009688", "#4caf50"
]
def handle_predicted_patches(
patch_file: PathLike,
level: int,
column: str
):
"""
Read a patch file.
Read lines of the patch csv looking for 'column' label.
Args:
patch_file (str): absolute path to a csv patch file.
level (int): pyramid level to query patches in the csv.
column (str): header of the column to use to label individual patches.
Yields:
tuple: position and label of patches (x, y, label).
"""
df = pd.read_csv(patch_file)
level_df = df[df["level"] == level]
for _, row in level_df.iterrows():
yield row["x"], row["y"], row["dx"], row[column], column
def get_category(
val: float,
thresholds: Dict[int, Tuple[float, float]]
):
"""
Return the break-apart categorical label from estimation.
*********************************************************
"""
for label, bounds in thresholds.items():
low, high = bounds
if val >= low and val < high:
return label
raise OutOfBound(
"Value: {} is out of bounds for thresholds: {}!".format(val, thresholds)
)
def gen_categorical_from_floatpreds(
patch_file: PathLike,
level: int,
column: str,
thresholds: Dict[int, Tuple[float, float]]
):
"""
Yield categorical patches from float predictions.
*************************************************
"""
for patch in handle_predicted_patches(
patch_file, level, column
):
x, y, d, val, author = patch
try:
yield x, y, d, get_category(val, thresholds), author
except OutOfBound:
pass
def get_categorical_layer_edges(
categorical_patch_generator: Iterable,
color_dict: Dict[int, str],
classname_dict: Dict[int, str]
):
"""
Create graph features from patches for every layer of annotation.
*******************************************************************
"""
layer_nodes = dict()
layer_edges = dict()
layer_meta = dict()
interval = None
for patch in categorical_patch_generator:
x, y, d, cl, author = patch
if interval is None:
interval = d
cl_name = classname_dict[cl]
if cl_name not in layer_nodes:
layer_nodes[cl_name] = set()
layer_meta[cl_name] = {
"label": "{}_{}".format(author, cl_name),
"author": "{}".format(author),
"text": "",
"color": color_dict[cl],
"date": ""
}
# (x, y) is just the top left corner, to plot the polygon,
# we will need the four corners
layer_nodes[cl_name].add((x, y))
layer_nodes[cl_name].add((x + interval, y))
layer_nodes[cl_name].add((x, y + interval))
layer_nodes[cl_name].add((x + interval, y + interval))
for layer, nodes in layer_nodes.items():
layer_edges[layer] = set()
for node in nodes:
x, y = node
for neighbor in [
(x + interval, y),
(x - interval, y),
(x, y + interval),
(x, y - interval),
(x - interval, y - interval),
(x - interval, y + interval),
(x + interval, y + interval),
(x + interval, y - interval)
]:
if neighbor in nodes:
layer_edges[layer].add((node, neighbor))
return layer_edges, layer_meta, interval
def get_categorical_segments_from_edges(layer_edges: Dict):
"""
Create segments from layer edges.
*********************************
"""
layer_segments = dict()
for layer, edges in layer_edges.items():
layer_segments[layer] = []
# create a graph
layer_graph = nx.Graph()
layer_graph.add_edges_from(edges)
for c in nx.connected_components(layer_graph):
layer_segments[layer].append(layer_graph.subgraph(c).copy())
return layer_segments
def get_contour_points(segment, adj=8):
"""
Find contour points of a segment.
*********************************
"""
contour = []
for pt in segment.nodes:
if segment.degree[pt] < adj:
contour.append(pt)
return contour
def convert_coord(coord, slide_dims):
"""
Compute relative coords from abs.
*********************************
"""
sx, sy = slide_dims
x, y = coord
return float(x) / sx, float(y) / sy
def find_bottom_left(pts):
"""
Find bottom left point.
***********************
"""
ymax = max([pt[1] for pt in pts])
xmin = 100000000000
bl = None
for pt in pts:
x, y = pt
if y == ymax:
if x <= xmin:
bl = x, y
if bl is not None:
return bl
raise BottomLeftNotFound("Did not find bottom left point of the cloud !!!")
def turn_left(orientation):
"""
Compute a new orientation after turning on the left.
**************************************************
"""
new_orientation = dict()
new_orientation["front"] = orientation["left"]
new_orientation["left"] = orientation["back"]
new_orientation["back"] = orientation["right"]
new_orientation["right"] = orientation["front"]
return new_orientation
def turn_right(orientation):
"""
Compute a new orientation after turning on the right.
*****************************************************
"""
new_orientation = dict()
new_orientation["front"] = orientation["right"]
new_orientation["left"] = orientation["front"]
new_orientation["back"] = orientation["left"]
new_orientation["right"] = orientation["back"]
return new_orientation
def turn_back(orientation):
"""
Compute a new orientation after turning back.
*********************************************
"""
# basically, it's just 'turn_right' twice...
new_orientation = turn_right(orientation)
new_new_orientation = turn_right(new_orientation)
return new_new_orientation
def go_to_next_point(pt, orientation, perimeter):
"""
Compute next point in the path.
*******************************
"""
x, y = pt
front = x + orientation["front"][0], y + orientation["front"][1]
left = x + orientation["left"][0], y + orientation["left"][1]
right = x + orientation["right"][0], y + orientation["right"][1]
back = x + orientation["back"][0], y + orientation["back"][1]
if left in perimeter:
new_orientation = turn_left(orientation)
return left, new_orientation
if front in perimeter:
return front, orientation
if right in perimeter:
new_orientation = turn_right(orientation)
return right, new_orientation
if back in perimeter:
new_orientation = turn_back(orientation)
return back, new_orientation
raise NextPointNotFound(
"Point {} \nhas no next point in neighborhood {} \nthat is in perimeter {}".format(
pt, {"left": left, "front": front, "right": right, "back": back}, perimeter
)
)
def compute_path(pts, d):
"""
Compute the path around a segment.
**********************************
"""
path = []
# first set remaining points to the whole cloud
perimeter = set(pts)
# find the bottom left point
start_point = find_bottom_left(perimeter)
path.append(start_point)
# set the initial orientation
start_orientation = {
"front": (0, -d),
"left": (-d, 0),
"back": (0, d),
"right": (d, 0)
}
current_point, orientation = go_to_next_point(
start_point, start_orientation, perimeter
)
while current_point != start_point:
path.append(current_point)
# remaining.remove(current_point)
next_point, next_orientation = go_to_next_point(
current_point, orientation, perimeter
)
current_point = next_point
orientation = next_orientation
if len(path) > 0:
return path
else:
raise NoPathFound(
"No path found for {}, with interval {}!!!".format(pts, d)
)
def layer_segment_to_json_struct(
interval,
layer_segments,
layer_meta,
slide
):
"""
Create the json annotation file from the segments.
**************************************************
"""
slide_id = os.path.basename(slide._filename)
# annotations = {"slide_id": slide_id, "layers": dict()}
annotations = {"slide_id": slide_id, "layers": []}
for layer, segments in layer_segments.items():
meta = layer_meta[layer]
layer_annotation = {
"id": meta["label"],
"color": meta["color"],
"shapes": []
}
# annotations["layers"][layer] = dict()
for idx, segment in enumerate(segments):
# create one annotation by segment
try:
contour = get_contour_points(segment, adj=8)
polygon = compute_path(contour, interval)
polygon = [convert_coord(
pt, slide.dimensions
) for pt in polygon]
shape = {
"points": [
{"x": x * 100, "y": y * 100,
"status": "written"} for x, y in polygon
],
"id": str(idx),
"author": meta["author"],
"text": meta["text"],
"color": meta["color"],
"label": meta["label"],
"date": meta["date"]
}
layer_annotation["shapes"].append(shape)
except (NoPathFound, NextPointNotFound, BottomLeftNotFound) as e:
warnings.warn(str(e))
annotations["layers"].append(layer_annotation)
return annotations