Class: Nyaplot::DataFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/nyaplot/data.rb

Overview

Ruby DataFrame for plotting

Constant Summary collapse

DEFAULT_OPTS =
{
  :col_sep => ',',
  :headers => true,
  :converters => :numeric,
  :header_converters => :symbol
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, name = SecureRandom.uuid()) ⇒ DataFrame

Returns a new instance of DataFrame.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# 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

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



17
18
19
# File 'lib/nyaplot/data.rb', line 17

def rows
  @rows
end

Class Method Details

.from_csv(*args) {|csv| ... } ⇒ Object

Yields:

  • (csv)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

Instance Method Details

#[](name) ⇒ Object

The alias method for DataFrame#column



165
166
167
# File 'lib/nyaplot/data.rb', line 165

def [](name)
  return self.column(name)
end

#column(name) ⇒ Object

Access column using its label



104
105
106
107
108
# 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_labelsObject



169
170
171
# File 'lib/nyaplot/data.rb', line 169

def column_labels
  @rows[0].keys
end

#delete_column(name) ⇒ Object



96
97
98
99
100
101
# 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) ⇒ Object



125
126
127
128
129
# 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) ⇒ Object



131
132
133
134
135
# File 'lib/nyaplot/data.rb', line 131

def each_row(&block)
  @rows.each do |row|
    block.call(row)
  end
end

#filter(&block) ⇒ Object

Filtering row out using recieved block

Examples:

new_df = df.filter{|row| row[:a] %2 == 0}


77
78
79
# File 'lib/nyaplot/data.rb', line 77

def filter(&block)
  DataFrame.new(@rows.select(&block))
end

#filter!(&block) ⇒ Object

destructive version of DataFrame#filter



82
83
84
# File 'lib/nyaplot/data.rb', line 82

def filter!(&block)
  @rows.select!(&block)
end

#insert_column(name, arr) ⇒ Object



91
92
93
94
# 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) ⇒ Object

Insert row using index

Parameters:

  • row (Hash)

    row to insert

  • index (Numeric) (defaults to: @rows.length)

    if not specified, the row will be inserted to the end



113
114
115
# File 'lib/nyaplot/data.rb', line 113

def insert_row(row, index=@rows.length)
  @rows.insert(index, row)
end

#nameString

Returns the name of dataframe. If not specified when initializing, uuid v4 will be set.

Returns:

  • (String)

    the name of dataframe. If not specified when initializing, uuid v4 will be set.



87
88
89
# File 'lib/nyaplot/data.rb', line 87

def name
  @name
end

#row(index) ⇒ Object



117
118
119
# File 'lib/nyaplot/data.rb', line 117

def row(index)
  @rows[index]
end

#to_html(threshold = 15) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



121
122
123
# File 'lib/nyaplot/data.rb', line 121

def to_json(*args)
  @rows.to_json
end

#to_sObject



160
161
162
# File 'lib/nyaplot/data.rb', line 160

def to_s
  to_html
end