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 def load_trajectory(fname):
10 """Load trajectory from file.
13 fname -- The file name.
16 raise ValueError("File name as argument needed")
17 with open(fname, "r") as f:
18 trajectory = loads(f.read())
21 def load_trajectories(dname):
22 """Load trajectories from directory.
25 dname -- The directory name.
28 raise ValueError("Directory name as argument needed")
30 for f in listdir(dname):
31 trajectories.append(load_trajectory("{}/{}".format(dname, f)))
36 plt.rcParams["font.size"] = 24
38 ax = fig.add_subplot(111)
39 #ax.set_aspect("equal")
41 #ax.set_title("TITLE")
42 #ax.set_xlabel("Time [s]")
43 #ax.set_ylabel("YLABEL")
47 #plt.plot(xcoords, ycoords, color="blue", label="LABEL")
48 #plt.hist(vals) # log=?, range=[?], bins=?
49 #ax.bar(xvals, yvals) # width=?
50 #ax.set_xticklabels(xvals, rotation=90)
53 #plt.savefig("WHATEVER")
55 def count_if_exist(trajectories, what):
56 """From multiple trajectories compute the number of occurences.
59 trajectories -- The list of trajectories.
60 what -- Number of occurences of what to compute.
63 for t in trajectories:
71 def get_lasts_if_exist(trajectories, what):
72 """From multiple trajectories get the list of last values.
75 trajectories -- The list of trajectories.
76 what -- The last values of what to take.
79 for t in trajectories:
81 val.append(t[what][-1])
86 def get_maxs_if_exist(trajectories, what):
87 """From multiple trajectories get the list of maximum values.
90 trajectories -- The list of trajectories.
91 what -- The maximum values of what to take.
94 for t in trajectories:
96 val.append(max(t[what]))
101 def plot_successrate():
102 """Plot success rate of single/multi-core implementations."""
103 LOGF="logs_T2st3co2_lpar"
104 LOGSF=["opt1_nn3", "opt1_nn4", "opt1_nn4_save-heading"]
106 LOGSF[0]: "T2, st3, co2, nn3",
107 LOGSF[1]: "T2, st3, co2, nn4",
108 LOGSF[2]: "T2, st3, co2, nn4 save heading"}
112 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
116 v[a] = (100 * count_if_exist(r[a], "traj") /
117 count_if_exist(r[a], "elap"))
118 vs = sorted(v.items(), key=lambda kv: kv[1])
120 plt.rcParams["font.size"] = 24
122 ax = fig.add_subplot(111)
123 ax.set_title("Trajectories found in 10 seconds period")
125 ax.set_ylabel("Framework tested [-]")
126 ax.set_xlabel("Found successfully [%]")
128 ax.set_yticklabels([])
131 print("{}: {}%".format(k, i))
132 ax.barh(j, float(i), 4, label=k)
138 def plot_mintrajcost():
139 """Plot minimum trajectory cost found."""
141 ALG=["Karaman2011", "Kuwata2008", "LaValle1998"]
142 ALGT={"Karaman2011": "RRT*",
143 "Kuwata2008": "RRT + heur",
144 "LaValle1998": "RRT"}
154 r["{}, {}, {}".format(ALGT[a], STT[st], COT[co])] = (
155 load_trajectories("{}/{}st{}co{}_lpar".format(
160 v[a] = get_lasts_if_exist(r[a], "cost")
162 #plt.rcParams["font.size"] = 24
164 for i in range(len(r.keys())):
165 ax = fig.add_subplot(3, 1, i + 1)
166 ax.set_title("Minimum trajectory cost found in 10 seconds (sequential)")
168 ax.set_ylabel("Framework tested [-]")
169 ax.set_xlabel("Minimum trajectory cost [m]")
170 #ax.set_yticklabels([])
172 plt.bar(range(len(v[r.keys()[i]])), v[r.keys()[i]], label=r.keys()[i])
179 """Plot distribution of last costs across measured tests."""
180 LOGF="logs_T2st3co2_lpar"
181 LOGSF=["opt0", "opt1_nn4", "opt2_nn4"]
183 LOGSF[0]: "T2, st3, co2, no opt",
184 LOGSF[1]: "T2, st3, co2, Dijkstra opt",
185 LOGSF[2]: "T2, st3, co2, Triangular opt"}
189 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
193 v[a] = get_lasts_if_exist(r[a], "cost")
195 plt.rcParams["font.size"] = 24
197 ax = fig.add_subplot(111)
198 ax.set_title("Trajectories last costs distribution")
201 ax.set_xlabel("Minimum trajectory cost [m]")
202 ax.set_yticklabels([])
206 plt.hist(v[a], label=a, alpha=0.5, bins=200)
212 """Plot time of the last traj (the maximum time)."""
213 LOGF="logs_T2st3co2_lpar"
214 LOGSF=["opt1_nn3", "opt1_nn4", "opt1_nn4_save-heading"]
216 LOGSF[0]: "T2, st3, co2, nn3",
217 LOGSF[1]: "T2, st3, co2, nn4",
218 LOGSF[2]: "T2, st3, co2, nn4 save heading"}
222 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
226 v[a] = get_lasts_if_exist(r[a], "secs")
228 plt.rcParams["font.size"] = 24
230 ax = fig.add_subplot(111)
231 ax.set_title("The last trajectory time")
234 ax.set_xlabel("The last trajectory time [s]")
235 ax.set_yticklabels([])
239 plt.hist(v[a], label=a, alpha=0.5, bins=np.arange(0, 10, 0.05))
244 if __name__ == "__main__":