Statistics
| Branch: | Revision:

root / fiddle / simulation_analysis / plot_experiment_result.py @ e066fa74

1 2 3 4ceb9f92 Quynh PX Nguyen ```#!/usr/bin/env python ``` ```""" ``` ```Input: *.plotdata ``` ```Output: a graph showing the average of BC and HBC ``` ```""" ``` ```import argparse ``` ```import os ``` ```import numpy as np ``` ```import matplotlib.pyplot as plt ``` e066fa74 Quynh PX Nguyen ```import matplotlib.axes as axes ``` ```from pdb import set_trace as debugger ``` 4ceb9f92 Quynh PX Nguyen ```class ExperimentVisualizer(): ``` ``` graph_descriptions = { ``` e066fa74 Quynh PX Nguyen ``` "CN": "Synthetic Community Network", ``` ``` "CNR": "Real Community Network", ``` 4ceb9f92 Quynh PX Nguyen ``` "ER": "Erdos", ``` e066fa74 Quynh PX Nguyen ``` "PL": "Power Law", ``` ``` "FG": "FFG", ``` ``` "FW": "FFW", ``` ``` "NN": "Ninux", ``` ``` } ``` ``` devices = { ``` ``` "r": "Router", ``` ``` "s": "Computer", ``` ``` } ``` ``` chart_types = { ``` ``` "line": "plot_line_chart", ``` ``` "bar": "plot_bar_chart", ``` ``` "speedup": "plot_speed_up_ratio", ``` 4ceb9f92 Quynh PX Nguyen ``` } ``` ``` def __init__(self): ``` ``` self.args = None ``` e066fa74 Quynh PX Nguyen ``` self.graph_type = None # will be deduced by the filename ``` 4ceb9f92 Quynh PX Nguyen ``` def parse_args(self): ``` ``` parser = argparse.ArgumentParser(description="Plotting the comparison \ ``` ``` between Brandes Betweenness Centrality \ ``` e066fa74 Quynh PX Nguyen ``` (BBC) and Heuristic BC (HBC)") ``` 4ceb9f92 Quynh PX Nguyen ``` parser.add_argument("-i", dest="input") ``` ``` parser.add_argument("-o", dest="output", required=False, ``` ``` default="") ``` e066fa74 Quynh PX Nguyen ``` parser.add_argument("-c", dest="charttype", required=True, ``` ``` choices=self.chart_types.keys()), ``` ``` parser.add_argument("-d", dest="device", required=False, ``` ``` default="s", choices=self.devices.keys()) ``` 4ceb9f92 Quynh PX Nguyen ``` self.args = parser.parse_args() ``` ``` def plot(self): ``` ``` input_dir = os.path.dirname(self.args.input) ``` ``` filename = os.path.splitext(os.path.basename(self.args.input))[0] ``` e066fa74 Quynh PX Nguyen ``` self.graph_type = self.graph_descriptions.get(filename[0:2], "") ``` 4ceb9f92 Quynh PX Nguyen ``` if self.args.output == "": ``` ``` self.args.output = '%s/%s_BC_HBC.png' % (input_dir, filename) ``` ``` plot_data = self.read_plot_data() ``` ``` # Plot ``` e066fa74 Quynh PX Nguyen ``` plot_function = getattr(self, self.chart_types[self.args.charttype]) ``` ``` print '\n%s\n' % self.args.input ``` ``` plot_function(plot_data) ``` 4ceb9f92 Quynh PX Nguyen ``` def read_plot_data(self): ``` ``` plot_data = np.loadtxt(self.args.input, ``` ``` dtype= { ``` e066fa74 Quynh PX Nguyen ``` 'names': ('filename', 'typotype', 'graphtype', 'numofnodes', 'avg_bc', 'avg_hbc', ``` 4ceb9f92 Quynh PX Nguyen ``` 'std_bc', 'std_hbc'), ``` e066fa74 Quynh PX Nguyen ``` 'formats': ('a40', 'a10', 'a2', 'i4', 'f8', 'f8', 'f8', 'f8') ``` 4ceb9f92 Quynh PX Nguyen ``` }, ``` ``` delimiter=',' ``` ``` ) ``` e066fa74 Quynh PX Nguyen 4ceb9f92 Quynh PX Nguyen ``` plot_data = plot_data[plot_data['numofnodes'].argsort()] ``` ``` return plot_data ``` e066fa74 Quynh PX Nguyen ``` def plot_speed_up_ratio(self, plot_data): ``` ``` if plot_data['graphtype'][0] in ['NN', 'FG', 'FW']: ``` ``` xlabel = [self.graph_descriptions[term] for term in plot_data['graphtype']] ``` ``` self.graph_type = self.graph_descriptions["CNR"] ``` ``` x = [1, 2, 3] ``` ``` else: ``` ``` x = plot_data['numofnodes'] ``` ``` xlabel = x ``` ``` ys = list() ``` ``` ys.append(plot_data['avg_bc'] / plot_data['avg_hbc']) ``` ``` ys.append(plot_data['avg_bc'] / plot_data['avg_bc']) ``` ``` colors = ['r', 'b'] ``` ``` markers = ['^', 'o'] ``` ``` labels= ['HBC', 'BBC'] ``` ``` size = 100 ``` ``` fontsize = 18 ``` ``` for index, y in enumerate(ys): ``` ``` plt.scatter(x, y, c=colors[index], marker=markers[index], s=size, label=labels[index]) ``` ``` plt.xticks(x, xlabel) ``` ``` ymin = plt.gca().get_ylim()[0] ``` ``` if ymin < 0: ``` ``` plt.ylim(ymin=0) ``` ``` plt.tick_params(axis='both', which='major', labelsize=fontsize) ``` ``` plt.title("%s on %s" % (self.graph_type, self.devices[self.args.device]), fontsize=fontsize) ``` ``` plt.legend(bbox_to_anchor=(1, 0.8)) ``` ``` plt.ylabel("Speed-up ratio", fontsize=fontsize) ``` ``` if self.graph_type != self.graph_descriptions["CNR"]: ``` ``` plt.xlabel("# of nodes", fontsize=fontsize) ``` ``` plt.savefig(self.args.output) ``` ``` print "Finish plotting %s" % self.args.output ``` ``` plt.close() ``` ``` def plot_line_chart(self, plot_data): ``` 4ceb9f92 Quynh PX Nguyen ``` x = plot_data['numofnodes'] ``` e066fa74 Quynh PX Nguyen ``` print 'num of nodes: %s' % x ``` 4ceb9f92 Quynh PX Nguyen ``` y1 = plot_data['avg_bc'] ``` ``` y2 = plot_data['avg_hbc'] ``` e066fa74 Quynh PX Nguyen ``` plt.plot(x, y1, marker='o', linestyle='-', color='b', label='BBC') ``` ``` plt.plot(x, y2, marker='^', linestyle='--', color='r', label='HBC') ``` ``` fontsize = 18 ``` ``` plt.tick_params(axis='both', which='major', labelsize=fontsize) ``` ``` plt.title("%s on %s" % (self.graph_type, self.devices[self.args.device]), fontsize=fontsize) ``` ``` plt.legend(bbox_to_anchor=(0.25, 1)) ``` ``` plt.xlabel("# of nodes", fontsize=fontsize) ``` ``` plt.ylabel("Running time (secs)", fontsize=fontsize) ``` ``` plt.savefig(self.args.output) ``` ``` print "Finish plotting %s" % self.args.output ``` ``` plt.close() ``` ``` def plot_bar_chart(self, plot_data): ``` ``` x = [self.graph_descriptions[i] for i in plot_data['graphtype']] ``` ``` bc = plot_data['avg_bc'] ``` ``` hbc = plot_data['avg_hbc'] ``` ``` bc_std = plot_data['std_bc'] ``` ``` hbc_std = plot_data['std_hbc'] ``` ``` N = len(bc_std) ``` ``` ind = np.arange(N) ``` ``` width = 0.35 ``` ``` fontsize = 18 ``` ``` p1 = plt.bar(ind, bc, width, color='b', yerr=bc_std) ``` ``` p2 = plt.bar(ind + 0.35, hbc, width, color='r', yerr=hbc_std) ``` ``` plt.xticks(ind + width, x) ``` ``` plt.ylabel("Running time (secs)", fontsize=fontsize) ``` ``` plt.legend((p1[0], p2[0]), ('BBC', 'HBC')) ``` ``` title = "Real community networks on %s" % self.devices[self.args.device] ``` ``` plt.title(title, fontsize=fontsize) ``` ``` plt.tick_params(axis='both', which='major', labelsize=fontsize) ``` 4ceb9f92 Quynh PX Nguyen ``` plt.savefig(self.args.output) ``` e066fa74 Quynh PX Nguyen ``` print "Finish plotting %s" % self.args.output ``` 4ceb9f92 Quynh PX Nguyen ``` plt.close() ``` ```if __name__ == '__main__': ``` ``` vis = ExperimentVisualizer() ``` ``` vis.parse_args() ``` ` vis.plot()`