Statistics
| Branch: | Revision:

root / fiddle / simulation_analysis / plot_experiment_result.py @ 4ceb9f92

History | View | Annotate | Download (2.32 KB)

1
#!/usr/bin/env python
2

    
3
"""
4
Input: *.plotdata
5
Output: a graph showing the average of BC and HBC
6
"""
7
import argparse
8
import os
9
import numpy as np
10
import matplotlib.pyplot as plt
11

    
12
class ExperimentVisualizer():
13
    graph_descriptions = {
14
        "CN": "Community Network",
15
        "ER": "Erdos",
16
        "PL": "Power Lab",
17
    }
18

    
19
    def __init__(self):
20
        self.args = None
21
        self.graph_type = None
22

    
23
    def parse_args(self):
24
        parser = argparse.ArgumentParser(description="Plotting the comparison  \
25
                                         between Brandes Betweenness Centrality \
26
                                         (BC) and Heuristic BC (HBC)")
27
        parser.add_argument("-i", dest="input")
28
        parser.add_argument("-o", dest="output", required=False,
29
                            default="")
30

    
31
        self.args = parser.parse_args()
32

    
33
    def plot(self):
34
        input_dir = os.path.dirname(self.args.input)
35
        filename = os.path.splitext(os.path.basename(self.args.input))[0]
36

    
37
        self.graph_type = self.graph_descriptions.get(filename, "")
38

    
39
        if self.args.output == "":
40
            self.args.output = '%s/%s_BC_HBC.png' % (input_dir, filename)
41

    
42
        plot_data = self.read_plot_data()
43

    
44
        # Plot
45
        self.plot_line_graph(plot_data)
46

    
47
    def read_plot_data(self):
48
        plot_data = np.loadtxt(self.args.input,
49
            dtype= {
50
                'names': ('typotype', 'graphtype', 'numofnodes', 'avg_bc', 'avg_hbc',
51
                          'std_bc', 'std_hbc'),
52
                'formats': ('a6', 'a2', 'i4', 'f8', 'f8', 'f8', 'f8')
53
            },
54
            delimiter=','
55
        )
56
        plot_data = plot_data[plot_data['numofnodes'].argsort()]
57
        return plot_data
58

    
59

    
60
    def plot_line_graph(self, plot_data):
61
        x = plot_data['numofnodes']
62
        y1 = plot_data['avg_bc']
63
        y2 = plot_data['avg_hbc']
64

    
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()
71
        plt.savefig(self.args.output)
72
        plt.close()
73

    
74

    
75
if __name__ == '__main__':
76
    vis = ExperimentVisualizer()
77
    vis.parse_args()
78
    vis.plot()