asdrubal 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/asdrubal.gemspec +21 -0
- data/lib/asdrubal.rb +2 -0
- data/lib/asdrubal/time_series.rb +130 -0
- data/lib/asdrubal/version.rb +3 -0
- data/spec/lib/asdrubal/time_series_spec.rb +65 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 $GIT_AUTHOR_NAME
|
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,35 @@
|
|
1
|
+
# Asdrúbal
|
2
|
+
|
3
|
+
A simple helper for time series manipulation and analysis.
|
4
|
+
|
5
|
+
Asdrúbal allows you to create a time series with values and perform simple analysis on it.
|
6
|
+
|
7
|
+
Currently, the only analysis it does is "burst of values". Using a given "time span" (let's say 2 days) it consider the that any date with value > 0 at maximum 2 dates of distance behind and ahead is part of the same "burst".
|
8
|
+
|
9
|
+
The current purpose of this analysis is to find the period an applicant is looking for a job :)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'asdrubal'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install asdrubal
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Okay, I'll put something here as soon I have time. You you really want to use it as is, chech "spec" directory ;)
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/asdrubal.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/asdrubal/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Ronie Uliana']
|
6
|
+
gem.email = ['ronie.uliana@gmail.com']
|
7
|
+
gem.description = %q{Simple helper for time series analysis}
|
8
|
+
gem.summary = %q{Asdrúbal is a simple helper that takes dates and values and allows you to ask more information about a point in time.}
|
9
|
+
gem.homepage = ''
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = 'asdrubal'
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = Asdrubal::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rb-inotify', '~> 0.8'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.11'
|
20
|
+
gem.add_development_dependency 'guard-rspec', '~> 1.2'
|
21
|
+
end
|
data/lib/asdrubal.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
class TimeSeries
|
2
|
+
attr_accessor :smooth_window
|
3
|
+
|
4
|
+
def initialize(from_date, to_date = from_date, step_days = 1)
|
5
|
+
@span = (from_date..to_date)
|
6
|
+
@step = step_days
|
7
|
+
@dates = Hash.new(0)
|
8
|
+
self.smooth_window = 2
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.from(year, month, day)
|
12
|
+
TimeSeries.new to_date(year, month, day)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to(year, month, day)
|
16
|
+
TimeSeries.new @span.first, self.class.to_date(year, month, day)
|
17
|
+
end
|
18
|
+
|
19
|
+
def step(days)
|
20
|
+
@step = days
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def each
|
25
|
+
@span.step(@step).each do |date|
|
26
|
+
yield date, self[date]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def include?(date)
|
31
|
+
@span.include?(date)
|
32
|
+
end
|
33
|
+
|
34
|
+
def at(date)
|
35
|
+
refresh_cache
|
36
|
+
@cache[date]
|
37
|
+
end
|
38
|
+
|
39
|
+
def [](date)
|
40
|
+
raise Exception.new('Out of range') unless include?(date)
|
41
|
+
refresh_cache
|
42
|
+
at(date).value
|
43
|
+
end
|
44
|
+
|
45
|
+
def []=(date, value)
|
46
|
+
raise Exception.new('Out of range') unless include?(date)
|
47
|
+
reset_cache
|
48
|
+
@dates[date] = value
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def self.to_date(year, month, day)
|
53
|
+
Date.new year, month, day
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_value(date, previous = nil)
|
57
|
+
Value.new(self, date, @dates[date], previous)
|
58
|
+
end
|
59
|
+
|
60
|
+
def reset_cache
|
61
|
+
@cache = nil
|
62
|
+
end
|
63
|
+
|
64
|
+
def refresh_cache
|
65
|
+
return if @cache
|
66
|
+
|
67
|
+
@cache = Hash.new
|
68
|
+
|
69
|
+
previous = to_value(@span.first)
|
70
|
+
@cache[@span.first] = previous
|
71
|
+
|
72
|
+
@span.step(@step).drop(1).each do |date|
|
73
|
+
current = to_value(date, previous)
|
74
|
+
@cache[date] = current
|
75
|
+
|
76
|
+
previous = current
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class Value
|
82
|
+
attr_reader :value
|
83
|
+
attr_accessor :previous, :next
|
84
|
+
|
85
|
+
def initialize(parent, date, value, previous = nil)
|
86
|
+
@parent, @date, @value = parent, date, value || 0
|
87
|
+
@previous = previous
|
88
|
+
previous.next = self if previous
|
89
|
+
end
|
90
|
+
|
91
|
+
def signal
|
92
|
+
self.value > 0 ? 1 : 0
|
93
|
+
end
|
94
|
+
|
95
|
+
def smooth_signal
|
96
|
+
return 1 if self.signal == 1
|
97
|
+
|
98
|
+
if any_signal_behind? && any_signal_ahead?
|
99
|
+
1
|
100
|
+
else
|
101
|
+
0
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Warning: recursion ahead
|
106
|
+
def any_signal_behind?
|
107
|
+
|
108
|
+
def any_before(value, steps)
|
109
|
+
return false if steps <= 0 || value.nil?
|
110
|
+
value.signal == 1 || any_before(value.previous, steps - 1)
|
111
|
+
end
|
112
|
+
|
113
|
+
any_before(self.previous, @parent.smooth_window)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Warning: recursion ahead
|
117
|
+
def any_signal_ahead?
|
118
|
+
|
119
|
+
def any_after(value, steps)
|
120
|
+
return false if steps <= 0 || value.nil?
|
121
|
+
value.signal == 1 || any_after(value.next, steps - 1)
|
122
|
+
end
|
123
|
+
|
124
|
+
any_after(self.next, @parent.smooth_window)
|
125
|
+
end
|
126
|
+
|
127
|
+
def to_s
|
128
|
+
"#{@date.strftime('%Y-%m-%d')} = #{@value}"
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative '../../../lib/asdrubal.rb'
|
2
|
+
describe TimeSeries do
|
3
|
+
describe 'basic features' do
|
4
|
+
let(:time_series) { TimeSeries.from(2000, 12, 31).to(2001, 1, 1) }
|
5
|
+
it 'is initially filled with zeros' do
|
6
|
+
expect(time_series[Date.new(2000, 12, 31)]).to be_equal(0)
|
7
|
+
expect(time_series.at(Date.new(2000, 12, 31)).value).to be_equal(0)
|
8
|
+
|
9
|
+
expect(time_series[Date.new(2001, 1, 1)]).to be_equal(0)
|
10
|
+
expect(time_series.at(Date.new(2001, 1, 1)).value).to be_equal(0)
|
11
|
+
end
|
12
|
+
it 'checks if date is in the time series' do
|
13
|
+
expect { time_series[Date.new(2001, 1, 2)] }.to raise_exception
|
14
|
+
expect { time_series.at(Date.new(2001, 1, 2).value) }.to raise_exception
|
15
|
+
end
|
16
|
+
it 'allow put values in any date' do
|
17
|
+
time_series[Date.new(2000, 12, 31)] = 99
|
18
|
+
expect(time_series[Date.new(2000, 12, 31)]).to equal(99)
|
19
|
+
expect(time_series.at(Date.new(2000, 12, 31)).value).to equal(99)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
describe 'calculations' do
|
23
|
+
let(:time_series) do
|
24
|
+
time_series = TimeSeries.from(2001, 1, 1).to(2001, 1, 8)
|
25
|
+
time_series[Date.new(2001, 1, 4)] = 9999
|
26
|
+
time_series[Date.new(2001, 1, 7)] = 1
|
27
|
+
time_series
|
28
|
+
end
|
29
|
+
it 'mark any non zero value as "signal = 1"' do
|
30
|
+
expect(time_series.at(Date.new(2001, 1, 3)).signal).to be_equal(0)
|
31
|
+
expect(time_series.at(Date.new(2001, 1, 4)).signal).to be_equal(1)
|
32
|
+
expect(time_series.at(Date.new(2001, 1, 5)).signal).to be_equal(0)
|
33
|
+
expect(time_series.at(Date.new(2001, 1, 6)).signal).to be_equal(0)
|
34
|
+
expect(time_series.at(Date.new(2001, 1, 7)).signal).to be_equal(1)
|
35
|
+
expect(time_series.at(Date.new(2001, 1, 8)).signal).to be_equal(0)
|
36
|
+
end
|
37
|
+
it 'try to detect "bursts" of values' do
|
38
|
+
time_series.smooth_window = 2
|
39
|
+
expect(time_series.at(Date.new(2001, 1, 1)).smooth_signal).to be_equal(0)
|
40
|
+
expect(time_series.at(Date.new(2001, 1, 2)).smooth_signal).to be_equal(0)
|
41
|
+
expect(time_series.at(Date.new(2001, 1, 3)).smooth_signal).to be_equal(0)
|
42
|
+
expect(time_series.at(Date.new(2001, 1, 4)).smooth_signal).to be_equal(1)
|
43
|
+
expect(time_series.at(Date.new(2001, 1, 5)).smooth_signal).to be_equal(1)
|
44
|
+
expect(time_series.at(Date.new(2001, 1, 6)).smooth_signal).to be_equal(1)
|
45
|
+
expect(time_series.at(Date.new(2001, 1, 7)).smooth_signal).to be_equal(1)
|
46
|
+
expect(time_series.at(Date.new(2001, 1, 8)).smooth_signal).to be_equal(0)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
describe 'weekly time series' do
|
50
|
+
let(:time_series) do
|
51
|
+
time_series = TimeSeries.from(2012, 3, 11).to(2012, 4, 8).step(7)
|
52
|
+
time_series[Date.new(2012, 3, 11)] = 3
|
53
|
+
time_series[Date.new(2012, 4, 8)] = 127
|
54
|
+
time_series
|
55
|
+
end
|
56
|
+
it 'has no difference from "by day" use' do
|
57
|
+
time_series.smooth_window = 3
|
58
|
+
expect(time_series.at(Date.new(2012, 3, 11)).smooth_signal).to be_equal(1)
|
59
|
+
expect(time_series.at(Date.new(2012, 3, 18)).smooth_signal).to be_equal(1)
|
60
|
+
expect(time_series.at(Date.new(2012, 3, 25)).smooth_signal).to be_equal(1)
|
61
|
+
expect(time_series.at(Date.new(2012, 4, 1)).smooth_signal).to be_equal(1)
|
62
|
+
expect(time_series.at(Date.new(2012, 4, 8)).smooth_signal).to be_equal(1)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asdrubal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ronie Uliana
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rb-inotify
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.8'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.8'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.11'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.11'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard-rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.2'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.2'
|
62
|
+
description: Simple helper for time series analysis
|
63
|
+
email:
|
64
|
+
- ronie.uliana@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Guardfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- asdrubal.gemspec
|
76
|
+
- lib/asdrubal.rb
|
77
|
+
- lib/asdrubal/time_series.rb
|
78
|
+
- lib/asdrubal/version.rb
|
79
|
+
- spec/lib/asdrubal/time_series_spec.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.21
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Asdrúbal is a simple helper that takes dates and values and allows you to
|
104
|
+
ask more information about a point in time.
|
105
|
+
test_files:
|
106
|
+
- spec/lib/asdrubal/time_series_spec.rb
|