simple_time_series 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -5
- data/lib/simple_time_series/simple_time_series.rb +10 -1
- data/lib/simple_time_series/version.rb +1 -1
- data/spec/lib/simple_time_series_spec.rb +27 -0
- 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: 022c7f24717b5750931b1a724b4c501f35faa824
|
4
|
+
data.tar.gz: 790727d3ebc4c5f2e661cdc2c2924c1851f6303f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdecc62389e5e0d65fbccf6437ad5213351310228084da6a3a955d949f462c28909cb1489f97915ab12cad6e459d74644ce644b5b4179f1c3103fb457923c2a3
|
7
|
+
data.tar.gz: 9be0f918259ebcde9f315eb92c5ec93dfc734decbd25defb0a3263ac3143a889f97748c417f142a4f792493aa14d370985fd7dfe24e89b533c3cd708c25d6264
|
data/README.md
CHANGED
@@ -155,11 +155,29 @@ You can also set a subset of values
|
|
155
155
|
my_data.pizzas_subset('2014-01-03','Jan 5, 2014') # returns [7, 8, 9.5]
|
156
156
|
my_data.pizzas # returns [0, 0, 7, 8, 9.5, 0, 2]
|
157
157
|
|
158
|
+
And you can get an array of differences between consecutive observations for any data_var:
|
159
|
+
|
160
|
+
my_data.tasks_done_diff # returns [nil, 1, -3, 14, -11, 8, -11]
|
161
|
+
my_data.pizzas_diff # returns [nil, 0, 1, -1, 0.5, -0.5, 2]
|
162
|
+
|
163
|
+
You can also grab subsets of these arrays of differences. It returns a single value if you request a single value:
|
164
|
+
|
165
|
+
my_data.tasks_done_diff('Saturday') # returns -11
|
166
|
+
my_data.tasks_done_diff('Sunday') # returns nil (because the first time slot cannot have a difference)
|
167
|
+
my_data.pizzas_diff('Saturday') # returns 2
|
168
|
+
my_data.pizzas_diff('2014-01-01') # returns nil (again, because the first time slot has no difference)
|
169
|
+
my_data.pizzas_diff('Jan 4, 2014') # returns -1
|
170
|
+
|
171
|
+
It returns an array if you ask for a range of values:
|
172
|
+
|
173
|
+
my_data.tasks_done_diff('Sunday','Monday') # returns [nil, 1]
|
174
|
+
my_data.tasks_done_diff('Tuesday','Saturday') # returns [-3, 14, -11, 8, -11]
|
175
|
+
|
158
176
|
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.
|
159
177
|
|
160
178
|
## Disclaimer
|
161
179
|
|
162
|
-
This began as a simple code example for a collague who
|
180
|
+
This began as a simple code example for a collague who had programmed something similar but in a manual, repetitive way with integer-indexed subscript references to rows and columns. The code worked but had become an unmaintainable, non-extensible monster. When he said he might actually use my code example, I turned it into this gem. It's really simple, with very basic functionality, but he has now started using this, so I may progressively add bells and whistles.
|
163
181
|
|
164
182
|
## Contributing
|
165
183
|
|
@@ -171,10 +189,9 @@ This began as a simple code example for a collague who needed to do something si
|
|
171
189
|
|
172
190
|
## Todo
|
173
191
|
|
174
|
-
1. Enable
|
175
|
-
2. Enable
|
176
|
-
3. Enable range
|
177
|
-
4. Enable setting values for a range via data_var[start_time, end_time]=
|
192
|
+
1. Enable cumulative sum on any data_var
|
193
|
+
2. Enable range extraction via data_var[start_time, end_time]. This currently works as data_var_subset(start_time, end_time).
|
194
|
+
3. Enable setting values for a range via data_var[start_time, end_time]=
|
178
195
|
|
179
196
|
## Acknowledgements
|
180
197
|
|
@@ -40,7 +40,7 @@ class SimpleTimeSeries
|
|
40
40
|
define_getter_and_setter(var)
|
41
41
|
var_on = "#{var}_on"
|
42
42
|
self.class.class_eval do
|
43
|
-
define_method("#{var}_subset") do |first, last|
|
43
|
+
define_method("#{var}_subset") do |first, last=first|
|
44
44
|
start_idx = index_of_date_value(first)
|
45
45
|
last_idx = index_of_date_value(last)
|
46
46
|
(start_idx && last_idx) ? eval(var)[start_idx..last_idx] : nil
|
@@ -54,6 +54,15 @@ class SimpleTimeSeries
|
|
54
54
|
raise "Could not run #{var}_subset with values #{val_arr}"
|
55
55
|
end
|
56
56
|
end
|
57
|
+
define_method("#{var}_diff") do |first=nil, last=nil| # should work only on numeric data
|
58
|
+
# this could be made more efficient by caching the full array and/or calculating only a subset of values
|
59
|
+
time_vars.each do |tv_key, tv_val|
|
60
|
+
start_idx = index_of_date_value(first) || 0
|
61
|
+
last_idx = index_of_date_value(last) || (first.nil? ? -1 : start_idx)
|
62
|
+
answer = (eval(var).each_cons(2).map { |val1, val2| val2 - val1 }.unshift(nil))[start_idx..last_idx]
|
63
|
+
return answer.length == 1 ? answer[0] : answer
|
64
|
+
end
|
65
|
+
end
|
57
66
|
define_method(var_on) do |date|
|
58
67
|
time_vars.each do |tv_key, tv_val|
|
59
68
|
# tv_key is something like 'dows' or 'dates'
|
@@ -161,6 +161,10 @@ describe SimpleTimeSeries do
|
|
161
161
|
@my_data.pizzas_subset('Tuesday','Thursday').should == [1, 0, 0.5]
|
162
162
|
end
|
163
163
|
|
164
|
+
it "returns the correct array subset when passed a single time_var value" do
|
165
|
+
@my_data.pizzas_subset('Thursday').should == [0.5]
|
166
|
+
end
|
167
|
+
|
164
168
|
it "returns the correct array subset when passed time_var values" do
|
165
169
|
@my_data.pizzas_subset('Tuesday','Saturday').should == [1, 0, 0.5, 0, 2]
|
166
170
|
end
|
@@ -193,4 +197,27 @@ describe SimpleTimeSeries do
|
|
193
197
|
|
194
198
|
end
|
195
199
|
|
200
|
+
describe "#xyz_diff" do
|
201
|
+
|
202
|
+
it "should calculate the correct vector of differences for the referenced data_var" do
|
203
|
+
@my_data.tasks_done_diff.should == [nil, 1, -3, 14, -11, 8, -11]
|
204
|
+
@my_data.tasks_done_diff[3].should == 14
|
205
|
+
@my_data.pizzas_diff.should == [nil, 0, 1, -1, 0.5, -0.5, 2]
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should let me access a single value in a vector of differences with any time_var value" do
|
209
|
+
@my_data.tasks_done_diff('Saturday').should == -11
|
210
|
+
@my_data.tasks_done_diff('Sunday').should be_nil
|
211
|
+
@my_data.pizzas_diff('Saturday').should == 2
|
212
|
+
@my_data.pizzas_diff('2014-01-01').should be_nil
|
213
|
+
@my_data.pizzas_diff('Jan 4, 2014').should == -1
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should let me access a subarray of a vector of differences with any time_var value" do
|
217
|
+
@my_data.tasks_done_diff('Sunday','Monday').should == [nil, 1]
|
218
|
+
@my_data.tasks_done_diff('Tuesday','Saturday').should == [-3, 14, -11, 8, -11]
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|
222
|
+
|
196
223
|
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.5
|
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-
|
11
|
+
date: 2014-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|