Skip to content

Commit c35b04d

Browse files
Update cryovesnet evaluation
1 parent bbecced commit c35b04d

File tree

9 files changed

+272
-16
lines changed

9 files changed

+272
-16
lines changed

scripts/baselines/cryo_ves_net/common.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@ def _prepare_input(path, output_folder, input_key, resolution, rel_folder=None):
5353
return out_path, sub_folder
5454

5555

56-
def _process_output(tmp, tmp_file, output_folder, output_key, rel_folder=None, mask_file=None, mask_key=None):
56+
def _get_output_path(fname, output_folder, rel_folder=None):
57+
if rel_folder is None:
58+
this_output_folder = output_folder
59+
else:
60+
this_output_folder = os.path.join(output_folder, rel_folder)
61+
os.makedirs(this_output_folder, exist_ok=True)
62+
63+
out_path = os.path.join(this_output_folder, f"{fname}.h5")
64+
return out_path
65+
66+
67+
def _process_output(tmp, tmp_file, out_path, output_key, mask_file=None, mask_key=None):
5768
fname = Path(tmp_file).stem
5869
seg_path = os.path.join(tmp, "cryovesnet", f"{fname}_convex_labels.mrc")
5970
with mrcfile.open(seg_path, "r") as f:
@@ -66,13 +77,6 @@ def _process_output(tmp, tmp_file, output_folder, output_key, rel_folder=None, m
6677
seg = np.asarray(seg).copy()
6778
seg[~mask] = 0
6879

69-
if rel_folder is None:
70-
this_output_folder = output_folder
71-
else:
72-
this_output_folder = os.path.join(output_folder, rel_folder)
73-
os.makedirs(this_output_folder, exist_ok=True)
74-
75-
out_path = os.path.join(this_output_folder, f"{fname}.h5")
7680
with h5py.File(out_path, "a") as f:
7781
f.create_dataset(output_key, data=seg, compression="gzip")
7882

@@ -101,16 +105,22 @@ def apply_cryo_vesnet(
101105
with tempfile.TemporaryDirectory() as tmp:
102106

103107
for i, file in enumerate(files):
108+
fname = Path(file).stem
104109

105110
# Get the resolution info for this file.
106111
if resolution is None:
107112
res = None
108113
else:
109-
fname = Path(file).stem
110114
res = resolution[fname] if isinstance(resolution, dict) else resolution
111115

112-
# Prepare the input files by copying them over or resaving them (if h5).
116+
# Get the current output path, skip processing if it already exists
113117
rel_folder = os.path.split(os.path.relpath(file, input_folder))[0] if nested else None
118+
out_path = _get_output_path(fname, output_folder, rel_folder=rel_folder)
119+
if os.path.exists(out_path):
120+
print("Skipping processing of the", file, "because the output at", out_path, "already exists")
121+
continue
122+
123+
# Prepare the input files by copying them over or resaving them (if h5).
114124
tmp_file, sub_folder = _prepare_input(file, tmp, input_key, res, rel_folder=rel_folder)
115125

116126
# Segment the vesicles in the file.
@@ -119,5 +129,5 @@ def apply_cryo_vesnet(
119129
# Write the output file.
120130
mask_file = None if mask_files is None else mask_files[i]
121131
_process_output(
122-
sub_folder, tmp_file, output_folder, output_key, rel_folder, mask_file=mask_file, mask_key=mask_key
132+
sub_folder, tmp_file, out_path, output_key, mask_file=mask_file, mask_key=mask_key
123133
)

scripts/baselines/cryo_ves_net/evaluate_cooper.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"01_hoi_maus_2020_incomplete",
1414
"02_hcc_nanogold",
1515
"03_hog_cs1sy7",
16+
"04",
1617
"05_stem750_sv_training",
1718
"07_hoi_s1sy7_tem250_ihgp",
1819
"10_tem_single_release",
@@ -30,8 +31,8 @@ def evaluate_dataset(ds_name):
3031
return results
3132

3233
print("Evaluating ds", ds_name)
33-
input_files = sorted(glob(os.path.join(INPUT_ROOT, ds_name, "*.h5")))
34-
pred_files = sorted(glob(os.path.join(OUTPUT_ROOT, ds_name, "*.h5")))
34+
input_files = sorted(glob(os.path.join(INPUT_ROOT, ds_name, "**/*.h5"), recursive=True))
35+
pred_files = sorted(glob(os.path.join(OUTPUT_ROOT, ds_name, "**/*.h5"), recursive=True))
3536

3637
results = {
3738
"dataset": [],
@@ -76,7 +77,7 @@ def main():
7677
"07_hoi_s1sy7_tem250_ihgp",
7778
"10_tem_single_release",
7879
"11_tem_multiple_release"],
79-
"STEM": ["05_stem750_sv_training"],
80+
"STEM": ["04", "05_stem750_sv_training"],
8081
"Overall": DATASETS,
8182
}
8283

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
from glob import glob
3+
4+
import h5py
5+
import pandas as pd
6+
from elf.evaluation.matching import matching
7+
8+
9+
INPUT_FOLDER = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/fernandez-busnadiego/vesicle_gt/v3" # noqa
10+
OUTPUT_FOLDER = "./predictions/cryo"
11+
12+
13+
def evaluate_dataset(ds_name="cryo"):
14+
result_folder = "./results/cryo"
15+
os.makedirs(result_folder, exist_ok=True)
16+
result_path = os.path.join(result_folder, f"{ds_name}.csv")
17+
if os.path.exists(result_path):
18+
results = pd.read_csv(result_path)
19+
return results
20+
21+
print("Evaluating ds", ds_name)
22+
input_files = sorted(glob(os.path.join(INPUT_FOLDER, "*.h5")))
23+
pred_files = sorted(glob(os.path.join(OUTPUT_FOLDER, "*.h5")))
24+
25+
results = {
26+
"dataset": [],
27+
"file": [],
28+
"precision": [],
29+
"recall": [],
30+
"f1-score": [],
31+
}
32+
for inf, predf in zip(input_files, pred_files):
33+
fname = os.path.basename(inf)
34+
35+
with h5py.File(inf, "r") as f:
36+
gt = f["/labels/vesicles"][:]
37+
with h5py.File(predf, "r") as f:
38+
seg = f["/prediction/vesicles/cryovesnet"][:]
39+
assert gt.shape == seg.shape
40+
41+
scores = matching(seg, gt)
42+
43+
results["dataset"].append(ds_name)
44+
results["file"].append(fname)
45+
results["precision"].append(scores["precision"])
46+
results["recall"].append(scores["recall"])
47+
results["f1-score"].append(scores["f1"])
48+
49+
results = pd.DataFrame(results)
50+
results.to_csv(result_path, index=False)
51+
return results
52+
53+
54+
def main():
55+
result = evaluate_dataset()
56+
print(result)
57+
print(result["f1-score"].mean())
58+
59+
60+
if __name__ == "__main__":
61+
main()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
from glob import glob
3+
4+
import h5py
5+
import pandas as pd
6+
from elf.evaluation.matching import matching
7+
8+
9+
INPUT_FOLDER = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/wichmann/extracted/endbulb_of_held/Automatische_Segmentierung_Dataset_Validierung" # noqa
10+
OUTPUT_FOLDER = "./predictions/endbulb"
11+
12+
13+
def evaluate_dataset(ds_name="endbulb"):
14+
result_folder = "./results/endbulb"
15+
os.makedirs(result_folder, exist_ok=True)
16+
result_path = os.path.join(result_folder, f"{ds_name}.csv")
17+
if os.path.exists(result_path):
18+
results = pd.read_csv(result_path)
19+
return results
20+
21+
print("Evaluating ds", ds_name)
22+
input_files = sorted(glob(os.path.join(INPUT_FOLDER, "*.h5")))
23+
pred_files = sorted(glob(os.path.join(OUTPUT_FOLDER, "*.h5")))
24+
25+
results = {
26+
"dataset": [],
27+
"file": [],
28+
"precision": [],
29+
"recall": [],
30+
"f1-score": [],
31+
}
32+
for inf, predf in zip(input_files, pred_files):
33+
fname = os.path.basename(inf)
34+
35+
with h5py.File(inf, "r") as f:
36+
gt = f["/labels/vesicles"][:]
37+
with h5py.File(predf, "r") as f:
38+
seg = f["/prediction/vesicles/cryovesnet"][:]
39+
assert gt.shape == seg.shape
40+
41+
scores = matching(seg, gt)
42+
43+
results["dataset"].append(ds_name)
44+
results["file"].append(fname)
45+
results["precision"].append(scores["precision"])
46+
results["recall"].append(scores["recall"])
47+
results["f1-score"].append(scores["f1"])
48+
49+
results = pd.DataFrame(results)
50+
results.to_csv(result_path, index=False)
51+
return results
52+
53+
54+
def main():
55+
result = evaluate_dataset()
56+
print(result)
57+
print()
58+
print(result["f1-score"].mean())
59+
print(result["f1-score"].std())
60+
61+
62+
if __name__ == "__main__":
63+
main()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
from glob import glob
3+
4+
import h5py
5+
import pandas as pd
6+
from elf.evaluation.matching import matching
7+
8+
9+
INPUT_FOLDER = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/moser/vesicle_gt" # noqa
10+
OUTPUT_FOLDER = "./predictions/inner_ear"
11+
12+
13+
def evaluate_dataset(ds_name="inner_ear"):
14+
result_folder = "./results/inner_ear"
15+
os.makedirs(result_folder, exist_ok=True)
16+
result_path = os.path.join(result_folder, f"{ds_name}.csv")
17+
if os.path.exists(result_path):
18+
results = pd.read_csv(result_path)
19+
return results
20+
21+
print("Evaluating ds", ds_name)
22+
input_files = sorted(glob(os.path.join(INPUT_FOLDER, "*.h5")))
23+
pred_files = sorted(glob(os.path.join(OUTPUT_FOLDER, "*.h5")))
24+
25+
results = {
26+
"dataset": [],
27+
"file": [],
28+
"precision": [],
29+
"recall": [],
30+
"f1-score": [],
31+
}
32+
for inf, predf in zip(input_files, pred_files):
33+
fname = os.path.basename(inf)
34+
35+
with h5py.File(inf, "r") as f:
36+
gt = f["/labels/vesicles"][:]
37+
with h5py.File(predf, "r") as f:
38+
seg = f["/prediction/vesicles/cryovesnet"][:]
39+
assert gt.shape == seg.shape
40+
41+
scores = matching(seg, gt)
42+
43+
results["dataset"].append(ds_name)
44+
results["file"].append(fname)
45+
results["precision"].append(scores["precision"])
46+
results["recall"].append(scores["recall"])
47+
results["f1-score"].append(scores["f1"])
48+
49+
results = pd.DataFrame(results)
50+
results.to_csv(result_path, index=False)
51+
return results
52+
53+
54+
def main():
55+
result = evaluate_dataset()
56+
print(result)
57+
print()
58+
print(result["f1-score"].mean())
59+
print(result["f1-score"].std())
60+
61+
62+
if __name__ == "__main__":
63+
main()

scripts/baselines/cryo_ves_net/segment_cooper.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,26 @@ def segment_dataset(ds_name, resolution):
3333
)
3434

3535

36+
def segment_04():
37+
ds_name = "04"
38+
input_folder = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/cooper/ground_truth/04Dataset_for_vesicle_eval" # noqa
39+
output_folder = os.path.join(OUTPUT_ROOT, ds_name)
40+
41+
n_inputs = len(glob(os.path.join(input_folder, "**/*.h5"), recursive=True))
42+
n_outputs = len(glob(os.path.join(output_folder, "**/*.h5"), recursive=True))
43+
if n_inputs == n_outputs:
44+
print(ds_name, "is already processed")
45+
return
46+
47+
resolution = (8.681, 8.681, 8.681)
48+
apply_cryo_vesnet(
49+
input_folder, output_folder, pattern="*.h5", input_key="raw", resolution=resolution, nested=True,
50+
mask_folder=input_folder, mask_key="labels/compartment"
51+
)
52+
53+
3654
def main():
55+
segment_04()
3756
for ds_name, resolution in RESOLUTIONS.items():
3857
resolution = tuple(res * 10 for res in resolution)
3958
segment_dataset(ds_name, resolution)

scripts/baselines/cryo_ves_net/segment_cryo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
def main():
5-
input_folder = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/fernandez-busnadiego/vesicle_gt/v2" # noqa
6-
output_folder = "./cryo-vesnet-test"
5+
input_folder = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/fernandez-busnadiego/vesicle_gt/v3" # noqa
6+
output_folder = "./predictions/cryo"
77

88
# Resolution in Angstrom in XYZ
99
# The two tomograms have a different resolution.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from common import apply_cryo_vesnet
2+
3+
4+
def main():
5+
input_folder = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/wichmann/extracted/endbulb_of_held/Automatische_Segmentierung_Dataset_Validierung" # noqa
6+
output_folder = "./predictions/endbulb"
7+
8+
# Resolution in Angstrom in XYZ
9+
# The two tomograms have a different resolution.
10+
resolution = (17.48,) * 3
11+
apply_cryo_vesnet(
12+
input_folder, output_folder,
13+
pattern="*.h5", input_key="raw",
14+
mask_folder=input_folder, mask_key="labels/endbulb",
15+
resolution=resolution
16+
)
17+
18+
19+
if __name__ == "__main__":
20+
main()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from common import apply_cryo_vesnet
2+
3+
4+
def main():
5+
input_folder = "/mnt/lustre-emmy-hdd/projects/nim00007/data/synaptic-reconstruction/moser/vesicle_gt" # noqa
6+
output_folder = "./predictions/inner_ear"
7+
8+
# Resolution in Angstrom in XYZ
9+
# The two tomograms have a different resolution.
10+
resolution = (11.8, 11.8, 11.88)
11+
apply_cryo_vesnet(
12+
input_folder, output_folder,
13+
pattern="*.h5", input_key="raw",
14+
resolution=resolution
15+
)
16+
17+
18+
if __name__ == "__main__":
19+
main()

0 commit comments

Comments
 (0)