Skip to content

Commit fc7a3a1

Browse files
Update radius extraction for IMOD
1 parent 475a9b5 commit fc7a3a1

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

scripts/cooper/export_vesicles_to_imod.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66

77
def export_vesicles_to_imod(args):
8-
export_function = partial(write_segmentation_to_imod_as_points, min_radius=args.min_radius, radius_factor=args.increase_radius)
8+
export_function = partial(
9+
write_segmentation_to_imod_as_points, min_radius=args.min_radius, radius_factor=args.increase_radius
10+
)
911
export_helper(args.input_path, args.segmentation_path, args.output_path, export_function, force=args.force)
1012

1113

@@ -32,7 +34,7 @@ def main():
3234
help="Whether to over-write already present export results."
3335
)
3436
parser.add_argument(
35-
"--increase_radius", type=float, default=1.5,
37+
"--increase_radius", type=float, default=1.3,
3638
help="The factor to increase the radius of the exported vesicles.",
3739
)
3840
args = parser.parse_args()

synaptic_reconstruction/imod/to_imod.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def convert_segmentation_to_spheres(
2222
num_workers: Optional[int] = None,
2323
resolution: Optional[Tuple[float, float, float]] = None,
2424
radius_factor: float = 1.0,
25+
estimate_radius_2d: bool = True,
2526
):
2627
"""Extract spheres parameterized by center and radius from a segmentation.
2728
@@ -31,6 +32,9 @@ def convert_segmentation_to_spheres(
3132
num_workers: Number of workers to use for parallelization.
3233
resolution: The physical resolution of the data.
3334
radius_factor: Factor for increasing the radius to account for too small exported spheres.
35+
estimate_radius_2d: If true the distance to boundary for determining the centroid and computing
36+
the radius will be computed only in 2d rather than in 3d. This can lead to better results
37+
in case of deformation across the depth axis.
3438
3539
Returns:
3640
np.array: the center coordinates
@@ -45,10 +49,14 @@ def coords_and_rads(prop):
4549
bbox = prop.bbox
4650
bb = np.s_[bbox[0]:bbox[3], bbox[1]:bbox[4], bbox[2]:bbox[5]]
4751
mask = segmentation[bb] == seg_id
48-
seg_radii = distance_transform_edt(mask, sampling=resolution)
4952

50-
max_coord = np.unravel_index(np.argmax(seg_radii), mask.shape)
51-
radius = seg_radii[max_coord] * radius_factor
53+
if estimate_radius_2d:
54+
dists = np.array([distance_transform_edt(ma, sampling=resolution[1:]) for ma in mask])
55+
else:
56+
dists = distance_transform_edt(mask, sampling=resolution)
57+
58+
max_coord = np.unravel_index(np.argmax(dists), mask.shape)
59+
radius = dists[max_coord] * radius_factor
5260

5361
offset = np.array(bbox[:3])
5462
coord = np.array(max_coord) + offset
@@ -120,6 +128,7 @@ def write_segmentation_to_imod_as_points(
120128
output_path: str,
121129
min_radius: Union[int, float],
122130
radius_factor: float = 1.0,
131+
estimate_radius_2d: bool = True,
123132
):
124133
"""Write segmentation results to .mod file with imod point annotations.
125134
@@ -131,6 +140,9 @@ def write_segmentation_to_imod_as_points(
131140
output_path: Where to save the .mod file.
132141
min_radius: Minimum radius for export.
133142
radius_factor: Factor for increasing the radius to account for too small exported spheres.
143+
estimate_radius_2d: If true the distance to boundary for determining the centroid and computing
144+
the radius will be computed only in 2d rather than in 3d. This can lead to better results
145+
in case of deformation across the depth axis.
134146
"""
135147

136148
# Read the resolution information from the mrcfile.
@@ -142,7 +154,9 @@ def write_segmentation_to_imod_as_points(
142154

143155
# Extract the center coordinates and radii from the segmentation.
144156
segmentation = imageio.imread(segmentation_path)
145-
coordinates, radii = convert_segmentation_to_spheres(segmentation, resolution=resolution, radius_factor=radius_factor)
157+
coordinates, radii = convert_segmentation_to_spheres(
158+
segmentation, resolution=resolution, radius_factor=radius_factor, estimate_radius_2d=estimate_radius_2d
159+
)
146160

147161
# Write the point annotations to imod.
148162
write_points_to_imod(coordinates, radii, segmentation.shape, min_radius, output_path)

0 commit comments

Comments
 (0)