to_histogram 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a497ddca60be2c0142f10ab562d09e0eb1ff402e
4
- data.tar.gz: 7b9f1c0d564baecb56efb0fd8b5da7285978ee12
3
+ metadata.gz: 9601f9e6c6772c4cf1d71f2963caa864ddafbfd7
4
+ data.tar.gz: dc6eaf01aae093f8ca0213fc561a876e015efdc5
5
5
  SHA512:
6
- metadata.gz: c97bad0682ad8cf60b4929194fd6b055ee6c7df5a5f4ceff6df4fba9c5851d97ef4f57e3ed1028742712f9779371731605d95e81b154c7842a268b78b6ee0c9e
7
- data.tar.gz: f1079ff1691fceb3a050b1eb467118aa403c534e8b99b99cc3c5c3622efe1d299e121ec8bc93cc78dc23a13ba720968275b6650797158aa62ee451b5ac3901c3
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
- puts "Min Value: #{@original_array[0]}, Max Value: #{@original_array[-1]}"
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 (out of #{total_data_value_length})", "Histogram (each * =~ 1%)")
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
- @original_array.reduce(:+) / @original_array.length
56
+ def mean(array)
57
+ array.reduce(:+) / array.length
51
58
  end
52
59
 
53
- def median
54
- @original_array[@original_array.length / 2]
60
+ def median(array)
61
+ array[array.length / 2]
55
62
  end
56
63
 
57
- def mode
58
- frequency = @original_array.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
64
+ def mode(array)
65
+ frequency = array.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
59
66
 
60
- @original_array.max_by { |v| frequency[v] }
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.7
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-02 00:00:00.000000000 Z
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.'