class Charty::TableAdapters::NMatrixAdapter
Attributes
data[R]
Public Class Methods
new(data, columns: nil)
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 10 def initialize(data, columns: nil) case data.shape.length when 1 data = data.reshape(data.size, 1) when 2 # do nothing else raise ArgumentError, "Unsupported data format" end @data = data self.columns = Index.new(generate_column_names(data.shape[1], columns)) self.index = index || RangeIndex.new(0 ... length) end
supported?(data)
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 6 def self.supported?(data) defined?(NMatrix) && data.is_a?(NMatrix) && data.shape.length <= 2 end
Public Instance Methods
[](row, column)
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 34 def [](row, column) if row @data[row, resolve_column_index(column)] else column_data = @data[:*, resolve_column_index(column)].reshape([@data.shape[0]]) Charty::Vector.new(column_data, index: index, name: column) end end
column_length()
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 30 def column_length data.shape[1] end
length()
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 26 def length data.shape[0] end
Private Instance Methods
generate_column_names(n_columns, columns)
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 57 def generate_column_names(n_columns, columns) columns ||= [] if columns.length >= n_columns columns[0, n_columns] else columns + columns.length.upto(n_columns - 1).map {|i| "X#{i}" } end end
resolve_column_index(column)
click to toggle source
# File lib/charty/table_adapters/nmatrix_adapter.rb, line 43 def resolve_column_index(column) case column when String, Symbol index = column_names.index(column.to_sym) || column_names.index(column.to_s) return index if index raise IndexError, "invalid column name: #{column}" when Integer column else message = "column must be String or Integer: #{column.inspect}" raise ArgumentError, message end end