-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_plot.py
80 lines (68 loc) · 3.21 KB
/
metrics_plot.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
import argparse
import os
import re
import xml.etree.ElementTree as ET
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
def parse_tripinfo_file(file_path):
try:
tree = ET.parse(file_path)
root = tree.getroot()
tripinfos = root.findall("tripinfo")
return tripinfos
except Exception as e:
print(f"Error parsing {file_path}: {e}")
return None
def get_waiting_times(tripinfos):
waiting_times = []
for info in tripinfos:
waiting_times.append(float(info.get("waitingTime"))/60)
return waiting_times
def process_folder(folder_path):
data_throughput = []
data_waiting_time = []
for filename in os.listdir(folder_path):
if match := re.match(r"(tripinfo|tripinfo-platooning)_(\d+\.\d+|\d+)\.xml", filename):
category = match.group(1)
traffic_intensity = float(match.group(2))
file_path = os.path.join(folder_path, filename)
# Parse the XML and get the tripinfo count
tripinfos = parse_tripinfo_file(file_path)
throughput = len(tripinfos) if tripinfos else 0
data_throughput.append({"category": category, "traffic_intensity": traffic_intensity, "throughput": throughput})
waiting_times = get_waiting_times(tripinfos)
for waiting_time in waiting_times:
data_waiting_time.append({"category": category, "traffic_intensity": traffic_intensity, "waiting_time": waiting_time})
return {"throughput": pd.DataFrame(data_throughput), "waiting_time": pd.DataFrame(data_waiting_time)}
def plot_throughput_data(df, order):
plt.figure(figsize=(12, 6))
sns.barplot(x="traffic_intensity", y="throughput", hue="category", palette="Blues", data=df, hue_order=order)
plt.title("Throughput (considered unit time = 1 hour) over traffic intensity")
plt.xlabel("Traffic intensity")
plt.ylabel("Throughput")
plt.legend(title="Simulation kind", bbox_to_anchor=(0, 1), loc='upper left', ncol=2)
plt.tight_layout() # Adjust layout to prevent clipping
plt.show()
def plot_waiting_time_data(df, order):
plt.figure(figsize=(12, 6))
sns.boxplot(x="traffic_intensity", y="waiting_time", hue="category", palette="Blues", data=df, hue_order=order)
plt.title("Waiting time over traffic intensity")
plt.xlabel("Traffic intensity")
plt.ylabel("Waiting time [min]")
plt.legend(title="Simulation kind", bbox_to_anchor=(0, 1), loc='upper left', ncol=2)
plt.tight_layout() # Adjust layout to prevent clipping
plt.show()
if __name__ == "__main__":
# Create the parser
parser = argparse.ArgumentParser(description='Plot the simulation related metrics')
parser.add_argument('folder_path', type=str, help='Folder containing the data to process')
# Parse the arguments
args = parser.parse_args()
metrics = process_folder(args.folder_path)
order = ["tripinfo", "tripinfo-platooning"]
if not metrics["throughput"].empty and not metrics["waiting_time"].empty:
plot_throughput_data(metrics["throughput"], order)
plot_waiting_time_data(metrics["waiting_time"], order)
else:
print("No matching files found or no valid data to plot.")