Class: Daru::MultiIndex

Inherits:
Index
  • Object
show all
Includes:
Enumerable
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, inherited, new, #slice

Constructor Details

#initialize(opts = {}) ⇒ MultiIndex

Returns a new instance of MultiIndex.

Raises:

  • (ArgumentError)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/daru/index.rb', line 179

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| Hash[e.map.with_index.to_a] }
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.



173
174
175
# File 'lib/daru/index.rb', line 173

def labels
  @labels
end

Class Method Details

.from_arrays(arrays) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/daru/index.rb', line 204

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

  arrays.each_with_index do |arry, level_index|
    label = []
    level = levels[level_index]
    arry.each do |lvl|
      label << level.index(lvl)
    end

    labels << label
  end

  MultiIndex.new labels: labels, levels: levels
end

.from_tuples(tuples) ⇒ Object



221
222
223
# File 'lib/daru/index.rb', line 221

def self.from_tuples tuples
  from_arrays tuples.transpose
end

Instance Method Details

#&(other) ⇒ Object



307
308
309
# File 'lib/daru/index.rb', line 307

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

#==(other) ⇒ Object



331
332
333
334
335
# File 'lib/daru/index.rb', line 331

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

#[](*key) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/daru/index.rb', line 225

def [] *key
  key.flatten!
  case
  when key[0].is_a?(Range) then retrieve_from_range(key[0])
  when (key[0].is_a?(Integer) and key.size == 1) then 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

#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



353
354
355
356
# File 'lib/daru/index.rb', line 353

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



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

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

#dupObject



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

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

#each(&block) ⇒ Object



165
166
167
# File 'lib/daru/index.rb', line 165

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

#empty?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/daru/index.rb', line 311

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

#include?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


315
316
317
318
319
320
321
# File 'lib/daru/index.rb', line 315

def include? tuple
  tuple.flatten!
  tuple.each_with_index do |tup, i|
    return false unless @levels[i][tup]
  end
  true
end

#inspectObject



345
346
347
# File 'lib/daru/index.rb', line 345

def inspect
  "Daru::MultiIndex:#{object_id} (levels: #{levels}\nlabels: #{labels})"
end

#key(index) ⇒ Object

Raises:

  • (ArgumentError)


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/daru/index.rb', line 278

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

  level_indexes =
    @labels.each_with_object([]) do |label, memo|
      memo << label[index]
    end

  tuple = []
  level_indexes.each_with_index do |level_index, i|
    tuple << @levels[i].keys[level_index]
  end

  tuple
end

#levelsObject



175
176
177
# File 'lib/daru/index.rb', line 175

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

#map(&block) ⇒ Object



169
170
171
# File 'lib/daru/index.rb', line 169

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

#sizeObject



323
324
325
# File 'lib/daru/index.rb', line 323

def size
  @labels[0].size
end

#to_aObject



337
338
339
# File 'lib/daru/index.rb', line 337

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

#try_retrieve_from_integer(int) ⇒ Object



239
240
241
242
# File 'lib/daru/index.rb', line 239

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

#valuesObject



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

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

#widthObject



327
328
329
# File 'lib/daru/index.rb', line 327

def width
  @levels.size
end

#|(other) ⇒ Object



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

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