Skip to content

Commit

Permalink
set bins to same value if min == max
Browse files Browse the repository at this point in the history
  • Loading branch information
jtprince committed Oct 3, 2015
1 parent 73d32ea commit b70d40f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
28 changes: 13 additions & 15 deletions lib/histogram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def iqrange(obj, opts={})
hi_idx += 1 unless sz.even?
median(srted[hi_idx..-1]) - median(srted[0..lo_idx])
else
raise ArgumentError, "method must be :tukey"
raise ArgumentError, "method must be :tukey or :moore_mccabe"
end
answer.to_f
end
Expand Down Expand Up @@ -318,9 +318,9 @@ def histogram(*args)
# NUMBER OF BINS:
########################################################
# Create the scaling factor

dmin = _min.to_f
conv = _max == _min ? 0 : bins.to_f/(_max - _min)
min_equals_max = _max == _min
conv = min_equals_max ? 0 : bins.to_f/(_max - _min)

_bins =
if self.is_a?(Array)
Expand Down Expand Up @@ -352,20 +352,18 @@ def histogram(*args)
iconv = 1.0/conv
case bin_boundary
when :avg
if bins == 1
_bins[0] = self.to_a.inject(0.0) {|sum, val| sum + val } / self.size
else
(0...bins).each do |i|
_bins[i] = ((i+0.5) * iconv) + dmin
end
if min_equals_max
set_bin_value = self.to_a.inject(0.0) {|sum, val| sum + val } / self.size
end
(0...bins).each do |i|
_bins[i] = min_equals_max ? set_bin_value : ((i+0.5) * iconv) + dmin
end
when :min
if bins == 1
_bins[0] = self.min
else
(0...bins).each do |i|
_bins[i] = (i * iconv) + dmin
end
if min_equals_max
set_bin_value = self.min
end
(0...bins).each do |i|
_bins[i] = min_equals_max ? set_bin_value : (i * iconv) + dmin
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/histogram/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Histogram
VERSION = "0.2.4.0"
VERSION = "0.2.4.1"
end
6 changes: 6 additions & 0 deletions spec/histogram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def round(n=nil)
freq.to_a.should == [5]
end

it 'sets all bin values to the same if min equals max' do
bins, freq = obj6.histogram(2)
bins.to_a.should == [0.0, 0.0]
freq.to_a.should == [5.0, 0.0]
end

end

describe Histogram do
Expand Down

0 comments on commit b70d40f

Please sign in to comment.