-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_results.py
executable file
·206 lines (177 loc) · 6.36 KB
/
plot_results.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
#!/usr/bin/env python3
""" Plot RT Cloud Simulator data. """
import argparse
import matplotlib.pyplot as plt
import pandas as pd
plt.set_loglevel("warning")
def get_flowid_from_column(column_name: str, column_type: str) -> str:
""" Collect flow ID from a column name (e.g., delay[0] -> 0).
Parameters:
column_name (str): Full name of a dataframe column
column_type (str): Type of the column (rate, delay, etc.), a substring of column_name
Returns:
flow ID as a string
"""
return column_name.replace("_slo", '').replace(f"{column_type}[", '').replace(']', '')
def plot_csv(csv_file: str, title=None, outfile=None, tasks=None, flows=None):
""" Plot simulator CSV data.
Parameters:
csv_file (str): input CSV file path
title (str): plot title
outfile (str/None): write graph to this PNG file; show plot if set to None
tasks (list/None): names of tasks to plot
flows (list/None): names of flows to plot
"""
_dataframe = pd.read_csv(csv_file)
plot_df(_dataframe, title, outfile, tasks, flows)
def plot_df(df: pd.DataFrame, title=None, outfile=None, tasks=None, flows=None):
"""Plot simulator data from a Pandas Dataframe. Either write to file
or show plot.
Parameters:
df (pandas.DataFrame): input dataframe
title (str): plot title
outfile (str/None): write graph to this PNG file; show plot if set to None
tasks (list/None): names of tasks to plot
flows (list/None): names of flows to plot
"""
flow_names = flows or [""]
task_names = tasks or [""]
flow_columns = [c for c in df.columns if any(f in c for f in flow_names)]
task_columns = [c for c in df.columns if any(t in c for t in task_names)]
plt.style.use('seaborn-colorblind')
size = 12.5
params = {'legend.fontsize': 'large',
'axes.labelsize': size,
'axes.titlesize': size,
#'figure.titlesize': size*1.375,
'figure.titlesize': size*1.1,
'xtick.labelsize': size*0.75,
'ytick.labelsize': size*0.75,
'axes.titlepad': size*1.25}
plt.rcParams.update(params)
marker_size = 5
fig, axes = plt.subplots(4, 1,
#figsize=(15, 10.5),
figsize=(9, 9),
sharex=True,
)
num_plot_lines = []
# rate
columns_to_plot = [c for c in df.columns if "rate" in c and c in flow_columns]
num_plot_lines.append(len(columns_to_plot))
for y_ax in columns_to_plot:
flow_id = get_flowid_from_column(y_ax, "rate")
legend_label = f"{flow_id}"
marker = 'o'
if "slo" in y_ax:
legend_label += " (SLO)"
marker = ''
df.plot(
ax=axes[0],
marker=marker,
ms=marker_size,
y=y_ax,
label=legend_label,
)
axes[0].set_title('Flow Rates')
axes[0].set_xlabel('Time [round]')
axes[0].set_ylabel('Rate')
# delay
columns_to_plot = [c for c in df.columns if "delay" in c and c in flow_columns]
num_plot_lines.append(len(columns_to_plot))
for y_ax in columns_to_plot:
flow_id = get_flowid_from_column(y_ax, "delay")
legend_label = f"{flow_id}"
marker = 'X'
if "slo" in y_ax:
legend_label += " (SLO)"
marker = ''
df.plot(
ax=axes[1],
marker=marker,
ms=marker_size,
y=y_ax,
label=legend_label,
)
axes[1].set_title('Flow Delays')
axes[1].set_ylabel('Flow Delay')
# weight
columns_to_plot = [c for c in df.columns if "weight" in c and c in task_columns]
num_plot_lines.append(len(columns_to_plot))
for y_ax in columns_to_plot:
legend_label = y_ax.replace("weight[", '').replace(']', '')
df.plot(
ax=axes[2],
marker='D',
ms=marker_size,
y=y_ax,
label=legend_label,
)
axes[2].set_title('Task Weights')
axes[2].set_ylabel('Weight')
axes[2].set_xlabel('Time [round]')
# lambda
columns_to_plot = [c for c in df.columns if "lambda" in c and c in task_columns]
num_plot_lines.append(len(columns_to_plot))
markers = ['D', 'P', 'X', '*', 'p', 'o', 'H']
base_alpha = .9
for mark_idx, y_ax in enumerate(columns_to_plot):
legend_label = y_ax.replace("lambda[", '').replace(']', '')
df.plot(
ax=axes[3],
linewidth=.75,
alpha=base_alpha - (mark_idx * .05),
marker=markers[mark_idx % len(markers)],
ms=marker_size*1.8,
y=y_ax,
label=legend_label,
)
axes[3].set_title('Task Lambdas')
axes[3].set_ylabel('Lambda')
axes[3].set_xlabel('Time [round]')
axes[3].set_yticks([0, 1])
axes[3].set_ylim(-.1, 1.1)
axes[3].set_xticks(range(0, len(df.index)))
# show xticklabels for every 5th xtick
xticks = axes[3].xaxis.get_major_ticks()
for i, tick in enumerate(xticks):
if i % 5 != 0:
tick.label1.set_visible(False)
# set number of legend columns based on the number of lines to plot:
# aim for 6 rows per column, but max 3 columns
num_legend_cols = min(max(max(num_plot_lines) // 6, 1), 3)
for ax in axes:
ax.legend(
ncol=num_legend_cols,
loc="center left",
bbox_to_anchor=(1, .5),
)
# set title
if title:
fig.suptitle(title)
# set layout
layout_args = {}
if title:
layout_args['rect'] = [0, 0, 1, .95]
plt.tight_layout(**layout_args)
# present figure
if outfile:
plt.savefig(outfile, dpi=150)
else:
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('csvfile', type=argparse.FileType('r'),
help='csv to show')
parser.add_argument("-o", "--outfile", type=str,
help="File to save plot")
parser.add_argument("-t", "--title", type=str,
help="Plot title", default='')
parser.add_argument('--verbose', '-v', action='store_true',
help='Enable verbose mode')
args = parser.parse_args()
dataframe = pd.read_csv(args.csvfile)
if args.verbose:
print(dataframe)
# plot figure
plot_df(dataframe, args.title, args.outfile)