Skip to content

Commit e0b3d8e

Browse files
committed
able to now get and set basic ontologies
1 parent 3ce0fc5 commit e0b3d8e

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

labelbox/schema/ontology_generator.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'''
22
TODO
3-
1. make classification pulls recursive (go beyond just the 1st layer) -> but how to make classification->option->classification->infinitely?
43
2. validate that we can pull in all sorts of project ontology classes
54
3. validate we can submit basic ones back in (fix build() method)
65
4. do the rest of the stuff below
@@ -32,11 +31,16 @@
3231
class InconsistentOntologyException(Exception):
3332
pass
3433

34+
class Classification:
35+
pass
36+
3537
@dataclass
3638
class Option:
3739
value: str
3840
schema_id: Optional[str] = None
3941
feature_schema_id: Optional[str] = None
42+
#TODO: need to look further into how to make this so that the user cannot input anything here
43+
options: Optional[Classification] = None
4044

4145
@property
4246
def label(self):
@@ -47,15 +51,22 @@ def to_dict(self) -> dict:
4751
"schemaNodeId": self.schema_id,
4852
"featureSchemaId": self.feature_schema_id,
4953
"label": self.label,
50-
"value": self.value
54+
"value": self.value,
55+
"options": [classification.to_dict() for classification in self.options]
5156
}
5257

5358
@classmethod
5459
def from_dict(cls, dictionary: dict):
60+
def has_nested_classifications(dictionary: dict):
61+
if "options" in dictionary.keys():
62+
return [Classification.from_dict(nested_class) for nested_class in dictionary["options"]]
63+
return list()
64+
5565
return Option(
5666
value = dictionary["value"],
57-
schema_id = dictionary['schemaNodeId'],
58-
feature_schema_id = dictionary['featureSchemaId']
67+
schema_id = dictionary["schemaNodeId"],
68+
feature_schema_id = dictionary["featureSchemaId"],
69+
options = has_nested_classifications(dictionary)
5970
)
6071

6172

@@ -81,13 +92,13 @@ def name(self):
8192

8293
def to_dict(self) -> dict:
8394
return {
84-
"type": self.class_type.value,
85-
"instructions": self.instructions,
86-
"name": self.name,
87-
"required": self.required,
88-
"options": [option.to_dict() for option in classification.options],
89-
"schemaNodeId": self.schema_id,
90-
"featureSchemaId": self.feature_schema_id
95+
"type": self.class_type.value,
96+
"instructions": self.instructions,
97+
"name": self.name,
98+
"required": self.required,
99+
"options": [option.to_dict() for option in self.options],
100+
"schemaNodeId": self.schema_id,
101+
"featureSchemaId": self.feature_schema_id
91102
}
92103

93104
@classmethod
@@ -162,22 +173,22 @@ def from_project(cls, project: Project):
162173
return_ontology = Ontology()
163174

164175
for tool in ontology["tools"]:
165-
return_ontology.add_tool(tool)
176+
return_ontology.tools.append(Tool.from_dict(tool))
166177

167178
for classification in ontology["classifications"]:
168-
return_ontology.add_classification(classification)
179+
return_ontology.classifications.append(Classification.from_dict(classification))
169180

170181
return return_ontology
171182

172-
def add_tool(self, tool: dict) -> Tool:
173-
new_tool = Tool.from_dict(tool)
183+
def add_tool(self, *args, **kwargs) -> Tool:
184+
new_tool = Tool(*args, **kwargs)
174185
if new_tool.name in (tool.name for tool in self.tools):
175186
raise InconsistentOntologyException(f"Duplicate tool name '{new_tool.name}'. ")
176187
self.tools.append(new_tool)
177188
return new_tool
178189

179-
def add_classification(self, classification: dict) -> Classification:
180-
new_classification = Classification.from_dict(classification)
190+
def add_classification(self, *args, **kwargs) -> Classification:
191+
new_classification = Classification(*args, **kwargs)
181192
if new_classification.instructions in (classification.instructions for classification in self.classifications):
182193
raise InconsistentOntologyException(f"Duplicate classifications instructions '{new_classification.instructions}'. ")
183194
self.classifications.append(new_classification)
@@ -194,7 +205,7 @@ def build(self):
194205
"name": tool.name,
195206
"required": tool.required,
196207
"color": tool.color,
197-
"classifications": tool.classifications,
208+
"classifications": [classification.to_dict() for classification in tool.classifications],
198209
"schemaNodeId": tool.schema_id,
199210
"featureSchemaId": tool.feature_schema_id
200211

@@ -213,23 +224,24 @@ def build(self):
213224

214225
return {"tools": all_tools, "classifications": all_classifications}
215226

216-
217-
#made this just to test in my own project. not keeping this
227+
'''
228+
EVERYTHING BELOW THIS LINE IS JUST FOR SELF TESTING
229+
'''
218230
def run():
219231
frontend = list(client.get_labeling_frontends(where=LabelingFrontend.name == "Editor"))[0]
220232
project.setup(frontend, o.build())
221233
return project
222-
#also filler right here for now for testing
234+
223235
def print_stuff():
224236
tools = o.tools
225237
classifications = o.classifications
226238

227239
print("tools\n")
228240
for tool in tools:
229-
print(tool)
241+
print("\n",tool)
230242
print("\nclassifications\n")
231243
for classification in classifications:
232-
print(classification)
244+
print("\n",classification)
233245

234246
if __name__ == "__main__":
235247
import json
@@ -239,19 +251,20 @@ def print_stuff():
239251
project = client.get_project("ckhchkye62xn30796uui5lu34")
240252

241253
o = Ontology().from_project(project)
242-
print_stuff()
254+
# print_stuff()
243255

244256

245-
# o.add_tool(tool=Tool.Type.POLYGON, name="i am a polygon tool2")
257+
o.add_tool(tool=Tool.Type.POLYGON, name="I AM HERE FOR TESTING")
246258
# checklist = o.add_classification(class_type=Classification.Type.CHECKLIST, instructions="I AM A CHECKLIST2")
247259
# checklist.add_option(value="checklist answer 1")
248260
# checklist.add_option(value="checklist answer 2")
249261

250262

263+
# print_stuff()
251264
# print(o.build())
252265
# print(type(o.build()))
253266
# print(o.build())
254-
# run()
267+
run()
255268

256269

257270

0 commit comments

Comments
 (0)