simple_time_series 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +63 -16
- data/lib/simple_time_series/simple_time_series.rb +17 -9
- data/lib/simple_time_series/version.rb +1 -1
- data/spec/lib/simple_time_series_spec.rb +37 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9ad59d5ec507c02b79dfac4e610a9e53713b251
|
4
|
+
data.tar.gz: e8a81306099468b983d7218253109766b274e00d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1f60e0b32bae7007e5ff17a186abfd5ac8c16822000049a44a34f1ee3a9b597c85e98a29e1327179c5b6ffedc51d333a5640aa9c47a2fe4e720024b90d30471
|
7
|
+
data.tar.gz: 9af94d475ed26267745dab1150b2fcf63f0c0d9495d85e5f0acdfdb53900132259cb6d0be1687876b9f210dfc2221a49e85898e746d30bfb5cec13ca7f236c4c
|
data/README.md
CHANGED
@@ -20,38 +20,81 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
Example:
|
22
22
|
|
23
|
-
Imagine you have recorded over the past week the number of pizzas consumed, the number of miles run, and the number of tasks done
|
24
|
-
|
25
|
-
You have these stored in simple Ruby arrays (that you can name anything you want):
|
23
|
+
Imagine you have recorded over the past week the number of pizzas you have consumed, the number of miles you've run, and the number of tasks you've done and you have stored these values in simple Ruby arrays (that you can name anything you want):
|
26
24
|
|
27
25
|
pizzas = [0, 0, 1, 0, 0.5, 0, 2]
|
28
26
|
miles = [2.2, 3.1, 0.0, 4.3, 1.2, 12.2, 2.3]
|
29
27
|
tasks_done = [2, 3, 0, 14, 3, 11, 0]
|
30
28
|
|
31
|
-
To associate these observations with the days they belong to, you create arrays
|
29
|
+
To associate these observations with the days they belong to, you create one or more ordered arrays holding sequential observation numbers, days-of-the-week, dates or anything else you want to use to label the ordered events/dates for which you're recording data. (You can put whatever time/date values in these arrays that you wish. Only their order in the array(s) matters.) Below, we create "dows" (for days-of-the-week), "dates" and "full_dates," but you can make whatever ordered observation/date variables you desire. And you need only one time variable:
|
32
30
|
|
33
|
-
dows = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
|
34
|
-
|
35
|
-
|
36
|
-
'2014-01-04', '2014-01-05', '2014-01-06', '2014-01-07']
|
31
|
+
dows = ['Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday', 'Saturday']
|
32
|
+
dates = ['2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05', '2014-01-06', '2014-01-07']
|
33
|
+
full_dates = ['Jan 1, 2014', 'Jan 2, 2014', 'Jan 3, 2014', 'Jan 4, 2014', 'Jan 5, 2014', 'Jan 6, 2014', 'Jan 7, 2014']
|
37
34
|
|
38
35
|
This is sufficient to package up your data into a SimpleTimeSeries object:
|
39
36
|
|
40
37
|
require 'simple_time_series'
|
41
38
|
my_data = SimpleTimeSeries.new(:time_vars => {'dows' => dows,
|
42
|
-
'dates' => dates
|
39
|
+
'dates' => dates,
|
40
|
+
'full_dates' => full_dates},
|
43
41
|
:data_vars => {'pizzas' => pizzas,
|
44
42
|
'miles' => miles,
|
45
43
|
'tasks_done' => tasks_done})
|
46
44
|
|
47
|
-
You can now easily access the value of any data variable for any value of one of your time variables:
|
45
|
+
You can now easily access the value of any data variable for any value of one of your time variables via xxx_on methods created for each of your data_vars (here called 'pizzas_on,' 'miles_on' and 'tasks_done_on'):
|
46
|
+
|
47
|
+
puts "Pizzas eaten on Tuesday: #{my_data.pizzas_on('Tuesday')}" # prints 1
|
48
|
+
puts "Pizzas eaten on 2014-01-03: #{my_data.pizzas_on('2014-01-03')}" # prints 1
|
49
|
+
puts "Pizzas eaten on 'Jan 3, 2014': #{my_data.pizzas_on('Jan 3, 2014')}" # prints 1
|
50
|
+
|
51
|
+
puts "Miles run on Friday: #{my_data.miles_run_on('Friday')}" # prints 12.2
|
52
|
+
puts "Miles run on 2014-01-06: #{my_data.miles_run_on('2014-01-06')}" # prints 12.2
|
53
|
+
puts "Miles run on 'Jan 6, 2014': #{my_data.miles_run_on('Jan 6, 2014')}" # prints 12.2
|
54
|
+
|
55
|
+
puts "Tasks done on Saturday: #{my_data.tasks_done_on('Saturday')}" # prints 0
|
56
|
+
puts "Tasks done on 2014-01-07: #{my_data.tasks_done_on('2014-01-07')}" # prints 0
|
57
|
+
puts "Tasks done on 'Jan 7, 2014': #{my_data.tasks_done_on('Jan 7, 2014')}" # prints 0
|
58
|
+
|
59
|
+
You can get the same values by calling SimpleTimeSeries#find with two arguments, first the data_var name and then the time_var value:
|
60
|
+
|
61
|
+
puts "Pizzas eaten on 'Jan 2, 2014': #{my_data.find('pizzas', 'Jan 2, 2014')}" # prints 0
|
62
|
+
puts "Miles on Friday: #{my_data.find('miles', 'Friday')}" # prints 12.2
|
63
|
+
puts "Miles on 2014-01-05: #{my_data.find('miles','2014-01-05')}" # prints 1.2
|
64
|
+
puts "Tasks done on Wednesday: #{my_data.find('tasks_done', 'Wednesday')}" # prints 14
|
65
|
+
puts "Tasks done on 2014-01-05: #{my_data.find('tasks_done', '2014-01-05')}" # prints 3
|
66
|
+
|
67
|
+
You can view all the values associated with any variable:
|
68
|
+
|
69
|
+
my_data.miles # prints [2.2, 3.1, 0.0, 4.3, 1.2, 12.2, 2.3]
|
70
|
+
|
71
|
+
You can also get the array of values with #current:
|
72
|
+
|
73
|
+
my_data.current('miles') # prints [2.2, 3.1, 0.0, 4.3, 1.2, 12.2, 2.3]
|
48
74
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
puts "
|
53
|
-
|
54
|
-
|
75
|
+
You can change all the values associated with any data variable by setting the value to a different array:
|
76
|
+
|
77
|
+
my_data.pizzas = [4, 6, 3, 2, 3.5, 7, 2]
|
78
|
+
puts "Pizzas eaten on Tuesday: #{my_data.pizzas_on('Tuesday')}" # prints 3
|
79
|
+
|
80
|
+
You can change a single value in any data variable by calling #set:
|
81
|
+
|
82
|
+
my_data.set('pizzas', 'Tuesday', 44)
|
83
|
+
|
84
|
+
This will change the value:
|
85
|
+
|
86
|
+
puts my_data.pizzas_on('Tuesday') # prints 44
|
87
|
+
puts my_data.pizzas_on('2014-01-03') # prints 44
|
88
|
+
puts my_data.pizzas_on('Jan 3, 2014') # prints 44
|
89
|
+
|
90
|
+
A second example of #set:
|
91
|
+
|
92
|
+
my_data.set('tasks_done', 'Saturday', 77)
|
93
|
+
puts my_data.tasks_done_on('Saturday') # prints 77
|
94
|
+
puts my_data.tasks_done_on('2014-01-07') # prints 77
|
95
|
+
puts my_data.tasks_done_on('Jan 7, 2014') # prints 77
|
96
|
+
|
97
|
+
Currently, SimpleTimeSeries assumes all variable arrays have equal lengths and represent the same sequence of observations. Though the gem says "time series," it should work with any kind of sequential data.
|
55
98
|
|
56
99
|
## Disclaimer
|
57
100
|
|
@@ -64,3 +107,7 @@ This began as a simple code example for a collague who needed to do something si
|
|
64
107
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
108
|
4. Push to the branch (`git push origin my-new-feature`)
|
66
109
|
5. Create new Pull Request
|
110
|
+
|
111
|
+
## Acknowledgements
|
112
|
+
|
113
|
+
Thanks to my employer, Hedgeye, for letting me create and publish this.
|
@@ -13,6 +13,22 @@ class SimpleTimeSeries
|
|
13
13
|
send (what + '_on').to_sym, date
|
14
14
|
end
|
15
15
|
|
16
|
+
def current(what)
|
17
|
+
send what.to_sym
|
18
|
+
end
|
19
|
+
|
20
|
+
def index_of_date_value(date)
|
21
|
+
time_vars.each do |tv_key, tv_val|
|
22
|
+
return tv_val.index(date) if tv_val.include?(date)
|
23
|
+
end
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def set(data_var, date, value)
|
28
|
+
arr_index = index_of_date_value(date)
|
29
|
+
current(data_var)[arr_index] = value
|
30
|
+
end
|
31
|
+
|
16
32
|
private
|
17
33
|
|
18
34
|
def define_time_methods_and_set_values
|
@@ -29,7 +45,7 @@ class SimpleTimeSeries
|
|
29
45
|
self.class.class_eval do
|
30
46
|
define_method(var_on) do |date|
|
31
47
|
time_vars.each do |tv_key, tv_val|
|
32
|
-
# tv_key is 'dows' or 'dates'
|
48
|
+
# tv_key is something like 'dows' or 'dates'
|
33
49
|
# tv_val is an array of associated values
|
34
50
|
return eval(var)[tv_val.index(date)] if tv_val.include?(date)
|
35
51
|
end
|
@@ -50,12 +66,4 @@ class SimpleTimeSeries
|
|
50
66
|
end
|
51
67
|
end
|
52
68
|
|
53
|
-
def dows_index(date)
|
54
|
-
dows.index(date)
|
55
|
-
end
|
56
|
-
|
57
|
-
def date_index(date)
|
58
|
-
dates.index(date)
|
59
|
-
end
|
60
|
-
|
61
69
|
end
|
@@ -5,6 +5,7 @@ describe SimpleTimeSeries do
|
|
5
5
|
before do
|
6
6
|
@dows = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
|
7
7
|
@dates = ['2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05', '2014-01-06', '2014-01-07']
|
8
|
+
@full_dates = ['Jan 1, 2014', 'Jan 2, 2014', 'Jan 3, 2014', 'Jan 4, 2014', 'Jan 5, 2014', 'Jan 6, 2014', 'Jan 7, 2014']
|
8
9
|
@pizzas = [0, 0, 1, 0, 0.5, 0, 2]
|
9
10
|
@miles = [2.2, 3.1, 0.0, 4.3, 1.2, 12.2, 2.3]
|
10
11
|
@tasks_done = [2, 3, 0, 14, 3, 11, 0]
|
@@ -12,7 +13,8 @@ describe SimpleTimeSeries do
|
|
12
13
|
{'pizzas' => @pizzas, 'miles' => @miles,
|
13
14
|
'tasks_done' => @tasks_done},
|
14
15
|
:time_vars =>
|
15
|
-
{'dows' => @dows, 'dates' => @dates
|
16
|
+
{'dows' => @dows, 'dates' => @dates,
|
17
|
+
'full_dates' => @full_dates})
|
16
18
|
end
|
17
19
|
|
18
20
|
it "should be creatable" do
|
@@ -47,6 +49,7 @@ describe SimpleTimeSeries do
|
|
47
49
|
end
|
48
50
|
|
49
51
|
it "should create setter methods for updating a data series" do
|
52
|
+
@my_data.pizzas_on('Tuesday').should == 1
|
50
53
|
@my_data.pizzas = [10, 11, 12, 13, 14, 15, 16]
|
51
54
|
@my_data.pizzas_on('Tuesday').should == 12
|
52
55
|
@my_data.pizzas_on('Thursday').should == 14
|
@@ -54,4 +57,37 @@ describe SimpleTimeSeries do
|
|
54
57
|
@my_data.pizzas_on('2014-01-07').should == 16
|
55
58
|
end
|
56
59
|
|
60
|
+
describe "#current" do
|
61
|
+
|
62
|
+
it "should print the current array for any data variable" do
|
63
|
+
@my_data.current('pizzas').should == @pizzas
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#index_of_date_value" do
|
69
|
+
|
70
|
+
it "should return the array index of any date value" do
|
71
|
+
@my_data.index_of_date_value('Thursday').should == 4
|
72
|
+
@my_data.index_of_date_value('2014-01-01').should == 0
|
73
|
+
@my_data.index_of_date_value('Jan 7, 2014').should == 6
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#set" do
|
79
|
+
|
80
|
+
it "should allow updating a single data series value" do
|
81
|
+
@my_data.set('pizzas', 'Tuesday', 44)
|
82
|
+
@my_data.pizzas_on('Tuesday').should == 44
|
83
|
+
@my_data.pizzas_on('2014-01-03').should == 44
|
84
|
+
@my_data.pizzas_on('Jan 3, 2014').should == 44
|
85
|
+
@my_data.set('tasks_done', 'Saturday', 77)
|
86
|
+
@my_data.tasks_done_on('Saturday').should == 77
|
87
|
+
@my_data.tasks_done_on('2014-01-07').should == 77
|
88
|
+
@my_data.tasks_done_on('Jan 7, 2014').should == 77
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
57
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_time_series
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Lavin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|