Skip to content

Commit d570c5f

Browse files
authored
Make skeleton key optional on KeypointsAnnotation (#404)
1 parent c47753b commit d570c5f

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

nucleus/annotation.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ class KeypointsAnnotation(Annotation):
482482
label (str): The label for this annotation.
483483
keypoints (List[:class:`Keypoint`]): The list of keypoints objects.
484484
names (List[str]): A list that corresponds to the names of each keypoint.
485-
skeleton (List[List[int]]): A list of 2-length lists indicating a beginning and ending
485+
skeleton (Optional[List[List[int]]]): A list of 2-length lists indicating a beginning and ending
486486
index for each line segment in the skeleton of this keypoint label.
487487
reference_id (str): User-defined ID of the image to which to apply this
488488
annotation.
@@ -502,8 +502,8 @@ class KeypointsAnnotation(Annotation):
502502
label: str
503503
keypoints: List[Keypoint]
504504
names: List[str]
505-
skeleton: List[List[int]]
506505
reference_id: str
506+
skeleton: Optional[List[List[int]]] = None
507507
annotation_id: Optional[str] = None
508508
metadata: Optional[Dict] = None
509509
track_reference_id: Optional[str] = None
@@ -524,29 +524,37 @@ def __post_init__(self):
524524
seen.add(name)
525525

526526
max_segment_index = len(self.keypoints) - 1
527-
for segment in self.skeleton:
528-
if len(segment) != 2:
529-
raise ValueError(
530-
"The keypoints skeleton must contain a list of line segments with exactly 2 indices"
531-
)
532-
for index in segment:
533-
if index > max_segment_index:
527+
528+
if self.skeleton is not None:
529+
for segment in self.skeleton:
530+
if len(segment) != 2:
534531
raise ValueError(
535-
f"The skeleton index {index} is not a valid keypoint index"
532+
"The keypoints skeleton must contain a list of line segments with exactly 2 indices"
536533
)
534+
for index in segment:
535+
if index > max_segment_index:
536+
raise ValueError(
537+
f"The skeleton index {index} is not a valid keypoint index"
538+
)
537539
if self.annotation_id is None:
538540
self.annotation_id = f"{self.label}-{self.reference_id}-keypoints"
539541

540542
@classmethod
541543
def from_json(cls, payload: dict):
542544
geometry = payload.get(GEOMETRY_KEY, {})
545+
skeleton = (
546+
geometry[KEYPOINTS_SKELETON_KEY]
547+
if KEYPOINTS_SKELETON_KEY in geometry
548+
else None
549+
)
550+
543551
return cls(
544552
label=payload.get(LABEL_KEY, 0),
545553
keypoints=[
546554
Keypoint.from_json(_) for _ in geometry.get(KEYPOINTS_KEY, [])
547555
],
548556
names=geometry[KEYPOINTS_NAMES_KEY],
549-
skeleton=geometry[KEYPOINTS_SKELETON_KEY],
557+
skeleton=skeleton,
550558
reference_id=payload[REFERENCE_ID_KEY],
551559
annotation_id=payload.get(ANNOTATION_ID_KEY, None),
552560
metadata=payload.get(METADATA_KEY, {}),

0 commit comments

Comments
 (0)