class Mikon::Series

Attributes

index[R]
name[R]

Public Class Methods

new(name, source, options={}) click to toggle source
# File lib/mikon/core/series.rb, line 11
def initialize(name, source, options={})
  options = {
    index: nil
  }.merge(options)

  case
  when source.is_a?(Array) || source.is_a?(NMatrix)
    @data = Mikon::DArray.new(source)
  when source.is_a?(Mikon::DArray)
    @data = source
  else
    raise "Non-acceptable Arguments Error"
  end

  @index = options[:index]
  @name = name

  _check_if_valid
end

Public Instance Methods

%(arg) click to toggle source
# File lib/mikon/core/series.rb, line 107
def %(arg)
  if arg.is_a?(Numeric)
    Series.new(self.name, @data%arg, index: self.index)
  else
    raise ArgumentError
  end
end
*(arg) click to toggle source
# File lib/mikon/core/series.rb, line 91
def *(arg)
  if arg.is_a?(Numeric)
    Series.new(self.name, @data*arg, index: self.index)
  else
    raise ArgumentError
  end
end
+(arg) click to toggle source
# File lib/mikon/core/series.rb, line 123
def +(arg)
  if arg.is_a?(Mikon::Series) && arg.length == self.length
    Series.new(self.name, arg.coerce(@data).inject(:+), index: self.index)
  else
    raise ArgumentError
  end
end
-(arg) click to toggle source
# File lib/mikon/core/series.rb, line 115
def -(arg)
  if arg.is_a?(Mikon::Series) && arg.length == self.length
    Series.new(self.name, arg.coerce(@data).inject(:-), index: self.index)
  else
    raise ArgumentError
  end
end
/(arg) click to toggle source
# File lib/mikon/core/series.rb, line 99
def /(arg)
  if arg.is_a?(Numeric)
    Series.new(self.name, @data/arg, index: self.index)
  else
    raise ArgumentError
  end
end
[](arg) click to toggle source
# File lib/mikon/core/series.rb, line 44
def [](arg)
  pos = @index.index(arg)
  raise "There is no index named" + arg.to_s if pos.nil?
  @data[pos]
end
coerce(other) click to toggle source
# File lib/mikon/core/series.rb, line 131
def coerce(other)
  if other.is_a?(Mikon::DArray)
    return other, @data
  elsif other.is_a?(Numeric)
    return self, other
  else
    raise ArgumentError
  end
end
each(&block) click to toggle source
# File lib/mikon/core/series.rb, line 40
def each(&block)
  @data.each(&block)
end
length() click to toggle source
# File lib/mikon/core/series.rb, line 36
def length
  @data.length
end
plot(args={}) click to toggle source
# File lib/mikon/plot.rb, line 5
def plot(args={})
  args = {
    :type => :histogram
  }.merge(args)

  plot = Nyaplot::Plot.new

  case args[:type]
  when :histogram
    plot.add(:histogram, @data.to_a)
  when :line
    plot.add(:line, @index, @data.to_a)
  end

  plot
end
to_a() click to toggle source
# File lib/mikon/core/series.rb, line 83
def to_a
  @data.to_a
end
to_darr() click to toggle source
# File lib/mikon/core/series.rb, line 87
def to_darr
  @data
end
to_html(threshold=5) click to toggle source
# File lib/mikon/core/series.rb, line 50
def to_html(threshold=5)
  html = "<table><tr><th></th><th>" + self.name.to_s + "</th></tr>"
  @index.each.with_index do |index, pos|
    next if pos > threshold && pos != self.length-1
    html += "<tr><th>" + index.to_s + "</th><td>" + @data[pos].to_s + "</td></tr>"
    html += "<tr><th>...</th><td>...</td></tr>" if pos == threshold
  end
  html + "</table>"
end
to_json(*args) click to toggle source
# File lib/mikon/core/series.rb, line 70
def to_json(*args)
  @data.to_json
end
to_s(threshold=5) click to toggle source
# File lib/mikon/core/series.rb, line 60
def to_s(threshold=5)
  arr = []
  @index.each.with_index do |index, pos|
    next nil if pos > threshold && pos != self.length-1
    arr.push({"" => index, @name => @data[pos]})
    arr.push({"" => "...", @name => "..."}) if pos == threshold
  end
  Formatador.display_table(arr.select{|el| !(el.nil?)})
end

Private Instance Methods

_check_if_valid() click to toggle source
# File lib/mikon/core/series.rb, line 31
def _check_if_valid
  @index = (0..(length-1)).to_a if @index.nil?
  raise "index should have the same length as arrays" if @index.length != @data.length
end