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]))
125 def get_val_if_exist(trajectories, what):
126 """From m ultiple trajectories get value.
129 trajectories -- The list of trajectories.
130 what -- What to take.
133 for t in trajectories:
141 """Plot distribution of last costs across measured tests."""
144 v[a] = get_lasts_if_exist(r[a], "cost")
147 ax = fig.add_subplot(111)
148 ax.set_title("Path cost histogram")
150 ax.set_ylabel("Number of paths with given cost [-]")
151 ax.set_xlabel("Path cost [m]")
163 X_WHERE = np.percentile(v[a["f"]], [95])
164 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
172 """Plot time of the last traj (the maximum time)."""
175 v[a] = get_lasts_if_exist(r[a], "secs")
178 ax = fig.add_subplot(111)
179 ax.set_title("Histogram of time to find the path")
181 ax.set_ylabel("Number of paths found [-]")
182 ax.set_xlabel("Algorithm computation time [s]")
190 bins = np.arange(0, 10, 0.1),
194 X_WHERE = np.percentile(v[a["f"]], [95])
195 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
202 def plot_nothingdone():
203 """Plot *nothing done* time of ``overlaptrees`` procedure."""
206 v[a] = get_lasts_if_exist(r[a], "nodo")
209 ax = fig.add_subplot(111)
210 ax.set_title("Histogram of nothing-done-time")
212 ax.set_ylabel("Occurences [-]")
213 ax.set_xlabel("Nothing-done-time percentage [-]")
220 bins = np.arange(0, 1, 0.1),
224 X_WHERE = np.percentile(v[a["f"]], [95])
225 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
232 def print_nofnodes():
233 """Print average number of nodes."""
236 lasts = get_lasts_if_exist(r[a], "node")
237 v[a] = np.average(lasts)
239 print("Average number of nodes:")
241 print("{}: {}".format(a["f"], v[a["f"]]))
243 def print_successrate():
244 """Print success rate of implementations."""
247 v[a] = (100.0 * count_if_exist(r[a], "traj") /
248 count_if_exist(r[a], "elap"))
250 print("Success rate:")
252 print("{}: {}".format(a["f"], v[a["f"]]))
254 if __name__ == "__main__":
255 plt.rcParams["font.size"] = 29
257 for sf in [i["f"] for i in LOG]:
258 r[sf] = load_trajectories("{}/{}".format(LOGF, sf))