-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualization.py
More file actions
157 lines (125 loc) · 5.03 KB
/
visualization.py
File metadata and controls
157 lines (125 loc) · 5.03 KB
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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import os
import glob
import json
from arguments import argparser
def draw_is_success_graph(n_episode, is_success, path):
xx = np.arange(n_episode)
yy = is_success
cmap = plt.get_cmap("tab10")
ax = sns.regplot(xx, yy, order=5, color=cmap(0), truncate=True)
ax.plot(xx, yy, label='Success Rate', color=cmap(0), alpha=0.3)
ax.set_title("is_success_recent20")
ax.set_xlabel("Episodes")
ax.set_ylabel("is_success")
# Ignore Outliers
ylim_cnt = max(int(len(yy) * 0.2), 1)
sorted_yy = sorted(yy)
ylim_min = np.mean(sorted_yy[:ylim_cnt])
ylim_max = np.mean(sorted_yy[-ylim_cnt:])
ax.set_ylim(ylim_min - 0.02, ylim_max + 0.05)
middle_path = os.path.join('images', path)
if not os.path.exists(middle_path):
os.makedirs(middle_path)
plt.savefig(os.path.join(middle_path, "is_success.png"), dpi=300)
plt.close()
def slide_window_success_rate_no_overlap(n_episode, window, is_success, path):
success_rate = np.array([np.mean(is_success[step: step * window]) for step in range(n_episode // window)])
success_rate = success_rate[~np.isnan(success_rate)]
xx = np.arange(len(success_rate))
yy = success_rate
cmap = plt.get_cmap("tab10")
ax = sns.regplot(xx, yy, order=5, color=cmap(0), truncate=True)
ax.plot(xx, yy, label='Success Rate', color=cmap(0), alpha=0.3)
ax.set_title("success_rate_recent20")
ax.set_xlabel("Episodes")
ax.set_ylabel("success_rate(%)")
# Ignore Outliers
ylim_cnt = max(int(len(yy) * 0.2), 1)
sorted_yy = sorted(yy)
ylim_min = np.mean(sorted_yy[:ylim_cnt])
ylim_max = np.mean(sorted_yy[-ylim_cnt:])
ax.set_ylim(ylim_min - 0.02, ylim_max + 0.05)
middle_path = os.path.join('images', path)
if not os.path.exists(middle_path):
os.makedirs(middle_path)
plt.savefig(os.path.join(middle_path, "slide_window_success_rate.png"), dpi=300)
plt.close()
def draw_dealtime_graph(n_episode, deal_time, timer, path):
xx = np.arange(n_episode)
yy = np.array(deal_time)
cmap = plt.get_cmap("tab10")
ax = sns.regplot(xx, yy, order=5, color=cmap(0), )
ax.plot(xx, yy, label='Deal Time', color=cmap(0), alpha=0.3)
ax.set_title("Deal Time Trend(ms)")
ax.set_xlabel("Episodes")
ax.set_ylabel("Deal Time")
# Ignore Outliers
ylim_cnt = max(int(len(yy) * 0.2), 1)
sorted_yy = sorted(yy)
ylim_min = np.mean(sorted_yy[:ylim_cnt])
ylim_max = np.mean(sorted_yy[-ylim_cnt:])
ax.set_ylim(ylim_min - 10, ylim_max + 10)
middle_path = os.path.join('images', path)
if not os.path.exists(middle_path):
os.makedirs(middle_path)
plt.savefig(os.path.join(middle_path, "deal_time.png"), dpi=300)
plt.close()
def draw_purchase_graph(orderbook, start_time, timer, path, idx):
# try:
# print(orderbook[0]['when'])
# except:
# print(orderbook)
quantity = np.max([order["when"] for order in orderbook])
prev_point = (0, quantity)
for order in orderbook:
id_ = order['id']
amount = order['amount']
timestamp = order['timestamp']
next_point = (timestamp - start_time, prev_point[1] - amount)
plt.plot(*list(zip(prev_point, next_point)), label=id_)
plt.annotate(id_, xy=prev_point)
prev_point = next_point
plt.xlim(0, timer)
plt.ylim(0, quantity)
plt.xlabel("Deal_time(ms)")
plt.ylabel("remain_amount")
plt.title("purchase tracker")
middle_path = os.path.join('images', path)
if not os.path.exists(middle_path):
os.makedirs(middle_path)
plt.savefig(os.path.join(middle_path, "purchase_amount_{}".format(idx)), dpi=300)
plt.close()
def visualize(path, args=None):
if path.startswith('logs/'):
path = path[5:]
json_files = sorted(glob.glob(os.path.join('logs', path, '*')))
is_success = []
deal_time = []
orders = []
start_times = []
for file in json_files:
with open(file) as f:
data = json.load(f)
is_success.append(data['dealSuccess'])
deal_time.append(data['dealTime'])
orders.append(data['orders'])
start_times.append(data['startTime'])
draw_is_success_graph(len(is_success), np.array(is_success), path)
slide_window_success_rate_no_overlap(len(is_success), args.window, np.array(is_success), path)
draw_dealtime_graph(len(deal_time), deal_time, args.timer, path)
idx = 0
for orderbook, start_time in zip(orders, start_times):
# print(idx)
if start_time != -1 and is_success[idx]:
# print(json_files[idx])
draw_purchase_graph(orderbook, start_time, args.timer, path, idx)
print('{}th purchase graph is drawn'.format(idx))
idx += 1
if __name__ == '__main__':
args = argparser()
visualize(args.vis_dir, args)