]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/commitdiff
Add printed results of measure scenarios
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Mon, 13 Apr 2020 22:21:41 +0000 (00:21 +0200)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Mon, 13 Apr 2020 22:21:47 +0000 (00:21 +0200)
CHANGELOG.md
scripts/print.py [new file with mode: 0644]

index 5e89ce27b83e1c34b19b078c3ba16a43c890bb57..26bd5f8bbea2cd7e2ff0d8d96335e2fa5c3002d0 100644 (file)
@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog][] and this project adheres to
   “Randomized Bidirectional B-Spline Parameterization Motion Planning.” IEEE
   Transactions on Intelligent Transportation Systems 17, no. 2 (February 2016):
   406–19. https://doi.org/10.1109/TITS.2015.2477355.
+- Script to print results from measured scenarios.
 
 ### Changed
 - Update python plot scripts.
diff --git a/scripts/print.py b/scripts/print.py
new file mode 100644 (file)
index 0000000..7706278
--- /dev/null
@@ -0,0 +1,160 @@
+"""Procedures for printing scenario results."""
+import sys
+import matplotlib.pyplot as plt
+import numpy as np
+import scipy.stats as ss
+
+import scenario
+
+LATEX = True
+
+def mean_conf_int(data, conf=0.95):
+    """Return (mean, lower, uppper) of data.
+
+    see https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data
+
+    Keyword arguments:
+    data -- A list of data.
+    conf -- Confidence interval.
+    """
+    a = np.array(data)
+    n = len(a)
+    m = np.mean(a)
+    se = ss.sem(a)
+    h = se * ss.t.ppf((1 + conf) / 2, n - 1)
+    return (m, m - h, m + h)
+
+def infoprint(w={}, t=""):
+    """Print statistic information about scenario results.
+
+    Keyword arguments:
+    w -- What to print.
+    t -- Print title.
+    """
+    if LATEX:
+        print("\\begin{table*}[h]")
+    else:
+        print(t)
+    if LATEX:
+        print("\\begin{tabular}{", end="")
+        print("|c|", end="")
+        for k, v in w.items():
+            print("c|", end="")
+        print("}", end="")
+        print()
+    if LATEX:
+        print("\hline")
+    if LATEX:
+        print("{:<14}".format("Scenarios:"), end="")
+    else:
+        print("{:<12}".format("Scenarios:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8}".format(" &" if LATEX else "", k),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    if LATEX:
+        print("\hline")
+    print("{:<12}".format("Mean:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8.2f}".format(" &" if LATEX else "", np.mean(v)),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    print("{:<12}".format("0.95 low:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8.2f}".format(" &" if LATEX else "", mean_conf_int(v)[1]),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    print("{:<12}".format("0.95 high:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8.2f}".format(" &" if LATEX else "", mean_conf_int(v)[2]),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    print("{:<12}".format("Median:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8.2f}".format(" &" if LATEX else "", np.median(v)),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    print("{:<12}".format("0.95 perc.:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8.2f}".format(
+                " &" if LATEX else "",
+                np.percentile(v, [95])[0],
+            ),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    print("{:<12}".format("Maximum:"), end="")
+    for k, v in w.items():
+        print(
+            "{} {:<8.2f}".format(" &" if LATEX else "", np.max(v)),
+            end="",
+        )
+    print("\\\\" if LATEX else "")
+    if LATEX:
+        print("\hline")
+    if LATEX:
+        print("\\end{tabular}")
+        print("\\caption{", end="")
+        print("{}".format(t), end="")
+        print("}")
+        print("\\end{table*}")
+
+def error_infoprint(w={}, t=""):
+    """Print error information about scenario results.
+
+    Keyword arguments:
+    w -- What to print.
+    t -- Print title.
+    """
+    print(t)
+    w = w[0]
+    print("{:<12}".format("Scenarios:"), end="")
+    for k, v in w.items():
+        print(" {:<8}".format(k), end="")
+    print()
+    print("{:<12}".format("Rate:"), end="")
+    for k, v in w.items():
+        print(" {:<8.2f}".format(v), end="")
+    print()
+
+if __name__ == "__main__":
+    if len(sys.argv) > 1:
+        w = sys.argv[1]
+    else:
+        w = "time"
+
+    plt.rcParams["font.size"] = 22
+    plt.rcParams["font.family"] = "sans-serif"
+    plt.rcParams["figure.figsize"] = [12, 4]
+
+    if w == "time":
+        infoprint(scenario.time(), "Elapsed time")
+    elif w == "cost":
+        infoprint(scenario.cost(), "Final path cost")
+    elif w == "orig_cost":
+        infoprint(scenario.orig_cost(), "Original path cost")
+    elif w == "cusp":
+        infoprint(scenario.cusp(), "Changes in direction")
+    elif w == "error":
+        error_infoprint(scenario.error_rate(), "Error rate")
+    elif w == "iter":
+        infoprint(
+            scenario.iter(),
+            "Number of iterations",
+        )
+    else:
+        print("""The following arguments are allowed:
+
+        time, cost, orig_cost, cusp, error, iter
+        """)