dtw 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +7 -0
- data/.rubocop_hound.yml +1063 -0
- data/.rubocop_todo.yml +36 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/dtw.gemspec +23 -0
- data/lib/dtw.rb +5 -0
- data/lib/dtw/naive.rb +70 -0
- data/lib/dtw/version.rb +3 -0
- metadata +100 -0
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-05-03 18:22:33 -0700 using RuboCop version 0.30.0.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 1
|
9
|
+
# Cop supports --auto-correct.
|
10
|
+
Lint/UnusedBlockArgument:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
# Offense count: 68
|
14
|
+
# Cop supports --auto-correct.
|
15
|
+
Lint/UnusedMethodArgument:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
# Offense count: 4
|
19
|
+
Metrics/AbcSize:
|
20
|
+
Max: 28
|
21
|
+
|
22
|
+
# Offense count: 47
|
23
|
+
# Configuration parameters: AllowURI, URISchemes.
|
24
|
+
Metrics/LineLength:
|
25
|
+
Max: 171
|
26
|
+
|
27
|
+
# Offense count: 7
|
28
|
+
# Cop supports --auto-correct.
|
29
|
+
Style/ExtraSpacing:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
# Offense count: 42
|
33
|
+
# Cop supports --auto-correct.
|
34
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
35
|
+
Style/StringLiterals:
|
36
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Dtw
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dtw`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'dtw'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install dtw
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/dtw/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dtw"
|
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
|
data/bin/setup
ADDED
data/dtw.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dtw/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dtw"
|
8
|
+
spec.version = Dtw::VERSION
|
9
|
+
spec.authors = ["Chris Beer"]
|
10
|
+
spec.email = ["chris@cbeer.info"]
|
11
|
+
|
12
|
+
spec.summary = %q{Dynamic time warping for ruby}
|
13
|
+
spec.homepage = "https://github.com/cbeer/dtw"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
end
|
data/lib/dtw.rb
ADDED
data/lib/dtw/naive.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Dtw
|
2
|
+
class Naive
|
3
|
+
attr_reader :series, :options
|
4
|
+
def initialize(*series)
|
5
|
+
@options = series.pop if series.last.is_a? Hash
|
6
|
+
@series = series
|
7
|
+
end
|
8
|
+
|
9
|
+
def path
|
10
|
+
@path ||= begin
|
11
|
+
path = []
|
12
|
+
|
13
|
+
i = series.first.length - 1
|
14
|
+
j = series.last.length - 1
|
15
|
+
|
16
|
+
while i > 0 || j > 0
|
17
|
+
path.unshift [i, j]
|
18
|
+
i, j = matrix[:path][[i, j]]
|
19
|
+
end
|
20
|
+
|
21
|
+
path.unshift [0, 0]
|
22
|
+
|
23
|
+
path
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def matrix
|
30
|
+
@matrix ||= begin
|
31
|
+
g = { path: {} }
|
32
|
+
warping_matrix(g, series.first.length - 1, series.last.length - 1)
|
33
|
+
g
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def warping_matrix(g, i, j)
|
38
|
+
return g[[i, j]] if g[[i, j]]
|
39
|
+
d = distance(i, j)
|
40
|
+
|
41
|
+
if i == 0 && j == 0
|
42
|
+
g[:path][[i, j]] = [nil, nil]
|
43
|
+
g[[i, j]] = d
|
44
|
+
return d
|
45
|
+
end
|
46
|
+
|
47
|
+
deletion = warping_matrix(g, i, j - 1) if j > 0
|
48
|
+
match = warping_matrix(g, i - 1, j - 1) if i > 0 && j > 0
|
49
|
+
insertion = warping_matrix(g, i - 1, j) if i > 0
|
50
|
+
|
51
|
+
m = [deletion, match, insertion].compact.min
|
52
|
+
|
53
|
+
g[:path][[i, j]] = if m == match
|
54
|
+
[i - 1, j - 1]
|
55
|
+
elsif m == deletion
|
56
|
+
[i, j - 1]
|
57
|
+
elsif m == insertion
|
58
|
+
[i - 1, j]
|
59
|
+
end
|
60
|
+
|
61
|
+
g[[i, j]] = m + d
|
62
|
+
end
|
63
|
+
|
64
|
+
def distance(i, j)
|
65
|
+
a, b = series.first[i], series.last[j]
|
66
|
+
z = (a - b)**2
|
67
|
+
z
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/dtw/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dtw
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Beer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-13 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: '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
|
+
email:
|
57
|
+
- chris@cbeer.info
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".rubocop_hound.yml"
|
66
|
+
- ".rubocop_todo.yml"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- dtw.gemspec
|
74
|
+
- lib/dtw.rb
|
75
|
+
- lib/dtw/naive.rb
|
76
|
+
- lib/dtw/version.rb
|
77
|
+
homepage: https://github.com/cbeer/dtw
|
78
|
+
licenses: []
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Dynamic time warping for ruby
|
100
|
+
test_files: []
|