Skip to content

Commit fd17888

Browse files
Plot updates
1 parent 527e636 commit fd17888

File tree

5 files changed

+150
-5
lines changed

5 files changed

+150
-5
lines changed

flamingo_tools/extract_block_util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def extract_block(
6464

6565
if output_dir == "":
6666
output_dir = input_dir
67+
os.makedirs(output_dir, exist_ok=True)
6768

6869
if tif:
6970
if output_key is None:

reproducibility/templates_processing/segment_unet_IHC_template.sbatch

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#SBATCH --mem 400G
99

1010
source ~/.bashrc
11-
micromamba activate micro-sam_gpu
11+
# micromamba activate micro-sam_gpu
12+
micromamba activate sam
1213

1314
# Print out some info.
1415
echo "Submitting job with sbatch from directory: ${SLURM_SUBMIT_DIR}"
@@ -19,7 +20,8 @@ echo "Current node: ${SLURM_NODELIST}"
1920
# Run the script
2021
#python myprogram.py $SLURM_ARRAY_TASK_ID
2122

22-
SCRIPT_REPO=/user/schilling40/u15000/flamingo-tools
23+
# SCRIPT_REPO=/user/schilling40/u15000/flamingo-tools
24+
SCRIPT_REPO=/user/pape41/u12086/Work/my_projects/flamingo-tools
2325
cd "$SCRIPT_REPO"/flamingo_tools/segmentation/ || exit
2426

2527
# name of cochlea, as it appears in MoBIE and the NHR

scripts/figures/plot_fig2.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def plot_seg_crop(img_path, seg_path, save_path, xlim1, xlim2, ylim1, ylim2, bou
6666
plt.close()
6767

6868

69-
def fig_02b_sgn(save_dir, plot=False):
69+
def fig_02a_sgn(save_dir, plot=False):
7070
"""Plot crops of SGN segmentation of CochleaNet, Cellpose and micro-sam.
7171
"""
7272
cochlea_dir = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet"
@@ -402,9 +402,12 @@ def main():
402402

403403
os.makedirs(args.figure_dir, exist_ok=True)
404404

405-
# Panel C: Evaluation of the segmentation results:
406-
fig_02b_sgn(save_dir=args.figure_dir, plot=args.plot)
405+
# Panes A and B: Qualitative comparison of visualization results.
406+
fig_02a_sgn(save_dir=args.figure_dir, plot=args.plot)
407+
return
407408
fig_02b_ihc(save_dir=args.figure_dir, plot=args.plot)
409+
410+
# Panel C: Evaluation of the segmentation results:
408411
fig_02c(save_path=os.path.join(args.figure_dir, "fig_02c"), plot=args.plot, all_versions=False)
409412

410413
# Panel D: The number of SGNs, IHCs and average number of ribbon synapses per IHC
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import os
2+
from glob import glob
3+
from pathlib import Path
4+
import json
5+
6+
import imageio.v3 as imageio
7+
import napari
8+
import numpy as np
9+
from skimage.segmentation import find_boundaries
10+
11+
FOR_COMPARISON = ["distance_unet", "micro-sam", "cellpose3"]
12+
13+
14+
def _eval_seg(seg, eval_path):
15+
with open(eval_path, "r") as f:
16+
eval_res = json.load(f)
17+
18+
correct, wrong = eval_res["tp_objects"], eval_res["fp"]
19+
all_ids = correct + wrong
20+
seg[~np.isin(seg, all_ids)] = 0
21+
22+
eva_mask = np.zeros_like(seg)
23+
24+
eva_mask[np.isin(seg, correct)] = 1
25+
eva_mask[np.isin(seg, wrong)] = 2
26+
27+
bd = find_boundaries(seg)
28+
return bd, eva_mask
29+
30+
31+
def sgn_comparison():
32+
z = 10
33+
34+
cochlea_dir = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet"
35+
val_sgn_dir = f"{cochlea_dir}/predictions/val_sgn"
36+
image_dir = f"{cochlea_dir}/AnnotatedImageCrops/F1ValidationSGNs/for_consensus_annotation"
37+
38+
image_paths = sorted(glob(os.path.join(image_dir, "*.tif")))
39+
40+
for path in image_paths:
41+
image = imageio.imread(path)[z]
42+
43+
seg_fname = Path(path).stem + "_seg.tif"
44+
eval_fname = Path(path).stem + "_dic.json"
45+
46+
segmentations, boundaries, eval_im = {}, {}, {}
47+
for seg_name in FOR_COMPARISON:
48+
seg_path = os.path.join(val_sgn_dir, seg_name, seg_fname)
49+
eval_path = os.path.join(val_sgn_dir, seg_name, eval_fname)
50+
assert os.path.exists(seg_path), seg_path
51+
52+
seg = imageio.imread(seg_path)[z]
53+
54+
bd, eva = _eval_seg(seg, eval_path)
55+
segmentations[seg_name] = seg
56+
boundaries[seg_name] = bd
57+
eval_im[seg_name] = eva
58+
59+
v = napari.Viewer()
60+
v.add_image(image)
61+
for seg_name, bd in boundaries.items():
62+
v.add_labels(bd, name=seg_name, colormap={1: "cyan"})
63+
v.add_labels(eval_im[seg_name], name=f"{seg_name}_eval", colormap={1: "green", 2: "red"})
64+
v.title = Path(path).stem
65+
napari.run()
66+
67+
68+
def ihc_comparison():
69+
z = 10
70+
71+
cochlea_dir = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet"
72+
val_sgn_dir = f"{cochlea_dir}/predictions/val_ihc"
73+
image_dir = f"{cochlea_dir}/AnnotatedImageCrops/F1ValidationIHCs"
74+
75+
image_paths = sorted(glob(os.path.join(image_dir, "*.tif")))
76+
77+
for path in image_paths:
78+
image = imageio.imread(path)[z]
79+
80+
seg_fname = Path(path).stem + "_seg.tif"
81+
eval_fname = Path(path).stem + "_dic.json"
82+
83+
segmentations, boundaries, eval_im = {}, {}, {}
84+
for seg_name in FOR_COMPARISON:
85+
# FIXME distance_unet_v4b is missing the eval files
86+
seg_name_ = "distance_unet_v3" if seg_name == "distance_unet" else seg_name
87+
seg_path = os.path.join(val_sgn_dir, seg_name_, seg_fname)
88+
eval_path = os.path.join(val_sgn_dir, seg_name_, eval_fname)
89+
assert os.path.exists(seg_path), seg_path
90+
91+
seg = imageio.imread(seg_path)[z]
92+
93+
bd, eva = _eval_seg(seg, eval_path)
94+
segmentations[seg_name] = seg
95+
boundaries[seg_name] = bd
96+
eval_im[seg_name] = eva
97+
98+
v = napari.Viewer()
99+
v.add_image(image)
100+
for seg_name, bd in boundaries.items():
101+
v.add_labels(bd, name=seg_name, colormap={1: "cyan"})
102+
v.add_labels(eval_im[seg_name], name=f"{seg_name}_eval", colormap={1: "green", 2: "red"})
103+
v.title = Path(path).stem
104+
napari.run()
105+
106+
107+
def main():
108+
# sgn_comparison()
109+
ihc_comparison()
110+
111+
112+
if __name__ == "__main__":
113+
main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from flamingo_tools.extract_block_util import extract_block
2+
3+
4+
positions_with_signal = [
5+
[1215.074063453085, 912.697256780485, 1036.814204517708],
6+
[1030.351117830933, 1262.3358840155736, 1123.2581736686361],
7+
[1192.167776682008, 354.058713359485, 767.1544606203263],
8+
[916.9294364078347, 754.7061965177552, 923.607923806173],
9+
]
10+
positions_without_signal = [
11+
[1383.4288658807268, 783.0008672288084, 467.5426478786816],
12+
]
13+
14+
halo = [256, 256, 64]
15+
16+
17+
for pos in positions_with_signal + positions_without_signal:
18+
extract_block(
19+
input_path="M_LR_000099_L/images/ome-zarr/PV.ome.zarr",
20+
coords=pos,
21+
output_dir="./MLR99L_for_DA",
22+
input_key="s0",
23+
roi_halo=halo,
24+
tif=True,
25+
s3=True
26+
)

0 commit comments

Comments
 (0)