histogram 0.2.3.0 → 0.2.4.1
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/histogram.rb +19 -15
- data/lib/histogram/version.rb +1 -1
- data/spec/histogram_spec.rb +12 -0
- 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: 1c90147f3e7e173081d8ac028ef4e5e1068518d5
|
4
|
+
data.tar.gz: e1abfe94cfdf162c8308c7d2a3582427b42a4a01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc1f1267f0b6225201c9fafdc493b0a1efd8fca53e44bd87f1193b46c0639e062f80dd0bfca996789221b734e95ad35d119cc50813f08009333292d7c17db7a3
|
7
|
+
data.tar.gz: 50e6c5896bf2e0755ba2dbe81dfe0f73dbebe46ec4531483ffeb83562ed3a158a37019cf092a2375b00f882655733de8cd2939f41967a7c566172548af57932a
|
data/lib/histogram.rb
CHANGED
@@ -72,7 +72,7 @@ module Histogram
|
|
72
72
|
hi_idx += 1 unless sz.even?
|
73
73
|
median(srted[hi_idx..-1]) - median(srted[0..lo_idx])
|
74
74
|
else
|
75
|
-
raise ArgumentError, "method must be :tukey"
|
75
|
+
raise ArgumentError, "method must be :tukey or :moore_mccabe"
|
76
76
|
end
|
77
77
|
answer.to_f
|
78
78
|
end
|
@@ -88,11 +88,15 @@ module Histogram
|
|
88
88
|
#
|
89
89
|
# middle is the median between the other three values
|
90
90
|
#
|
91
|
+
# Note: always returns 1 if all values are the same.
|
92
|
+
#
|
91
93
|
# inspired by {Richard Cotton's matlab
|
92
94
|
# implementation}[http://www.mathworks.com/matlabcentral/fileexchange/21033-calculate-number-of-bins-for-histogram]
|
93
95
|
# and the {histogram page on
|
94
96
|
# wikipedia}[http://en.wikipedia.org/wiki/Histogram]
|
95
97
|
def number_of_bins(methd=DEFAULT_BIN_METHOD, quartile_method=DEFAULT_QUARTILE_METHOD)
|
98
|
+
return 1 if self.to_a.uniq.size == 1
|
99
|
+
|
96
100
|
if methd == :middle
|
97
101
|
[:scott, :sturges, :fd].map {|v| number_of_bins(v) }.sort[1]
|
98
102
|
else
|
@@ -171,6 +175,8 @@ module Histogram
|
|
171
175
|
# (so, values lower than first bin are not included, but all values
|
172
176
|
# higher, than last bin are included. Current implementation of custom
|
173
177
|
# bins is slow.
|
178
|
+
# * If the number of bins must be determined and all values are the same,
|
179
|
+
# will use 1 bin.
|
174
180
|
# * if other_sets are supplied, the same bins will be used for all the sets.
|
175
181
|
# It is useful if you just want a certain number of bins and for the sets
|
176
182
|
# to share the exact same bins. In this case returns [bins, freqs(caller),
|
@@ -312,9 +318,9 @@ module Histogram
|
|
312
318
|
# NUMBER OF BINS:
|
313
319
|
########################################################
|
314
320
|
# Create the scaling factor
|
315
|
-
|
316
321
|
dmin = _min.to_f
|
317
|
-
|
322
|
+
min_equals_max = _max == _min
|
323
|
+
conv = min_equals_max ? 0 : bins.to_f/(_max - _min)
|
318
324
|
|
319
325
|
_bins =
|
320
326
|
if self.is_a?(Array)
|
@@ -346,20 +352,18 @@ module Histogram
|
|
346
352
|
iconv = 1.0/conv
|
347
353
|
case bin_boundary
|
348
354
|
when :avg
|
349
|
-
if
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
end
|
355
|
+
if min_equals_max
|
356
|
+
set_bin_value = self.to_a.inject(0.0) {|sum, val| sum + val } / self.size
|
357
|
+
end
|
358
|
+
(0...bins).each do |i|
|
359
|
+
_bins[i] = min_equals_max ? set_bin_value : ((i+0.5) * iconv) + dmin
|
355
360
|
end
|
356
361
|
when :min
|
357
|
-
if
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
end
|
362
|
+
if min_equals_max
|
363
|
+
set_bin_value = self.min
|
364
|
+
end
|
365
|
+
(0...bins).each do |i|
|
366
|
+
_bins[i] = min_equals_max ? set_bin_value : (i * iconv) + dmin
|
363
367
|
end
|
364
368
|
end
|
365
369
|
end
|
data/lib/histogram/version.rb
CHANGED
data/spec/histogram_spec.rb
CHANGED
@@ -110,6 +110,18 @@ shared_examples 'something that can histogram' do
|
|
110
110
|
bins, freq = obj8.histogram
|
111
111
|
end
|
112
112
|
|
113
|
+
it 'uses 1 bin if all values are the same' do
|
114
|
+
bins, freq = obj6.histogram(:sturges)
|
115
|
+
bins.to_a.should == [0]
|
116
|
+
freq.to_a.should == [5]
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'sets all bin values to the same if min equals max' do
|
120
|
+
bins, freq = obj6.histogram(2)
|
121
|
+
bins.to_a.should == [0.0, 0.0]
|
122
|
+
freq.to_a.should == [5.0, 0.0]
|
123
|
+
end
|
124
|
+
|
113
125
|
end
|
114
126
|
|
115
127
|
describe Histogram do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: histogram
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John T. Prince
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|