Class: Charty::TableAdapters::NMatrixAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/charty/table_adapters/nmatrix_adapter.rb

Instance Attribute Summary collapse

Attributes inherited from BaseAdapter

#columns, #index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#==, #column?, #column_names, #compare_data_equality, #drop_na, #group_by, #melt, #sort_values

Constructor Details

#initialize(data, columns: nil) ⇒ NMatrixAdapter

Returns a new instance of NMatrixAdapter.



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/charty/table_adapters/nmatrix_adapter.rb', line 24

def data
  @data
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# 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

Instance Method Details

#[](row, column) ⇒ Object



34
35
36
37
38
39
40
41
# 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_lengthObject



30
31
32
# File 'lib/charty/table_adapters/nmatrix_adapter.rb', line 30

def column_length
  data.shape[1]
end

#lengthObject



26
27
28
# File 'lib/charty/table_adapters/nmatrix_adapter.rb', line 26

def length
  data.shape[0]
end