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
10 LOGSF=["T2","T3", "Klemm2015"]
14 LOGSF[2]: "Klemm2015"}
16 LEG[LOGSF[0]]: "orange",
18 LEG[LOGSF[2]]: "blue"}
20 def load_trajectory(fname):
21 """Load trajectory from file.
24 fname -- The file name.
27 raise ValueError("File name as argument needed")
28 with open(fname, "r") as f:
29 trajectory = loads(f.read())
32 def load_trajectories(dname):
33 """Load trajectories from directory.
36 dname -- The directory name.
39 raise ValueError("Directory name as argument needed")
41 for f in listdir(dname):
42 trajectories.append(load_trajectory("{}/{}".format(dname, f)))
47 plt.rcParams["font.size"] = 24
49 ax = fig.add_subplot(111)
50 #ax.set_aspect("equal")
52 #ax.set_title("TITLE")
53 #ax.set_xlabel("Time [s]")
54 #ax.set_ylabel("YLABEL")
58 #plt.plot(xcoords, ycoords, color="blue", label="LABEL")
59 #plt.hist(vals) # log=?, range=[?], bins=?
60 #ax.bar(xvals, yvals) # width=?
61 #ax.set_xticklabels(xvals, rotation=90)
64 #plt.savefig("WHATEVER")
66 def count_if_exist(trajectories, what):
67 """From multiple trajectories compute the number of occurences.
70 trajectories -- The list of trajectories.
71 what -- Number of occurences of what to compute.
74 for t in trajectories:
82 def get_lasts_if_exist(trajectories, what):
83 """From multiple trajectories get the list of last values.
86 trajectories -- The list of trajectories.
87 what -- The last values of what to take.
90 for t in trajectories:
92 val.append(t[what][-1])
97 def get_maxs_if_exist(trajectories, what):
98 """From multiple trajectories get the list of maximum values.
101 trajectories -- The list of trajectories.
102 what -- The maximum values of what to take.
105 for t in trajectories:
107 val.append(max(t[what]))
112 def plot_successrate():
113 """Plot success rate of single/multi-core implementations."""
117 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
121 v[a] = (100 * count_if_exist(r[a], "traj") /
122 count_if_exist(r[a], "elap"))
123 vs = sorted(v.items(), key=lambda kv: kv[1])
125 plt.rcParams["font.size"] = 24
127 ax = fig.add_subplot(111)
128 ax.set_title("Success rate of trajectories found within 10 seconds limit")
130 ax.set_ylabel("Framework tested [-]")
131 ax.set_xlabel("Found successfully [%]")
133 ax.set_yticklabels([])
136 print("{}: {}%".format(k, i))
137 ax.barh(j, float(i), 4, label=k)
143 def plot_mintrajcost():
144 """Plot minimum trajectory cost found."""
150 r["{}, {}, {}".format(ALGT[a], STT[st], COT[co])] = (
151 load_trajectories("{}/{}st{}co{}_lpar".format(
156 v[a] = get_lasts_if_exist(r[a], "cost")
158 #plt.rcParams["font.size"] = 24
160 for i in range(len(r.keys())):
161 ax = fig.add_subplot(3, 1, i + 1)
162 ax.set_title("Minimum trajectory cost found in 10 seconds (sequential)")
164 ax.set_ylabel("Framework tested [-]")
165 ax.set_xlabel("Minimum trajectory cost [m]")
166 #ax.set_yticklabels([])
168 plt.bar(range(len(v[r.keys()[i]])), v[r.keys()[i]], label=r.keys()[i])
175 """Plot distribution of last costs across measured tests."""
179 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
183 v[a] = get_lasts_if_exist(r[a], "cost")
185 plt.rcParams["font.size"] = 24
187 ax = fig.add_subplot(111)
188 ax.set_title("Path cost histogram")
190 ax.set_ylabel("Number of paths with given cost [-]")
191 ax.set_xlabel("Path cost [m]")
193 ax.set_aspect("equal")
196 plt.hist(v[a], alpha=0.5, label=a, bins=100, histtype="step",
199 X_WHERE = np.percentile(v[a], [95])
200 plt.axvline(X_WHERE, lw=1, color=COLS[a], linestyle="--")
208 """Plot time of the last traj (the maximum time)."""
212 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
216 v[a] = get_lasts_if_exist(r[a], "secs")
218 plt.rcParams["font.size"] = 24
220 ax = fig.add_subplot(111)
221 ax.set_title("Path found time histogram")
223 ax.set_ylabel("Number of paths found [-]")
224 ax.set_xlabel("Algorithm elapsed time [s]")
226 ax.set_aspect("equal")
229 plt.hist(v[a], alpha=0.5, label=a, bins=np.arange(0, 10, 0.1),
230 histtype="step", color=COLS[a])
232 X_WHERE = np.percentile(v[a], [95])
233 plt.axvline(X_WHERE, lw=1, color=COLS[a], linestyle="--")
240 def print_nofnodes():
241 """Print average number of nodes."""
245 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
249 lasts = get_lasts_if_exist(r[a], "node")
250 v[a] = np.average(lasts)
252 print("Average number of points:")
254 print("{}: {}".format(a, v[a]))
256 def print_successrate():
257 """Print success rate of implementations."""
260 r["{}".format(LEG[sf])] = load_trajectories("{}/{}".format(LOGF, sf))
264 v[a] = (100.0 * count_if_exist(r[a], "traj") /
265 count_if_exist(r[a], "elap"))
267 print("Success rate:")
269 print("{}: {}".format(a, v[a]))
271 if __name__ == "__main__":