Class: Pest::DataSet::NArray
- Inherits:
-
Object
- Object
- Pest::DataSet::NArray
show all
- Includes:
- Pest::DataSet
- Defined in:
- lib/pest/data_set/narray.rb
Instance Attribute Summary collapse
#data, #variables
Class Method Summary
collapse
Instance Method Summary
collapse
#==, #destroy, #except, included, #save
Constructor Details
#initialize(*args) ⇒ NArray
Returns a new instance of NArray.
43
44
45
46
|
# File 'lib/pest/data_set/narray.rb', line 43
def initialize(*args)
super *args
@variable_array = variables.to_a.sort
end
|
Instance Attribute Details
#variable_array ⇒ Object
Returns the value of attribute variable_array.
41
42
43
|
# File 'lib/pest/data_set/narray.rb', line 41
def variable_array
@variable_array
end
|
Class Method Details
.from_csv(file, args = {}) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/pest/data_set/narray.rb', line 28
def self.from_csv(file, args={})
args = {:col_sep => "\t", :headers => true, :converters => :all}.merge args
csv_data = CSV.read(file, args).map(&:to_hash)
data_set = new(
csv_data.first.keys.to_set,
NMatrix.to_na(csv_data.map(&:values)).transpose
)
data_set.instance_variable_set(:@variable_array, csv_data.first.keys)
data_set
end
|
.from_file(file) ⇒ Object
24
25
26
|
# File 'lib/pest/data_set/narray.rb', line 24
def self.from_file(file)
from_csv(file)
end
|
.from_hash(hash) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/pest/data_set/narray.rb', line 15
def self.from_hash(hash)
data_set = new(
hash.keys.to_set,
NMatrix.to_na(hash.values)
)
data_set.instance_variable_set(:@variable_array, hash.keys)
data_set
end
|
.translators ⇒ Object
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
#+(other) ⇒ Object
Return the union of self and other
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/pest/data_set/narray.rb', line 79
def +(other)
unless other.variables == variables
raise ArgumentError, "DataSets have different variables"
end
union = self.class.new
union.variables = variables
union.data = NMatrix[*(data.transpose.to_a + other.data.transpose.to_a)].transpose
union
end
|
#[](*args) ⇒ Object
Return a subset of the data with the same variables, but only the vectors specified by i
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/pest/data_set/narray.rb', line 63
def [](*args)
unless args.any?
raise ArgumentError, "Indices not specified"
end
args.map do |arg|
subset = self.class.new
subset.variables = self.variables
subset.data = self.data[arg,true]
subset
end.inject(:+)
end
|
#dup ⇒ Object
112
113
114
115
116
|
# File 'lib/pest/data_set/narray.rb', line 112
def dup
instance = self.class.new( variables.dup, data.dup)
instance.instance_variable_set(:@variable_array, variable_array)
instance
end
|
#each(&block) ⇒ Object
106
107
108
109
110
|
# File 'lib/pest/data_set/narray.rb', line 106
def each(&block)
(0..length-1).to_a.each do |i|
yield data[i,true].transpose.to_a.first
end
end
|
#length ⇒ Object
56
57
58
|
# File 'lib/pest/data_set/narray.rb', line 56
def length
data.shape[0]
end
|
#merge(other) ⇒ Object
118
119
120
|
# File 'lib/pest/data_set/narray.rb', line 118
def merge(other)
dup.merge!(other)
end
|
#merge!(other) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/pest/data_set/narray.rb', line 122
def merge!(other)
other = self.class.from_hash(other) if other.kind_of?(::Hash)
raise ArgumentError, "Lengths must be the same" if other.length != length
@variable_array += (other.variables - variables).to_a
@variables += other.variables
new_data = ::NArray.object(length, variables.length)
new_data[true, 0..data.shape[1]-1] = data
other.variables.each do |variable|
new_data[true, variable_array.index(variable)] = other.pick(variable).to_a.flatten
end
self.data = new_data
self
end
|
#pick(*args) ⇒ Object
Return a subset of the data with the same vectors, but only the variables specified in args
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/pest/data_set/narray.rb', line 93
def pick(*args)
unless args.any?
raise ArgumentError, "You didn't specify any variables to pick"
end
picked_indices = args.map do |variable|
raise ArgumentError, "Dataset doesn't include #{variable}" unless variables.include?(variable)
self.variable_array.index(variable)
end
self.class.new(args, self.data[true, picked_indices] )
end
|
#to_hash ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/pest/data_set/narray.rb', line 48
def to_hash
hash = {}
variable_array.each_index do |i|
hash[variable_array[i]] = data[true,i].to_a[0]
end
hash
end
|