1 """Procedures for loading, parsing, and processing JSON scenarios.
3 The structure of input directory (``dname``) must be the
16 from json import loads
17 from os import listdir
22 """Sort dict returned by ``f``."""
23 def w(*args, **kwargs):
24 d = f(*args, **kwargs)
25 d = {k: v for k, v in sorted(d.items(), key=lambda i: i[0])}
30 """Load scenario from file.
36 raise ValueError("File name as argument needed")
37 with open(fname, "r") as f:
39 scenario = loads(f.read())
41 scenario = {"error": "Bad JSON format."}
44 def load_dir(dname=DNAME):
45 """Load scenarios from directory.
48 dname -- Directory name.
51 raise ValueError("Directory name as argument needed")
53 for d in listdir(dname):
54 for f in listdir("{}/{}".format(dname, d)):
55 s = load("{}/{}/{}".format(dname, d, f))
58 s["fname"] = f.split(".")[0]
62 def load_multidir(dname=DNAME):
63 """Load scenarios from directories in ``dname``.
66 dname -- Directory name.
69 raise ValueError("Directory name as argument needed")
71 if isinstance(dname, list):
79 def grep_all(what="time"):
80 """Return the dictionary of ``fname``'s and corresponding list of values.
82 If ``what`` not present, use ``-1`` instead.
85 what -- What to filter.
88 scenarios = load_multidir(DNAME)
90 if s["fname"] not in filtered:
91 filtered[s["fname"]] = []
93 filtered[s["fname"]].append(s[what])
95 filtered[s["fname"]].append(-1)
99 def grep(what="time"):
100 """Return the dictionary of ``fname``'s and corresponding list of values.
103 what -- What to filter.
106 scenarios = load_multidir(DNAME)
108 if s["fname"] not in filtered:
109 filtered[s["fname"]] = []
111 filtered[s["fname"]].append(s[what])
115 """Return the dictionary of ``fname``'s and corresponding error rates."""
118 for k, v in f.items():
119 dones = [1 for c in v if c < 9999 and c > -1]
120 e[k] = 100 * (len(v) - len(dones)) / len(v)
124 """Return the dictionary of ``fname``'s and corresponding times."""
128 """Return the dictionary of ``fname``'s and corresponding costs."""
132 """Return the dictionary of ``fname``'s and corresponding cusps."""
133 return grep("cusps-in-path")