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, 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

#binsObject

Returns the value of attribute bins.



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

def bins
  @bins
end

#countObject (readonly)

Returns the value of attribute count.



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

def count
  @count
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#offsetObject (readonly)

Returns the value of attribute offset.



32
33
34
# File 'lib/geospatial/histogram.rb', line 32

def offset
  @offset
end

#scaleObject (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

#eachObject



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

#inspectObject



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

#peaksObject



90
91
92
# File 'lib/geospatial/histogram.rb', line 90

def peaks
	Peaks.new(self)
end

#sizeObject



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