Class: Shades::StreamReservoir
- Inherits:
-
Object
- Object
- Shades::StreamReservoir
- Defined in:
- lib/shades/histo.rb
Instance Method Summary collapse
- #add(bin) ⇒ Object
- #compress ⇒ Object
-
#histo_text(output_width) ⇒ Object
outputs a histogram of the form 0.502 ( 27) ############################## 1.108 ( 14) ############### 1.731 ( 7) ####### 2.343 ( 3) ### 3.138 ( 4) #### 3.968 ( 6) ###### 4.548 ( 4) #### 5.225 ( 2) ## 5.990 ( 2) ## 8.720 ( 1) # .
-
#initialize(max_size) ⇒ StreamReservoir
constructor
A new instance of StreamReservoir.
- #lines ⇒ Object
- #placement(bin) ⇒ Object
Constructor Details
#initialize(max_size) ⇒ StreamReservoir
Returns a new instance of StreamReservoir.
34 35 36 37 38 |
# File 'lib/shades/histo.rb', line 34 def initialize(max_size) @max_size = max_size @n = 0 @bins = [] end |
Instance Method Details
#add(bin) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/shades/histo.rb', line 40 def add(bin) @n += bin.count # bind the bin index to place this data i = placement(bin) @bins.insert(i, bin) end |
#compress ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/shades/histo.rb', line 56 def compress while @bins.length > @max_size min_gap_index = -1 min_gap = Float::MAX # find the bin covering the smallest range (@bins.length-1).times do |i| bin_a = @bins[i] bin_b = @bins[i+1] gap = bin_b.mean - bin_a.mean if min_gap > gap min_gap = gap min_gap_index = i end end # and merge that bin with the one to its right prevbin = @bins[min_gap_index] nextbin = @bins.delete_at(min_gap_index+1) prevbin.merge(nextbin) end end |
#histo_text(output_width) ⇒ Object
outputs a histogram of the form 0.502 ( 27) ############################## 1.108 ( 14) ############### 1.731 ( 7) ####### 2.343 ( 3) ### 3.138 ( 4) #### 3.968 ( 6) ###### 4.548 ( 4) #### 5.225 ( 2) ## 5.990 ( 2) ## 8.720 ( 1) #
So, the line above that reads “0.502 ( 27) ##############################” can be read as: “There are 27 values close to 0.502”
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/shades/histo.rb', line 99 def histo_text(output_width) a = [] max_bin_count = 1 @bins.each do |b| if b.count > max_bin_count max_bin_count = b.count end end scaled_max = yield max_bin_count @bins.each do |b| scaled_value = yield b.count repeat = (output_width * ( Float(scaled_value) / Float(scaled_max) )).to_i a << "%14.3f (%5d) %s" % [b.mean, b.count, '#' * repeat] end a.join("\n") end |
#lines ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/shades/histo.rb', line 77 def lines a = [] @bins.each do |b| a << "%8d %10.4f" % [b.count, b.mean] end a.join("\n") end |
#placement(bin) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/shades/histo.rb', line 47 def placement(bin) @bins.length.times do |i| if @bins[i].mean >= bin.mean return i end end return @bins.length end |