|
8 | 8 | import scipy.io as sio
|
9 | 9 |
|
10 | 10 | from .mask_utils import bounding_box, get_inst_centroid, get_inst_types
|
11 |
| -from .vectorize import inst2gdf, sem2gdf |
12 | 11 |
|
13 | 12 | try:
|
14 | 13 | import tables as tb
|
@@ -328,132 +327,6 @@ def gdf_to_file(
|
328 | 327 | elif suffix == ".geojson":
|
329 | 328 | gdf.to_file(path.with_suffix(".geojson"), driver="GeoJSON")
|
330 | 329 |
|
331 |
| - @staticmethod |
332 |
| - def to_gson( |
333 |
| - masks: Dict[str, np.ndarray], |
334 |
| - path: Union[str, Path], |
335 |
| - coords: Tuple[int, int, int, int] = None, |
336 |
| - compute_centroids: bool = False, |
337 |
| - compute_bboxes: bool = False, |
338 |
| - class_dict_inst: Dict[int, str] = None, |
339 |
| - class_dict_sem: Dict[int, str] = None, |
340 |
| - class_dict_cyto: Dict[int, str] = None, |
341 |
| - use_subfolders: bool = True, |
342 |
| - silence_warnings: bool = True, |
343 |
| - ) -> None: |
344 |
| - """Write a geojson/feather/parquet files from a dict of model output masks. |
345 |
| -
|
346 |
| - Note: |
347 |
| - Additonal computed arrays are saved if the corresponding flags are set: |
348 |
| - - centroid: The centroids of the instance masks. Shape (N, 2). |
349 |
| - - bbox: The bounding boxes of the instance masks. Shape (N, 4). |
350 |
| -
|
351 |
| - Note: |
352 |
| - Each specific mask type is either embedded to the end of the filename or |
353 |
| - saved to it's corresponding subfolder if `use_subfolders` is set to True: |
354 |
| - - inst: instance masks |
355 |
| - - sem: semantic masks |
356 |
| - - cyto: cytoplasm masks |
357 |
| -
|
358 |
| - Parameters: |
359 |
| - masks (Dict[str, np.ndarray]): |
360 |
| - The masks to be saved. E.g. {"inst": np.ndarray, "type": np.ndarray}. |
361 |
| - path (str or Path): |
362 |
| - The output filename. One of .geojson, .feather, .parquet. |
363 |
| - coords (Tuple[int, int, int, int], default=None): |
364 |
| - The XYWH-coordinates of the image patch. (x0, y0, width, height). |
365 |
| - compute_centroids (bool, default=False): |
366 |
| - Compute the centroids of the instance masks. |
367 |
| - compute_bboxes (bool, default=False): |
368 |
| - Compute the bounding boxes of the instance masks. |
369 |
| - class_dict_inst (Dict[int, str], default=None): |
370 |
| - A dictionary mapping class indices to class names. |
371 |
| - E.g. {1: 'neoplastic', 2: 'immune'}. |
372 |
| - class_dict_sem (Dict[int, str], default=None): |
373 |
| - A dictionary mapping class indices to class names. |
374 |
| - E.g. {1: 'tumor', 2: 'stroma'}. |
375 |
| - class_dict_cyto (Dict[int, str], default=None): |
376 |
| - A dictionary mapping class indices to class names. |
377 |
| - E.g. {1: 'neoplastic_cyto', 2: 'connective_cyto'}. |
378 |
| - use_subfolders (bool, default=True): |
379 |
| - If True, saves the masks to their respective subfolders. I.e. subfolders |
380 |
| - called 'inst', 'sem', 'cyto'. If False, the mask type is embedded in the |
381 |
| - end of the filename. |
382 |
| - silence_warnings (bool, default=True): |
383 |
| - If True, warnings are silenced. |
384 |
| - """ |
385 |
| - path = Path(path) |
386 |
| - |
387 |
| - xoff = coords[0] if coords is not None else None |
388 |
| - yoff = coords[1] if coords is not None else None |
389 |
| - |
390 |
| - if masks.get("inst", None) is not None: |
391 |
| - if use_subfolders: |
392 |
| - inst_subdir = path.parent / "inst" |
393 |
| - inst_subdir.mkdir(parents=True, exist_ok=True) |
394 |
| - inst_path = inst_subdir / path.name |
395 |
| - else: |
396 |
| - inst_path = path.parent / f"{path.stem}_inst{path.suffix}" |
397 |
| - |
398 |
| - if masks.get("type", None) is not None: |
399 |
| - inst_gdf = inst2gdf( |
400 |
| - masks["inst"], |
401 |
| - masks["type"], |
402 |
| - xoff=xoff, |
403 |
| - yoff=yoff, |
404 |
| - class_dict=class_dict_inst, |
405 |
| - ) |
406 |
| - else: |
407 |
| - inst_gdf = inst2gdf(masks["inst"], xoff=xoff, yoff=yoff) |
408 |
| - |
409 |
| - if compute_centroids: |
410 |
| - inst_gdf["centroid"] = inst_gdf["geometry"].centroid |
411 |
| - if compute_bboxes: |
412 |
| - inst_gdf["bbox"] = inst_gdf["geometry"].apply(lambda x: x.bounds) |
413 |
| - |
414 |
| - FileHandler.gdf_to_file(inst_gdf, inst_path, silence_warnings) |
415 |
| - |
416 |
| - if masks.get("sem", None) is not None: |
417 |
| - if use_subfolders: |
418 |
| - sem_subdir = path.parent / "sem" |
419 |
| - sem_subdir.mkdir(parents=True, exist_ok=True) |
420 |
| - sem_path = sem_subdir / path.name |
421 |
| - else: |
422 |
| - sem_path = path.parent / f"{path.stem}_sem{path.suffix}" |
423 |
| - |
424 |
| - sem_gdf = sem2gdf( |
425 |
| - masks["sem"], |
426 |
| - xoff=xoff, |
427 |
| - yoff=yoff, |
428 |
| - class_dict=class_dict_sem, |
429 |
| - ) |
430 |
| - FileHandler.gdf_to_file(sem_gdf, sem_path, silence_warnings) |
431 |
| - |
432 |
| - if masks.get("cyto", None) is not None: |
433 |
| - if use_subfolders: |
434 |
| - cyto_subdir = path.parent / "cyto" |
435 |
| - cyto_subdir.mkdir(parents=True, exist_ok=True) |
436 |
| - cyto_path = cyto_subdir / path.name |
437 |
| - else: |
438 |
| - cyto_path = path.parent / f"{path.stem}_cyto{path.suffix}" |
439 |
| - |
440 |
| - if masks.get("cyto_type", None) is not None: |
441 |
| - cyto_gdf = inst2gdf( |
442 |
| - masks["cyto"], |
443 |
| - masks["cyto_type"], |
444 |
| - xoff=xoff, |
445 |
| - yoff=yoff, |
446 |
| - class_dict=class_dict_cyto, |
447 |
| - ) |
448 |
| - else: |
449 |
| - cyto_gdf = inst2gdf( |
450 |
| - masks["cyto"], |
451 |
| - xoff=xoff, |
452 |
| - yoff=yoff, |
453 |
| - class_dict=class_dict_cyto, |
454 |
| - ) |
455 |
| - FileHandler.gdf_to_file(cyto_gdf, cyto_path, silence_warnings) |
456 |
| - |
457 | 330 | @staticmethod
|
458 | 331 | def to_mat(
|
459 | 332 | masks: Dict[str, np.ndarray],
|
|
0 commit comments