Skip to content

Commit 59e01dd

Browse files
Update result visualization
1 parent 64d56e5 commit 59e01dd

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import os
2+
from glob import glob
3+
4+
import h5py
5+
import napari
6+
import numpy as np
7+
import pandas as pd
8+
9+
ROOT = "./04_full_reconstruction"
10+
TABLE = "/home/pape/Desktop/sfb1286/mboc_synapse/draft_figures/full_reconstruction.xlsx"
11+
12+
# Skip datasets for which all figures were already done.
13+
SKIP_DS = ["20241019_Tomo-eval_MF_Synapse"]
14+
15+
16+
def _get_name_and_row(path, table):
17+
ds_name, name = os.path.split(path)
18+
ds_name = os.path.split(ds_name)[1]
19+
row = table[(table["dataset"] == ds_name) & (table["tomogram"] == name)]
20+
return ds_name, name, row
21+
22+
23+
def _get_compartment_ids(row):
24+
compartment_ids = []
25+
for comp in ("Compartment 1", "Compartment 2", "Compartment 3", "Compartment 4"):
26+
comp_ids = row[comp].values[0]
27+
try:
28+
comp_ids = list(map(int, comp_ids.split(", ")))
29+
except AttributeError:
30+
pass
31+
32+
if np.isnan(comp_ids).all():
33+
compartment_ids.append(None)
34+
continue
35+
36+
if isinstance(comp_ids, int):
37+
comp_ids = [comp_ids]
38+
compartment_ids.append(comp_ids)
39+
40+
return compartment_ids
41+
42+
43+
def visualize_result(path, table):
44+
ds_name, name, row = _get_name_and_row(path, table)
45+
46+
if ds_name in SKIP_DS:
47+
return
48+
49+
# if row["Use for vis"].values[0] == "yes":
50+
if row["Use for vis"].values[0] in ("yes", "no"):
51+
return
52+
compartment_ids = _get_compartment_ids(row)
53+
54+
# access = np.s_[:]
55+
access = np.s_[::2, ::2, ::2]
56+
57+
with h5py.File(path, "r") as f:
58+
raw = f["raw"][access]
59+
vesicles = f["labels/vesicles"][access]
60+
active_zone = f["labels/active_zone"][access]
61+
mitos = f["labels/mitochondria"][access]
62+
compartments = f["labels/compartments"][access]
63+
64+
if any(comp_ids is not None for comp_ids in compartment_ids):
65+
mask = np.zeros(raw.shape, dtype="bool")
66+
compartments_new = np.zeros_like(compartments)
67+
68+
print("Filtering compartments:")
69+
for i, comp_ids in enumerate(compartment_ids, 1):
70+
if comp_ids is None:
71+
continue
72+
print(i, comp_ids)
73+
this_mask = np.isin(compartments, comp_ids)
74+
mask[this_mask] = 1
75+
compartments_new[this_mask] = i
76+
77+
vesicles[~mask] = 0
78+
mitos[~mask] = 0
79+
compartments = compartments_new
80+
81+
v = napari.Viewer()
82+
v.add_image(raw)
83+
v.add_labels(mitos)
84+
v.add_labels(vesicles)
85+
v.add_labels(compartments)
86+
v.add_labels(active_zone)
87+
v.title = f"{ds_name}/{name}"
88+
napari.run()
89+
90+
91+
def visualize_only_compartment(path, table):
92+
ds_name, name, row = _get_name_and_row(path, table)
93+
compartment_ids = _get_compartment_ids(row)
94+
95+
# Skip if we already have annotated the presynapse compartment(s)
96+
if any(comp_id is not None for comp_id in compartment_ids):
97+
print("Compartments already annotated for", ds_name, name)
98+
return
99+
100+
# access = np.s_[:]
101+
access = np.s_[::2, ::2, ::2]
102+
103+
with h5py.File(path, "r") as f:
104+
raw = f["raw"][access]
105+
compartments = f["labels/compartments"][access]
106+
107+
v = napari.Viewer()
108+
v.add_image(raw)
109+
v.add_labels(compartments)
110+
v.title = f"{ds_name}/{name}"
111+
napari.run()
112+
113+
114+
def main():
115+
paths = sorted(glob(os.path.join(ROOT, "**/*.h5"), recursive=True))
116+
table = pd.read_excel(TABLE)
117+
for path in paths:
118+
visualize_result(path, table)
119+
# visualize_only_compartment(path, table)
120+
121+
122+
if __name__ == "__main__":
123+
main()

0 commit comments

Comments
 (0)