to_histogram 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bucketizer.rb +7 -1
- data/lib/histogram.rb +2 -1
- data/lib/stdout_print.rb +17 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9601f9e6c6772c4cf1d71f2963caa864ddafbfd7
|
4
|
+
data.tar.gz: dc6eaf01aae093f8ca0213fc561a876e015efdc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07b9ee278e369bcc7d1349094b0d99d05e2db082a87d4fde855dca8a447830a2731f96b475be81dfa6954d262bdb76e3239da591e3d29cb5ee12b44606980fa4
|
7
|
+
data.tar.gz: 9328e8cb97e0dfebbc213f4eebb1a7c18cbc78e6218380b19b57e8d20946d50db88665b8e613c4d7266a103740e4255aae2b93eee54cfa288ee81025cba43925
|
data/lib/bucketizer.rb
CHANGED
@@ -5,15 +5,21 @@ module ToHistogram
|
|
5
5
|
def initialize(array, num_buckets, percentile)
|
6
6
|
@arr = array.sort
|
7
7
|
@num_buckets = num_buckets
|
8
|
+
@percentile =percentile
|
8
9
|
@bucket_increments = get_bucket_increment(percentile)
|
9
10
|
end
|
10
|
-
attr_reader :bucket_increments
|
11
|
+
attr_reader :bucket_increments, :arr
|
11
12
|
|
12
13
|
def create_buckets
|
13
14
|
l_index = 0
|
14
15
|
next_bucket = @bucket_increments
|
15
16
|
buckets = []
|
16
17
|
|
18
|
+
# Remove any elements outside of the percentile
|
19
|
+
if(@percentile != 100)
|
20
|
+
@arr = @arr[0..(@arr.length * (@percentile / 100.0) - 1).to_i]
|
21
|
+
end
|
22
|
+
|
17
23
|
# Deal with the special case where we have elements that == 0 and an increment sizes of 1 (count 0 as a value and don't lump it in with 1)
|
18
24
|
if(@arr.count(0) > 0 && next_bucket == 1)
|
19
25
|
bucket_0 = []
|
data/lib/histogram.rb
CHANGED
@@ -11,8 +11,9 @@ module ToHistogram
|
|
11
11
|
@increments = bucketizer.bucket_increments
|
12
12
|
@percentile = percentile
|
13
13
|
@num_buckets = num_buckets
|
14
|
+
@arr = bucketizer.arr
|
14
15
|
end
|
15
|
-
attr_reader :increments, :percentile, :num_buckets
|
16
|
+
attr_reader :increments, :percentile, :num_buckets, :arr
|
16
17
|
|
17
18
|
def each(&block)
|
18
19
|
@buckets.each do |b|
|
data/lib/stdout_print.rb
CHANGED
@@ -16,16 +16,23 @@ module ToHistogram
|
|
16
16
|
def print_header
|
17
17
|
puts "\n**************************************************************"
|
18
18
|
puts "Results for #to_histogram(num_buckets: #{@histogram.num_buckets}, percentile: #{@histogram.percentile}, print_info: true)"
|
19
|
+
|
20
|
+
puts "\n"
|
21
|
+
|
22
|
+
percentile_info = (@histogram.percentile == 100) ? '' : "(Numbers limited to the #{@histogram.percentile}th percentile)"
|
23
|
+
puts "Data set used in this calculation #{percentile_info}"
|
24
|
+
puts "Data set Size: #{@histogram.arr.length} items"
|
25
|
+
puts "Min Value: #{@histogram.arr[0]}, Max Value: #{@histogram.arr[-1]}"
|
26
|
+
puts "Mean: #{mean(@histogram.arr)}, Median: #{median(@histogram.arr)}, Mode: #{mode(@histogram.arr)}"
|
19
27
|
puts "\n"
|
20
|
-
|
21
|
-
puts "Mean: #{mean}, Median: #{median}, Mode: #{mode}"
|
28
|
+
|
22
29
|
puts "Histogram bucket sizes: #{@histogram.increments}"
|
23
30
|
puts "**************************************************************\n\n"
|
24
31
|
end
|
25
32
|
|
26
33
|
def print_body
|
27
34
|
total_data_value_length = (@histogram.map { |b| b.length }).reduce(:+)
|
28
|
-
printf("%-20s %-20s %-30s %-20s \n\n", "Range", "Frequency", " Percentage
|
35
|
+
printf("%-20s %-20s %-30s %-20s \n\n", "Range", "Frequency", " Percentage", "Histogram (each * =~ 1%)")
|
29
36
|
|
30
37
|
@histogram.each_with_index do |b, i|
|
31
38
|
next_bucket = (@histogram[i + 1]) ? @histogram[i + 1][0] : b[-1]
|
@@ -46,18 +53,18 @@ module ToHistogram
|
|
46
53
|
end
|
47
54
|
end
|
48
55
|
|
49
|
-
def mean
|
50
|
-
|
56
|
+
def mean(array)
|
57
|
+
array.reduce(:+) / array.length
|
51
58
|
end
|
52
59
|
|
53
|
-
def median
|
54
|
-
|
60
|
+
def median(array)
|
61
|
+
array[array.length / 2]
|
55
62
|
end
|
56
63
|
|
57
|
-
def mode
|
58
|
-
frequency =
|
64
|
+
def mode(array)
|
65
|
+
frequency = array.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
|
59
66
|
|
60
|
-
|
67
|
+
array.max_by { |v| frequency[v] }
|
61
68
|
end
|
62
69
|
|
63
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_histogram
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brett Sykes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Adds #to_histogram to Array. Returns a histogram distribution object
|
14
14
|
from an Array and optionally prints detailed info to stdout.'
|