forked from alexeyignatiev/mkplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statutil.py
340 lines (269 loc) · 10.6 KB
/
statutil.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
338
339
340
#!/usr/bin/env python
#-*- coding:utf-8 -*-
##
## statutil.py
##
## Created on: May 22, 2013
## Author: Alexey S. Ignatiev
## E-mail: [email protected]
##
#
#==============================================================================
from __future__ import print_function
import json
import sys
#
#==============================================================================
class JSONException(Exception):
pass
#
#==============================================================================
class Stat:
"""
Simple statistical data class.
"""
def __init__(self, filename=None):
"""
Constructor.
"""
if filename is None:
self.insts_own = []
self.preamble = {}
self.data = {}
elif type(filename) is list:
print( 'in case of several files use "StatArray" class', file=sys.stderr)
else:
self.read(filename)
def read(self, filename=None):
"""
Reads a file into a Stat object.
"""
if filename is None:
print( 'no filename was specified', file=sys.stderr)
return
with open(filename, 'r') as fp:
print('reading {0}'.format(filename), file=sys.stderr)
try:
data_full = json.load(fp)
except:
raise JSONException('Unable to parse \'{0}\'.'.format(filename))
self.data = data_full['stats']
self.preamble = data_full['preamble']
self.preamble['origin'] = filename
self.insts_own = sorted(list(set(self.data.keys())))
def write(self, to=None):
"""
Writes a Stat object to a file.
"""
to_write = {'preamble': self.preamble, 'stats': self.data}
if to is None:
to = self.preamble['origin']
# 'origin' field is not needed anymore
del(self.preamble['origin'])
if type(to) is str:
with open(to, 'w') as fp:
json.dump(to_write, fp, indent=4, separators=(',', ': '))
elif type(to) is file:
json.dump(to_write, to, indent=4, separators=(',', ': '))
else:
print('don\'t know how to write to {0}'.format(type(to)), file=sys.stderr)
def update(self, success=None, failure=None):
"""
Updates stats using additional success and failure signs.
"""
if success:
pass
if failure:
sign = lambda x: x
key = failure
if failure[:3] == 'no-':
sign = lambda x: not x
key = failure[3:]
for inst in self.insts_own:
if inst in self.data and self.data[inst]['status'] == True:
if sign(key in self.data[inst]):
print('updating', inst, file=sys.stderr)
self.data[inst]['status'] = False
self.write()
def list(self, crit=None):
"""
Lists instances satisfying the criterion.
"""
if crit:
pred = lambda x: x == crit['val']
if crit['pred'] == '<':
pred = lambda x: x < crit['val']
elif crit['pred'] == '<=':
pred = lambda x: x <= crit['val']
elif crit['pred'] == '>':
pred = lambda x: x > crit['val']
elif crit['pred'] == '>=':
pred = lambda x: x >= crit['val']
for inst in self.insts_own:
if inst in self.data and self.data[inst]['status'] == True:
if pred(self.data[inst][crit['key']]):
print('{0}: {1} = {2}'.format(inst, crit['key'], self.data[inst][crit['key']]))
#
#==============================================================================
class StatArray:
"""
Contains statistical data for several files.
"""
def __init__(self, files=None):
"""
Constructor.
"""
if files is None:
self.inst_full = []
self.stat_objs = []
elif type(files) is list:
self.read(files)
else:
print('in case of just one file use "Stat" class', file=sys.stderr)
self.read([files])
def __getitem__(self, key):
if key < len(self.stat_objs):
return self.stat_objs[key]
def __len__(self):
return len(self.stat_objs)
def __iter__(self):
for stat_obj in self.stat_objs:
yield stat_obj
def read(self, files=None):
"""
Reads several files into a StatArray object.
"""
if files is None:
print('no files was specified', file=sys.stderr)
return
self.stat_objs = []
for f in files:
self.stat_objs.append(Stat(f))
inst_set = set()
for stat_obj in self.stat_objs:
inst_set = inst_set.union(set(stat_obj.insts_own))
self.inst_full = sorted(list(inst_set))
def write(self, files=None):
"""
Writes a StatArray object to given files.
"""
if files is None:
files = [stat_obj.preamble['origin'] for stat_obj in self.stat_objs]
assert len(files) == len(self.stat_objs), 'wrong number of filenames'
for f, stat_obj in zip(files, self.stat_objs):
stat_obj.write(f)
def cluster(self, use_key=['program', 'prog_args']):
"""
Clasters Stat objects according to their preamble values.
"""
# the key should be a list
if type(use_key) is not list:
use_key = [use_key]
clusters = {}
for stat_obj in self.stat_objs:
# updating the Stat object
for i, i_old in enumerate(stat_obj.insts_own):
i_new = '{0}@{1}'.format(i_old, stat_obj.preamble['benchmark'])
stat_obj.insts_own[i] = i_new
stat_obj.data[i_new] = stat_obj.data.pop(i_old)
key = ' '.join([stat_obj.preamble[one_key] for one_key in use_key])
if key in clusters:
# update the cluster
clusters[key].insts_own.extend(stat_obj.insts_own)
clusters[key].data.update(stat_obj.data)
clusters[key].preamble['benchmark'].append(stat_obj.preamble['benchmark'])
clusters[key].preamble['runsolver_args'].append(stat_obj.preamble['runsolver_args'])
else:
# add new cluster
clusters[key] = stat_obj
clusters[key].preamble['benchmark'] = [clusters[key].preamble['benchmark']]
clusters[key].preamble['runsolver_args'] = [clusters[key].preamble['runsolver_args']]
self.stat_objs = [cl for cl in clusters.values()]
inst_set = set()
for stat_obj in self.stat_objs:
inst_set = inst_set.union(set(stat_obj.insts_own))
self.inst_full = sorted(list(inst_set))
def unclaster(self):
"""
Unclasters previously clastered Stat objects.
"""
print('unclaster() method is not implemented yet', file=sys.stderr)
def make_vbs(self, addit_key=None):
"""
Makes vbs using the status, rtime and additional key as the measurement.
NOTE: the use of addit_key is not implemented yet.
"""
vbs = Stat()
vbs.insts_own = self.inst_full
vbs.preamble = self.stat_objs[0].preamble
vbs.preamble['program'] = 'vbs'
vbs.preamble['prog_args'] = ''
vbs.preamble['origin'] = [obj.preamble['origin'] for obj in self.stat_objs]
for inst in self.inst_full:
alts = []
for stat_obj in self.stat_objs:
if inst in stat_obj.data and stat_obj.data[inst]['status'] == True:
alts.append(stat_obj.data[inst])
if alts:
vbs.data[inst] = min(alts, key=lambda x: x['rtime'])
else:
# all fail; choose any:
vbs.data[inst] = self.stat_objs[0].data[inst]
self.stat_objs.append(vbs)
def compare(self, cmp_key=None):
"""
Compares values for a specific key. Do nothing if cmp_key is None.
"""
if cmp_key:
for inst in self.inst_full:
vals = {}
for stat_obj in self.stat_objs:
if inst in stat_obj.data and stat_obj.data[inst]['status'] == True and cmp_key in stat_obj.data[inst]:
if stat_obj.data[inst][cmp_key] in vals:
vals[stat_obj.data[inst][cmp_key]].append(stat_obj.preamble['origin'])
else:
vals[stat_obj.data[inst][cmp_key]] = [stat_obj.preamble['origin']]
if len(vals.keys()) > 1:
print('different values found', file=sys.stderr)
print('instance:', inst, file=sys.stderr)
print('values:', vals, file=sys.stderr)
def list_simple(self, to_list='all'):
"""
Shows instances required by user.
"""
if to_list:
print('showing {0}:'.format(to_list))
if to_list == 'all':
for inst in self.inst_full:
print(inst)
else:
status = False if to_list == 'failed' else True
for inst in self.inst_full:
objs = []
for stat_obj in self.stat_objs:
if inst in stat_obj.data and stat_obj.data[inst]['status'] == status:
p = stat_obj.preamble
if 'prog_alias' in p:
objs.append(p['prog_alias'])
else:
objs.append(p['program'] + ' ' + p['prog_args'])
if objs:
if len(self.stat_objs) > 1:
objs = '[{0}]'.format(', '.join(obj for obj in objs))
print('{0}: {1}'.format(inst, objs))
else:
print(inst)
def list(self, crit=None):
"""
Shows instances required by user.
"""
if crit:
for stat_obj in self.stat_objs:
stat_obj.list(crit)
def update(self, success=None, failure=None):
"""
Update stats using additional success and failure signs.
"""
if success or failure:
for stat_obj in self.stat_objs:
stat_obj.update(success, failure)