class Charty::VectorAdapters::DaruVectorAdapter

Public Class Methods

new(data) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 10
def initialize(data)
  @data = check_data(data)
end
supported?(data) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 6
def self.supported?(data)
  defined?(Daru::Vector) && data.is_a?(Daru::Vector)
end

Public Instance Methods

[](key) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 42
def [](key)
  case key
  when Charty::Vector
    where(key)
  else
    data[key]
  end
end
boolean?() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 80
def boolean?
  case
  when numeric?, categorical?
    false
  else
    case first_nonnil
    when true, false
      true
    else
      false
    end
  end
end
categories() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 97
def categories
  data.categories.compact if categorical?
end
compare_data_equality(other) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 33
def compare_data_equality(other)
  case other
  when DaruVectorAdapter
    data == other.data
  else
    to_a == other.to_a
  end
end
drop_na() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 128
def drop_na
  values = data.reject {|x| Util.missing?(x) }
  Charty::Vector.new(Daru::Vector.new(values))
end
eq(val) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 133
def eq(val)
  Charty::Vector.new(data.eq(val).to_a,
                     index: data.index.to_a,
                     name: name)
end
first_nonnil() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 76
def first_nonnil
  data.drop_while(&:nil?).first
end
group_by(grouper) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 105
def group_by(grouper)
  case grouper
  when Daru::Vector
    if grouper.category?
      # TODO: A categorical Daru::Vector cannot perform group_by well
      grouper = Daru::Vector.new(grouper.to_a)
    end
    groups = grouper.group_by.groups
    groups.map { |g, indices|
      [g.first, Charty::Vector.new(data[*indices])]
    }.to_h
  when Charty::Vector
    case grouper.data
    when Daru::Vector
      return group_by(grouper.data)
    else
      return group_by(Daru::Vector.new(grouper.to_a))
    end
  else
    return group_by(Charty::Vector.new(grouper))
  end
end
index() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 16
def index
  DaruIndex.new(data.index)
end
index=(new_index) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 20
def index=(new_index)
  case new_index
  when DaruIndex
    data.index = new_index.values
  when Index
    data.index = new_index.to_a
  else
    data.index = new_index
  end
end
notnull() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 139
def notnull
  notnull_data = data.map {|x| ! Util.missing?(x) }
  Charty::Vector.new(notnull_data, index: data.index.to_a, name: name)
end
percentile(q) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 154
def percentile(q)
  a = data.reject_values(*Daru::MISSING_VALUES).to_a
  Statistics.percentile(a, q)
end
stdev(population: false) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 146
def stdev(population: false)
  if population
    data.standard_deviation_sample
  else
    data.standard_deviation_population
  end
end
unique_values() click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 101
def unique_values
  data.uniq.to_a
end
values_at(*indices) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 53
def values_at(*indices)
  indices.map {|i| data.at(i) }
end
where(mask) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 57
def where(mask)
  masked_data, masked_index = where_in_array(mask)
  Charty::Vector.new(Daru::Vector.new(masked_data, index: masked_index), name: name)
end
where_in_array(mask) click to toggle source
# File lib/charty/vector_adapters/daru_adapter.rb, line 62
def where_in_array(mask)
  mask = check_mask_vector(mask)
  masked_data = []
  masked_index = []
  mask.each_with_index do |f, i|
    case f
    when true, 1
      masked_data << data[i]
      masked_index << data.index.key(i)
    end
  end
  return masked_data, masked_index
end