lttb 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c647e0772742ee162459334ede93428d12a7c79c
4
+ data.tar.gz: 525ce8a01dab924bd16d94e5cad4981e9173194f
5
+ SHA512:
6
+ metadata.gz: fd1e18e0a2e234c49af4d9a865aae858f262e5249b82464012e4f2d2bc3d661f0e1338261a03a06bcddb49f4eb1674da2bc2bc96986e8eb4b0b4ee86609cf055
7
+ data.tar.gz: 62da5c7ea2c2990aa83ea5008f11c71494ba74cc1c2792a486e84d241535499057147a382f3e9e9a6e7b488a17b9d5917ffd5d9d236383ed130e3829ce109822
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lttb.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jubke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # lttb - Largest-Triangle-Three-Buckets (Ruby)
2
+ This is an implementation of the Largest-Triangle-Three-Buckets (LTTB) downsampling algorithm in Ruby.
3
+
4
+ The code has been translated from the work of Sveinn Steinarsson in his plugin for Flot charts.
5
+ More information is available on [his page](https://github.com/sveinn-steinarsson/flot-downsample/),
6
+ and you can find the thesis describing the algorithm [here](http://skemman.is/handle/1946/15343).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'lttb'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lttb
23
+
24
+ ## Usage
25
+
26
+ Data passed should be in the format [[x1,y1],[x2,y2]].
27
+ ```ruby
28
+ data = [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]
29
+ threshold = 3
30
+ Lttb.process(data, threshold)
31
+ # => [1, 1], [3, 9], [5, 25]]
32
+ ```
33
+
34
+ Pass `:dates => true` to process DateTime objects correctly.
35
+ ```ruby
36
+ Lttb.process(data, threshold, dates: true)
37
+ ```
38
+
39
+ ## Known limitations
40
+
41
+ Does not support gaps (null values) in the data array.
42
+ X-values must be in a strictly increasing order.
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Jubke/lttb.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
52
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lttb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require "lttb/version"
2
+ require "lttb/process"
@@ -0,0 +1,138 @@
1
+ module Lttb
2
+
3
+ # Return a downsampled version of data.
4
+ # Parameters
5
+ # ----------
6
+ # data: list of lists/tuples
7
+ # data must be formated this way: [[x,y], [x,y], [x,y], ...]
8
+ # or: [(x,y), (x,y), (x,y), ...]
9
+ # threshold: int
10
+ # threshold must be >= 2 and <= to the len of data
11
+ # Returns
12
+ # -------
13
+ # data, but downsampled using threshold
14
+ def self.largest_triangle_three_buckets(data, threshold, options = {})
15
+
16
+ # Check if data and threshold are valid
17
+ check_data(data)
18
+ check_threshold(threshold)
19
+ check_tuples(data) if options[:check_tuples]
20
+
21
+ data = handle_dates(data) if options[:dates]
22
+
23
+ # cache data size
24
+ data_length = data.size
25
+
26
+ # Nothing to do?
27
+ return data if threshold >= data_length || threshold == 0
28
+
29
+ # Bucket size. Leave room for start and end data points
30
+ every = (data_length - 2) / (threshold - 2)
31
+
32
+ a = 0 # Initially a is the first point in the triangle
33
+ sampled = [data[a]] # Always add the first point
34
+
35
+ (0..(threshold - 3)).each do |i|
36
+ # Calculate point average for next bucket (containing c)
37
+ avg_x = 0
38
+ avg_y = 0
39
+ avg_range_start = (((i + 1) * every).floor + 1).to_i
40
+ avg_range_end = (((i + 2) * every).floor + 1).to_i
41
+ avg_range_end = avg_range_end < data_length ? avg_range_end : data_length
42
+
43
+ avg_range_length = avg_range_end - avg_range_start
44
+
45
+ while avg_range_start < avg_range_end
46
+ avg_x += data[avg_range_start][0]
47
+ avg_y += data[avg_range_start][1]
48
+
49
+ avg_range_start += 1 # increment
50
+ end
51
+
52
+ avg_x /= avg_range_length
53
+ avg_y /= avg_range_length
54
+
55
+ # Get the range for this bucket
56
+ range_offs = (((i + 0) * every).floor + 1).to_i
57
+ range_to = (((i + 1) * every).floor + 1).to_i
58
+ range_to = range_to < data_length - 1 ? range_to : data_length - 1
59
+
60
+ # Point a
61
+ point_ax = data[a][0]
62
+ point_ay = data[a][1]
63
+
64
+ max_area = area = -1
65
+
66
+ while range_offs < range_to
67
+ # Calculate triangle area over three buckets
68
+ area = (
69
+ (point_ax - avg_x) * (data[range_offs][1] - point_ay) -
70
+ (point_ax - data[range_offs][0]) * (avg_y - point_ay)
71
+ ).abs * 0.5
72
+
73
+ if area > max_area
74
+ max_area = area
75
+ max_area_point = data[range_offs]
76
+ next_a = range_offs # Next a is this b
77
+ end
78
+
79
+ range_offs += 1 # increment
80
+ end
81
+
82
+ sampled.push(max_area_point) # Pick this point from the bucket
83
+ a = next_a # This a is the next a (chosen b)
84
+ end
85
+
86
+ sampled.push(data[data.size - 1]) # Always add last
87
+
88
+ sampled = as_dates(sampled) if options[:dates]
89
+ sampled
90
+ end
91
+
92
+ class << self
93
+
94
+ alias process largest_triangle_three_buckets
95
+
96
+ private
97
+
98
+ def check_data(data)
99
+ raise LttbException, 'data is not an array' unless data.is_a? Array
100
+ end
101
+
102
+ def check_threshold(threshold)
103
+ return if threshold.is_a?(Integer) && threshold > 2
104
+ raise LttbException, "threshold not well defined: #{threshold}"
105
+ end
106
+
107
+ def check_tuples(data)
108
+ data.each do |i|
109
+ next if i.is_a?(Array) && i.size == 2
110
+ raise LttbException, 'datapoints are not lists or tuples'
111
+ end
112
+ end
113
+
114
+ def handle_dates(data)
115
+ data.map do |d|
116
+ d[0] = d[0].strftime('%Q').to_i
117
+ d
118
+ end
119
+ end
120
+
121
+ def as_dates(data)
122
+ data.map do |d|
123
+ d[0] = DateTime.strptime(d[0].to_s, '%Q')
124
+ d
125
+ end
126
+ end
127
+
128
+ end
129
+
130
+ class LttbException < StandardError
131
+
132
+ def initialize(msg)
133
+ super msg
134
+ end
135
+
136
+ end
137
+
138
+ end
@@ -0,0 +1,3 @@
1
+ module Lttb
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lttb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lttb"
8
+ spec.version = Lttb::VERSION
9
+ spec.authors = ["Jubke"]
10
+ spec.email = ["luebke.julian@gmail.com"]
11
+
12
+ spec.summary = %q{Largest-Triangle-Three-Buckets (LTTB) downsampling algorithm in Ruby.}
13
+ spec.description = %q{The code has been translated from the work of Sveinn Steinarsson in his plugin for Flot charts.
14
+ More information is available on [his page](https://github.com/sveinn-steinarsson/flot-downsample/),
15
+ and you can find the thesis describing the algorithm [here](http://skemman.is/handle/1946/15343).}
16
+ spec.homepage = "https://github.com/Jubke/lttb"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lttb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jubke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-29 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ The code has been translated from the work of Sveinn Steinarsson in his plugin for Flot charts.
57
+ More information is available on [his page](https://github.com/sveinn-steinarsson/flot-downsample/),
58
+ and you can find the thesis describing the algorithm [here](http://skemman.is/handle/1946/15343).
59
+ email:
60
+ - luebke.julian@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/lttb.rb
75
+ - lib/lttb/process.rb
76
+ - lib/lttb/version.rb
77
+ - lttb.gemspec
78
+ homepage: https://github.com/Jubke/lttb
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Largest-Triangle-Three-Buckets (LTTB) downsampling algorithm in Ruby.
102
+ test_files: []