|
16 | 16 | from tqdm import tqdm
|
17 | 17 |
|
18 | 18 |
|
| 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 | + |
19 | 68 | def convert_segmentation_to_spheres(
|
20 | 69 | segmentation: np.ndarray,
|
21 | 70 | verbose: bool = False,
|
22 | 71 | num_workers: Optional[int] = None,
|
23 | 72 | resolution: Optional[Tuple[float, float, float]] = None,
|
24 | 73 | radius_factor: float = 1.0,
|
25 | 74 | estimate_radius_2d: bool = True,
|
26 |
| -): |
| 75 | +) -> Tuple[np.ndarray, np.ndarray]: |
27 | 76 | """Extract spheres parameterized by center and radius from a segmentation.
|
28 | 77 |
|
29 | 78 | Args:
|
@@ -80,7 +129,7 @@ def write_points_to_imod(
|
80 | 129 | min_radius: Union[float, int],
|
81 | 130 | output_path: str,
|
82 | 131 | color: Optional[Tuple[int, int, int]] = None,
|
83 |
| -): |
| 132 | +) -> None: |
84 | 133 | """Write point annotations to a .mod file for IMOD.
|
85 | 134 |
|
86 | 135 | Args:
|
@@ -129,7 +178,7 @@ def write_segmentation_to_imod_as_points(
|
129 | 178 | min_radius: Union[int, float],
|
130 | 179 | radius_factor: float = 1.0,
|
131 | 180 | estimate_radius_2d: bool = True,
|
132 |
| -): |
| 181 | +) -> None: |
133 | 182 | """Write segmentation results to .mod file with imod point annotations.
|
134 | 183 |
|
135 | 184 | This approximates each segmented object as a sphere.
|
@@ -183,7 +232,7 @@ def export_helper(
|
183 | 232 | output_root: str,
|
184 | 233 | export_function: callable,
|
185 | 234 | force: bool = False,
|
186 |
| -): |
| 235 | +) -> None: |
187 | 236 | """
|
188 | 237 | Helper function to run imod export for files in a directory.
|
189 | 238 |
|
|
0 commit comments