Skip to content

Commit 7046235

Browse files
Add cli to visualize vesicle pool assignments
1 parent 457d558 commit 7046235

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"synapse_net.export_to_imod_objects = synapse_net.tools.cli:imod_object_cli",
1919
"synapse_net.run_supervised_training = synapse_net.training.supervised_training:main",
2020
"synapse_net.run_domain_adaptation = synapse_net.training.domain_adaptation:main",
21+
"synapse_net.visualize_vesicle_tools = synapse_net.tools.cli:pool_visualization_cli",
2122
],
2223
"napari.manifest": [
2324
"synapse_net = synapse_net:napari.yaml",

synapse_net/tools/cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..inference.inference import _get_model_registry, get_model, get_model_training_resolution, run_segmentation
99
from ..inference.scalable_segmentation import scalable_segmentation
1010
from ..inference.util import inference_helper, parse_tiling
11+
from .pool_visualization import _visualize_vesicle_pools
1112

1213

1314
def imod_point_cli():
@@ -99,6 +100,25 @@ def imod_object_cli():
99100
)
100101

101102

103+
def pool_visualization_cli():
104+
parser = argparse.ArgumentParser(description="Load tomogram data, vesicle pools and additional segmentations for viualization.") # noqa
105+
parser.add_argument(
106+
"--input_path", "-i", required=True,
107+
help="The filepath to the mrc file containing the tomogram data."
108+
)
109+
parser.add_argument(
110+
"--vesicle_path", "-v", required=True, help="The filepath to the tif file containing the vesicle segmentation."
111+
)
112+
parser.add_argument(
113+
"--table_path", "-t", required=True, help="The filepath to the table with the vesicle pool assignments."
114+
)
115+
parser.add_argument(
116+
"-s", "--segmentation_paths", nargs="+", help="Filepaths for additional segmentations."
117+
)
118+
args = parser.parse_args()
119+
_visualize_vesicle_pools(args.input_path, args.vesicle_path, args.table_path, args.segmentation_paths)
120+
121+
102122
# TODO: handle kwargs
103123
def segmentation_cli():
104124
parser = argparse.ArgumentParser(description="Run segmentation.")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from pathlib import Path
2+
3+
import imageio.v3 as imageio
4+
import napari
5+
import pandas as pd
6+
7+
from ..file_utils import read_mrc
8+
9+
10+
def _create_pools(vesicles, table):
11+
label_ids, pool_colors = table.label.values, table.color.values
12+
13+
pools = vesicles
14+
colormap = {label_id: color for label_id, color in zip(label_ids, pool_colors)}
15+
colormap[None] = [0, 0, 0, 0]
16+
17+
return pools, colormap
18+
19+
20+
def _visualize_vesicle_pools(input_path, vesicle_path, table_path, segmentation_paths):
21+
# Load the tomogram data, including scale information.
22+
data, voxel_size = read_mrc(input_path)
23+
axes = "zyx" if data.ndim == 3 else "yx"
24+
scale = tuple(float(voxel_size[ax]) for ax in axes)
25+
print("Loading data with scale", scale, "nanometer")
26+
27+
# Load the vesicle layer.
28+
vesicles = imageio.imread(vesicle_path)
29+
30+
# Load the table with the pool assignments.
31+
# Create and add the pool layer.
32+
table = pd.read_excel(table_path)
33+
pools, colormap = _create_pools(vesicles, table)
34+
35+
viewer = napari.Viewer()
36+
viewer.add_image(data, scale=scale)
37+
viewer.add_labels(vesicles, scale=scale)
38+
viewer.add_labels(pools, scale=scale, name="pools", colormap=colormap)
39+
40+
# Add the additional segmentations.
41+
if segmentation_paths is not None:
42+
for seg_path in segmentation_paths:
43+
name = Path(seg_path).stem
44+
seg = imageio.imread(seg_path)
45+
viewer.add_labels(seg, name=name, scale=scale)
46+
47+
# FIXME something is wrong here.
48+
# Add the scale bar.
49+
# @magicgui(call_button="Add Scale Bar")
50+
# def add_scale_bar(v: napari.Viewer):
51+
# v.scale_bar.visible = True
52+
# v.scale_bar.unit = "nm"
53+
# viewer.window.add_dock_widget(add_scale_bar)
54+
55+
napari.run()

0 commit comments

Comments
 (0)