Skip to content

Commit 186c92d

Browse files
Update inner ear analysis scripts
1 parent 0b7884d commit 186c92d

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

scripts/cooper/full_reconstruction/visualize_results.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import numpy as np
77
import pandas as pd
88

9+
from skimage.filters import gaussian
10+
911
ROOT = "./04_full_reconstruction"
1012
TABLE = "/home/pape/Desktop/sfb1286/mboc_synapse/draft_figures/full_reconstruction.xlsx"
1113

1214
# Skip datasets for which all figures were already done.
13-
SKIP_DS = ["20241019_Tomo-eval_MF_Synapse"]
15+
SKIP_DS = ["20241019_Tomo-eval_MF_Synapse", "20241019_Tomo-eval_PS_Synapse"]
16+
# SKIP_DS = []
1417

1518

1619
def _get_name_and_row(path, table):
@@ -46,20 +49,23 @@ def visualize_result(path, table):
4649
if ds_name in SKIP_DS:
4750
return
4851

49-
# if row["Use for vis"].values[0] == "yes":
50-
if row["Use for vis"].values[0] in ("yes", "no"):
52+
if row["Use for Vis"].values[0] == "no":
5153
return
5254
compartment_ids = _get_compartment_ids(row)
5355

5456
# access = np.s_[:]
55-
access = np.s_[::2, ::2, ::2]
57+
access = np.s_[::3, ::3, ::3]
5658

5759
with h5py.File(path, "r") as f:
5860
raw = f["raw"][access]
5961
vesicles = f["labels/vesicles"][access]
6062
active_zone = f["labels/active_zone"][access]
6163
mitos = f["labels/mitochondria"][access]
6264
compartments = f["labels/compartments"][access]
65+
print("Loading done")
66+
67+
raw = gaussian(raw)
68+
print("Gaussian done")
6369

6470
if any(comp_ids is not None for comp_ids in compartment_ids):
6571
mask = np.zeros(raw.shape, dtype="bool")
@@ -78,12 +84,14 @@ def visualize_result(path, table):
7884
mitos[~mask] = 0
7985
compartments = compartments_new
8086

87+
vesicle_ids = np.unique(vesicles)[1:]
88+
8189
v = napari.Viewer()
8290
v.add_image(raw)
8391
v.add_labels(mitos)
84-
v.add_labels(vesicles)
85-
v.add_labels(compartments)
86-
v.add_labels(active_zone)
92+
v.add_labels(vesicles, colormap={ves_id: "orange" for ves_id in vesicle_ids})
93+
v.add_labels(compartments, colormap={1: "red", 2: "green", 3: "orange"})
94+
v.add_labels(active_zone, colormap={1: "blue"})
8795
v.title = f"{ds_name}/{name}"
8896
napari.run()
8997

@@ -115,6 +123,7 @@ def main():
115123
paths = sorted(glob(os.path.join(ROOT, "**/*.h5"), recursive=True))
116124
table = pd.read_excel(TABLE)
117125
for path in paths:
126+
print(path)
118127
visualize_result(path, table)
119128
# visualize_only_compartment(path, table)
120129

scripts/inner_ear/analysis/analyze_distances.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _plot_all(distances):
3535
# TODO rename the method names.
3636
# We only care about the following distances:
3737
# - MP-V -> PD, AZ (Boundary)
38-
# - Docked-V -> PD
38+
# - Docked-V -> PD, AZ
3939
# - RA-V -> Ribbon
4040
def _plot_selected(distances, save_path=None):
4141
fig, axes = plt.subplots(2, 2)
@@ -46,7 +46,7 @@ def _plot_selected(distances, save_path=None):
4646

4747
def _plot(pool_name, distance_col, structure_name, ax):
4848

49-
this_distances = distances[distances["pool"] == pool_name][["approach", distance_col]]
49+
this_distances = distances[distances["pool"] == pool_name][["tomogram", "approach", distance_col]]
5050

