Class: Timeline

Inherits:
Object
  • Object
show all
Defined in:
lib/math/time_line.rb

Overview

This class collects all data values of the z dimension of a DataSeries for a given pair of coordinates (x,y). For a given resolution size the values are assigned to the nearest value of value_boundaries to be drawn in a terminal or window.

Direct Known Subclasses

TimelineScaling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta_data, data_series, parameters) ⇒ Timeline

initialization

Parameters:

  • meta_data (VisMetaData)

    the meta information of the regarded data series

  • data_series (DataSeries)

    the data series which should be used

  • parameters (Hash)

    a hash containing the required parameter



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/math/time_line.rb', line 21

def initialize(, data_series, parameters)
  check_and_set_ysize(parameters[:y_size])
  check_dataset_dimension()

  values = collect_values(, data_series,
                          parameters[:x], parameters[:y])

  determine_extrema(values) # extrema of time values

  determine_value_boundaries # ordinate values for each line

  create_output(determine_nearest_index(values))
end

Instance Attribute Details

#extremaHash (readonly, private)

Returns the extreme values of the timeline.

Returns:

  • (Hash)

    the extreme values of the timeline



39
40
41
# File 'lib/math/time_line.rb', line 39

def extrema
  @extrema
end

#linesInteger (readonly, private)

Returns resolution of the value scale.

Returns:

  • (Integer)

    resolution of the value scale



41
42
43
# File 'lib/math/time_line.rb', line 41

def lines
  @lines
end

#mapped_valuesHash (readonly)

Returns the occurence of a boundary value in the z dimension as the result of being the nearest index for a collected value at position z.

Returns:

  • (Hash)

    the occurence of a boundary value in the z dimension as the result of being the nearest index for a collected value at position z



15
16
17
# File 'lib/math/time_line.rb', line 15

def mapped_values
  @mapped_values
end

#value_bondariesArray (readonly, private)

Returns the values for each output line.

Returns:

  • (Array)

    the values for each output line



37
38
39
# File 'lib/math/time_line.rb', line 37

def value_bondaries
  @value_bondaries
end

Instance Method Details

#check_and_set_ysize(y_size) ⇒ Object (private)

method to check and set the number of values for the y-dimension constraint: at least 5 values in y

Parameters:

  • y_size (Fixnum)

    number of values in y

Raises:

  • (RangeError)

    if the number of y values is less than 5



47
48
49
50
51
52
# File 'lib/math/time_line.rb', line 47

def check_and_set_ysize(y_size)
  if (y_size < 5)
    raise RangeError, ' Error : invalid y_value of timeline (min.: 5)'.red
  end
  @lines = y_size
end

#check_dataset_dimension(meta_data) ⇒ Object (private)

method to check for correct dataset dimensions

Parameters:

  • meta_data (VisMetaData)

    the required meta data

Raises:

  • (RangeError)

    if one of the datasets has an incorrect dimension



57
58
59
60
61
62
# File 'lib/math/time_line.rb', line 57

def check_dataset_dimension()
  if (!TerminalVis.data_repo.dataset_dimension_correct?())
    raise RangeError,
          ' Error: dimension of at least one dataset is incorrect'.red
  end
end

#check_extrema(value) ⇒ Array (private)

method to determine if the value is an extreme value and return the value if it is an extreme value

Parameters:

  • value (Float)

    the considered value

Returns:

  • (Array)

    an array containing Symbol and the value if it is an extreme value



167
168
169
170
171
# File 'lib/math/time_line.rb', line 167

def check_extrema(value)
  return [:minimum, value] if (value == @extrema[:minimum])
  return [:maximum, value] if (value == @extrema[:maximum])
  return [:hit]
end

#collect_values(meta_data, data_series, x, y) ⇒ Array (private)

this method collects all data values d(x,y) in z

Parameters:

  • meta_data (VisMetaData)

    the meta information of the regarded data series

  • data_series (DataSeries)

    the data series which should be used

  • x (Float)

    x-coordinate of the regarded point

  • y (Float)

    y-coordinate of the regarded point

Returns:

  • (Array)

    the collected values d(x,y)



70
71
72
73
74
75
76
77
78
79
# File 'lib/math/time_line.rb', line 70

def collect_values(, data_series, x, y)
  values = Array.new()

  data_series.series.each { |data_set|
    values << TerminalVis::Interpolation::BilinearInterpolation.
                             bilinear_interpolation(,data_set, x, y)
  }

  return values
end

#create_output(mapped_values) ⇒ Object (private)

this method creates the basic output which can be visualized.

Parameters:

  • mapped_values (Hash)

    mapping of (value => index)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/math/time_line.rb', line 144

def create_output(mapped_values)
  output = Hash.new(@lines)

  @value_bondaries.each_index { |index|
    line = Array.new()
    mapped_values.values.each { |value|
      if (value[0] == index)
        line << value[1]
      else
        line << [:miss]
      end
    }
    output[@value_bondaries[index]] = line
  }

  @mapped_values = output
end

#determine_extrema(values) ⇒ Object (private)

this method determines the extreme values of the collected data d(x,y)

Parameters:

  • values (Array)

    the collected values d(x,y)



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/math/time_line.rb', line 83

def determine_extrema(values)
  @extrema = {
    :maximum => values[0],
    :minimum => values[0]
  }

  values.each { |value|
    @extrema[:maximum] = value if (value > @extrema[:maximum])
    @extrema[:minimum] = value if (value < @extrema[:minimum])
  }
end

#determine_nearest_index(values) ⇒ Hash (private)

this method calculates for every collected value the index of the interval values @value_boundaries with the least distance

Parameters:

  • values (Array)

    the collected values d(x,y)

Returns:

  • (Hash)

    mapping of (value => index)



114
115
116
117
118
119
120
121
122
# File 'lib/math/time_line.rb', line 114

def determine_nearest_index(values)
  mapped_values = Hash.new()

  values.each { |value|
    mapped_values[value] = [get_nearest_index(value), check_extrema(value)]
  }

  return mapped_values
end

#determine_value_boundariesObject (private)

this method determines the value boundaries based on the vertical number of lines



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/math/time_line.rb', line 97

def determine_value_boundaries
  delta = (@extrema[:maximum] - @extrema[:minimum]).abs
  upper_boundary = @extrema[:maximum] + delta / 20.0 # 5 % variance
  lower_boundary = @extrema[:minimum] - delta / 20.0 # 5 % variance

  delta = (upper_boundary - lower_boundary).abs / @lines
  @value_bondaries = [lower_boundary.round(5)]
  while (lower_boundary.round(5) < upper_boundary.round(5))
    lower_boundary += delta
    @value_bondaries << lower_boundary.round(5)
  end
end

#get_nearest_index(value) ⇒ Integer (private)

helper methode to calculate the nearest index for the collected data values

Parameters:

  • value (Float)

    data value

Returns:

  • (Integer)

    the nearest index



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/math/time_line.rb', line 127

def get_nearest_index(value)
  delta = value
  nearest_index = 0

  @value_bondaries.each_index { |index|
    tmp_delta = (@value_bondaries[index] - value).abs
    if (tmp_delta < delta)
      delta = tmp_delta
      nearest_index = index
    end
  }

  return nearest_index
end