to_histogram 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/bucketizer.rb +35 -0
- data/lib/histogram.rb +28 -0
- data/lib/stdout_print.rb +29 -0
- data/lib/to_histogram.rb +13 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 58c176192de9ba9ed43fb91778c36eed19fc1c5d
|
4
|
+
data.tar.gz: d830c2a73ac48d2e5d88f8ffbad6297c76414c58
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af37532d0ffd6715bbc0c4fb02e5692d81861cd5b10e160d8dad1dbaf1c89a2c863a88b9e4f3fe5b7181c9b9b4a1119e480ed15568eda53d490ca16ebcb5ed77
|
7
|
+
data.tar.gz: 86cad0be42831b5693b7559d68aabca95232a2bb0589da4ae0c995a7835ba4659285c383d10afe118e3864c254d9f3136c68ae9a521596ea5146d6ceb2c9cc78
|
data/lib/bucketizer.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module ToHistogram
|
2
|
+
|
3
|
+
class Bucketizer
|
4
|
+
|
5
|
+
def create_buckets(array, num_buckets)
|
6
|
+
arr = array.sort
|
7
|
+
bucket_increments = (arr[-1].to_f) / num_buckets
|
8
|
+
l_index = 0
|
9
|
+
next_bucket = bucket_increments
|
10
|
+
buckets = []
|
11
|
+
|
12
|
+
arr.each_with_index do |e, i|
|
13
|
+
if !(e <= next_bucket)
|
14
|
+
buckets << arr[l_index..(i - 1)]
|
15
|
+
|
16
|
+
# Special case where all of the results fit into the first bucket
|
17
|
+
if buckets[0].length == arr.length
|
18
|
+
l_index = (arr.length - 1)
|
19
|
+
break
|
20
|
+
end
|
21
|
+
|
22
|
+
l_index = i
|
23
|
+
next_bucket += bucket_increments
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if(l_index != (arr.length - 1))
|
28
|
+
buckets << arr[l_index..(arr.length - 1)]
|
29
|
+
end
|
30
|
+
|
31
|
+
return buckets
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/histogram.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative './bucketizer'
|
2
|
+
|
3
|
+
module ToHistogram
|
4
|
+
|
5
|
+
class Histogram
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(array, num_buckets)
|
9
|
+
bucketizer = Bucketizer.new()
|
10
|
+
@buckets = bucketizer.create_buckets(array, num_buckets)
|
11
|
+
end
|
12
|
+
|
13
|
+
def each(&block)
|
14
|
+
@buckets.each do |b|
|
15
|
+
yield b
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def [](i)
|
20
|
+
return @buckets[i]
|
21
|
+
end
|
22
|
+
|
23
|
+
def length
|
24
|
+
return @buckets.length
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/lib/stdout_print.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module ToHistogram
|
2
|
+
|
3
|
+
class StdoutPrint
|
4
|
+
def initialize(histogram)
|
5
|
+
@histogram = histogram
|
6
|
+
end
|
7
|
+
|
8
|
+
def invoke
|
9
|
+
last_value = nil
|
10
|
+
total_data_value_length = (@histogram.map { |b| b.length }).reduce(:+)
|
11
|
+
printf("%-20s %-20s %-30s %-20s \n\n", "range", "frequency", " percentage (out of #{total_data_value_length})", "histogram (percetage)")
|
12
|
+
|
13
|
+
@histogram.each do |b|
|
14
|
+
range = last_value.nil? ? "#{b[0]} to #{b[-1]}" : "#{last_value + 1} to #{b[-1]}"
|
15
|
+
frequency = b.length
|
16
|
+
percentage = ((frequency.to_f / total_data_value_length) * 100)
|
17
|
+
stars = ''
|
18
|
+
percentage.round.times { |x| stars << '*' }
|
19
|
+
|
20
|
+
printf("%-20s | %-20s | %-30s | %-20s \n", range, frequency, ('%.4f' % percentage), stars)
|
21
|
+
last_value = b[-1]
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "\n\n"
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/to_histogram.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative './histogram'
|
2
|
+
require_relative './stdout_print'
|
3
|
+
|
4
|
+
class Array
|
5
|
+
def to_histogram(num_buckets: 10)
|
6
|
+
histogram = ToHistogram::Histogram.new(self, num_buckets)
|
7
|
+
stdout_print = ToHistogram::StdoutPrint.new(histogram)
|
8
|
+
|
9
|
+
stdout_print.invoke
|
10
|
+
|
11
|
+
return histogram
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_histogram
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brett Sykes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Creates a simple histogram output on the command line. Usefull in IRB
|
14
|
+
sessions and command line programs
|
15
|
+
email: brettcsykes@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/bucketizer.rb
|
21
|
+
- lib/histogram.rb
|
22
|
+
- lib/stdout_print.rb
|
23
|
+
- lib/to_histogram.rb
|
24
|
+
homepage: https://github.com/bretts/to_histogram
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.4.5
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Creates a simple histogram output on the command line
|
48
|
+
test_files: []
|