Class: TimeWise::Base
- Inherits:
-
Object
- Object
- TimeWise::Base
- Defined in:
- lib/time_wise/base.rb
Overview
The Base class is the foundation of all time series operations in TimeWise
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dates ⇒ Object
readonly
Returns the value of attribute dates.
Instance Method Summary collapse
-
#initialize(data, dates = nil) ⇒ Base
constructor
Initialize a new time series object.
-
#length ⇒ Integer
Length of the time series.
-
#moving_average ⇒ Object
Access moving average methods.
-
#plot ⇒ Object
Access visualization methods.
-
#slice(start_idx, end_idx) ⇒ TimeWise::Base
Get a section of the time series.
-
#slice_by_date(start_date, end_date) ⇒ TimeWise::Base
Get a section of the time series by date range.
-
#stats ⇒ Object
Apply basic statistical methods.
-
#to_a ⇒ Array
Convert to array.
-
#to_h ⇒ Hash
Get the time series as a hash with date and value arrays.
Constructor Details
#initialize(data, dates = nil) ⇒ Base
Initialize a new time series object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/time_wise/base.rb', line 14 def initialize(data, dates = nil) validate_inputs(data, dates) @data = Numo::DFloat.cast(data) @dates = dates if dates&.length&.positive? @has_dates = true @frequency = detect_frequency else @has_dates = false @frequency = nil end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/time_wise/base.rb', line 9 def data @data end |
#dates ⇒ Object (readonly)
Returns the value of attribute dates.
9 10 11 |
# File 'lib/time_wise/base.rb', line 9 def dates @dates end |
Instance Method Details
#length ⇒ Integer
Length of the time series
78 79 80 |
# File 'lib/time_wise/base.rb', line 78 def length @data.size end |
#moving_average ⇒ Object
Access moving average methods
88 89 90 |
# File 'lib/time_wise/base.rb', line 88 def moving_average TimeWise::MovingAverage.new(self) end |
#plot ⇒ Object
Access visualization methods
93 94 95 |
# File 'lib/time_wise/base.rb', line 93 def plot TimeWise::Visualization.new(self) end |
#slice(start_idx, end_idx) ⇒ TimeWise::Base
Get a section of the time series
43 44 45 46 47 48 49 50 |
# File 'lib/time_wise/base.rb', line 43 def slice(start_idx, end_idx) end_idx = @data.size - 1 if end_idx >= @data.size new_data = @data[start_idx..end_idx] new_dates = @has_dates ? @dates[start_idx..end_idx] : nil TimeWise.create(new_data.to_a, new_dates) end |
#slice_by_date(start_date, end_date) ⇒ TimeWise::Base
Get a section of the time series by date range
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/time_wise/base.rb', line 56 def slice_by_date(start_date, end_date) raise TimeWise::Errors::MissingDatesError, "Time series has no date information" unless @has_dates parsed_start = parse_date(start_date) parsed_end = parse_date(end_date) indices = find_indices_in_date_range(parsed_start, parsed_end) new_data = indices.map { |idx| @data[idx] } new_dates = indices.map { |idx| @dates[idx] } TimeWise.create(new_data, new_dates) end |
#stats ⇒ Object
Apply basic statistical methods
83 84 85 |
# File 'lib/time_wise/base.rb', line 83 def stats TimeWise::Statistics.new(self) end |
#to_a ⇒ Array
Convert to array
72 73 74 |
# File 'lib/time_wise/base.rb', line 72 def to_a @data.to_a end |
#to_h ⇒ Hash
Get the time series as a hash with date and value arrays
31 32 33 34 35 36 37 |
# File 'lib/time_wise/base.rb', line 31 def to_h if @has_dates { dates: @dates, values: @data.to_a } else { indices: (0...@data.size).to_a, values: @data.to_a } end end |