diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-09-27 19:03:22 -0400 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-09-27 19:03:22 -0400 |
| commit | 7c09ec981c6e06af2e62d67a609eb53728267954 (patch) | |
| tree | 76a93db7cadc452ac70eabbd52fdd87ed5fd54c4 /parse/point.py | |
| parent | 5554e053e9f3d5f7987d3f1d889802b211af8eab (diff) | |
Added script to parse directory data, create CSVs for every chagned value.
This change also makes SchedTrace and OverheadTrace events configurable.
Diffstat (limited to 'parse/point.py')
| -rw-r--r-- | parse/point.py | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/parse/point.py b/parse/point.py new file mode 100644 index 0000000..4343d03 --- /dev/null +++ b/parse/point.py | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | """ | ||
| 2 | Too much duplicate code in this file | ||
| 3 | """ | ||
| 4 | |||
| 5 | import copy | ||
| 6 | import numpy as np | ||
| 7 | from enum import Enum | ||
| 8 | from collections import defaultdict | ||
| 9 | |||
| 10 | Type = Enum(['Min','Max','Avg','Var']) | ||
| 11 | default_typemap = {Type.Max : {Type.Max : 1, Type.Min : 0, Type.Avg : 1, Type.Var : 1}, | ||
| 12 | Type.Min : {Type.Max : 1, Type.Min : 0, Type.Avg : 1, Type.Var : 1}, | ||
| 13 | Type.Avg : {Type.Max : 1, Type.Min : 0, Type.Avg : 1, Type.Var : 1}} | ||
| 14 | |||
| 15 | def make_typemap(): | ||
| 16 | return copy.deepcopy(default_typemap) | ||
| 17 | |||
| 18 | def dict_str(adict, sep = "\n"): | ||
| 19 | return sep.join(["%s: %s" % (k, str(v)) for (k,v) in adict.iteritems()]) | ||
| 20 | |||
| 21 | class Measurement(object): | ||
| 22 | def __init__(self, id = None, kv = {}): | ||
| 23 | self.id = id | ||
| 24 | self.stats = {} | ||
| 25 | for k, v in kv.iteritems(): | ||
| 26 | self[k] = v | ||
| 27 | |||
| 28 | def from_array(self,array): | ||
| 29 | array = np.array(array) | ||
| 30 | self[Type.Max] = array.max() | ||
| 31 | self[Type.Avg] = array.mean() | ||
| 32 | self[Type.Var] = array.var() | ||
| 33 | return self | ||
| 34 | |||
| 35 | def __check_type(self, type): | ||
| 36 | if not type in Type: | ||
| 37 | raise AttributeError("Not a valid type '%s'" % type) | ||
| 38 | |||
| 39 | def __getitem__(self, type): | ||
| 40 | self.__check_type(type) | ||
| 41 | return self.stats[type] | ||
| 42 | |||
| 43 | def __iter__(self): | ||
| 44 | return self.stats.iteritems() | ||
| 45 | |||
| 46 | def __contains__(self, type): | ||
| 47 | self.__check_type(type) | ||
| 48 | return type in self.stats | ||
| 49 | |||
| 50 | def __setitem__(self, type, value): | ||
| 51 | self.__check_type(type) | ||
| 52 | self.stats[type] = value | ||
| 53 | |||
| 54 | def __str__(self): | ||
| 55 | return "<Measurement-%s> %s" % (self.id, dict_str(self.stats, " ")) | ||
| 56 | |||
| 57 | |||
| 58 | class Summary(Measurement): | ||
| 59 | def __init__(self, id, measures, typemap = default_typemap): | ||
| 60 | super(Summary, self).__init__("Summary-%s" % id) | ||
| 61 | |||
| 62 | self.__check_types(measures, typemap) | ||
| 63 | self.__summarize(measures, typemap) | ||
| 64 | |||
| 65 | def __check_types(self, measures, typemap): | ||
| 66 | required_types = self.__get_required(typemap) | ||
| 67 | for m in measures: | ||
| 68 | for type in required_types: | ||
| 69 | if type not in m: | ||
| 70 | raise ValueError("measurement '%s' missing type '%s'" % | ||
| 71 | (self.id, type)) | ||
| 72 | |||
| 73 | def __summarize(self, measures, typemap): | ||
| 74 | for sum_type in Type: | ||
| 75 | self[sum_type] = Measurement(self.id) | ||
| 76 | |||
| 77 | def avg(vals): | ||
| 78 | return sum(vals) / len(vals) | ||
| 79 | |||
| 80 | for base_type in Type: | ||
| 81 | for sum_type, func in (Type.Min,min),(Type.Max,max),(Type.Avg, avg): | ||
| 82 | if typemap[sum_type][base_type]: | ||
| 83 | val = func([m[base_type] for m in measures]) | ||
| 84 | self[sum_type][base_type] = val | ||
| 85 | |||
| 86 | def __get_required(self, typemap): | ||
| 87 | required = [] | ||
| 88 | for base_type in Type: | ||
| 89 | matches = [t[base_type] for t in typemap.itervalues()] | ||
| 90 | if bool(sum(matches)): | ||
| 91 | required += [base_type] | ||
| 92 | return required | ||
| 93 | |||
| 94 | class ExpPoint(object): | ||
| 95 | def __init__(self, id = "", init = {}): | ||
| 96 | self.stats = {} | ||
| 97 | for type, value in init.iteritems(): | ||
| 98 | self[type] = value | ||
| 99 | self.id = id | ||
| 100 | |||
| 101 | def __check_val(self, obj): | ||
| 102 | if not isinstance(obj, Measurement): | ||
| 103 | raise AttributeError("Not a valid measurement '%s'" % obj) | ||
| 104 | |||
| 105 | def __getitem__(self, type): | ||
| 106 | return self.stats[type] | ||
| 107 | |||
| 108 | def __iter__(self): | ||
| 109 | return self.stats.iteritems() | ||
| 110 | |||
| 111 | def __contains__(self, type): | ||
| 112 | return type in self.stats | ||
| 113 | |||
| 114 | def __setitem__(self, type, value): | ||
| 115 | self.__check_val(value) | ||
| 116 | self.stats[type] = value | ||
| 117 | |||
| 118 | def __str__(self): | ||
| 119 | return "<ExpPoint-%s>\n%s" % (self.id, dict_str(self.stats)) | ||
| 120 | |||
| 121 | def get_stats(self): | ||
| 122 | return self.stats.keys() | ||
| 123 | |||
| 124 | class SummaryPoint(ExpPoint): | ||
| 125 | def __init__(self, id, points, typemap = default_typemap): | ||
| 126 | super(SummaryPoint,self).__init__("Summary-%s" % id) | ||
| 127 | |||
| 128 | grouped = defaultdict(lambda : []) | ||
| 129 | |||
| 130 | for exp in points: | ||
| 131 | for name,measure in exp.stats.iteritems(): | ||
| 132 | grouped[name] += [measure] | ||
| 133 | |||
| 134 | for key in grouped.iterkeys(): | ||
| 135 | self[key] = Summary(key, grouped[key], typemap) | ||
