Skip to content

Commit 9b8c7a2

Browse files
Add more inner ear analysis code
1 parent 5feff6a commit 9b8c7a2

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import os
2+
import sys
3+
4+
from glob import glob
5+
6+
import mrcfile
7+
import pandas as pd
8+
from tqdm import tqdm
9+
10+
from synaptic_reconstruction.imod.export import load_points_from_imodinfo
11+
from synaptic_reconstruction.file_utils import get_data_path
12+
13+
sys.path.append("../processing")
14+
15+
16+
def aggregate_radii(data_root, table, save_path, get_tab):
17+
if os.path.exists(save_path):
18+
return
19+
20+
radius_table = []
21+
for _, row in tqdm(table.iterrows(), total=len(table), desc="Collect tomo information"):
22+
folder = row["Local Path"]
23+
if folder == "":
24+
continue
25+
26+
tomo_name = os.path.relpath(folder, os.path.join(data_root, "Electron-Microscopy-Susi/Analyse"))
27+
tab_path = get_tab(folder)
28+
if tab_path is None:
29+
continue
30+
31+
tab = pd.read_excel(tab_path)
32+
this_tab = tab[["pool", "radius [nm]"]]
33+
this_tab.insert(0, "tomogram", [tomo_name] * len(this_tab))
34+
radius_table.append(this_tab)
35+
36+
radius_table = pd.concat(radius_table)
37+
print("Saving table for", len(radius_table), "vesicles to", save_path)
38+
radius_table.to_excel(save_path, index=False)
39+
40+
41+
def aggregate_radii_imod(data_root, table, save_path):
42+
if os.path.exists(save_path):
43+
return
44+
45+
radius_table = []
46+
for _, row in tqdm(table.iterrows(), total=len(table), desc="Collect tomo information"):
47+
folder = row["Local Path"]
48+
if folder == "":
49+
continue
50+
51+
tomo_name = os.path.relpath(folder, os.path.join(data_root, "Electron-Microscopy-Susi/Analyse"))
52+
annotation_folder = os.path.join(folder, "manuell")
53+
if not os.path.exists(annotation_folder):
54+
annotation_folder = os.path.join(folder, "Manuell")
55+
if not os.path.exists(annotation_folder):
56+
continue
57+
58+
annotations = glob(os.path.join(annotation_folder, "*.mod"))
59+
annotation_file = [ann for ann in annotations if ("vesikel" in ann.lower()) or ("vesicle" in ann.lower())]
60+
if len(annotation_file) != 1:
61+
continue
62+
annotation_file = annotation_file[0]
63+
64+
tomo_file = get_data_path(folder)
65+
with mrcfile.open(tomo_file) as f:
66+
shape = f.data.shape
67+
resolution = list(f.voxel_size.item())
68+
resolution = [res / 10 for res in resolution][0]
69+
70+
try:
71+
_, radii, labels, label_names = load_points_from_imodinfo(annotation_file, shape, resolution=resolution)
72+
except AssertionError:
73+
continue
74+
75+
this_tab = pd.DataFrame({
76+
"tomogram": [tomo_name] * len(radii),
77+
"pool": [label_names[label_id] for label_id in labels],
78+
"radius [nm]": radii,
79+
})
80+
radius_table.append(this_tab)
81+
82+
radius_table = pd.concat(radius_table)
83+
print("Saving table for", len(radius_table), "vesicles to", save_path)
84+
radius_table.to_excel(save_path, index=False)
85+
86+
87+
def get_tab_automatic(folder):
88+
tab_name = "measurements_uncorrected_assignments.xlsx"
89+
res_path = os.path.join(folder, "korrektur", tab_name)
90+
if not os.path.exists(res_path):
91+
res_path = os.path.join(folder, "Korrektur", tab_name)
92+
if not os.path.exists(res_path):
93+
res_path = None
94+
return res_path
95+
96+
97+
def get_tab_semi_automatic(folder):
98+
tab_name = "measurements.xlsx"
99+
res_path = os.path.join(folder, "korrektur", tab_name)
100+
if not os.path.exists(res_path):
101+
res_path = os.path.join(folder, "Korrektur", tab_name)
102+
if not os.path.exists(res_path):
103+
res_path = None
104+
return res_path
105+
106+
107+
def get_tab_manual(folder):
108+
tab_name = "measurements.xlsx"
109+
res_path = os.path.join(folder, "manuell", tab_name)
110+
if not os.path.exists(res_path):
111+
res_path = os.path.join(folder, "Manuell", tab_name)
112+
if not os.path.exists(res_path):
113+
res_path = None
114+
return res_path
115+
116+
117+
def main():
118+
from parse_table import parse_table, get_data_root
119+
120+
data_root = get_data_root()
121+
table_path = os.path.join(data_root, "Electron-Microscopy-Susi", "Übersicht.xlsx")
122+
table = parse_table(table_path, data_root)
123+
124+
# TODO get the radii from imod
125+
aggregate_radii(data_root, table, save_path="./results/vesicle_radii_automatic.xlsx", get_tab=get_tab_automatic)
126+
aggregate_radii(data_root, table, save_path="./results/vesicle_radii_semi_automatic.xlsx", get_tab=get_tab_semi_automatic) # noqa
127+
aggregate_radii(data_root, table, save_path="./results/vesicle_radii_manual.xlsx", get_tab=get_tab_manual)
128+
aggregate_radii_imod(data_root, table, save_path="./results/vesicle_radii_imod.xlsx")
129+
130+
131+
if __name__ == "__main__":
132+
main()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
5+
def main():
6+
man_path = "../results/20240917_1/fully_manual_analysis_results.xlsx"
7+
auto_path = "../results/20240917_1/automatic_analysis_results.xlsx"
8+
9+
man_measurements = pd.read_excel(man_path, sheet_name="morphology")
10+
man_measurements = man_measurements[man_measurements.structure == "ribbon"][
11+
["tomogram", "surface [nm^2]", "volume [nm^3]"]
12+
]
13+
14+
auto_measurements = pd.read_excel(auto_path, sheet_name="morphology")
15+
auto_measurements = auto_measurements[auto_measurements.structure == "ribbon"][
16+
["tomogram", "surface [nm^2]", "volume [nm^3]"]
17+
]
18+
19+
# save all the automatic measurements
20+
auto_measurements.to_excel("./results/ribbon_morphology_auto.xlsx", index=False)
21+
22+
man_tomograms = pd.unique(man_measurements["tomogram"])
23+
auto_tomograms = pd.unique(auto_measurements["tomogram"])
24+
tomos = np.intersect1d(man_tomograms, auto_tomograms)
25+
26+
man_measurements = man_measurements[man_measurements.tomogram.isin(tomos)]
27+
auto_measurements = auto_measurements[auto_measurements.tomogram.isin(tomos)]
28+
29+
save_path = "./results/ribbon_morphology_man-v-auto.xlsx"
30+
man_measurements.to_excel(save_path, sheet_name="manual", index=False)
31+
with pd.ExcelWriter(save_path, engine="openpyxl", mode="a") as writer:
32+
auto_measurements.to_excel(writer, sheet_name="auto", index=False)
33+
34+
35+
if __name__ == "__main__":
36+
main()

0 commit comments

Comments
 (0)