1
- import abc
2
1
from dataclasses import dataclass , field
3
- from enum import Enum , auto
2
+ from enum import Enum
4
3
import colorsys
5
4
6
- from typing import Any , Callable , Dict , List , Optional , Union
5
+ from typing import Any , Dict , List , Optional , Union
7
6
8
7
from labelbox .schema .project import Project
9
- from labelbox .orm import query
10
- from labelbox .orm .db_object import DbObject , Updateable , BulkDeletable
11
- from labelbox .orm .model import Entity , Field , Relationship
12
- from labelbox .utils import snake_case , camel_case
8
+ from labelbox .orm .db_object import DbObject
9
+ from labelbox .orm .model import Field , Relationship
13
10
from labelbox .exceptions import InconsistentOntologyException
14
11
15
12
16
13
@dataclass
17
14
class Option :
18
15
"""
19
16
An option is a possible answer within a Classification object in
20
- a Project's ontology.
17
+ a Project's ontology.
21
18
22
19
To instantiate, only the "value" parameter needs to be passed in.
23
20
@@ -41,13 +38,11 @@ def label(self):
41
38
42
39
@classmethod
43
40
def from_dict (cls , dictionary : Dict [str , Any ]):
44
- return Option (value = dictionary ["value" ],
45
- schema_id = dictionary .get ("schemaNodeId" , None ),
46
- feature_schema_id = dictionary .get ("featureSchemaId" , None ),
47
- options = [
48
- Classification .from_dict (o )
49
- for o in dictionary .get ("options" , [])
50
- ])
41
+ return cls (
42
+ value = dictionary ["value" ],
43
+ schema_id = dictionary .get ("schemaNodeId" , None ),
44
+ feature_schema_id = dictionary .get ("featureSchemaId" , None ),
45
+ options = [cls .from_dict (o ) for o in dictionary .get ("options" , [])])
51
46
52
47
def asdict (self ) -> Dict [str , Any ]:
53
48
return {
@@ -69,13 +64,13 @@ def add_option(self, option: 'Classification'):
69
64
@dataclass
70
65
class Classification :
71
66
"""
72
- A classfication to be added to a Project's ontology. The
67
+ A classfication to be added to a Project's ontology. The
73
68
classification is dependent on the Classification Type.
74
69
75
70
To instantiate, the "class_type" and "instructions" parameters must
76
71
be passed in.
77
72
78
- The "options" parameter holds a list of Option objects. This is not
73
+ The "options" parameter holds a list of Option objects. This is not
79
74
necessary for some Classification types, such as TEXT. To see which
80
75
types require options, look at the "_REQUIRES_OPTIONS" class variable.
81
76
@@ -120,16 +115,15 @@ def name(self):
120
115
121
116
@classmethod
122
117
def from_dict (cls , dictionary : Dict [str , Any ]):
123
- return Classification (
124
- class_type = Classification .Type (dictionary ["type" ]),
125
- instructions = dictionary ["instructions" ],
126
- required = dictionary .get ("required" , False ),
127
- options = [Option .from_dict (o ) for o in dictionary ["options" ]],
128
- schema_id = dictionary .get ("schemaNodeId" , None ),
129
- feature_schema_id = dictionary .get ("featureSchemaId" , None ))
118
+ return cls (class_type = cls .Type (dictionary ["type" ]),
119
+ instructions = dictionary ["instructions" ],
120
+ required = dictionary .get ("required" , False ),
121
+ options = [Option .from_dict (o ) for o in dictionary ["options" ]],
122
+ schema_id = dictionary .get ("schemaNodeId" , None ),
123
+ feature_schema_id = dictionary .get ("featureSchemaId" , None ))
130
124
131
125
def asdict (self ) -> Dict [str , Any ]:
132
- if self .class_type in Classification ._REQUIRES_OPTIONS \
126
+ if self .class_type in self ._REQUIRES_OPTIONS \
133
127
and len (self .options ) < 1 :
134
128
raise InconsistentOntologyException (
135
129
f"Classification '{ self .instructions } ' requires options." )
@@ -160,13 +154,13 @@ class Tool:
160
154
To instantiate, the "tool" and "name" parameters must
161
155
be passed in.
162
156
163
- The "classifications" parameter holds a list of Classification objects.
157
+ The "classifications" parameter holds a list of Classification objects.
164
158
This can be used to add nested classifications to a tool.
165
159
166
160
Example(s):
167
161
tool = Tool(
168
162
tool = Tool.Type.LINE,
169
- name = "Tool example")
163
+ name = "Tool example")
170
164
classification = Classification(
171
165
class_type = Classification.Type.TEXT,
172
166
instructions = "Classification Example")
@@ -200,16 +194,16 @@ class Type(Enum):
200
194
201
195
@classmethod
202
196
def from_dict (cls , dictionary : Dict [str , Any ]):
203
- return Tool (name = dictionary ['name' ],
204
- schema_id = dictionary .get ("schemaNodeId" , None ),
205
- feature_schema_id = dictionary .get ("featureSchemaId" , None ),
206
- required = dictionary .get ("required" , False ),
207
- tool = Tool .Type (dictionary ["tool" ]),
208
- classifications = [
209
- Classification .from_dict (c )
210
- for c in dictionary ["classifications" ]
211
- ],
212
- color = dictionary ["color" ])
197
+ return cls (name = dictionary ['name' ],
198
+ schema_id = dictionary .get ("schemaNodeId" , None ),
199
+ feature_schema_id = dictionary .get ("featureSchemaId" , None ),
200
+ required = dictionary .get ("required" , False ),
201
+ tool = cls .Type (dictionary ["tool" ]),
202
+ classifications = [
203
+ Classification .from_dict (c )
204
+ for c in dictionary ["classifications" ]
205
+ ],
206
+ color = dictionary ["color" ])
213
207
214
208
def asdict (self ) -> Dict [str , Any ]:
215
209
return {
@@ -287,9 +281,9 @@ class OntologyBuilder:
287
281
for making Project ontologies from scratch. OntologyBuilder can also
288
282
pull from an already existing Project's ontology.
289
283
290
- There are no required instantiation arguments.
284
+ There are no required instantiation arguments.
291
285
292
- To create an ontology, use the asdict() method after fully building your
286
+ To create an ontology, use the asdict() method after fully building your
293
287
ontology within this class, and inserting it into project.setup() as the
294
288
"labeling_frontend_options" parameter.
295
289
@@ -303,19 +297,18 @@ class OntologyBuilder:
303
297
tools: (list)
304
298
classifications: (list)
305
299
306
-
300
+
307
301
"""
308
302
tools : List [Tool ] = field (default_factory = list )
309
303
classifications : List [Classification ] = field (default_factory = list )
310
304
311
305
@classmethod
312
306
def from_dict (cls , dictionary : Dict [str , Any ]):
313
- return OntologyBuilder (
314
- tools = [Tool .from_dict (t ) for t in dictionary ["tools" ]],
315
- classifications = [
316
- Classification .from_dict (c )
317
- for c in dictionary ["classifications" ]
318
- ])
307
+ return cls (tools = [Tool .from_dict (t ) for t in dictionary ["tools" ]],
308
+ classifications = [
309
+ Classification .from_dict (c )
310
+ for c in dictionary ["classifications" ]
311
+ ])
319
312
320
313
def asdict (self ):
321
314
self ._update_colors ()
@@ -337,7 +330,7 @@ def _update_colors(self):
337
330
@classmethod
338
331
def from_project (cls , project : Project ):
339
332
ontology = project .ontology ().normalized
340
- return OntologyBuilder .from_dict (ontology )
333
+ return cls .from_dict (ontology )
341
334
342
335
def add_tool (self , tool : Tool ):
343
336
if tool .name in (t .name for t in self .tools ):
0 commit comments