-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathView.py
178 lines (129 loc) · 5.43 KB
/
View.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 8 22:09:11 2019
@author: gutowski
"""
import tkinter
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
from lib.realtimeplot import RealtimePlot
def viewDensity(nbArms, algo, d, nameDataset, viewAll, a):
if viewAll == 1:
# print("toto")
for i in range(0, nbArms):
# print("i: "+str(i))
for f in range(0, d):
tab = []
for z in range(1, int(algo.count[i]) - 1):
tab.append(float(algo.features_theta[a][z][f]))
label_line = "Class: " + str(i) + " - Features: " + str(f)
if tab[len(tab) - 1] != 0.0:
fig = sns.distplot(tab, hist=False, kde=True, kde_kws={'linewidth': 2}, label=label_line)
# select=range (0,len(tab))
# viewGraphic(0,select,tab,[],"weight","feature convergence","2D")
plt.xlabel("Theta weights")
plt.ylabel("Density")
plt.title("Density per features for each class in " + str(nameDataset))
plt.show(fig)
# print(tab)
else:
for f in range(0, d):
tab = []
for z in range(1, int(algo.count[f]) - 1):
tab.append(float(algo.features_theta[a][z][f]))
label_line = "Class: " + str(a) + " - Features: " + str(f)
if tab[len(tab) - 1] != 0.0:
fig = sns.distplot(tab, hist=False, kde=True, kde_kws={'linewidth': 2}, label=label_line)
plt.xlabel("Theta weights")
plt.ylabel("Density")
plt.title("Density per features for class " + str(a) + " in " + str(nameDataset))
plt.show(fig)
# print(tab)
def viewGraphic(measure, xTab, yTab, zTab, metric, graphicTitle, dim):
print("\n" + str(metric) + ": " + str(measure))
root = tkinter.Tk()
root.wm_title(graphicTitle)
fig = Figure(figsize=(5, 4), dpi=100)
if dim == "2D":
fig.add_subplot(111, xlabel="Round", ylabel=metric).plot(xTab, yTab)
elif dim == "3D":
ax = fig.gca(projection='3d')
ax.plot(xTab, yTab, zTab, label=graphicTitle)
ax.set_xlabel("Round")
ax.set_ylabel(metric.split("-")[0])
ax.set_zlabel(metric.split("-")[1])
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
if dim == "3D":
ax.mouse_init()
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect("key_press_event", on_key_press)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)
tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.
def displayRound(i):
if (i + 1) % 1000 == 0 and i > 0:
print("Round -> " + str(i + 1))
def displayDataInformations(nbArms, nbContexts, nbPred, dContexts, horizon):
print("Number of arms: " + str(nbArms))
print("Number of instances: " + str(nbContexts))
print("Number of ratings: " + str(nbPred))
print("\nNumber of dimensions: " + str(dContexts))
print("\nHorizon: " + str(horizon))
def displayAlgorithmInformations(algoName, algo):
if algoName == "linucb":
print("\ndelta: " + str(algo.delta))
print("alpha: " + str(algo.alpha))
if algoName == "egreedy":
print("\nepsilon: " + str(algo.epsilon))
def dynamicView(display, i, metric):
display.add(i + 1, metric)
plt.pause(0.001)
def figure(horizon, nameAlgo, nameData, xl, yl, tl):
fig, axes = plt.subplots()
display = RealtimePlot(axes, horizon)
plt.ylabel(yl)
plt.xlabel(xl)
plt.title(tl + str(nameAlgo) + " - " + str(nameData) + " dataset.")
return display
def plot3D(x, y, z, la):
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z, label=la)
ax.legend()
plt.show()
def nameProcessing(nameAlgorithm):
algorithm = nameAlgorithm
if algorithm == "random":
return nameAlgorithm[0].upper() + nameAlgorithm[1:len(nameAlgorithm)]
if algorithm == "linucb":
return nameAlgorithm[0].upper() + nameAlgorithm[1:3] + nameAlgorithm[3:len(nameAlgorithm)].upper()
if algorithm == "egreedy":
return "epsilon-" + nameAlgorithm[1].upper() + nameAlgorithm[2:3] + nameAlgorithm[3:len(nameAlgorithm)]
if algorithm == "ucb1":
return nameAlgorithm[0:len(nameAlgorithm)].upper()
if algorithm == "ts":
return nameAlgorithm[0:len(nameAlgorithm)].upper()
if algorithm == "lints":
return nameAlgorithm[0].upper() + nameAlgorithm[1:3] + nameAlgorithm[3:len(nameAlgorithm)].upper()
if algorithm == "exp3":
return nameAlgorithm[0:len(nameAlgorithm)].upper()