Statistics
| Branch: | Revision:

root / fiddle / simulation_analysis / analyzer.py @ 072a5b5a

History | View | Annotate | Download (3.26 KB)

1 072a5b5a Quynh PX Nguyen
#!/usr/bin/env python
2
3
import argparse
4
import os
5
import utility
6
from process_out1 import process_out1
7
from process_out2 import process_out2
8
9
class Analyzer():
10
    analyzer_types = {1: "process_out1",
11
                   2: "process_out2"}
12
13
    def __init__(self):
14
        self.args = None
15
        self.result_analyzer = None
16
17
    def parse_args(self):
18
        parser = argparse.ArgumentParser(description="Performing analysis for \
19
                                         Betweenness Centrality (BC) and \
20
                                         Heuristic BC")
21
        parser.add_argument("-t", dest="type", help="determining whether it's \
22
                            *.out1 or *.out2", type=int,
23
                            choices=self.analyzer_types.keys(), required=True)
24
        parser.add_argument("-i", dest="input_file", help="path to input file",
25
                            required=True)
26
        parser.add_argument("-o", dest="output_file", help="path to output file",
27
                            default="", required=False)
28
        parser.add_argument("-p", dest="prefix", required=False,
29
                            help="prefix to add to output files", type=str,
30
                            default=None)
31
32
        self.args = parser.parse_args()
33
34
    def process_out1(self, input_filepath, output_filepath):
35
        input_data = utility.read_out1(input_filepath)
36
        bc_result = utility.get_statistical_data(input_data, 'filename', 'bc_time')
37
        hbc_result = utility.get_statistical_data(input_data, 'filename', 'hbc_time')
38
39
        output_data = utility.format_output_data_out1(bc_result, hbc_result)
40
        utility.write_to_file(output_filepath, output_data)
41
        return True
42
43
    def process_out2(self, input_filepath, output_filepath):
44
        input_data = utility.read_out2(input_filepath)
45
        bc_result = utility.get_statistical_data(input_data, 'typotype', 'avg_bc')
46
        hbc_result = utility.get_statistical_data(input_data, 'typotype', 'avg_hbc')
47
48
        output_data = utility.format_output_data_out2(bc_result, hbc_result)
49
        utility.write_to_file(output_filepath, output_data)
50
        return True
51
52
    def analyze_result(self):
53
        analyzer_function = getattr(self,
54
                                          self.analyzer_types[self.args.type])
55
56
        r = analyzer_function(self.args.input_file, self.args.output_file)
57
58
        if r:
59
            print "Done analyzing %s" % self.args.input_file
60
            print "Result is saved in: %s" % self.args.output_file
61
62
if __name__ == '__main__':
63
    analyzer = Analyzer()
64
    analyzer.parse_args()
65
66
    input_filepath = analyzer.args.input_file
67
    input_dir = os.path.dirname(os.path.realpath(input_filepath))
68
    input_filename = os.path.splitext(os.path.basename(input_filepath))[0]
69
    output_filename = '%s.out%s' % (input_filename, (analyzer.args.type + 1))
70
71
    if analyzer.args.prefix:
72
        output_name = analyzer.args.prefix
73
        analyzer.args.output_file = '%s/%s' % (analyzer.args.prefix, output_filename)
74
75
    # This option has higher priority
76
    if analyzer.args.output_file == "":
77
        analyzer.args.output_file = '%s/%s' % (input_dir, output_filename)
78
79
80
81
        output_filepath = '%s/%s.out2' % (input_dir, input_filename)
82
83
        analyzer.args.output_file = output_filepath
84
85
    analyzer.analyze_result()