Skip to content

Commit e879b79

Browse files
committed
Update comment
1 parent ac91670 commit e879b79

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

libs/labelbox/src/labelbox/data/serialization/ndjson/label.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class NDLabel(BaseModel):
6262
annotations: AnnotationType
6363

6464
@classmethod
65-
def from_common(cls, data: LabelCollection) -> Generator["NDLabel", None, None]:
65+
def from_common(
66+
cls, data: LabelCollection
67+
) -> Generator["NDLabel", None, None]:
6668
for label in data:
6769
yield from cls._create_relationship_annotations(label)
6870
yield from cls._create_non_video_annotations(label)
@@ -126,12 +128,16 @@ def _create_video_annotations(
126128
if isinstance(
127129
annot, (VideoClassificationAnnotation, VideoObjectAnnotation)
128130
):
129-
video_annotations[annot.feature_schema_id or annot.name].append(annot)
131+
video_annotations[annot.feature_schema_id or annot.name].append(
132+
annot
133+
)
130134
elif isinstance(annot, VideoMaskAnnotation):
131135
yield NDObject.from_common(annotation=annot, data=label.data)
132136

133137
for annotation_group in video_annotations.values():
134-
segment_frame_ranges = cls._get_segment_frame_ranges(annotation_group)
138+
segment_frame_ranges = cls._get_segment_frame_ranges(
139+
annotation_group
140+
)
135141
if isinstance(annotation_group[0], VideoClassificationAnnotation):
136142
annotation = annotation_group[0]
137143
frames_data = []
@@ -197,10 +203,12 @@ def _create_relationship_annotations(
197203
NDRelationship: Validated relationship annotations in NDJSON format
198204
199205
Raises:
200-
TypeError: If source/target types violate the validation rules:
201-
- Invalid source type for PDF target
202-
- Non-ObjectAnnotation source for non-PDF target
203-
- Non-ObjectAnnotation target
206+
TypeError: If source/target types are invalid:
207+
- Source:
208+
- For PDF target annotations (DocumentRectangle, DocumentEntity): source must be ObjectAnnotation or ClassificationAnnotation
209+
- For other target annotations: source must be ObjectAnnotation
210+
- Target:
211+
- Target must always be ObjectAnnotation
204212
"""
205213
for annotation in label.annotations:
206214
if isinstance(annotation, RelationshipAnnotation):
@@ -210,7 +218,9 @@ def _create_relationship_annotations(
210218
target = copy.copy(annotation.value.target)
211219

212220
# Check if source type is valid based on target type
213-
if isinstance(target.value, (DocumentRectangle, DocumentEntity)):
221+
if isinstance(
222+
target.value, (DocumentRectangle, DocumentEntity)
223+
):
214224
if not isinstance(
215225
source, (ObjectAnnotation, ClassificationAnnotation)
216226
):

libs/labelbox/tests/data/annotation_import/test_relationships.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def configured_project(
179179
data_row_data = []
180180

181181
for _ in range(3):
182-
data_row_data.append(data_row_json_by_media_type[media_type](rand_gen(str)))
182+
data_row_data.append(
183+
data_row_json_by_media_type[media_type](rand_gen(str))
184+
)
183185

184186
task = dataset.create_data_rows(data_row_data)
185187
task.wait_till_done()
@@ -254,7 +256,9 @@ def create_pdf_annotation(target_type: str) -> ObjectAnnotation:
254256
)
255257
raise ValueError(f"Unknown target type: {target_type}")
256258

257-
def verify_relationship(source: ClassificationAnnotation, target: ObjectAnnotation):
259+
def verify_relationship(
260+
source: ClassificationAnnotation, target: ObjectAnnotation
261+
):
258262
relationship = RelationshipAnnotation(
259263
name="relationship",
260264
value=Relationship(
@@ -263,12 +267,16 @@ def verify_relationship(source: ClassificationAnnotation, target: ObjectAnnotati
263267
type=Relationship.Type.UNIDIRECTIONAL,
264268
),
265269
)
266-
label = Label(data={"global_key": "global_key"}, annotations=[relationship])
270+
label = Label(
271+
data={"global_key": "global_key"}, annotations=[relationship]
272+
)
267273
result = list(NDJsonConverter.serialize([label]))
268274
assert len(result) == 1
269275

270276
# Test case 1: Text Classification -> DocumentRectangle
271-
text_source = ClassificationAnnotation(name="text", value=Text(answer="test"))
277+
text_source = ClassificationAnnotation(
278+
name="text", value=Text(answer="test")
279+
)
272280
verify_relationship(text_source, create_pdf_annotation("bbox"))
273281

274282
# Test case 2: Text Classification -> DocumentEntity
@@ -284,7 +292,9 @@ def verify_relationship(source: ClassificationAnnotation, target: ObjectAnnotati
284292
ClassificationAnnotation(
285293
name="second_sub_radio_question",
286294
value=Radio(
287-
answer=ClassificationAnswer(name="second_sub_radio_answer")
295+
answer=ClassificationAnswer(
296+
name="second_sub_radio_answer"
297+
)
288298
),
289299
)
290300
],
@@ -323,5 +333,7 @@ def test_classification_relationship_restrictions():
323333
TypeError,
324334
match="Unable to create relationship with non ObjectAnnotation source: .*",
325335
):
326-
label = Label(data={"global_key": "test_key"}, annotations=[relationship])
336+
label = Label(
337+
data={"global_key": "test_key"}, annotations=[relationship]
338+
)
327339
list(NDJsonConverter.serialize([label]))

0 commit comments

Comments
 (0)