Class: DataInput::DataRepository

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

Overview

This class serves as a data repository storing the read data and handling the meta information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil, key = nil) ⇒ DataRepository

initialization

Parameters:

  • filename (String) (defaults to: nil)

    filepath

  • key (VisMetaData) (defaults to: nil)

    key for the data series



17
18
19
20
21
22
23
24
# File 'lib/data/data_repository.rb', line 17

def initialize(filename = nil,key = nil)
  @repository = Hash.new()
  add_data_with_default_meta(filename) if (key == nil && filename != nil)
  if (filename != nil && key != nil)
    data = read_file(filename)
    @repository[key] = create_dataseries(data)
  end
end

Instance Attribute Details

#repositoryHash (readonly)

Returns:



12
13
14
# File 'lib/data/data_repository.rb', line 12

def repository
  @repository
end

Instance Method Details

#add_data(filename) ⇒ VisMetaData

reads the file and creates meta information and data of its content

Parameters:

  • filename (String)

    filepath

Returns:

  • (VisMetaData)

    the meta data object for this data



29
30
31
32
33
34
35
# File 'lib/data/data_repository.rb', line 29

def add_data(filename)
  data = read_file(filename)
   = (data)
  check_for_existenz()
  @repository[] = create_dataseries(data)
  return 
end

#add_data_with_default_meta(filename) ⇒ VisMetaData

reads the file and creates data of its content with default meta information

Parameters:

  • filename (String)

    filepath

Returns:

  • (VisMetaData)

    the meta data object for this data



41
42
43
44
45
46
47
48
49
# File 'lib/data/data_repository.rb', line 41

def add_data_with_default_meta(filename)
  data = read_file(filename)
  data_series = create_dataseries(data)
  meta_string = build_meta_string(data_series, filename)

   = MetaData::VisMetaData.new(meta_string)
  @repository[] = data_series
  return 
end

#build_meta_string(data_series, filename) ⇒ Array (private)

method to build the meta string that is uses for default

{MetaData::VisMetaData}

Parameters:

  • data_series (DataSeries)

    the given data series

  • filename (String)

    the name of the input file

Returns:

  • (Array)

    the constructed meta string



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/data/data_repository.rb', line 167

def build_meta_string(data_series, filename)
  meta_string = ["#{filename}", \
                 'X', 0, data_series.series[0].data[0].size - 1, 1, \
                 'Y', 0, data_series.series[0].data.size - 1, 1]

  if (data_series.series.size > 1)
    meta_string.concat( ['Z', 0, data_series.series.size, 1] )
  else
    meta_string.concat( ['Z', 0, 1, 1] )
  end

  return meta_string
end

#check_for_existenz(key) ⇒ Object (private)

checks if a given key already exists in the repository

Parameters:

  • key (VisMetaData)

    key that should be checked



201
202
203
204
205
# File 'lib/data/data_repository.rb', line 201

def check_for_existenz(key)
  if (@repository[key] != nil)
      puts 'Info: A data set with this key already exists. Overwriting...'
  end
end

#check_for_metadata(data) ⇒ VisMetaData (private)

checks for meta data in the first line of the raw data and creates meta information from it

Parameters:

  • data (Array)

    the read data

Returns:

  • (VisMetaData)

    the meta data for the data series



192
193
194
195
196
197
# File 'lib/data/data_repository.rb', line 192

def (data)
  meta_string = data[0]
  data.delete_at(1)
  data.delete_at(0)
  MetaData::VisMetaData.new(meta_string)
end

#create_dataseries(data) ⇒ DataSeries (private)

creates DataInput::DataSets of the parsed data and stores it into a DataInput::DataSeries

Parameters:

  • data (Array)

    the read data

Returns:

  • (DataSeries)

    the data series created by the data input



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

def create_dataseries(data)
  raw_data = Array.new()
  value = DataSeries.new()

  # parse multiple data sets (but at least 1)
  data.each { |line|
    if (line.empty?)
      value.add_data_set(DataSet.new(raw_data))
      raw_data = Array.new()
    else
      raw_data << line
    end
  }

  # get the last data set, since the loop ended before putting it there
  value.add_data_set(DataSet.new(raw_data))
  return value
end

#data_complete?(meta_data) ⇒ boolean

