]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - cplot.py
Merge branch 'release/0.7.0'
[hubacji1/iamcar.git] / cplot.py
1 # -*- coding: utf-8 -*-
2 """This scipt loads result JSON and print cost dependent on time.
3
4 Keyword arguments:
5 argv[1] -- 1st file/folder with json trajectory results.
6 argv[2] -- 2nd file/folder with json trajectory results.
7 """
8 from json import loads
9 from math import cos, pi, sin
10 from matplotlib import pyplot as plt
11 from os import listdir
12 from sys import argv, exit
13
14 YAX = "cost"
15 YAX_LOG = False
16 YAX_ASP = False
17 ELAP_HIST_MAX = 10
18
19 if len(argv) != 3:
20     print("Exactly 2 arguments needed.")
21     exit(1)
22
23 TRAJ_FILE = argv[1]
24 TRAJ_FILE2 = argv[2]
25
26 def load_trajectory(fname):
27     """Load trajectory from file."""
28     if fname is None:
29         raise ValueError("File name as argument needed")
30     with open(fname, "r") as f:
31         trajectory = loads(f.read())
32         return trajectory
33
34 def plot_nodes(nodes=[]):
35     """Return xcoords, ycoords of nodes to plot.
36
37     Keyword arguments:
38     nodes -- The list of nodes to plot.
39     """
40     xcoords = []
41     ycoords = []
42     for n in nodes:
43         xcoords.append(n[0])
44         ycoords.append(n[1])
45     return (xcoords, ycoords)
46
47 if __name__ == "__main__":
48     nodes = []
49     podes = []
50     nhist = []
51     phist = []
52     # blue nodes (not parallel)
53     try:
54         for f in listdir(TRAJ_FILE):
55             try:
56                 t = load_trajectory("{}/{}".format(TRAJ_FILE, f))
57                 for p in zip(t["secs"], t[YAX]):
58                     nodes.append(p)
59                 nhist.append(t["elap"])
60             except:
61                 pass
62     except:
63         try:
64             t = load_trajectory(TRAJ_FILE) # fixed to trajectories
65             for p in zip(t["secs"], t[YAX]):
66                 nodes.append(p)
67             nhist.append(t["elap"])
68         except:
69             print("Failed loading 1st")
70             exit(1)
71     # red nodes (parallel)
72     try:
73         for f in listdir(TRAJ_FILE2):
74             try:
75                 t = load_trajectory("{}/{}".format(TRAJ_FILE2, f))
76                 for p in zip(t["secs"], t[YAX]):
77                     podes.append(p)
78                 phist.append(t["elap"])
79             except:
80                 pass
81     except:
82         try:
83             t = load_trajectory(TRAJ_FILE2) # fixed to trajectories
84             for p in zip(t["secs"], t[YAX]):
85                 podes.append(p)
86             phist.append(t["elap"])
87         except:
88             print("Failed loading 2nd")
89             exit(1)
90
91     fig = plt.figure()
92     ax = fig.add_subplot(111)
93     if YAX_ASP:
94         ax.set_aspect("equal")
95     if YAX_LOG:
96         ax.set_yscale("log")
97     ax.set_title("SCENARIO")
98     ax.set_xlabel("Time [s]")
99     if YAX is "cost":
100         ax.set_ylabel("Cost (Euclidean length) [m]")
101     elif YAX is "node":
102         ax.set_ylabel("Number of nodes [-]")
103     #ax.set_xlim(0, 60)
104     #ax.set_ylim(0, 100)
105
106     # plot here
107     nodes.sort()
108     podes.sort()
109     try:
110         plt.plot(*plot_nodes(nodes), color="blue")
111         plt.plot(*plot_nodes(podes), color="red")
112     except:
113         pass
114     # end plot here
115
116     plt.show()
117     #plt.savefig("{}_{}.png".format(argv[1], argv[2]))
118     plt.close(fig)
119
120 if ELAP_HIST_MAX > 0:
121     plt.hist(nhist, log=True, range=[0, ELAP_HIST_MAX], bins=100)
122     plt.show()
123     plt.hist(phist, log=True, range=[0, ELAP_HIST_MAX], bins=100)
124     plt.show()