-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeleton_metrics.py
More file actions
979 lines (821 loc) · 30 KB
/
skeleton_metrics.py
File metadata and controls
979 lines (821 loc) · 30 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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
"""
Created on Mon Oct 20 12:00:00 2025
@author: Anna Grim
@email: anna.grim@alleninstitute.org
Implementation of class that computes skeleton-based metrics by comparing a
predicted neuron segmentation to a set of ground truth graphs.
"""
from abc import ABC, abstractmethod
from copy import deepcopy
from collections import deque
from scipy.spatial import KDTree
from tqdm import tqdm
import networkx as nx
import numpy as np
import pandas as pd
from segmentation_skeleton_metrics.utils import util
class SkeletonMetric(ABC):
"""
Abstract base class for skeleton-based evaluation metrics.
"""
def __init__(self, verbose=True):
"""
Instantiates a SkeletonMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Instance attributes
self.verbose = verbose
@abstractmethod
def __call__(self, gt_graphs):
"""
Abstract method to be implemented by the subclasses.
"""
pass
def get_pbar(self, total):
"""
Gets a progress bar to be displayed.
Parameters
----------
total : int
Size of progress bar.
Returns
-------
tqdm.tqdm or None
Progress bar to be displayed if verose; otherwise, None.
"""
return tqdm(total=total, desc=self.name) if self.verbose else None
def reformat(self, results):
"""
Converts a dictionary of results into a pandas DataFrame.
Parameters
----------
results : Dict[str, float]
Dictionary where keys will become the DataFrame index and values
are used as the single column data.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
results = pd.DataFrame.from_dict(
results, orient="index", columns=[self.name]
)
return results
# --- Subclasses ---
class SplitEdgePercentMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes the percentage of split edges.
"""
def __init__(self, verbose=True):
"""
Instantiates a SplitEdgePercentMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "% Split Edges"
def __call__(self, gt_graphs):
"""
Computes the percentage of split edges in the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
results = dict()
pbar = self.get_pbar(len(gt_graphs))
for name, graph in gt_graphs.items():
# Compute result
num_split_edges = self.count_split_edges(graph)
percent = 100 * num_split_edges / graph.number_of_edges()
results[name] = percent
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(results)
@staticmethod
def count_split_edges(graph):
"""
Counts the number of split edges in the given graph.
Parameters
----------
graph : LabeledGraph
Graph to be evaluated.
Returns
-------
num_split_edges : int
Number fo split edges in the given graph.
"""
num_split_edges = 0
for i, j in nx.dfs_edges(graph):
is_different = graph.node_labels[i] != graph.node_labels[j]
is_nonzero = graph.node_labels[i] and graph.node_labels[j]
if is_different and is_nonzero:
num_split_edges += 1
return num_split_edges
class OmitEdgePercentMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes the percentage of omit edges.
"""
def __init__(self, verbose=True):
"""
Instantiates an OmitEdgePercentMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "% Omit Edges"
def __call__(self, gt_graphs):
"""
Computes the percentage of omit edges in the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
results = dict()
pbar = self.get_pbar(len(gt_graphs))
for name, graph in gt_graphs.items():
# Compute result
num_omit_edges = self.count_omit_edges(graph)
omit_edge_percent = 100 * num_omit_edges / graph.number_of_edges()
results[name] = omit_edge_percent
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(results)
@staticmethod
def count_omit_edges(graph):
"""
Counts the number of omit edges in the given graph.
Parameters
----------
graph : LabeledGraph
Graph to be evaluated.
Returns
-------
num_omit_edges : int
Number fo omit edges in the given graph.
"""
num_omit_edges = 0
for i, j in nx.dfs_edges(graph):
if graph.node_labels[i] == 0 and graph.node_labels[j] == 0:
num_omit_edges += 1
return num_omit_edges
class MergedEdgePercentMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes the percentage of edges that
are associated with a merge mistake.
"""
def __init__(self, verbose=True):
"""
Instantiates a MergedEdgePercentMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "% Merged Edges"
def __call__(self, gt_graphs):
"""
Computes the percentage of merged edges in the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graph to be evaluated.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
# Find graphs with common labels
self.detect_label_intersections(gt_graphs)
# Compile results
results = dict()
for name, graph in gt_graphs.items():
# Count number of edges associated with a merge
num_merged_edges = 0
for label in graph.labels_with_merge:
num_merged_edges += len(graph.get_nodes_with_label(label)) - 1
# Compute result
percent = 100 * num_merged_edges / graph.number_of_edges()
results[name] = percent
return self.reformat(results)
def detect_label_intersections(self, gt_graphs):
"""
Detects pairs of distinct graphs that contain nodes that share the
same label.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be searched for intersecting labels.
"""
visited = set()
pbar = self.get_pbar(len(gt_graphs))
for name1, graph1 in gt_graphs.items():
# Search other graphs for label intersections
for name2, graph2 in gt_graphs.items():
names = frozenset((name1, name2))
if name1 != name2 and names not in visited:
visited.add(names)
labels1 = set(graph1.get_node_labels())
labels2 = set(graph2.get_node_labels())
for label in labels1.intersection(labels2):
# Check if intersection is meaningful
num_nodes1 = len(graph1.get_nodes_with_label(label))
num_nodes2 = len(graph2.get_nodes_with_label(label))
if num_nodes1 > 50 and num_nodes2 > 50:
graph1.labels_with_merge.add(label)
graph2.labels_with_merge.add(label)
# Update progress bar
if self.verbose:
pbar.update(1)
class SplitCountMetric(SkeletonMetric):
"""
A skeleton metric subclass that counts the number of splits.
"""
def __init__(self, verbose=True):
"""
Instantiates a SplitCountMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "# Splits"
def __call__(self, gt_graphs):
"""
Counts the number of split mistakes in each of the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
Results
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
results = dict()
pbar = self.get_pbar(len(gt_graphs))
for name, graph in gt_graphs.items():
# Compute result
num_splits = max(len(graph.get_node_labels()) - 1, 0)
results[name] = int(num_splits)
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(results)
class MergeCountMetric(SkeletonMetric):
"""
A skeleton metric subclass that counts the number merges.
"""
merge_dist_threshold = 50
def __init__(self, verbose=True):
"""
Instantiates a MergeCountMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.fragments_with_merge = set()
self.merge_sites = list()
self.name = "# Merges"
# --- Core Routines ---
def __call__(self, gt_graphs, fragment_graphs):
"""
Counts the number of split merges in each of the given ground truth
graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
fragment_graphs : Dict[str, FragmentGraph]
Graphs corresponding to the predicted segmentation.
Results
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
# Main
pbar = self.get_pbar(len(gt_graphs))
for gt_graph in gt_graphs.values():
# Build ground truth kd-tree
gt_graph.init_kdtree()
# Search intersecting fragments
labels = gt_graph.get_node_labels()
for fragment_graph in fragment_graphs.values():
if fragment_graph.label in labels:
self.search_for_merges(gt_graph, fragment_graph)
# Update progress bar
if self.verbose:
pbar.update(1)
# Postprocess merge sites
self.remove_repeat_merge_sites()
# Compile results
results = dict()
for name in gt_graphs:
if len(self.merge_sites) > 0:
num_merges = (self.merge_sites["GroundTruth_ID"] == name).sum()
else:
num_merges = 0
results[name] = num_merges
return self.reformat(results)
def search_for_merges(self, gt_graph, fragment_graph):
"""
Searches for potential merge errors in a fragment graph by comparing
it to a ground truth graph.
Parameters
----------
gt_graph : LabeledGraph
Graph to be evaluated.
fragment_graph : FragmentGraph
Graph corresponding to a segment in the predicted segmentation.
"""
visited = set()
for leaf in util.get_leafs(fragment_graph):
# Check whether to visit
if leaf in visited or visited.add(leaf):
continue
# Find closet node in ground truth
xyz = fragment_graph.get_xyz(leaf)
dist, _ = gt_graph.kdtree.query(xyz)
# Check if distance to ground truth flags a merge mistake
if dist > MergeCountMetric.merge_dist_threshold:
self.find_merge_site(gt_graph, fragment_graph, leaf, visited)
def find_merge_site(self, gt_graph, fragment_graph, source, visited):
"""
Traverses fragment graph from a source node to locate and verify
potential merge sites relative to the ground truth graph.
Parameters
----------
gt_graph : LabeledGraph
Graphs to be evaluated.
fragment_graphs : FragmentGraph
Graph corresponding to a segment in the predicted segmentation.
source : int
Starting node ID in the fragment graph from which to begin
traversal.
visited : Set[int]
Node IDs from "fragment_graphs" that have already been visited,
used to avoid redundant exploration.
"""
queue = deque([source])
visited.add(source)
while len(queue) > 0:
# Visit node
i = queue.pop()
xyz_i = fragment_graph.get_xyz(i)
dist_i, gt_node = gt_graph.kdtree.query(xyz_i)
if dist_i < 6:
self.verify_site(gt_graph, fragment_graph, gt_node, i)
break
# Update queue
for j in fragment_graph.neighbors(i):
if j not in visited:
queue.append(j)
visited.add(j)
def verify_site(self, gt_graph, fragment_graph, gt_node, fragment_node):
"""
Verifies whether a given site in a fragment graph corresponds to a
merge mistake relative to the ground truth graph. If so, the site is
saved in an internal data structure.
Parameters
----------
gt_graph : LabeledGraph
Graph to be evaluated.
fragment_graph : FragmentGraph
Graph corresponding to a segment in the predicted segmentation.
gt_node : int
Node ID in the ground truth graph corresponding to the site.
fragment_node : int
Node ID in the fragment graph corresponding to the candidate site.
"""
# Check if pass through site without merge mistake
if self.is_nonmerge_pass_thru(gt_graph, fragment_graph, gt_node):
return
# Move site to nearby branching point if possible
fragment_node = util.search_branching_node(
fragment_graph, gt_graph.kdtree, fragment_node
)
# Record site as merge mistake
voxel = fragment_graph.voxels[fragment_node]
xyz = fragment_graph.get_xyz(fragment_node)
self.fragments_with_merge.add(fragment_graph.name)
self.merge_sites.append(
{
"Segment_ID": fragment_graph.name,
"GroundTruth_ID": gt_graph.name,
"Voxel": tuple(map(int, voxel)),
"World": tuple([float(round(t, 2)) for t in xyz]),
"Added Cable Length (μm)": 0.0
}
)
def is_nonmerge_pass_thru(self, gt_graph, fragment_graph, gt_node):
"""
Determines whether a ground truth node belongs to a small connected
component of the same label in the ground truth graph, indicating a
likely non-merge pass-through.
Parameters
----------
gt_graph : LabeledGraph
Graph to be evaluated.
fragment_graph : FragmentGraph
Graph corresponding to a segment in the predicted segmentation.
gt_node : int
Node ID in the ground truth graph to evaluate.
Returns
-------
bool
True is the node is a likely non-merge detection; otherwise, the
site is considered to be a merge mistake.
"""
nodes = gt_graph.get_nodes_with_label(fragment_graph.label)
subgraph = gt_graph.subgraph(nodes)
for nodes_cc in nx.connected_components(subgraph):
if gt_node in nodes_cc:
return len(nodes_cc) < 50
return True
# --- Helpers ---
def add_merge_site_names(self):
"""
Assigns unique name to each detected merge site.
"""
row_names = list()
for i, _ in enumerate(self.merge_sites.index, 1):
row_names.append(f"merge-{i}.swc")
self.merge_sites.index = row_names
def remove_repeat_merge_sites(self):
"""
Removes spatially redundant merge sites within a fixed distance
threshold.
"""
if len(self.merge_sites) > 0:
# Build kdtree from merge sites
kdtree = KDTree([s["World"] for s in self.merge_sites])
# Search for repeat sites
rm_idxs = set()
for i, site in enumerate(self.merge_sites):
if i not in rm_idxs:
idxs = kdtree.query_ball_point(site["World"], 40)
idxs.remove(i)
rm_idxs |= set(idxs)
# Remove repeat sites
self.merge_sites = pd.DataFrame(self.merge_sites).drop(rm_idxs)
self.add_merge_site_names()
else:
self.merge_sites = pd.DataFrame()
class ERLMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes the expected run length (ERL).
"""
def __init__(self, verbose):
"""
Instantiates an ERL object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "ERL"
def __call__(self, gt_graphs):
"""
Comptues the expected run length (ERL) of the given graphs.
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
results = dict()
pbar = self.get_pbar(len(gt_graphs))
for name, graph in gt_graphs.items():
# Compute result
erl = self.compute_graph_erl(graph)
results[name] = round(erl, 2)
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(results)
@staticmethod
def compute_graph_erl(graph):
"""
Computes the ERL of the given graph.
Parameters
----------
graph : LabeledGraph
Graph to be evaluated.
Returns
-------
float
ERL of the given graph.
"""
wgts = list()
run_lengths = list()
for label in graph.get_node_labels():
# Compute run length for label
nodes = graph.get_nodes_with_label(label)
run_length = graph.run_length_from(nodes[0])
graph.labeled_run_length += run_length
# Update
wgts.append(run_length)
run_lengths.append(
0 if label in graph.labels_with_merge else run_length
)
return np.average(run_lengths, weights=wgts) if len(wgts) > 0 else 0
# --- Derived Skeleton Metrics ---
class SplitRateMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes split rate as µm / num_splits.
"""
def __init__(self, verbose=True):
"""
Instantiates a SplitRateMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "Split Rate"
def __call__(self, gt_graphs, results):
"""
Computes split rates for the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
results : pandas.DataFrame
Data frame containing the skeleton metric results computed so far.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
new_results = dict()
pbar = self.get_pbar(len(results.index))
for name, graph in gt_graphs.items():
# Compute result
if results["# Splits"][name] > 0:
rl = util.compute_segmented_run_length(graph, results, name)
new_results[name] = round(rl / results["# Splits"][name], 2)
else:
new_results[name] = np.nan
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(new_results)
class MergeRateMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes merge rate as µm / num_merges.
"""
def __init__(self, verbose=True):
"""
Instantiates a MergeRateMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "Merge Rate"
def __call__(self, gt_graphs, results):
"""
Computes merge rates for the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
results : pandas.DataFrame
Data frame containing the skeleton metric results computed so far.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
new_results = dict()
pbar = self.get_pbar(len(gt_graphs))
for name, graph in gt_graphs.items():
# Compute result
if results["# Merges"][name] > 0:
rl = util.compute_segmented_run_length(graph, results, name)
new_results[name] = round(rl / results["# Merges"][name], 2)
else:
new_results[name] = np.nan
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(new_results)
class EdgeAccuracyMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes edge accuracy.
"""
def __init__(self, verbose=True):
"""
Instantiates an EdgeAccuracyMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "Edge Accuracy"
def __call__(self, gt_graphs, results):
"""
Computes the edge accuracy of the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
results : pandas.DataFrame
Data frame containing the skeleton metric results computed so far.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are dictionary keys and values are
stored under a column called "self.name".
"""
new_results = dict()
pbar = self.get_pbar(len(gt_graphs))
for idx in results.index:
# Compute result
edge_accuracy = 100 - (
results["% Split Edges"].loc[idx] +
results["% Omit Edges"].loc[idx] +
results["% Merged Edges"].loc[idx])
new_results[idx] = round(edge_accuracy, 2)
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(new_results)
class NormalizedERLMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes normalized expected run
length (ERL).
"""
def __init__(self, verbose=True):
"""
Instantiates a NormalizedERLMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "Normalized ERL"
def __call__(self, gt_graphs, results):
"""
Computes the normalized ERL of the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
results : pandas.DataFrame
Data frame containing the skeleton metric results computed so far.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
new_results = dict()
pbar = self.get_pbar(len(gt_graphs))
for name, graph in gt_graphs.items():
# Compute result
normalized_erl = results["ERL"][name] / graph.run_length
new_results[name] = round(normalized_erl, 4)
# Update progress bar
if self.verbose:
pbar.update(1)
return self.reformat(new_results)
class AddedCableLengthMetric(SkeletonMetric):
"""
A skeleton metric subclass that computes added cable length.
"""
def __init__(self, verbose=True):
"""
Instantiates an AddedCableLengthMetric object.
Parameters
----------
verbose : bool, optional
Indication of whether to display a progress bar. Default is True.
"""
# Call parent class
super().__init__(verbose=verbose)
# Instance attributes
self.name = "Added Cable Length (μm)"
def __call__(self, gt_graphs, fragment_graphs, merge_sites):
"""
Computes the normalized ERL of the given graphs.
Parameters
----------
gt_graphs : Dict[str, LabeledGraph]
Graphs to be evaluated.
fragment_graphs : Dict[str, FragmentGraph]
Graphs corresponding to the predicted segmentation.
merge_sites : pandas.DataFrame
Data frame containing detected merge sites.
Returns
-------
results : pandas.DataFrame
DataFrame where the indices are the dictionary keys and values are
stored under a column called "self.name".
"""
pbar = self.get_pbar(len(merge_sites.index))
pair_to_length = dict()
for i in merge_sites.index:
# Extract site info
segment_id = merge_sites["Segment_ID"][i]
gt_id = merge_sites["GroundTruth_ID"][i]
pair_id = (segment_id, gt_id)
# Check wheter to visit
if pair_id in pair_to_length:
merge_sites.loc[i, self.name] = pair_to_length[pair_id]
else:
# Get graphs
gt_graph = gt_graphs[gt_id]
fragment_graph = deepcopy(fragment_graphs[segment_id])
# Compute metric
pair_to_length[pair_id] = self.compute_added_length(
gt_graph, fragment_graph
)
merge_sites.loc[i, self.name] = pair_to_length[pair_id]
# Update progress bar
if self.verbose:
pbar.update(1)
def compute_added_length(self, gt_graph, fragment_graph):
"""
Computes the total cable length of fragment components that are not
sufficiently close to the ground-truth graph.
Parameters
----------
gt_graph : LabeledGraph
Graph containing merge mistake.
fragment_graph : FragmentGraph
Fragment that is merged to the given ground truth graph.
Returns
-------
cable_length : float
Total cable length of fragment components that remain after pruning
nodes near the ground-truth graph.
"""
# Remove nodes close to ground truth
xyz_arr = fragment_graph.voxels * fragment_graph.anisotropy
dists, _ = gt_graph.kdtree.query(xyz_arr)
max_dist = MergeCountMetric.merge_dist_threshold
fragment_graph.remove_nodes_from(np.where(dists < max_dist)[0])
# Compute cable length
cable_length = 0
for nodes in nx.connected_components(fragment_graph):
node = util.sample_once(nodes)
cable_length += fragment_graph.run_length_from(node)
return round(float(cable_length), 2)