class Nyaplot::DataFrame

Ruby DataFrame for plotting

Constants

DEFAULT_OPTS

Attributes

rows[R]

Public Class Methods

from_csv(*args) { |csv| ... } click to toggle source
# File lib/nyaplot/data.rb, line 48
def self.from_csv(*args)
  path   = args.shift

  opts      = DEFAULT_OPTS
  if args.size > 0 && args.first.is_a?(Hash)
    opts    = opts.merge(args.shift)
  else
    opts[:col_sep] = args.shift if args.size > 0
    opts[:headers] = args.shift if args.size > 0
  end

  csv  = CSV.open(path, "r", opts)
  yield csv if block_given?

  rows = []
  csv.each do |row|
    hash = {}
    row.each_with_index do |el,i|
      next if el[0].nil? && el[1].nil?
      hash[el[0].to_sym] = el[1]
    end
    rows << hash
  end
  self.new(rows)
end
new(source, name=SecureRandom.uuid()) click to toggle source
# File lib/nyaplot/data.rb, line 19
def initialize(source, name=SecureRandom.uuid())
  @name = name
  @rows = []
  case
  when source.is_a?(Array)
    # like [{a:10, b:10},{a:20,b:20}]
    @rows = source
  when source.is_a?(Hash)
    # like {a:[10,20], b:[10, 20]}
    keys = source.keys
    len = source[keys[0]].length
    (0..len-1).each do |i|
      hash = {}
      keys.each{|key| hash[key] = source[key][i]}
      @rows.push(hash)
    end
  end

  # transform String to Symbol as a key
  unless @rows.all? {|row| row.keys.all? {|el| el.is_a?(Symbol)}}
    @rows.map! do |row|
      row.inject({}) do |hash, (key, val)|
        hash[key.to_sym]=val
        hash
      end
    end
  end
end

Public Instance Methods

[](name) click to toggle source

The alias method for DataFrame#column

# File lib/nyaplot/data.rb, line 165
def [](name)
  return self.column(name)
end
column(name) click to toggle source

Access column using its label

# File lib/nyaplot/data.rb, line 104
def column(name)
  id = name.is_a?(Symbol) ? name : name.to_sym
  column = @rows.map{|row| row[id]}
  return Series.new(name, column)
end
column_labels() click to toggle source
# File lib/nyaplot/data.rb, line 169
def column_labels
  @rows[0].keys
end
delete_column(name) click to toggle source
# File lib/nyaplot/data.rb, line 96
def delete_column(name)
  name = name.is_a?(Symbol) ? name : name.to_sym
  @rows.each do |row|
    row.delete(name)
  end
end
each_column(&block) click to toggle source
# File lib/nyaplot/data.rb, line 125
def each_column(&block)
  self.column_labels.each do |label|
    block.call(column(label).to_a)
  end
end
each_row(&block) click to toggle source
# File lib/nyaplot/data.rb, line 131
def each_row(&block)
  @rows.each do |row|
    block.call(row)
  end
end
filter(&block) click to toggle source

Filtering row out using recieved block @example

new_df = df.filter{|row| row[:a] %2 == 0}
# File lib/nyaplot/data.rb, line 77
def filter(&block)
  DataFrame.new(@rows.select(&block))
end
filter!(&block) click to toggle source

destructive version of DataFrame#filter

# File lib/nyaplot/data.rb, line 82
def filter!(&block)
  @rows.select!(&block)
end
insert_column(name, arr) click to toggle source
# File lib/nyaplot/data.rb, line 91
def insert_column(name, arr)
  name = name.is_a?(Symbol) ? name : name.to_sym
  arr.each_with_index{|val, i| @rows[i][name]=val}
end
insert_row(row, index=@rows.length) click to toggle source

Insert row using index @param [Hash] row row to insert @param [Numeric] index if not specified, the row will be inserted to the end

# File lib/nyaplot/data.rb, line 113
def insert_row(row, index=@rows.length)
  @rows.insert(index, row)
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/nyaplot/data.rb, line 173
def method_missing(name, *args, &block)
  if md = name.match(/(.+)\=/)
    self.insert_column(name[/(.+)\=/].delete("="), args[0])
    return
  elsif column_labels.include?(name)
    return self.column(name)
  else
    super(name, *args, &block)
  end
end
name() click to toggle source

@return [String] the name of dataframe. If not specified when initializing, uuid v4 will be set.

# File lib/nyaplot/data.rb, line 87
def name
  @name
end
row(index) click to toggle source
# File lib/nyaplot/data.rb, line 117
def row(index)
  @rows[index]
end
to_html(threshold = 15) click to toggle source
# File lib/nyaplot/data.rb, line 137
def to_html(threshold = 15)
  html = '<table>'

  unless @rows[0].nil?
    html += '<tr>'
    @rows[0].each {|key, val| html.concat('<th>' + key.to_s + '</th>')}
    html += '</tr>'
  end

  @rows.each_with_index do |row, i|
    next if i > threshold && i < @rows.length-1
    html += '<tr>'
    row.each{|key, val| html.concat('<td>' + val.to_s + '</td>')}
    html += '</tr>'
    if i == threshold
      html += '<tr>'
      row.length.times {html.concat('<td>...</td>')}
      html += '</tr>'
    end
  end
  html += '</table>'
end
to_json(*args) click to toggle source
# File lib/nyaplot/data.rb, line 121
def to_json(*args)
  @rows.to_json
end
to_s() click to toggle source
# File lib/nyaplot/data.rb, line 160
def to_s
  to_html
end