]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blobdiff - scripts/scenario.py
Fix failure rate, resize plot
[hubacji1/iamcar2.git] / scripts / scenario.py
index 5dba5d3b3fe35f0106321aacf8597c676dbafeab..3ef8be3bae865a3040cdb66131088f982348619e 100644 (file)
@@ -16,7 +16,30 @@ following::
 from json import loads
 from os import listdir
 
-DNAME = "out"
+TITLE = ""
+DNAME = "/home/jiri/phd/measurements/iamcar2_uniform-dist-sampling"
+PNAME = [i for i in range(0, 10)]
+PNAME += [chr(i) for i in range(97, 123)]
+PNAME = [
+    0,
+    "L1R", "L1E", "L1M", "L1H",
+    "G2R", "G2E", "G2M", "G2H",
+    "T2R", "T2E", "T2M", "T2H",
+    "G3R", "G3E", "G3M", "G3H",
+    "T3R", "T3E", "T3M", "T3H",
+]
+PNAME = [
+    0,
+    "T3H",
+]
+
+def sort_dict(f):
+    """Sort dict returned by ``f``."""
+    def w(*args, **kwargs):
+        d = f(*args, **kwargs)
+        d = {k: v for k, v in sorted(d.items(), key=lambda i: i[0])}
+        return d
+    return w
 
 def load(fname):
     """Load scenario from file.
@@ -41,46 +64,202 @@ def load_dir(dname=DNAME):
     """
     if dname is None:
         raise ValueError("Directory name as argument needed")
+    global TITLE
+    TITLE = dname
     scenarios = []
     for d in listdir(dname):
         for f in listdir("{}/{}".format(dname, d)):
             s = load("{}/{}/{}".format(dname, d, f))
             s["dname"] = dname
             s["subdname"] = d
-            s["fname"] = f
+            s["fname"] = f.split(".json")[0]
             scenarios.append(s)
     return scenarios
 
-def grep_all(what="time"):
+def load_multidir(dname=DNAME):
+    """Load scenarios from directories in ``dname``.
+
+    Keyword arguments:
+    dname -- Directory name.
+    """
+    if dname is None:
+        raise ValueError("Directory name as argument needed")
+    global TITLE
+    TITLE = dname
+    s = []
+    if isinstance(dname, list):
+        for d in dname:
+            s += load_dir(d)
+    else:
+        s = load_dir(dname)
+    return s
+
+@sort_dict
+def grep(what="time", grep_all=False):
     """Return the dictionary of ``fname``'s and corresponding list of values.
 
     If ``what`` not present, use ``-1`` instead.
 
     Keyword arguments:
     what -- What to filter.
+    grep_all -- Add ``-1`` if ``what`` not in scenario.
     """
     filtered = {}
-    scenarios = load_dir(DNAME)
+    scenarios = load_multidir(DNAME)
     for s in scenarios:
         if s["fname"] not in filtered:
             filtered[s["fname"]] = []
-        if what in s:
+        if what in s and s[what] < 9999:
             filtered[s["fname"]].append(s[what])
-        else:
+        elif grep_all:
             filtered[s["fname"]].append(-1)
     return filtered
 
-def grep(what="time"):
+def greps(what="time", grep_all=False):
     """Return the dictionary of ``fname``'s and corresponding list of values.
 
+    If ``what`` not present, use ``-1`` instead.
+
     Keyword arguments:
     what -- What to filter.
+    grep_all -- Add ``-1`` if ``what`` not in scenario.
     """
     filtered = {}
-    scenarios = load_dir(DNAME)
+    scenarios = load_multidir(DNAME)
     for s in scenarios:
         if s["fname"] not in filtered:
-            filtered[s["fname"]] = []
+            filtered[s["fname"]] = {}
         if what in s:
-            filtered[s["fname"]].append(s[what])
+            filtered[s["fname"]][int(s["subdname"])] = s[what]
     return filtered
+
+def error_rate():
+    """Return the dictionary of ``fname``'s and corresponding error rates."""
+    f = grep("cost", True)
+    e = {}
+    for k, v in f.items():
+        dones = [1 for c in v if c < 9999 and c > 0]
+        e[k] = 100 * (len(v) - len(dones)) / len(v)
+    e2 = {}
+    i = 1
+    for k, v in e.items():
+        e2[PNAME[i]] = v
+        i += 1
+    return [e2]
+
+def time():
+    """Return the dictionary of ``fname``'s and corresponding times."""
+    r = grep("time")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[k] = v
+        i += 1
+    r = grep("otime")
+    for k, v in r.items():
+        if len(v) > 0:
+            r2["opt. {}".format(k)] = v
+    return r2
+
+def otime():
+    """Return the dictionary of ``fname``'s and corresponding opt. times."""
+    r = grep("otime")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[k] = v
+        i += 1
+    return r2
+
+def cost():
+    """Return the dictionary of ``fname``'s and corresponding costs."""
+    r = grep("cost")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[PNAME[i]] = v
+        i += 1
+    return r2
+
+def costs():
+    """Return the dictionary of ``fname``'s and corresponding costs."""
+    r = greps("log_path_cost")
+    r2 = {}
+    M = 0
+    MV = 0
+    it = {}
+    for k, v in r.items():
+        for k2, v2 in v.items():
+            M = max(M, len(v2))
+    for k, v in r.items():
+        for k2, v2 in v.items():
+            if len(v2) == M:
+                it[k2] = v2
+            else:
+                MV = max(MV, M - len(v2))
+                it[k2] = [0 for i in range(M - len(v2))]
+                it[k2].extend(v2)
+    r2[PNAME[1]] = it
+    r = greps("path_cost_before_opt")
+    r3 = {}
+    i = 1
+    for k, v in r.items():
+        r3[PNAME[i]] = v
+        i += 1
+    r = greps("iterations")
+    r4 = {}
+    i = 1
+    for k, v in r.items():
+        r4[PNAME[i]] = v
+        i += 1
+    return [r2, r3, r4]
+
+def orig_cost():
+    """Return the dictionary of ``fname``'s and corresponding orig. costs."""
+    r = grep("orig_path_cost")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[PNAME[i]] = v
+        i += 1
+    return r2
+
+def cusp():
+    """Return the dictionary of ``fname``'s and corresponding cusps."""
+    r = grep("cusps-in-path")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[PNAME[i]] = v
+        i += 1
+    return r2
+
+def orig_cusp():
+    """Return the dictionary of ``fname``'s and corresponding cusps."""
+    r = grep("orig_cusps-in-path")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[PNAME[i]] = v
+        i += 1
+    return r2
+
+def iter():
+    """Return the dictionary of ``fname``'s and number of iterations."""
+    r = grep("iterations")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[PNAME[i]] = v
+        i += 1
+    return r2
+
+def log_path_cost():
+    """Return the dictionary of ``fname``'s and logged path costs."""
+    r = grep("log_path_cost")
+    r2 = {}
+    i = 1
+    for k, v in r.items():
+        r2[PNAME[i]] = v
+        i += 1
+    return r2