Class: SimpleTimeSeries
- Inherits:
-
Object
- Object
- SimpleTimeSeries
- Defined in:
- lib/simple_time_series/version.rb,
lib/simple_time_series/simple_time_series.rb
Constant Summary collapse
- VERSION =
"0.1.3"- DEBUG =
false
Instance Attribute Summary collapse
-
#data_vars ⇒ Object
Returns the value of attribute data_vars.
-
#time_vars ⇒ Object
Returns the value of attribute time_vars.
Instance Method Summary collapse
- #current(what, opts = {}) ⇒ Object
- #data_array(*data_var_names, opts) ⇒ Object
- #find(what, date, end_date = nil, opts = {}) ⇒ Object
- #find_plus_label(what, date, end_date = nil) ⇒ Object
- #index_of_date_value(date) ⇒ Object
-
#initialize(opts) ⇒ SimpleTimeSeries
constructor
A new instance of SimpleTimeSeries.
- #new_data_var(var, vals) ⇒ Object
- #new_time_var(var, vals) ⇒ Object
- #set(data_var, date, value) ⇒ Object
- #sum_by_date(*data_var_names, opts) ⇒ Object
Constructor Details
#initialize(opts) ⇒ SimpleTimeSeries
Returns a new instance of SimpleTimeSeries.
7 8 9 10 11 12 |
# File 'lib/simple_time_series/simple_time_series.rb', line 7 def initialize(opts) @time_vars = opts[:time_vars] @data_vars = opts[:data_vars] define_data_methods_and_set_values define_time_methods_and_set_values end |
Instance Attribute Details
#data_vars ⇒ Object
Returns the value of attribute data_vars.
5 6 7 |
# File 'lib/simple_time_series/simple_time_series.rb', line 5 def data_vars @data_vars end |
#time_vars ⇒ Object
Returns the value of attribute time_vars.
5 6 7 |
# File 'lib/simple_time_series/simple_time_series.rb', line 5 def time_vars @time_vars end |
Instance Method Details
#current(what, opts = {}) ⇒ Object
53 54 55 56 57 |
# File 'lib/simple_time_series/simple_time_series.rb', line 53 def current(what, opts={}) puts "#current called with #{what} and #{opts}" if DEBUG vals = send(what.to_sym, opts) opts[:prepend_names] ? vals.dup.unshift(what) : vals end |
#data_array(*data_var_names, opts) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/simple_time_series/simple_time_series.rb', line 35 def data_array(*data_var_names, opts) err_msg = "The last parameter passed to #data_array must be a hash of start/end values.\n" err_msg += "Example: {:start => 'Tuesday', :end => '2014-01-06'}\n" err_msg += "If you want to use all observations, you can pass a blank hash, like {}\n" raise err_msg unless opts data_arr = [] Array(data_var_names).each do |name| puts "Looping through data_var_names inside data_array with #{name}" if DEBUG if opts[:start] && opts[:end] puts "Calling find(#{name}, #{opts[:start]}, #{opts[:end]}, #{opts})" if DEBUG data_arr << find(name, opts[:start], opts[:end], opts) else data_arr << current(name, opts) end end data_arr end |
#find(what, date, end_date = nil, opts = {}) ⇒ Object
14 15 16 17 |
# File 'lib/simple_time_series/simple_time_series.rb', line 14 def find(what, date, end_date=nil, opts={}) puts "Calling send(#{what}_on, #{date}, #{end_date}, #{opts})" if DEBUG send((what + '_on').to_sym, date, end_date, opts) end |
#find_plus_label(what, date, end_date = nil) ⇒ Object
19 20 21 |
# File 'lib/simple_time_series/simple_time_series.rb', line 19 def find_plus_label(what, date, end_date=nil) find(what, date, end_date).dup.unshift(what) end |
#index_of_date_value(date) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/simple_time_series/simple_time_series.rb', line 59 def index_of_date_value(date) time_vars.each do |tv_key, tv_val| return tv_val.index(date) if tv_val.include?(date) end nil end |
#new_data_var(var, vals) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/simple_time_series/simple_time_series.rb', line 79 def new_data_var(var, vals) define_getter_and_setter(var) self.class.class_eval do define_method("#{var}_subset") do |first, last=first| start_idx = index_of_date_value(first) last_idx = index_of_date_value(last) (start_idx && last_idx) ? eval(var)[start_idx..last_idx] : nil end define_method("#{var}_subset_set") do |first, last, val_arr| start_idx = index_of_date_value(first) last_idx = index_of_date_value(last) if (start_idx && last_idx) eval(var)[start_idx..last_idx] = val_arr else raise "Could not run #{var}_subset with values #{val_arr}" end end define_method("#{var}_diff") do |first=nil, last=nil| # should work only on numeric data # this could be made more efficient by caching the full array and/or calculating only a subset of values time_vars.each do |tv_key, tv_val| start_idx = index_of_date_value(first) || 0 last_idx = index_of_date_value(last) || (first.nil? ? -1 : start_idx) answer = (eval(var).each_cons(2).map { |val1, val2| val2 - val1 }.dup.unshift(nil))[start_idx..last_idx] return answer.length == 1 ? answer[0] : answer end end # should DRY out variable creation, probably by passing in a strategy along with the variable name define_method("#{var}_cumsum") do |first=nil, last=nil| # should work only on numeric data # this could be made more efficient by caching the full array and/or calculating only a subset of values time_vars.each do |tv_key, tv_val| start_idx = index_of_date_value(first) || 0 last_idx = index_of_date_value(last) || (first.nil? ? -1 : start_idx) sum = eval(var)[0] answer = (eval(var).each_cons(2).map { |val1, val2| sum += val2 }.dup.unshift(eval(var)[start_idx]))[start_idx..last_idx] return answer.length == 1 ? answer[0] : answer end end end define_var_on(var) instance_variable_set("@#{var}", vals) if vals data_vars[var] = vals unless data_vars.has_key?(var) end |
#new_time_var(var, vals) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/simple_time_series/simple_time_series.rb', line 72 def new_time_var(var, vals) define_getter_and_setter(var) instance_variable_set("@#{var}", vals) if vals time_vars[var] = vals unless time_vars.has_key?(var) define_var_on(var) end |
#set(data_var, date, value) ⇒ Object
66 67 68 69 |
# File 'lib/simple_time_series/simple_time_series.rb', line 66 def set(data_var, date, value) arr_index = index_of_date_value(date) current(data_var)[arr_index] = value end |
#sum_by_date(*data_var_names, opts) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/simple_time_series/simple_time_series.rb', line 23 def sum_by_date(*data_var_names, opts) arr = [] data_var_names.each do |name| if opts[:start] && opts[:end] arr << find(name, opts[:start], opts[:end], opts) else arr << current(name, opts) end end arr.transpose.map { |arr| arr.reduce(:+) } end |