Class: Daru::CategoricalIndex

Inherits:
Index show all
Defined in:
lib/daru/index/categorical_index.rb

Instance Attribute Summary

Attributes inherited from Index

#relation_hash

Instance Method Summary collapse

Methods inherited from Index

#&, #[], __new__, #_dump, _load, coerce, #conform, inherited, #inspect, #key, new, #reorder, #slice, #valid?, #|

Constructor Details

#initialize(indexes) ⇒ Daru::CategoricalIndex

Create a categorical index object.

Examples:

Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]
# => #<Daru::CategoricalIndex(5): {a, 1, a, 1, c}>

Parameters:

  • indexes (Array<object>)

    array of indexes


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/daru/index/categorical_index.rb', line 9

def initialize indexes
  # Create a hash to map each category to positional indexes
  categories = indexes.each_with_index.group_by(&:first)
  @cat_hash = categories.map { |cat, group| [cat, group.map(&:last)] }.to_h

  # Map each category to a unique integer for effective storage in @array
  map_cat_int = categories.keys.each_with_index.to_h

  # To link every instance to its category,
  # it stores integer for every instance representing its category
  @array = map_cat_int.values_at(*indexes)
end

Instance Method Details

#==(other) ⇒ true, false

Compares two index object. Returns true if every instance of category occur at the same position

Examples:

a = Daru::CategoricalIndex.new [:a, :a, :b]
b = Daru::CategoricalIndex.new [:b, :a, :a]
a == b
# => false

Parameters:

  • other (Daru::CateogricalIndex)

    index object to be checked against

Returns:

  • (true, false)

    true if other is similar to self


101
102
103
104
105
# File 'lib/daru/index/categorical_index.rb', line 101

def == other
  self.class == other.class &&
    size == other.size &&
    to_h == other.to_h
end

#add(*indexes) ⇒ Daru::CategoricalIndex

Add specified index values to the index object

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
idx.add :d
# => #<Daru::CategoricalIndex(6): {a, b, a, b, c, d}>

Parameters:

  • *indexes (Array<object>)

    index values to add

Returns:


187
188
189
# File 'lib/daru/index/categorical_index.rb', line 187

def add *indexes
  Daru::CategoricalIndex.new(to_a + indexes)
end

#at(*positions) ⇒ object

Takes positional values and returns subset of the self

capturing the categories at mentioned positions

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
idx.at 0, 1
# => #<Daru::CategoricalIndex(2): {a, b}>

Parameters:

  • positional (Array<Integer>)

    values

Returns:

  • (object)

    index object


170
171
172
173
174
175
176
177
178
# File 'lib/daru/index/categorical_index.rb', line 170

def at *positions
  positions = preprocess_positions(*positions)
  validate_positions(*positions)
  if positions.is_a? Integer
    index_from_pos(positions)
  else
    Daru::CategoricalIndex.new positions.map(&method(:index_from_pos))
  end
end

#categoriesObject

Returns array of categories

Examples:

x = Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]
x.categories
# => [:a, 1, :c]

41
42
43
# File 'lib/daru/index/categorical_index.rb', line 41

def categories
  @cat_hash.keys
end

#dupDaru::CategoricalIndex

Duplicates the index object and return it

Returns:


24
25
26
27
# File 'lib/daru/index/categorical_index.rb', line 24

def dup
  # Improve it by intializing index by hash
  Daru::CategoricalIndex.new to_a
end

#eachEnumerator

Returns enumerator enumerating all index values in the order they occur

Examples:

idx = Daru::CategoricalIndex.new [:a, :a, :b]
idx.each.to_a
# => [:a, :a, :b]

Returns:

  • (Enumerator)

    all index values


86
87
88
89
90
# File 'lib/daru/index/categorical_index.rb', line 86

def each
  return enum_for(:each) unless block_given?
  @array.each { |pos| yield cat_from_int pos }
  self
end

#empty?true, false

Returns true if index object is storing no category

Examples:

i = Daru::CategoricalIndex.new []
# => #<Daru::CategoricalIndex(0): {}>
i.empty?
# => true

Returns:

  • (true, false)

    true if index object is empty


143
144
145
# File 'lib/daru/index/categorical_index.rb', line 143

def empty?
  @array.empty?
end

#include?(index) ⇒ true, false

Returns true index or category is valid

Parameters:

  • index (object)

    the index value to look for

Returns:

  • (true, false)

    true if index is included, false otherwise


32
33
34
# File 'lib/daru/index/categorical_index.rb', line 32

def include? index
  @cat_hash.include? index
end

#index_from_pos(pos) ⇒ object

Returns index value from position

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
idx.index_from_pos 1
# => :b

Parameters:

  • pos (Integer)

    the position to look for

Returns:

  • (object)

    category corresponding to position


76
77
78
# File 'lib/daru/index/categorical_index.rb', line 76

def index_from_pos pos
  cat_from_int @array[pos]
end

#pos(*indexes) ⇒ Object

Note:

If the argument does not a valid category it treats it as position value and return it as it is.

Returns positions given categories or positions

Examples:

x = Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]
x.pos :a, 1
# => [0, 1, 2, 3]

Parameters:

  • *indexes (Array<object>)

    categories or positions


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/daru/index/categorical_index.rb', line 53

def pos *indexes
  positions = indexes.map do |index|
    if include? index
      @cat_hash[index]
    elsif index.is_a?(Numeric) && index < @array.size
      index
    else
      raise IndexError, "#{index.inspect} is neither a valid category"\
        ' nor a valid position'
    end
  end

  positions.flatten!
  positions.size == 1 ? positions.first : positions.sort
end

#sizeInteger

Returns size of the index object

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a]
idx.size
# => 3

Returns:

  • (Integer)

    total number of instances of all categories


132
133
134
# File 'lib/daru/index/categorical_index.rb', line 132

def size
  @array.size
end

#subset(*indexes) ⇒ Daru::CategoricalIndex

Return subset given categories or positions

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
idx.subset :a, :b
# => #<Daru::CategoricalIndex(4): {a, b, a, b}>

Parameters:

  • *indexes (Array<object>)

    categories or positions

Returns:


155
156
157
158
159
160
# File 'lib/daru/index/categorical_index.rb', line 155

def subset *indexes
  positions = pos(*indexes)
  new_index = positions.map { |pos| index_from_pos pos }

  Daru::CategoricalIndex.new new_index.flatten
end

#to_aArray

Returns all the index values

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a]
idx.to_a

Returns:

  • (Array)

    all index values


112
113
114
# File 'lib/daru/index/categorical_index.rb', line 112

def to_a
  each.to_a
end

#to_hHash

Returns hash table mapping category to positions at which they occur

Examples:

idx = Daru::CategoricalIndex.new [:a, :b, :a]
idx.to_h
# => {:a=>[0, 2], :b=>[1]}

Returns:

  • (Hash)

    hash table mapping category to array of positions


122
123
124
# File 'lib/daru/index/categorical_index.rb', line 122

def to_h
  @cat_hash
end