checks if all data sets in a data_series have the dimension specified in the MetaData::VisMetaData information

Parameters:

  • meta_data (MetaData)

    the meta data which should be checked

Returns:

  • (boolean)

    true, if data fulfills the information provded by the meta data; false, if one data dimension of the number of datasets fails



56
57
58
# File 'lib/data/data_repository.rb', line 56

def data_complete?()
  dataset_dimension_correct?() || z_dimension_correct?()
end

#dataset_dimension_correct?(meta_data) ⇒ boolean

checks if the dimension of each dataset is consistent with the information of the corresponding MetaData::VisMetaData

Parameters:

  • meta_data (VisMetaData)

    the meta data which should be checked

Returns:

  • (boolean)

    true, if the dimension of every dataset is consistent with the meta information; false, if not



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/data/data_repository.rb', line 65

def dataset_dimension_correct?()
  domain_values = get_domain_values()

  @repository[].series.each_with_index { |data_set, index|
    data_values = get_data_values(data_set)

    if (domain_values[:x] != data_values[:x] ||
        domain_values[:y] != data_values[:y])
      print_domain_mismatch(index, domain_values, data_values)
      return false
    end
  }

  return true
end

#get_data_values(data_set) ⇒ Hash (private)

method to retireve the number of data values in the x and y dimension based on the size of the DataInput::DataSet

Parameters:

  • data_set (DataSet)

    the dataset that should be checked

Returns:

  • (Hash)

    a hash containing the number of data values in x and y



98
99
100
101
102
103
# File 'lib/data/data_repository.rb', line 98

def get_data_values(data_set)
  data_values = Hash.new()
  data_values[:x] = data_set.data[0].size
  data_values[:y] = data_set.data.size
  return data_values
end

#get_domain_values(meta_data) ⇒ Hash (private)

method to retrieve the number of data values in the x and y dimension based on the MetaData::VisMetaData information

Parameters:

  • meta_data (VisMetaData)

    the meta data whose values should be used

Returns:

  • (Hash)

    a hash containing the number of data values in x and y



87
88
89
90
91
92
# File 'lib/data/data_repository.rb', line 87

def get_domain_values()
  domain_values = Hash.new()
  domain_values[:x] = .domain_x.number_of_values
  domain_values[:y] = .domain_y.number_of_values
  return domain_values
end

method to print a warning if the dimension of the meta data does not fit with the dimension of the actual dataset

Parameters:

  • index (Integer)

    the index of the dataset in the given data series

  • domain_values (Hash)

    a hash containing the number of data values in x and y based on the meta data

  • data_values (Hash)

    a hash containing the number of data values in x and y based on the dataset



112
113
114
115
116
117
# File 'lib/data/data_repository.rb', line 112

def print_domain_mismatch(index, domain_values, data_values)
  puts " Warning: Size of dataset #{index + 1} does not match " \
       "with meta data information.".yellow
  puts "   meta_data: #{domain_values[:x]}, #{domain_values[:y]}".yellow
  puts "   data_set: #{data_values[:x]}, #{data_values[:y]}".yellow
end

#read_file(filename) ⇒ Array (private)

calls the FileReader to get the content of the file

Parameters:

  • filename (String)

    filepath

Returns:

  • (Array)

    the data of the file as strings



184
185
186
# File 'lib/data/data_repository.rb', line 184

def read_file(filename)
  RubyUtils::FileReader.new(filename, ',').data
end

#z_dimension_correct?(meta_data) ⇒ boolean (private)

checks if all data sets in a data_series have the dimension in z specified in the MetaData::VisMetaData information

Parameters:

  • meta_data (VisMetaData)

    the meta data which should be checked

Returns:

  • (boolean)

    true, if number of dataset is consistent with the meta information; false, if not



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/data/data_repository.rb', line 124

def z_dimension_correct?()
  data_series = @repository[]
  number_value_z = .domain_z.number_of_values
  number_data_z = data_series.series.size

  if (number_value_z != number_data_z)
    puts ' Warning: Size of dataseries does not match with' \
       ' meta data information.'.yellow
    puts "   meta_data: #{number_value_z} datasets to data_series: " \
       "#{number_data_z} datasets".yellow
    return false
  end

  return true
end