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``, ``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 "err": 100 * (len(sl) - len(wl)) / len(sl),
85 if __name__ == "__main__":
92 """Generate output string for values.
95 h -- String for header?
97 pr = "{:<8.2f}" if not h else "{:<8}"
100 pr += " {:<16.6f}" if not h else " {:<16}"
103 scenarios = get_scenarios(SCEN_DIR)
106 key=lambda kv: int(kv[0]) if kv[0].isnumeric() else kv[0]
108 print(gos(True).format(
111 "max [s]", # elapsed time
115 "max [m]", # path cost
119 "max [-]", # path cusps
125 for (f, sl) in scenarios:
126 tl = compute_stats(sl, "time")
127 cl = compute_stats(sl, "cost")
128 cusps = compute_stats(sl, "path#cusp")