Class: Daru::MultiIndex

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

Overview

class Index

Instance Attribute Summary collapse

Attributes inherited from Index

#relation_hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Index

__new__, #_dump, _load, coerce, inherited, new, #slice

Constructor Details

#initialize(opts = {}) ⇒ MultiIndex

Returns a new instance of MultiIndex.

Raises:

  • (ArgumentError)

309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/daru/index.rb', line 309

def initialize opts={}
  labels = opts[:labels]
  levels = opts[:levels]
  raise ArgumentError,
    'Must specify both labels and levels' unless labels && levels
  raise ArgumentError,
    'Labels and levels should be same size' if labels.size != levels.size
  raise ArgumentError,
    'Incorrect labels and levels' if incorrect_fields?(labels, levels)

  @labels = labels
  @levels = levels.map { |e| e.map.with_index.to_h }
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels


303
304
305
# File 'lib/daru/index.rb', line 303

def labels
  @labels
end

Class Method Details

.from_arrays(arrays) ⇒ Object


331
332
333
334
335
336
337
338
339
340
# File 'lib/daru/index.rb', line 331

def self.from_arrays arrays
  levels = arrays.map { |e| e.uniq.sort_by(&:to_s) }

  labels = arrays.each_with_index.map do |arry, level_index|
    level = levels[level_index]
    arry.map { |lvl| level.index(lvl) }
  end

  MultiIndex.new labels: labels, levels: levels
end

.from_tuples(tuples) ⇒ Object


342
343
344
# File 'lib/daru/index.rb', line 342

def self.from_tuples tuples
  from_arrays tuples.transpose
end

.try_from_tuples(tuples) ⇒ Object


346
347
348
349
350
351
352
# File 'lib/daru/index.rb', line 346

def self.try_from_tuples tuples
  if tuples.respond_to?(:first) && tuples.first.is_a?(Array)
    from_tuples(tuples)
  else
    nil
  end
end

Instance Method Details

#&(other) ⇒ Object


492
493
494
# File 'lib/daru/index.rb', line 492

def & other
  MultiIndex.from_tuples(to_a & other.to_a)
end

#==(other) ⇒ Object


514
515
516
517
518
# File 'lib/daru/index.rb', line 514

def == other
  self.class == other.class  &&
    labels   == other.labels &&
    levels   == other.levels
end

#[](*key) ⇒ Object


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/daru/index.rb', line 354

def [] *key
  key.flatten!
  case
  when key[0].is_a?(Range)
    retrieve_from_range(key[0])
  when key[0].is_a?(Integer) && key.size == 1
    try_retrieve_from_integer(key[0])
  else
    begin
      retrieve_from_tuples key
    rescue NoMethodError
      raise IndexError, "Specified index #{key.inspect} do not exist"
    end
  end
end

#add(*indexes) ⇒ Object


424
425
426
# File 'lib/daru/index.rb', line 424

def add *indexes
  Daru::MultiIndex.from_tuples to_a << indexes
end

#at(*positions) ⇒ object

Takes positional values and returns subset of the self

capturing the indexes at mentioned positions

Examples:

idx = Daru::MultiIndex.from_tuples [[:a, :one], [:a, :two], [:b, :one], [:b, :two]]
idx.at 0, 1
# => #<Daru::MultiIndex(2x2)>
#   a one
#     two

Parameters:

  • positional (Array<Integer>)

    values

Returns:

  • (object)

    index object


414
415
416
417
418
419
420
421
422
# File 'lib/daru/index.rb', line 414

def at *positions
  positions = preprocess_positions(*positions)
  validate_positions(*positions)
  if positions.is_a? Integer
    key(positions)
  else
    Daru::MultiIndex.from_tuples positions.map(&method(:key))
  end
end

#conform(input_indexes) ⇒ Object

Provide a MultiIndex for sub vector produced

Parameters:

  • input_indexes (Array)

    the input by user to index the vector

Returns:

  • (Object)

    the MultiIndex object for sub vector produced


542
543
544
545
# File 'lib/daru/index.rb', line 542

def conform input_indexes
  return self if input_indexes[0].is_a? Range
  drop_left_level input_indexes.size
end

#drop_left_level(by = 1) ⇒ Object


484
485
486
# File 'lib/daru/index.rb', line 484

def drop_left_level by=1
  MultiIndex.from_arrays to_a.transpose[by..-1]
end

#dupObject


480
481
482
# File 'lib/daru/index.rb', line 480

def dup
  MultiIndex.new levels: levels.dup, labels: labels
end

#each(&block) ⇒ Object


295
296
297
# File 'lib/daru/index.rb', line 295

def each(&block)
  to_a.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)

