1 # -*- coding: utf-8 -*-
2 """This module contain functions to ease graph plots."""
4 from matplotlib import pyplot as plt
6 from sys import argv, exit
9 # ed - euclidean distance
10 # rs - reeds and shepp path length
11 # sh - reeds and shepp, same heading
13 # ad - optimize with dijkstra, all nodes
14 # as - optimize with smart, all nodes
15 # ar - optimize with remove redundant points, all nodes
16 # cd - optimize with dijkstra, cusp nodes
17 # cs - optimize with smart, cusp nodes
18 # cr - optimize with remove redundant points, cusp nodes
22 {"f": "rs", "c": "orange", "l": "Reeds and Shepp path length"},
23 {"f": "sh", "c": "blue", "l": "Reeds and Shepp same heading"},
24 {"f": "ed", "c": "red", "l": "Euclidean distance"},
25 # {"f": "ed", "c": "orange", "l": "No optimization"},
26 # {"f": "ar", "c": "green", "l": "Remove redundant points"},
27 # {"f": "as", "c": "blue", "l": "Smart"},
28 # {"f": "cd", "c": "red", "l": "Dijkstra on cusp nodes"},
33 def load_trajectory(fname):
34 """Load trajectory from file.
37 fname -- The file name.
40 raise ValueError("File name as argument needed")
41 with open(fname, "r") as f:
42 trajectory = loads(f.read())
45 def load_trajectories(dname):
46 """Load trajectories from directory.
49 dname -- The directory name.
52 raise ValueError("Directory name as argument needed")
54 for f in listdir(dname):
55 trajectories.append(load_trajectory("{}/{}".format(dname, f)))
59 """Plot graph. Template."""
60 plt.rcParams["font.size"] = 24
62 ax = fig.add_subplot(111)
63 #ax.set_aspect("equal")
65 #ax.set_title("TITLE")
66 #ax.set_xlabel("Time [s]")
67 #ax.set_ylabel("YLABEL")
71 #plt.plot(xcoords, ycoords, color="blue", label="LABEL")
72 #plt.hist(vals) # log=?, range=[?], bins=?
73 #ax.bar(xvals, yvals) # width=?
74 #ax.set_xticklabels(xvals, rotation=90)
77 #plt.savefig("WHATEVER")
79 def count_if_exist(trajectories, what):
80 """From multiple trajectories compute the number of occurences.
83 trajectories -- The list of trajectories.
84 what -- Number of occurences of what to compute.
87 for t in trajectories:
95 def get_lasts_if_exist(trajectories, what):
96 """From multiple trajectories get the list of last values.
99 trajectories -- The list of trajectories.
100 what -- The last values of what to take.
103 for t in trajectories:
105 val.append(t[what][-1])
110 def get_maxs_if_exist(trajectories, what):
111 """From multiple trajectories get the list of maximum values.
114 trajectories -- The list of trajectories.
115 what -- The maximum values of what to take.
118 for t in trajectories:
120 val.append(max(t[what]))
126 """Plot distribution of last costs across measured tests."""
129 v[a] = get_lasts_if_exist(r[a], "cost")
132 ax = fig.add_subplot(111)
133 ax.set_title("Path cost histogram")
135 ax.set_ylabel("Number of paths with given cost [-]")
136 ax.set_xlabel("Path cost [m]")
148 X_WHERE = np.percentile(v[a["f"]], [95])
149 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
157 """Plot time of the last traj (the maximum time)."""
160 v[a] = get_lasts_if_exist(r[a], "secs")
163 ax = fig.add_subplot(111)
164 ax.set_title("Histogram of time to find the path")
166 ax.set_ylabel("Number of paths found [-]")
167 ax.set_xlabel("Algorithm computation time [s]")
175 bins = np.arange(0, 10, 0.1),
179 X_WHERE = np.percentile(v[a["f"]], [95])
180 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
187 def print_nofnodes():
188 """Print average number of nodes."""
191 lasts = get_lasts_if_exist(r[a], "node")
192 v[a] = np.average(lasts)
194 print("Average number of nodes:")
196 print("{}: {}".format(a["f"], v[a["f"]]))
198 def print_successrate():
199 """Print success rate of implementations."""
202 v[a] = (100.0 * count_if_exist(r[a], "traj") /
203 count_if_exist(r[a], "elap"))
205 print("Success rate:")
207 print("{}: {}".format(a["f"], v[a["f"]]))
209 if __name__ == "__main__":
210 plt.rcParams["font.size"] = 29
212 for sf in [i["f"] for i in LOG]:
213 r[sf] = load_trajectories("{}/{}".format(LOGF, sf))