From 72bbff11e684c75979f830b14ad014fd9bb693e3 Mon Sep 17 00:00:00 2001 From: John Wilkie Date: Thu, 16 Jan 2025 15:36:01 +0000 Subject: [PATCH] Tests for identifying medical / MONAI slots --- darwin/importer/importer.py | 12 +++---- tests/darwin/importer/importer_test.py | 43 ++++++++++++++++++++------ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/darwin/importer/importer.py b/darwin/importer/importer.py index a1d64965e..b9d846c87 100644 --- a/darwin/importer/importer.py +++ b/darwin/importer/importer.py @@ -2487,28 +2487,26 @@ def _scale_coordinates_by_pixdims( if not remote_files_that_require_pixel_to_mm_transform: return maybe_parsed_files for file in maybe_parsed_files: - if Path(file.full_path) in remote_files_that_require_pixel_to_mm_transform: + if file.full_path in remote_files_that_require_pixel_to_mm_transform: for annotation in file.annotations: slot_name = annotation.slot_names[0] if ( slot_name not in remote_files_that_require_pixel_to_mm_transform[ - Path(file.full_path) + file.full_path ] ): continue pixdims = remote_files_that_require_pixel_to_mm_transform[ - Path(file.full_path) + file.full_path ][slot_name] if isinstance(annotation, dt.VideoAnnotation): for _, frame_annotation in annotation.frames.items(): frame_annotation.scale_coordinates( - float(pixdims["x"]), float(pixdims["y"]) + float(pixdims[0]), float(pixdims[1]) ) elif isinstance(annotation, dt.Annotation): - annotation.scale_coordinates( - float(pixdims["x"]), float(pixdims["y"]) - ) + annotation.scale_coordinates(float(pixdims[0]), float(pixdims[1])) return maybe_parsed_files diff --git a/tests/darwin/importer/importer_test.py b/tests/darwin/importer/importer_test.py index 63010492f..2e4934412 100644 --- a/tests/darwin/importer/importer_test.py +++ b/tests/darwin/importer/importer_test.py @@ -39,6 +39,8 @@ _get_remote_files_targeted_by_import, _get_remote_medical_file_transform_requirements, _scale_coordinates_by_pixdims, + slot_is_medical, + slot_is_handled_by_monai, ) from darwin.exceptions import RequestEntitySizeExceeded from darwin.datatypes import ( @@ -4240,15 +4242,15 @@ def test_scale_coordinates_by_pixdims(): ) remote_files_that_require_pixel_to_mm_transform: Dict[ - Path, Dict[str, Dict[str, float]] + str, Dict[str, List[float]] ] = { - image_annotations_path: { - "slot1": {"x": 2.0, "y": 3.0}, - "slot2": {"x": 1.5, "y": 2.5}, + str(image_annotations_path): { + "slot1": [2.0, 3.0], + "slot2": [1.5, 2.5], }, - video_annotations_path: { - "slot1": {"x": 4.0, "y": 6.0}, - "slot2": {"x": 3.0, "y": 5.0}, + str(video_annotations_path): { + "slot1": [4.0, 6.0], + "slot2": [3.0, 5.0], }, } @@ -4297,10 +4299,10 @@ def test_scale_coordinates_by_pixdims_no_scaling_needed(): ) remote_files_that_require_pixel_to_mm_transform: Dict[ - Path, Dict[str, Dict[str, float]] + str, Dict[str, List[float]] ] = { - image_annotations_path: { - "slot2": {"x": 1.0, "y": 1.0}, + str(image_annotations_path): { + "slot2": [1.0, 1.0], }, } @@ -4315,3 +4317,24 @@ def test_scale_coordinates_by_pixdims_no_scaling_needed(): "w": 30, "h": 40, } + + +def test_slot_is_medical(): + """ + Test that slot_is_medical returns True if the slot has medical metadata + """ + medical_slot = {"metadata": {"medical": {}}} + non_medical_slot = {"metadata": {}} + assert slot_is_medical(medical_slot) is True + assert slot_is_medical(non_medical_slot) is False + + +def test_slot_is_handled_by_monai(): + """ + Test that slot_is_handled_by_monai returns True if the slot has MONAI handler + + """ + monai_slot = {"metadata": {"medical": {"handler": "MONAI"}}} + non_monai_slot = {"metadata": {"medical": {}}} + assert slot_is_handled_by_monai(monai_slot) is True + assert slot_is_handled_by_monai(non_monai_slot) is False