Class: Geospatial::Histogram
- Inherits:
-
Object
- Object
- Geospatial::Histogram
- Defined in:
- lib/geospatial/histogram.rb
Overview
This location is specifically relating to a WGS84 coordinate on Earth.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bins ⇒ Object
Returns the value of attribute bins.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#scale ⇒ Object
readonly
Returns the value of attribute scale.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #add(value, amount = 1, item: nil) ⇒ Object
- #each ⇒ Object
-
#initialize(min: 0, max: 1, scale: 0.1, items: true) ⇒ Histogram
constructor
A new instance of Histogram.
- #inspect ⇒ Object
- #map(value) ⇒ Object
- #peaks ⇒ Object
- #size ⇒ Object
- #unmap(index) ⇒ Object
Constructor Details
#initialize(min: 0, max: 1, scale: 0.1, items: true) ⇒ Histogram
Returns a new instance of Histogram.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/geospatial/histogram.rb', line 10 def initialize(min: 0, max: 1, scale: 0.1, items: true) @min = min @max = max @scale = scale @count = 0 if items @items = Hash.new{|h,k| h[k] = Array.new} end @size = ((@max - @min) / @scale).ceil @bins = [0] * @size @offset = 0 @scale = scale end |
Instance Attribute Details
#bins ⇒ Object
Returns the value of attribute bins.
27 28 29 |
# File 'lib/geospatial/histogram.rb', line 27 def bins @bins end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
30 31 32 |
# File 'lib/geospatial/histogram.rb', line 30 def count @count end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
28 29 30 |
# File 'lib/geospatial/histogram.rb', line 28 def items @items end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
32 33 34 |
# File 'lib/geospatial/histogram.rb', line 32 def offset @offset end |
#scale ⇒ Object (readonly)
Returns the value of attribute scale.
33 34 35 |
# File 'lib/geospatial/histogram.rb', line 33 def scale @scale end |
Instance Method Details
#[](index) ⇒ Object
41 42 43 |
# File 'lib/geospatial/histogram.rb', line 41 def [] index @bins[index] end |
#add(value, amount = 1, item: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/geospatial/histogram.rb', line 57 def add(value, amount = 1, item: nil) index = map(value).floor % @size if !block_given? or yield(index, value) @count += 1 @bins[index] += amount if @items and item @items[index] << item end end return index end |
#each ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/geospatial/histogram.rb', line 82 def each return to_enum unless block_given? @bins.each_with_index do |value, index| yield unmap(index), value end end |
#inspect ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/geospatial/histogram.rb', line 72 def inspect buffer = String.new("\#<#{self.class}") @bins.each_with_index do |bin, index| buffer << " #{unmap(index)}: #{bin}" end buffer << ">" end |
#map(value) ⇒ Object
49 50 51 |
# File 'lib/geospatial/histogram.rb', line 49 def map(value) ((value - @min) / @scale) end |
#peaks ⇒ Object
90 91 92 |
# File 'lib/geospatial/histogram.rb', line 90 def peaks Peaks.new(self) end |
#size ⇒ Object
45 46 47 |
# File 'lib/geospatial/histogram.rb', line 45 def size @bins.size end |
#unmap(index) ⇒ Object
53 54 55 |
# File 'lib/geospatial/histogram.rb', line 53 def unmap(index) @min + (index * @scale) end |