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 "L1R", "L1E", "L1M", "L1H",
25 "G2R", "G2E", "G2M", "G2H",
26 "T2R", "T2E", "T2M", "T2H",
27 "G3R", "G3E", "G3M", "G3H",
28 "T3R", "T3E", "T3M", "T3H",
32 """Sort dict returned by ``f``."""
33 def w(*args, **kwargs):
34 d = f(*args, **kwargs)
35 d = {k: v for k, v in sorted(d.items(), key=lambda i: i[0])}
40 """Load scenario from file.
46 raise ValueError("File name as argument needed")
47 with open(fname, "r") as f:
49 scenario = loads(f.read())
51 scenario = {"error": "Bad JSON format."}
54 def load_dir(dname=DNAME):
55 """Load scenarios from directory.
58 dname -- Directory name.
61 raise ValueError("Directory name as argument needed")
63 for d in listdir(dname):
64 for f in listdir("{}/{}".format(dname, d)):
65 s = load("{}/{}/{}".format(dname, d, f))
68 s["fname"] = f.split(".")[0]
72 def load_multidir(dname=DNAME):
73 """Load scenarios from directories in ``dname``.
76 dname -- Directory name.
79 raise ValueError("Directory name as argument needed")
81 if isinstance(dname, list):
89 def grep(what="time", grep_all=False):
90 """Return the dictionary of ``fname``'s and corresponding list of values.
92 If ``what`` not present, use ``-1`` instead.
95 what -- What to filter.
96 grep_all -- Add ``-1`` if ``what`` not in scenario.
99 scenarios = load_multidir(DNAME)
101 if s["fname"] not in filtered:
102 filtered[s["fname"]] = []
103 if what in s and s[what] < 9999:
104 filtered[s["fname"]].append(s[what])
106 filtered[s["fname"]].append(-1)
110 """Return the dictionary of ``fname``'s and corresponding error rates."""
111 f = grep("cost", True)
113 for k, v in f.items():
114 dones = [1 for c in v if c < 9999 and c > -1]
115 e[k] = 100 * (len(v) - len(dones)) / len(v)
118 for k, v in e.items():
124 """Return the dictionary of ``fname``'s and corresponding times."""
128 for k, v in r.items():
134 """Return the dictionary of ``fname``'s and corresponding costs."""
138 for k, v in r.items():
144 """Return the dictionary of ``fname``'s and corresponding orig. costs."""
145 r = grep("orig_path_cost")
148 for k, v in r.items():
154 """Return the dictionary of ``fname``'s and corresponding cusps."""
155 r = grep("cusps-in-path")
158 for k, v in r.items():
164 """Return the dictionary of ``fname``'s and corresponding cusps."""
165 r = grep("orig_cusps-in-path")
168 for k, v in r.items():
174 """Return the dictionary of ``fname``'s and number of iterations."""
175 r = grep("iterations")
178 for k, v in r.items():