class CTioga2::Data::DataPoint

This class represents a datapoint, ie. an index in a given DataSet.

Attributes

dataset[RW]

The Dataset object the point is in

index[RW]

The index of the data point within the Dataset

Public Class Methods

from_text(plotmaker, text, dataset = nil) click to toggle source

Creates a DataPoint object based on the following text specification. It needs a reference to a plotmaker, since it accesses the data stack.

Specification: ({dataset})?(relative|@index)

# File lib/ctioga2/data/point.rb, line 43
def self.from_text(plotmaker, text, dataset = nil)
  if text =~ /^(?:\s*\{([^}]+)\})?\s*(?:([.\d]+)|@(\d+))\s*$/
    which = $1 || -1
    if $2
      rel = Float($2)
    else
      idx = $3.to_i
    end
    dataset ||= plotmaker.data_stack.stored_dataset(which)
    
    if ! dataset
      raise "Invalid or empty dataset: #{which}"
    end
    if rel
      idx = (rel * (dataset.x.values.size - 1)).to_i
    end
    return DataPoint.new(dataset, idx)
  else
    raise "Not a valid datapoint specification: '#{text}'"
  end
end
new(dataset,index) click to toggle source

Creates a DataPoint with the given information.

# File lib/ctioga2/data/point.rb, line 33
def initialize(dataset,index)
  @dataset = dataset
  @index = index
end

Public Instance Methods

dx(navg = 3) click to toggle source
Returns the average value of the difference between two

consecutive X values

# File lib/ctioga2/data/point.rb, line 140
def dx(navg = 3)
  xvect = @dataset.x.values
  di = (navg-1)/2
  navg = 2*di + 1

  idx = usable_index(di, xvect.size)
  return (xvect[idx+di]-xvect[idx-di])/navg
end
point() click to toggle source
# File lib/ctioga2/data/point.rb, line 73
def point
  return [self.x, self.y]
end
slope(navg = 3) click to toggle source

Returns the value of the slope around the datapoint. This is obtained using a linear regression, so it should be rather reliable.

# File lib/ctioga2/data/point.rb, line 112
def slope(navg = 3)
  xvect = @dataset.x.values
  yvect = @dataset.y.values
  di = (navg-1)/2
  navg = 2*di + 1

  idx = usable_index(di, xvect.size)

  sx = 0
  sxx = 0
  sxy = 0
  sy = 0

  (idx-di).upto(idx+di) do |i|
    sx += xvect[i]
    sy += yvect[i]
    sxx += xvect[i]**2
    sxy += xvect[i] * yvect[i]
  end
  if sxx*navg == sx*sx
    return 1
  else
    return (sxy * navg - sx*sy)/(sxx * navg - sx*sx)
  end
end
x() click to toggle source
# File lib/ctioga2/data/point.rb, line 65
def x
  return @dataset.x.values[@index]
end
x_val(navg = 3) click to toggle source

Returns the averaged X value around the datapoint

# File lib/ctioga2/data/point.rb, line 78
def x_val(navg = 3)

  xvect = @dataset.x.values
  di = (navg-1)/2
  navg = 2*di + 1

  idx = usable_index(di, xvect.size)

  xval = 0
  (idx-di).upto(idx+di) do |i|
    xval += xvect[i]
  end
  return xval/(navg)
end
y() click to toggle source
# File lib/ctioga2/data/point.rb, line 69
def y
  return @dataset.y.values[@index]
end
y_val(navg = 3) click to toggle source

Returns the averaged Y value around the datapoint

# File lib/ctioga2/data/point.rb, line 95
def y_val(navg = 3)
  yvect = @dataset.y.values
  di = (navg-1)/2
  navg = 2*di + 1

  idx = usable_index(di, yvect.size)

  yval = 0
  (idx-di).upto(idx+di) do |i|
    yval += yvect[i]
  end
  return yval/(navg)
end

Protected Instance Methods

usable_index(di, size) click to toggle source

Makes sure the boudaries for averaging are fine

# File lib/ctioga2/data/point.rb, line 152
def usable_index(di, size)
  # Boundary checks
  if @index - di < 0
    return di
  elsif @index + di >= size
    return size - 1 - di
  end
  return @index
end