simple_time_series 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -0
- data/lib/simple_time_series/simple_time_series.rb +17 -0
- data/lib/simple_time_series/version.rb +1 -1
- data/spec/lib/simple_time_series_spec.rb +50 -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: 6f0bbac3ae67f54d62e95b9f1384a0c929261fcf
|
4
|
+
data.tar.gz: 0958d9d5d77ec9ec70083f6f7e3a0c6d9138b914
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 061975cfffc0d57edcac1d7827aba80c1168bef9f63482932f47108387408af8dbbb1cfa636071f070ff2113ea6caf346904006b1c7ea4a6fde6f304dc359e77
|
7
|
+
data.tar.gz: 3a472b8b83c3d92be143dcb7969e4ff1c275fa64ad81ca61cf50bb8a9a8ebb8c811d816c0195e3322d1a0fa0df9947cf72a0f20d80e9cefa52647b0243698159
|
data/README.md
CHANGED
@@ -138,6 +138,23 @@ You can create new time and data variables and use them together:
|
|
138
138
|
puts my_data.hours_of_tv_on('Fri') # prints 6.5
|
139
139
|
puts my_data.hours_of_tv_on('Sun') # prints 7
|
140
140
|
|
141
|
+
You can extract a subset of any data variable by using any time variable values as indexes for the first and last elements you want in your subset:
|
142
|
+
|
143
|
+
my_data.pizzas_subset('Tuesday','Thursday') # returns [1, 0, 0.5]
|
144
|
+
my_data.tasks_done_subset('Jan 2, 2014','Jan 5, 2014') # returns [3, 0, 14, 3]
|
145
|
+
|
146
|
+
You can even mix different time variables together:
|
147
|
+
|
148
|
+
my_data.tasks_done_subset('Monday','Jan 5, 2014') # returns [3, 0, 14, 3]
|
149
|
+
my_data.pizzas_subset('Jan 1, 2014','2014-01-04') # returns [0, 0, 1, 0]
|
150
|
+
|
151
|
+
You can also set a subset of values
|
152
|
+
|
153
|
+
my_data.pizzas_subset_set('Tuesday','Thursday', [7, 8, 9.5])
|
154
|
+
my_data.pizzas_subset('Tuesday','Thursday') # returns [7, 8, 9.5]
|
155
|
+
my_data.pizzas_subset('2014-01-03','Jan 5, 2014') # returns [7, 8, 9.5]
|
156
|
+
my_data.pizzas # returns [0, 0, 7, 8, 9.5, 0, 2]
|
157
|
+
|
141
158
|
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.
|
142
159
|
|
143
160
|
## Disclaimer
|
@@ -152,6 +169,13 @@ This began as a simple code example for a collague who needed to do something si
|
|
152
169
|
4. Push to the branch (`git push origin my-new-feature`)
|
153
170
|
5. Create new Pull Request
|
154
171
|
|
172
|
+
## Todo
|
173
|
+
|
174
|
+
1. Enable generation of a difference array for any data_var
|
175
|
+
2. Enable cumulative sum on any data_var
|
176
|
+
3. Enable range extraction via data_var[start_time, end_time]. This currently works as data_var_subset(start_time, end_time).
|
177
|
+
4. Enable setting values for a range via data_var[start_time, end_time]=
|
178
|
+
|
155
179
|
## Acknowledgements
|
156
180
|
|
157
181
|
Thanks to my employer, Hedgeye, for letting me create and publish this.
|
@@ -40,6 +40,20 @@ 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|
|
44
|
+
start_idx = index_of_date_value(first)
|
45
|
+
last_idx = index_of_date_value(last)
|
46
|
+
(start_idx && last_idx) ? eval(var)[start_idx..last_idx] : nil
|
47
|
+
end
|
48
|
+
define_method("#{var}_subset_set") do |first, last, val_arr|
|
49
|
+
start_idx = index_of_date_value(first)
|
50
|
+
last_idx = index_of_date_value(last)
|
51
|
+
if (start_idx && last_idx)
|
52
|
+
eval(var)[start_idx..last_idx] = val_arr
|
53
|
+
else
|
54
|
+
raise "Could not run #{var}_subset with values #{val_arr}"
|
55
|
+
end
|
56
|
+
end
|
43
57
|
define_method(var_on) do |date|
|
44
58
|
time_vars.each do |tv_key, tv_val|
|
45
59
|
# tv_key is something like 'dows' or 'dates'
|
@@ -51,6 +65,9 @@ class SimpleTimeSeries
|
|
51
65
|
end
|
52
66
|
instance_variable_set("@#{var}", vals) if vals
|
53
67
|
data_vars[var] = vals unless data_vars.has_key?(var)
|
68
|
+
#def eval(var).[] (first, last)
|
69
|
+
# send "#{var}_subset".to_sym, first, last
|
70
|
+
#end
|
54
71
|
end
|
55
72
|
|
56
73
|
private
|
@@ -143,4 +143,54 @@ describe SimpleTimeSeries do
|
|
143
143
|
|
144
144
|
end
|
145
145
|
|
146
|
+
describe "#[,]" do
|
147
|
+
|
148
|
+
it "returns the correct array subset when passed index values" do
|
149
|
+
@my_data.pizzas[1,4].should == @pizzas[1,4]
|
150
|
+
end
|
151
|
+
|
152
|
+
#it "returns the correct array subset when passed index values" do
|
153
|
+
# @my_data.miles['2014-01-03','Saturday'].should = [0.0, 4.3, 1.2, 12.2, 2.3]
|
154
|
+
#end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
describe "#xyz_subset" do
|
159
|
+
|
160
|
+
it "returns the correct array subset when passed time_var values" do
|
161
|
+
@my_data.pizzas_subset('Tuesday','Thursday').should == [1, 0, 0.5]
|
162
|
+
end
|
163
|
+
|
164
|
+
it "returns the correct array subset when passed time_var values" do
|
165
|
+
@my_data.pizzas_subset('Tuesday','Saturday').should == [1, 0, 0.5, 0, 2]
|
166
|
+
end
|
167
|
+
|
168
|
+
it "returns the correct array subset when passed time_var values" do
|
169
|
+
@my_data.pizzas_subset('2014-01-03','2014-01-07').should == [1, 0, 0.5, 0, 2]
|
170
|
+
end
|
171
|
+
|
172
|
+
it "returns the correct array subset when passed time_var values" do
|
173
|
+
@my_data.tasks_done_subset('Jan 2, 2014','Jan 5, 2014').should == [3, 0, 14, 3]
|
174
|
+
@my_data.pizzas_subset('Jan 1, 2014','Jan 4, 2014').should == [0, 0, 1, 0]
|
175
|
+
end
|
176
|
+
|
177
|
+
it "returns the correct array subset when passed a mix of time_var types" do
|
178
|
+
@my_data.tasks_done_subset('Monday','Jan 5, 2014').should == [3, 0, 14, 3]
|
179
|
+
@my_data.pizzas_subset('Jan 1, 2014','2014-01-04').should == [0, 0, 1, 0]
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "#xyz_subset_set" do
|
185
|
+
|
186
|
+
it "returns the correct array subset when passed time_var values" do
|
187
|
+
@my_data.pizzas_subset('Tuesday','Thursday').should == [1, 0, 0.5]
|
188
|
+
@my_data.pizzas_subset_set('Tuesday','Thursday', [7, 8, 9.5])
|
189
|
+
@my_data.pizzas_subset('Tuesday','Thursday').should == [7, 8, 9.5]
|
190
|
+
@my_data.pizzas_subset('2014-01-03','Jan 5, 2014').should == [7, 8, 9.5]
|
191
|
+
@my_data.pizzas.should == [0, 0, 7, 8, 9.5, 0, 2]
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
146
196
|
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.4
|
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-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|