-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMetricAverage.py
122 lines (88 loc) · 3.08 KB
/
MetricAverage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
# Imports
from __future__ import unicode_literals
import glob
import argparse as ap
from numpy import mean
from math import isnan
# Script information
__author__ = "Marti Municoy"
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Marti Municoy"
__email__ = "marti.municoy@bsc.es"
# Functions
def parseReports(reports_to_parse, parser):
"""It identifies the reports to add to the plot
PARAMETERS
----------
reports_to_parse : list of strings
all the report files that want to be added to the plot
parser : ArgumentParser object
contains information about the command line arguments
RETURNS
-------
parsed_data : tuple of a list and a string
the list specifies the report columns that want to be plotted
in the axis and the string sets the name of the axis
"""
reports = []
for reports_list in reports_to_parse:
trajectories_found = glob.glob(reports_list)
if len(trajectories_found) == 0:
print("Warning: path to report file \'" +
"{}".format(reports_list) + "\' not found.")
for report in glob.glob(reports_list):
reports.append(report)
if len(reports) == 0:
print "Error: list of report files is empty."
parser.print_help()
exit(1)
return reports
def parseArgs():
"""Parse arguments from command-line
RETURNS
-------
reports : string
list of report files to look for data
colnum : int
column number that
"""
parser = ap.ArgumentParser()
required = parser.add_argument_group('required arguments')
required.add_argument("-i", "--input", required=True, metavar="FILE",
type=str, nargs='*', help="path to report files")
required.add_argument("-c", "--column", required=True, metavar="INTEGER",
type=int, help="column number of the metric that " +
"wants to be averaged", default=None)
args = parser.parse_args()
reports = parseReports(args.input, parser)
colnum = args.column
return reports, colnum
def readMetricFromReports(reports, colnum):
metric_list = []
for report in reports:
with open(report, 'r') as report_file:
next(report_file)
for line in report_file:
value = float(line.split()[colnum - 1])
if not isnan(value) or value > -1000.:
metric_list.append(value)
return metric_list
def meanMetric(metric_list):
return mean(metric_list)
def main():
"""Main function
It is called when this script is the main program called by the interpreter
"""
# Parse command-line arguments
reports, colnum = parseArgs()
# Get list of all metric's values from report files
metric_list = readMetricFromReports(reports, colnum)
# Calculate mean
mean = meanMetric(metric_list)
# Print results
print(mean)
if __name__ == "__main__":
"""Call the main function"""
main()