Skip to content

Commit 1e977ec

Browse files
author
anna-grim
committed
feat: log results overview
1 parent c251ff2 commit 1e977ec

File tree

10 files changed

+63
-29
lines changed

10 files changed

+63
-29
lines changed

demo/demo.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@ def evaluate():
2929
3030
"""
3131
# Initializations
32-
path = f"{output_dir}/results.xls"
3332
pred_labels = TiffReader(pred_labels_path)
3433
skeleton_metric = SkeletonMetric(
3534
groundtruth_pointer,
3635
pred_labels,
3736
fragments_pointer=fragments_pointer,
3837
output_dir=output_dir,
3938
)
40-
full_results, avg_results = skeleton_metric.run(path)
39+
skeleton_metric.run(output_dir)
4140

4241

4342
if __name__ == "__main__":
4443
# Initializations
4544
output_dir = "./"
46-
pred_labels_path = "./pred_labels.tif"
47-
fragments_pointer = "./pred_swcs.zip"
48-
groundtruth_pointer = "./target_swcs.zip"
45+
pred_labels_path = "./data/pred_labels.tif"
46+
fragments_pointer = "./data/pred_swcs.zip"
47+
groundtruth_pointer = "./data/target_swcs.zip"
4948

5049
# Run
5150
evaluate()

demo/evaluation_results.xls

-5.5 KB
Binary file not shown.

demo/merged_ids-segmentation.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

demo/merged_segment_ids.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Label - Physical Coordinate

demo/results-overview.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Average Results...
2+
# splits: 7.6878
3+
# merges: 0.0
4+
% omit: 0.0514
5+
% split: 0.0158
6+
% merged: 0.0
7+
edge accuracy: 0.9486
8+
erl: 173.2351
9+
normalized erl: 0.3895
10+
11+
Total Results...
12+
# splits: 36
13+
# merges: 0

src/segmentation_skeleton_metrics/skeleton_metric.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def __init__(
5959
self,
6060
gt_pointer,
6161
label_mask,
62+
output_dir,
6263
anisotropy=(1.0, 1.0, 1.0),
6364
connections_path=None,
6465
fragments_pointer=None,
65-
output_dir=None,
6666
preexisting_merges=None,
6767
save_merges=False,
6868
valid_labels=None,
@@ -79,6 +79,8 @@ def __init__(
7979
coordinates.
8080
label_mask : ImageReader
8181
Predicted segmentation mask.
82+
output_dir : str
83+
Path to directory wehere results are written.
8284
anisotropy : Tuple[float], optional
8385
Image to physical coordinate scaling factors applied to SWC files
8486
stored at "fragments_pointer". The default is (1.0, 1.0, 1.0).
@@ -90,8 +92,6 @@ def __init__(
9092
"swc_util.Reader" for documentation. Notes: (1) "anisotropy" is
9193
applied to these SWC files and (2) these SWC files are required
9294
for counting merges. The default is None.
93-
output_dir : str, optional
94-
Path to directory wehere results are written. The default is None.
9595
preexisting_merges : List[int], optional
9696
List of segment IDs that are known to contain a merge mistake. The
9797
default is None.
@@ -346,29 +346,28 @@ def init_zip_writer(self):
346346
347347
"""
348348
# Initialize output directory
349-
output_dir = os.path.join(self.output_dir, "projections")
350-
util.mkdir(output_dir)
349+
projections_dir = os.path.join(self.output_dir, "projections")
350+
util.mkdir(projections_dir)
351351

352352
# Save intial graphs
353353
self.zip_writer = dict()
354354
for key in self.graphs.keys():
355-
self.zip_writer[key] = ZipFile(f"{output_dir}/{key}.zip", "w")
355+
zip_path = f"{projections_dir}/{key}.zip"
356+
self.zip_writer[key] = ZipFile(zip_path, "w")
356357
self.graphs[key].to_zipped_swc(self.zip_writer[key])
357358

358359
# -- Main Routine --
359-
def run(self, path=None):
360+
def run(self):
360361
"""
361362
Computes skeleton-based metrics.
362363
363364
Parameters
364365
----------
365-
path : str, optional
366-
Path where the results will be saved. The default is None.
366+
None
367367
368368
Returns
369369
-------
370-
tuple
371-
...
370+
None
372371
373372
"""
374373
print("\n(3) Evaluation")
@@ -381,21 +380,23 @@ def run(self, path=None):
381380
self.detect_merges()
382381
self.quantify_merges()
383382

384-
# Report results
383+
# Compute metrics
385384
full_results, avg_results = self.compile_results()
386-
print(f"\nAverage Results...")
387-
for key in avg_results.keys():
388-
print(f" {key}: {round(avg_results[key], 4)}")
389385

390-
print(f"\nTotal Results...")
391-
print("# splits:", self.count_total_splits())
392-
print("# merges:", self.count_total_merges())
386+
# Report full results
387+
prefix = "corrected-" if self.connections_path else ""
388+
path = f"{self.output_dir}/{prefix}results.xls"
389+
util.save_results(path, full_results)
393390

394-
# Save results (if applicable)
395-
if path:
396-
util.save_results(path, full_results)
391+
# Report results overview
392+
path = os.path.join(self.output_dir, f"{prefix}results-overview.txt")
393+
util.update_txt(path, "Average Results...")
394+
for key in avg_results.keys():
395+
util.update_txt(path, f" {key}: {round(avg_results[key], 4)}")
397396

398-
return full_results, avg_results
397+
util.update_txt(path, "\nTotal Results...")
398+
util.update_txt(path, f" # splits: {self.count_total_splits()}")
399+
util.update_txt(path, f" # merges: {self.count_total_merges()}")
399400

400401
# -- Split Detection --
401402
def detect_splits(self):
@@ -695,7 +696,7 @@ def save_merged_labels(self):
695696
"""
696697
# Save detected merges
697698
prefix = "corrected_" if self.connections_path else ""
698-
filename = f"merged_{prefix}_segment_ids.txt"
699+
filename = f"merged_{prefix}segment_ids.txt"
699700
with open(os.path.join(self.output_dir, filename), "w") as f:
700701
f.write(f" Label - Physical Coordinate\n")
701702
for _, label, xyz in self.merged_labels:

src/segmentation_skeleton_metrics/utils/util.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ def read_txt(path):
148148
return f.read().splitlines()
149149

150150

151+
def update_txt(path, text):
152+
"""
153+
Appends the given text to a specified text file and prints the text.
154+
155+
Parameters
156+
----------
157+
path : str
158+
Path to txt file where the text will be appended.
159+
text : str
160+
Text to be written to the file.
161+
162+
Returns
163+
-------
164+
None
165+
166+
"""
167+
print(text)
168+
with open(path, 'a') as file:
169+
file.write(text + "\n")
170+
171+
151172
# --- Miscellaneous ---
152173
def get_segment_id(filename):
153174
"""

0 commit comments

Comments
 (0)