Skip to content

Commit fc45105

Browse files
authored
[PLT-71] Relationship imports (#1429)
1 parent c5c549b commit fc45105

File tree

3 files changed

+81
-30
lines changed

3 files changed

+81
-30
lines changed

labelbox/data/serialization/ndjson/converter.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,37 @@ def serialize(
7474
]] = []
7575
# First pass to get all RelatiohnshipAnnotaitons
7676
# and update the UUIDs of the source and target annotations
77-
for relationship_annotation in (
78-
annotation for annotation in label.annotations
79-
if isinstance(annotation, RelationshipAnnotation)):
80-
if relationship_annotation in uuid_safe_annotations:
81-
relationship_annotation = copy.deepcopy(
82-
relationship_annotation)
83-
new_source_uuid = uuid.uuid4()
84-
new_target_uuid = uuid.uuid4()
85-
relationship_uuids[relationship_annotation.value.source.
86-
_uuid].append(new_source_uuid)
87-
relationship_uuids[relationship_annotation.value.target.
88-
_uuid].append(new_target_uuid)
89-
relationship_annotation.value.source._uuid = new_source_uuid
90-
relationship_annotation.value.target._uuid = new_target_uuid
91-
if relationship_annotation._uuid in used_uuids:
92-
relationship_annotation._uuid = uuid.uuid4()
93-
used_uuids.add(relationship_annotation._uuid)
94-
uuid_safe_annotations.append(relationship_annotation)
77+
for annotation in label.annotations:
78+
if isinstance(annotation, RelationshipAnnotation):
79+
annotation = copy.deepcopy(annotation)
80+
new_source_uuid = uuid.uuid4()
81+
new_target_uuid = uuid.uuid4()
82+
relationship_uuids[annotation.value.source._uuid].append(
83+
new_source_uuid)
84+
relationship_uuids[annotation.value.target._uuid].append(
85+
new_target_uuid)
86+
annotation.value.source._uuid = new_source_uuid
87+
annotation.value.target._uuid = new_target_uuid
88+
if annotation._uuid in used_uuids:
89+
annotation._uuid = uuid.uuid4()
90+
used_uuids.add(annotation._uuid)
91+
uuid_safe_annotations.append(annotation)
9592
# Second pass to update UUIDs for annotations referenced by RelationshipAnnotations
9693
for annotation in label.annotations:
97-
if not isinstance(annotation, RelationshipAnnotation):
98-
if hasattr(annotation, "_uuid"):
99-
if annotation in uuid_safe_annotations:
100-
annotation = copy.deepcopy(annotation)
101-
next_uuids = relationship_uuids[annotation._uuid]
102-
if len(next_uuids) > 0:
103-
annotation._uuid = next_uuids.popleft()
94+
if (not isinstance(annotation, RelationshipAnnotation) and
95+
hasattr(annotation, "_uuid")):
96+
annotation = copy.deepcopy(annotation)
97+
next_uuids = relationship_uuids[annotation._uuid]
98+
if len(next_uuids) > 0:
99+
annotation._uuid = next_uuids.popleft()
104100

105-
if annotation._uuid in used_uuids:
106-
annotation._uuid = uuid.uuid4()
107-
used_uuids.add(annotation._uuid)
101+
if annotation._uuid in used_uuids:
102+
annotation._uuid = uuid.uuid4()
103+
used_uuids.add(annotation._uuid)
108104
uuid_safe_annotations.append(annotation)
105+
else:
106+
if not isinstance(annotation, RelationshipAnnotation):
107+
uuid_safe_annotations.append(annotation)
109108
label.annotations = uuid_safe_annotations
110109
for example in NDLabel.from_common([label]):
111110
annotation_uuid = getattr(example, "uuid", None)

tests/data/assets/ndjson/relationship_import.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,45 @@
3838
"target": "d8813907-b15d-4374-bbe6-b9877fb42ccd",
3939
"type": "unidirectional"
4040
}
41+
},
42+
{
43+
"uuid": "d8813907-b15d-4374-bbe6-b9877fb42ccd",
44+
"dataRow": {
45+
"id": "clf98gj90000qp38ka34yhptl-DIFFERENT"
46+
},
47+
"name": "cat",
48+
"classifications": [],
49+
"bbox": {
50+
"top": 200.0,
51+
"left": 100.0,
52+
"height": 100.0,
53+
"width": 100.0
54+
}
55+
},
56+
{
57+
"uuid": "9b1e1249-36b4-4665-b60a-9060e0d18660",
58+
"dataRow": {
59+
"id": "clf98gj90000qp38ka34yhptl-DIFFERENT"
60+
},
61+
"name": "dog",
62+
"classifications": [],
63+
"bbox": {
64+
"top": 500.0,
65+
"left": 400.0,
66+
"height": 200.0,
67+
"width": 200.0
68+
}
69+
},
70+
{
71+
"uuid": "0e6354eb-9adb-47e5-8e52-217ed016d948",
72+
"dataRow": {
73+
"id": "clf98gj90000qp38ka34yhptl-DIFFERENT"
74+
},
75+
"name": "is chasing",
76+
"relationship": {
77+
"source": "9b1e1249-36b4-4665-b60a-9060e0d18660",
78+
"target": "d8813907-b15d-4374-bbe6-b9877fb42ccd",
79+
"type": "unidirectional"
80+
}
4181
}
4282
]

tests/data/serialization/ndjson/test_relationship.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def test_relationship():
1414
res = list(NDJsonConverter.serialize(res))
1515
assert len(res) == len(data)
1616

17-
res_relationship_annotation = [
17+
res_relationship_annotation, res_relationship_second_annotation = [
1818
annot for annot in res if "relationship" in annot
19-
][0]
19+
]
2020
res_source_and_target = [
2121
annot for annot in res if "relationship" not in annot
2222
]
@@ -29,6 +29,18 @@ def test_relationship():
2929
annot["uuid"] for annot in res_source_and_target
3030
]
3131

32+
assert res_relationship_second_annotation
33+
assert res_relationship_second_annotation["relationship"][
34+
"source"] != res_relationship_annotation["relationship"]["source"]
35+
assert res_relationship_second_annotation["relationship"][
36+
"target"] != res_relationship_annotation["relationship"]["target"]
37+
assert res_relationship_second_annotation["relationship"]["source"] in [
38+
annot["uuid"] for annot in res_source_and_target
39+
]
40+
assert res_relationship_second_annotation["relationship"]["target"] in [
41+
annot["uuid"] for annot in res_source_and_target
42+
]
43+
3244

3345
def test_relationship_nonexistent_object():
3446
with open("tests/data/assets/ndjson/relationship_import.json", "r") as file:

0 commit comments

Comments
 (0)