From 11fb90b686de5c296c0ed0b5604f323b1f922ffd Mon Sep 17 00:00:00 2001 From: John Wilkie <124276291+JBWilkie@users.noreply.github.com> Date: Fri, 11 Oct 2024 11:43:18 +0100 Subject: [PATCH] [DAR-4299][External] Allow import of NifTI annotations to dataset items with >1 slot (#940) * Skip item-properties import for non-Darwin JSON formats * Allow import of NifTI annotations to dataset items with >1 slot --- darwin/importer/importer.py | 7 +++-- tests/darwin/importer/importer_test.py | 43 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/darwin/importer/importer.py b/darwin/importer/importer.py index 118cae2bc..69b015e63 100644 --- a/darwin/importer/importer.py +++ b/darwin/importer/importer.py @@ -2115,12 +2115,13 @@ def _display_slot_warnings_and_errors( Raises ------ TypeError - If there are any warnings generated and the annotation format is not Darwin JSON 2.0 + If there are any warnings generated and the annotation format is not Darwin JSON 2.0 or NifTI """ - # Warnings can only be generated by referring to slots, which is only supported by Darwin JSON + # Warnings can only be generated by referring to slots, which is only supported by the Darwin JSON & NiFTI formats # Therefore, stop imports of all other formats if there are any warnings - if (slot_errors or slot_warnings) and annotation_format != "darwin": + supported_formats = ["darwin", "nifti"] + if (slot_errors or slot_warnings) and annotation_format not in supported_formats: raise TypeError( "You are attempting to import annotations to multi-slotted or multi-channeled items using an annotation format that doesn't support them. To import annotations to multi-slotted or multi-channeled items, please use the Darwin JSON 2.0 format: https://docs.v7labs.com/reference/darwin-json" ) diff --git a/tests/darwin/importer/importer_test.py b/tests/darwin/importer/importer_test.py index ac4492ae8..8eb16cb67 100644 --- a/tests/darwin/importer/importer_test.py +++ b/tests/darwin/importer/importer_test.py @@ -2591,6 +2591,49 @@ def test_does_not_raise_error_for_darwin_format_with_warnings(): assert not slot_errors +def test_does_not_raise_error_for_nifti_format_with_warnings(): + bounding_box_class = dt.AnnotationClass( + name="class1", annotation_type="bounding_box" + ) + local_files = [ + dt.AnnotationFile( + path=Path("file1"), + remote_path="/", + filename="file1", + annotation_classes={bounding_box_class}, + annotations=[ + dt.Annotation( + annotation_class=bounding_box_class, + data={"x": 5, "y": 10, "w": 5, "h": 10}, + slot_names=[], + ), + dt.Annotation( + annotation_class=bounding_box_class, + data={"x": 15, "y": 20, "w": 15, "h": 20}, + slot_names=[], + ), + ], + ), + ] + remote_files = { + "/file1": { + "item_id": "123", + "slot_names": ["0", "1"], + "layout": {"type": "grid", "version": 1, "slots": ["0", "1"]}, + }, + } + + local_files, slot_errors, slot_warnings = _verify_slot_annotation_alignment( + local_files, + remote_files, + ) + + console = MagicMock() + _display_slot_warnings_and_errors(slot_errors, slot_warnings, "nifti", console) + + assert not slot_errors + + @patch("darwin.importer.importer._get_team_properties_annotation_lookup") @pytest.mark.parametrize("setup_data", ["section"], indirect=True) def test_import_existing_section_level_property_values_without_manifest(