496
497
498
# File 'lib/daru/index.rb', line 496

def empty?
  @labels.flatten.empty? && @levels.all?(&:empty?)
end

#include?(tuple) ⇒ Boolean

Returns:

  • (Boolean)

500
501
502
503
504
# File 'lib/daru/index.rb', line 500

def include? tuple
  return false unless tuple.is_a? Enumerable
  tuple.flatten.each_with_index
       .all? { |tup, i| @levels[i][tup] }
end

#inspect(threshold = 20) ⇒ Object


528
529
530
531
# File 'lib/daru/index.rb', line 528

def inspect threshold=20
  "#<Daru::MultiIndex(#{size}x#{width})>\n" +
    Formatters::Table.format([], row_headers: sparse_tuples, threshold: threshold)
end

#key(index) ⇒ Object

Raises:

  • (ArgumentError)

471
472
473
474
475
476
477
478
# File 'lib/daru/index.rb', line 471

def key index
  raise ArgumentError,
    "Key #{index} is too large" if index >= @labels[0].size

  @labels
    .each_with_index
    .map { |label, i| @levels[i].keys[label[index]] }
end

#levelsObject


305
306
307
# File 'lib/daru/index.rb', line 305

def levels
  @levels.map(&:keys)
end

#map(&block) ⇒ Object


299
300
301
# File 'lib/daru/index.rb', line 299

def map(&block)
  to_a.map(&block)
end

#pos(*indexes) ⇒ Object

Note:

If the arugent is both a valid index and a valid position, it will treated as valid index

Returns positions given indexes or positions

Examples:

idx = Daru::MultiIndex.from_tuples [[:a, :one], [:a, :two], [:b, :one], [:b, :two]]
idx.pos :a
# => [0, 1]

Parameters:

  • *indexes (Array<object>)

    indexes or positions


386
387
388
389
390
391
392
393
394
# File 'lib/daru/index.rb', line 386

def pos *indexes
  if indexes.first.is_a? Integer
    return indexes.first if indexes.size == 1
    return indexes
  end
  res = self[indexes]
  return res if res.is_a? Integer
  res.map { |i| self[i] }
end

#reorder(new_order) ⇒ Object


428
429
430
431
# File 'lib/daru/index.rb', line 428

def reorder(new_order)
  from = to_a
  self.class.from_tuples(new_order.map { |i| from[i] })
end

#sizeObject


506
507
508
# File 'lib/daru/index.rb', line 506

def size
  @labels[0].size
end

#sparse_tuplesObject

Return tuples with nils in place of repeating values, like this:

:a , :bar, :one
nil, nil , :two
nil, :foo, :one

553
554
555
556
557
558
559
# File 'lib/daru/index.rb', line 553

def sparse_tuples
  tuples = to_a
  [tuples.first] + each_cons(2).map { |prev, cur|
    left = cur.zip(prev).drop_while { |c, p| c == p }
    [nil] * (cur.size - left.size) + left.map(&:first)
  }
end

#subset(*indexes) ⇒ Object


396
397
398
399
400
401
402
# File 'lib/daru/index.rb', line 396

def subset *indexes
  if indexes.first.is_a? Integer
    MultiIndex.from_tuples(indexes.map { |index| key(index) })
  else
    self[indexes].conform indexes
  end
end

#to_aObject


520
521
522
# File 'lib/daru/index.rb', line 520

def to_a
  (0...size).map { |e| key(e) }
end

#to_htmlObject


533
534
535
536
# File 'lib/daru/index.rb', line 533

def to_html
  path = File.expand_path('../iruby/templates/multi_index.html.erb', __FILE__)
  ERB.new(File.read(path).strip).result(binding)
end

#try_retrieve_from_integer(int) ⇒ Object


433
434
435
# File 'lib/daru/index.rb', line 433

def try_retrieve_from_integer int
  @levels[0].key?(int) ? retrieve_from_tuples([int]) : int
end

#valid?(*indexes) ⇒ Boolean

Returns:

  • (Boolean)

370
371
372
373
374
375
376
# File 'lib/daru/index.rb', line 370

def valid? *indexes
  # FIXME: This is perhaps not a good method
  pos(*indexes)
  return true
rescue IndexError
  return false
end

#valuesObject


524
525
526
# File 'lib/daru/index.rb', line 524

def values
  Array.new(size) { |i| i }
end

#widthObject


510
511
512
# File 'lib/daru/index.rb', line 510

def width
  @levels.size
end

#|(other) ⇒ Object


488
489
490
# File 'lib/daru/index.rb', line 488

def | other
  MultiIndex.from_tuples(to_a | other.to_a)
end