1 """Print JSON formatted scenarios time data.
3 The structure of input directory (``sys.argv[1]``) must be the
16 from json import loads
17 from math import cos, inf, pi, sin
18 from matplotlib import pyplot as plt
19 from os import listdir
20 from sys import argv, exit
22 def get_scenario(fname):
23 """Load scenario from file.
29 raise ValueError("File name as argument needed")
30 with open(fname, "r") as f:
32 scenario = loads(f.read())
34 scenario = {"error": "Bad JSON format."}
37 def get_scenarios(dname):
38 """Load scenarios from directiory.
41 dname -- Directory name.
44 raise ValueError("Directory name as argument needed")
46 for d in listdir(dname):
47 for f in listdir("{}/{}".format(dname, d)):
54 "sc": get_scenario("{}/{}/{}".format(dname, d, f)),
58 def compute_stats(sl={}, what="time"):
59 """Return ``mean``, ``std``, ``med``, ``max``, and ``min`` of ``what``.
63 what -- The variable in scenario stats to compute with.
67 s["sc"][what] for s in sl
72 and s["sc"]["cost"] != 9999
80 and s["sc"]["cost"] != 9999
81 and s["sc"]["cost"] != -1
90 wl_mean = sum(wl) / len(wl)
91 wl_tmp = [(x - wl_mean)**2 for x in wl]
92 wl_std = (sum(wl_tmp) / len(wl_tmp))**0.5
96 wl_med = wl[round(len(wl) / 2)]
98 "err": 100 * (len(sl) - len(el)) / len(sl),
106 if __name__ == "__main__":
111 scenarios = get_scenarios(SCEN_DIR)
114 key=lambda kv: int(kv[0]) if kv[0].isnumeric() else kv[0]
118 for (f, sl) in scenarios:
119 assert f == sl[0]["fn"]
120 tres.append(compute_stats(sl, "time"))
121 cres.append(compute_stats(sl, "cost"))
122 with open("data_time_{}".format(SCEN_DIR.split("/")[0]), "w") as wf:
123 for i in range(len(tres[0])):
125 for s in range(len(tres)):
127 l = "{} {}".format(l, tres[s][i])
129 l = "{} {}".format(l, 0)
132 with open("data_cost_{}".format(SCEN_DIR.split("/")[0]), "w") as wf:
133 for i in range(len(cres[0])):
135 for s in range(len(cres)):
137 l = "{} {}".format(l, cres[s][i])
139 l = "{} {}".format(l, 0)