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
19 DNAME = "/home/jiri/phd/measurements/iamcar2_uniform-dist-sampling"
20 PNAME = [i for i in range(0, 10)]
21 PNAME += [chr(i) for i in range(97, 123)]
24 """Sort dict returned by ``f``."""
25 def w(*args, **kwargs):
26 d = f(*args, **kwargs)
27 d = {k: v for k, v in sorted(d.items(), key=lambda i: i[0])}
32 """Load scenario from file.
38 raise ValueError("File name as argument needed")
39 with open(fname, "r") as f:
41 scenario = loads(f.read())
43 scenario = {"error": "Bad JSON format."}
46 def load_dir(dname=DNAME):
47 """Load scenarios from directory.
50 dname -- Directory name.
53 raise ValueError("Directory name as argument needed")
55 for d in listdir(dname):
56 for f in listdir("{}/{}".format(dname, d)):
57 s = load("{}/{}/{}".format(dname, d, f))
60 s["fname"] = f.split(".")[0]
64 def load_multidir(dname=DNAME):
65 """Load scenarios from directories in ``dname``.
68 dname -- Directory name.
71 raise ValueError("Directory name as argument needed")
73 if isinstance(dname, list):
81 def grep(what="time", grep_all=False):
82 """Return the dictionary of ``fname``'s and corresponding list of values.
84 If ``what`` not present, use ``-1`` instead.
87 what -- What to filter.
88 grep_all -- Add ``-1`` if ``what`` not in scenario.
91 scenarios = load_multidir(DNAME)
93 if s["fname"] not in filtered:
94 filtered[s["fname"]] = []
95 if what in s and s[what] < 9999:
96 filtered[s["fname"]].append(s[what])
98 filtered[s["fname"]].append(-1)
102 """Return the dictionary of ``fname``'s and corresponding error rates."""
103 f = grep("cost", True)
105 for k, v in f.items():
106 dones = [1 for c in v if c < 9999 and c > -1]
107 e[k] = 100 * (len(v) - len(dones)) / len(v)
110 for k, v in e.items():
116 """Return the dictionary of ``fname``'s and corresponding times."""
120 for k, v in r.items():
126 """Return the dictionary of ``fname``'s and corresponding costs."""
130 for k, v in r.items():
136 """Return the dictionary of ``fname``'s and corresponding orig. costs."""
137 r = grep("orig_path_cost")
140 for k, v in r.items():
146 """Return the dictionary of ``fname``'s and corresponding cusps."""
147 r = grep("cusps-in-path")
150 for k, v in r.items():
156 """Return the dictionary of ``fname``'s and number of iterations."""
157 r = grep("iterations")
160 for k, v in r.items():