-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic_composition.py
78 lines (67 loc) · 2.41 KB
/
traffic_composition.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
import pandas as pd
import numpy as np
import os, time, pickle
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime, timedelta
import datetime as dt
import matplotlib.dates as mdates
from sklearn.cluster import KMeans
from collections import Counter
from sklearn.preprocessing import StandardScaler
from keras.models import load_model
import util
sns.set_style("darkgrid")
import warnings
warnings.filterwarnings('ignore')
def load_input(filename):
max_len = 80
df_unsupervised = pickle.load(open(filename,'rb'))
X = df_unsupervised.iloc[:,0:max_len]
X = StandardScaler().fit_transform(X.transpose()).transpose()
X = X.reshape(X.shape[0],X.shape[1],1)
return X
def get_scores(X):
d = model.predict(X)
scores = np.array([d[:,0] + d[:,2],d[:,1] + d[:,4],d[:,3] + d[:,5]])
return scores
def get_composition(filename):
X = load_input(filename)
prediction = model.predict(X)
d = Counter(np.argmax(np.round(prediction),axis=1))
composition = np.array([d[0] + d[2],d[1] + d[4],d[3] + d[5]])
return sorted(composition,reverse=True)
def ood_diff(f):
X = load_input(f)
scores = get_scores(X)
return util.get_ood_diff(scores,scores_test)
from keras.models import load_model
classes = ['googlemusic', 'skype', 'spotify', 'vimeo', 'whatsappvideo','youtube']
model = load_model('cnn_model.h5')
max_len = 80
X_test, y_test, d_name= pickle.load(open('cnn_test_sets.pkl','rb'))
scores_test = get_scores(X_test[:,0:max_len])
cell_name = ['campnou','paolo']
composition_dict = dict()
OOD_dict = dict()
for cell in cell_name:
print('Traffic composition for {} cell'.format(cell))
OOD_list = []
folder = '../use_case_files/'
filenames = [f for f in os.listdir(folder) if 'df_' + cell in f]
filepaths = [os.path.join(folder,name) for name in sorted(filenames)]
for num,f in enumerate(filepaths):
print('Slot {}/12'.format(num+1))
if num == 0:
array_tot = get_composition(f)
else:
array_temp = get_composition(f)
array_tot = np.vstack([array_tot, array_temp])
OOD_list.append(np.mean(ood_diff(f)))
composition_dict[cell] = array_tot
OOD_dict[cell] = np.array(OOD_list)
for cell in cell_name:
util.bar_plot_confidence(composition_dict[cell],OOD_dict[cell],cell)
plt.ylim([0,450])
plt.title(cell,size=15)
plt.savefig(cell + '_composition.pdf',bbox_inches='tight')