Class: Nyaplot::DataFrame

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/nyaplot/data.rb,
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) ⇒ DataFrame

Returns a new instance of DataFrame.



13
14
15
16
17
18
19
20
21
# File 'lib/nyaplot/data.rb', line 13

def initialize(*args)
  super()
  if args.length == 1 && args.first.is_a?(Mikon::DataFrame)
    @df = args.first
  else
    @df = Mikon::DataFrame.new(*args)
  end
  data(@df)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nyaplot/data.rb', line 33

def method_missing(name, *args, &block)
  if @df.respond_to? name
    ret = @df.send(name, *args, &block)
    if ret.is_a?(Mikon::DataFrame)
      self.class.new(ret)
    else
      ret
    end
  else
    super
  end
end

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



60
61
62
# File 'lib/nyaplot/data.rb', line 60

def rows
  @rows
end

Class Method Details

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

Yields:

  • (csv)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nyaplot/data.rb', line 91

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



208
209
210
# File 'lib/nyaplot/data.rb', line 208

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

#before_to_jsonObject



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

def before_to_json
  data(@rows)
end

#column(name) ⇒ Object

Access column using its label



147
148
149
150
151
# File 'lib/nyaplot/data.rb', line 147

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



212
213
214
# File 'lib/nyaplot/data.rb', line 212

def column_labels
  @rows[0].keys
end

#delete_column(name) ⇒ Object



139
140
141
142
143
144
# File 'lib/nyaplot/data.rb', line 139

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



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

def each_column(&block)
  self.column_labels.each do |label|
    block.call(column(label).to_a)
  end
end

#each_row(&block) ⇒ Object



174
175
176
177
178
# File 'lib/nyaplot/data.rb', line 174

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}


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

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

#filter!(&block) ⇒ Object

destructive version of DataFrame#filter



125
126
127
# File 'lib/nyaplot/data.rb', line 125

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

#insert_column(name, arr) ⇒ Object



134
135
136
137
# File 'lib/nyaplot/data.rb', line 134

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



156
157
158
# File 'lib/nyaplot/data.rb', line 156

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.



130
131
132
# File 'lib/nyaplot/data.rb', line 130

def name
  @uuid
end

#row(index) ⇒ Object



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

def row(index)
  @rows[index]
end

#select!(*args, &block) ⇒ Object



23
24
25
26
27
# File 'lib/nyaplot/data.rb', line 23

def select!(*args, &block)
  @df = @df.select(*args, &block)
  data(@df)
  self
end

#to_html(threshold = 15) ⇒ Object



29
30
31
# File 'lib/nyaplot/data.rb', line 29

def to_html
  @df.to_html
end

#to_sObject



203
204
205
# File 'lib/nyaplot/data.rb', line 203

def to_s
  to_html
end