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


hpp-fcl
Author(s):
autogenerated on Fri Aug 2 2024 02:45:14