-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsti_functions.py
327 lines (273 loc) · 12.3 KB
/
sti_functions.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import base64
import itertools
import json
import time
from pathlib import Path
from typing import NamedTuple
import jwt
import matplotlib.pyplot as plt
import numpy as np
import requests
from requests.models import Response
from uuid import uuid4
class STIConnectionDetails(NamedTuple):
uaa_url: str
service_url: str
client_id: str
client_secret: str
token_cache: dict = {}
def get_connection_object(config_file: Path) -> STIConnectionDetails:
f = open(config_file,)
config = json.load(f)
connection = STIConnectionDetails(
uaa_url=config["uaa"]["url"],
service_url=config["sti_service_url"],
client_id=config["uaa"]["clientid"],
client_secret=config["uaa"]["clientsecret"],
)
return connection
def get_access_token(connection: STIConnectionDetails, print_message=True):
"""
Performs OAuth authentication from provided client id/secret and caches jwt token.
If token is expired new token is obtained else returned from cache
"""
if token_cache.get("sti_access_token"):
token = token_cache["sti_access_token"]
try:
jwt_token = jwt.decode(token, options={"verify_signature": False})
except Exception as e:
jwt_token = jwt.decode(token, verify=False, algorithms="RS256")
if int(time.time()) < jwt_token["exp"]:
if print_message:
print("Returning token from cache")
return token
url = f"{connection.uaa_url}/oauth/token"
if print_message:
print(f"Getting new token from {url}")
subscription_text = connection.client_id + ":" + connection.client_secret
xsuaa_subscription_key_base64 = base64.b64encode(
subscription_text.encode("utf-8")
).decode("utf-8")
headers = {
"content-type": "application/x-www-form-urlencoded",
"Authorization": f"Basic {xsuaa_subscription_key_base64}",
}
data_payload = {"grant_type": "client_credentials"}
response = requests.post(url, data=data_payload, headers=headers)
access_token = response.json().get("access_token")
token_cache["sti_access_token"] = access_token
try:
token_expiry = jwt.decode(access_token, options={"verify_signature": False})["exp"]
except Exception as e:
token_expiry = jwt.decode(access_token, verify=False, algorithms="RS256")["exp"]
token_expiry = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(token_expiry))
if print_message:
print(f"New token expires at {token_expiry}")
return access_token
def add_headers(print_message=True):
"""
Adds content type and bearer token to the request header, if not already added.
"""
def decorate(func):
def wrapper(sti_instance, *args, **kwargs):
correlation_id = uuid4().hex
print(f"Correlation id: {correlation_id}")
if kwargs.get("headers") is None:
headers = {
"content-type": "application/json",
"X-Request-ID": correlation_id,
"Authorization": "Bearer {}".format(
get_access_token(sti_instance.connection, print_message)
),
}
kwargs["headers"] = headers
return func(sti_instance, *args, **kwargs)
return wrapper
return decorate
def parse_response(print_message=True):
"""
If return type is Response object this decorator prints the response time to console
and returns the result as json object
"""
def decorate(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, Response):
if print_message:
print("Response time: {} ms".format(result.elapsed.total_seconds() * 1000))
return result.json()
return result
return wrapper
return decorate
def plot(cm, classes, normalize=False, title="Confusion matrix", cmap=plt.cm.Blues, subplot=None):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]
# print("Normalized confusion matrix")
# else:
# print("Confusion matrix, without normalization")
if subplot:
nrows, ncols, subplot_num = subplot
plt.subplot(nrows, ncols, subplot_num)
classes = [str(l[:20]) + '...' for l in classes]
plt.imshow(cm, interpolation="nearest", cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes, rotation=45)
fmt = ".2f" if normalize else "d"
thresh = cm.max() / 2.0
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(
j,
i,
format(cm[i, j], fmt),
horizontalalignment="center",
color="red" if cm[i, j] > thresh else "black",
)
plt.tight_layout()
plt.ylabel("True label")
plt.xlabel("Predicted label")
def automation_accuracy_plot(threshold_list, automation_list, accuracy_list, category_name):
fig, ax1 = plt.subplots(figsize=(15, 10))
ax1.set_title(category_name, size=20)
color = 'tab:red'
ax1.set_xlabel('threshold', fontsize=20)
ax1.set_ylabel('automation rate', color=color, fontsize=20)
ax1.plot(threshold_list, automation_list, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('accuracy', color=color, fontsize=20) # we already handled the x-label with ax1
ax2.plot(threshold_list, accuracy_list, color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()
class STIFunctions:
"""
Helper class with functions to access STI REST endpoints in pythonic way
"""
def __init__(self, connection: STIConnectionDetails):
self.connection = connection
@add_headers(True)
@parse_response(True)
def list_models(self, headers):
url = f"{self.connection.service_url}/sti/training/model"
response = requests.get(url, headers=headers)
return response
@add_headers(True)
@parse_response(True)
def get_model_accuracy(self, model_id, headers):
url = f"{self.connection.service_url}/sti/training/model/accuracy?model_id={model_id}"
response = requests.get(url, headers=headers)
return response
@add_headers(False)
@parse_response(False)
def get_model_accuracy_with_threshold(self, model_id, threshold, headers):
url = f"{self.connection.service_url}/sti/training/model/accuracy?model_id={model_id}&threshold={threshold}"
response = requests.get(url, headers=headers)
return response
def plot_confusion_matrix(self, model_id):
accuracy = self.get_model_accuracy(model_id)
for idx, result in enumerate(accuracy["validation_results"]):
fig = plt.figure(figsize=(20, 20))
labels = accuracy["validation_results"][idx]["confusion_matrix"]["labels"]
values = accuracy["validation_results"][idx]["confusion_matrix"]["values"]
field = accuracy["validation_results"][idx]["field"]
confusion_matrix = np.array(values).T
plot(confusion_matrix, classes=labels, title=field)
def plot_confusion_matrix_for_different_threshold(self, model_id):
threshold_list = np.arange(0, 1, 0.1)
tmp = self.get_model_accuracy(model_id)
category_number = len(tmp['validation_results'])
for idx in range(category_number):
fig = plt.figure(figsize=(50, 50))
for thresh_num, thresh in enumerate(threshold_list):
accuracy = self.get_model_accuracy_with_threshold(model_id, thresh)
if thresh == 0.0:
validation_results = accuracy['validation_results']
else:
validation_results = accuracy['validation_results_at_thresholds']['validation_results']
labels = validation_results[idx]["confusion_matrix"]["labels"]
values = validation_results[idx]["confusion_matrix"]["values"]
field = validation_results[idx]["field"]
confusion_matrix = np.array(values, dtype=int).T
plot(confusion_matrix, classes=labels, title=field + '_with_threshold_' + str(round(thresh, 2)),
subplot=(5, 2, 1 + thresh_num))
def plot_automation_accuracy(self, model_id):
threshold_list = np.arange(0, 1, 0.1)
tmp = self.get_model_accuracy(model_id)
num_categories = len(tmp['validation_results'])
automation_list = [[] for _ in range(num_categories)]
accuracy_list = [[] for _ in range(num_categories)]
category_list = [item['field'] for item in tmp['validation_results']]
for thresh in threshold_list:
accuracy = self.get_model_accuracy_with_threshold(model_id, thresh)
if thresh == 0.0:
results = accuracy['validation_results']
for num, cat_result in enumerate(results):
automation_list[num].append(1)
cm = np.array(cat_result['confusion_matrix']['values'])
accuracy_list[num].append(np.sum(cm.diagonal()) / np.sum(cm))
else:
validation_results = accuracy['validation_results_at_thresholds']['validation_results']
for num, cat_result in enumerate(validation_results):
automation_list[num].append(cat_result['probability_of_exceeding_threshold'])
accuracy_list[num].append(cat_result['accuracy'])
for accuracy, automation, category in zip(accuracy_list, automation_list, category_list):
automation_accuracy_plot(threshold_list, automation, accuracy, category)
@add_headers(True)
@parse_response(True)
def get_model_status(self, model_id, headers):
url = f"{self.connection.service_url}/sti/training/model/status?model_id={model_id}"
response = requests.get(url, headers=headers)
return response
@add_headers(True)
@parse_response(True)
def start_model_training(self, model_id, headers):
url = f"{self.connection.service_url}/sti/training/model/train"
data_payload = {"model_id": "{}".format(model_id)}
response = requests.post(url, headers=headers, data=json.dumps(data_payload))
return response
@add_headers(True)
@parse_response(True)
def activate_model(self, model_id, headers):
url = f"{self.connection.service_url}/sti/training/model/activate"
data_payload = {"model_id": "{}".format(model_id)}
response = requests.put(url, headers=headers, data=json.dumps(data_payload))
return response
@add_headers(True)
@parse_response(True)
def deactivate_model(self, model_id, headers):
url = f"{self.connection.service_url}/sti/training/model/deactivate"
data_payload = {"model_id": "{}".format(model_id)}
response = requests.put(url, headers=headers, data=json.dumps(data_payload))
return response
@add_headers(True)
@parse_response(True)
def classify_text(self, data_payload, headers):
url = f"{self.connection.service_url}/sti/text/classify"
response = requests.post(url, headers=headers, data=json.dumps(data_payload))
return response
@add_headers(True)
@parse_response(True)
def recommend(self, data_payload, headers):
url = f"{self.connection.service_url}/sti/solution/recommend"
response = requests.post(url, headers=headers, data=json.dumps(data_payload))
return response
@add_headers(True)
@parse_response(True)
def delete_model(self, model_id, headers):
url = f"{self.connection.service_url}/sti/training/model?model_id={model_id}"
response = requests.delete(url, headers=headers)
return response
@add_headers(True)
@parse_response(True)
def file_upload(self, data_payload, headers):
url = f"{self.connection.service_url}/sti/training/model"
response = requests.post(url, headers=headers, data=json.dumps(data_payload))
return response