Class: Pest::DataSet::NArray

Inherits:
NMatrix
  • Object
show all
Includes:
Pest::DataSet
Defined in:
lib/pest/data_set/narray.rb

Defined Under Namespace

Classes: VectorEnumerable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pest::DataSet

#destroy, included, #variable_array

Instance Attribute Details

#variablesObject

Returns the value of attribute variables.



50
51
52
# File 'lib/pest/data_set/narray.rb', line 50

def variables
  @variables
end

Class Method Details

.from_csv(file, args = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pest/data_set/narray.rb', line 38

def self.from_csv(file, args={})
  args = args.merge({:converters => :all})
  data = CSV.read(file, args)
  data_set = to_na(data[1..-1]).transpose
  data_set.variables = {}
  data[0].each do |key|
    variable = key.kind_of?(Pest::Variable) ? key : Pest::Variable.new(:name => key)
    data_set.variables[variable.name] = variable
  end
  data_set
end

.from_file(file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pest/data_set/narray.rb', line 25

def self.from_file(file)
  file = File.open(file.to_s, 'r') if file.kind_of?(String)

  begin
    variables, matrix = Marshal.restore(file)
    data_set = to_na(matrix)
    data_set.variables = variables
    data_set
  rescue
    raise "File does not seem to contain valid data"
  end
end

.from_hash(hash) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/pest/data_set/narray.rb', line 15

def self.from_hash(hash)
  data_set = to_na(hash.keys.sort.map {|key| hash[key]}) # Ensure the matrix is sorted the same as the variables
  data_set.variables = {}
  hash.keys.each do |key|
    variable = key.kind_of?(Pest::Variable) ? key : Pest::Variable.new(:name => key)
    data_set.variables[variable.name] = variable
  end
  data_set
end

.translatorsObject



6
7
8
9
10
11
12
13
# File 'lib/pest/data_set/narray.rb', line 6

def self.translators
  {
    Hash    => :from_hash,
    File    => :from_file,
    String  => :from_file,
    Symbol  => :from_file
  }
end

Instance Method Details

#data_vectors(variables = nil) ⇒ Object

variables: an array of variables for which each vector should contain values Order is retained in the returned value



62
63
64
# File 'lib/pest/data_set/narray.rb', line 62

def data_vectors(variables=nil)
  VectorEnumerable.new(self, variables)
end

#lengthObject



66
67
68
# File 'lib/pest/data_set/narray.rb', line 66

def length
  shape[0]
end

#save(file = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/pest/data_set/narray.rb', line 70

def save(file=nil)
  file ||= Tempfile.new('pest_hash_dataset')
  file = File.open(file, 'w') if file.kind_of?(String)
  Marshal.dump([variables,to_a], file)
  file.close
end

#to_hashObject



52
53
54
55
56
57
58
# File 'lib/pest/data_set/narray.rb', line 52

def to_hash
  hash = {}
  variables.values.each_index do |i|
    hash[variables.values[i]] = self[true,i].to_a[0]
  end
  hash
end