obb.py
Go to the documentation of this file.
1 import matplotlib.pyplot as plt
2 import csv, sys, numpy as np
3 from math import sqrt
4 
5 filename = sys.argv[1]
6 
7 with open(filename, "r") as file:
8  reader = csv.reader(file, strict=True)
9  fieldnames = None
10  # fieldnames = reader.fieldnames
11  for row in reader:
12  if fieldnames is None:
13  fieldnames = [n.strip() for n in row]
14  values = [[] for _ in fieldnames]
15  continue
16 
17  values[0].append(int(row[0]))
18  for i, v in enumerate(row[1:]):
19  values[i + 1].append(float(v))
20 
21 # Compute mean and variance for each values, for each separating axis
22 means = [
23  [
24  0.0,
25  ]
26  * 12
27  for _ in fieldnames[4:]
28 ]
29 stddevs = [
30  [
31  0.0,
32  ]
33  * 12
34  for _ in fieldnames[4:]
35 ]
36 nb_occurence = [
37  0,
38 ] * 12
39 
40 for i, id in enumerate(values[0]):
41  nb_occurence[id] += 1
42 
43 for i, id in enumerate(values[0]):
44  for k, n in enumerate(fieldnames[4:]):
45  v = values[k + 4][i]
46  means[k][id] += v / nb_occurence[id]
47  stddevs[k][id] += v * v / nb_occurence[id]
48 
49 for k, n in enumerate(fieldnames[4:]):
50  for id in range(12):
51  # means [k][id] /= nb_occurence[id]
52  # stddevs[k][id] = sqrt (stddevs[k][id]) / nb_occurence[id] - means[k][id])
53  stddevs[k][id] = sqrt(stddevs[k][id] - means[k][id] * means[k][id])
54 
55 subplots = False
56 Nrows = 1
57 Ncols = 3
58 iplot = 1
59 time_vs_sep_axis = True
60 nb_occ_sep_axis = False
61 avg_time_vs_impl = True
62 
63 if time_vs_sep_axis:
64  if subplots:
65  plt.subplot(Nrows, Ncols, iplot)
66  else:
67  plt.figure(iplot)
68  plt.title("Time, with std dev, versus separating axis")
69  for k, n in enumerate(fieldnames[4:]):
70  # plt.errorbar ([ np.linspace(0, 11, 12) + shift for shift in np.linspace (-0.2, 0.2, ) ], means[k], stddevs[k], label=n)
71  plt.errorbar(np.linspace(0, 11, 12), means[k], stddevs[k], label=n)
72  # plt.errorbar (np.linspace(0, 11, 12), means[k], [ [ 0 ] * len(stddevs[k]), stddevs[k] ], label=n)
73  plt.xlim([-0.5, 11.5])
74  plt.ylabel("Time (ns)")
75  plt.xlabel("Separating axis")
76  plt.legend(loc="upper left")
77 
78  axx = plt.gca().twinx()
79  axx.hist(
80  values[0],
81  bins=[i - 0.5 for i in range(13)],
82  bottom=-0.5,
83  cumulative=True,
84  rwidth=0.5,
85  fill=False,
86  label="Cumulative occurence",
87  )
88  axx.set_ylabel("Nb occurence of a separating axis.")
89  plt.legend(loc="lower right")
90 
91 iplot += 1
92 if nb_occ_sep_axis:
93  if subplots:
94  plt.subplot(Nrows, Ncols, iplot)
95  else:
96  plt.figure(iplot)
97  plt.title("Nb of occurence per separating axis")
98  plt.hist(values[0], bins=[i - 0.5 for i in range(13)])
99  plt.ylabel("Nb occurence")
100  plt.xlabel("Separating axis")
101  dlb_id = 1
102  d_id = 2
103  # plt.title ("Time, with std dev, versus distance")
104  # for k, n in enumerate(fieldnames[4:]):
105  # plt.plot (values[dlb_id], values[k+4], '.', label=n)
106 
107 iplot += 1
108 if avg_time_vs_impl:
109  if subplots:
110  plt.subplot(Nrows, Ncols, iplot)
111  else:
112  plt.figure(iplot)
113  plt.title("Average time versus the implementation")
114  # plt.boxplot(values[4:], labels=fieldnames[4:], showmeans=True)
115  _mins = np.min(values[4:], axis=1)
116  _maxs = np.max(values[4:], axis=1)
117  _means = np.mean(values[4:], axis=1)
118  _stddev = np.std(values[4:], axis=1)
119  _sorted = sorted(
120  zip(fieldnames[4:], _means, _stddev, _mins, _maxs), key=lambda x: x[1]
121  )
122  plt.errorbar(
123  [f for f, m, s, l, u in _sorted],
124  [m for f, m, s, l, u in _sorted],
125  [s for f, m, s, l, u in _sorted],
126  fmt="go",
127  linestyle="",
128  label="mean and std deviation",
129  )
130  plt.plot(
131  [f for f, m, s, l, u in _sorted],
132  [l for f, m, s, l, u in _sorted],
133  "b+",
134  ls="",
135  label="min",
136  )
137  plt.plot(
138  [f for f, m, s, l, u in _sorted],
139  [u for f, m, s, l, u in _sorted],
140  "r+",
141  ls="",
142  label="max",
143  )
144  plt.ylabel("Time (ns)")
145  plt.xticks(rotation=20)
146  plt.legend()
147 
148 plt.show()


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:01