-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdp-hsmm.py
644 lines (460 loc) · 25.6 KB
/
hdp-hsmm.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#%%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
from ordered_set import OrderedSet
import copy
from statistics import mean
from sklearn.preprocessing import normalize
import pickle
from scipy.stats import stats
from sklearn.metrics import mean_squared_error, mean_absolute_error
from math import sqrt
from tqdm import tqdm
from statistics import stdev
from sklearn.metrics import accuracy_score, multilabel_confusion_matrix, classification_report
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from json import loads
#%%
import pyhsmm
import pyhsmm.internals
import pyhsmm.basic.distributions as distributions
#%%
class SemiMarkov():
def __init__(self):
pass
def df_col_diff(self, df, columns):
"""Places the difference betweeen to rows of dataframe in a new column named with '_diff' """
df_columns = df.columns
for col in columns:
if col in df_columns:
df[col+'_diff'] = df[col].diff()
else:
for column in df_columns:
if col in column and column+'_diff' not in df.columns:
df[column+'_diff'] = df[column].diff()
return df
def transition_matrix(transitions):
n = 1+ max(transitions) #number of states
M = [[0]*n for _ in range(n)]
for (i,j) in zip(transitions,transitions[1:]):
M[i][j] += 1
#now convert to probabilities:
for row in M:
s = sum(row)
if s > 0:
row[:] = [f/s for f in row]
return M
def save_model(self, model, kappa, iter, fig = 'save'):
with open('HSMM_Models/{}/{}_{}_{}_{}kap_{}iter.pickle'.format(device_name.split('_')[0],device_name,feature_names,model_name,kappa, iter),'wb') as outfile:
pickle.dump(model,outfile,protocol=-1)
fig = plt.figure()
plt.clf()
model.plot()
#model.plot_observations()
#model.plot_stateseq()
plt.gcf().suptitle('HDP-HSMM for {}_{}_{}_{}kap_{}iter'.format(device_name,feature_names,model_name, kappa, iter))
plt.tight_layout()
if fig == 'save':
plt.savefig('figures/{}_{}_{}_{}kap_{}iter.png'.format(device_name,feature_names,model_name, kappa, iter))
else:
plt.show()
def run_HSMM(self, data, features, extra_states = 0, model_count = 4, kappa = 0.05, progprint_xrange_var = 400):
true_labels= data['state']
states = list(OrderedSet(true_labels))
data = data.drop(data.columns[[0,-1]], axis=1)
Nmax = len(states) + extra_states
data = data.reset_index(drop=True)
#data = normalize(data[['cpu_user_time_diff','cpu_system_time_diff','cpu_idle_time_diff','memory','net_sent_diff']])
data = data[features].to_numpy()
obs_dim = len(data[0])
obs_hypparams = {'mu_0':np.zeros(obs_dim),
'sigma_0':np.eye(obs_dim),
'kappa_0': kappa,
'nu_0':obs_dim+10}
dur_hypparams = {'alpha_0':2*10,
'beta_0':2}
distributions.DurationDistribution
obs_distns = [distributions.Gaussian(**obs_hypparams) for state in range(Nmax)]
dur_distns = [distributions.PoissonDuration(**dur_hypparams) for state in range(Nmax)]
posteriormodel = pyhsmm.models.WeakLimitHDPHSMM(
alpha=6.,gamma=6., # better to sample over these; see concentration-resampling.py
init_state_concentration=6., # pretty inconsequential
obs_distns=obs_distns,
dur_distns=dur_distns)
posteriormodel.add_data(data)
models = []
for idx in pyhsmm.pyhsmm.util.text.progprint_xrange(progprint_xrange_var):
posteriormodel.resample_model()
if (idx+1) % int(progprint_xrange_var/model_count) == 0:
models.append(copy.deepcopy(posteriormodel))
model = models[-1]
return model, model.stateseqs, true_labels, states
def get_HSMM_state_seq(self, data, model_path, device_name, model_count = 4, progprint_xrange_var = 400, plot = False):
objects = []
true_labels = data['state']
with (open(model_path, "rb")) as openfile:
while True:
try:
objects.append(pickle.load(openfile))
except EOFError:
break
model = objects[0]
if plot == True:
model.plot()
plt.gcf().suptitle('HDP-HSMM for {}'.format(device_name))
plt.tight_layout()
plt.show()
return objects[0], objects[0].stateseqs, true_labels
def HSMM_pred(self, model, seed_start, seed_end, pred_window):
global df
obs, stateseq = model.predict(df[seed_start:seed_end],pred_window)
log_likelihood = model.log_likelihood(obs)
return obs, stateseq, log_likelihood
def metrics_plots(self, obs, pred_obs, features):# pred_stateseq, labels_running, labels_top_cpu, features):
"""
plt.plot(real_stateseq[0], color = 'red', label = 'states')
plt.plot(pred_stateseq, color = 'blue', label = 'predicted')
plt.legend(loc='best')
plt.grid()
plt.show()
"""
obs_dict = {feature:[] for feature in features}
pred_obs_dict = {feature:[] for feature in features}
results_dict = {}
for pred_ob in pred_obs:
for feature in features:
pred_obs_dict[feature].append(pred_ob[features.index(feature)])
for ob in obs:
for feature in features:
obs_dict[feature].append(ob[features.index(feature)])
for feature in features:
results_dict[feature+'_observations'] = obs_dict[feature]
results_dict[feature+'_predicted_observations'] = pred_obs_dict[feature]
results_dict[feature+'_rmse'] = sqrt(mean_squared_error(obs_dict[feature],pred_obs_dict[feature]))
results_dict[feature+'_mae'] = mean_absolute_error(obs_dict[feature],pred_obs_dict[feature])
"""state_sequences
plt.plot(data[feature][test_start_idx:], color = 'red', label = 'obs')
plt.plot(pred_obs_dict[feature][test_start_idx:], color = 'blue', label = 'predicted')
plt.title(feature)
plt.legend(loc='best')
plt.grid()
plt.show()
"""
return results_dict
def merge_datasets(self, save, df_path, dataset_list):
df = pd.concat(dataset_list)
if save == True:
df.to_csv(df_path)
return df
def merge_dataset(self, device_name,dataset_name_list, new_name):
prep_data_list = list()
num_list = ''
for dataset_name in dataset_name_list:
prep_data_list.append(pd.read_csv('data/{}/{}_res_usage_data_{}.csv'.format(device_name.split('_')[0],device_name,dataset_name), index_col = 'time_stamp'))
return self.merge_datasets(True, 'data/{}/{}_res_usage_data_{}.csv'.format(device_name.split('_')[0],device_name, new_name), prep_data_list)
def preprocess_data(self, device_name, freq, data_name):
"""Preprocessing labeled data"""
#Read Data, create difference feature, and clean nans
labeled_data = pd.read_csv(f"data/{device_name.split('_')[0]}/{device_name}_{freq}MHz_res_usage_data_{data_name}.csv", index_col = 'time_stamp')
labeled_data = SM.df_col_diff(labeled_data, diff_columns)
labeled_data = labeled_data.fillna(0)
#Move label column to the end
labeled_data_cols = labeled_data.columns.tolist()
oldindex = labeled_data_cols.index('state')
labeled_data_cols.insert(len(labeled_data_cols), labeled_data_cols.pop(oldindex))
labeled_data = labeled_data[labeled_data_cols]
# remove first row due to diff = 0
labeled_data = labeled_data.iloc[1: , :]
# remove rows with tranisition saving data states
labeled_data = labeled_data[labeled_data['state'] != 'transition']
labeled_data = labeled_data[labeled_data['state'] != 'saving data']
# For quick test
#labeled_data = labeled_data[:2000] #for testing only
# train/test split
labeled_data_train = labeled_data[:int(0.7*len(labeled_data))]
labeled_data_test = labeled_data[int(0.7*len(labeled_data)):]
return labeled_data_train, labeled_data_test
def check_duplicate_label(self, dict):
for key1, val1 in dict.items():
for key2, val2 in dict.items():
if key1 != key2:
if val2 == val1:
print('Duplicate labels detected, not saving model')
return False
else:
pass
return True
def grid_search(self, extras, kappas, iters, save='save'):
max_accuracy = 0
best_extra, best_kappa, best_iter = 0, 0, 0
for extra in extras:
for kap in kappas:
for iter in iters:
print(f'Training for {extra} extra states, kappa = {kap}, and iters = {iter}')
temp_model, temp_statesseqs, temp_true_labels, temp_states = self.run_HSMM(labeled_data_train, features, extra_states = extra, kappa = kap, progprint_xrange_var =iter)
labeled_data_train['predicted'] = temp_statesseqs[0]
Labels = {}
Accuracies = []
for name, _ in labeled_data_train.groupby('state'):
print(name)
label = labeled_data_train.groupby('state').get_group(name)['predicted'].value_counts(normalize=True)
print(label)
Labels[name] = label.idxmax()
Accuracies.append(label.max())
avg_acc = mean(Accuracies)
print(Labels)
print(f'Accuracies: {Accuracies}')
print(f'Average Accuracy = {avg_acc}')
if SM.check_duplicate_label(Labels):
if avg_acc > max_accuracy:
max_accuracy = avg_acc
best_model, best_state_sequences, best_true_labels, best_states = temp_model, temp_statesseqs, temp_true_labels, temp_states
best_extra, best_kappa, best_iter = extra, kap, iter
print(f'Model saved for {best_extra} extra states, kappa = {best_kappa}, and iters = {best_iter}')
self.save_model(best_model, best_kappa, best_iter, save)
return best_model, best_state_sequences, best_true_labels, best_states
def plot_states(self, colors):
indexes_dict = labeled_data_test.groupby('state').indices
for key in indexes_dict:
new_list = []
prev_ind = indexes_dict[key][0]
new_list.append(prev_ind)
for inds in indexes_dict[key][1:]:
if inds - prev_ind > 1 : # plot backgroud color for new state
new_list.append(prev_ind)
plt.axvspan(new_list[0],new_list[1], facecolor=colors[key])
new_list = []
new_list.append(inds)
if inds == indexes_dict[key][-1]: # plot backgroud color for las state
new_list.append(inds)
plt.axvspan(new_list[0],new_list[1], facecolor=colors[key])
prev_ind = inds
def plot_accuracy_likelihood(self, prediction_window, rolling_window):
print(f"Steps {prediction_window} - MA Window {rolling_window}")
scaler = MinMaxScaler()
accuracy_scaled = scaler.fit_transform(labeled_data_test[f'accuracy - {prediction_window} step'].rolling(rolling_window).mean().values.reshape(-1, 1))
log_likelihood_scaled = scaler.fit_transform(labeled_data_test[f'log_likelihood - {prediction_window} step'].values.reshape(-1, 1))
plt.figure(figsize=(12, 8), dpi=80)
plt.plot(accuracy_scaled, color='black', label='Accuracy')
plt.plot(log_likelihood_scaled, color='b', label='Log Likelihood')
colors = {'game': 'salmon', 'augmented_reality': 'lightblue', 'idle': 'lightgreen', 'mining': 'peachpuff', 'stream': 'whitesmoke'}
print(colors)
self.plot_states(colors)
plt.legend(loc='best')
plt.grid()
plt.show()
def plot_confusion_matrix(self, confusion_matrix, axes, class_label, class_names, fontsize=14):
df_cm = pd.DataFrame(confusion_matrix, index=class_names, columns=class_names)
try:
heatmap = sns.heatmap(df_cm, annot=True, fmt="d", cbar=False, ax=axes)
except ValueError:
raise ValueError("Confusion matrix values must be integers.")
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize)
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize)
axes.set_ylabel('True label')
axes.set_xlabel('Predicted label')
axes.set_title(class_label)
def classification_report(self, confusion_matrix_list, labels):
fig, ax = plt.subplots(1, 5, figsize=(12, 3))
for axes, cfs_matrix, label in zip(ax.flatten(), np.average(confusion_matrix_list, axis=0), labels):
self.plot_confusion_matrix(np.round(cfs_matrix).astype(int), axes, label, ["T", "F"])
print(label)
cfs_matrix = list(cfs_matrix)
recall = cfs_matrix[0][0] / sum(cfs_matrix[0])
spcificity = cfs_matrix[1][1] / sum(cfs_matrix[1])
precision = cfs_matrix[0][0] / (cfs_matrix[0][0] + cfs_matrix[1][0])
print('Recall', round(recall*100,2))
print('Specificity', round(spcificity*100,2))
print('Precision', round(precision*100,2))
F1 = round(2 * (precision * recall) / (precision + recall),2)
print('F1', F1)
fig.tight_layout()
plt.show()
#%%
"""Designate model generation dataset"""
SM = SemiMarkov()
features = ['cpu_user_time_diff','cpu_system_time_diff','cpu_idle_time_diff','memory']#,'net_sent_diff']
diff_columns = ['cpu_user_time', 'cpu_system_time','cpu_idle_time', 'net_sent', 'net_recv', 'io_counters_read_count_', 'io_counters_write_count_', 'io_counters_read_bytes_', 'io_counters_write_bytes_','io_counters_read_chars_', 'io_counters_write_chars_', 'cpu_times_user_','cpu_times_system_', 'cpu_times_children_user_', 'cpu_times_children_system_']
device_name = 'RPi4B8GB' #RPi4B8GB, RPi4B4GB, RPi4B2GB2, RPi4B2GB1
freq = 1800 # 1800, 1500, 1500, 1200
feature_names = 'cpu-all_mem'
progprint = 400
#model_count = 4
model_index = 3
model_name = 'rvp_random_48hr'
lookback = 300
#%%
"""Pre-process Data"""
data_name = model_name
prediction_windows = [1,2,5,10,15,30,60]
labeled_data_train, labeled_data_test = SM.preprocess_data(device_name, freq, data_name)
#%%
"""Generates HSMM Model"""
kappa_1 = 0.05
model, state_sequences, true_labels, states = SM.run_HSMM(labeled_data_train, features,extra_states = 1, kappa = kappa_1, progprint_xrange_var = progprint)
#SM.save_model(model,'save')
#%%
"""Generates HSMM Model using Grid Search"""
model, state_sequences, true_labels, states = SM.grid_search([2], [0.1,0.1,0.1], [800], "don't save fig") # number of extra states, kappa, number of sampling iterations
#%%
"""Reads previously generated model and extracts it"""
model_path = 'HSMM_Models/{}/{}_{}_{}_{}kap_{}iter.pickle'.format(device_name.split('_')[0],device_name, feature_names, model_name,0.1,800)
model, state_sequences, true_labels = SM.get_HSMM_state_seq(labeled_data_train, model_path, device_name)
#%%
"""Place Modeled Hidden-States"""
labeled_data_train['predicted'] = state_sequences[0]
#%%
"""Training Accuracy"""
Labels = {}
Accuracies = []
for name,group in labeled_data_train.groupby('state'):
print(name)
label = labeled_data_train.groupby('state').get_group(name)['predicted'].value_counts(normalize=True)
print(label)
Labels[name] = label.idxmax()
Accuracies.append(label.max())
#%%
"""Create multi-step labels"""
labeled_data_train['label'] = labeled_data_train['state'].map(Labels)
labeled_data_test['label'] = labeled_data_test['state'].map(Labels)
#create rolling window for prediction evaluations
for pw in prediction_windows:
labeled_data_test[f'label - {pw} step'] = [list(map(int,window.to_list())) for window in labeled_data_test['label'].rolling(window=pw)]
labeled_data_test[f'label - {pw} step'] = labeled_data_test[f'label - {pw} step'].shift(1-pw)
#%%
"""Save pre-processed Dataset"""
test_name = data_name
labeled_data_train.to_csv(f"data/{device_name.split('_')[0]}/{device_name}_{freq}MHz_res_usage_data_train_pred_{test_name}.csv")
#%%
"""Prepares test data, predictions start after lookback period"""
prediction_start = lookback
test_labels = labeled_data_test['state'].values
df = labeled_data_test[features]
#%%
"""Generates Label Predictions"""
prediction_windows = [1,2,5,10,15,30,60]
for prediction_window in prediction_windows:
observs_list, pred_observs_list = [], []
predicted_observations_list, observations_list = [], []
predicted_stateseq_list = []
test_labels_window_list = []
log_likelihoods_list = []
save_dict = {}
rmse_dict = {feature+'_rmse':[] for feature in features}
mae_dict = {feature+'_mae':[] for feature in features}
observations_dict = {feature+'_observations':[] for feature in features}
pred_observations_dict = {feature+'_predicted_observations':[] for feature in features}
rmse_dict_stat, mae_dict_stat = {}, {}
print(f"{prediction_window}-step prediction")
for i in tqdm(range(prediction_start,len(labeled_data_test)+1)):
seed_start_idx = i - lookback
seed_end_idx = seed_start_idx + lookback
if seed_end_idx > len(labeled_data_test)-prediction_window:
break
predicted_observations, predicted_stateseq, log_likelihood = SM.HSMM_pred(model,seed_start_idx, seed_end_idx, prediction_window)
predicted_observations_list.append(predicted_observations[lookback:].tolist())
predicted_stateseq_list.append(predicted_stateseq[lookback:].tolist())
log_likelihoods_list.append(round(log_likelihood,2))
observations_list.append(df[i:i+prediction_window].values)
test_labels_window_list.append(test_labels[i:i+prediction_window])
# store results
predicted_stateseq_list = ['lookback']*lookback + predicted_stateseq_list
log_likelihoods_list = ['lookback']*lookback + log_likelihoods_list
if len(labeled_data_test) != len(predicted_stateseq_list):
if prediction_window != 1:
labeled_data_test = labeled_data_test[:-(prediction_window-1)]
labeled_data_test[f'predicted states - {prediction_window} step'] = predicted_stateseq_list
labeled_data_test[f'log_likelihood - {prediction_window} step'] = log_likelihoods_list
"""For Observation Prediction"""
idx = 0
for obs in observations_list:
obs_dict = SM.metrics_plots(obs, predicted_observations_list[idx], features) #predicted_stateseq, test_labels_window_list[idx], features)
idx += 1
for feature in features:
rmse_dict[feature+'_rmse'].append(obs_dict[feature+'_rmse'])
mae_dict[feature+'_mae'].append(obs_dict[feature+'_mae'])
observations_dict[feature+'_observations'].append(obs_dict[feature+'_observations'])
pred_observations_dict[feature+'_predicted_observations'].append(obs_dict[feature+'_predicted_observations'])
for key in rmse_dict:
#print('Avg',key,mean(rmse_dict[key]))
#print('Stdv',key,stdev(rmse_dict[key]))
rmse_dict_stat['Avg_'+key] = mean(rmse_dict[key])
rmse_dict_stat['Stdv_'+key] = stdev(rmse_dict[key])
rmse_dict_all = {**rmse_dict, **rmse_dict_stat}
for key in mae_dict:
#print('Avg',key,mean(mae_dict[key]))
#print('Stdv',key,stdev(mae_dict[key]))
mae_dict_stat['Avg_'+key] = mean(mae_dict[key])
mae_dict_stat['Stdv_'+key] = stdev(mae_dict[key])
mae_dict_all = {**mae_dict, **mae_dict_stat}
#%%
rmse_df = pd.DataFrame(rmse_dict_all)
mae_df = pd.DataFrame(mae_dict_all)
observations_df = pd.DataFrame(observations_dict)
pred_observations_df = pd.DataFrame(pred_observations_dict)
results = pd.concat([observations_df,pred_observations_df,rmse_df, mae_df], axis=1)
#%%
"""Saves newly generated results"""
results.to_csv('Results/HSMM_Results_{}_{}sec_lookbk_{}sec_pred_window.csv'.format(device_name,lookback*5,prediction_window*5))
print(f"Lookback: {lookback}, Pred. Steps: {prediction_window}\n")
for feature in features:
observs_list, pred_observs_list = [], []
for index, row in results.iterrows():
if type(row[feature+'_observations']) == str:
observs = loads(row[feature+'_observations'])
pred_observs = loads(row[feature+'_predicted_observations'])
else:
observs = row[feature+'_observations']
pred_observs = row[feature+'_predicted_observations']
pred_observs = [0 if i < 0 else i for i in pred_observs]
observs_list.append(observs)
pred_observs_list.append(pred_observs)
print(feature,'mae' , round(mean_absolute_error(observs_list,pred_observs_list),3))
print(feature,'rmse', round(sqrt(mean_squared_error(observs_list,pred_observs_list)),3))
save_dict[feature+'_mae'] = [round(mean_absolute_error(observs_list,pred_observs_list),3)]
save_dict[feature+'_rmse'] = [round(sqrt(mean_squared_error(observs_list,pred_observs_list)),3)]
pd.DataFrame(save_dict).to_csv('Results/{}/HSMM_Error_Results_{}_{}sec_lookbk_{}sec_pred_window.csv'.format(device_name.split('_')[0],device_name,lookback*5,prediction_window*5))
read_flag = 0
print("Done!")
#%%
"""Testing Accuracy"""
for name,group in labeled_data_test.groupby('state'):
print(name)
print(labeled_data_test[lookback:].groupby('state').get_group(name)[f'predicted states - {1} step'].value_counts(normalize=True))
#%%
"""Save test predictons"""
test_name = data_name
labeled_data_test.to_csv(f"data/{device_name.split('_')[0]}/{device_name}_{freq}MHz_res_usage_data_test_pred_{test_name}.csv")
#%%
"""Read test predictions"""
test_name = data_name
labeled_data_test = pd.read_csv(f"data/{device_name.split('_')[0]}/{device_name}_{freq}MHz_res_usage_data_test_pred_{test_name}.csv")
read_flag = 1
#%%
"""remove lookback section"""
labeled_data_test = labeled_data_test[lookback:]
#%%
"""Analysis of Predictions"""
plt.rc('font', **{'weight' : 'bold', 'size' : 18})
for prediction_window in prediction_windows:
accuracy, conf_matrix = [], []
for index, row in labeled_data_test.iterrows():
if read_flag == 0:
accuracy.append(accuracy_score(row[f'label - {prediction_window} step'], row[f'predicted states - {prediction_window} step'])*100)
conf_matrix.append(multilabel_confusion_matrix(row[f'label - {prediction_window} step'], row[f'predicted states - {prediction_window} step'], labels=labeled_data_test['label'].unique().tolist()))
else:
accuracy.append(accuracy_score(loads(row[f'label - {prediction_window} step']), loads(row[f'predicted states - {prediction_window} step']))*100)
conf_matrix.append(multilabel_confusion_matrix(loads(row[f'label - {prediction_window} step']), loads(row[f'predicted states - {prediction_window} step']), labels=labeled_data_test['label'].unique().tolist()))
labeled_data_test[f'accuracy - {prediction_window} step'] = accuracy
labeled_data_test[f'confusion matrix - {prediction_window} step'] = conf_matrix
print(f"{prediction_window} step prediction accuracy: {round(mean(accuracy),2)}%")
print(f"{prediction_window} step confusion matrix: ")
SM.classification_report(conf_matrix, labeled_data_test['state'].unique().tolist())
SM.plot_accuracy_likelihood(prediction_window, 100)
#%%
"""Reads previously generated results"""
results = pd.read_csv('Results/{}/HSMM_Results_{}_{}sec_lookbk_{}sec_pred_window.csv'.format(device_name.split('_')[0],device_name,lookback*5,prediction_window*5))