statval 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,6 @@ module StatVal
2
2
 
3
3
  class StatVal
4
4
 
5
- POS_INFINITY = 1.0/0.0
6
- NEG_INFINITY = - 1.0/0.0
7
-
8
5
  attr_reader :num
9
6
  attr_reader :min
10
7
  attr_reader :max
@@ -23,12 +20,7 @@ module StatVal
23
20
  options
24
21
  end
25
22
 
26
-
27
- def all_keys ; [ :avg, :std, :min, :max, :num, :sum, :sq_sum, :avg_sq, :var ] end
28
- def default_keys ; [ :avg, :std, :min, :max, :num ] end
29
- def writable_keys ; [ :num, :min, :max, :sum, :sq_sum ] end
30
-
31
- alias_method :keys, :default_keys
23
+ def keys ; ::StatVal.keys(:default) end
32
24
 
33
25
  def [](key)
34
26
  case key
@@ -121,27 +113,8 @@ module StatVal
121
113
  end
122
114
  end
123
115
 
124
- def key_hash(which_keys = nil)
125
- return which_keys if which_keys.is_a?(Hash)
126
-
127
- case which_keys
128
- when nil then iter = keys
129
- when :all then iter = all_keys
130
- when :default then iter = default_keys
131
- when :writable then iter = writable_keys
132
- else
133
- if which_keys.repsond_to?(:each)
134
- iter = which_Keys
135
- else
136
- return { which_keys => which_keys }
137
- end
138
- end
139
-
140
- iter.inject({}) { |h, k| h[k] = k; h }
141
- end
142
-
143
- def to_hash(which_keys = nil)
144
- key_hash(which_keys).inject({}) { |h, (name, attr)| h[name] = self[attr]; h }
116
+ def to_hash(which_keys = nil, convert_to_s = false)
117
+ ::StatVal.key_hash(which_keys).inject({}) { |h, (attr, name)| h[(if convert_to_s then name.to_s else name end)] = self[attr]; h }
145
118
  end
146
119
 
147
120
  def to_s ; to_hash.to_s end
@@ -183,14 +156,39 @@ module StatVal
183
156
  @max = if new_val then new_val else (if empty? then NEG_INFINITY else avg+std end) end
184
157
  end
185
158
 
159
+ def std_ratio ; std / avg end
160
+
186
161
  private
187
162
 
188
- def abs_is_infinite(val) ; val.abs.to_f === POS_INFINITY end
163
+ POS_INFINITY = 1.0/0.0
164
+ NEG_INFINITY = - 1.0/0.0
189
165
 
166
+ def abs_is_infinite(val) ; val.abs.to_f === POS_INFINITY end
190
167
  def zero_if_unbounded(val) ; if bounded? then val else 0.0 end end
191
-
192
168
  def abs_div(nom, denom) ; nom.abs.to_f / denom end
169
+ end
193
170
 
171
+ def self.new(options = {}) ; StatVal.new(options) end
172
+
173
+ def self.all_keys ; [ :avg, :std, :std_ratio, :min, :max, :num, :sum, :sq_sum, :avg_sq, :var ] end
174
+ def self.default_keys ; [ :avg, :std, :min, :max, :num ] end
175
+ def self.writable_keys ; [ :num, :min, :max, :sum, :sq_sum ] end
176
+
177
+ def self.keys(ident = :default)
178
+ case ident
179
+ when :all then all_keys
180
+ when :writable then writable_keys
181
+ when :default then default_keys
182
+ when nil then default_keys
183
+ else
184
+ return ident if ident.respond_to?(:each)
185
+ return [ident]
186
+ end
187
+ end
188
+
189
+ def self.key_hash(which_keys = nil)
190
+ return which_keys if which_keys.is_a?(Hash)
191
+ keys(which_keys).inject({}) { |h, k| h[k] = k; h }
194
192
  end
195
193
 
196
194
  # Take hash that contains StatVal values and create new hash that is identical to it but has
@@ -211,6 +209,8 @@ module StatVal
211
209
  # raises on key conflict
212
210
  #
213
211
  def self.flatmap_hash(h, which_keys = nil, prefix=true, use_symbols=false)
212
+ return h.to_hash(which_keys, ! use_symbols) if h.kind_of?(StatVal)
213
+
214
214
  flat = {}
215
215
  h.each_pair do |k,r|
216
216
  if r.kind_of? StatVal
@@ -1,3 +1,3 @@
1
1
  module StatVal
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'set'
2
-
3
2
  require 'statval'
4
3
 
5
4
  module StatVal
@@ -75,6 +74,11 @@ module StatVal
75
74
  ::StatVal.flatmap_hash(@it).keys.to_set.should be == [ 'a', 'num_h', 'std_h', 'min_h', 'max_h', 'avg_h' ].to_set
76
75
  end
77
76
 
77
+ it 'renders statvals with key renaming as if they were hashes' do
78
+ @it = ::StatVal.flatmap_hash(StatVal.new, ::StatVal.key_hash(nil).tap {|h| h[:max] = :susi } ).keys.to_set
79
+ @it.should be == ['min', 'susi', 'num', 'std', 'avg'].to_set
80
+ end
81
+
78
82
  it 'times' do
79
83
  @it = StatVal.new
80
84
  @it.time {|| sleep(2) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statval
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: script
11
11
  cert_chain: []
12
- date: 2012-04-16 00:00:00.000000000 Z
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Utility class for incrementally recording measured values and reporting
15
15
  avg, variance, min, and max
@@ -43,9 +43,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- segments:
47
- - 0
48
- hash: -3832811953487468408
49
46
  required_rubygems_version: !ruby/object:Gem::Requirement
50
47
  none: false
51
48
  requirements: