1 """Print JSON formatted scenarios statistics.
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)):
53 "sc": get_scenario("{}/{}/{}".format(dname, d, f)),
57 def compute_stats(sl={}, what="time"):
58 """Return ``mean``, ``std``, ``med``, ``max``, and ``min`` of ``what``.
62 what -- The variable in scenario stats to compute with.
65 wl = [s["sc"][what] for s in sl if "sc" in s and what in s["sc"]]
72 wl_mean = sum(wl) / len(wl)
73 wl_tmp = [(x - wl_mean)**2 for x in wl]
74 wl_std = (sum(wl_tmp) / len(wl_tmp))**0.5
78 wl_med = wl[round(len(wl) / 2)]
80 "err": 100 * (len(sl) - len(wl)) / len(sl),
88 if __name__ == "__main__":
95 """Generate output string for values.
98 h -- String for header?
100 pr = "{:<8.2f}" if not h else "{:<8}"
103 pr += " {:<16.6f}" if not h else " {:<16}"
106 scenarios = get_scenarios(SCEN_DIR)
109 key=lambda kv: int(kv[0]) if kv[0].isnumeric() else kv[0]
111 print(gos(True).format(
114 "max [s]", # elapsed time
118 "max [m]", # path cost
122 "max [-]", # path cusps
128 for (f, sl) in scenarios:
129 tl = compute_stats(sl, "time")
130 cl = compute_stats(sl, "cost")
131 cusps = compute_stats(sl, "cusps-in-path")