forked from heronyang/airport-simulation
-
Notifications
You must be signed in to change notification settings - Fork 11
/
reporter.py
151 lines (122 loc) · 4.68 KB
/
reporter.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
"""Reporter provides helper functions for saving the final results of the batch
simulation runs into csv files or plots.
"""
import os
import shutil
import json
import pylab
import pandas as pd
import matplotlib.pyplot as plt
from config import Config as cfg
from utils import get_batch_plan_name
def save_batch_result(name, expr_var_name, expr_var_range, logs, times):
"""Saves the batch result into files.
Parameters:
expr_var_name: the name of the experimental variable
expr_var_range: the value range of the experimental variable
logs: the number of failures stored in a dataframe
times: the number of sample per setting
"""
metrics_filename = "/metrics.json"
m_metrics = [__get_blank_metrics(expr_var_name) for _ in range(times)]
for expr_var in expr_var_range:
for nth in range(times):
filename = cfg.OUTPUT_DIR + get_batch_plan_name(
name, expr_var, nth) + metrics_filename
try:
m_metrics[nth] = __append_expr_output(
filename, expr_var_name, expr_var, m_metrics[nth])
except FileNotFoundError:
print("Skipped loading %s for report" % filename)
metrics = pd.concat(m_metrics).set_index(expr_var_name)
metrics = metrics.groupby(metrics.index).mean()
output_dir = cfg.BATCH_OUTPUT_DIR + name + "/"
__setup_output_dir(output_dir)
__save_metrics(metrics, output_dir)
__save_logs(logs, output_dir)
def __get_blank_metrics(expr_var_name):
return pd.DataFrame(columns=[
expr_var_name,
"avg_active_aircrafts",
"conflicts",
"makespan",
"avg_queue_size",
"avg_reschedule_exec_time",
"n_delay",
"n_scheduler_delay",
"n_uncertainty_delay"
])
def __append_expr_output(filename, expr_var_name, expr_var, metrics):
with open(filename) as fin:
table = json.load(fin)
metrics = metrics.append({
expr_var_name: expr_var,
"avg_active_aircrafts": table["avg_active_aircrafts"],
"conflicts": table["conflicts"],
"makespan": table["makespan"],
"avg_queue_size": table["avg_queue_size"],
"avg_reschedule_exec_time": table["avg_reschedule_exec_time"],
"n_delay": table["n_delay"],
"n_scheduler_delay": table["n_scheduler_delay"],
"n_uncertainty_delay": table["n_uncertainty_delay"]
}, ignore_index=True)
return metrics
def __save_metrics_bk(metrics, output_dir):
metrics.to_csv(output_dir + "metrics.csv")
for col in list(metrics):
plt.clf()
plt.figure(figsize=cfg.OUTPUT_FIG_SIZE)
filename = output_dir + col + ".png"
metrics[col].plot(kind="line")
plt.tight_layout()
plt.savefig(filename, dpi=cfg.OUTPUT_FIG_DPI)
plt.close('all')
def __save_metrics(metrics, output_dir):
# Saves to a CSV file
metrics.to_csv(output_dir + "metrics.csv")
# Saves plots
# Plot delay
pylab.plot(metrics["n_delay"], '-.', label="Total Delay")
pylab.plot(metrics["n_scheduler_delay"], '--', label="Scheduler Delay")
pylab.plot(metrics["n_uncertainty_delay"], ':',
label="Uncertainty Delay")
pylab.legend()
pylab.savefig(output_dir + "delay.png")
pylab.clf()
# Plot queue size
pylab.plot(metrics["avg_queue_size"], '-', label="Queue Size")
pylab.legend()
pylab.savefig(output_dir + "queue_size.png")
pylab.clf()
# Plot schedule execution time
pylab.plot(metrics["avg_reschedule_exec_time"], '-',
label="Schedule Execution Time")
pylab.legend()
pylab.savefig(output_dir + "schedule_exec_time.png")
pylab.clf()
def __setup_output_dir(output_dir):
# Removes the folder if it's already exists
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
# Creates a brand new folder
os.makedirs(output_dir)
def __save_logs(logs, output_dir):
if logs is None:
print("Logs are empty")
return
# Saves to csv file
logs.to_csv(output_dir + "logs.csv")
# Saves the plot
failed = logs[["expr_var", "failed"]]
failed_mean_count = failed.groupby("expr_var").agg(["mean", "count"])
pylab.plot(failed_mean_count["failed"]["mean"], "-", label="Portion of the"
+ " early-terminated simulations")
pylab.legend()
pylab.savefig(output_dir + "failure.png")
pylab.clf()
def save_failed_num(name, expr_var, nth, failed):
"""Saves the number of failed simulation runs into a file."""
filename = cfg.OUTPUT_DIR +\
get_batch_plan_name(name, expr_var, nth) + "/failed"
with open(filename, "w") as fout:
fout.write(str(failed))