Class: Daru::CategoricalIndex
Instance Attribute Summary
Attributes inherited from Index
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compares two index object.
-
#add(*indexes) ⇒ Daru::CategoricalIndex
Add specified index values to the index object.
-
#at(*positions) ⇒ object
Takes positional values and returns subset of the self capturing the categories at mentioned positions.
-
#categories ⇒ Object
Returns array of categories.
-
#dup ⇒ Daru::CategoricalIndex
Duplicates the index object and return it.
-
#each ⇒ Enumerator
Returns enumerator enumerating all index values in the order they occur.
-
#empty? ⇒ true, false
Returns true if index object is storing no category.
-
#include?(index) ⇒ true, false
Returns true index or category is valid.
-
#index_from_pos(pos) ⇒ object
Returns index value from position.
-
#initialize(indexes) ⇒ Daru::CategoricalIndex
constructor
Create a categorical index object.
-
#pos(*indexes) ⇒ Object
Returns positions given categories or positions.
-
#size ⇒ Integer
Returns size of the index object.
-
#subset(*indexes) ⇒ Daru::CategoricalIndex
Return subset given categories or positions.
-
#to_a ⇒ Array
Returns all the index values.
-
#to_h ⇒ Hash
Returns hash table mapping category to positions at which they occur.
Methods inherited from Index
#&, #[], __new__, #_dump, _load, coerce, #conform, inherited, #inspect, #key, #map, new, #reorder, #slice, #valid?, #|
Constructor Details
#initialize(indexes) ⇒ Daru::CategoricalIndex
Create a categorical index object.
569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/daru/index.rb', line 569 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
661 662 663 664 665 |
# File 'lib/daru/index.rb', line 661 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
747 748 749 |
# File 'lib/daru/index.rb', line 747 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
730 731 732 733 734 735 736 737 738 |
# File 'lib/daru/index.rb', line 730 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 |
#categories ⇒ Object
Returns array of categories
601 602 603 |
# File 'lib/daru/index.rb', line 601 def categories @cat_hash.keys end |
#dup ⇒ Daru::CategoricalIndex
Duplicates the index object and return it
584 585 586 587 |
# File 'lib/daru/index.rb', line 584 def dup # Improve it by intializing index by hash Daru::CategoricalIndex.new to_a end |
#each ⇒ Enumerator
Returns enumerator enumerating all index values in the order they occur
646 647 648 649 650 |
# File 'lib/daru/index.rb', line 646 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
703 704 705 |
# File 'lib/daru/index.rb', line 703 def empty? @array.empty? end |
#include?(index) ⇒ true, false
Returns true index or category is valid
592 593 594 |
# File 'lib/daru/index.rb', line 592 def include? index @cat_hash.include? index end |
#index_from_pos(pos) ⇒ object
Returns index value from position
636 637 638 |
# File 'lib/daru/index.rb', line 636 def index_from_pos pos cat_from_int @array[pos] end |
#pos(*indexes) ⇒ Object
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
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/daru/index.rb', line 613 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 |
#size ⇒ Integer
Returns size of the index object
692 693 694 |
# File 'lib/daru/index.rb', line 692 def size @array.size end |
#subset(*indexes) ⇒ Daru::CategoricalIndex
Return subset given categories or positions
715 716 717 718 719 720 |
# File 'lib/daru/index.rb', line 715 def subset *indexes positions = pos(*indexes) new_index = positions.map { |pos| index_from_pos pos } Daru::CategoricalIndex.new new_index.flatten end |
#to_a ⇒ Array
Returns all the index values
672 673 674 |
# File 'lib/daru/index.rb', line 672 def to_a each.to_a end |
#to_h ⇒ Hash
Returns hash table mapping category to positions at which they occur
682 683 684 |
# File 'lib/daru/index.rb', line 682 def to_h @cat_hash end |