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.



36
37
38
39
# File 'lib/ctioga2/data/point.rb', line 36

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

Instance Attribute Details

#datasetObject

The Dataset object the point is in



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

def dataset
  @dataset
end

#indexObject

The index of the data point within the Dataset



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

def index
  @index
end

Class Method Details

.from_text(plotmaker, text) ⇒ 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)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ctioga2/data/point.rb', line 46

def self.from_text(plotmaker, text)
  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).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



135
136
137
138
139
140
141
142
# File 'lib/ctioga2/data/point.rb', line 135

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

#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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ctioga2/data/point.rb', line 107

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_val(navg = 3) ⇒ Object

Returns the averaged X value around the datapoint



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ctioga2/data/point.rb', line 73

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_val(navg = 3) ⇒ Object

Returns the averaged Y value around the datapoint



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ctioga2/data/point.rb', line 90

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