-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
157 lines (120 loc) · 6.52 KB
/
utils.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
import numpy as np
import torch
import matplotlib.pyplot as plt
import scienceplots
plt.style.use(['science', 'notebook', 'grid'])
import pandas as pd
def retrieve_name(var):
import inspect
callers_local_vars = inspect.currentframe().f_back.f_back.f_locals.items()
return [var_name for var_name, var_val in callers_local_vars if var_val is var]
def log(*argv, show=False):
for arg in argv:
print(f"-"*75)
print(f"{retrieve_name(arg)}")
if show:
print(f"content: ")
print(arg)
print(f"type: {type(arg)}")
if isinstance(arg, np.ndarray) or isinstance(arg, torch.Tensor):
print(f"dtype: {arg.dtype}")
print(f"shape: {arg.shape}")
elif isinstance(arg, list) or isinstance(arg, str) or isinstance(arg, dict):
print(f"len: {len(arg)}")
def plot_confidence_interval2(x, vals: list, num_samples=2000, z=1.96, horizontal_line_width=0.1, marker='o', color1='#2187bb', color2='#2187bb'):
mean, std = vals
confidence_interval = z * std / np.sqrt(num_samples)
top, bot = mean - confidence_interval, mean + confidence_interval
left = x - horizontal_line_width / 2
right = x + horizontal_line_width / 2
plt.plot(x, mean, marker=marker, color=color1) # '#f44336'
plt.plot([x, x], [top, bot], color=color2) # '#2187bb'
plt.plot([left, right], [top, top], color=color2)
plt.plot([left, right], [bot, bot], color=color2)
return confidence_interval
def random_vs_optimal_vs_agent(random, optimal, agent, title, xlabel, ylabel, loc, xticks, stamp):
plt.xlabel(xlabel)
plt.ylabel(ylabel)
for i, data in enumerate(random):
plot_confidence_interval2(i, data, marker='o', color1='#2ca02c')
for i, data in enumerate(optimal):
plot_confidence_interval2(i, data, marker='o', color1='#1f77b4')
for i, data in enumerate(agent):
plot_confidence_interval2(i, data, marker='o', color1='#f44336')
plt.plot([i for i in range(len(random))], [data[0] for data in random], linestyle='--', color='#2ca02c', linewidth=1.5, label="Random")
plt.plot([i for i in range(len(optimal))], [data[0] for data in optimal], linestyle='-.', color='#1f77b4', linewidth=1.5, label="Baseline")
plt.plot([i for i in range(len(agent))], [data[0] for data in agent], linestyle='-', color='#f44336', linewidth=1.5, label="PPO")
# plt.xscale('symlog')
# plt.yscale('symlog')
plt.xticks([i for i in range(len(agent))], xticks)
plt.legend(loc=loc)
# plt.savefig(f"./results/{title}-{stamp}.png", format='png')
plt.savefig(f"./results/{title}-{stamp}.eps", format='eps')
plt.show()
def opt_mse_matrix_vs_signal_tx(opt11, opt21, opt12, opt22, title, xlabel, ylabel, loc, xticks, stamp):
plt.xlabel(xlabel)
plt.ylabel(ylabel)
# ['blue', 'orange', 'green', 'red', 'purple', 'brown', 'pink', 'grey', 'darkyellow', 'cyan']
# ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
for i, data in enumerate(opt11):
plot_confidence_interval2(i, data, marker='', color1='#2ca02c')
for i, data in enumerate(opt21):
plot_confidence_interval2(i, data, marker='', color1='#2ca02c')
plt.plot([i for i in range(len(opt11))], [data[0] for data in opt11],
linestyle='-.', color='#1f77b4', linewidth=1.5, label="Simulate-MSE-Matrix-0")
plt.plot([i for i in range(len(opt21))], [data[0] for data in opt21],
linestyle='-.', color='#ff7f0e', linewidth=1.5, label="Simulate-Signal-Tx-0")
for i, data in enumerate(opt12):
plot_confidence_interval2(i, data, marker='', color1='#2ca02c')
for i, data in enumerate(opt22):
plot_confidence_interval2(i, data, marker='', color1='#2ca02c')
plt.plot([i for i in range(len(opt12))], [data[0] for data in opt12],
linestyle='-.', color='#2ca02c', linewidth=1.5, label="Simulate-MSE-Matrix-1")
plt.plot([i for i in range(len(opt22))], [data[0] for data in opt22],
linestyle='-.', color='#d62728', linewidth=1.5, label="Simulate-Signal-Tx-1")
plt.xticks([i for i in range(len(opt11))], xticks)
plt.legend(loc=loc)
# plt.savefig(f"./results/{title}-{stamp}.png", format='png')
plt.savefig(f"./results/{title}-{stamp}.eps", format='eps')
plt.show()
def mse_matrix_vs_signal_tx(x1, x2, title, xlabel, ylabel, loc, xticks, stamp):
plt.xlabel(xlabel)
plt.ylabel(ylabel)
# ['blue', 'orange', 'green', 'red', 'purple', 'brown', 'pink', 'grey', 'darkyellow', 'cyan']
# ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
for i, data in enumerate(x1):
plot_confidence_interval2(i, data, marker='', color1='#d62728')
for i, data in enumerate(x2):
plot_confidence_interval2(i, data, marker='', color1='#2ca02c')
plt.plot([i for i in range(len(x1))], [data[0] for data in x1],
linestyle='-.', color='#d62728', linewidth=1.5, label="Simulate-MSE-Matrix")
plt.plot([i for i in range(len(x2))], [data[0] for data in x2],
linestyle='-.', color='#2ca02c', linewidth=1.5, label="Simulate-Signal-Tx")
plt.xticks([i for i in range(len(x1))], xticks)
plt.legend(loc=loc)
plt.savefig(f"./results/{title}-{stamp}.eps", format='eps')
plt.show()
def read_csv_and_extract_value(csv_file):
# Read the CSV file into a pandas DataFrame
df = pd.read_csv(csv_file)
# Extract the 'Value' column and store it in a list
xvalues = df['Step'].to_numpy()
yvalues = df['Value'].to_numpy()
return xvalues, yvalues
def plot_instant_reward(title, filename, xscale, yscale, stamp):
# Specify the CSV file name
csv_file_name = f'./data/{filename}.csv'
# Call the function to read and extract 'Value' data
xvalues, yvalues = read_csv_and_extract_value(csv_file_name)
print(xvalues.shape, yvalues.shape)
# Plotting the graph
plt.plot(xvalues / xscale, yvalues / yscale, marker='', linestyle='-')
# plt.yscale("symlog")
plt.yticks([-0.5*i for i in range(1, 13, 2)])
# Adding labels and title
plt.xlabel('Training episodes')
plt.ylabel('Mean episode reward rollout (2.048e4)')
# plt.title('Instant Reward Graph')
# Display and save the plot
plt.savefig(f"./results/{title}-{stamp}.eps", format='eps')
plt.show()