-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_search.py
613 lines (470 loc) · 25 KB
/
graph_search.py
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
from heapq import heappush, heappop
import numpy as np
def run_dijkstras(prob_map, start_ind, graph_structure):
"""Run dijkstra's algorithm using graph formed by a specified probability map (weights) and graph structure (edges).
Start index indicates which node to begin from
_________
prob_map: probability map, numpy array with shape: (width, height).
_________
graph_structure: a 2D list where the first dimension is the index of the vertex and
the second dimension contains the indices for the neighbours which it connects to with a directed edge
_________
start_ind: start index expects a number indicating what node (index) to start the search from
_________
Returns:
List of shortest paths. One for each vertex indexed by vertex number which is either:
(1): 0 indicating unreachable vertex or
(2): a tuple of the form: (final shortest path length, previous vertex index)
__________
Indices are calculated by: index = column + (row * graph width)
__________
"""
max_ind = prob_map.shape[0] * prob_map.shape[1] - 1
# setup list containing final shortest paths
# each element corresponds to a vertex (vertex index = list index) and is a tuple expected to be of the form:
# (final shortest distance to vertex, previous vertex visited on final shortest path)
shortest_paths = [None] * (prob_map.shape[0] * prob_map.shape[1])
# setup queue to contain incomplete vertices
# each queue entry corresponds to a vertex and is a tuple expected to be of the form:
# (current shortest distance to vertex, neighbour priority (lower is higher),
# index of vertex, previous vertex visited on shortest path)
candidates_q = [(0, 0, 0, start_ind, 0)]
add_count = 1
while candidates_q: # while we still have incomplete vertices
path_len, _, _, v, a = heappop(candidates_q)
if shortest_paths[v] is None:
# we have not found the shortest path for v yet
shortest_paths[v] = (path_len, a) # found final shortest path for this vertex
if v == max_ind:
# we have found the shortest path to the bottom-rightmost corner -> we are DONE!
break
num_neighbours = len(graph_structure[v])
for i in range(num_neighbours):
n = graph_structure[v][i]
cur_v_col = int(v % prob_map.shape[0])
cur_v_row = int(v / prob_map.shape[0])
cur_v_prob = prob_map[cur_v_col][cur_v_row]
neigh_v_col = int(n % prob_map.shape[0])
neigh_v_row = int(n / prob_map.shape[0])
neigh_v_prob = prob_map[neigh_v_col][neigh_v_row]
if cur_v_col == neigh_v_col:
# edge_len = 0
edge_len = np.max(2 - (cur_v_prob + neigh_v_prob), 0)
else:
edge_len = np.max(2 - (cur_v_prob + neigh_v_prob), 0)
if shortest_paths[n] is None:
# we have not found the shortest path for n yet
if neigh_v_col == cur_v_col and neigh_v_row == cur_v_row + 1:
heappush(candidates_q, (path_len + edge_len, 0, add_count, n, v))
else:
heappush(candidates_q, (path_len + edge_len, i + 1, add_count, n, v))
add_count += 1
else:
pass
# we have already finished this neighbour vertex
# we won't find a shorter path so don't bother with it
else:
# we have already finished this vertex, we won't find a shorter path so don't bother with it
pass
# assign zero distance to unreachable vertices
return [0 if x is None else x for x in shortest_paths]
def create_graph_structure(shape, max_grad=1):
"""Create structure for a gridded graph of specified shape
_________
shape: Shape of graph which is expected as a tuple of the form (width, height).
Here width does not include the first and last additional appended columns, these are automatically
appended in here.
_________
Returns:
graph structure info as a 2D list where the first dimension is the index of the vertex
and the second dimension contains the indices of it's neighbours which it connects to with a directed edge
_________
Indices are calculated by: index = column + (row * graph width)
_________
"""
graph_width = shape[0] + 2 # append the two extra columns
graph_height = shape[1]
# setup graph neighbours as an empty 2D list
graph = [[] for _ in range(graph_width * graph_height)]
# for each vertex in the gridded graph
for i in range(graph_height):
for j in range(graph_width):
# calculate indices of various neighbours
node = j + i * graph_width
nodes_diagup = []
nodes_diagdown = []
for grad in range(1, max_grad + 1):
node_diagup = (j + 1) + (i - grad) * graph_width
nodes_diagup.append(node_diagup)
node_diagdown = (j + 1) + (i + grad) * graph_width
nodes_diagdown.append(node_diagdown)
node_right = (j + 1) + i * graph_width
node_down = (j) + (i + 1) * graph_width
# add neighbours as required for various cases of first/middle/last, row/col
# ensuring that non-existent neighbours are not added
# example non-existent neighbour: a vertex below the bottommost row
if i == graph_height - 1:
# last row
if j == graph_width - 1:
# last column
pass
else:
# first and middle columns
graph[node].append(node_right)
for grad_ind in range(0, max_grad):
if i - grad_ind - 1 >= 0:
# check that we do not overflow above
graph[node].append(nodes_diagup[grad_ind])
elif i == 0:
# first row
if j == graph_width - 1:
# last column
graph[node].append(node_down)
elif j == 0:
# first column
graph[node].append(node_right)
graph[node].append(node_down)
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
else:
# middle column
graph[node].append(node_right)
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
else:
# middle row
if j == graph_width - 1:
# last column
graph[node].append(node_down)
elif j == 0:
# first column
graph[node].append(node_right)
graph[node].append(node_down)
for grad_ind in range(0, max_grad):
if i - grad_ind - 1 >= 0:
# check that we do not overflow above
graph[node].append(nodes_diagup[grad_ind])
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
else:
# middle column
graph[node].append(node_right)
for grad_ind in range(0, max_grad):
if i - grad_ind - 1 >= 0:
# check that we do not overflow above
graph[node].append(nodes_diagup[grad_ind])
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
return graph
def create_graph_structure_vertical(shape):
max_grad = 1
graph_width = shape[0] + 2 # append the two extra columns
graph_height = shape[1]
# setup graph neighbours as an empty 2D list
graph = [[] for _ in range(graph_width * graph_height)]
# for each vertex in the gridded graph
for i in range(graph_height):
for j in range(graph_width):
# calculate indices of various neighbours
node = j + i * graph_width
nodes_diagup = []
nodes_diagdown = []
for grad in range(1, max_grad + 1):
node_diagup = (j + 1) + (i - grad) * graph_width
nodes_diagup.append(node_diagup)
node_diagdown = (j + 1) + (i + grad) * graph_width
nodes_diagdown.append(node_diagdown)
node_right = (j + 1) + i * graph_width
node_down = (j) + (i + 1) * graph_width
node_up = (j) + (i - 1) * graph_width
# add neighbours as required for various cases of first/middle/last, row/col
# ensuring that non-existent neighbours are not added
# example non-existent neighbour: a vertex below the bottommost row
if i == graph_height - 1:
# last row
if j == graph_width - 1:
# last column
pass
else:
# first and middle columns
graph[node].append(node_right)
graph[node].append(node_up)
for grad_ind in range(0, max_grad):
if i - grad_ind - 1 >= 0:
# check that we do not overflow above
graph[node].append(nodes_diagup[grad_ind])
elif i == 0:
# first row
if j == graph_width - 1:
# last column
graph[node].append(node_down)
elif j == 0:
# first column
graph[node].append(node_right)
graph[node].append(node_down)
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
else:
# middle column
graph[node].append(node_right)
graph[node].append(node_down)
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
else:
# middle row
if j == graph_width - 1:
# last column
graph[node].append(node_down)
elif j == 0:
# first column
graph[node].append(node_right)
graph[node].append(node_down)
for grad_ind in range(0, max_grad):
if i - grad_ind - 1 >= 0:
# check that we do not overflow above
graph[node].append(nodes_diagup[grad_ind])
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
else:
# middle column
graph[node].append(node_right)
graph[node].append(node_up)
graph[node].append(node_down)
for grad_ind in range(0, max_grad):
if i - grad_ind - 1 >= 0:
# check that we do not overflow above
graph[node].append(nodes_diagup[grad_ind])
for grad_ind in range(0, max_grad):
if i + grad_ind + 1 <= graph_height - 1:
# check that we do not overflow below
graph[node].append(nodes_diagdown[grad_ind])
return graph
def append_firstlast_cols(prob_map):
"""Append first and last columns of probability one (maximum probability) to a given map
_________
prob_map: probability map, numpy array with shape: (width, height)
Values in map should be normalised between 0 and 1.
_________
Returns the modified map with appended columns. shape: (width + 2, height)
_________
"""
map_height = prob_map.shape[1]
prob_map = np.concatenate((np.ones((1, map_height)), prob_map), axis=0) # append first col
prob_map = np.concatenate((prob_map, np.ones((1, map_height))), axis=0) # append last col
return prob_map
def delineate_boundary(prob_map, graph_structure):
"""Delineate boundary (obtain a single row prediction for each column) for given probability map using a
gridded graph constructed both from the probabilities of the map and the specified graph connectivity structure.
_________
prob_map: probability map, numpy array with shape (width, height).
Values in map should be normalised between 0 and 1.
_________
graph_structure: a 2D list where the first dimension is the index of the vertex and
the second dimension contains the indices for the neighbours connected with a directed edge
_________
Returns a numpy array containing the delineated boundary positions for the prob map (one row position
for each column as required), shape: (width,)
_________
Indices are calculated by:
index = column + row * graph width
To go back and extract row and column for position tuple of the form (col, row):
(ind % width, ind / width) = (ind % width, floor(ind / width))
_________
"""
prob_map = append_firstlast_cols(prob_map)
shortest_paths = run_dijkstras(prob_map, 0, graph_structure)
map_width = prob_map.shape[0]
map_height = prob_map.shape[1]
# extract shortest path starting at bottom right corner and working back
final_node_ind = (map_width * map_height) - 1
node_ind = final_node_ind # current node index
node_coord = (node_ind % map_width, int(node_ind / map_width)) # current node coordinate
prev_node_ind = shortest_paths[node_ind][1] # previous node index
node_order_coords = [] # list of node coordinates along shortest path in reverse order
while node_coord != (0, 0): # keep adding while we haven't reached the start vertex
node_order_coords.append(node_coord)
next_node_coord = (prev_node_ind % map_width, int(prev_node_ind / map_width))
node_coord = next_node_coord
prev_node_ind = shortest_paths[prev_node_ind][1]
delin = np.zeros((map_width - 2)) # numpy array of row values corresponding to the delineated boundary
# (one for each column in the original map: exclude the appended columns)
for coord in node_order_coords:
# do not add the coordinate if it is part of the first or last column
# these first and last columns do not form part of the delineation
if coord[0] != 0 and coord[0] != map_width - 1:
delin[coord[0] - 1] = coord[1]
return delin
def delineate_boundary_vertical(prob_map, graph_structure):
prob_map = append_firstlast_cols(prob_map)
shortest_paths = run_dijkstras(prob_map, 0, graph_structure)
map_width = prob_map.shape[0]
map_height = prob_map.shape[1]
# extract shortest path starting at bottom right corner and working back
final_node_ind = (map_width * map_height) - 1
node_ind = final_node_ind # current node index
node_coord = (node_ind % map_width, int(node_ind / map_width)) # current node coordinate
prev_node_ind = shortest_paths[node_ind][1] # previous node index
node_order_coords = [] # list of node coordinates along shortest path in reverse order
while node_coord != (0, 0): # keep adding while we haven't reached the start vertex
node_order_coords.append(node_coord)
next_node_coord = (prev_node_ind % map_width, int(prev_node_ind / map_width))
node_coord = next_node_coord
prev_node_ind = shortest_paths[prev_node_ind][1]
delin = np.zeros((map_width - 2)) # numpy array of row values corresponding to the delineated boundary
counts = np.zeros((map_width - 2))
# (one for each column in the original map: exclude the appended columns)
for coord in node_order_coords:
# do not add the coordinate if it is part of the first or last column
# these first and last columns do not form part of the delineation
if coord[0] != 0 and coord[0] != map_width - 1:
delin[coord[0] - 1] += coord[1]
counts[coord[0] - 1] += 1
for col in range(delin.shape[0]):
delin[col] = delin[col] / counts[col]
return delin
def calc_errors(prediction, truth):
"""Calculate delineation errors by comparing the predictions and truths.
Predictions or truths that are NaN or <= 0 have a NaN error.
Predictions and truths must be the same shape.
Errors are calculated by:
error = predicted value - true value
_________
prediction: numpy array of integer values corresponding to the row prediction for each column.
shape: (width,)
_________
truth: numpy array of integer values corresponding to the true row position for each column.
shape: (width,)
_________
Returns numpy array containing the errors. Shape: (width,). Where error cannot be calculated or is invalid,
it is replaced by np.nan
_________
"""
width = prediction.shape[0]
error = np.zeros((width,), dtype='float64')
for i in range(width):
if np.isnan(truth[i]):
error[i] = np.nan
elif truth[i] <= 0:
error[i] = np.nan
else:
error[i] = prediction[i].astype('float64') - truth[i]
return error
def segment_maps(prob_maps, truths, eval_params=None, graph_structure=None):
"""Delineate boundaries using specified neighbours structure for a number of probability maps
and subsequently calculate delineation errors.
_________
prob_maps: numpy array of probability maps with the shape: (number of maps/boundaries, width, height).
Probability map values assumed to be uint8 between 0 and 255. These will be normalised to between float64
0 and 1 here.
_________
truths: numpy array of values with the shape: (number of maps/boundaries, width) corresponding to the true row
locations for each column for each map.
_________
graph_structure: a 2D list where the first dimension is the index of each vertex and
the second dimension contains the indices for the neighbours connected with a directed edge
_________
Returns delineations and errors for each probability map in numpy arrays. Two structures:
(1) predictions: numpy array with shape: (number of maps, width) corresponding with a
predicted value for each column for each map
(2) errors: numpy array with shape: (number of maps, width) corresponding to the error between the predicted
and true value for each column for each map
_________
"""
if eval_params is not None:
graph_structure = eval_params.graph_structure
prob_maps.astype('float64')
prob_maps = prob_maps / 255
num_maps = prob_maps.shape[0]
width = prob_maps.shape[1]
predictions = np.zeros((num_maps, width), dtype='uint16')
errors = np.zeros((num_maps, width), dtype='float64')
if eval_params is not None and eval_params.trim_maps is True:
map_ind = eval_params.trim_ref_ind
if eval_params.vertical_graph_search is False:
ref_prediction = delineate_boundary(prob_maps[map_ind], graph_structure)
elif eval_params.vertical_graph_search is True:
ref_prediction = delineate_boundary_vertical(prob_maps[map_ind], graph_structure)
elif eval_params.vertical_graph_search == "ilm_vertical":
if map_ind == 0:
ref_prediction = delineate_boundary_vertical(prob_maps[map_ind], graph_structure[0])
else:
ref_prediction = delineate_boundary(prob_maps[map_ind], graph_structure[1])
predictions[map_ind, :] = ref_prediction
if eval_params.flatten_pred_edges is True:
predictions[map_ind, :eval_params.flat_marg] = predictions[map_ind, eval_params.flat_marg]
predictions[map_ind, -eval_params.flat_marg:] = predictions[map_ind, -eval_params.flat_marg]
if truths is not None:
error = calc_errors(ref_prediction, truths[map_ind, :])
errors[map_ind, :] = error
top_bounds = ref_prediction.astype('uint16') - eval_params.trim_window[0]
bottom_bounds = ref_prediction.astype('uint16') + eval_params.trim_window[1]
top_bounds[top_bounds > 1000] = 0
bottom_bounds[bottom_bounds > 1000] = 0
for map_ind in range(num_maps):
if map_ind == eval_params.trim_ref_ind:
continue
for col in range(prob_maps.shape[1]):
prob_maps[map_ind, col, 0:top_bounds[col]] = 0
prob_maps[map_ind, col, bottom_bounds[col]:] = 0
if eval_params.vertical_graph_search is False:
prediction = delineate_boundary(prob_maps[map_ind], graph_structure)
elif eval_params.vertical_graph_search is True:
prediction = delineate_boundary_vertical(prob_maps[map_ind], graph_structure)
elif eval_params.vertical_graph_search == "ilm_vertical":
if map_ind == 0:
prediction = delineate_boundary_vertical(prob_maps[map_ind], graph_structure[0])
else:
prediction = delineate_boundary(prob_maps[map_ind], graph_structure[1])
predictions[map_ind, :] = prediction
if eval_params.flatten_pred_edges is True:
predictions[map_ind, :eval_params.flat_marg] = predictions[map_ind, eval_params.flat_marg]
predictions[map_ind, -eval_params.flat_marg:] = predictions[map_ind, -eval_params.flat_marg]
if truths is not None:
error = calc_errors(prediction, truths[map_ind, :])
errors[map_ind, :] = error
else:
for map_ind in range(num_maps):
if eval_params.vertical_graph_search is False:
prediction = delineate_boundary(prob_maps[map_ind], graph_structure)
elif eval_params.vertical_graph_search is True:
prediction = delineate_boundary_vertical(prob_maps[map_ind], graph_structure)
elif eval_params.vertical_graph_search == "ilm_vertical":
if map_ind == 0:
prediction = delineate_boundary_vertical(prob_maps[map_ind], graph_structure[0])
else:
prediction = delineate_boundary(prob_maps[map_ind], graph_structure[1])
predictions[map_ind, :] = prediction
if eval_params.flatten_pred_edges is True:
predictions[map_ind, :eval_params.flat_marg] = predictions[map_ind, eval_params.flat_marg]
predictions[map_ind, -eval_params.flat_marg:] = predictions[map_ind, -eval_params.flat_marg]
if truths is not None:
error = calc_errors(prediction, truths[map_ind, :])
errors[map_ind, :] = error
return predictions, errors, prob_maps
def calculate_overall_errors(errors, col_error_range):
num_boundaries = errors.shape[0]
mean_abs_err = np.zeros((num_boundaries, ), dtype='float64')
mean_err = np.zeros((num_boundaries, ), dtype='float64')
abs_err_sd = np.zeros((num_boundaries, ), dtype='float64')
err_sd = np.zeros((num_boundaries, ), dtype='float64')
errors = errors[:, col_error_range[0]:col_error_range[-1] + 1]
for boundary_ind in range(num_boundaries):
mean_abs_err[boundary_ind] = np.nanmean(np.abs(errors[boundary_ind]))
mean_err[boundary_ind] = np.nanmean(errors[boundary_ind])
abs_err_sd[boundary_ind] = np.nanstd(np.abs(errors[boundary_ind]))
err_sd[boundary_ind] = np.nanstd(errors[boundary_ind])
return [mean_abs_err, mean_err, abs_err_sd, err_sd]