Skip to content

Commit 0c15027

Browse files
authored
Merge pull request #516 from Labelbox/jt/al-1886
[AL-1886] Inclusion of Scope param to Classification
2 parents c017394 + 610286d commit 0c15027

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

labelbox/schema/ontology.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class Type(Enum):
124124
RADIO = "radio"
125125
DROPDOWN = "dropdown"
126126

127+
class Scope(Enum):
128+
GLOBAL = "global"
129+
INDEX = "index"
130+
127131
_REQUIRES_OPTIONS = {Type.CHECKLIST, Type.RADIO, Type.DROPDOWN}
128132

129133
class_type: Type
@@ -132,6 +136,7 @@ class Type(Enum):
132136
options: List[Option] = field(default_factory=list)
133137
schema_id: Optional[str] = None
134138
feature_schema_id: Optional[str] = None
139+
scope: Scope = None
135140

136141
def __post_init__(self):
137142
if self.class_type == Classification.Type.DROPDOWN:
@@ -151,21 +156,31 @@ def from_dict(cls, dictionary: Dict[str, Any]) -> Dict[str, Any]:
151156
required=dictionary.get("required", False),
152157
options=[Option.from_dict(o) for o in dictionary["options"]],
153158
schema_id=dictionary.get("schemaNodeId", None),
154-
feature_schema_id=dictionary.get("featureSchemaId", None))
159+
feature_schema_id=dictionary.get("featureSchemaId", None),
160+
scope=cls.Scope(dictionary.get("scope", cls.Scope.GLOBAL)))
155161

156162
def asdict(self) -> Dict[str, Any]:
157163
if self.class_type in self._REQUIRES_OPTIONS \
158164
and len(self.options) < 1:
159165
raise InconsistentOntologyException(
160166
f"Classification '{self.instructions}' requires options.")
161167
return {
162-
"type": self.class_type.value,
163-
"instructions": self.instructions,
164-
"name": self.name,
165-
"required": self.required,
168+
"type":
169+
self.class_type.value,
170+
"instructions":
171+
self.instructions,
172+
"name":
173+
self.name,
174+
"required":
175+
self.required,
166176
"options": [o.asdict() for o in self.options],
167-
"schemaNodeId": self.schema_id,
168-
"featureSchemaId": self.feature_schema_id
177+
"schemaNodeId":
178+
self.schema_id,
179+
"featureSchemaId":
180+
self.feature_schema_id,
181+
"scope":
182+
self.scope.value
183+
if self.scope is not None else self.Scope.GLOBAL.value
169184
}
170185

171186
def add_option(self, option: Option) -> None:

tests/unit/test_unit_ontology.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
"nested classification",
4747
"type":
4848
"radio",
49+
"scope":
50+
"global",
4951
"options": [{
5052
"schemaNodeId":
5153
None,
@@ -62,6 +64,7 @@
6264
"instructions": "nested nested text",
6365
"name": "nested nested text",
6466
"type": "text",
67+
"scope": "global",
6568
"options": []
6669
}]
6770
}, {
@@ -78,6 +81,7 @@
7881
"instructions": "nested text",
7982
"name": "nested text",
8083
"type": "text",
84+
"scope": "global",
8185
"options": []
8286
}]
8387
}, {
@@ -118,6 +122,8 @@
118122
"This is a question.",
119123
"type":
120124
"radio",
125+
"scope":
126+
"global",
121127
"options": [{
122128
"schemaNodeId": None,
123129
"featureSchemaId": None,
@@ -244,5 +250,6 @@ def test_option_add_option() -> None:
244250

245251

246252
def test_ontology_asdict() -> None:
253+
print(OntologyBuilder.from_dict(_SAMPLE_ONTOLOGY))
247254
assert OntologyBuilder.from_dict(
248255
_SAMPLE_ONTOLOGY).asdict() == _SAMPLE_ONTOLOGY

0 commit comments

Comments
 (0)