Class: Charty::TableAdapters::NArrayAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/charty/table_adapters/narray_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, #drop_na, #group_by, #melt, #sort_values

Constructor Details

#initialize(data, columns: nil, index: nil) ⇒ NArrayAdapter

Returns a new instance of NArrayAdapter.


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/charty/table_adapters/narray_adapter.rb', line 10

def initialize(data, columns: nil, index: nil)
  case data.ndim
  when 1
    data = data.reshape(data.length, 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/narray_adapter.rb', line 24

def data
  @data
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)

6
7
8
# File 'lib/charty/table_adapters/narray_adapter.rb', line 6

def self.supported?(data)
  defined?(Numo::NArray) && data.is_a?(Numo::NArray) && data.ndim <= 2
end

Instance Method Details

#[](row, column) ⇒ Object


43
44
45
46
47
48
49
50
# File 'lib/charty/table_adapters/narray_adapter.rb', line 43

def [](row, column)
  if row
    @data[row, resolve_column_index(column)]
  else
    column_data = @data[true, resolve_column_index(column)]
    Charty::Vector.new(column_data, index: index, name: column)
  end
end

#column_lengthObject


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

def column_length
  data.shape[1]
end

#compare_data_equality(other) ⇒ Object


34
35
36
37
38
39
40
41
# File 'lib/charty/table_adapters/narray_adapter.rb', line 34

def compare_data_equality(other)
  case other
  when NArrayAdapter
    data == other.data
  else
    super
  end
end

#lengthObject


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

def length
  data.shape[0]
end