Debian Codegraph @

scripts/heri-stat-addons

raw
1 #!/usr/bin/env ruby
2
3 # Copyright (c) 2015-2017 Aleksey Cheusov <[email protected]>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 # This is an internal herisvm script. It takes output "heri-stat -R"
25 # on input and outputs maximum deviations
26
27 require 'optparse'
28
29 @options = {}
30
31 OptionParser.new do |opts|
32 opts.banner = <<EOF
33 heri-stat-addons calculates mean and daviations for statistics
34 calculated by heri-stat
35 Usage:
36 heri-stat-addons [OPTIONS] [files...]
37 OPTIONS:
38 EOF
39
40 opts.on('-h', '--help','display this message and exit') do
41 puts opts
42 exit 0
43 end
44
45 @options[:raw] = false
46 opts.on('-R', '--raw','raw tab-separated output') do
47 @options[:raw] = true
48 end
49
50 opts.separator " "
51 end.parse!
52
53 lines = []
54 while line = gets do
55 lines << line.split("\t")
56 end
57
58 module Enumerable
59 def sum
60 return self.inject(0){|accum, i| accum + i }
61 end
62
63 def mean
64 return self.sum / self.length.to_f
65 end
66
67 def sample_variance
68 m = self.mean
69 sum = self.inject(0){|accum, i| accum + (i - m) ** 2 }
70 return sum / (self.length - 1).to_f
71 end
72
73 def standard_deviation
74 return Math.sqrt(self.sample_variance)
75 end
76
77 end
78
79 def hash2hash2array
80 return Hash.new do
81 |h,k| h [k] = Hash.new do
82 |h,k| h [k] = []
83 end
84 end
85 end
86
87 def hash2hash
88 return Hash.new do
89 |h,k| h [k] = {}
90 end
91 end
92
93 def print_value_raw(t, f, value)
94 puts "%s\t%s\t%s" % [t, f, value]
95 end
96
97 def print_value(t, f, metrics, mean, stddev, maxdev)
98 puts "%-13s %8s> %-26s: %s %s %s" % [t, metrics, f, mean, stddev, maxdev]
99 end
100
101 values = hash2hash2array
102
103 lines.each do |tokens|
104 values [tokens[1]][tokens[0]] << tokens[2].to_f
105 end
106
107 mi = hash2hash
108 ma = hash2hash
109 mean = hash2hash
110 max_deviation = hash2hash
111 std_deviation = hash2hash
112
113 values.each do |key1, hash|
114 hash.each do |key2, arr|
115 mi [key1] [key2] = arr.min
116 ma [key1] [key2] = arr.max
117 mean [key1] [key2] = arr.mean
118 max_deviation [key1] [key2] = [arr.max - arr.mean, arr.mean - arr.min].max
119 std_deviation [key1] [key2] = arr.standard_deviation
120 end
121 end
122
123 #puts std_deviation.inspect
124 #exit 0
125
126 TYPES = {"" => 1, "Macro average" => 1}
127 mi.each do |f, h|
128 pairs = []
129 max_deviation[f].each do |t, max_dev|
130 pairs << [f, t] if max_dev && TYPES.include?(t)
131 end
132 max_deviation[f].each do |t, max_dev|
133 pairs << [f, t] if max_dev && ! TYPES.include?(t)
134 end
135
136 pairs.each do |ft|
137 max_dev = max_deviation [ft[0]][ft[1]] * 100
138 std_dev = std_deviation [ft[0]][ft[1]] * 100
139 mean_value = mean [ft[0]][ft[1]] * 100
140 if @options[:raw]
141 max_dev = "%#.3g" % [max_dev]
142 std_dev = "%#.3g" % [std_dev]
143 mean_value = "%#.3g" % [mean_value]
144
145 print_value_raw(ft[1], "#{f}: mean", mean_value)
146 print_value_raw(ft[1], "#{f}: maxdev", max_dev)
147 print_value_raw(ft[1], "#{f}: stddev", std_dev)
148 else
149 max_dev = "%#6.3g" % [max_dev]
150 std_dev = "%#6.3g" % [std_dev]
151 mean_value = "%-#.3g" % [mean_value]
152
153 print_value(ft[1], "mean, maxdev, stddev", f, mean_value, max_dev, std_dev)
154 end
155 end
156 puts ''
157 end