Skip to content

Commit 2904cb2

Browse files
committed
flesh out Segmentation Class
1 parent 04fa2cb commit 2904cb2

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

nucleus/annotation.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,31 @@
1414
LABEL_KEY,
1515
TYPE_KEY,
1616
VERTICES_KEY,
17+
ITEM_ID_KEY,
18+
MASK_URL_KEY,
19+
INDEX_KEY,
20+
ANNOTATIONS_KEY,
1721
)
1822

23+
1924
class Segment:
20-
def __init__(self, label: str, index: int, metadata: Optional[dict] = None):
25+
def __init__(
26+
self, label: str, index: int, metadata: Optional[dict] = None
27+
):
2128
self.label = label
2229
self.index = index
2330
self.metadata = metadata
2431

2532
def __str__(self):
26-
return self.to_payload()
33+
return str(self.to_payload())
2734

2835
@classmethod
2936
def from_json(cls, payload: dict):
30-
return cls(payload.get(LABEL_KEY), payload.get(INDEX_KEY), payload.get(METADATA_KEY, None))
37+
return cls(
38+
label=payload.get(LABEL_KEY),
39+
index=payload.get(INDEX_KEY),
40+
metadata=payload.get(METADATA_KEY, None),
41+
)
3142

3243
def to_payload(self) -> dict:
3344
payload = {
@@ -38,8 +49,15 @@ def to_payload(self) -> dict:
3849
payload[METADATA_KEY] = self.metadata
3950
return payload
4051

52+
4153
class SegmentationAnnotation:
42-
def __init__(self, mask_url: str, annotations: List[Segment], reference_id: Optional[str] = None, item_id: Optional[str] = None):
54+
def __init__(
55+
self,
56+
mask_url: str,
57+
annotations: List[Segment],
58+
reference_id: Optional[str] = None,
59+
item_id: Optional[str] = None,
60+
):
4361
if bool(reference_id) == bool(item_id):
4462
raise Exception(
4563
"You must specify either a reference_id or an item_id for an annotation."
@@ -49,17 +67,28 @@ def __init__(self, mask_url: str, annotations: List[Segment], reference_id: Opti
4967
self.reference_id = reference_id
5068
self.item_id = item_id
5169

52-
5370
def __str__(self):
71+
return str(self.to_payload())
5472

5573
@classmethod
5674
def from_json(cls, payload: dict):
75+
return cls(
76+
mask_url=payload[MASK_URL_KEY],
77+
annotations=payload[ANNOTATIONS_KEY],
78+
reference_id=payload.get(REFERENCE_ID_KEY, None),
79+
item_id=payload.get(ITEM_ID_KEY, None),
80+
)
5781

5882
def to_payload(self) -> dict:
5983
payload = {
6084
MASK_URL_KEY: self.mask_url,
6185
ANNOTATIONS_KEY: [ann.to_payload for ann in self.annotations],
6286
}
87+
if self.reference_id:
88+
payload[REFERENCE_ID_KEY] = self.reference_id
89+
else:
90+
payload[ITEM_ID_KEY] = self.item_id
91+
return payload
6392

6493

6594
class AnnotationTypes(Enum):

nucleus/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
MODEL_RUN_ID_KEY = "model_run_id"
2222
MODEL_ID_KEY = "model_id"
2323
DATASET_ITEM_ID_KEY = "dataset_item_id"
24+
ITEM_ID_KEY = "item_id"
2425
DATASET_ITEM_IDS_KEY = "dataset_item_ids"
2526
SLICE_ID_KEY = "slice_id"
2627
DATASET_NAME_KEY = "name"

0 commit comments

Comments
 (0)