Skip to content

Commit c3e2766

Browse files
Update annotation comparison
1 parent 80d205d commit c3e2766

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import os
2+
from glob import glob
3+
4+
import napari
5+
import pandas as pd
6+
import tifffile
7+
8+
ROOT = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/AnnotatedImageCrops/F1ValidationIHCs"
9+
# ANNOTATION_FOLDERS = ["AnnotationsEK", "AnnotationsAMD", "AnnotationsLR"]
10+
ANNOTATION_FOLDERS = ["Annotations_AMD", "Annotations_LR"]
11+
COLOR = ["green", "yellow", "orange"]
12+
13+
14+
def _match_annotations(image_path):
15+
prefix = os.path.basename(image_path).split("_")[:3]
16+
prefix = "_".join(prefix)
17+
18+
annotations = {}
19+
for annotation_folder in ANNOTATION_FOLDERS:
20+
all_annotations = glob(os.path.join(ROOT, annotation_folder, "*.csv"))
21+
matches = [ann for ann in all_annotations if os.path.basename(ann).startswith(prefix)]
22+
if len(matches) == 0:
23+
continue
24+
assert len(matches) == 1
25+
annotation_path = matches[0]
26+
27+
annotation = pd.read_csv(annotation_path)[["axis-0", "axis-1", "axis-2"]].values
28+
annotations[annotation_folder] = annotation
29+
30+
return annotations
31+
32+
33+
def compare_annotations(image_path):
34+
annotations = _match_annotations(image_path)
35+
36+
image = tifffile.memmap(image_path)
37+
v = napari.Viewer()
38+
v.add_image(image)
39+
for i, (name, annotation) in enumerate(annotations.items()):
40+
v.add_points(annotation, name=name, face_color=COLOR[i])
41+
v.title = os.path.basename(image_path)
42+
napari.run()
43+
44+
45+
def visualize(image_paths):
46+
for image_path in image_paths:
47+
compare_annotations(image_path)
48+
49+
50+
def check_annotations(image_paths):
51+
annotation_status = {"file": []}
52+
annotation_status.update({ann: [] for ann in ANNOTATION_FOLDERS})
53+
for image_path in image_paths:
54+
annotations = _match_annotations(image_path)
55+
annotation_status["file"].append(os.path.basename(image_path))
56+
for ann in ANNOTATION_FOLDERS:
57+
annotation_status[ann].append("Yes" if ann in annotations else "No")
58+
annotation_status = pd.DataFrame(annotation_status)
59+
print(annotation_status)
60+
61+
62+
def main():
63+
import argparse
64+
parser = argparse.ArgumentParser()
65+
parser.add_argument("--images", nargs="+")
66+
parser.add_argument("--check", action="store_true")
67+
args = parser.parse_args()
68+
69+
if args.images is None:
70+
image_paths = sorted(glob(os.path.join(ROOT, "*.tif")))
71+
else:
72+
image_paths = args.images
73+
74+
if args.check:
75+
check_annotations(image_paths)
76+
else:
77+
visualize(image_paths)
78+
79+
80+
if __name__ == "__main__":
81+
main()

scripts/validation/IHCs/run_evaluation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
)
88

99
ROOT = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/AnnotatedImageCrops/F1ValidationIHCs"
10-
ANNOTATION_FOLDERS = ["Annotations_LR"]
10+
# ANNOTATION_FOLDERS = ["AnnotationsEK", "AnnotationsAMD", "AnnotationsLR"]
11+
ANNOTATION_FOLDERS = ["Annotations_AMD", "Annotations_LR"]
1112

1213

1314
def run_evaluation(root, annotation_folders, result_file, cache_folder):

scripts/validation/SGNs/compare_annotations.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import tifffile
77

88
ROOT = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/AnnotatedImageCrops/F1ValidationSGNs"
9-
ANNOTATION_FOLDERS = ["AnnotationsEK", "AnnotationsAMD", "AnnotationLR"]
9+
ANNOTATION_FOLDERS = ["AnnotationsEK", "AnnotationsAMD", "AnnotationsLR"]
1010
COLOR = ["green", "yellow", "orange"]
1111

1212

@@ -18,8 +18,9 @@ def _match_annotations(image_path):
1818
for annotation_folder in ANNOTATION_FOLDERS:
1919
all_annotations = glob(os.path.join(ROOT, annotation_folder, "*.csv"))
2020
matches = [ann for ann in all_annotations if os.path.basename(ann).startswith(prefix)]
21-
if len(matches) != 1:
21+
if len(matches) == 0:
2222
continue
23+
assert len(matches) == 1
2324
annotation_path = matches[0]
2425

2526
annotation = pd.read_csv(annotation_path)[["axis-0", "axis-1", "axis-2"]].values
@@ -40,19 +41,39 @@ def compare_annotations(image_path):
4041
napari.run()
4142

4243

44+
def visualize(image_paths):
45+
for image_path in image_paths:
46+
compare_annotations(image_path)
47+
48+
49+
def check_annotations(image_paths):
50+
annotation_status = {"file": []}
51+
annotation_status.update({ann: [] for ann in ANNOTATION_FOLDERS})
52+
for image_path in image_paths:
53+
annotations = _match_annotations(image_path)
54+
annotation_status["file"].append(os.path.basename(image_path))
55+
for ann in ANNOTATION_FOLDERS:
56+
annotation_status[ann].append("Yes" if ann in annotations else "No")
57+
annotation_status = pd.DataFrame(annotation_status)
58+
print(annotation_status)
59+
60+
4361
def main():
4462
import argparse
4563
parser = argparse.ArgumentParser()
4664
parser.add_argument("--images", nargs="+")
65+
parser.add_argument("--check", action="store_true")
4766
args = parser.parse_args()
4867

4968
if args.images is None:
5069
image_paths = sorted(glob(os.path.join(ROOT, "*.tif")))
5170
else:
5271
image_paths = args.images
5372

54-
for image_path in image_paths:
55-
compare_annotations(image_path)
73+
if args.check:
74+
check_annotations(image_paths)
75+
else:
76+
visualize(image_paths)
5677

5778

5879
if __name__ == "__main__":

0 commit comments

Comments
 (0)