Skip to content

Commit

Permalink
Merge labels into one when found duplicates
Browse files Browse the repository at this point in the history
Instead of raising an error. Two disconnected labels is not always an
error.
Still raises a warning.
  • Loading branch information
phcerdan committed Dec 19, 2023
1 parent bbb8d58 commit c64b8a9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mdai_utils/download_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,18 @@ def main(args):
label_file = labels_parent_folder / f"{label_name}__{hash_id}.nrrd"
# Check we are not introducing two labels:
if label_name in pair_data_entry:
raise ValueError(
f"Found two labels with the same name {label_name} in the same slice {hash_id}. Please fix it in md.ai."
old_label_file = Path(pair_data_entry[label_name])
old_label_image = itk.imread(old_label_file)
old_label_image_np = itk.array_from_image(old_label_image)
# Merge both np arrays: old_label_image_np and slice_label_mask
merged_label_image_np = np.maximum(old_label_image_np, slice_label_mask)
# This is not always an error, there could be two unconected masks in the same slice.
logger.warning(
f"Found labels with the same name {label_name} in the same slice {hash_id}. Merging into one."
)
# overwrite the label_image with the merged one
label_image = itk.image_from_array(merged_label_image_np)

pair_data_entry[label_name] = str(label_file.resolve())

if not no_fixing_metadata:
Expand Down

0 comments on commit c64b8a9

Please sign in to comment.