module Dataset::ConvertingTimeAndIndices

Converts between time and indices for referencing data lines

Public Instance Methods

idx(time) click to toggle source

Returns the index for a given time @param [time] time @return [Integer] row index

# 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) click to toggle source

Returns the time for a given index @param [Integer] idx row index @return [type of `data`]

# File lib/data_modeler/dataset/helper.rb, line 17
def time idx
  data[:time][idx]
end