]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - scripts/print.py
Add arc len computation method to point
[hubacji1/iamcar2.git] / scripts / print.py
1 #!/usr/bin/env python3
2 """Procedures for printing scenario results."""
3 import sys
4 import matplotlib.pyplot as plt
5 import numpy as np
6 import scipy.stats as ss
7
8 import scenario
9
10 LATEX = False
11
12 def mean_conf_int(data, conf=0.95):
13     """Return (mean, lower, uppper) of data.
14
15     see https://stackoverflow.com/questions/15033511/compute-a-confidence-interval-from-sample-data
16
17     Keyword arguments:
18     data -- A list of data.
19     conf -- Confidence interval.
20     """
21     a = np.array(data)
22     n = len(a)
23     m = np.mean(a)
24     se = ss.sem(a)
25     h = se * ss.t.ppf((1 + conf) / 2, n - 1)
26     return (m, m - h, m + h)
27
28 def infoprint(w={}, t=""):
29     """Print statistic information about scenario results.
30
31     Keyword arguments:
32     w -- What to print.
33     t -- Print title.
34     """
35     if LATEX:
36         print("\\begin{table*}[h]")
37     else:
38         print(t)
39     if LATEX:
40         print("\\begin{tabular}{", end="")
41         print("|c|", end="")
42         for k, v in w.items():
43             print("c|", end="")
44         print("}", end="")
45         print()
46     if LATEX:
47         print("\hline")
48     if LATEX:
49         print("{:<14}".format("Scenarios:"), end="")
50     else:
51         print("{:<12}".format("Scenarios:"), end="")
52     for k, v in w.items():
53         print(
54             "{} {:<6}".format(" &" if LATEX else "", k.replace("test", "")),
55             end="",
56         )
57     print("\\\\" if LATEX else "")
58     if LATEX:
59         print("\hline")
60     print("{:<12}".format("Mean:"), end="")
61     for k, v in w.items():
62         w[k] = [i*1000.0 for i in v]
63     for k, v in w.items():
64         print(
65             "{} {:<6.2f}".format(" &" if LATEX else "", np.mean(v)),
66             end="",
67         )
68     print("\\\\" if LATEX else "")
69     print("{:<12}".format("0.95 low:"), end="")
70     for k, v in w.items():
71         print(
72             "{} {:<6.2f}".format(" &" if LATEX else "", mean_conf_int(v)[1]),
73             end="",
74         )
75     print("\\\\" if LATEX else "")
76     print("{:<12}".format("0.95 high:"), end="")
77     for k, v in w.items():
78         print(
79             "{} {:<6.2f}".format(" &" if LATEX else "", mean_conf_int(v)[2]),
80             end="",
81         )
82     print("\\\\" if LATEX else "")
83     print("{:<12}".format("Median:"), end="")
84     for k, v in w.items():
85         print(
86             "{} {:<6.2f}".format(" &" if LATEX else "", np.median(v)),
87             end="",
88         )
89     print("\\\\" if LATEX else "")
90     print("{:<12}".format("0.95 perc.:"), end="")
91     for k, v in w.items():
92         print(
93             "{} {:<6.2f}".format(
94                 " &" if LATEX else "",
95                 np.percentile(v, [95])[0],
96             ),
97             end="",
98         )
99     print("\\\\" if LATEX else "")
100     print("{:<12}".format("Minimum:"), end="")
101     for k, v in w.items():
102         print(
103             "{} {:<6.2f}".format(" &" if LATEX else "", np.min(v)),
104             end="",
105         )
106     print("\\\\" if LATEX else "")
107     print("{:<12}".format("Maximum:"), end="")
108     for k, v in w.items():
109         print(
110             "{} {:<6.2f}".format(" &" if LATEX else "", np.max(v)),
111             end="",
112         )
113     print("\\\\" if LATEX else "")
114     if LATEX:
115         print("\hline")
116     if LATEX:
117         print("\\end{tabular}")
118         print("\\caption{", end="")
119         print("{}".format(t), end="")
120         print("}")
121         print("\\end{table*}")
122
123 def error_infoprint(w={}, t=""):
124     """Print error information about scenario results.
125
126     Keyword arguments:
127     w -- What to print.
128     t -- Print title.
129     """
130     print(t)
131     w = w[0]
132     print("{:<12}".format("Scenarios:"), end="")
133     for k, v in w.items():
134         print(" {:<8}".format(k), end="")
135     print()
136     print("{:<12}".format("Rate:"), end="")
137     for k, v in w.items():
138         print(" {:<8.2f}".format(v), end="")
139     print()
140
141 if __name__ == "__main__":
142     if len(sys.argv) > 1:
143         w = sys.argv[1]
144     else:
145         w = "time"
146     if len(sys.argv) > 2:
147         scenario.DNAME = sys.argv[2]
148
149     plt.rcParams["font.size"] = 22
150     plt.rcParams["font.family"] = "sans-serif"
151     plt.rcParams["figure.figsize"] = [12, 4]
152
153     if w == "time":
154         infoprint(scenario.time(), "Elapsed time")
155     elif w == "otime":
156         infoprint(scenario.otime(), "Elapsed time")
157     elif w == "cost":
158         infoprint(scenario.cost(), "Final path cost")
159     elif w == "orig_cost":
160         infoprint(scenario.orig_cost(), "Original path cost")
161     elif w == "cusp":
162         infoprint(scenario.cusp(), "Changes in direction")
163     elif w == "orig_cusp":
164         infoprint(scenario.orig_cusp(), "Changes in direction")
165     elif w == "error":
166         error_infoprint(scenario.error_rate(), "Error rate")
167     elif w == "iter":
168         infoprint(
169             scenario.iter(),
170             "Number of iterations",
171         )
172     else:
173         print("""The following arguments are allowed:
174
175         time, cost, orig_cost, cusp, orig_cusp, error, iter
176         """)