Class: Charty::VectorAdapters::DaruVectorAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/charty/vector_adapters/daru_adapter.rb

Instance Attribute Summary

Attributes inherited from BaseAdapter

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#==, adapter_name, #inverse_log_scale, #log_scale, #mean

Constructor Details

#initialize(data) ⇒ DaruVectorAdapter

Returns a new instance of DaruVectorAdapter.



10
11
12
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 10

def initialize(data)
  @data = check_data(data)
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 6

def self.supported?(data)
  defined?(Daru::Vector) && data.is_a?(Daru::Vector)
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
45
46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#categoriesObject



97
98
99
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 97

def categories
  data.categories.compact if categorical?
end

#compare_data_equality(other) ⇒ Object



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



128
129
130
131
# 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) ⇒ Object



133
134
135
136
137
# 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_nonnilObject



76
77
78
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 76

def first_nonnil
  data.drop_while(&:nil?).first
end

#group_by(grouper) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

#indexObject



16
17
18
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 16

def index
  DaruIndex.new(data.index)
end

#index=(new_index) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# 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

#notnullObject



139
140
141
142
# 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) ⇒ Object



154
155
156
157
# 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) ⇒ Object



146
147
148
149
150
151
152
# 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_valuesObject



101
102
103
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 101

def unique_values
  data.uniq.to_a
end

#values_at(*indices) ⇒ Object



53
54
55
# File 'lib/charty/vector_adapters/daru_adapter.rb', line 53

def values_at(*indices)
  indices.map {|i| data.at(i) }
end

#where(mask) ⇒ Object



57
58
59
60
# 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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# 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