-
Notifications
You must be signed in to change notification settings - Fork 12
/
display.py
238 lines (199 loc) · 6.71 KB
/
display.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
# -*- coding: utf-8 -*-
import matplotlib.pylab as plt
import numpy as np
import copy
import time
import threading
import pickle
class Display:
def __init__(self, id2ap, REFRESH_INTERVAL=1, PREDICT=False):
self.t_begin = None
self.predict = PREDICT
self.t = []
self.rssi = {}
self.action = []
self.reward = []
self.q_value = {}
if self.predict:
self.feature_vector = []
self.end = False
self.threads = []
self.interval = REFRESH_INTERVAL
self.id2ap = id2ap
pass
def append(self, data):
"""
Pass a dict to display which is in a specific format
:param data:
{'timestamp' : int, 'rssi' : np.array(numAps), 'q' : np.array(numAps), 'action_index' : int, 'reward' : int, 'feature_vector' : np.array(hidden_size) }
:return:
"""
t = copy.copy(self.t)
q_value = copy.deepcopy(self.q_value)
rssi = copy.deepcopy(self.rssi)
reward = copy.copy(self.reward)
action = copy.copy(self.action)
if self.predict:
feature_vector = copy.copy(self.feature_vector)
if len(t) == 0:
self.t_begin = data['timestamp']
t.append(0)
else:
t.append(data['timestamp'] - self.t_begin)
reward.append(data['reward'])
action.append(data['action_index'])
if self.predict:
feature_vector.append(data['feature_vector'])
for ap, r in enumerate(data['rssi']):
if ap not in rssi.keys():
rssi[ap] = []
rssi[ap].append(r)
for ap, q in enumerate(data['q']):
if ap not in q_value.keys():
q_value[ap] = []
q_value[ap].append(q)
self.t = copy.copy(t)
self.q_value = copy.deepcopy(q_value)
self.rssi = copy.deepcopy(rssi)
self.reward = copy.copy(reward)
self.action = copy.copy(action)
if self.predict:
self.feature_vector = copy.copy(feature_vector)
def _plot(self):
fig, ax = plt.subplots(nrows=2, ncols=2, sharex=True)
rssi_fig = ax[0][0]
reward_fig = ax[0][1]
action_fig = ax[1][0]
q_fig = ax[1][1]
fig_tp = plt.figure()
ax_feature_vector = fig_tp.add_subplot(111)
while not self.end:
t = copy.copy(self.t)
q_value = copy.deepcopy(self.q_value)
rssi = copy.deepcopy(self.rssi)
reward = copy.copy(self.reward)
action = copy.copy(self.action)
if self.predict:
feature_vector = copy.copy(self.feature_vector)
feature_vector = np.array(feature_vector)
feature_vector = feature_vector.transpose((1,0))
print feature_vector.shape
if len(t) == 0:
time.sleep(self.interval)
continue
if len(t) != len(q_value[0]) or len(t) != len(rssi[0]) or len(t) != len(reward) or len(t) != len(action):
continue
max_t = t[-1] + 100
min_t = max_t - 900 if max_t - 900 > 0 else 0
rssi_fig.cla()
rssi_fig.set_title("Rssi / dBm")
rssi_fig.set_ylim(-90, -20)
rssi_fig.set_xlim(min_t, max_t)
rssi_fig.grid()
for id, r in rssi.items():
rssi_fig.plot(t, np.array(r) - 255, label=self.id2ap[id])
rssi_fig.legend(loc='best')
reward_fig.cla()
reward_fig.set_title("Throughtout / Mbps")
reward_fig.set_ylim(0, 70)
reward_fig.set_xlim(min_t, max_t)
reward_fig.grid()
reward_fig.plot(t, reward)
action_fig.cla()
action_fig.set_title("AP")
action_fig.set_xlabel("Time / s")
action_fig.set_yticks([0, 1])
action_fig.set_yticklabels([self.id2ap[i] for i in [0, 1]], rotation=30)
action_fig.set_ylim(-1, 2)
action_fig.set_xlim(min_t, max_t)
action_fig.grid()
action_fig.plot(t, action)
q_fig.cla()
q_fig.set_title("Q Value / Mbps")
q_fig.set_xlabel("Time / s")
q_fig.set_ylim(100, 160)
q_fig.set_xlim(min_t, max_t)
q_fig.grid()
for id, q in q_value.items():
q_fig.plot(t, q, label=self.id2ap[id])
q_fig.legend(loc='best')
#ax_feature_vector.imshow(feature_vector)
plt.pause(self.interval)
if self.predict:
save_data = {
't': self.t,
'rssi': self.rssi,
'q': self.q_value,
'reward': self.reward,
'action': self.action,
'id2ap': self.id2ap,
'feature_vector': self.feature_vector
}
else:
save_data = {
't': self.t,
'rssi': self.rssi,
'q': self.q_value,
'reward': self.reward,
'action': self.action,
'id2ap': self.id2ap,
}
output = open('fig.pkl', 'wb')
pickle.dump(save_data, output, -1)
pass
def display(self):
t1 = threading.Thread(target=self._plot)
self.threads.append(t1)
for t in self.threads:
t.setDaemon(True)
t.start()
print('display starting...')
def stop(self):
self.end = True
for t in self.threads:
t.join()
print('stop display')
def display(data):
"""
Pass a dict to display which is in a specific format
:param data:
{'timestamp' : int, 'rssi' : np.array(numAps), 'q' : np.array(numAps), 'action' : string, 'reward' : int}
:return:
"""
print(data)
pass
if __name__ == '__main__':
f = Display()
f.display()
data = {}
data['timestamp'] = time.time()
data['rssi'] = np.array([170, 108])
data['q'] = np.array([142, 130])
data['reward'] = 65
data['action_index'] = 1
f.append(data)
time.sleep(0.1)
data['timestamp'] = time.time()
data['rssi'] = np.array([170, 108])
data['q'] = np.array([142, 130])
data['reward'] = 65
data['action_index'] = 1
f.append(data)
time.sleep(0.1)
data['timestamp'] = time.time()
data['rssi'] = np.array([170, 108])
data['q'] = np.array([142, 130])
data['reward'] = 65
data['action_index'] = 1
f.append(data)
time.sleep(0.1)
data['timestamp'] = time.time()
data['rssi'] = np.array([170, 108])
data['q'] = np.array([142, 130])
data['reward'] = 65
data['action_index'] = 1
f.append(data)
time.sleep(0.1)
# f.plot()
time.sleep(5)
f.stop()