simple_time_series 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +44 -0
- data/lib/simple_time_series/simple_time_series.rb +26 -15
- data/lib/simple_time_series/version.rb +1 -1
- data/spec/lib/simple_time_series_spec.rb +63 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a99423c6c3e3ef3717c741f6c62a8f6b7d432140
|
4
|
+
data.tar.gz: 94125e0737e9353a24c0e81ce75cd9c226742bde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87f4f7d17a23a0eb793483b4864913d7319ffdd4675706c2b9899d262b854977e63263cb422928f80e4ab865e97b072cb57f96b64c32a4d8e5204fe08f033e5c
|
7
|
+
data.tar.gz: 1b6cfeab601d9fb0e70e7a44ddba59c1a378ea76f5e09d845b3ae38799cb45a6ab0963665f62b86d69f8e79321dda06e20c4c30ae40faff6f0e810a64aaedeff
|
data/README.md
CHANGED
@@ -94,6 +94,50 @@ A second example of #set:
|
|
94
94
|
puts my_data.tasks_done_on('2014-01-07') # prints 77
|
95
95
|
puts my_data.tasks_done_on('Jan 7, 2014') # prints 77
|
96
96
|
|
97
|
+
You can use #set to build up a data variable from an empty array. Begin by assigning an empty array to a data_var:
|
98
|
+
|
99
|
+
my_data2 = SimpleTimeSeries.new(:time_vars => {'dows' => dows,
|
100
|
+
'dates' => dates,
|
101
|
+
'full_dates' => full_dates},
|
102
|
+
:data_vars => {'empty' => []}
|
103
|
+
|
104
|
+
You can then set individual values using #set till you've filled in all values:
|
105
|
+
|
106
|
+
my_data2.set('empty', 'Jan 1, 2014', 1)
|
107
|
+
my_data2.set('empty', 'Monday', 2)
|
108
|
+
my_data2.set('empty', '2014-01-03', 3)
|
109
|
+
my_data2.set('empty', 'Jan 4, 2014', 4)
|
110
|
+
my_data2.set('empty', 'Thursday', 5)
|
111
|
+
my_data2.set('empty', '2014-01-06', 6)
|
112
|
+
my_data2.set('empty', 'Jan 7, 2014', 7)
|
113
|
+
|
114
|
+
my_data2.empty now equals [1, 2, 3, 4, 5, 6, 7]
|
115
|
+
|
116
|
+
You can also create new time variables and data variables after you have created your SimpleTimeSeries object.
|
117
|
+
|
118
|
+
Here's how you can create and use a new time variable:
|
119
|
+
|
120
|
+
my_data.new_time_var('short_dow',['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'])
|
121
|
+
|
122
|
+
puts my_data.tasks_done_on('Fri') # prints 11
|
123
|
+
puts my_data.pizzas_on('Thurs') # prints 0.5
|
124
|
+
|
125
|
+
Here's how you can create and use a new data variable:
|
126
|
+
|
127
|
+
my_data.new_data_var('hours_of_tv',[7, 3, 3.5, 3, 4, 6.5, 11])
|
128
|
+
|
129
|
+
puts my_data.hours_of_tv # prints [7, 3, 3.5, 3, 4, 6.5, 11]
|
130
|
+
puts my_data.hours_of_tv_on('Friday') # prints 6.5
|
131
|
+
puts my_data.hours_of_tv_on('2014-01-01') # prints 7
|
132
|
+
|
133
|
+
You can create new time and data variables and use them together:
|
134
|
+
|
135
|
+
my_data.new_time_var('short_dow',['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'])
|
136
|
+
my_data.new_data_var('hours_of_tv',[7, 3, 3.5, 3, 4, 6.5, 11])
|
137
|
+
|
138
|
+
puts my_data.hours_of_tv_on('Fri') # prints 6.5
|
139
|
+
puts my_data.hours_of_tv_on('Sun') # prints 7
|
140
|
+
|
97
141
|
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.
|
98
142
|
|
99
143
|
## Disclaimer
|
@@ -29,30 +29,41 @@ class SimpleTimeSeries
|
|
29
29
|
current(data_var)[arr_index] = value
|
30
30
|
end
|
31
31
|
|
32
|
+
|
33
|
+
def new_time_var(var, vals)
|
34
|
+
define_getter_and_setter(var)
|
35
|
+
instance_variable_set("@#{var}", vals) if vals
|
36
|
+
time_vars[var] = vals unless time_vars.has_key?(var)
|
37
|
+
end
|
38
|
+
|
39
|
+
def new_data_var(var, vals)
|
40
|
+
define_getter_and_setter(var)
|
41
|
+
var_on = "#{var}_on"
|
42
|
+
self.class.class_eval do
|
43
|
+
define_method(var_on) do |date|
|
44
|
+
time_vars.each do |tv_key, tv_val|
|
45
|
+
# tv_key is something like 'dows' or 'dates'
|
46
|
+
# tv_val is an array of associated values
|
47
|
+
return eval(var)[tv_val.index(date)] if tv_val.include?(date)
|
48
|
+
end
|
49
|
+
raise "Can't find #{var_on} for #{date}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
instance_variable_set("@#{var}", vals) if vals
|
53
|
+
data_vars[var] = vals unless data_vars.has_key?(var)
|
54
|
+
end
|
55
|
+
|
32
56
|
private
|
33
57
|
|
34
58
|
def define_time_methods_and_set_values
|
35
59
|
time_vars.each do |var, vals|
|
36
|
-
|
37
|
-
instance_variable_set("@#{var}", vals) if vals
|
60
|
+
new_time_var(var, vals)
|
38
61
|
end
|
39
62
|
end
|
40
63
|
|
41
64
|
def define_data_methods_and_set_values
|
42
65
|
data_vars.each do |var, vals|
|
43
|
-
|
44
|
-
var_on = "#{var}_on"
|
45
|
-
self.class.class_eval do
|
46
|
-
define_method(var_on) do |date|
|
47
|
-
time_vars.each do |tv_key, tv_val|
|
48
|
-
# tv_key is something like 'dows' or 'dates'
|
49
|
-
# tv_val is an array of associated values
|
50
|
-
return eval(var)[tv_val.index(date)] if tv_val.include?(date)
|
51
|
-
end
|
52
|
-
raise "Can't find #{var_on} for #{date}"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
instance_variable_set("@#{var}", vals) if vals
|
66
|
+
new_data_var(var, vals)
|
56
67
|
end
|
57
68
|
end
|
58
69
|
|
@@ -11,28 +11,28 @@ describe SimpleTimeSeries do
|
|
11
11
|
@tasks_done = [2, 3, 0, 14, 3, 11, 0]
|
12
12
|
@my_data = SimpleTimeSeries.new(:data_vars =>
|
13
13
|
{'pizzas' => @pizzas, 'miles' => @miles,
|
14
|
-
'tasks_done' => @tasks_done},
|
14
|
+
'tasks_done' => @tasks_done, 'empty' => []},
|
15
15
|
:time_vars =>
|
16
16
|
{'dows' => @dows, 'dates' => @dates,
|
17
17
|
'full_dates' => @full_dates})
|
18
18
|
end
|
19
19
|
|
20
|
-
it "
|
20
|
+
it "is creatable" do
|
21
21
|
@my_data.should be_a(SimpleTimeSeries)
|
22
22
|
end
|
23
23
|
|
24
|
-
it "
|
24
|
+
it "sets #time_vars correctly" do
|
25
25
|
@my_data.time_vars["dates"].should == @dates
|
26
26
|
@my_data.time_vars["dows"].should == @dows
|
27
27
|
end
|
28
28
|
|
29
|
-
it "
|
29
|
+
it "has the correct methods" do
|
30
30
|
[:time_vars, :time_vars=, :data_vars, :data_vars=, :find, :pizzas, :pizzas=, :pizzas_on, :miles, :miles=, :miles_on, :tasks_done, :tasks_done=, :tasks_done_on, :dows, :dows=, :dates, :dates=].each do |mthd|
|
31
31
|
@my_data.methods.should include(mthd)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
it "
|
35
|
+
it "creates accessor methods for looking up data values for any time observation" do
|
36
36
|
@my_data.pizzas_on('Tuesday').should == 1
|
37
37
|
@my_data.pizzas_on('Thursday').should == 0.5
|
38
38
|
@my_data.miles_on('Sunday').should == 2.2
|
@@ -40,7 +40,7 @@ describe SimpleTimeSeries do
|
|
40
40
|
@my_data.tasks_done_on('2014-01-02').should == 3
|
41
41
|
end
|
42
42
|
|
43
|
-
it "
|
43
|
+
it "creates a #find method for finding any data value for any time observation" do
|
44
44
|
@my_data.find('pizzas', 'Tuesday').should == 1
|
45
45
|
@my_data.find('pizzas', 'Thursday').should == 0.5
|
46
46
|
@my_data.find('miles', 'Sunday').should == 2.2
|
@@ -48,7 +48,7 @@ describe SimpleTimeSeries do
|
|
48
48
|
@my_data.find('tasks_done', '2014-01-02').should == 3
|
49
49
|
end
|
50
50
|
|
51
|
-
it "
|
51
|
+
it "creates setter methods for updating a data series" do
|
52
52
|
@my_data.pizzas_on('Tuesday').should == 1
|
53
53
|
@my_data.pizzas = [10, 11, 12, 13, 14, 15, 16]
|
54
54
|
@my_data.pizzas_on('Tuesday').should == 12
|
@@ -59,7 +59,7 @@ describe SimpleTimeSeries do
|
|
59
59
|
|
60
60
|
describe "#current" do
|
61
61
|
|
62
|
-
it "
|
62
|
+
it "prints the current array for any data variable" do
|
63
63
|
@my_data.current('pizzas').should == @pizzas
|
64
64
|
end
|
65
65
|
|
@@ -67,7 +67,7 @@ describe SimpleTimeSeries do
|
|
67
67
|
|
68
68
|
describe "#index_of_date_value" do
|
69
69
|
|
70
|
-
it "
|
70
|
+
it "returns the array index of any date value" do
|
71
71
|
@my_data.index_of_date_value('Thursday').should == 4
|
72
72
|
@my_data.index_of_date_value('2014-01-01').should == 0
|
73
73
|
@my_data.index_of_date_value('Jan 7, 2014').should == 6
|
@@ -77,7 +77,7 @@ describe SimpleTimeSeries do
|
|
77
77
|
|
78
78
|
describe "#set" do
|
79
79
|
|
80
|
-
it "
|
80
|
+
it "updates a single data series value" do
|
81
81
|
@my_data.set('pizzas', 'Tuesday', 44)
|
82
82
|
@my_data.pizzas_on('Tuesday').should == 44
|
83
83
|
@my_data.pizzas_on('2014-01-03').should == 44
|
@@ -88,6 +88,59 @@ describe SimpleTimeSeries do
|
|
88
88
|
@my_data.tasks_done_on('Jan 7, 2014').should == 77
|
89
89
|
end
|
90
90
|
|
91
|
+
it "lets you build up a data series datapoint by datapoint" do
|
92
|
+
@my_data.current('empty').should == []
|
93
|
+
@my_data.set('empty', 'Jan 1, 2014', 1)
|
94
|
+
@my_data.set('empty', 'Monday', 2)
|
95
|
+
@my_data.set('empty', '2014-01-03', 3)
|
96
|
+
@my_data.set('empty', 'Jan 4, 2014', 4)
|
97
|
+
@my_data.set('empty', 'Thursday', 5)
|
98
|
+
@my_data.set('empty', '2014-01-06', 6)
|
99
|
+
@my_data.set('empty', 'Jan 7, 2014', 7)
|
100
|
+
@my_data.current('empty').should == [1, 2, 3, 4, 5, 6, 7]
|
101
|
+
end
|
102
|
+
|
103
|
+
it "lets you build up a data series datapoint by datapoint in any order" do
|
104
|
+
@my_data.current('empty').should == []
|
105
|
+
@my_data.set('empty', '2014-01-06', 6)
|
106
|
+
@my_data.set('empty', 'Jan 4, 2014', 4)
|
107
|
+
@my_data.set('empty', 'Jan 1, 2014', 1)
|
108
|
+
@my_data.set('empty', '2014-01-03', 3)
|
109
|
+
@my_data.set('empty', 'Thursday', 5)
|
110
|
+
@my_data.set('empty', 'Jan 7, 2014', 7)
|
111
|
+
@my_data.set('empty', 'Monday', 2)
|
112
|
+
@my_data.current('empty').should == [1, 2, 3, 4, 5, 6, 7]
|
113
|
+
@my_data.empty.should == [1, 2, 3, 4, 5, 6, 7]
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#new_time_var" do
|
119
|
+
|
120
|
+
it "lets you create a new time variable after object instantiation" do
|
121
|
+
@my_data.new_time_var('short_dow',['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'])
|
122
|
+
@my_data.tasks_done_on('Fri').should == 11
|
123
|
+
@my_data.pizzas_on('Thurs').should == 0.5
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#new_data_var" do
|
129
|
+
|
130
|
+
it "lets you create a new data variable after object instantiation" do
|
131
|
+
@my_data.new_data_var('hours_of_tv',[7, 3, 3.5, 3, 4, 6.5, 11])
|
132
|
+
@my_data.hours_of_tv.should == [7, 3, 3.5, 3, 4, 6.5, 11]
|
133
|
+
@my_data.hours_of_tv_on('Friday').should == 6.5
|
134
|
+
@my_data.hours_of_tv_on('2014-01-01').should == 7
|
135
|
+
end
|
136
|
+
|
137
|
+
it "lets you use a new data variable with a new time variable" do
|
138
|
+
@my_data.new_time_var('short_dow',['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'])
|
139
|
+
@my_data.new_data_var('hours_of_tv',[7, 3, 3.5, 3, 4, 6.5, 11])
|
140
|
+
@my_data.hours_of_tv_on('Fri').should == 6.5
|
141
|
+
@my_data.hours_of_tv_on('Sun').should == 7
|
142
|
+
end
|
143
|
+
|
91
144
|
end
|
92
145
|
|
93
146
|
end
|