@@ -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 )
0 commit comments