Skip to content

Commit 96ea707

Browse files
committed
Added final SGN consensus
1 parent 4ea20d5 commit 96ea707

File tree

1 file changed

+90
-9
lines changed

1 file changed

+90
-9
lines changed

scripts/baselines/eval_baseline.py

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ def eval_all_sgn():
5858
annotation_dir = os.path.join(cochlea_dir,
5959
"AnnotatedImageCrops",
6060
"F1ValidationSGNs",
61-
"for_consensus_annotation",
62-
"consensus_annotations")
61+
"final_annotations",
62+
"final_consensus_annotations")
63+
6364
baselines = [
6465
"cellpose3",
65-
"cellpose-sam"
66-
"distance-unet",
66+
"cellpose-sam",
67+
"distance_unet",
6768
"micro-sam",
6869
"stardist"]
6970

7071
for baseline in baselines:
71-
eval_segmentation(seg_dir=seg_dir, annotation_dir=annotation_dir)
72+
eval_segmentation(os.path.join(seg_dir, baseline), annotation_dir=annotation_dir)
7273

7374

7475
def eval_all_ihc():
@@ -79,15 +80,16 @@ def eval_all_ihc():
7980
annotation_dir = (cochlea_dir, "AnnotatedImageCrops/F1ValidationIHCs/consensus_annotation")
8081
baselines = [
8182
"cellpose3",
82-
"cellpose-sam"
83-
"distance-unet_v3",
83+
"cellpose-sam",
84+
"distance_unet_v3",
8485
"micro-sam"]
8586

8687
for baseline in baselines:
87-
eval_segmentation(seg_dir=seg_dir, annotation_dir=annotation_dir)
88+
eval_segmentation(os.path.join(seg_dir, baseline), annotation_dir=annotation_dir)
8889

8990

9091
def eval_segmentation(seg_dir, annotation_dir):
92+
print(f"Evaluating segmentation in directory {seg_dir}")
9193
segs = [entry.path for entry in os.scandir(seg_dir) if entry.is_file() and ".tif" in entry.path]
9294

9395
seg_dicts = []
@@ -97,6 +99,7 @@ def eval_segmentation(seg_dir, annotation_dir):
9799
basename = ".".join(basename.split(".")[:-1])
98100
basename = "".join(basename.split("_seg")[0])
99101
print(basename)
102+
print("Annotation_dir", annotation_dir)
100103
dic_out = os.path.join(seg_dir, f"{basename}_dic.json")
101104
if not os.path.isfile(dic_out):
102105

@@ -110,7 +113,7 @@ def eval_segmentation(seg_dir, annotation_dir):
110113
seg_filtered = filter_seg(seg_arr=seg_arr)
111114

112115
seg_dic = compute_matches_for_annotated_slice(segmentation=seg_filtered,
113-
annotations=annotation_dir,
116+
annotations=df,
114117
matching_tolerance=5)
115118
seg_dic["annotation_length"] = len(df)
116119
seg_dic["crop_name"] = basename
@@ -125,3 +128,81 @@ def eval_segmentation(seg_dir, annotation_dir):
125128
json_out = os.path.join(seg_dir, "eval_seg.json")
126129
with open(json_out, "w") as f:
127130
json.dump(seg_dicts, f, indent='\t', separators=(',', ': '))
131+
132+
133+
def print_accuracy(eval_dir):
134+
"""Print 'Precision', 'Recall', and 'F1-score' for dictionaries in a given directory.
135+
"""
136+
eval_dicts = [entry.path for entry in os.scandir(eval_dir) if entry.is_file() and "dic.json" in entry.path]
137+
precision_list = []
138+
recall_list = []
139+
f1_score_list = []
140+
for eval_dic in eval_dicts:
141+
with open(eval_dic, "r") as f:
142+
d = json.load(f)
143+
tp = len(d["tp_objects"])
144+
fp = len(d["fp"])
145+
fn = len(d["fn"])
146+
147+
if tp + fp != 0:
148+
precision = tp / (tp + fp)
149+
else:
150+
precision = 0
151+
if tp + fn != 0:
152+
recall = tp / (tp + fn)
153+
else:
154+
recall = 0
155+
if precision + recall != 0:
156+
f1_score = 2 * precision * recall / (precision + recall)
157+
else: f1_score = 0
158+
159+
precision_list.append(precision)
160+
recall_list.append(recall)
161+
f1_score_list.append(f1_score)
162+
163+
names = ["Precision", "Recall", "F1 score"]
164+
for num, lis in enumerate([precision_list, recall_list, f1_score_list]):
165+
print(names[num], sum(lis) / len(lis))
166+
167+
168+
def print_accuracy_sgn():
169+
"""Print 'Precision', 'Recall', and 'F1-score' for all SGN baselines.
170+
"""
171+
cochlea_dir = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet"
172+
seg_dir = os.path.join(cochlea_dir, "predictions/val_sgn")
173+
baselines = [
174+
"cellpose3",
175+
"cellpose-sam",
176+
"distance_unet",
177+
"micro-sam",
178+
"stardist"]
179+
for baseline in baselines:
180+
print(f"Evaluating baseline {baseline}")
181+
print_accuracy(os.path.join(seg_dir, baseline))
182+
183+
184+
def print_accuracy_ihc():
185+
"""Print 'Precision', 'Recall', and 'F1-score' for all IHC baselines.
186+
"""
187+
cochlea_dir = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet"
188+
seg_dir = os.path.join(cochlea_dir, "predictions/val_ihc")
189+
baselines = [
190+
"cellpose3",
191+
"cellpose-sam",
192+
"distance_unet_v3",
193+
"micro-sam"]
194+
195+
for baseline in baselines:
196+
print(f"Evaluating baseline {baseline}")
197+
print_accuracy(os.path.join(seg_dir, baseline))
198+
199+
200+
def main():
201+
#eval_all_sgn()
202+
#eval_all_ihc()
203+
#print_accuracy_sgn()
204+
print_accuracy_ihc()
205+
206+
207+
if __name__ == "__main__":
208+
main()

0 commit comments

Comments
 (0)