-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnotations.py
193 lines (152 loc) · 5.15 KB
/
Annotations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
from Classifiers import Classifier
from Labels import *
class AnnotationBase():
def __init__(self, labelType=RealValuedLabelType):
self._labelType = labelType
@property
def labelType(self):
return self._labelType
class AnnotationBinary(AnnotationBase):
def __init__(self,
id,
classifier=None,
zooniverseAnnotations=None,
taskName=None,
trueValue=None,
falseValue=None,
**kwargs):
super().__init__(BoolValuedLabelType)
if not isinstance(classifier, Classifier):
raise TypeError(
'The classifier argument must be of type {}. Type {} passed.'.
format(type(Classifier), type(classifier)))
self._id = id
self._classifier = classifier
self._zooniverseAnnotations = zooniverseAnnotations
self._taskName = taskName
self._trueValue = trueValue
self._falseValue = falseValue
self._label = self.extractLabel()
@property
def id(self):
return self._id
@property
def label(self):
return self._label
@label.setter
def label(self, label):
self._label = label
@property
def trueValue(self):
return self._trueValue
@trueValue.setter
def trueValue(self, trueValue):
self._trueValue = trueValue
@property
def falseValue(self):
return self._falseValue
@falseValue.setter
def falseValue(self, falseValue):
self._falseValue = falseValue
@property
def classifier(self):
return self._classifier
@classifier.setter
def classifier(self, classifier):
if not isinstance(classifier, Classifier):
raise TypeError(
'The classifier argument must be of type {}. Type {} passed.'.
format(type(Classifier), type(classifier)))
self._classifier = classifier
@property
def taskName(self):
return self._taskName
@taskName.setter
def taskName(self, taskName):
self._taskName = taskName
@property
def zooniverseAnnotations(self):
return self._zooniverseAnnotations
@zooniverseAnnotations.setter
def zooniverseAnnotations(self, zooniverseAnnotations):
self._zooniverseAnnotations = zooniverseAnnotations
def extractLabel(self):
if self.taskName in self.zooniverseAnnotations:
# print(self.zooniverseAnnotations)
annotationValue = self.zooniverseAnnotations[self.taskName][0][
'value']
if self.trueValue is not None and annotationValue == self.trueValue:
return True
if self.trueValue is not None and annotationValue == self.falseValue:
return False
return None
def __str__(self):
return '\n'.join(['-~~AnnotationBinary~~-'] + [
'{} => {}'.format(name[1:], value)
for name, value in vars(self).items()
] + ['-~~AnnotationBinary~~-'])
class AnnotationKeyPoint(AnnotationBase):
def __init__(self, x=None, y=None, label=None, classifier=None):
self._x = x
self._y = y
self._label = label
self._classifier = classifier
@property
def x(self):
return self._x
@x.setter
def x(self, x):
self._x = x
@property
def y(self):
return self._y
@y.setter
def y(self, y):
self._y = y
@property
def label(self):
return self._label
@label.setter
def label(self, label):
self._label = label
@property
def classifier(self):
return self._classifier
@classifier.setter
def classifier(self, classifier):
self._classifier = classifier
class Annotations():
def __init__(self, annotations=[]):
self._annotations = [
annotation for annotation in annotations
if issubclass(type(annotation), AnnotationBase)
]
@property
def annotations(self):
return self._annotations
@annotations.setter
def annotations(self, annotations):
self._annotations = [
annotation for annotation in annotations
if issubclass(type(annotation), AnnotationBase)
]
def items(self):
for annotation in self.annotations:
yield annotation
def append(self, annotations):
if issubclass(type(annotations), AnnotationBase):
self.annotations.append(annotations)
elif isinstance(annotations, Annotations):
self.annotations.extend(annotations.annotations)
else:
raise TypeError(
'The annotation argument must a subclass of type {}. Type {} passed.'.
format(type(AnnotationBase), type(annotation)))
def getUniqueLabels(self):
return np.unique([annotation.label for annotation in self.annotations])
def getUniqueLabelTypes(self):
return set([annotation.labelType for annotation in self.annotations])
# np.unique(
def __str__(self):
return '\n'.join(str(annotation) for annotation in self.annotations)