process_shonan_timing_results.py
Go to the documentation of this file.
1 """
2 Process timing results from timeShonanAveraging
3 """
4 
5 import xlrd
6 import numpy as np
7 import matplotlib.pyplot as plt
8 from matplotlib.ticker import FuncFormatter
9 import heapq
10 from collections import Counter
11 
12 def make_combined_plot(name, p_values, times, costs, min_cost_range=10):
13  """ Make a plot that combines timing and SO(3) cost.
14  Arguments:
15  name: string of the plot title
16  p_values: list of p-values (int)
17  times: list of timings (seconds)
18  costs: list of costs (double)
19  Will calculate the range of the costs, default minimum range = 10.0
20  """
21  min_cost = min(costs)
22  cost_range = max(max(costs)-min_cost,min_cost_range)
23  fig = plt.figure()
24  ax1 = fig.add_subplot(111)
25  ax1.plot(p_values, times, label="time")
26  ax1.set_ylabel('Time used to optimize \ seconds')
27  ax1.set_xlabel('p_value')
28  ax2 = ax1.twinx()
29  ax2.plot(p_values, costs, 'r', label="cost")
30  ax2.set_ylabel('Cost at SO(3) form')
31  ax2.set_xlabel('p_value')
32  ax2.set_xticks(p_values)
33  ax2.set_ylim(min_cost, min_cost + cost_range)
34  plt.title(name, fontsize=12)
35  ax1.legend(loc="upper left")
36  ax2.legend(loc="upper right")
37  plt.interactive(False)
38  plt.show()
39 
40 def make_convergence_plot(name, p_values, times, costs, iter=10):
41  """ Make a bar that show the success rate for each p_value according to whether the SO(3) cost converges
42  Arguments:
43  name: string of the plot title
44  p_values: list of p-values (int)
45  times: list of timings (seconds)
46  costs: list of costs (double)
47  iter: int of iteration number for each p_value
48  """
49 
50  max_cost = np.mean(np.array(heapq.nlargest(iter, costs)))
51  # calculate mean costs for each p value
52  p_values = list(dict(Counter(p_values)).keys())
53  # make sure the iter number
54  iter = int(len(times)/len(p_values))
55  p_mean_cost = [np.mean(np.array(costs[i*iter:(i+1)*iter])) for i in range(len(p_values))]
56  p_max = p_values[p_mean_cost.index(max(p_mean_cost))]
57  # print(p_mean_cost)
58  # print(p_max)
59 
60  #take mean and make the combined plot
61  mean_times = []
62  mean_costs = []
63  for p in p_values:
64  costs_tmp = costs[p_values.index(p)*iter:(p_values.index(p)+1)*iter]
65  mean_cost = sum(costs_tmp)/len(costs_tmp)
66  mean_costs.append(mean_cost)
67  times_tmp = times[p_values.index(p)*iter:(p_values.index(p)+1)*iter]
68  mean_time = sum(times_tmp)/len(times_tmp)
69  mean_times.append(mean_time)
70  make_combined_plot(name, p_values,mean_times, mean_costs)
71 
72  # calculate the convergence rate for each p_value
73  p_success_rates = []
74  if p_mean_cost[0] >= 0.95*np.mean(np.array(costs)) and p_mean_cost[0] <= 1.05*np.mean(np.array(costs)):
75  p_success_rates = [ 1.0 for p in p_values]
76  else:
77  for p in p_values:
78  if p > p_max:
79  p_costs = costs[p_values.index(p)*iter:(p_values.index(p)+1)*iter]
80  # print(p_costs)
81  converged = [ int(p_cost < 0.3*max_cost) for p_cost in p_costs]
82  success_rate = sum(converged)/len(converged)
83  p_success_rates.append(success_rate)
84  else:
85  p_success_rates.append(0)
86 
87  plt.bar(p_values, p_success_rates, align='center', alpha=0.5)
88  plt.xticks(p_values)
89  plt.yticks(np.arange(0, 1.2, 0.2), ['0%', '20%', '40%', '60%', '80%', '100%'])
90  plt.xlabel("p_value")
91  plt.ylabel("success rate")
92  plt.title(name, fontsize=12)
93  plt.show()
94 
95 def make_eigen_and_bound_plot(name, p_values, times1, costPs, cost3s, times2, min_eigens, subounds):
96  """ Make a plot that combines time for optimizing, time for optimizing and compute min_eigen,
97  min_eigen, subound (subound = (f_R - f_SDP) / f_SDP).
98  Arguments:
99  name: string of the plot title
100  p_values: list of p-values (int)
101  times1: list of timings (seconds)
102  costPs: f_SDP
103  cost3s: f_R
104  times2: list of timings (seconds)
105  min_eigens: list of min_eigen (double)
106  subounds: list of subound (double)
107  """
108 
109  if dict(Counter(p_values))[5] != 1:
110  p_values = list(dict(Counter(p_values)).keys())
111  iter = int(len(times1)/len(p_values))
112  p_mean_times1 = [np.mean(np.array(times1[i*iter:(i+1)*iter])) for i in range(len(p_values))]
113  p_mean_times2 = [np.mean(np.array(times2[i*iter:(i+1)*iter])) for i in range(len(p_values))]
114  print("p_values \n", p_values)
115  print("p_mean_times_opti \n", p_mean_times1)
116  print("p_mean_times_eig \n", p_mean_times2)
117 
118  p_mean_costPs = [np.mean(np.array(costPs[i*iter:(i+1)*iter])) for i in range(len(p_values))]
119  p_mean_cost3s = [np.mean(np.array(cost3s[i*iter:(i+1)*iter])) for i in range(len(p_values))]
120  p_mean_lambdas = [np.mean(np.array(min_eigens[i*iter:(i+1)*iter])) for i in range(len(p_values))]
121 
122  print("p_mean_costPs \n", p_mean_costPs)
123  print("p_mean_cost3s \n", p_mean_cost3s)
124  print("p_mean_lambdas \n", p_mean_lambdas)
125  print("*******************************************************************************************************************")
126 
127 
128  else:
129  plt.figure(1)
130  plt.ylabel('Time used (seconds)')
131  plt.xlabel('p_value')
132  plt.plot(p_values, times1, 'r', label="time for optimizing")
133  plt.plot(p_values, times2, 'blue', label="time for optimizing and check")
134  plt.title(name, fontsize=12)
135  plt.legend(loc="best")
136  plt.interactive(False)
137  plt.show()
138 
139  plt.figure(2)
140  plt.ylabel('Min eigen_value')
141  plt.xlabel('p_value')
142  plt.plot(p_values, min_eigens, 'r', label="min_eigen values")
143  plt.title(name, fontsize=12)
144  plt.legend(loc="best")
145  plt.interactive(False)
146  plt.show()
147 
148  plt.figure(3)
149  plt.ylabel('sub_bounds')
150  plt.xlabel('p_value')
151  plt.plot(p_values, subounds, 'blue', label="sub_bounds")
152  plt.title(name, fontsize=12)
153  plt.legend(loc="best")
154  plt.show()
155 
156 # Process arguments and call plot function
157 import argparse
158 import csv
159 import os
160 
161 parser = argparse.ArgumentParser()
162 parser.add_argument("path")
163 args = parser.parse_args()
164 
165 
166 file_path = []
167 domain = os.path.abspath(args.path)
168 for info in os.listdir(args.path):
169  file_path.append(os.path.join(domain, info))
170 file_path.sort()
171 print(file_path)
172 
173 
174 # name of all the plots
175 names = {}
176 names[0] = 'tinyGrid3D vertex = 9, edge = 11'
177 names[1] = 'smallGrid3D vertex = 125, edge = 297'
178 names[2] = 'parking-garage vertex = 1661, edge = 6275'
179 names[3] = 'sphere2500 vertex = 2500, edge = 4949'
180 # names[4] = 'sphere_bignoise vertex = 2200, edge = 8647'
181 names[5] = 'torus3D vertex = 5000, edge = 9048'
182 names[6] = 'cubicle vertex = 5750, edge = 16869'
183 names[7] = 'rim vertex = 10195, edge = 29743'
184 
185 # Parse CSV file
186 for key, name in names.items():
187  print(key, name)
188 
189  # find according file to process
190  name_file = None
191  for path in file_path:
192  if name[0:3] in path:
193  name_file = path
194  if name_file == None:
195  print("The file %s is not in the path" % name)
196  continue
197 
198  p_values, times1, costPs, cost3s, times2, min_eigens, subounds = [],[],[],[],[],[],[]
199  with open(name_file) as csvfile:
200  reader = csv.reader(csvfile, delimiter='\t')
201  for row in reader:
202  print(row)
203  p_values.append(int(row[0]))
204  times1.append(float(row[1]))
205  costPs.append(float(row[2]))
206  cost3s.append(float(row[3]))
207  if len(row) > 4:
208  times2.append(float(row[4]))
209  min_eigens.append(float(row[5]))
210  subounds.append(float(row[6]))
211 
212  #plot
213  # make_combined_plot(name, p_values, times1, cost3s)
214  # make_convergence_plot(name, p_values, times1, cost3s)
215  make_eigen_and_bound_plot(name, p_values, times1, costPs, cost3s, times2, min_eigens, subounds)
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
def make_convergence_plot(name, p_values, times, costs, iter=10)
#define max(a, b)
Definition: datatypes.h:20
return int(ret)+1
def make_eigen_and_bound_plot(name, p_values, times1, costPs, cost3s, times2, min_eigens, subounds)
#define min(a, b)
Definition: datatypes.h:19
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
Definition: pytypes.h:1301
Definition: pytypes.h:1255
const KeyVector keys
size_t len(handle h)
Definition: pytypes.h:1514
def make_combined_plot(name, p_values, times, costs, min_cost_range=10)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:29