Skip to content

Commit 3cd5f0c

Browse files
Implement IMOD mask export WIP
1 parent eabd2aa commit 3cd5f0c

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

scripts/cooper/export_mask_to_imod.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import argparse
2+
3+
from synaptic_reconstruction.imod.to_imod import write_segmentation_to_imod
4+
5+
6+
def export_mask_to_imod(args):
7+
# Test script
8+
# write_segmentation_to_imod(
9+
# "synapse-examples/36859_J1_66K_TS_CA3_PS_26_rec_2Kb1dawbp_crop.mrc",
10+
# "synapse-examples/36859_J1_66K_TS_CA3_PS_26_rec_2Kb1dawbp_crop_mitos.tif",
11+
# "synapse-examples/mito.mod"
12+
# )
13+
write_segmentation_to_imod(args.input_path, args.segmentation_path, args.output_path)
14+
15+
16+
def main():
17+
parser = argparse.ArgumentParser()
18+
19+
args = parser.parse_args()
20+
parser.add_argument(
21+
"-i", "--input_path", required=True,
22+
help="The filepath to the mrc file containing the data."
23+
)
24+
parser.add_argument(
25+
"-s", "--segmentation_path", required=True,
26+
help="The filepath to the tif file containing the segmentation."
27+
)
28+
parser.add_argument(
29+
"-o", "--output_path", required=True,
30+
help="The filepath where the mod file with contours will be saved."
31+
)
32+
args = parser.parse_args()
33+
export_mask_to_imod(args)
34+
35+
36+
if __name__ == "__main__":
37+
main()

synaptic_reconstruction/imod/to_imod.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,63 @@
1616
from tqdm import tqdm
1717

1818

19+
# FIXME how to bring the data to the IMOD axis convention?
20+
def _to_imod_order(data):
21+
# data = np.swapaxes(data, 0, -1)
22+
# data = np.fliplr(data)
23+
# data = np.swapaxes(data, 0, -1)
24+
return data
25+
26+
27+
def write_segmentation_to_imod(
28+
mrc_path: str,
29+
segmentation_path: str,
30+
output_path: str,
31+
) -> None:
32+
"""Write a segmentation to a mod file as contours.
33+
34+
Args:
35+
mrc_path: a
36+
segmentation_path: a
37+
output_path: a
38+
"""
39+
cmd = "imodauto"
40+
cmd_path = shutil.which(cmd)
41+
assert cmd_path is not None, f"Could not find the {cmd} imod command."
42+
43+
assert os.path.exists(mrc_path)
44+
with mrcfile.open(mrc_path, mode="r+") as f:
45+
voxel_size = f.voxel_size
46+
47+
with tempfile.NamedTemporaryFile(suffix=".mrc") as f:
48+
tmp_path = f.name
49+
50+
seg = (imageio.imread(segmentation_path) > 0).astype("uint8")
51+
seg_ = _to_imod_order(seg)
52+
53+
# import napari
54+
# v = napari.Viewer()
55+
# v.add_image(seg)
56+
# v.add_labels(seg_)
57+
# napari.run()
58+
59+
mrcfile.new(tmp_path, data=seg_, overwrite=True)
60+
with mrcfile.open(tmp_path, mode="r+") as f:
61+
f.voxel_size = voxel_size
62+
f.update_header_from_data()
63+
64+
cmd_list = [cmd, "-E", "1", "-u", tmp_path, output_path]
65+
run(cmd_list)
66+
67+
1968
def convert_segmentation_to_spheres(
2069
segmentation: np.ndarray,
2170
verbose: bool = False,
2271
num_workers: Optional[int] = None,
2372
resolution: Optional[Tuple[float, float, float]] = None,
2473
radius_factor: float = 1.0,
2574
estimate_radius_2d: bool = True,
26-
):
75+
) -> Tuple[np.ndarray, np.ndarray]:
2776
"""Extract spheres parameterized by center and radius from a segmentation.
2877
2978
Args:
@@ -80,7 +129,7 @@ def write_points_to_imod(
80129
min_radius: Union[float, int],
81130
output_path: str,
82131
color: Optional[Tuple[int, int, int]] = None,
83-
):
132+
) -> None:
84133
"""Write point annotations to a .mod file for IMOD.
85134
86135
Args:
@@ -129,7 +178,7 @@ def write_segmentation_to_imod_as_points(
129178
min_radius: Union[int, float],
130179
radius_factor: float = 1.0,
131180
estimate_radius_2d: bool = True,
132-
):
181+
) -> None:
133182
"""Write segmentation results to .mod file with imod point annotations.
134183
135184
This approximates each segmented object as a sphere.
@@ -183,7 +232,7 @@ def export_helper(
183232
output_root: str,
184233
export_function: callable,
185234
force: bool = False,
186-
):
235+
) -> None:
187236
"""
188237
Helper function to run imod export for files in a directory.
189238

0 commit comments

Comments
 (0)