Statistics
| Branch: | Revision:

root / fiddle / simulation_analysis / analyzer.py @ e066fa74

History | View | Annotate | Download (3.23 KB)

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