Skip to content

Commit

Permalink
Merge pull request #516 from Labelbox/jt/al-1886
Browse files Browse the repository at this point in the history
[AL-1886] Inclusion of Scope param to Classification
  • Loading branch information
jtsodapop authored Apr 7, 2022
2 parents c017394 + 610286d commit 0c15027
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
29 changes: 22 additions & 7 deletions labelbox/schema/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class Type(Enum):
RADIO = "radio"
DROPDOWN = "dropdown"

class Scope(Enum):
GLOBAL = "global"
INDEX = "index"

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

class_type: Type
Expand All @@ -132,6 +136,7 @@ class Type(Enum):
options: List[Option] = field(default_factory=list)
schema_id: Optional[str] = None
feature_schema_id: Optional[str] = None
scope: Scope = None

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

def asdict(self) -> Dict[str, Any]:
if self.class_type in self._REQUIRES_OPTIONS \
and len(self.options) < 1:
raise InconsistentOntologyException(
f"Classification '{self.instructions}' requires options.")
return {
"type": self.class_type.value,
"instructions": self.instructions,
"name": self.name,
"required": self.required,
"type":
self.class_type.value,
"instructions":
self.instructions,
"name":
self.name,
"required":
self.required,
"options": [o.asdict() for o in self.options],
"schemaNodeId": self.schema_id,
"featureSchemaId": self.feature_schema_id
"schemaNodeId":
self.schema_id,
"featureSchemaId":
self.feature_schema_id,
"scope":
self.scope.value
if self.scope is not None else self.Scope.GLOBAL.value
}

def add_option(self, option: Option) -> None:
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_unit_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"nested classification",
"type":
"radio",
"scope":
"global",
"options": [{
"schemaNodeId":
None,
Expand All @@ -62,6 +64,7 @@
"instructions": "nested nested text",
"name": "nested nested text",
"type": "text",
"scope": "global",
"options": []
}]
}, {
Expand All @@ -78,6 +81,7 @@
"instructions": "nested text",
"name": "nested text",
"type": "text",
"scope": "global",
"options": []
}]
}, {
Expand Down Expand Up @@ -118,6 +122,8 @@
"This is a question.",
"type":
"radio",
"scope":
"global",
"options": [{
"schemaNodeId": None,
"featureSchemaId": None,
Expand Down Expand Up @@ -244,5 +250,6 @@ def test_option_add_option() -> None:


def test_ontology_asdict() -> None:
print(OntologyBuilder.from_dict(_SAMPLE_ONTOLOGY))
assert OntologyBuilder.from_dict(
_SAMPLE_ONTOLOGY).asdict() == _SAMPLE_ONTOLOGY

0 comments on commit 0c15027

Please sign in to comment.