Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWilkie committed Sep 24, 2024
1 parent e6bf522 commit b0e8d35
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 4 deletions.
22 changes: 18 additions & 4 deletions darwin/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,8 @@ def import_annotations( # noqa: C901

if annotation_format == "darwin":
dataset.client.load_feature_flags()
# Check if the flag exists. WHen the flag is deprecated in the future we will always perform this check

# Check if the flag exists. When the flag is deprecated in the future we will always perform this check
static_instance_id_feature_flag_exists = any(
feature.name == "STATIC_INSTANCE_ID"
for feature in dataset.client.features.get(dataset.team, [])
Expand All @@ -879,7 +879,7 @@ def import_annotations( # noqa: C901
) or not static_instance_id_feature_flag_exists

if check_for_multi_instance_id_annotations:
warn_for_annotations_with_multiple_instance_ids(local_files, console)
_warn_for_annotations_with_multiple_instance_ids(local_files, console)

console.print(
f"{len(local_files) + len(local_files_missing_remotely)} annotation file(s) found.",
Expand Down Expand Up @@ -1698,9 +1698,23 @@ def _display_slot_warnings_and_errors(
console.print(f" - {warning}")


def warn_for_annotations_with_multiple_instance_ids(
def _warn_for_annotations_with_multiple_instance_ids(
local_files: List[dt.AnnotationFile], console: Console
) -> None:
"""
Warns the user if any video annotations have multiple unique instance IDs.
This function checks each video annotation in the provided list of local annotation
files for multiple instance IDs. If any are found, a warning is printed to the console.
The user is then prompted to confirm if they want to proceed with the import.
Parameters
----------
local_files : List[dt.AnnotationFile]
A list of local annotation files to be checked.
console : Console
The console object used to print warnings and messages.
"""
files_with_multi_instance_id_annotations = {}
files_with_video_annotations = [
local_file for local_file in local_files if local_file.is_video
Expand Down
127 changes: 127 additions & 0 deletions tests/darwin/importer/importer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
_resolve_annotation_classes,
_verify_slot_annotation_alignment,
_import_properties,
_warn_for_annotations_with_multiple_instance_ids,
)


Expand Down Expand Up @@ -1812,3 +1813,129 @@ def test_import_new_annotation_level_properties_with_manifest(
],
granularity=PropertyGranularity.annotation,
)


def test_no_instance_id_warning_with_no_video_annotations():
bounding_box_class = dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
)
local_files = [
dt.AnnotationFile(
path=Path("file1.json"),
is_video=False,
annotations=[],
filename="file1",
annotation_classes={bounding_box_class},
),
dt.AnnotationFile(
path=Path("file2.json"),
is_video=False,
annotations=[],
filename="file2",
annotation_classes={bounding_box_class},
),
]
console = MagicMock()
_warn_for_annotations_with_multiple_instance_ids(local_files, console)
console.print.assert_not_called()


def test_warning_with_multiple_files_with_multi_instance_id_annotations(monkeypatch):
bounding_box_class = dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
)
annotation1 = dt.VideoAnnotation(
annotation_class=bounding_box_class,
frames={
0: dt.Annotation(
annotation_class=dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
),
data={"x": 5, "y": 10, "w": 5, "h": 10},
subs=[
dt.SubAnnotation(annotation_type="instance_id", data="1"),
],
),
1: dt.Annotation(
annotation_class=dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
),
data={"x": 15, "y": 20, "w": 15, "h": 20},
subs=[
dt.SubAnnotation(annotation_type="instance_id", data="2"),
],
),
2: dt.Annotation(
annotation_class=dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
),
data={"x": 25, "y": 30, "w": 25, "h": 30},
subs=[
dt.SubAnnotation(annotation_type="instance_id", data="3"),
],
),
},
keyframes={0: True, 1: False, 2: True},
segments=[[0, 2]],
interpolated=False,
)
annotation2 = dt.VideoAnnotation(
annotation_class=bounding_box_class,
frames={
0: dt.Annotation(
annotation_class=dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
),
data={"x": 5, "y": 10, "w": 5, "h": 10},
subs=[
dt.SubAnnotation(annotation_type="instance_id", data="1"),
],
),
1: dt.Annotation(
annotation_class=dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
),
data={"x": 15, "y": 20, "w": 15, "h": 20},
subs=[
dt.SubAnnotation(annotation_type="instance_id", data="2"),
],
),
2: dt.Annotation(
annotation_class=dt.AnnotationClass(
name="class1", annotation_type="bounding_box"
),
data={"x": 25, "y": 30, "w": 25, "h": 30},
subs=[
dt.SubAnnotation(annotation_type="instance_id", data="3"),
],
),
},
keyframes={0: True, 1: False, 2: True},
segments=[[0, 2]],
interpolated=False,
)
local_files = [
dt.AnnotationFile(
path=Path("file1.json"),
is_video=True,
annotations=[annotation1],
filename="file1",
annotation_classes={bounding_box_class},
),
dt.AnnotationFile(
path=Path("file2.json"),
is_video=True,
annotations=[annotation2],
filename="file2",
annotation_classes={bounding_box_class},
),
]
console = MagicMock()
# Simulate the user stopping the export at the warning
monkeypatch.setattr("builtins.input", lambda _: "n")
with pytest.raises(SystemExit):
_warn_for_annotations_with_multiple_instance_ids(local_files, console)
console.print.assert_called()
assert (
console.print.call_count == 3
) # One for the warning message, two for the file details

0 comments on commit b0e8d35

Please sign in to comment.