--- /dev/null
+"""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
+ """)