This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
forked from VeraLiconaResearchGroup/MHSGenerationAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollator.py
executable file
·245 lines (188 loc) · 7.22 KB
/
collator.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
#!/usr/bin/env python
# coding: utf-8
# MHS algorithm benchmark output collator
# Copyright Vera-Licona Research Group (C) 2015
# Author: Andrew Gainer-Dewar, Ph.D. <[email protected]>
# This file is part of MHSGenerationAlgorithms.
# MHSGenerationAlgorithms is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# MHSGenerationAlgorithms is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import argparse
import json
import pandas
import numpy
import os
from collections import defaultdict
sorted_alglist = ["mmcs",
"rs",
"pMMCS",
"pRS",
"hbc",
"bmr",
"htcbdd",
"knuth",
"primdecomp",
"mhs2",
"dl",
"fka.begk",
"bool.iterative",
"hst",
"hsdag",
"berge",
"ocsanaGreedy",
"bm",
"staccato"]
cutoff_algs = ["pMMCS",
"pRS",
"mhs2",
"bool.iterative",
"hst",
"hsdag",
"berge",
"ocsanaGreedy",
"staccato"
]
thread_algs = ["pMMCS",
"pRS",
"mhs2",
"bm"]
omit_algs = ['ks',
'partran']
def parse_results_algname(algname_str):
orig_name = algname_str
# Process the name into its components
parts = orig_name.split("-")
# The algorithm name always comes first
stripped_name = orig_name[0]
# The thread and cutoff specifications, if any, come later
threads = 1
cutoff = 0
for specpart in parts[1:]:
if specpart[0] == 't':
threads = int(specpart[1:])
if specpart[0] == 'c':
cutoff = int(specpart[1:])
# Build the return dict
result = {
"alg_name": stripped_name,
"threads": threads,
"cutoff": cutoff
}
return result
def algorithm_result_frame(alg, runtimes):
# Build a DataFrame with the results of a given algorithm in the given
# results record
# Find the instances of this algorithm in the results file
alg_instances = filter(lambda algname: algname.split('-')[0] == alg, runtimes)
# For each one, find the runtimes
results = defaultdict(dict)
for alg_instance in alg_instances:
alg_instance_runtimes = runtimes[alg_instance]
median_runtime = numpy.median(alg_instance_runtimes)
alg_instance_specs = parse_results_algname(alg_instance)
threads = alg_instance_specs["threads"]
cutoff = alg_instance_specs["cutoff"]
results[cutoff][threads] = median_runtime
# TODO: Can we annotate this DataFrame to explain the axis labels?
result_frame = pandas.DataFrame(results)
return result_frame
def problem_results_panel(results_json):
runtimes = results_json["runtimes"]
# Get the list of algorithm names
algs = set(alg["algName"] for alg in results_json["algs"] if alg["algName"] not in omit_algs)
# For each algorithm, build a DataFrame with its results
results = dict()
for alg in algs:
results[alg] = algorithm_result_frame(alg, runtimes)
result_panel = pandas.Panel(results)
return result_panel
def panel4d_from_filenames(alg_filenames):
results = dict()
for input_filename in alg_filenames:
with open(input_filename) as input_file:
problem_results_json = json.load(input_file)
problem_results = problem_results_panel(problem_results_json)
problem_shortname = os.path.splitext(os.path.basename(input_filename))[0]
results[problem_shortname] = problem_results
result_panel = pandas.Panel4D(results)
return result_panel
def generate_full_csv(data4d, basename, reverse_data_sort):
print "Processing base case"
# Extract the data we want
data = data4d.ix[:,:,1,0].T.dropna(axis='columns', how='all').filter(items = sorted_alglist, axis=1)
# Sort the input sets lexicographically
data.sort_index(ascending = not reverse_data_sort,
inplace = True)
# Construct filename
output_file_name = "{0}.full.csv".format(basename)
# Write the data
data.to_csv(output_file_name,
index_label = "Input")
def generate_cutoff_csv(data4d, basename):
print "Processing cutoff cases"
# Find cutoff size list
orig_cutoff_order = data4d.axes[3]
# Put 0 last, if appropriate, for later reordering of dataframe
if 0 in orig_cutoff_order:
new_cutoff_order = list(orig_cutoff_order)
new_cutoff_order.remove(0)
new_cutoff_order.append(0)
else:
new_cutoff_order = list(orig_cutoff_order)
# Iterate over datasets
num_datasets = len(data4d.axes[0])
for i in xrange(num_datasets):
# Extract the data we want
data = data4d.ix[i,:,1,:].filter(items = cutoff_algs, axis=1)
# Reorder cutoffs to put 0 last
data = data.reindex(new_cutoff_order)
# Rename 0 to "None"
data.rename(index = {0: 'None'}, inplace = True)
# Get the name of the dataset we sliced
datasetname = data4d.axes[0][i]
# Construct filename
output_file_name = "{0}.{1}.cutoff.csv".format(basename, datasetname)
# Write the data
data.to_csv(output_file_name,
index_label = "Cutoff")
def generate_thread_csv(data4d, basename):
print "Processing parallel cases"
# Iterate over datasets
num_datasets = len(data4d.axes[0])
for i in xrange(num_datasets):
# Extract the data we want
data = data4d.ix[i,:,:,0].filter(items = thread_algs, axis=1)
# Get the name of the dataset we sliced
datasetname = data4d.axes[0][i]
# Construct filename
output_file_name = "{0}.{1}.thread.csv".format(basename, datasetname)
# Write the data
data.to_csv(output_file_name,
index_label = "Threads")
def main():
# Set up argument processing
parser = argparse.ArgumentParser(description="MHS algorithm benchmark runner")
# Add arguments
parser.add_argument("output_file_basename", help="Base name for output files")
parser.add_argument("input_files", help="Input file(s) to collate", nargs="+")
parser.add_argument("-r", "--reverse_data_sort", action="store_true", help="Sort data sets in reverse lex order")
# Process the arguments
args = parser.parse_args()
# Process the files
data4d = panel4d_from_filenames(args.input_files)
# Generate the data4d and write the plots to files
generate_full_csv(data4d, args.output_file_basename, args.reverse_data_sort)
generate_cutoff_csv(data4d, args.output_file_basename)
generate_thread_csv(data4d, args.output_file_basename)
if __name__ == "__main__":
main()
### Emacs configuration
# Local Variables:
# mode: python
# End