Skip to content

Commit d344d49

Browse files
committed
2D vesicle export to imod
1 parent 9c252ed commit d344d49

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

synapse_net/imod/to_imod.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def write_points_to_imod(
176176
"""Write point annotations to a .mod file for IMOD.
177177
178178
Args:
179-
coordinates: Array with the point coordinates.
179+
coordinates: Array with the point coordinates. #2D or 3D
180180
radii: Array with the point radii.
181181
shape: Shape of the volume to be exported.
182182
min_radius: Minimum radius for export.
@@ -193,16 +193,28 @@ def _pad(inp, n=3):
193193
pw = plen * " "
194194
return f"{pw}{inp}.00"
195195

196+
is_3d = coordinates.shape[1] == 3
197+
if not is_3d and coordinates.shape[1] != 2:
198+
raise ValueError("Coordinates must have shape (N, 2) for 2D or (N, 3) for 3D.")
199+
196200
with tempfile.NamedTemporaryFile() as tmp_file:
197201
fname = tmp_file.name
198202
with open(fname, "w") as f:
199203
for coord, radius in zip(coordinates, radii):
200204
if radius < min_radius:
201205
continue
202-
# IMOD needs peculiar whitespace padding
203-
x = _pad(coord[2])
204-
y = _pad(shape[1] - coord[1])
205-
z = _pad(coord[0])
206+
207+
if is_3d:
208+
# (z, y, x) indexing
209+
x = _pad(coord[2])
210+
y = _pad(shape[1] - coord[1])
211+
z = _pad(coord[0])
212+
else:
213+
# (y, x) indexing, single z-plane
214+
x = _pad(coord[1])
215+
y = _pad(shape[0] - coord[0])
216+
z = _pad(1) # all points in z=0 plane
217+
206218
f.write(f"{x}{y}{z}{_pad(radius, 2)}\n")
207219

208220
cmd = [cmd, "-si", "-scat", fname, output_path]
@@ -214,6 +226,7 @@ def _pad(inp, n=3):
214226
run(cmd)
215227

216228

229+
217230
def write_segmentation_to_imod_as_points(
218231
mrc_path: str,
219232
segmentation: Union[str, np.ndarray],

0 commit comments

Comments
 (0)