From b88c4ef7aa96c2a824f021d6e1987addf0ea82b1 Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Fri, 22 Nov 2024 20:40:32 -0600 Subject: [PATCH 1/4] Add in tear down helper --- .../tests/data/annotation_import/test_relationships.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libs/labelbox/tests/data/annotation_import/test_relationships.py b/libs/labelbox/tests/data/annotation_import/test_relationships.py index 038767e49..1335261e5 100644 --- a/libs/labelbox/tests/data/annotation_import/test_relationships.py +++ b/libs/labelbox/tests/data/annotation_import/test_relationships.py @@ -145,6 +145,7 @@ def configured_project( rand_gen, data_row_json_by_media_type, normalized_ontology_by_media_type_relationship, + teardown_helpers, ): """Configure project for test. Request.param will contain the media type if not present will use Image MediaType. The project will have 10 data rows.""" @@ -186,6 +187,10 @@ def configured_project( project.global_keys = global_keys yield project + teardown_helpers.teardown_project_labels_ontology_feature_schemas(project) + + if dataset: + dataset.delete() @pytest.mark.parametrize( From 3d6f3a18c0a5e77c7ef36e1e5111b84b93c53ce1 Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Sat, 23 Nov 2024 05:27:23 -0600 Subject: [PATCH 2/4] optimize --- .../data/serialization/ndjson/label.py | 44 ++++++++----------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py b/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py index 9ceb5dafd..835ef9201 100644 --- a/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py +++ b/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py @@ -65,11 +65,7 @@ def from_common( cls, data: LabelCollection ) -> Generator["NDLabel", None, None]: for label in data: - if all( - isinstance(model, RelationshipAnnotation) - for model in label.annotations - ): - yield from cls._create_relationship_annotations(label) + yield from cls._create_relationship_annotations(label) yield from cls._create_non_video_annotations(label) yield from cls._create_video_annotations(label) @@ -195,24 +191,20 @@ def _create_non_video_annotations(cls, label: Label): ) def _create_relationship_annotations(cls, label: Label): - relationship_annotations = [ - annotation - for annotation in label.annotations - if isinstance(annotation, RelationshipAnnotation) - ] - for relationship_annotation in relationship_annotations: - uuid1 = uuid4() - uuid2 = uuid4() - source = copy.copy(relationship_annotation.value.source) - target = copy.copy(relationship_annotation.value.target) - if not isinstance(source, ObjectAnnotation) or not isinstance( - target, ObjectAnnotation - ): - raise TypeError( - f"Unable to create relationship with non ObjectAnnotations. `Source: {type(source)} Target: {type(target)}`" - ) - if not source._uuid: - source._uuid = uuid1 - if not target._uuid: - target._uuid = uuid2 - yield relationship_annotation + for annotation in label.annotations: + if isinstance(annotation, RelationshipAnnotation): + uuid1 = uuid4() + uuid2 = uuid4() + source = copy.copy(annotation.value.source) + target = copy.copy(annotation.value.target) + if not isinstance(source, ObjectAnnotation) or not isinstance( + target, ObjectAnnotation + ): + raise TypeError( + f"Unable to create relationship with non ObjectAnnotations. `Source: {type(source)} Target: {type(target)}`" + ) + if not source._uuid: + source._uuid = uuid1 + if not target._uuid: + target._uuid = uuid2 + yield annotation From 327d13aeeb7a488486c6a55087099b59523ffd01 Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Sat, 23 Nov 2024 07:36:33 -0600 Subject: [PATCH 3/4] set it up as class method --- libs/labelbox/src/labelbox/data/serialization/ndjson/label.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py b/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py index 835ef9201..1507766a7 100644 --- a/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py +++ b/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py @@ -190,6 +190,7 @@ def _create_non_video_annotations(cls, label: Label): f"Unable to convert object to MAL format. `{type(getattr(annotation, 'value',annotation))}`" ) + @classmethod def _create_relationship_annotations(cls, label: Label): for annotation in label.annotations: if isinstance(annotation, RelationshipAnnotation): From 6025cf05ce923fb649a6bdb84944f255f256bcf4 Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:57:17 -0600 Subject: [PATCH 4/4] fixed slightly --- libs/labelbox/src/labelbox/data/serialization/ndjson/label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py b/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py index 1507766a7..e822f3c42 100644 --- a/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py +++ b/libs/labelbox/src/labelbox/data/serialization/ndjson/label.py @@ -208,4 +208,4 @@ def _create_relationship_annotations(cls, label: Label): source._uuid = uuid1 if not target._uuid: target._uuid = uuid2 - yield annotation + yield NDRelationship.from_common(annotation, label.data)