Class: Charty::VectorAdapters::NArrayAdapter

Inherits:
BaseAdapter
  • Object
show all
Extended by:
Forwardable
Includes:
IndexSupport, NameSupport, Enumerable
Defined in:
lib/charty/vector_adapters/narray_adapter.rb

Instance Attribute Summary

Attributes included from IndexSupport

#index

Attributes included from NameSupport

#name

Attributes inherited from BaseAdapter

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexSupport

#[], #[]=

Methods inherited from BaseAdapter

#==, adapter_name, #inverse_log_scale, #log_scale, #percentile, #values_at, #where_in_array

Constructor Details

#initialize(data) ⇒ NArrayAdapter

Returns a new instance of NArrayAdapter.


13
14
15
16
# 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

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)

9
10
11
# File 'lib/charty/vector_adapters/narray_adapter.rb', line 9

def self.supported?(data)
  defined?(Numo::NArray) && data.is_a?(Numo::NArray)
end

Instance Method Details

#boolean?Boolean

Returns:

  • (Boolean)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)

86
87
88
# File 'lib/charty/vector_adapters/narray_adapter.rb', line 86

def categorical?
  false
end

#categoriesObject


90
91
92
# File 'lib/charty/vector_adapters/narray_adapter.rb', line 90

def categories
  nil
end

#compare_data_equality(other) ⇒ Object


18
19
20
21
22
23
24
25
26
27
# 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_naObject


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object


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


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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

#meanObject


175
176
177
# File 'lib/charty/vector_adapters/narray_adapter.rb', line 175

def mean
  data.mean(nan: true)
end

#notnullObject


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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?Boolean

Returns:

  • (Boolean)

76
77
78
79
80
81
82
83
84
# 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) ⇒ Object


179
180
181
182
183
184
185
186
187
188
# 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_aObject


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


94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
# 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