class Charty::VectorAdapters::NArrayAdapter

Public Class Methods

new(data) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 13
def initialize(data)
  @data = check_data(data)
  self.index = index || RangeIndex.new(0 ... length)
end
supported?(data) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 9
def self.supported?(data)
  defined?(Numo::NArray) && data.is_a?(Numo::NArray)
end

Public Instance Methods

boolean?() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 55
def boolean?
  case data
  when Numo::Bit
    true
  when Numo::RObject
    i, n = 0, data.size
    while i < n
      case data[i]
      when nil, true, false
        # do nothing
      else
        return false
      end
      i += 1
    end
    true
  else
    false
  end
end
categorical?() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 86
def categorical?
  false
end
categories() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 90
def categories
  nil
end
compare_data_equality(other) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 18
def compare_data_equality(other)
  case other
  when ArrayAdapter, NArrayAdapter
    data == other.data
  when NumpyAdapter, PandasSeriesAdapter
    other.compare_data_equality(self)
  else
    data == other.to_a
  end
end
drop_na() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 131
def drop_na
  case data
  when Numo::DFloat, Numo::SFloat, Numo::DComplex, Numo::SComplex
    Charty::Vector.new(data[~data.isnan])
  when Numo::RObject
    where_is_nan = data.isnan
    values = []
    i, n = 0, data.size
    while i < n
      x = data[i]
      unless x.nil? || where_is_nan[i] == 1
        values << x
      end
      i += 1
    end
    Charty::Vector.new(Numo::RObject[*values])
  else
    self
  end
end
eq(val) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 152
def eq(val)
  Charty::Vector.new(data.eq(val),
                     index: index,
                     name: name)
end
group_by(grouper) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 109
def group_by(grouper)
  case grouper
  when Charty::Vector
    # nothing to do
  else
    grouper = Charty::Vector.new(grouper)
  end

  group_keys = grouper.unique_values

  case grouper.data
  when Numo::NArray
    grouper = grouper.data
  else
    grouper = Numo::NArray[*grouper.to_a]
  end

  group_keys.map { |g|
    [g, Charty::Vector.new(data[grouper.eq(g)])]
  }.to_h
end
mean() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 175
def mean
  data.mean(nan: true)
end
notnull() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 158
def notnull
  case data
  when Numo::RObject
    i, n = 0, length
    notnull_data = Numo::Bit.zeros(n)
    while i < n
      notnull_data[i] = ! Util.missing?(data[i])
      i += 1
    end
  when ->(x) { x.respond_to?(:isnan) }
    notnull_data = ~data.isnan
  else
    notnull_data = Numo::Bit.ones(length)
  end
  Charty::Vector.new(notnull_data, index: index, name: name)
end
numeric?() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 76
def numeric?
  case data
  when Numo::Bit,
       Numo::RObject
    false
  else
    true
  end
end
stdev(population: false) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 179
def stdev(population: false)
  s = data.stddev(nan: true)
  if population
    # Numo::NArray does not support population standard deviation
    n = data.isnan.sum
    s * (n - 1) / n
  else
    s
  end
end
to_a() click to toggle source
Calls superclass method
# File lib/charty/vector_adapters/narray_adapter.rb, line 32
def to_a
  case data
  when Numo::Bit
    map {|bit| bit == 1 }
  else
    super
  end
end
unique_values() click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 94
def unique_values
  existence = {}
  i, n = 0, data.size
  unique = []
  while i < n
    x = data[i]
    unless existence[x]
      unique << x
      existence[x] = true
    end
    i += 1
  end
  unique
end
where(mask) click to toggle source
# File lib/charty/vector_adapters/narray_adapter.rb, line 41
def where(mask)
  mask = check_mask_vector(mask)
  case mask.data
  when Numo::Bit
    bits = mask.data
    masked_data = data[bits]
    masked_index = bits.where.map {|i| index[i] }.to_a
  else
    masked_data, masked_index = where_in_array(mask)
    masked_data = data.class[*masked_data]
  end
  Charty::Vector.new(masked_data, index: masked_index, name: name)
end