downsampler 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69b6e23fcf7ca6d7cfd13deef886f10f0659b6e2
4
+ data.tar.gz: 292a7affaa256255e3cdf01b64cd9db90e4b84bb
5
+ SHA512:
6
+ metadata.gz: 4aaff0f40310b3051f959d6bc405d4456f6b63106ef825ca1daa457bb7c4a23b23918531938cefbb665e24f910cf8becafd83f3551c4b05c68fc710338e75c41
7
+ data.tar.gz: 7f7fcb4619c21b50db7b1bc830b6f95302bfc9f3fcf22480b683c60ae126e58a10c796782a701a1d4e640fee8f41d4e4537c360426d7aad156800ad0f3c7056b
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .ruby-version
16
+ /bin
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in downsampler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Owen Mockler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Downsampler
2
+
3
+ This gem is for downsampling and "bucketing" time based data. Great for passing data to [chartkick](https://github.com/ankane/chartkick)
4
+ ```ruby
5
+ <%= line_chart User.downsample_by(60, :created_at).counts %> # 1 minute buckets
6
+ ```
7
+ Implemented without any dependencies so you don't need activesupport or activerecord.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'downsampler'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install downsampler
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ DataPoint = Struct.new(:time, :value)
30
+ data_points = (1..9).map { |i| DataPoint.new(Time.now + i*20, i)}
31
+
32
+ downsampled = data_points.downsample_by(1.minute, &:time)
33
+ #{
34
+ # 2014-08-18 15:01:00 -0400=>
35
+ # [#<struct DataPoint time=2014-08-18 15:03:40 -0400, value=1>,
36
+ # #<struct DataPoint time=2014-08-18 15:03:20 -0400, value=2>,
37
+ # #<struct DataPoint time=2014-08-18 15:03:00 -0400, value=3>],
38
+ # 2014-08-18 15:02:00 -0400=>
39
+ # [#<struct DataPoint time=2014-08-18 15:02:40 -0400, value=4>,
40
+ # #<struct DataPoint time=2014-08-18 15:02:20 -0400, value=5>,
41
+ # #<struct DataPoint time=2014-08-18 15:02:00 -0400, value=6>],
42
+ # 2014-08-18 15:03:00 -0400=>
43
+ # [#<struct DataPoint time=2014-08-18 15:01:40 -0400, value=7>,
44
+ # #<struct DataPoint time=2014-08-18 15:01:20 -0400, value=8>,
45
+ # #<struct DataPoint time=2014-08-18 15:01:00 -0400, value=9>]
46
+ #}
47
+
48
+ downsampled.sum &:value
49
+ #{
50
+ # 2014-08-18 15:01:00 -0400=> 6,
51
+ # 2014-08-18 15:02:00 -0400=> 15,
52
+ # 2014-08-18 15:03:00 -0400=> 24
53
+ #}
54
+
55
+ downsampled.mean { |point| point.value }
56
+ #{
57
+ # 2014-08-18 15:01:00 -0400=> 2,
58
+ # 2014-08-18 15:02:00 -0400=> 5,
59
+ # 2014-08-18 15:03:00 -0400=> 8
60
+ #}
61
+
62
+ downsampled.counts
63
+ #{
64
+ # 2014-08-18 15:01:00 -0400=> 3,
65
+ # 2014-08-18 15:02:00 -0400=> 3,
66
+ # 2014-08-18 15:03:00 -0400=> 3
67
+ #}
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/omockler/downsampler/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'nested']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'downsampler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "downsampler"
8
+ spec.version = Downsampler::VERSION
9
+ spec.authors = ["Owen Mockler"]
10
+ spec.email = ["omockler@gmail.com"]
11
+ spec.summary = %q{Downsample and group temporal data sets in ruby.}
12
+ spec.description = %q{Downsample and group temporal data sets in ruby. Great for preparing time data for display in a chart.}
13
+ spec.homepage = "https://github.com/omockler/downsampler"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry", "~> 0.10"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "rspec-nc", "~> 0.1"
26
+ end
@@ -0,0 +1,9 @@
1
+ require "downsampler/version"
2
+ require "downsampler/time_extensions"
3
+ require "downsampler/enumerable_extensions"
4
+ require "downsampler/downsampled_hash"
5
+ require "downsampler/base"
6
+
7
+ module Downsampler
8
+
9
+ end
@@ -0,0 +1,11 @@
1
+ module Enumerable
2
+ def downsample_by secs, &block
3
+ downsampled_hash = group_by do |value|
4
+ time = yield value
5
+ time = Time.parse(time) unless time.is_a?(Time)
6
+ time = time.dup.extend(Downsampler::TimeExt)
7
+ time.floor(secs)
8
+ end
9
+ Downsampler::DownsampledHash.new(downsampled_hash)
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ module Downsampler
2
+ class DownsampledHash < DelegateClass(Hash)
3
+
4
+ def mean &block
5
+ inject({}) do |initial, (key, value)|
6
+ result = value.dup.extend(EnumerableExt).mean(&block) if value.is_a?(Array)
7
+ initial[key] = result
8
+ initial
9
+ end
10
+ end
11
+
12
+ def sum &block
13
+ inject({}) do |initial, (key, value)|
14
+ result = value.dup.extend(EnumerableExt).sum(&block) if value.is_a?(Array)
15
+ initial[key] = result
16
+ initial
17
+ end
18
+ end
19
+
20
+ def counts
21
+ inject({}) do |initial, (key, value)|
22
+ initial[key] = values.count
23
+ initial
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ module Downsampler
2
+ module EnumerableExt
3
+ def sum &block
4
+ inject(0.0) do |result, el|
5
+ el = yield(el) if block_given?
6
+ result + el.to_f
7
+ end
8
+ end
9
+
10
+ def mean &block
11
+ sum(&block) / size
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Downsampler
2
+ module TimeExt
3
+ def floor(seconds = 60)
4
+ Time.at((self.to_f / seconds).floor * seconds)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Downsampler
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Downsampler do
4
+ let(:data) { generate_enumerable }
5
+ let(:downsampled_data) { data.downsample_by(60, &:time) }
6
+
7
+ it "should add downsample_by method" do
8
+ expect([]).to respond_to :downsample_by
9
+ end
10
+
11
+ describe ".downsample_by" do
12
+
13
+ it "should return a downsampled hash" do
14
+ expect(downsampled_data).to be_a(Downsampler::DownsampledHash)
15
+ end
16
+
17
+ it "should generate the correct group keys" do
18
+ sorted_keys = downsampled_data.keys.sort
19
+ first_time = sorted_keys.first
20
+ generated_keys = sorted_keys.size.times.map do |i|
21
+ first_time + i*60
22
+ end
23
+ expect(generated_keys == sorted_keys).to be true
24
+ end
25
+
26
+ it "should create 3 groups for the 9 datapoints" do
27
+ expect(downsampled_data.count).to eq(3)
28
+ end
29
+
30
+ it "should disribute the groups correctly" do
31
+ downsampled_data.each_pair do |key, values|
32
+ expect(
33
+ (values.first.time + 60) > values.last.time
34
+ ).to be true
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ describe ".sum" do
41
+ let(:first_key) { downsampled_data.keys.first }
42
+ let(:summed) { downsampled_data.sum(&:value) }
43
+
44
+ it "should correctly sum each group" do
45
+ #first group should have the values [1,2,3]
46
+ expect(summed[first_key]).to eq 6
47
+ end
48
+
49
+ it "should leak sum method to original array" do
50
+ summed
51
+ expect(downsampled_data[first_key]).not_to respond_to :sum
52
+ end
53
+ end
54
+
55
+ describe ".mean" do
56
+ let(:first_key) { downsampled_data.keys.first }
57
+ let(:meaned) { downsampled_data.mean(&:value) }
58
+
59
+ it "should correctly average each group" do
60
+ expect(meaned[first_key]).to eq 2
61
+ end
62
+
63
+ it "should not leak mean method to original array" do
64
+ meaned
65
+ expect(downsampled_data[first_key]).not_to respond_to :mean
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,12 @@
1
+ require 'pry'
2
+ require 'downsampler'
3
+
4
+ DataPoint = Struct.new :name, :time, :value
5
+
6
+ def generate_enumerable
7
+ time = Time.at((Time.now.to_f / 60).floor * 60) # Emulate Time.floor
8
+
9
+ (1..9).map do |i|
10
+ DataPoint.new("Point #{i}", (time - i*20), i)
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: downsampler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Owen Mockler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-19 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-nc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ description: Downsample and group temporal data sets in ruby. Great for preparing
84
+ time data for display in a chart.
85
+ email:
86
+ - omockler@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - downsampler.gemspec
97
+ - lib/downsampler.rb
98
+ - lib/downsampler/base.rb
99
+ - lib/downsampler/downsampled_hash.rb
100
+ - lib/downsampler/enumerable_extensions.rb
101
+ - lib/downsampler/time_extensions.rb
102
+ - lib/downsampler/version.rb
103
+ - spec/downsampler_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/omockler/downsampler
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Downsample and group temporal data sets in ruby.
129
+ test_files:
130
+ - spec/downsampler_spec.rb
131
+ - spec/spec_helper.rb
132
+ has_rdoc: