class SimpleMetric::DataSet

Attributes

data_points[R]

Public Class Methods

new(data_points) click to toggle source
# File lib/simple_metric.rb, line 63
def initialize(data_points)
  @data_points = data_points.sort_by { |point| point.x }
end

Public Instance Methods

get_value(x) click to toggle source
# File lib/simple_metric.rb, line 67
def get_value(x)
  x = x.to_f
  value = @data_points.find { |point| point.x == x }.try(:y)

  if value.blank?
    points = [points_before(x, 2), points_after(x, 2)].flatten

    spliner = Spliner::Spliner.new(points.map(&:x), points.map(&:y))
    value = spliner[x]
  end

  value
end

Private Instance Methods

points_after(x, count) click to toggle source
# File lib/simple_metric.rb, line 95
def points_after(x, count)
  points = []

  @data_points.each do |point|
    break if points.size >= count

    points << point if point.x >= x
  end

  points.sort_by{ |p| p.x }
end
points_before(x, count) click to toggle source
# File lib/simple_metric.rb, line 83
def points_before(x, count)
  points = []

  @data_points.reverse.each do |point|
    break if points.size >= count

    points << point if point.x <= x
  end

  points.sort_by{ |p| p.x }
end