Class: Geospatial::Histogram

Inherits:
Object
  • Object
show all
Defined in:
lib/geospatial/histogram.rb

Overview

This location is specifically relating to a WGS84 coordinate on Earth.

Direct Known Subclasses

RadialHistogram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min: 0, max: 1, scale: 0.1) ⇒ Histogram

Returns a new instance of Histogram.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/geospatial/histogram.rb', line 10

def initialize(min: 0, max: 1, scale: 0.1)
  @min = min
  @max = max
  @scale = scale
  
  @count = 0
  
  @size = ((@max - @min) / @scale).ceil
  @bins = [0] * @size
  @offset = 0
  @scale = scale
end

Instance Attribute Details

#binsObject

Returns the value of attribute bins.



23
24
25
# File 'lib/geospatial/histogram.rb', line 23

def bins
  @bins
end

#countObject (readonly)

Returns the value of attribute count.



25
26
27
# File 'lib/geospatial/histogram.rb', line 25

def count
  @count
end

#offsetObject (readonly)

Returns the value of attribute offset.



27
28
29
# File 'lib/geospatial/histogram.rb', line 27

def offset
  @offset
end

#scaleObject (readonly)

Returns the value of attribute scale.



28
29
30
# File 'lib/geospatial/histogram.rb', line 28

def scale
  @scale
end

Instance Method Details

#[](index) ⇒ Object



30
31
32
# File 'lib/geospatial/histogram.rb', line 30

def [] index
  @bins[index]
end

#add(value, amount = 1) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/geospatial/histogram.rb', line 46

def add(value, amount = 1)
  index = map(value).floor % @size
  
  if !block_given? or yield(index, value)
    @count += 1
    @bins[index] += amount
  end
  
  return index
end

#eachObject



67
68
69
70
71
72
73
# File 'lib/geospatial/histogram.rb', line 67

def each
  return to_enum unless block_given?
  
  @bins.each_with_index do |value, index|
    yield unmap(index), value
  end
end

#inspectObject



57
58
59
60
61
62
63
64
65
# File 'lib/geospatial/histogram.rb', line 57

def inspect
  buffer = String.new("\#<#{self.class}")
  
  @bins.each_with_index do |bin, index|
    buffer << " #{unmap(index)}: #{bin}"
  end
  
  buffer << ">"
end

#map(value) ⇒ Object



38
39
40
# File 'lib/geospatial/histogram.rb', line 38

def map(value)
  ((value - @min) / @scale)
end

#peaksObject



75
76
77
# File 'lib/geospatial/histogram.rb', line 75

def peaks
  Peaks.new(self)
end

#sizeObject



34
35
36
# File 'lib/geospatial/histogram.rb', line 34

def size
  @bins.size
end

#unmap(index) ⇒ Object



42
43
44
# File 'lib/geospatial/histogram.rb', line 42

def unmap(index)
  @min + (index * @scale)
end