Module: DataModeler::ConvertingTimeAndIndices

Included in:
Dataset, DatasetGen
Defined in:
lib/data_modeler/dataset/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

Raises:

  • (TimeNotFoundError)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/data_modeler/dataset/dataset_helper.rb', line 14

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) ⇒ kind_of_time

Returns the time for a given index



7
8
9
# File 'lib/data_modeler/dataset/dataset_helper.rb', line 7

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