Class: Rover::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/rover/vector.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Vector

Returns a new instance of Vector.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rover/vector.rb', line 3

def initialize(data)
  @data =
    if data.is_a?(Vector)
      data.to_numo
    elsif data.is_a?(Numo::NArray)
      data
    else
      data = data.to_a
      if data.all? { |v| v.is_a?(Integer) }
        Numo::Int64.cast(data)
      elsif data.all? { |v| v.is_a?(Numeric) || v.nil? }
        Numo::DFloat.cast(data.map { |v| v || Float::NAN })
      elsif data.all? { |v| v == true || v == false }
        Numo::Bit.cast(data)
      else
        Numo::RObject.cast(data)
      end
    end

  raise ArgumentError, "Bad size: #{@data.shape}" unless @data.ndim == 1
end

Instance Method Details

#!Object



124
125
126
127
128
129
130
# File 'lib/rover/vector.rb', line 124

def !
  if @data.is_a?(Numo::Bit)
    Vector.new(@data.eq(0))
  else
    raise "Not implemented yet"
  end
end

#-@Object



132
133
134
# File 'lib/rover/vector.rb', line 132

def -@
  self * -1
end

#[](v) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rover/vector.rb', line 63

def [](v)
  if v.is_a?(Vector)
    Vector.new(v.to_numo.mask(@data))
  else
    @data[v]
  end
end

#[]=(k, v) ⇒ Object



71
72
73
74
# File 'lib/rover/vector.rb', line 71

def []=(k, v)
  k = k.to_numo if k.is_a?(Vector)
  @data[k] = v
end

#absObject



155
156
157
# File 'lib/rover/vector.rb', line 155

def abs
  Vector.new(@data.abs)
end

#all?(&block) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/rover/vector.rb', line 191

def all?(&block)
  @data.to_a.all?(&block)
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/rover/vector.rb', line 195

def any?(&block)
  @data.to_a.any?(&block)
end

#clamp(min, max) ⇒ Object



141
142
143
# File 'lib/rover/vector.rb', line 141

def clamp(min, max)
  dup.clamp!(min, max)
end

#clamp!(min, max) ⇒ Object



136
137
138
139
# File 'lib/rover/vector.rb', line 136

def clamp!(min, max)
  @data = @data.clip(min, max)
  self
end

#crosstab(other) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rover/vector.rb', line 211

def crosstab(other)
  index = uniq.sort
  index_pos = index.to_a.map.with_index.to_h
  df = DataFrame.new({"_" => index})
  other.uniq.sort.each do |k|
    df[k] = 0
  end
  to_a.zip(other.to_a) do |v1, v2|
    df[v2][index_pos[v1]] += 1
  end
  df
end

#diffObject

keep same number of rows as original to make it easy to add to original data frame



58
59
60
61
# File 'lib/rover/vector.rb', line 58

def diff
  diff = @data.cast_to(Numo::DFloat).diff
  Vector.new(diff.insert(0, Float::NAN))
end

#each(&block) ⇒ Object



159
160
161
# File 'lib/rover/vector.rb', line 159

def each(&block)
  to_a.each(&block)
end

#first(n = 1) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/rover/vector.rb', line 199

def first(n = 1)
  if n >= size
    Vector.new(@data)
  else
    Vector.new(@data[0...n])
  end
end

#head(n = 5) ⇒ Object



224
225
226
227
# File 'lib/rover/vector.rb', line 224

def head(n = 5)
  n += size if n < 0
  first(n)
end

#in?(values) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rover/vector.rb', line 110

def in?(values)
  ret = Numo::Bit.new(size).fill(false)
  values.each do |v|
    comp =
      if v.is_a?(Numeric) || v.is_a?(Numo::NArray)
        @data.eq(v)
      else
        Numo::Bit.cast(@data.map { |d| d == v })
      end
    ret |= comp
  end
  Vector.new(ret)
end

#inspectObject Also known as: to_s

TODO add type and size?



235
236
237
238
239
# File 'lib/rover/vector.rb', line 235

def inspect
  elements = first(5).to_a.map(&:inspect)
  elements << "..." if size > 5
  "#<Rover::Vector [#{elements.join(", ")}]>"
end

#last(n = 1) ⇒ Object



207
208
209
# File 'lib/rover/vector.rb', line 207

def last(n = 1)
  Vector.new(@data[-n..-1])
end

#map(&block) ⇒ Object



145
146
147
148
149
# File 'lib/rover/vector.rb', line 145

def map(&block)
  mapped = @data.map(&block)
  mapped = mapped.to_a if mapped.is_a?(Numo::RObject) # re-evaluate cast
  Vector.new(mapped)
end

#maxObject



163
164
165
# File 'lib/rover/vector.rb', line 163

def max
  @data.max
end

#meanObject



171
172
173
174
175
# File 'lib/rover/vector.rb', line 171

def mean
  # currently only floats have mean in Numo
  # https://github.com/ruby-numo/numo-narray/issues/79
  @data.cast_to(Numo::DFloat).mean
end

#medianObject



177
178
179
180
181
# File 'lib/rover/vector.rb', line 177

def median
  # need to cast to get correct result
  # TODO file bug with Numo
  @data.cast_to(Numo::DFloat).median
end

#minObject



167
168
169
# File 'lib/rover/vector.rb', line 167

def min
  @data.min
end

#missingObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rover/vector.rb', line 43

def missing
  bit =
    if @data.is_a?(Numo::RObject)
      Numo::Bit.cast(@data.map(&:nil?))
    elsif @data.respond_to?(:isnan)
      @data.isnan
    else
      Numo::Bit.new(size).fill(0)
    end

  Vector.new(bit)
end

#percentile(q) ⇒ Object



183
184
185
# File 'lib/rover/vector.rb', line 183

def percentile(q)
  @data.percentile(q)
end

#sizeObject



35
36
37
# File 'lib/rover/vector.rb', line 35

def size
  @data.size
end

#sortObject



151
152
153
# File 'lib/rover/vector.rb', line 151

def sort
  Vector.new(@data.respond_to?(:sort) ? @data.sort : @data.to_a.sort)
end

#sumObject



187
188
189
# File 'lib/rover/vector.rb', line 187

def sum
  @data.sum
end

#tail(n = 5) ⇒ Object



229
230
231
232
# File 'lib/rover/vector.rb', line 229

def tail(n = 5)
  n += size if n < 0
  last(n)
end

#to_aObject



29
30
31
32
33
# File 'lib/rover/vector.rb', line 29

def to_a
  a = @data.to_a
  a.map! { |v| !v.zero? } if @data.is_a?(Numo::Bit)
  a
end

#to_htmlObject

for IRuby



243
244
245
246
# File 'lib/rover/vector.rb', line 243

def to_html
  require "iruby"
  IRuby::HTML.table(to_a)
end

#to_numoObject



25
26
27
# File 'lib/rover/vector.rb', line 25

def to_numo
  @data
end

#uniqObject



39
40
41
# File 'lib/rover/vector.rb', line 39

def uniq
  Vector.new(@data.to_a.uniq)
end