]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/scenario.py
Add orig cusp to scenario print
[hubacji1/iamcar2.git] / scripts / scenario.py
1 """Procedures for loading, parsing, and processing JSON scenarios.
2
3 The structure of input directory (``dname``) must be the
4 following::
5
6    SCEN_DIR/
7       0/
8          fname1.json
9          fname2.json
10          ...
11       1/
12       2/
13       ...
14
15 """
16 from json import loads
17 from os import listdir
18
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)]
22 PNAME = [
23     0,
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",
29 ]
30
31 def sort_dict(f):
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])}
36         return d
37     return w
38
39 def load(fname):
40     """Load scenario from file.
41
42     Keyword arguments:
43     fname -- File name.
44     """
45     if fname is None:
46         raise ValueError("File name as argument needed")
47     with open(fname, "r") as f:
48         try:
49             scenario = loads(f.read())
50         except:
51             scenario = {"error": "Bad JSON format."}
52     return scenario
53
54 def load_dir(dname=DNAME):
55     """Load scenarios from directory.
56
57     Keyword arguments:
58     dname -- Directory name.
59     """
60     if dname is None:
61         raise ValueError("Directory name as argument needed")
62     scenarios = []
63     for d in listdir(dname):
64         for f in listdir("{}/{}".format(dname, d)):
65             s = load("{}/{}/{}".format(dname, d, f))
66             s["dname"] = dname
67             s["subdname"] = d
68             s["fname"] = f.split(".")[0]
69             scenarios.append(s)
70     return scenarios
71
72 def load_multidir(dname=DNAME):
73     """Load scenarios from directories in ``dname``.
74
75     Keyword arguments:
76     dname -- Directory name.
77     """
78     if dname is None:
79         raise ValueError("Directory name as argument needed")
80     s = []
81     if isinstance(dname, list):
82         for d in dname:
83             s += load_dir(d)
84     else:
85         s = load_dir(dname)
86     return s
87
88 @sort_dict
89 def grep(what="time", grep_all=False):
90     """Return the dictionary of ``fname``'s and corresponding list of values.
91
92     If ``what`` not present, use ``-1`` instead.
93
94     Keyword arguments:
95     what -- What to filter.
96     grep_all -- Add ``-1`` if ``what`` not in scenario.
97     """
98     filtered = {}
99     scenarios = load_multidir(DNAME)
100     for s in scenarios:
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])
105         elif grep_all:
106             filtered[s["fname"]].append(-1)
107     return filtered
108
109 def error_rate():
110     """Return the dictionary of ``fname``'s and corresponding error rates."""
111     f = grep("cost", True)
112     e = {}
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)
116     e2 = {}
117     i = 1
118     for k, v in e.items():
119         e2[PNAME[i]] = v
120         i += 1
121     return [e2]
122
123 def time():
124     """Return the dictionary of ``fname``'s and corresponding times."""
125     r = grep("time")
126     r2 = {}
127     i = 1
128     for k, v in r.items():
129         r2[PNAME[i]] = v
130         i += 1
131     return r2
132
133 def cost():
134     """Return the dictionary of ``fname``'s and corresponding costs."""
135     r = grep("cost")
136     r2 = {}
137     i = 1
138     for k, v in r.items():
139         r2[PNAME[i]] = v
140         i += 1
141     return r2
142
143 def orig_cost():
144     """Return the dictionary of ``fname``'s and corresponding orig. costs."""
145     r = grep("orig_path_cost")
146     r2 = {}
147     i = 1
148     for k, v in r.items():
149         r2[PNAME[i]] = v
150         i += 1
151     return r2
152
153 def cusp():
154     """Return the dictionary of ``fname``'s and corresponding cusps."""
155     r = grep("cusps-in-path")
156     r2 = {}
157     i = 1
158     for k, v in r.items():
159         r2[PNAME[i]] = v
160         i += 1
161     return r2
162
163 def orig_cusp():
164     """Return the dictionary of ``fname``'s and corresponding cusps."""
165     r = grep("orig_cusps-in-path")
166     r2 = {}
167     i = 1
168     for k, v in r.items():
169         r2[PNAME[i]] = v
170         i += 1
171     return r2
172
173 def iter():
174     """Return the dictionary of ``fname``'s and number of iterations."""
175     r = grep("iterations")
176     r2 = {}
177     i = 1
178     for k, v in r.items():
179         r2[PNAME[i]] = v
180         i += 1
181     return r2