Skip to content

Commit 6ddf400

Browse files
authored
Added the ability to change UIMode on ontology creation (#1676)
1 parent 4ec55e1 commit 6ddf400

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

libs/labelbox/src/labelbox/schema/ontology.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Classification:
9898
removed in a future release. Dropdown will also
9999
no longer be able to be created in the Editor on 3/31/2022.
100100
101-
A classfication to be added to a Project's ontology. The
101+
A classification to be added to a Project's ontology. The
102102
classification is dependent on the Classification Type.
103103
104104
To instantiate, the "class_type" and "name" parameters must
@@ -125,8 +125,10 @@ class Classification:
125125
instructions: (str)
126126
required: (bool)
127127
options: (list)
128+
ui_mode: (str)
128129
schema_id: (str)
129130
feature_schema_id: (str)
131+
scope: (str)
130132
"""
131133

132134
class Type(Enum):
@@ -138,6 +140,10 @@ class Type(Enum):
138140
class Scope(Enum):
139141
GLOBAL = "global"
140142
INDEX = "index"
143+
144+
class UIMode(Enum):
145+
HOTKEY = "hotkey"
146+
SEARCHABLE = "searchable"
141147

142148
_REQUIRES_OPTIONS = {Type.CHECKLIST, Type.RADIO, Type.DROPDOWN}
143149

@@ -149,6 +155,7 @@ class Scope(Enum):
149155
schema_id: Optional[str] = None
150156
feature_schema_id: Optional[str] = None
151157
scope: Scope = None
158+
ui_mode: Optional[UIMode] = None # How this classification should be answered (e.g. hotkeys / autocomplete, etc)
152159

153160
def __post_init__(self):
154161
if self.class_type == Classification.Type.DROPDOWN:
@@ -180,6 +187,7 @@ def from_dict(cls, dictionary: Dict[str, Any]) -> Dict[str, Any]:
180187
instructions=dictionary["instructions"],
181188
required=dictionary.get("required", False),
182189
options=[Option.from_dict(o) for o in dictionary["options"]],
190+
ui_mode=cls.UIMode(dictionary["uiMode"]) if "uiMode" in dictionary else None,
183191
schema_id=dictionary.get("schemaNodeId", None),
184192
feature_schema_id=dictionary.get("featureSchemaId", None),
185193
scope=cls.Scope(dictionary.get("scope", cls.Scope.GLOBAL)))
@@ -198,6 +206,9 @@ def asdict(self, is_subclass: bool = False) -> Dict[str, Any]:
198206
"schemaNodeId": self.schema_id,
199207
"featureSchemaId": self.feature_schema_id
200208
}
209+
if (self.class_type == self.Type.RADIO or self.class_type == self.Type.CHECKLIST) and self.ui_mode:
210+
# added because this key does nothing for text so no point of including
211+
classification["uiMode"] = self.ui_mode.value
201212
if is_subclass:
202213
return classification
203214
classification[

libs/labelbox/tests/unit/test_unit_ontology.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from labelbox.exceptions import InconsistentOntologyException
44
from labelbox import Tool, Classification, Option, OntologyBuilder
5+
from itertools import product
56

67
_SAMPLE_ONTOLOGY = {
78
"tools": [{
@@ -46,6 +47,8 @@
4647
"nested classification",
4748
"type":
4849
"radio",
50+
'uiMode':
51+
"searchable",
4952
"options": [{
5053
"schemaNodeId":
5154
None,
@@ -120,6 +123,8 @@
120123
"radio",
121124
"scope":
122125
"global",
126+
'uiMode':
127+
"searchable",
123128
"options": [{
124129
"schemaNodeId": None,
125130
"featureSchemaId": None,
@@ -148,6 +153,11 @@ def test_create_classification(class_type) -> None:
148153
c = Classification(class_type=class_type, name="classification")
149154
assert (c.class_type == class_type)
150155

156+
@pytest.mark.parametrize("ui_mode_type, class_type", list(product(list(Classification.UIMode), list(Classification.Type))))
157+
def test_create_classification_with_ui_mode(ui_mode_type, class_type) -> None:
158+
c = Classification(name="classification", class_type=class_type, ui_mode=ui_mode_type)
159+
assert (c.ui_mode == ui_mode_type)
160+
151161

152162
@pytest.mark.parametrize("value, expected_value, typing",
153163
[(3, 3, int), ("string", "string", str)])

0 commit comments

Comments
 (0)