-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzeRuns.py
executable file
·337 lines (279 loc) · 10.8 KB
/
analyzeRuns.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python3
import sys
import os
import math
# The sampling strategies that should be analyzed
TYPES = ["distBased_", "divDistBased_", "solvBased_", "rand", "henard"]
CASE_STUDIES = ["7z", "BerkeleyDBC", "Dune", "Hipacc", "JavaGC", "LLVM", "lrzip", "Polly", "VP9", "x264"]
SEPARATOR = "/"
SPL_CONQUEROR_PREFIX = "out_"
ALL_RESULTS_PREFIX = "all_"
ERROR_PREFIX = "error_"
STANDARD_DEVIATION_PREFIX = "sd_"
T_TEST_PREFIX = "ttest_"
KOLMOGOROV_SMIRNOV_PREFIX = "kstest_"
WHOLE_POPULATION = "wp"
VARIANCE_RESULT_PREFIX = "var_"
OTHER_FILE_PREFIXES = ["dist_", "measurement_", "point_"]
OTHER_FILE_SUFFIX = ".txt"
SAMPLED_CONFIGURATIONS_FILE = ["sampledConfigurations"]
SAMPLED_CONFIGURATIONS_FILE_SUFFIX = ".csv"
CSV_SEPARATOR = ";"
TOTAL = "total"
SAMPLED_CONFIGURATIONS_STATS_SUFFIX = "_stat"
PERCENT = "%"
def print_usage():
'''
Prints the usage of this script.
'''
print("Usage: <RunDirectory> <SummaryDirectory>")
print("RunDirectory\t\t The directory containing the data of all runs.")
print("SummaryDirectory\t The directory where the average run should be copied to.")
def list_directories(path):
'''
Returns the subdirectories of the given path.
:param path: the path to find the subdirectories from.
:return: the subdirectories as list.
'''
for root, dirs, files in os.walk(path):
return dirs
def get_specific_files_from_directory(path, prefixes, suffix, contains=None, excludes=None):
'''
Returns all files that begin with one of the given prefixes.
:param path: the path to check the files from
:param prefixes: the prefixes of the wanted files
:return: a list containing the files
'''
result = []
for root, dirs, files in os.walk(path):
for file in files:
found = False
for prefix in prefixes:
if prefix in file and file.endswith(suffix):
found = True
break
if not found:
continue
if contains is not None:
found = False
for containedString in contains:
if containedString in file:
found = True
break
if not found:
continue
if excludes is not None:
skip = False
for excludedString in excludes:
if excludedString in file:
skip = True
break
if skip:
continue
result.append(file)
return result
def add_to_dictionary(dictionary, file_name, number_run, value):
'''
Adds the given data to the dictionary.
:param dictionary: the dictionary to add to
:param file_name: the file name
:param number_run: the number of the random seed run
:param value: the value to add to the dictionary
'''
if file_name not in dictionary:
dictionary[file_name] = {}
dictionary[file_name][number_run] = value
def add_bucket_to_dictionary(dict, bucket, numberRun, value):
'''
Adds the given bucket to the dictionary
:param dict: the dictionary to add to
:param bucket: the bucket (key)
:param numberRun: the number of run
:param value: the value to add
'''
if bucket not in dict:
dict[bucket] = {}
dict[bucket][numberRun] = int(value)
def analyze_log_file(path):
'''
Analyzes the log files of SPL Conqueror.
:param path: the path to the log file
:return: the error rate
'''
error_rate = sys.float_info.max
python_learner = False
file = open(path, 'r')
parse_lines = False
for line in file:
# if "Models:" in line:
# continue;
if "command: analyze-learning" in line:
parse_lines = True
elif "command: learn-python" in line:
python_learner = True
elif "command: clean-sampling" in line:
parse_lines = False
elif python_learner and "Error rate" in line:
error_rate = float(line.strip().split(" ")[-1]) * 100
return error_rate
elif not python_learner and parse_lines and ";" in line and not "command" in line:
split_line = line.strip().split(";")
current_error_rate = float(split_line[len(split_line) - 1])
if current_error_rate < error_rate:
error_rate = current_error_rate
file.close()
return error_rate
def add_to_sum_dict(dict, file, value):
'''
Adds the given value to the dictionary.
:param dict: the dictionary to add up to
:param file: the file (key)
:param value: the value to add
'''
if file not in dict:
dict[file] = 0
dict[file] += value
def copy_file_content(opened_file, target, run):
'''
Copies the file content
:param opened_file: the file stream
:param target: the targeted file to read from
:param run: the run to write
'''
target_file = open(target, 'r')
# Skip the header
next(target_file)
for line in target_file:
opened_file.write(str(run) + ";" + line)
target_file.close()
def get_header_of(file):
'''
Returns the header of the file
:param file: the file to read the header from
:return: the header of the file
'''
f = open(file, 'r')
result = next(f)
return result
def add_values_from_file_to_dict(dictionary, run, file_path):
''''
Reads in the current content of the file into the dictionary
:param dictionary: the dictionary to save the content of the file into
:param run: the random seed run
:param file_path: the path to the file
'''
value_file = open(file_path, 'r')
# Skip the header
next(value_file)
# The files have to be written in a csv-like manner, where ';' is taken as element separator and
# '\n' as row separator
for line in value_file:
elements = line.split(';')
add_bucket_to_dictionary(dictionary, elements[0], run, elements[1])
def convert_dict_to_list(dictionary):
'''
Converts the given dictionary to a list.
:param dictionary: the dictionary to convert
:return: the dictionary as list
'''
result = []
for key in dictionary.keys():
result.append(dictionary[key])
return result
############
# MAIN #
############
def main():
'''
This is the main method, which (1) gathers and (2) processes the information of all the runs.
The accumulated information is stored in another directory.
'''
if len(sys.argv) != 3:
print_usage()
exit(0)
run_directory = sys.argv[1]
original_directory = sys.argv[2]
if not run_directory.endswith(SEPARATOR):
run_directory = run_directory + SEPARATOR
if not original_directory.endswith(SEPARATOR):
original_directory = original_directory + SEPARATOR
run_statistic = {}
# Precompute the prefixes of the files to analyze
prefixes = []
for type in TYPES:
prefixes.append(SPL_CONQUEROR_PREFIX + type[:len(type) - 1])
suffix = ".log"
name = ""
sampled_config_contains = ["_rand_", "_uni_"]
sampled_exclude = ["_stat"]
for case_study in CASE_STUDIES:
print("Analyzing " + case_study + ".")
directories = list_directories(run_directory + case_study + SEPARATOR)
average_values = {}
for directory in sorted(directories):
split_name = directory.split("_")
tmp_name = ""
print("Scanning " + split_name[len(split_name) - 1] + ". directory.")
for i in range(0, len(split_name) - 1):
if i != 0:
tmp_name += "_"
tmp_name += split_name[i]
name = tmp_name
number_run = int(split_name[len(split_name) - 1])
files = get_specific_files_from_directory(run_directory + case_study + SEPARATOR + directory, prefixes,
suffix)
for file in sorted(files):
error = analyze_log_file(run_directory + case_study + SEPARATOR + directory + SEPARATOR + file)
add_to_sum_dict(average_values, file, error)
add_to_dictionary(run_statistic, file, number_run, error)
for key in average_values.keys():
average_values[key] = average_values[key] / len(run_statistic[key])
# Retrieve the most average runs, print the error rates into a file
avg_runs = {}
best_runs = {}
worst_runs = {}
best_score = {}
worst_score = {}
standard_deviation = {}
for file in run_statistic.keys():
best_score[file] = 1000
worst_score[file] = 0
min_deviation = sys.float_info.max
standard_deviation[file] = 0
# Save the error rates in the following file (needed for box-plots)
mid_file_name = file[len(SPL_CONQUEROR_PREFIX):len(file) - len(suffix)]
error_rate_file = open(original_directory + case_study + os.path.sep + ALL_RESULTS_PREFIX + ERROR_PREFIX + mid_file_name +
OTHER_FILE_SUFFIX, 'w')
error_rate_file.write("Run;Error\n")
for run in run_statistic[file].keys():
error = run_statistic[file][run]
# Ignore runs where the error rate is Inf (in C#)
if error >= 1.79769313486e+308:
continue
if error < best_score[file]:
best_score[file] = error
best_runs[file] = run
if error > worst_score[file]:
worst_score[file] = error
worst_runs[file] = run
try:
standard_deviation[file] += (average_values[file] - error) ** 2
except:
print("Error of run " + str(run) + " too high.")
continue
error_rate_file.write(str(run) + ";" + str(error) + "\n")
deviation = abs(average_values[file] - error)
if deviation < min_deviation:
min_deviation = deviation
avg_runs[file] = run
error_rate_file.close()
# Compute the relative standard deviation
standard_deviation[file] /= len(run_statistic[file].keys())
standard_deviation[file] = math.sqrt(standard_deviation[file])
standard_deviation[file] /= average_values[file]
standard_deviation_file = open(original_directory + case_study + os.path.sep + ALL_RESULTS_PREFIX + STANDARD_DEVIATION_PREFIX +
mid_file_name + OTHER_FILE_SUFFIX, 'w')
standard_deviation_file.write(str(standard_deviation[file]) + "\n")
standard_deviation_file.close()
if "__main__" == __name__:
main()