1
1
import shutil
2
2
import tempfile
3
3
from subprocess import run
4
- from typing import Dict , Optional
4
+ from typing import Dict , List , Optional , Tuple
5
5
6
6
import imageio .v3 as imageio
7
7
import numpy as np
@@ -144,7 +144,23 @@ def export_segmentation(
144
144
imageio .imwrite (output_path , segmentation .astype ("uint8" ), compression = "zlib" )
145
145
146
146
147
- def draw_spheres (coordinates , radii , shape , verbose = True ):
147
+ def draw_spheres (
148
+ coordinates : np .ndarray ,
149
+ radii : np .ndarray ,
150
+ shape : Tuple [int , int , int ],
151
+ verbose : bool = True ,
152
+ ) -> np .ndarray :
153
+ """Create a volumetric segmentation by painting spheres around the given coordinates.
154
+
155
+ Args:
156
+ coordinates: The center coordinates of the spheres.
157
+ radii: The radii of the spheres.
158
+ shape: The shape of the volume.
159
+ verbose: Whether to print the progress bar.
160
+
161
+ Returns:
162
+ The segmentation volume with painted spheres.
163
+ """
148
164
labels = np .zeros (shape , dtype = "uint32" )
149
165
for label_id , (coord , radius ) in tqdm (
150
166
enumerate (zip (coordinates , radii ), start = 1 ), total = len (coordinates ), disable = not verbose
@@ -166,10 +182,35 @@ def draw_spheres(coordinates, radii, shape, verbose=True):
166
182
167
183
168
184
def load_points_from_imodinfo (
169
- imod_path , full_shape , bb = None ,
170
- exclude_labels = None , exclude_label_patterns = None ,
171
- resolution = None ,
172
- ):
185
+ imod_path : str ,
186
+ full_shape : Tuple [int , int , int ],
187
+ bb : Optional [Tuple [slice , slice , slice ]] = None ,
188
+ exclude_labels : Optional [List [int ]] = None ,
189
+ exclude_label_patterns : Optional [List [str ]] = None ,
190
+ resolution : Optional [float ] = None ,
191
+ ) -> Tuple [np .ndarray , np .ndarray , np .ndarray , Dict [int , str ]]:
192
+ """Load point coordinates, radii and label information from a .mod file.
193
+
194
+ The coordinates and sizes returned will be scaled so that they are in
195
+ the voxel coordinate space if the 'resolution' parameter is passed.
196
+ If it is not passed then the radius will be returned in the physical resolution.
197
+
198
+ Args:
199
+ imod_path: The filepath to the .mod file.
200
+ full_shape: The voxel shape of the volume.
201
+ bb: Optional bounding box to limit the extracted points to.
202
+ exclude_labels: Label ids to exclude from the export.
203
+ This can be used to exclude specific labels / classes, specifying them by their id.
204
+ exclude_label_patterns: Label names to exclude from the export.
205
+ This can be used to exclude specific labels / classes, specifying them by their name.
206
+ resolution: The resolution / voxel size of the data. Will be used to scale the radii.
207
+
208
+ Returns:
209
+ The center coordinates of the sphere annotations.
210
+ The radii of the spheres.
211
+ The ids of the semantic labels.
212
+ The names of the semantic labels.
213
+ """
173
214
coordinates , sizes , labels = [], [], []
174
215
label_names = {}
175
216
@@ -274,14 +315,33 @@ def load_points_from_imodinfo(
274
315
275
316
276
317
def export_point_annotations (
277
- imod_path ,
278
- shape ,
279
- bb = None ,
280
- exclude_labels = None ,
281
- exclude_label_patterns = None ,
282
- return_coords_and_radii = False ,
283
- resolution = None ,
284
- ):
318
+ imod_path : str ,
319
+ shape : Tuple [int , int , int ],
320
+ bb : Optional [Tuple [slice , slice , slice ]] = None ,
321
+ exclude_labels : Optional [List [int ]] = None ,
322
+ exclude_label_patterns : Optional [List [str ]] = None ,
323
+ return_coords_and_radii : bool = False ,
324
+ resolution : Optional [float ] = None ,
325
+ ) -> Tuple [np .ndarray , np .ndarray , Dict [int , str ]]:
326
+ """Create a segmentation by drawing spheres corresponding to objects from a .mod file.
327
+
328
+ Args:
329
+ imod_path: The filepath to the .mod file.
330
+ shape: The voxel shape of the volume.
331
+ bb: Optional bounding box to limit the extracted points to.
332
+ exclude_labels: Label ids to exclude from the segmentation.
333
+ This can be used to exclude specific labels / classes, specifying them by their id.
334
+ exclude_label_patterns: Label names to exclude from the segmentation.
335
+ This can be used to exclude specific labels / classes, specifying them by their name.
336
+ return_coords_and_radii: Whether to also return the underlying coordinates
337
+ and radii of the exported spheres.
338
+ resolution: The resolution / voxel size of the data. Will be used to scale the radii.
339
+
340
+ Returns:
341
+ The exported segmentation.
342
+ The label ids for the instance ids in the segmentation.
343
+ The map of label ids to corresponding obejct names.
344
+ """
285
345
coordinates , radii , labels , label_names = load_points_from_imodinfo (
286
346
imod_path , shape , bb = bb ,
287
347
exclude_labels = exclude_labels ,
0 commit comments