5151
ax.set_title(f"{pool_name} to {structure_name}")
5252
sns.histplot(
@@ -56,15 +56,27 @@ def _plot(pool_name, distance_col, structure_name, ax):
5656

5757
if save_path is not None:
5858
approaches = pd.unique(this_distances["approach"])
59-
dist_values = [
60-
this_distances[this_distances["approach"] == approach][distance_col].values.tolist()
61-
for approach in approaches
62-
]
63-
max_len = max([len(vals) for vals in dist_values])
64-
save_distances = {
65-
approach: dists + [np.nan] * (max_len - len(dists))
66-
for approach, dists in zip(approaches, dist_values)
67-
}
59+
tomo_names = pd.unique(this_distances["tomogram"])
60+
61+
tomograms = []
62+
distance_values = {approach: [] for approach in approaches}
63+
64+
for tomo in tomo_names:
65+
tomo_dists = this_distances[this_distances["tomogram"] == tomo]
66+
max_vesicles = 0
67+
for approach in approaches:
68+
n_vesicles = len(tomo_dists[tomo_dists["approach"] == approach].values)
69+
if n_vesicles > max_vesicles:
70+
max_vesicles = n_vesicles
71+
72+
for approach in approaches:
73+
app_dists = tomo_dists[tomo_dists["approach"] == approach][distance_col].values.tolist()
74+
app_dists = app_dists + [np.nan] * (max_vesicles - len(app_dists))
75+
distance_values[approach].extend(app_dists)
76+
tomograms.extend([tomo] * max_vesicles)
77+
78+
save_distances = {"tomograms": tomograms}
79+
save_distances.update(distance_values)
6880
save_distances = pd.DataFrame(save_distances)
6981

7082
sheet_name = f"{pool_name}_{structure_name}"
@@ -74,9 +86,11 @@ def _plot(pool_name, distance_col, structure_name, ax):
7486
else:
7587
save_distances.to_excel(save_path, index=False, sheet_name=sheet_name)
7688

89+
# NOTE: we over-ride a plot here, should not do this in the actual version
7790
_plot("MP-V", "pd_distance [nm]", "PD", axes[0, 0])
7891
_plot("MP-V", "boundary_distance [nm]", "AZ Membrane", axes[0, 1])
7992
_plot("Docked-V", "pd_distance [nm]", "PD", axes[1, 0])
93+
_plot("Docked-V", "boundary_distance [nm]", "AZ Membrane", axes[1, 0])
8094
_plot("RA-V", "ribbon_distance [nm]", "Ribbon", axes[1, 1])
8195

8296
fig.tight_layout()
@@ -87,17 +101,17 @@ def for_tomos_with_annotation(plot_all=True):
87101
manual_assignments, semi_automatic_assignments, automatic_assignments = get_measurements_with_annotation()
88102

89103
manual_distances = manual_assignments[
90-
["pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
104+
["tomogram", "pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
91105
]
92106
manual_distances["approach"] = ["manual"] * len(manual_distances)
93107

94108
semi_automatic_distances = semi_automatic_assignments[
95-
["pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
109+
["tomogram", "pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
96110
]
97111
semi_automatic_distances["approach"] = ["semi_automatic"] * len(semi_automatic_distances)
98112

99113
automatic_distances = automatic_assignments[
100-
["pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
114+
["tomogram", "pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
101115
]
102116
automatic_distances["approach"] = ["automatic"] * len(automatic_distances)
103117

@@ -113,12 +127,12 @@ def for_all_tomos(plot_all=True):
113127
semi_automatic_assignments, automatic_assignments = get_all_measurements()
114128

115129
semi_automatic_distances = semi_automatic_assignments[
116-
["pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
130+
["tomogram", "pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
117131
]
118132
semi_automatic_distances["approach"] = ["semi_automatic"] * len(semi_automatic_distances)
119133

120134
automatic_distances = automatic_assignments[
121-
["pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
135+
["tomogram", "pool", "ribbon_distance [nm]", "pd_distance [nm]", "boundary_distance [nm]"]
122136
]
123137
automatic_distances["approach"] = ["automatic"] * len(automatic_distances)
124138

scripts/inner_ear/analysis/analyze_vesicle_pools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ def for_all_tomos():
9494
errors.to_excel(writer, sheet_name="StandardDeviation", index=False)
9595

9696

97-
# TODO: export the vesicle diameters
98-
# TODO: export the ribbon and pd stats
97+
# TODO: export the ribbon and pd stats (first need to discuss this with Fid)
9998
def main():
10099
# for_tomos_with_annotation()
101100
for_all_tomos()

scripts/summarize_data.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
5+
az_train = pd.read_excel("data_summary/active_zone_training_data.xlsx")
6+
compartment_train = pd.read_excel("data_summary/compartment_training_data.xlsx")
7+
vesicle_train = pd.read_excel("data_summary/vesicle_training_data.xlsx")
8+
vesicle_da = pd.read_excel("data_summary/vesicle_domain_adaptation_data.xlsx", sheet_name="cryo")
9+
10+
11+
def training_resolutions():
12+
res_az = np.round(az_train["resolution"].mean(), 2)
13+
res_compartment = np.round(compartment_train["resolution"].mean(), 2)
14+
res_cryo = np.round(vesicle_da["resolution"].mean(), 2)
15+
res_vesicles = np.round(vesicle_train["resolution"].mean(), 2)
16+
17+
print("Training resolutions for models:")
18+
print("active_zone:", res_az)
19+
print("compartments:", res_compartment)
20+
# TODO
21+
print("mitochondria:", 1.0)
22+
print("vesicles_2d:", res_vesicles)
23+
print("vesicles_3d:", res_vesicles)
24+
print("vesicles_cryo:", res_cryo)
25+
26+
27+
training_resolutions()

0 commit comments

Comments
 (0)