binneroc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d70ff7c6d7bfa6b7f32f6a2fa5f685f8ac1b53d5
4
+ data.tar.gz: f52335a79ed002f66961a5652198ff6a58c841db
5
+ SHA512:
6
+ metadata.gz: b81eaa4e1c4b1236690e091b263efe898299dd226b247f98f205cc1c5131d07c48eb37f7bce8fefee504ad46a2297a54fe2a39c2738ec345cdd470ae8a8add5a
7
+ data.tar.gz: 327fae84a3591fedf8f0938f3e7f13cca1a5fe9b06767b36583efe890000158f2939d8a5a31bb0f088c3392da0c988d210acdea2274a6fcdba0585b5ff020d69
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in binneroc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2014 Brigham Young University
2
+ author: John T. Prince
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Binneroc
2
+
3
+ Bins in constant time, O(c) or O(1).
4
+
5
+ # Example
6
+
7
+ My use case is a spectrum of x,y data points (an x position associated with
8
+ some intensity y). You would like to bin the signal into discrete bin points
9
+ such that all the intensity is associated with equally spaced bins. The
10
+ simple algorithm used here finds the proper bin in constant time.
11
+
12
+ xvals = [3.3, 4.5, 6.6]
13
+ yvals = [4, 10, 2]
14
+
15
+ (newx, newy) = Binneroc.bin(xvals, yvals)
16
+
17
+ ## Installation
18
+
19
+ gem install binneroc
20
+
21
+ ## Copyright
22
+
23
+ MIT License. See LICENSE.txt.
24
+
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task :default => :spec
10
+
11
+ require 'rdoc/task'
12
+ Rake::RDocTask.new do |rdoc|
13
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
14
+
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = "rubabel #{version}"
17
+ rdoc.rdoc_files.include('README*')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ end
data/binneroc.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'binneroc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "binneroc"
8
+ spec.version = Binneroc::VERSION
9
+ spec.authors = ["John T. Prince"]
10
+ spec.email = ["jtprince@gmail.com"]
11
+ spec.summary = %q{bins x, y data into discrete bins using constant time binning}
12
+ spec.description = %q{bins x, y data into discrete bins using constant time binning. Useful.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ [
22
+ #["trollop", "~> 2.0.0"],
23
+ ].each do |args|
24
+ spec.add_dependency(*args)
25
+ end
26
+
27
+ [
28
+ ["bundler", "~> 1.5.3"],
29
+ ["rake"],
30
+ ["rspec", "~> 2.14.1"],
31
+ ["rdoc", "~> 4.1.0"],
32
+ ["simplecov", "~> 0.8.2"],
33
+ ].each do |args|
34
+ spec.add_development_dependency(*args)
35
+ end
36
+
37
+ end
@@ -0,0 +1,3 @@
1
+ module Binneroc
2
+ VERSION = "0.0.1"
3
+ end
data/lib/binneroc.rb ADDED
@@ -0,0 +1,192 @@
1
+ require "binneroc/version"
2
+ require 'bigdecimal'
3
+
4
+ module Binneroc
5
+
6
+ class << self
7
+ # if converting from a floating point number, the precision to use
8
+ BIG_DECIMAL_PRECISION = 10
9
+
10
+ # returns (new_x_coords, new_y_coords)
11
+ #
12
+ # each bin is the *minimum* for that bin. i.e., the bin includes all
13
+ # values starting at that point non-inclusive to that point + increment.
14
+ #
15
+ # Example:
16
+ #
17
+ #
18
+ # Note: The final bin + increment may exceed the :stop value.
19
+ # Note: Values falling outside the range of start to final bin + increment
20
+ # will not be included.
21
+ #
22
+ # xvals = the current x coordinates
23
+ # yvals = the parallel y values
24
+ #
25
+ # start = the starting x point for binning
26
+ # if nil then floor of lowest xvals value
27
+ # stop = no more bins created after stop
28
+ # if nil then ceil of highest xvals value
29
+ # increment = the x coordinate (binning) increment
30
+ # exclude_end = *false|true how to treat stop value
31
+ #
32
+ # default = the default value if no values lie in a bin
33
+ # note that values are not added on top of the default
34
+ # it is just the value used if there are no values for a bin
35
+ #
36
+ # consider_default = true: treat the default value as the first value in the bin
37
+ # false: default considered only if no values fall in the bin
38
+ #
39
+ # outputclass = (Array) make new arrays using this class
40
+ # needs to accept
41
+ # ::new(array)
42
+ # ::new(size)
43
+ # ::new(size, default)
44
+ #
45
+ # behavior = response when multiple values fall to the same bin
46
+ # sum => sums all values
47
+ # # below are not yet implemented but could easily be:
48
+ # avg => avgs the values # not yet implemented
49
+ # max_x => takes the value at the highest x coordinate
50
+ # max_y => takes the value of the highest y value (even if lower than baseline)
51
+ # max_yb => takes the highest y value (includes the baseline)
52
+ #
53
+ # return_xvals = true|false (default true)
54
+ #
55
+ # outputclass needs to be able to create a new vector like object with the
56
+ # arguments outputclass.new(size, value) or outputclass.new(size).
57
+ def bin(xvals, yvals, start: nil, stop: nil, increment: 1.0, default: 0.0, behavior: :sum, outputclass: Array, return_xvals: true, exclude_end: false, consider_default: false)
58
+ raise ArgumentError, "xvals and yvals need to be parallel (same size)!" unless xvals.size == yvals.size
59
+
60
+ if xvals.size == 0
61
+ if return_xvals
62
+ return [[],[]]
63
+ else
64
+ return []
65
+ end
66
+ end
67
+
68
+ increment = increment.to_f unless increment.is_a?(Float)
69
+
70
+ if start.nil? && stop.nil?
71
+ (min, max) = xvals.minmax
72
+ elsif start.nil?
73
+ min = xvals.min
74
+ stop = stop.to_f
75
+ elsif stop.nil?
76
+ max = xvals.max
77
+ start = start.to_f
78
+ end
79
+ start = min.floor.to_f if min
80
+ stop = max.ceil.to_f if max
81
+
82
+ (startbd, stopbd, incrementbd) = [start, stop, increment].map do |v|
83
+ if v.is_a?(Float)
84
+ BigDecimal.new(v, BIG_DECIMAL_PRECISION)
85
+ else
86
+ BigDecimal.new(v)
87
+ end
88
+ end
89
+
90
+ newsize = array_size(startbd, stopbd, incrementbd, exclude_end)
91
+ range = Range.new(startbd, startbd + (incrementbd * newsize), exclude_end)
92
+
93
+ yvec_new = outputclass.new(newsize, default)
94
+ index_bounds = (0...newsize)
95
+
96
+ case behavior
97
+ when :sum
98
+ if consider_default
99
+ xvals.zip(yvals) do |x, y|
100
+ index = ((x-startbd) / incrementbd).floor # round??
101
+ if index_bounds===(index)
102
+ yvec_new[index] += y
103
+ end
104
+ end
105
+ else
106
+ no_default = outputclass.new(newsize, 0.0)
107
+ xvals.zip(yvals) do |x, y|
108
+ index = ((x-startbd) / incrementbd).floor # round??
109
+ if index_bounds===(index)
110
+ yvec_new[index] = no_default[index] + y
111
+ no_default[index] += y
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ if return_xvals
118
+ new_xvals = range.step(incrementbd).to_a
119
+ new_xvals.pop if new_xvals.size > newsize
120
+ [outputclass.new(new_xvals), yvec_new]
121
+ else
122
+ yvec_new
123
+ end
124
+ end
125
+
126
+ # calculates the number of items that should be in the array.
127
+ def array_size(start, stop, step, exclude_end=false)
128
+ fractional_arr_sz = (stop - start) / step
129
+ array_size_floor = fractional_arr_sz.floor
130
+ array_size_floor += 1 unless exclude_end && (array_size_floor == fractional_arr_sz)
131
+ array_size_floor.to_i
132
+ end
133
+
134
+ end
135
+ end
136
+
137
+
138
+
139
+ ## reference from old behavior
140
+
141
+ ### CREATE array to hold mapped values and write in the baseline
142
+ #yvec_new = outputclass.new(max_ind+1-start_scaled, baseline)
143
+ #nobl = outputclass.new(max_ind+1, 0) unless behavior == :max_x
144
+
145
+ #case behavior
146
+ #when :sum
147
+ #xvec_scaled.each_with_index do |ind,i|
148
+ #val = yvec[i]
149
+ #yvec_new[ind-lshift] = nobl[ind] + val
150
+ #nobl[ind] += val
151
+ #end
152
+ #when :max_x ## FASTEST BEHAVIOR
153
+ #xvec_scaled.each_with_index do |ind,i|
154
+ #yvec_new[ind-lshift] = yvec[i]
155
+ #end
156
+ #when :avg
157
+ #count = Hash.new {|s,key| s[key] = 0 }
158
+ #xvec_scaled.each_with_index do |ind,i|
159
+ #val = yvec[i]
160
+ #yvec_new[ind-lshift] = nobl[ind] + val
161
+ #nobl[ind] += val
162
+ #count[ind] += 1
163
+ #end
164
+ #count.each do |k,co|
165
+ #if co > 1; yvec_new[k-lshift] /= co end
166
+ #end
167
+ #when :max_y
168
+ #xvec_scaled.each_with_index do |ind,i|
169
+ #val = yvec[i]
170
+ #if val > nobl[ind]
171
+ #yvec_new[ind-lshift] = val
172
+ #nobl[ind] = val
173
+ #end
174
+ #end
175
+ #when :max_yb
176
+ #xvec_scaled.each_with_index do |ind,i|
177
+ #val = yvec[i]
178
+ #if val > yvec_new[ind-lshift]
179
+ #yvec_new[ind-lshift] = val
180
+ #end
181
+ #end
182
+ #else
183
+ #abort "Not a valid behavior: #{behavior.inspect}"
184
+ #end
185
+
186
+ #if return_new_xvals
187
+ #xvec_new = outputclass.new(newsize)
188
+ #Range.new(start, stop, exclude_end).step(increment).each_with_index do |v,i|
189
+ #xvec_new[i] = v
190
+ #end
191
+ #end
192
+
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ require 'binneroc'
4
+
5
+ describe 'determining the number of bins' do
6
+ specify 'not excluding the end' do
7
+ args = [0.0, 1.0, 0.1, false]
8
+
9
+ ans = Binneroc.array_size(*args)
10
+ expect(ans).to eq 11
11
+ end
12
+
13
+ specify 'excluding the end' do
14
+ args = [0.0, 1.0, 0.1, true]
15
+
16
+ ans = Binneroc.array_size(*args)
17
+ expect(ans).to eq 10
18
+ end
19
+
20
+ specify 'finding with other weird values' do
21
+ args = [0.0, 1.0, 0.4, false]
22
+
23
+ ans = Binneroc.array_size(*args)
24
+ expect(ans).to eq 3
25
+ end
26
+
27
+ end
28
+
29
+ describe 'binning with no data' do
30
+ it 'works' do
31
+ (xs, ys) = Binneroc.bin([],[])
32
+ expect(xs).to eq []
33
+ expect(ys).to eq []
34
+
35
+ reply = Binneroc.bin([],[], return_xvals: false)
36
+ expect(reply).to eq []
37
+ end
38
+ end
39
+
40
+ describe 'super simple case' do
41
+ let(:vals) {
42
+ [
43
+ [3.15],
44
+ [ 4]
45
+ ]
46
+ }
47
+
48
+ specify 'does not bin values outside the range' do
49
+ reply = Binneroc.bin(*vals, start: 3.5, stop: 4.0, increment: 1.0)
50
+ expect(reply).to eq [[3.5], [0.0]]
51
+ end
52
+
53
+ specify 'does not exclude the end point by default' do
54
+ reply = Binneroc.bin(*vals, start: 3.0, stop: 4.0, increment: 1.0)
55
+ expect(reply).to eq [[3.0, 4.0], [4, 0]]
56
+ end
57
+
58
+ specify 'can exclude the final end value' do
59
+ reply = Binneroc.bin(*vals, start: 3.0, stop: 4.0, exclude_end: true, increment: 1.0)
60
+ expect(reply).to eq [[3.0], [4]]
61
+ end
62
+
63
+ specify 'can use a default value' do
64
+ reply = Binneroc.bin(*vals, start: 3.0, stop: 4.0, exclude_end: false, increment: 1.0, default: 7.7)
65
+ expect(reply).to eq [[3.0, 4.0], [4.0, 7.7]]
66
+ end
67
+
68
+ specify 'can add to a default value' do
69
+ reply = Binneroc.bin(*vals, start: 3.0, stop: 4.0, exclude_end: false, increment: 1.0, default: 7.7, consider_default: true)
70
+ expect(reply).to eq [[3.0, 4.0], [11.7, 7.7]]
71
+ end
72
+
73
+ end
74
+
75
+ describe 'iconic case' do
76
+ let(:vals) {
77
+ [
78
+ [4.0, 4.01, 4.15, 4.2, 5.0],
79
+ [10, 20, 30, 40, 50]
80
+ ]
81
+ }
82
+
83
+ specify 'works' do
84
+ reply = Binneroc.bin(*vals, start: 4.0, stop: 5.0, increment: 0.1)
85
+ expect(reply).to eq [(4..5).step(0.1).to_a, [30.0, 30.0, 40.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 50.0]]
86
+ end
87
+
88
+ specify 'accepts string values (for BigDecimal)' do
89
+ reply = Binneroc.bin(*vals, start: "4.0", stop: "5.0", increment: "0.1")
90
+ expect(reply).to eq [(4..5).step(0.1).to_a, [30.0, 30.0, 40.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 50.0]]
91
+ end
92
+
93
+ specify 'can avoid returning the x values' do
94
+ reply = Binneroc.bin(*vals, start: "4.0", stop: "5.0", increment: "0.1", return_xvals: false)
95
+ expect(reply).to eq [30.0, 30.0, 40.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 50.0]
96
+ end
97
+
98
+ end
@@ -0,0 +1,25 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'rspec'
5
+
6
+ require 'rspec/core/formatters/progress_formatter'
7
+ # doesn't say so much about pending guys
8
+ class QuietPendingFormatter < RSpec::Core::Formatters::ProgressFormatter
9
+ def example_pending(example)
10
+ output.print pending_color('*')
11
+ end
12
+ end
13
+
14
+ require 'rspec/core/formatters/documentation_formatter'
15
+ class QuietPendingDocFormatter < RSpec::Core::Formatters::DocumentationFormatter
16
+ def example_pending(example)
17
+ output.puts pending_color( "<pending>: #{example.execution_result[:pending_message]}" )
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |config|
22
+ config.treat_symbols_as_metadata_keys_with_true_values = true
23
+ config.formatter = QuietPendingDocFormatter
24
+ config.color = true
25
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: binneroc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John T. Prince
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.2
83
+ description: bins x, y data into discrete bins using constant time binning. Useful.
84
+ email:
85
+ - jtprince@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - binneroc.gemspec
96
+ - lib/binneroc.rb
97
+ - lib/binneroc/version.rb
98
+ - spec/binneroc_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: ''
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.2
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: bins x, y data into discrete bins using constant time binning
124
+ test_files:
125
+ - spec/binneroc_spec.rb
126
+ - spec/spec_helper.rb