Revision e066fa74 fiddle/simulation_analysis/plot_experiment_result.py
fiddle/simulation_analysis/plot_experiment_result.py  

8  8 
import os 
9  9 
import numpy as np 
10  10 
import matplotlib.pyplot as plt 
11 
import matplotlib.axes as axes 

12 
from pdb import set_trace as debugger 

11  13  
12  14 
class ExperimentVisualizer(): 
13  15 
graph_descriptions = { 
14 
"CN": "Community Network", 

16 
"CN": "Synthetic Community Network", 

17 
"CNR": "Real Community Network", 

15  18 
"ER": "Erdos", 
16 
"PL": "Power Lab", 

19 
"PL": "Power Law", 

20 
"FG": "FFG", 

21 
"FW": "FFW", 

22 
"NN": "Ninux", 

23 
} 

24  
25 
devices = { 

26 
"r": "Router", 

27 
"s": "Computer", 

28 
} 

29  
30 
chart_types = { 

31 
"line": "plot_line_chart", 

32 
"bar": "plot_bar_chart", 

33 
"speedup": "plot_speed_up_ratio", 

17  34 
} 
18  35  
19  36 
def __init__(self): 
20  37 
self.args = None 
21 
self.graph_type = None 

38 
self.graph_type = None # will be deduced by the filename


22  39  
23  40 
def parse_args(self): 
24  41 
parser = argparse.ArgumentParser(description="Plotting the comparison \ 
25  42 
between Brandes Betweenness Centrality \ 
26 
(BC) and Heuristic BC (HBC)") 

43 
(BBC) and Heuristic BC (HBC)")


27  44 
parser.add_argument("i", dest="input") 
28  45 
parser.add_argument("o", dest="output", required=False, 
29  46 
default="") 
47 
parser.add_argument("c", dest="charttype", required=True, 

48 
choices=self.chart_types.keys()), 

49 
parser.add_argument("d", dest="device", required=False, 

50 
default="s", choices=self.devices.keys()) 

30  51  
31  52 
self.args = parser.parse_args() 
32  53  
...  ...  
34  55 
input_dir = os.path.dirname(self.args.input) 
35  56 
filename = os.path.splitext(os.path.basename(self.args.input))[0] 
36  57  
37 
self.graph_type = self.graph_descriptions.get(filename, "") 

58 
self.graph_type = self.graph_descriptions.get(filename[0:2], "")


38  59  
39  60 
if self.args.output == "": 
40  61 
self.args.output = '%s/%s_BC_HBC.png' % (input_dir, filename) 
...  ...  
42  63 
plot_data = self.read_plot_data() 
43  64  
44  65 
# Plot 
45 
self.plot_line_graph(plot_data) 

66 
plot_function = getattr(self, self.chart_types[self.args.charttype]) 

67 
print '\n%s\n' % self.args.input 

68 
plot_function(plot_data) 

69  
46  70  
47  71 
def read_plot_data(self): 
48  72 
plot_data = np.loadtxt(self.args.input, 
49  73 
dtype= { 
50 
'names': ('typotype', 'graphtype', 'numofnodes', 'avg_bc', 'avg_hbc', 

74 
'names': ('filename', 'typotype', 'graphtype', 'numofnodes', 'avg_bc', 'avg_hbc',


51  75 
'std_bc', 'std_hbc'), 
52 
'formats': ('a6', 'a2', 'i4', 'f8', 'f8', 'f8', 'f8')


76 
'formats': ('a40', 'a10', 'a2', 'i4', 'f8', 'f8', 'f8', 'f8')


53  77 
}, 
54  78 
delimiter=',' 
55  79 
) 
80  
56  81 
plot_data = plot_data[plot_data['numofnodes'].argsort()] 
57  82 
return plot_data 
58  83  
59  84  
60 
def plot_line_graph(self, plot_data): 

85 
def plot_speed_up_ratio(self, plot_data): 

86 
if plot_data['graphtype'][0] in ['NN', 'FG', 'FW']: 

87 
xlabel = [self.graph_descriptions[term] for term in plot_data['graphtype']] 

88 
self.graph_type = self.graph_descriptions["CNR"] 

89 
x = [1, 2, 3] 

90 
else: 

91 
x = plot_data['numofnodes'] 

92 
xlabel = x 

93 
ys = list() 

94 
ys.append(plot_data['avg_bc'] / plot_data['avg_hbc']) 

95 
ys.append(plot_data['avg_bc'] / plot_data['avg_bc']) 

96  
97 
colors = ['r', 'b'] 

98 
markers = ['^', 'o'] 

99 
labels= ['HBC', 'BBC'] 

100 
size = 100 

101 
fontsize = 18 

102 
for index, y in enumerate(ys): 

103 
plt.scatter(x, y, c=colors[index], marker=markers[index], s=size, label=labels[index]) 

104 
plt.xticks(x, xlabel) 

105 
ymin = plt.gca().get_ylim()[0] 

106 
if ymin < 0: 

107 
plt.ylim(ymin=0) 

108 
plt.tick_params(axis='both', which='major', labelsize=fontsize) 

109 
plt.title("%s on %s" % (self.graph_type, self.devices[self.args.device]), fontsize=fontsize) 

110 
plt.legend(bbox_to_anchor=(1, 0.8)) 

111 
plt.ylabel("Speedup ratio", fontsize=fontsize) 

112  
113 
if self.graph_type != self.graph_descriptions["CNR"]: 

114 
plt.xlabel("# of nodes", fontsize=fontsize) 

115 
plt.savefig(self.args.output) 

116 
print "Finish plotting %s" % self.args.output 

117 
plt.close() 

118  
119  
120 
def plot_line_chart(self, plot_data): 

61  121 
x = plot_data['numofnodes'] 
122 
print 'num of nodes: %s' % x 

62  123 
y1 = plot_data['avg_bc'] 
63  124 
y2 = plot_data['avg_hbc'] 
64  125  
65 
plt.plot(x, y1, marker='s', linestyle='', color='b', label='BC') 

66 
plt.plot(x, y2, marker='o', linestyle='', color='r', label='HBC') 

67 
plt.xlabel("Number of nodes") 

68 
plt.ylabel("Running time (seconds)") 

69 
plt.title("Running time for BC and HBC with %s graphs" % self.graph_type) 

70 
plt.legend() 

126 
plt.plot(x, y1, marker='o', linestyle='', color='b', label='BBC') 

127 
plt.plot(x, y2, marker='^', linestyle='', color='r', label='HBC') 

128  
129 
fontsize = 18 

130 
plt.tick_params(axis='both', which='major', labelsize=fontsize) 

131 
plt.title("%s on %s" % (self.graph_type, self.devices[self.args.device]), fontsize=fontsize) 

132 
plt.legend(bbox_to_anchor=(0.25, 1)) 

133 
plt.xlabel("# of nodes", fontsize=fontsize) 

134 
plt.ylabel("Running time (secs)", fontsize=fontsize) 

135 
plt.savefig(self.args.output) 

136 
print "Finish plotting %s" % self.args.output 

137 
plt.close() 

138  
139 
def plot_bar_chart(self, plot_data): 

140 
x = [self.graph_descriptions[i] for i in plot_data['graphtype']] 

141 
bc = plot_data['avg_bc'] 

142 
hbc = plot_data['avg_hbc'] 

143  
144 
bc_std = plot_data['std_bc'] 

145 
hbc_std = plot_data['std_hbc'] 

146  
147 
N = len(bc_std) 

148 
ind = np.arange(N) 

149 
width = 0.35 

150 
fontsize = 18 

151  
152 
p1 = plt.bar(ind, bc, width, color='b', yerr=bc_std) 

153 
p2 = plt.bar(ind + 0.35, hbc, width, color='r', yerr=hbc_std) 

154 
plt.xticks(ind + width, x) 

155 
plt.ylabel("Running time (secs)", fontsize=fontsize) 

156 
plt.legend((p1[0], p2[0]), ('BBC', 'HBC')) 

157  
158 
title = "Real community networks on %s" % self.devices[self.args.device] 

159 
plt.title(title, fontsize=fontsize) 

160 
plt.tick_params(axis='both', which='major', labelsize=fontsize) 

71  161 
plt.savefig(self.args.output) 
162 
print "Finish plotting %s" % self.args.output 

72  163 
plt.close() 
73  164  
74  165 
Also available in: Unified diff