Class: DataInput::DataSet

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

Overview

Class to represent a two dimensional data set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data) ⇒ DataSet

initialization

Parameters:

  • raw_data (Array)

    the unformatted lines with data



19
20
21
22
# File 'lib/data/data_set.rb', line 19

def initialize(raw_data)
  @data = Hash.new()
  process_data(raw_data)
end

Instance Attribute Details

#dataHash (readonly)

Returns the data stored in a Hash (Integer => Array).

Returns:

  • (Hash)

    the data stored in a Hash (Integer => Array)



15
16
17
# File 'lib/data/data_set.rb', line 15

def data
  @data
end

#max_valueFloat (readonly)

Returns the maximal value of the dataset.

Returns:

  • (Float)

    the maximal value of the dataset



13
14
15
# File 'lib/data/data_set.rb', line 13

def max_value
  @max_value
end

#min_valueFloat (readonly)

Returns the minimal value of the dataset.

Returns:

  • (Float)

    the minimal value of the dataset



11
12
13
# File 'lib/data/data_set.rb', line 11

def min_value
  @min_value
end

Instance Method Details

#find_extreme_valuesObject (private)

searches for the minimal and maximal value of the dataset



46
47
48
49
50
51
52
53
54
55
# File 'lib/data/data_set.rb', line 46

def find_extreme_values
  @min_value = @data[0][1].to_f
  @max_value = @data[0][1].to_f
  data.each_value { |row|
    row.each { |value|
      @min_value = value if (value < @min_value)
      @max_value = value if (value > @max_value)
    }
  }
end

#process_data(raw_data) ⇒ Object (private)

processes the raw data and parses it in number values

Parameters:

  • raw_data (Array)

    the unformatted lines with data



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/data/data_set.rb', line 28

def process_data(raw_data)
  row = 0
  begin
    raw_data.each { |line|
      @data[row] = line.map { |s|
        Float(s)
        s.to_f }
      row += 1
    }
    find_extreme_values
  rescue StandardError => e
    raise ArgumentError,
          " Error in data set while parsing data:\n "
          .concat(e.message).red
  end
end