-
Notifications
You must be signed in to change notification settings - Fork 14
/
classifier.py
283 lines (243 loc) · 8.72 KB
/
classifier.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
Copyright 2018 Novartis Institutes for BioMedical Research Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import _thread
import joblib
from io import BytesIO
from sklearn.utils import all_estimators
estimators = all_estimators()
from server.utils import (
unpredictability,
prediction_proba_change,
convergence,
divergence,
)
def test_classifier(Classifier):
return hasattr(Classifier, "fit") and hasattr(Classifier, "predict_proba")
available_sklearn_classifiers = {}
for name, Classifier in all_estimators():
if test_classifier(Classifier):
available_sklearn_classifiers[name] = Classifier
def get_classifier(classifier_name):
if classifier_name in available_sklearn_classifiers:
return available_sklearn_classifiers[classifier_name]
return None
def done(instance, prefix: str, callback: callable = None):
def wrapped():
setattr(instance, "{}ed".format(prefix), True)
setattr(instance, "{}ing".format(prefix), False)
if callback is not None:
callback()
return wrapped
def train_threading(fit, train_X, train_y, done):
fit(train_X, train_y)
done()
def evaluate_threading(
fn,
X,
X_train,
prev_classifier,
prev_train,
prev_prev_classifier,
prev_prev_train,
done,
):
fn(X, X_train, prev_classifier, prev_train, prev_prev_train, prev_prev_classifier)
done()
class Classifier:
def __init__(
self,
classifier_class: str,
classifier_params: dict,
search_id: int,
classifier_id: int,
**kwargs,
):
self.search_id = search_id
self.classifier_id = classifier_id
if isinstance(classifier_class, str):
if get_classifier(classifier_class) is not None:
classifier_class_ = get_classifier(classifier_class)
else:
raise ValueError(
f"Unknown or unsupported classifier: {classifier_class}"
)
else:
if test_classifier(classifier_class):
classifier_class_ = classifier_class
else:
raise ValueError(
"Custom classifier needs to support fit and predict_proba"
)
try:
self.model = classifier_class_(**classifier_params)
except TypeError as exception:
raise TypeError(
"Holy smokes! Did you specify incorrect parameters for the classifier?"
) from exception
try:
self.unpredictability_all = kwargs["unpredictability_all"]
except KeyError:
self.unpredictability_all = None
try:
self.unpredictability_labels = kwargs["unpredictability_labels"]
except KeyError:
self.unpredictability_labels = None
try:
self.prediction_proba_change_all = kwargs["prediction_proba_change_all"]
except KeyError:
self.prediction_proba_change_all = None
try:
self.prediction_proba_change_labels = kwargs[
"prediction_proba_change_labels"
]
except KeyError:
self.prediction_proba_change_labels = None
try:
self.convergence_all = kwargs["convergence_all"]
except KeyError:
self.convergence_all = None
try:
self.convergence_labels = kwargs["convergence_labels"]
except KeyError:
self.convergence_labels = None
try:
self.divergence_all = kwargs["divergence_all"]
except KeyError:
self.divergence_all = None
try:
self.divergence_labels = kwargs["divergence_labels"]
except KeyError:
self.divergence_labels = None
self.is_trained = False
self.is_training = False
self.is_evaluated = (
self.unpredictability_all is not None
and self.unpredictability_labels is not None
)
self.is_evaluating = False
self.serialized_classifications = (
kwargs["serialized_classifications"]
if "serialized_classifications" in kwargs
else b""
)
def predict(self, X):
if not self.is_trained:
return None, None
fit_y = self.model.predict(X)
p_y = self.model.predict_proba(X)
return fit_y, p_y
def train(
self, train_X, train_y, n_estimators: int = 100, callback: callable = None
):
self.is_trained = False
self.is_training = True
try:
_thread.start_new_thread(
train_threading,
(self.model.fit, train_X, train_y, done(self, "is_train", callback)),
)
except Exception:
self.is_trained = False
self.is_training = False
def evaluate(
self,
X,
train,
prev_classifier=None,
prev_train=None,
prev_prev_classifier=None,
prev_prev_train=None,
):
p_y_all = self.model.predict_proba(X)[:, 1]
p_y_labels = self.model.predict_proba(train)[:, 1]
self.unpredictability_all = unpredictability(p_y_all)
self.unpredictability_labels = unpredictability(p_y_labels)
if prev_classifier is not None:
prev_p_y_all = prev_classifier.model.predict_proba(X)[:, 1]
p_y_prev_labels = self.model.predict_proba(prev_train)[:, 1]
prev_p_y_labels = prev_classifier.model.predict_proba(prev_train)[:, 1]
self.prediction_proba_change_all = prediction_proba_change(
p_y_all, prev_p_y_all
)
self.prediction_proba_change_labels = prediction_proba_change(
p_y_prev_labels, prev_p_y_labels
)
if prev_prev_classifier is not None:
prev_prev_p_y_all = prev_prev_classifier.model.predict_proba(X)[:, 1]
p_y_prev_prev_labels = self.model.predict_proba(prev_prev_train)[:, 1]
prev_p_y_prev_labels = prev_classifier.model.predict_proba(
prev_prev_train
)[:, 1]
prev_prev_p_y_labels = prev_prev_classifier.model.predict_proba(
prev_prev_train
)[:, 1]
self.convergence_all = convergence(
prev_prev_p_y_all, prev_p_y_all, p_y_all
)
self.convergence_labels = convergence(
prev_prev_p_y_labels, prev_p_y_prev_labels, p_y_prev_prev_labels
)
self.divergence_all = divergence(
prev_prev_p_y_all, prev_p_y_all, p_y_all
)
self.divergence_labels = divergence(
prev_prev_p_y_labels, prev_p_y_prev_labels, p_y_prev_prev_labels
)
return (
self.unpredictability_all,
self.unpredictability_labels,
self.prediction_proba_change_all,
self.prediction_proba_change_labels,
self.convergence_all,
self.convergence_labels,
self.divergence_all,
self.divergence_labels,
)
def evaluate_threading(
self,
X,
train,
prev_classifier=None,
prev_train=None,
prev_prev_classifier=None,
prev_prev_train=None,
callback: callable = None,
):
self.is_evaluated = False
self.is_evaluating = True
try:
# fn, X, X_train, prev_classifier, prev_prev_classifier, done
_thread.start_new_thread(
evaluate_threading,
(
self.evaluate,
X,
train,
prev_classifier,
prev_train,
prev_prev_classifier,
prev_prev_train,
done(self, "is_evaluat", callback),
),
)
except Exception:
self.is_evaluated = False
self.is_evaluating = False
def load(self, dumped_model):
with BytesIO(dumped_model) as b:
self.model = joblib.load(b)
self.is_trained = True
def dump(self):
with BytesIO() as b:
joblib.dump(self.model, b)
return b.getvalue()