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",
37 """Sort dict returned by ``f``."""
38 def w(*args, **kwargs):
39 d = f(*args, **kwargs)
40 d = {k: v for k, v in sorted(d.items(), key=lambda i: i[0])}
45 """Load scenario from file.
51 raise ValueError("File name as argument needed")
52 with open(fname, "r") as f:
54 scenario = loads(f.read())
56 scenario = {"error": "Bad JSON format."}
59 def load_dir(dname=DNAME):
60 """Load scenarios from directory.
63 dname -- Directory name.
66 raise ValueError("Directory name as argument needed")
70 for d in listdir(dname):
71 for f in listdir("{}/{}".format(dname, d)):
72 s = load("{}/{}/{}".format(dname, d, f))
75 s["fname"] = f.split(".json")[0]
79 def load_multidir(dname=DNAME):
80 """Load scenarios from directories in ``dname``.
83 dname -- Directory name.
86 raise ValueError("Directory name as argument needed")
90 if isinstance(dname, list):
98 def grep(what="time", grep_all=False):
99 """Return the dictionary of ``fname``'s and corresponding list of values.
101 If ``what`` not present, use ``-1`` instead.
104 what -- What to filter.
105 grep_all -- Add ``-1`` if ``what`` not in scenario.
108 scenarios = load_multidir(DNAME)
110 if s["fname"] not in filtered:
111 filtered[s["fname"]] = []
112 if what in s and s[what] < 99999999:
113 filtered[s["fname"]].append(s[what])
115 filtered[s["fname"]].append(-1)
118 def greps(what="time", grep_all=False):
119 """Return the dictionary of ``fname``'s and corresponding list of values.
121 If ``what`` not present, use ``-1`` instead.
124 what -- What to filter.
125 grep_all -- Add ``-1`` if ``what`` not in scenario.
128 scenarios = load_multidir(DNAME)
130 if s["fname"] not in filtered:
131 filtered[s["fname"]] = {}
133 filtered[s["fname"]][int(s["subdname"])] = s[what]
137 """Return the dictionary of ``fname``'s and corresponding error rates."""
138 f = grep("cost", True)
140 for k, v in f.items():
141 dones = [1 for c in v if c < 9999 and c > 0]
142 e[k] = 100 * (len(v) - len(dones)) / len(v)
145 for k, v in e.items():
151 """Return the dictionary of ``fname``'s and corresponding times."""
155 for k, v in r.items():
161 """Return the dictionary of ``fname``'s and corresponding costs."""
165 for k, v in r.items():
171 """Return the dictionary of ``fname``'s and corresponding costs."""
172 r = greps("log_path_cost")
177 for k, v in r.items():
178 for k2, v2 in v.items():
180 for k, v in r.items():
181 for k2, v2 in v.items():
185 MV = max(MV, M - len(v2))
186 it[k2] = [0 for i in range(M - len(v2))]
189 r = greps("path_cost_before_opt")
192 for k, v in r.items():
195 r = greps("iterations")
198 for k, v in r.items():
204 """Return the dictionary of ``fname``'s and corresponding orig. costs."""
205 r = grep("orig_path_cost")
208 for k, v in r.items():
214 """Return the dictionary of ``fname``'s and corresponding cusps."""
215 r = grep("cusps-in-path")
218 for k, v in r.items():
224 """Return the dictionary of ``fname``'s and corresponding cusps."""
225 r = grep("orig_cusps-in-path")
228 for k, v in r.items():
234 """Return the dictionary of ``fname``'s and number of iterations."""
235 r = grep("iterations")
238 for k, v in r.items():
244 """Return the dictionary of ``fname``'s and logged path costs."""
245 r = grep("log_path_cost")
248 for k, v in r.items():