Class: TimeArray::TimeArray
- Inherits:
-
Object
- Object
- TimeArray::TimeArray
- Defined in:
- lib/time_array/time_array.rb
Instance Attribute Summary collapse
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#v ⇒ Object
readonly
Returns the value of attribute v.
Instance Method Summary collapse
- #*(vec) ⇒ Object
- #+(vec) ⇒ Object
- #-(vec) ⇒ Object
- #/(vec) ⇒ Object
- #align_with(other_array) ⇒ Object
- #aligned_with?(other_array) ⇒ Boolean
-
#all_to(new_value) ⇒ Object
Set all vector values to a new value.
-
#avg(options = {}) ⇒ Object
Get the average of values.
- #clear_data ⇒ Object
- #clone ⇒ Object
-
#count(options = {}) ⇒ Object
Count the values.
- #empty? ⇒ Boolean
-
#end_time ⇒ Object
Return the time assiociated with the last value.
- #first_values(number) ⇒ Object
- #group_by(interval) ⇒ Object
-
#initialize(start_time, values = Vector.new, options = {}) ⇒ TimeArray
constructor
A new instance of TimeArray.
-
#max ⇒ Object
Get the maximum value.
-
#min ⇒ Object
Get the minimum value.
- #print_values ⇒ Object
-
#round!(ndigit = 3) ⇒ Object
Round every values of the array.
- #set_value(index, new_value) ⇒ Object
- #size ⇒ Object
-
#sum(options = {}) ⇒ Object
get the sum of the values.
- #time_zone ⇒ Object
- #to_s ⇒ Object
-
#until_the_end_of_the_year(fill_value = 0.0) ⇒ Object
mah!.
- #value(index) ⇒ Object
- #zone ⇒ Object
Constructor Details
#initialize(start_time, values = Vector.new, options = {}) ⇒ TimeArray
Returns a new instance of TimeArray.
11 12 13 14 15 |
# File 'lib/time_array/time_array.rb', line 11 def initialize(start_time, values=Vector.new, ={}) () set_start_time start_time set_values values end |
Instance Attribute Details
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
9 10 11 |
# File 'lib/time_array/time_array.rb', line 9 def start_time @start_time end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
9 10 11 |
# File 'lib/time_array/time_array.rb', line 9 def unit @unit end |
#v ⇒ Object (readonly)
Returns the value of attribute v.
9 10 11 |
# File 'lib/time_array/time_array.rb', line 9 def v @v end |
Instance Method Details
#*(vec) ⇒ Object
171 172 173 |
# File 'lib/time_array/time_array.rb', line 171 def *(vec) oper(vec, :*) end |
#+(vec) ⇒ Object
163 164 165 |
# File 'lib/time_array/time_array.rb', line 163 def +(vec) oper(vec, :+) end |
#-(vec) ⇒ Object
167 168 169 |
# File 'lib/time_array/time_array.rb', line 167 def -(vec) oper(vec, :-) end |
#/(vec) ⇒ Object
175 176 177 |
# File 'lib/time_array/time_array.rb', line 175 def /(vec) oper(vec, :/) end |
#align_with(other_array) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/time_array/time_array.rb', line 127 def align_with(other_array) # @start_time = other_array.start_time if @start_time.nil? # ==== return self if empty? return clear_data if other_array.empty? new_start_time = [start_time, other_array.start_time].max new_end_time = [end_time, other_array.end_time].min if end_time.nil? || other_array.end_time.nil? || new_start_time>new_end_time clear_data else @v = @v[((new_start_time-start_time)/3600).to_i, 1+((new_end_time-new_start_time)/3600).to_i] end @start_time = new_start_time self end |
#aligned_with?(other_array) ⇒ Boolean
123 124 125 |
# File 'lib/time_array/time_array.rb', line 123 def aligned_with?(other_array) self.start_time==other_array.start_time && @v.size==other_array.size end |
#all_to(new_value) ⇒ Object
Set all vector values to a new value
31 32 33 34 35 |
# File 'lib/time_array/time_array.rb', line 31 def all_to(new_value) @v = Vector.new(@v.size, new_value) # @v.map!{|e| e=new_value} self end |
#avg(options = {}) ⇒ Object
Get the average of values
77 78 79 80 81 |
# File 'lib/time_array/time_array.rb', line 77 def avg( = {}) c = count() return nil if c.zero? # if the array is empty will be returned nil sum() / c end |
#clear_data ⇒ Object
117 118 119 120 121 |
# File 'lib/time_array/time_array.rb', line 117 def clear_data raise NilStartTimeError if @start_time.nil? @v = Vector.new self end |
#clone ⇒ Object
26 27 28 |
# File 'lib/time_array/time_array.rb', line 26 def clone TimeArray.new(@start_time, (@v.clone rescue Vector.new), zone: Time.zone.name) end |
#count(options = {}) ⇒ Object
Count the values
66 67 68 69 70 71 72 73 |
# File 'lib/time_array/time_array.rb', line 66 def count( = {}) if [:values] raise ArgumentError, "Option not recognized" if !%w(positive negative non_positive non_negative non_zero zero all).include?([:values].to_s) @v.send("count_"+[:values].to_s) else @v.count_all end end |
#empty? ⇒ Boolean
107 108 109 |
# File 'lib/time_array/time_array.rb', line 107 def empty? @v.nil? || @v.empty? end |
#end_time ⇒ Object
Return the time assiociated with the last value
112 113 114 115 |
# File 'lib/time_array/time_array.rb', line 112 def end_time return nil if empty? @start_time + (@v.size-1).hours end |
#first_values(number) ⇒ Object
183 184 185 |
# File 'lib/time_array/time_array.rb', line 183 def first_values(number) @v[0,number] end |
#group_by(interval) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/time_array/time_array.rb', line 191 def group_by(interval) raise ArgumentError, "interval not valid. Valid intervals are :hour, :day, :wday, :month" unless Units.valid?(interval) t = start_time h = GroupHash.new{|h,k| h[k]=[]} @v.each do |v| h[t.send(interval)]<<v t+=1.hour end h end |
#max ⇒ Object
Get the maximum value
91 92 93 94 95 |
# File 'lib/time_array/time_array.rb', line 91 def max @v.compact.max rescue nil end |
#min ⇒ Object
Get the minimum value
84 85 86 87 88 |
# File 'lib/time_array/time_array.rb', line 84 def min @v.compact.min rescue nil end |
#print_values ⇒ Object
146 147 148 149 |
# File 'lib/time_array/time_array.rb', line 146 def print_values start_time = @start_time - 1.hour @v.collect{|v| "#{(start_time+=1.hour).strftime('%Y-%m-%d %H:%M %a')}\t#{v}" }.join("\n") end |
#round!(ndigit = 3) ⇒ Object
Round every values of the array
98 99 100 101 |
# File 'lib/time_array/time_array.rb', line 98 def round!(ndigit=3) @v.collect!{|e| e.nil? ? nil : e.round(ndigit)} self end |
#set_value(index, new_value) ⇒ Object
187 188 189 |
# File 'lib/time_array/time_array.rb', line 187 def set_value(index, new_value) @v[index]=new_value if index>=0 && index<@v.size end |
#size ⇒ Object
37 38 39 |
# File 'lib/time_array/time_array.rb', line 37 def size @v.size end |
#sum(options = {}) ⇒ Object
get the sum of the values
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/time_array/time_array.rb', line 43 def sum( = {}) if [:values] opt = [:values].to_sym case opt when :positive, :non_negative @v.sum_positive when :negative, :non_positive @v.sum_negative when :zero 0.0 when :all, :non_zero @v.sum_all else raise ArgumentError, "Option '#{opt}' not recognized" end else @v.sum_all end end |
#time_zone ⇒ Object
22 23 24 |
# File 'lib/time_array/time_array.rb', line 22 def time_zone Time.zone end |
#to_s ⇒ Object
151 152 153 |
# File 'lib/time_array/time_array.rb', line 151 def to_s "Start time: #{@start_time}\nData (size: #{@v.size}):\n#{print_values}" end |
#until_the_end_of_the_year(fill_value = 0.0) ⇒ Object
mah!
156 157 158 159 160 161 |
# File 'lib/time_array/time_array.rb', line 156 def until_the_end_of_the_year(fill_value=0.0) t = Time.zone.parse("#{@start_time.year+1}-01-01") hh = (t-@start_time)/( 60 * 60) # final size @v += Vector.new(hh-@v.size, fill_value) if hh>@v.size self end |
#value(index) ⇒ Object
179 180 181 |
# File 'lib/time_array/time_array.rb', line 179 def value(index) @v[index] end |
#zone ⇒ Object
18 19 20 |
# File 'lib/time_array/time_array.rb', line 18 def zone Time.zone.name end |