1 """Print JSON formatted scenarios histograms.
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 numpy import histogram
20 from os import listdir
21 from sys import argv, exit
23 def get_scenario(fname):
24 """Load scenario from file.
30 raise ValueError("File name as argument needed")
31 with open(fname, "r") as f:
33 scenario = loads(f.read())
35 scenario = {"error": "Bad JSON format."}
38 def get_scenarios(dname):
39 """Load scenarios from directiory.
42 dname -- Directory name.
45 raise ValueError("Directory name as argument needed")
47 for d in listdir(dname):
48 for f in listdir("{}/{}".format(dname, d)):
55 "sc": get_scenario("{}/{}/{}".format(dname, d, f)),
59 def compute_stats(sl={}, what="time", **kwargs):
60 """Return histogram of ``what``.
64 what -- The variable in scenario stats to compute with.
65 **kwargs -- Keyword arguments for ``numpy.histogram``.
68 wl = [s["sc"][what] for s in sl if "sc" in s and what in s["sc"]]
69 return histogram(wl, **kwargs)
71 if __name__ == "__main__":
77 scenarios = get_scenarios(SCEN_DIR)
80 key=lambda kv: int(kv[0]) if kv[0].isnumeric() else kv[0]
93 for (f, sl) in scenarios:
94 assert f == sl[0]["fn"]
95 dirs.append(sl[0]["dn"])
96 files.append(sl[0]["fn"])
98 tmp_hist = compute_stats(sl, w)
99 delta = (tmp_hist[1][1] - tmp_hist[1][0]) / 2
103 in zip(tmp_hist[1][1:], tmp_hist[0])
105 assert len(dirs) == len(files)
107 h2 = "%" # file (planner) names
112 h = "{:<%s}" % int(len(what) * 32 - 1)
113 print(h.format(d), end="")
115 h = "{:<%s}" % int(len(what) * 32)
116 print(h.format(d), end="")
123 h = "{:<%s}" % int(len(what) * 32 - 1)
124 print(h.format(f), end="")
126 h = "{:<%s}" % int(len(what) * 32)
127 print(h.format(f), end="")
135 print("{:<31}".format(w), end="")
137 print("{:<32}".format(w), end="")
140 assert len(histograms) > 0
141 hl = len(histograms[0])
143 for j in range(len(histograms)):
145 "{:<16.2f}{:<16}".format(