Class: CTioga2::Data::DataPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/data/point.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, index) ⇒ DataPoint

Creates a DataPoint with the given information.



33
34
35
36
# File 'lib/ctioga2/data/point.rb', line 33

def initialize(dataset,index)
  @dataset = dataset
  @index = index
end

Instance Attribute Details

#datasetObject

The Dataset object the point is in



27
28
29
# File 'lib/ctioga2/data/point.rb', line 27

def dataset
  @dataset
end

#indexObject

The index of the data point within the Dataset



30
31
32
# File 'lib/ctioga2/data/point.rb', line 30

def index
  @index
end

Class Method Details

.from_text(plotmaker, text, dataset = nil) ⇒ Object

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

Specification: (<em>dataset</em>)?(relative|@index)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Method Details

#dx(navg = 3) ⇒ Object

Returns the average value of the difference between two consecutive X values



140
141
142
143
144
145
146
147
# 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

#pointObject



73
74
75
# File 'lib/ctioga2/data/point.rb', line 73

def point
  return [self.x, self.y]
end

#slope(navg = 3) ⇒ Object

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

#xObject



65
66
67
# File 'lib/ctioga2/data/point.rb', line 65

def x
  return @dataset.x.values[@index]
end

#x_val(navg = 3) ⇒ Object

Returns the averaged X value around the datapoint



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

#yObject



69
70
71
# File 'lib/ctioga2/data/point.rb', line 69

def y
  return @dataset.y.values[@index]
end

#y_val(navg = 3) ⇒ Object

Returns the averaged Y value around the datapoint



95
96
97
98
99
100
101
102
103
104
105
106
107
# 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