Module: DataModeler::Dataset::ConvertingTimeAndIndices

Included in:
Accessor, Generator
Defined in:
lib/data_modeler/dataset/helper.rb

Overview

Converts between time and indices for referencing data lines

Instance Method Summary collapse

Instance Method Details

#idx(time) ⇒ Integer

Returns the index for a given time

Parameters:

Returns:

  • (Integer)

    row index

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/data_modeler/dataset/helper.rb', line 24

def idx time
  # TODO: optimize with `from:`
  # TODO: test corner case when index not found
  # find index of first above time
  idx = data[:time].index { |t| t > time }
  # if index not found: all data is below time, "first above" is outofbound
  idx ||= nrows
  # if first above time is 0: there is no element with that time
  raise TimeNotFoundError, "Time not found: #{time}" if idx.zero?
  # return index of predecessor (last below time)
  idx-1
end

#time(idx) ⇒ type of `data[:time]`

Returns the time for a given index

Parameters:

  • idx (Integer)

    row index

Returns:

  • (type of `data[:time]`)


17
18
19
# File 'lib/data_modeler/dataset/helper.rb', line 17

def time idx
  data[:time][idx]
end