Skip to content

Commit

Permalink
[PY-611] Silently ignore "empty" masks during import (#741)
Browse files Browse the repository at this point in the history
* update darwin import - removing masks that do not have a corresponding raster layer

* update importer - optimize code by using `sets`
  • Loading branch information
saurbhc committed Dec 15, 2023
1 parent 7eac38c commit a2b61f6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions darwin/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ def _import_annotations(
errors: dt.ErrorList = []
success: dt.Success = dt.Success.SUCCESS

rl: Optional[dt.Annotation] = None
rl_dense_rle_ids: Optional[Set[str]] = None
serialized_annotations = []
for annotation in annotations:
annotation_class = annotation.annotation_class
Expand Down Expand Up @@ -771,6 +773,33 @@ def _import_annotations(

data = _get_annotation_data(annotation, annotation_class_id, attributes)

# check if the mask is empty (i.e. masks that do not have a corresponding raster layer) if so, skip import of the mask
if annotation_type == "mask":
if rl is None:
rl = next(
(
a
for a in annotations
if a.annotation_class.annotation_type == "raster_layer"
),
None,
)
if rl and rl_dense_rle_ids is None:
rl_dense_rle_ids = set(rl.data["dense_rle"][::2])
if rl is not None and rl_dense_rle_ids is not None:
# check if the 'annotation_class_id' is in raster_layer's mask_annotation_ids_mapping dict
_annotation_id = annotation.id
if (
rl.data["mask_annotation_ids_mapping"][_annotation_id]
not in rl_dense_rle_ids
):
# skip import of the mask, and remove it from mask_annotation_ids_mapping
logger.warning(
f"Skipping import of mask annotation '{annotation_class.name}' as it does not have a corresponding raster layer"
)
del rl.data["mask_annotation_ids_mapping"][_annotation_id]
continue

actors: List[dt.DictFreeForm] = []
actors.extend(_handle_annotators(annotation, import_annotators))
actors.extend(_handle_reviewers(annotation, import_reviewers))
Expand Down

0 comments on commit a2b61f6

Please sign in to comment.