-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubjects.py
194 lines (166 loc) · 6.55 KB
/
Subjects.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
194
import matplotlib.pyplot as mplplot
import matplotlib.transforms as mpltrans
import numpy as np
from Annotations import AnnotationBase, Annotations
class Subject():
def __init__(self,
id=None,
annotations=None,
trueLabel=None,
difficulty=None):
self._id = id
self._annotations = annotations if annotations is not None else Annotations(
[])
self._difficulty = difficulty
self._trueLabel = trueLabel
def __eq__(self, other):
return self.id == other.id
def __hash__(self):
return hash(self.id)
@property
def id(self):
return self._id
@id.setter
def id(self, id):
self._id = id
@property
def annotations(self):
return self._annotations
@annotations.setter
def annotations(self, annotations):
# TODO: make compatible with passing a readymade Annotations instance
self._annotations = Annotations([
annotation for annotation in annotations
if issubclass(annotation, AnnotationBase)
])
@property
def difficulty(self):
return self._difficulty
@difficulty.setter
def difficulty(self, difficulty):
self._difficulty = difficulty
@property
def trueLabel(self):
return self._trueLabel
@trueLabel.setter
def trueLabel(self, trueLabel):
self._trueLabel = trueLabel
def computeTrueLabel(self, annotationModel, annotationPriorModel):
validLabels = self.annotations.getUniqueLabels()
# Predict subject label
labelMlEstimates = []
for trueLabel in validLabels:
if len(self.annotations.annotations) > 0:
dataProb = np.product([
annotationModel(trueLabel, annotation)
for annotation in self.annotations.items()
])
else:
dataProb = 1.0
labelMlEstimates.append(annotationPriorModel(trueLabel) * dataProb)
self._trueLabel = validLabels[np.argmax(labelMlEstimates)]
def __str__(self):
return '\n'.join(['-==Subject==-'] + [
'{} => {}'.format(name[1:], value)
for name, value in vars(self).items()
] + ['-==Subject==-'])
class Subjects():
def __init__(self, subjects=[]):
self._subjects = [
subject for subject in subjects if isinstance(subject, Subject)
]
@property
def subjects(self):
return self._subjects
@subjects.setter
def subjects(self, classifiers):
self._subjects = [
subject for subject in subjects if isinstance(subject, Subject)
]
def items(self):
for subject in self.subjects:
yield subject
def append(self, subject):
if isinstance(subject, Subject):
self.subjects.append(subject)
else:
raise TypeError(
'The subject argument must an instance of type {}. Type {} passed.'.
format(Subject, type(subject)))
def merge(self, subjects):
for subject in subjects.items():
for knownSubject in self.items():
if knownSubject.id == subject.id:
print(True)
knownSubject.annotations.append(subject.annotations)
break
else:
print(False)
self.append(subject)
def subsetCriterion(self, subject, id, trueLabel):
# Returns True by default if id and trueLabel are None
if id is not None and subject.id != id or trueLabel is not None and trueLabel != trueLabel:
return False
return True
def subset(self, id=None, trueLabel=None):
return Subjects([
subject for subject in self.subjects
if isinstance(subject, Subject)
and self.subsetCriterion(subject, id, trueLabel)
])
@property
def annotations(self):
return Annotations([
annotation for subject in self.items()
for annotation in subject.annotations.items()
])
def plotAnnotations(self, plotAxes=None):
annotations = self.annotations
# print('All annotations:\n', annotations)
# print([annotation.label for annotation in annotations.items()])
# print('Unique annotations: {}'.format(np.unique([annotation.label for annotation in annotations.items()])))
annotationTypes = annotations.getUniqueLabelTypes()
if len(annotationTypes) != 1:
raise RuntimeError(
'Annotations appear to be of different types ({}). Cannot plot!'.
format(', '.join([
annotationType.labelTypeName
for annotationType in annotationTypes
])))
annotationType = annotationTypes.pop()
numAnnotationLabels = annotationType.getNumLabels(annotations)
if np.isfinite(numAnnotationLabels):
if annotationType.labelTypeName == 'Categorical':
print('Not implemented for type: {}'.format(
type(annotationType)))
else:
# integer or boolean
contents, bins, _ = mplplot.hist(
[
float(annotation.label)
for annotation in annotations.items()
],
bins=numAnnotationLabels,
density=False)
bincentres = 0.5 * (bins[:-1] + bins[1:])
numAnnotations = float(len(annotations.annotations))
for content, bincentre in zip(contents / numAnnotations,
bincentres):
plotAxes = mplplot.gca() if plotAxes is None else plotAxes
annotateTrans = mpltrans.blended_transform_factory(
plotAxes.transData, plotAxes.transAxes)
mplplot.annotate(
'{:.2f}'.format(content),
xy=(bincentre, 0.5),
ha='center',
va='center',
color='w' if content > 0.45 else 'k',
xycoords=annotateTrans)
plotAxes.set_xlabel('Annotation Value', fontsize='x-large')
plotAxes.set_ylabel(
'Number of Annotations', fontsize='x-large')
else:
#Real valued
print('Not implemented')
def __str__(self):
return '\n'.join(str(subject) for subject in self.subjects)