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
20 DNAME = "/home/jiri/phd/measurements/iamcar2_uniform-dist-sampling"
21 PNAME = [i for i in range(0, 10)]
22 PNAME += [chr(i) for i in range(97, 123)]
25 "L1R", "L1E", "L1M", "L1H",
26 "G2R", "G2E", "G2M", "G2H",
27 "T2R", "T2E", "T2M", "T2H",
28 "G3R", "G3E", "G3M", "G3H",
29 "T3R", "T3E", "T3M", "T3H",
34 #"no", "sc", "gz", "opt", "osp"
38 """Sort dict returned by ``f``."""
39 def w(*args, **kwargs):
40 d = f(*args, **kwargs)
41 d = {k: v for k, v in sorted(d.items(), key=lambda i: i[0])}
46 """Load scenario from file.
52 raise ValueError("File name as argument needed")
53 with open(fname, "r") as f:
55 scenario = loads(f.read())
57 scenario = {"error": "Bad JSON format."}
60 def load_dir(dname=DNAME):
61 """Load scenarios from directory.
64 dname -- Directory name.
67 raise ValueError("Directory name as argument needed")
71 for d in listdir(dname):
72 for f in listdir("{}/{}".format(dname, d)):
73 s = load("{}/{}/{}".format(dname, d, f))
76 s["fname"] = f.split(".json")[0]
80 def load_multidir(dname=DNAME):
81 """Load scenarios from directories in ``dname``.
84 dname -- Directory name.
87 raise ValueError("Directory name as argument needed")
91 if isinstance(dname, list):
99 def grep(what="time", grep_all=False):
100 """Return the dictionary of ``fname``'s and corresponding list of values.
102 If ``what`` not present, use ``-1`` instead.
105 what -- What to filter.
106 grep_all -- Add ``-1`` if ``what`` not in scenario.
109 scenarios = load_multidir(DNAME)
111 if s["fname"] not in filtered:
112 filtered[s["fname"]] = []
113 if what in s and s[what] < 9999:
114 filtered[s["fname"]].append(s[what])
116 filtered[s["fname"]].append(-1)
119 def greps(what="time", grep_all=False):
120 """Return the dictionary of ``fname``'s and corresponding list of values.
122 If ``what`` not present, use ``-1`` instead.
125 what -- What to filter.
126 grep_all -- Add ``-1`` if ``what`` not in scenario.
129 scenarios = load_multidir(DNAME)
131 if s["fname"] not in filtered:
132 filtered[s["fname"]] = {}
134 filtered[s["fname"]][int(s["subdname"])] = s[what]
138 """Return the dictionary of ``fname``'s and corresponding error rates."""
139 f = grep("cost", True)
141 for k, v in f.items():
142 dones = [1 for c in v if c < 9999 and c > 0]
143 e[k] = 100 * (len(v) - len(dones)) / len(v)
146 for k, v in e.items():
152 """Return the dictionary of ``fname``'s and corresponding times."""
156 for k, v in r.items():
160 for k, v in r.items():
162 r2["opt. {}".format(k)] = v
166 """Return the dictionary of ``fname``'s and corresponding opt. times."""
170 for k, v in r.items():
176 """Return the dictionary of ``fname``'s and corresponding costs."""
180 for k, v in r.items():
186 """Return the dictionary of ``fname``'s and corresponding costs."""
187 r = greps("log_path_cost")
192 for k, v in r.items():
193 for k2, v2 in v.items():
195 for k, v in r.items():
196 for k2, v2 in v.items():
200 MV = max(MV, M - len(v2))
201 it[k2] = [0 for i in range(M - len(v2))]
204 r = greps("path_cost_before_opt")
207 for k, v in r.items():
210 r = greps("iterations")
213 for k, v in r.items():
219 """Return the dictionary of ``fname``'s and corresponding orig. costs."""
220 r = grep("orig_path_cost")
223 for k, v in r.items():
229 """Return the dictionary of ``fname``'s and corresponding cusps."""
230 r = grep("cusps-in-path")
233 for k, v in r.items():
239 """Return the dictionary of ``fname``'s and corresponding cusps."""
240 r = grep("orig_cusps-in-path")
243 for k, v in r.items():
249 """Return the dictionary of ``fname``'s and number of iterations."""
250 r = grep("iterations")
253 for k, v in r.items():
259 """Return the dictionary of ``fname``'s and logged path costs."""
260 r = grep("log_path_cost")
263 for k, v in r.items():