-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_parallel.py
367 lines (280 loc) · 12.8 KB
/
graph_parallel.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import sys
import numpy as np
import pandas as pd
import configparser
import seaborn as sns
import matplotlib.pyplot as plt
from OtherScripts.simutil import *
from typing import TypedDict, List
# TODO: ALL FOLDERS MUST BE OF THE SAME TYPE OF SIMULATION FIX THAT
# TODO: THE RESULT IS SAVED INSIDE THE OUT FOLDER (MAYBE FIX)
# TODO: CHECK IF CODE IS NOT TOO MESSY
PATH = ""
class LabelsNdf(TypedDict):
data: pd.DataFrame
labels: str
scene: str
def search_outputs_folder(directory):
folders_with_outputs = []
for root, dirs, _ in os.walk(directory):
if 'outputs' in dirs:
folders_with_outputs.append(root)
return folders_with_outputs
def read_flow_output(directory):
thr_match = []
delay_match = []
for root, dir, _ in os.walk(directory):
for sim in dir:
file_path = os.path.join(root, sim, 'FlowOutput.txt')
if os.path.isfile(file_path):
with open(file_path, 'r') as file:
content = file.readlines()
for line in content:
if "Mean flow throughput: " in line:
thr_match.append(float(
line.replace("Mean flow throughput: ", "")
.strip()))
elif "Mean flow delay: " in line:
delay_match.append(float(
line.replace("Mean flow delay: ", "")
.strip()))
else:
pass
return (thr_match, delay_match)
def get_array_for_violin():
folders = search_outputs_folder(PATH)
tempThr = []
tempDelay = []
delayArray = {'data': [], 'labels': []}
thrArray = {'data': [], 'labels': []}
for simFolder in folders:
thr, delay = read_flow_output(simFolder)
sim1Root = os.path.join(simFolder, "SIM1", "graph.ini")
simLabel = configparser.ConfigParser()
simLabel.read(sim1Root)
simLabel = simLabel["general"]["simlabel"]
tempThr.append({simLabel: thr})
tempDelay.append({simLabel: delay})
tempThr.sort(key=lambda x: list(x)[0])
tempDelay.sort(key=lambda x: list(x)[0])
thrArray["data"] = [list(x.values())[0] for x in tempThr]
thrArray["labels"] = [list(x)[0] for x in tempThr]
delayArray["data"] = [list(x.values())[0] for x in tempDelay]
delayArray["labels"] = [list(x)[0] for x in tempDelay]
return (thrArray, delayArray)
def data_from_file_to_dict(filename: str) -> dict:
"""
From a datafile takes all the data and creates a join dataframe where all
the values are saved. Returns a dictionary with a 'labels' and 'data' keys
which have a list of labels and dataframes, respectively. The lists are
sorted by the label and data in pos 'x' corresponds to label in pos 'x'
"""
folders = search_outputs_folder(PATH)
_dicts_with_df = []
dicts_with_df = {"data": [],
"labels": []}
# folders with a specific simulation is located (inside are the SIMX foldr)
for sim_folder in folders:
all_dfs = []
for root, dir, _ in os.walk(sim_folder):
for sim in dir: # sim are the SIMX folders
file_path = os.path.join(root, sim, filename)
if os.path.isfile(file_path):
df = pd.read_csv(file_path, sep="\t")
all_dfs.append(df)
# Get simulation label
sim1Root = os.path.join(sim_folder, "SIM1", "graph.ini")
simLabel = configparser.ConfigParser()
simLabel.read(sim1Root)
simLabel = simLabel["general"]["simlabel"]
# Concatenate all the dataframes
final_df = pd.concat(all_dfs)
# Add the data to the final list
_dicts_with_df.append({simLabel: final_df})
_dicts_with_df.sort(key=lambda x: list(x)[0])
dicts_with_df["data"] = [list(x.values())[0] for x in _dicts_with_df]
dicts_with_df["labels"] = [list(x)[0] for x in _dicts_with_df]
return dicts_with_df
def separate_by_scenario(data: LabelsNdf) -> List[LabelsNdf]:
# Create a dict with possible labels
scenarios = {}
for pos, label in enumerate(data["labels"]):
specs = [label[2*x:2*x+2] for x in range(len(label)//2)]
for spec in specs:
if "S" in spec:
if spec not in scenarios.keys():
scenarios[spec] = {"labels": [], "data": [], "scene": []}
scenarios[spec]["labels"].append(label.replace(spec, "")
.replace("A0", "A1")) # >Cochinada por ahora, arreglar en simulación después !!
scenarios[spec]["data"].append(data["data"][pos])
scenarios[spec]["scene"] = spec
return list(scenarios.values())
@info_n_time_decorator("Throughput Violin", True)
def violinGraphThr(data):
data = separate_by_scenario(data)
n_cols = len(data)
fig, axes = plt.subplots(1, n_cols)
axes[0].set_ylabel("Throughput [Mb/s]")
# palettes = [sns.color_palette("Set1")[:4], sns.color_palette("Set1")[4:]]
palettes = [sns.color_palette("Set1"), sns.color_palette("Pastel1")]
for pos, ax in enumerate(axes):
max_len = len(sorted(data[pos]["data"], key=lambda x: len(x))[-1])
thrs = []
for thr in data[pos]["data"]:
if len(thr) < max_len:
thr = thr + [np.nan] * (max_len - len(thr))
thrs.append(thr)
dii = dict(A1=r"$BLER_{10\%}$", A2=r"$BLER_{30\%}$",
A3=r"$BLER_{dyn}$", A4=r"$BLER_{hyb}$")
aaa = list(map(lambda x: dii[x], data[pos]["labels"]))
vals = np.array(thrs, dtype=float).T
df = pd.DataFrame(data=vals, columns=aaa)
sns.violinplot(data=df, ax=ax, cut=0, inner=None, palette=palettes[pos])
sns.pointplot(data=df, estimator=np.mean, color="black", ax=ax,
linestyles="--", errorbar=None, scale=0.5, label="Mean")
# Add text on top of the pointplots
for p in zip(ax.get_xticks(),
np.round(np.nanmean(vals, axis=0), decimals=1)):
# Distance from pointplot
weight = 1.032 if p[1] > 50 else 1.25
ax.text(p[0], p[1]*weight, p[1], color='black', ha='center',
bbox=dict(facecolor='white', alpha=0.4, boxstyle="round"))
location = "lower right"
if pos == 1:
ax.set_ylim([0, 65])
location = "best"
ax.set_title(data[pos]["scene"].replace("S", "Scenario "))
ax.set_xlabel("Algorithm")
ax.legend(loc=location)
ax.set_xticks(ticks=range(4), labels=aaa, rotation=5, fontsize=9.2)
fig.suptitle("Distribution of Throughput by Algorithm-Scenario")
fig.savefig(os.path.join(PATH, "Thr-Violin-Par.png"), dpi=300)
plt.close()
return True
@info_n_time_decorator("Delay Violin", debug=True)
def violinGraphDelay(data):
data = separate_by_scenario(data)
n_cols = len(data)
fig, axes = plt.subplots(1, n_cols)
palettes = [sns.color_palette("Set1"), sns.color_palette("Pastel1")]
axes[0].set_ylabel("Delay [ms]")
for pos, ax in enumerate(axes):
max_len = len(sorted(data[pos]["data"], key=lambda x: len(x))[-1])
thrs = []
for thr in data[pos]["data"]:
if len(thr) < max_len:
thr = thr + [np.nan] * (max_len - len(thr))
thrs.append(thr)
dii = dict(A1=r"$BLER_{10\%}$", A2=r"$BLER_{30\%}$",
A3=r"$BLER_{dyn}$", A4=r"$BLER_{hyb}$")
aaa = list(map(lambda x: dii[x], data[pos]["labels"]))
vals = np.array(thrs, dtype=float).T
df = pd.DataFrame(data=vals, columns=aaa)
df.replace(0, np.nan, inplace=True)
# df.to_csv("DelayDf.txt", sep="\t", encoding="utf-8")
sns.violinplot(data=df, ax=ax, cut=0, inner=None, palette=palettes[pos])
sns.pointplot(data=df, estimator=np.mean, color="black", ax=ax,
linestyles="--", errorbar=None, scale=0.5, label="Mean")
# Add text on top of the pointplots
for p in zip(ax.get_xticks(),
np.round(np.nanmean(vals, axis=0), decimals=1)):
# Distance from pointplot
weight = 1.11 if p[1] > 50 else 1.045
ax.text(p[0], p[1]*weight, p[1], color='black', ha='center',
bbox=dict(facecolor='white', alpha=0.4, boxstyle="round"))
if pos == 0:
ax.set_ylim([0, 20])
if pos == 1:
ax.set_ylim([0, 400])
ax.set_title(data[pos]["scene"].replace("S", "Scenario "))
ax.set_xlabel("Algorithm")
ax.legend()
ax.set_xticks(ticks=range(4), labels=aaa, rotation=5, fontsize=9.2)
fig.suptitle("Distribution of Packet Delay by Algorithm-Scenario")
fig.savefig(os.path.join(PATH, "Delay-Violin-Par.png"), dpi=300)
plt.close()
return True
@info_n_time_decorator("Retransmissions", True)
def stackedbar_graph_rtx():
data_dict = data_from_file_to_dict('RxPacketTrace.txt')
RTX_OPTIONS = pd.Series(0, index=range(-1, 4), dtype=np.float64)
for i, df in enumerate(data_dict["data"]):
df.reset_index(drop=True, inplace=True)
df["rtxNum"] = df.loc[df['direction'] == 'DL', "rv"]
df.loc[(df["rv"] == 3) &
(df["corrupt"] == 1) &
(df['direction'] == 'DL'), "rtxNum"] = -1
to_add = (df["rtxNum"].value_counts(normalize=True)*100)\
.sort_index()
data_dict["data"][i] = RTX_OPTIONS.add(to_add, fill_value=0).to_list()
colors = ["#BE0000", "#019875", "#72CC50", "#BFD834", "#00AEAD"]
data = separate_by_scenario(data_dict)
n_cols = len(data)
fig, axes = plt.subplots(1, n_cols, constrained_layout=True)
axes[0].set_ylabel("Percentage of sent blocks [%]")
for pos, ax in enumerate(axes):
bottom = np.zeros(len(data[pos]["labels"]))
xtick_translation = dict(A1=r"$BLER_{10\%}$", A2=r"$BLER_{30\%}$",
A3=r"$BLER_{dyn}$", A4=r"$BLER_{hyb}$")
xticks_labels = list(map(lambda x: xtick_translation[x], data[pos]["labels"]))
legend_labels = ["Failed", "No Re-TX", "1 Re-TX", "2 Re-TX", "3 Re-TX"]
for i, nrtx in enumerate(legend_labels):
values = np.array([x[i] for x in data[pos]["data"]])
ax.bar(data[pos]["labels"], values, 0.69, label=nrtx,
bottom=bottom, color=colors[i])
bottom += values
ax.set_title(data[pos]["scene"].replace("S", "Scenario "))
ax.set_xlabel("Algorithm")
# ax.legend()
ax.set_xticks(ticks=range(4), labels=xticks_labels, rotation=5, fontsize=9.2)
# Legend (https://stackoverflow.com/questions/9834452/how-do-i-make-a-single-legend-for-many-subplots)
lines_labels = [ax.get_legend_handles_labels()]
lines, labels = [sum(lol, []) for lol in zip(*lines_labels)]
fig.legend(lines, labels, loc='upper center', ncol=len(legend_labels), bbox_to_anchor=(0.54, 0.95))
fig.suptitle("Percentage of successful and failed blocks transmission\n\n")
fig.savefig(os.path.join(PATH, "Rtx-Bar-Par.png"), dpi=300)
plt.close()
return True
@info_n_time_decorator("Violin BLER", True)
def violin_graph_bler():
data_dict = data_from_file_to_dict('RxPacketTrace.txt')
for i, df in enumerate(data_dict["data"]):
data_dict["data"][i] = df.loc[(df["direction"] == "DL"), "TBler"]\
.to_list()
data = separate_by_scenario(data_dict)
n_cols = len(data)
fig, axes = plt.subplots(1, n_cols)
axes[0].set_ylabel("BLER")
for pos, ax in enumerate(axes):
max_len = len(sorted(data[pos]["data"], key=lambda x: len(x))[-1])
thrs = []
for thr in data[pos]["data"]:
if len(thr) < max_len:
thr = thr + [np.nan] * (max_len - len(thr))
thrs.append(thr)
vals = np.array(thrs, dtype=float).T
df = pd.DataFrame(data=vals, columns=data[pos]["labels"])
ax.grid(zorder=0)
sns.violinplot(data=df, ax=ax, cut=0, inner=None)
sns.pointplot(data=df, estimator=np.mean, color="black", ax=ax,
linestyles="--", errorbar=None, scale=.5, label="Mean")
ax.set_title(data[pos]["scene"].replace("S", "Scenario "))
ax.set_xlabel("Algorithm")
ax.set_yscale("log")
ax.legend()
fig.suptitle("Distribution of BLER by Algorithm-Scenario")
fig.savefig(os.path.join(PATH, "BLER-Violin-Par.png"), dpi=300)
plt.close()
return True
if __name__ == "__main__":
if len(sys.argv) == 2:
PATH = sys.argv[1]
else:
raise ArgumentError(f"{RED}Incorrect number of arguments, given "
f"{len(sys.argv)} expected 1{CLEAR}")
t, d = get_array_for_violin()
violinGraphThr(t)
violinGraphDelay(d)
stackedbar_graph_rtx()
violin_graph_bler()