|
| 1 | +"""Client side object for interacting with the ontology.""" |
| 2 | +import abc |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +from typing import Any, Callable, Dict, List, Optional, Union |
| 6 | + |
| 7 | +from labelbox.orm import query |
| 8 | +from labelbox.orm.db_object import DbObject, Updateable, BulkDeletable |
| 9 | +from labelbox.orm.model import Entity, Field, Relationship |
| 10 | +from labelbox.utils import snake_case, camel_case |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class OntologyEntity: |
| 15 | + required: bool |
| 16 | + name: str |
| 17 | + |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class Option: |
| 21 | + label: str |
| 22 | + value: str |
| 23 | + feature_schema_id: Optional[str] = None |
| 24 | + schema_node_id: Optional[str] = None |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def from_json(cls, json_dict): |
| 28 | + _dict = convert_keys(json_dict, snake_case) |
| 29 | + return cls(**_dict) |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class Classification(OntologyEntity): |
| 34 | + type: str |
| 35 | + instructions: str |
| 36 | + options: List[Option] |
| 37 | + feature_schema_id: Optional[str] = None |
| 38 | + schema_node_id: Optional[str] = None |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def from_json(cls, json_dict): |
| 42 | + _dict = convert_keys(json_dict, snake_case) |
| 43 | + _dict['options'] = [ |
| 44 | + Option.from_json(option) for option in _dict['options'] |
| 45 | + ] |
| 46 | + return cls(**_dict) |
| 47 | + |
| 48 | + |
| 49 | +@dataclass |
| 50 | +class Tool(OntologyEntity): |
| 51 | + tool: str |
| 52 | + color: str |
| 53 | + classifications: List[Classification] |
| 54 | + feature_schema_id: Optional[str] = None |
| 55 | + schema_node_id: Optional[str] = None |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def from_json(cls, json_dict): |
| 59 | + _dict = convert_keys(json_dict, snake_case) |
| 60 | + _dict['classifications'] = [ |
| 61 | + Classification.from_json(classification) |
| 62 | + for classification in _dict['classifications'] |
| 63 | + ] |
| 64 | + return cls(**_dict) |
| 65 | + |
| 66 | + |
| 67 | +class Ontology(DbObject): |
| 68 | + """ A ontology specifies which tools and classifications are available |
| 69 | + to a project. |
| 70 | +
|
| 71 | + NOTE: This is read only for now. |
| 72 | +
|
| 73 | + >>> project = client.get_project(name="<project_name>") |
| 74 | + >>> ontology = project.ontology() |
| 75 | + >>> ontology.normalized |
| 76 | +
|
| 77 | + """ |
| 78 | + |
| 79 | + name = Field.String("name") |
| 80 | + description = Field.String("description") |
| 81 | + updated_at = Field.DateTime("updated_at") |
| 82 | + created_at = Field.DateTime("created_at") |
| 83 | + normalized = Field.Json("normalized") |
| 84 | + object_schema_count = Field.Int("object_schema_count") |
| 85 | + classification_schema_count = Field.Int("classification_schema_count") |
| 86 | + |
| 87 | + projects = Relationship.ToMany("Project", True) |
| 88 | + created_by = Relationship.ToOne("User", False, "created_by") |
| 89 | + |
| 90 | + def __init__(self, *args, **kwargs) -> None: |
| 91 | + super().__init__(*args, **kwargs) |
| 92 | + self._tools: Optional[List[Tool]] = None |
| 93 | + self._classifications: Optional[List[Classification]] = None |
| 94 | + |
| 95 | + def tools(self) -> List[Tool]: |
| 96 | + if self._tools is None: |
| 97 | + self._tools = [ |
| 98 | + Tool.from_json(tool) for tool in self.normalized['tools'] |
| 99 | + ] |
| 100 | + return self._tools # type: ignore |
| 101 | + |
| 102 | + def classifications(self) -> List[Classification]: |
| 103 | + if self._classifications is None: |
| 104 | + self._classifications = [ |
| 105 | + Classification.from_json(classification) |
| 106 | + for classification in self.normalized['classifications'] |
| 107 | + ] |
| 108 | + return self._classifications # type: ignore |
| 109 | + |
| 110 | + |
| 111 | +def convert_keys(json_dict: Dict[str, Any], |
| 112 | + converter: Callable) -> Dict[str, Any]: |
| 113 | + if isinstance(json_dict, dict): |
| 114 | + return { |
| 115 | + converter(key): convert_keys(value, converter) |
| 116 | + for key, value in json_dict.items() |
| 117 | + } |
| 118 | + if isinstance(json_dict, list): |
| 119 | + return [convert_keys(ele, converter) for ele in json_dict] |
| 120 | + return json_dict |
0 commit comments