]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - gplot.py
Add success rate plot for single core
[hubacji1/iamcar.git] / gplot.py
1 # -*- coding: utf-8 -*-
2 """This module contain functions to ease graph plots."""
3 from json import loads
4 from matplotlib import pyplot as plt
5 from os import listdir
6 from sys import argv, exit
7
8 def load_trajectory(fname):
9     """Load trajectory from file.
10
11     Keyword arguments:
12     fname -- The file name.
13     """
14     if fname is None:
15         raise ValueError("File name as argument needed")
16     with open(fname, "r") as f:
17         trajectory = loads(f.read())
18         return trajectory
19
20 def load_trajectories(dname):
21     """Load trajectories from directory.
22
23     Keyword arguments:
24     dname -- The directory name.
25     """
26     if dname is None:
27         raise ValueError("Directory name as argument needed")
28     trajectories = []
29     for f in listdir(dname):
30         trajectories.append(load_trajectory("{}/{}".format(dname, f)))
31     return trajectories
32
33 def gplot():
34     """Plot graph."""
35     fig = plt.figure()
36     ax = fig.add_subplot(111)
37     #ax.set_aspect("equal")
38     #ax.set_yscale("log")
39     #ax.set_title("TITLE")
40     #ax.set_xlabel("Time [s]")
41     #ax.set_ylabel("YLABEL")
42     #ax.set_xlim(0, 100)
43     #ax.set_ylim(0, 100)
44
45     #plt.plot(xcoords, ycoords, color="blue", label="LABEL")
46     #plt.hist(vals) # log=?, range=[?], bins=?
47     #ax.bar(xvals, yvals) # width=?
48     #ax.set_xticklabels(xvals, rotation=90)
49
50     plt.show()
51     #plt.savefig("WHATEVER")
52
53 def count_if_exist(trajectories, what):
54     """From multiple trajectories compute the number of occurences.
55
56     Keyword arguments:
57     trajectories -- The list of trajectories.
58     what -- Number of occurences of what to compute.
59     """
60     occ = 0
61     for t in trajectories:
62         try:
63             if t[what]:
64                 occ += 1
65         except:
66             pass
67     return occ
68
69 if __name__ == "__main__":
70     """Plot success rate of single/multi-core implementations."""
71     ALG=["Karaman2011", "Kuwata2008", "LaValle1998"]
72     ALGT={"Karaman2011": "RRT*",
73             "Kuwata2008": "RRT + heur",
74             "LaValle1998": "RRT"}
75     ST=["3", "4"]
76     STT={"3": "R&S", "4": "Sim"}
77     CO=["1", "2"]
78     COT={"1": "E2", "2": "R&S"}
79
80     r={}
81     for a in ALG:
82         for st in ST:
83             for co in CO:
84                 r["{}, {}, {}".format(ALGT[a], STT[st], COT[co])] = (
85                         load_trajectories("logs_sq/{}st{}co{}_lpar".format(
86                             a, st, co)) +
87                         load_trajectories("logs_sq/{}st{}co{}_rper".format(
88                             a, st, co)))
89
90     v={}
91     for a in r.keys():
92         v[a] = (100 * count_if_exist(r[a], "traj") /
93                 count_if_exist(r[a], "elap"))
94     vs = sorted(v.items(), key=lambda kv: kv[1])
95
96     plt.rcParams["font.size"] = 24
97     fig = plt.figure()
98     ax = fig.add_subplot(111)
99     ax.set_title("Trajectories found in 20 seconds period (sequential)")
100
101     ax.set_ylabel("Framework tested [-]")
102     ax.set_xlabel("Found successfully [%]")
103     ax.set_xlim(0, 100)
104     ax.set_yticklabels([])
105     j = 0
106     for (k, i) in vs:
107         ax.barh(j, float(i), 4, label=k)
108         j += -5
109     ax.legend()
110
111     plt.show()