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 cost"},
23 {"f": "sh", "c": "blue", "l": "Reeds and Shepp same heading cost"},
24 {"f": "ed", "c": "red", "l": "Euclidean distance cost"},
29 def load_trajectory(fname):
30 """Load trajectory from file.
33 fname -- The file name.
36 raise ValueError("File name as argument needed")
37 with open(fname, "r") as f:
38 trajectory = loads(f.read())
41 def load_trajectories(dname):
42 """Load trajectories from directory.
45 dname -- The directory name.
48 raise ValueError("Directory name as argument needed")
50 for f in listdir(dname):
51 trajectories.append(load_trajectory("{}/{}".format(dname, f)))
55 """Plot graph. Template."""
56 plt.rcParams["font.size"] = 24
58 ax = fig.add_subplot(111)
59 #ax.set_aspect("equal")
61 #ax.set_title("TITLE")
62 #ax.set_xlabel("Time [s]")
63 #ax.set_ylabel("YLABEL")
67 #plt.plot(xcoords, ycoords, color="blue", label="LABEL")
68 #plt.hist(vals) # log=?, range=[?], bins=?
69 #ax.bar(xvals, yvals) # width=?
70 #ax.set_xticklabels(xvals, rotation=90)
73 #plt.savefig("WHATEVER")
75 def count_if_exist(trajectories, what):
76 """From multiple trajectories compute the number of occurences.
79 trajectories -- The list of trajectories.
80 what -- Number of occurences of what to compute.
83 for t in trajectories:
91 def get_lasts_if_exist(trajectories, what):
92 """From multiple trajectories get the list of last values.
95 trajectories -- The list of trajectories.
96 what -- The last values of what to take.
99 for t in trajectories:
101 val.append(t[what][-1])
106 def get_maxs_if_exist(trajectories, what):
107 """From multiple trajectories get the list of maximum values.
110 trajectories -- The list of trajectories.
111 what -- The maximum values of what to take.
114 for t in trajectories:
116 val.append(max(t[what]))
122 """Plot distribution of last costs across measured tests."""
125 v[a] = get_lasts_if_exist(r[a], "cost")
127 plt.rcParams["font.size"] = 24
129 ax = fig.add_subplot(111)
130 ax.set_title("Path cost histogram")
132 ax.set_ylabel("Number of paths with given cost [-]")
133 ax.set_xlabel("Path cost [m]")
145 X_WHERE = np.percentile(v[a["f"]], [95])
146 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
154 """Plot time of the last traj (the maximum time)."""
157 v[a] = get_lasts_if_exist(r[a], "secs")
159 plt.rcParams["font.size"] = 24
161 ax = fig.add_subplot(111)
162 ax.set_title("Path found time histogram")
164 ax.set_ylabel("Number of paths found [-]")
165 ax.set_xlabel("Algorithm elapsed time [s]")
173 bins = np.arange(0, 10, 0.1),
177 X_WHERE = np.percentile(v[a["f"]], [95])
178 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
185 def print_nofnodes():
186 """Print average number of nodes."""
189 lasts = get_lasts_if_exist(r[a], "node")
190 v[a] = np.average(lasts)
192 print("Average number of nodes:")
194 print("{}: {}".format(a["f"], v[a["f"]]))
196 def print_successrate():
197 """Print success rate of implementations."""
200 v[a] = (100.0 * count_if_exist(r[a], "traj") /
201 count_if_exist(r[a], "elap"))
203 print("Success rate:")
205 print("{}: {}".format(a["f"], v[a["f"]]))
207 if __name__ == "__main__":
209 for sf in [i["f"] for i in LOG]:
210 r[sf] = load_trajectories("{}/{}".format(LOGF, sf))