histogram 0.2.3.0 → 0.2.4.1

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: 87b16b66fb1ed20c22d818250d368d39fc3445fc
4
- data.tar.gz: f6bf7bd712834fc3028a6e8c2ade8a20274ce004
3
+ metadata.gz: 1c90147f3e7e173081d8ac028ef4e5e1068518d5
4
+ data.tar.gz: e1abfe94cfdf162c8308c7d2a3582427b42a4a01
5
5
  SHA512:
6
- metadata.gz: d19f5747ea74d6a9b70d9b9c22a0d0321f2be8b82679fbd1cbd4226bc9fae828775ded31c7c033bd3153c3528d5b238c1a8ff37bd6e77dcf2759137700fb690a
7
- data.tar.gz: 6dadf8f32857060dc8b70bf30493f21b9a63e8caaf99f612a4db42877bc6a00350775cd24d6e9c38b24042cb7b1c6c9bc6be6aeac43d3f040dabd0e13e751d5a
6
+ metadata.gz: cc1f1267f0b6225201c9fafdc493b0a1efd8fca53e44bd87f1193b46c0639e062f80dd0bfca996789221b734e95ad35d119cc50813f08009333292d7c17db7a3
7
+ data.tar.gz: 50e6c5896bf2e0755ba2dbe81dfe0f73dbebe46ec4531483ffeb83562ed3a158a37019cf092a2375b00f882655733de8cd2939f41967a7c566172548af57932a
@@ -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
- conv = _max == _min ? 0 : bins.to_f/(_max - _min)
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 bins == 1
350
- _bins[0] = self.to_a.inject(0.0) {|sum, val| sum + val } / self.size
351
- else
352
- (0...bins).each do |i|
353
- _bins[i] = ((i+0.5) * iconv) + dmin
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 bins == 1
358
- _bins[0] = self.min
359
- else
360
- (0...bins).each do |i|
361
- _bins[i] = (i * iconv) + dmin
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
@@ -1,3 +1,3 @@
1
1
  module Histogram
2
- VERSION = "0.2.3.0"
2
+ VERSION = "0.2.4.1"
3
3
  end
@@ -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.3.0
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-09-08 00:00:00.000000000 Z
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler