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
11 {"f": "rs", "c": "orange", "l": "Reeds and Shepp path length cost"},
12 {"f": "sh", "c": "blue", "l": "Reeds and Shepp same heading cost"},
13 {"f": "ed", "c": "red", "l": "Euclidean distance cost"},
18 def load_trajectory(fname):
19 """Load trajectory from file.
22 fname -- The file name.
25 raise ValueError("File name as argument needed")
26 with open(fname, "r") as f:
27 trajectory = loads(f.read())
30 def load_trajectories(dname):
31 """Load trajectories from directory.
34 dname -- The directory name.
37 raise ValueError("Directory name as argument needed")
39 for f in listdir(dname):
40 trajectories.append(load_trajectory("{}/{}".format(dname, f)))
45 plt.rcParams["font.size"] = 24
47 ax = fig.add_subplot(111)
48 #ax.set_aspect("equal")
50 #ax.set_title("TITLE")
51 #ax.set_xlabel("Time [s]")
52 #ax.set_ylabel("YLABEL")
56 #plt.plot(xcoords, ycoords, color="blue", label="LABEL")
57 #plt.hist(vals) # log=?, range=[?], bins=?
58 #ax.bar(xvals, yvals) # width=?
59 #ax.set_xticklabels(xvals, rotation=90)
62 #plt.savefig("WHATEVER")
64 def count_if_exist(trajectories, what):
65 """From multiple trajectories compute the number of occurences.
68 trajectories -- The list of trajectories.
69 what -- Number of occurences of what to compute.
72 for t in trajectories:
80 def get_lasts_if_exist(trajectories, what):
81 """From multiple trajectories get the list of last values.
84 trajectories -- The list of trajectories.
85 what -- The last values of what to take.
88 for t in trajectories:
90 val.append(t[what][-1])
95 def get_maxs_if_exist(trajectories, what):
96 """From multiple trajectories get the list of maximum values.
99 trajectories -- The list of trajectories.
100 what -- The maximum values of what to take.
103 for t in trajectories:
105 val.append(max(t[what]))
110 def plot_successrate():
111 """Plot success rate of single/multi-core implementations."""
115 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
119 v[a] = (100 * count_if_exist(r[a], "traj") /
120 count_if_exist(r[a], "elap"))
121 vs = sorted(v.items(), key=lambda kv: kv[1])
123 plt.rcParams["font.size"] = 24
125 ax = fig.add_subplot(111)
126 ax.set_title("Success rate of trajectories found within 10 seconds limit")
128 ax.set_ylabel("Framework tested [-]")
129 ax.set_xlabel("Found successfully [%]")
131 ax.set_yticklabels([])
134 print("{}: {}%".format(k, i))
135 ax.barh(j, float(i), 4, label=k)
141 def plot_mintrajcost():
142 """Plot minimum trajectory cost found."""
148 r["{}, {}, {}".format(ALGT[a], STT[st], COT[co])] = (
149 load_trajectories("{}/{}st{}co{}_lpar".format(
154 v[a] = get_lasts_if_exist(r[a], "cost")
156 #plt.rcParams["font.size"] = 24
158 for i in range(len(r.keys())):
159 ax = fig.add_subplot(3, 1, i + 1)
160 ax.set_title("Minimum trajectory cost found in 10 seconds (sequential)")
162 ax.set_ylabel("Framework tested [-]")
163 ax.set_xlabel("Minimum trajectory cost [m]")
164 #ax.set_yticklabels([])
166 plt.bar(range(len(v[r.keys()[i]])), v[r.keys()[i]], label=r.keys()[i])
173 """Plot distribution of last costs across measured tests."""
177 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
181 v[a] = get_lasts_if_exist(r[a], "cost")
183 plt.rcParams["font.size"] = 24
185 ax = fig.add_subplot(111)
186 ax.set_title("Path cost histogram")
188 ax.set_ylabel("Number of paths with given cost [-]")
189 ax.set_xlabel("Path cost [m]")
191 ax.set_aspect("equal")
194 plt.hist(v[a], alpha=0.5, label=a, bins=100, histtype="step",
197 X_WHERE = np.percentile(v[a], [95])
198 plt.axvline(X_WHERE, lw=1, color=COLS[a], linestyle="--")
206 """Plot time of the last traj (the maximum time)."""
209 v[a] = get_lasts_if_exist(r[a], "secs")
211 plt.rcParams["font.size"] = 24
213 ax = fig.add_subplot(111)
214 ax.set_title("Path found time histogram")
216 ax.set_ylabel("Number of paths found [-]")
217 ax.set_xlabel("Algorithm elapsed time [s]")
225 bins = np.arange(0, 10, 0.1),
229 X_WHERE = np.percentile(v[a["f"]], [95])
230 plt.axvline(X_WHERE, lw=1, color=a["c"], linestyle="--")
237 def print_nofnodes():
238 """Print average number of nodes."""
242 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
246 lasts = get_lasts_if_exist(r[a], "node")
247 v[a] = np.average(lasts)
249 print("Average number of points:")
251 print("{}: {}".format(a, v[a]))
253 def print_successrate():
254 """Print success rate of implementations."""
257 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
261 v[a] = (100.0 * count_if_exist(r[a], "traj") /
262 count_if_exist(r[a], "elap"))
264 print("Success rate:")
266 print("{}: {}".format(a, v[a]))
268 if __name__ == "__main__":
270 for sf in [i["f"] for i in LOG]:
271 r[sf] = load_trajectories("{}/{}".format(LOGF, sf))